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
;
459 int nfs_fstat64(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, struct nfs_stat_64
*st
)
461 struct sync_cb_data cb_data
;
463 cb_data
.is_finished
= 0;
464 cb_data
.return_data
= st
;
466 if (nfs_fstat64_async(nfs
, nfsfh
, stat64_cb
, &cb_data
) != 0) {
467 nfs_set_error(nfs
, "nfs_fstat64_async failed");
471 wait_for_nfs_reply(nfs
, &cb_data
);
473 return cb_data
.status
;
480 static void pwrite_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
482 struct sync_cb_data
*cb_data
= private_data
;
483 cb_data
->is_finished
= 1;
484 cb_data
->status
= status
;
487 nfs_set_error(nfs
, "%s call failed with \"%s\"", cb_data
->call
, (char *)data
);
490 int nfs_pwrite(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, char *buf
)
492 struct sync_cb_data cb_data
;
494 cb_data
.is_finished
= 0;
495 cb_data
.call
= "pwrite";
497 if (nfs_pwrite_async(nfs
, nfsfh
, offset
, count
, buf
, pwrite_cb
, &cb_data
) != 0) {
498 nfs_set_error(nfs
, "nfs_pwrite_async failed");
502 wait_for_nfs_reply(nfs
, &cb_data
);
504 return cb_data
.status
;
510 int nfs_write(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
)
512 struct sync_cb_data cb_data
;
514 cb_data
.is_finished
= 0;
515 cb_data
.call
= "write";
517 if (nfs_write_async(nfs
, nfsfh
, count
, buf
, pwrite_cb
, &cb_data
) != 0) {
518 nfs_set_error(nfs
, "nfs_write_async failed");
522 wait_for_nfs_reply(nfs
, &cb_data
);
524 return cb_data
.status
;
531 static void fsync_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
533 struct sync_cb_data
*cb_data
= private_data
;
534 cb_data
->is_finished
= 1;
535 cb_data
->status
= status
;
538 nfs_set_error(nfs
, "fsync call failed with \"%s\"", (char *)data
);
543 int nfs_fsync(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
)
545 struct sync_cb_data cb_data
;
547 cb_data
.is_finished
= 0;
549 if (nfs_fsync_async(nfs
, nfsfh
, fsync_cb
, &cb_data
) != 0) {
550 nfs_set_error(nfs
, "nfs_fsync_async failed");
554 wait_for_nfs_reply(nfs
, &cb_data
);
556 return cb_data
.status
;
565 static void ftruncate_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
567 struct sync_cb_data
*cb_data
= private_data
;
568 cb_data
->is_finished
= 1;
569 cb_data
->status
= status
;
572 nfs_set_error(nfs
, "ftruncate call failed with \"%s\"", (char *)data
);
577 int nfs_ftruncate(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
)
579 struct sync_cb_data cb_data
;
581 cb_data
.is_finished
= 0;
583 if (nfs_ftruncate_async(nfs
, nfsfh
, length
, ftruncate_cb
, &cb_data
) != 0) {
584 nfs_set_error(nfs
, "nfs_ftruncate_async failed");
588 wait_for_nfs_reply(nfs
, &cb_data
);
590 return cb_data
.status
;
598 static void truncate_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
600 struct sync_cb_data
*cb_data
= private_data
;
601 cb_data
->is_finished
= 1;
602 cb_data
->status
= status
;
605 nfs_set_error(nfs
, "truncate call failed with \"%s\"", (char *)data
);
610 int nfs_truncate(struct nfs_context
*nfs
, const char *path
, uint64_t length
)
612 struct sync_cb_data cb_data
;
614 cb_data
.is_finished
= 0;
616 if (nfs_truncate_async(nfs
, path
, length
, truncate_cb
, &cb_data
) != 0) {
617 nfs_set_error(nfs
, "nfs_ftruncate_async failed");
621 wait_for_nfs_reply(nfs
, &cb_data
);
623 return cb_data
.status
;
633 static void mkdir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
635 struct sync_cb_data
*cb_data
= private_data
;
636 cb_data
->is_finished
= 1;
637 cb_data
->status
= status
;
640 nfs_set_error(nfs
, "mkdir call failed with \"%s\"", (char *)data
);
645 int nfs_mkdir(struct nfs_context
*nfs
, const char *path
)
647 struct sync_cb_data cb_data
;
649 cb_data
.is_finished
= 0;
651 if (nfs_mkdir_async(nfs
, path
, mkdir_cb
, &cb_data
) != 0) {
652 nfs_set_error(nfs
, "nfs_mkdir_async failed");
656 wait_for_nfs_reply(nfs
, &cb_data
);
658 return cb_data
.status
;
668 static void rmdir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
670 struct sync_cb_data
*cb_data
= private_data
;
671 cb_data
->is_finished
= 1;
672 cb_data
->status
= status
;
675 nfs_set_error(nfs
, "rmdir call failed with \"%s\"", (char *)data
);
680 int nfs_rmdir(struct nfs_context
*nfs
, const char *path
)
682 struct sync_cb_data cb_data
;
684 cb_data
.is_finished
= 0;
686 if (nfs_rmdir_async(nfs
, path
, rmdir_cb
, &cb_data
) != 0) {
687 nfs_set_error(nfs
, "nfs_rmdir_async failed");
691 wait_for_nfs_reply(nfs
, &cb_data
);
693 return cb_data
.status
;
701 static void creat_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
703 struct sync_cb_data
*cb_data
= private_data
;
704 struct nfsfh
*fh
, **nfsfh
;
706 cb_data
->is_finished
= 1;
707 cb_data
->status
= status
;
710 nfs_set_error(nfs
, "creat call failed with \"%s\"", (char *)data
);
715 nfsfh
= cb_data
->return_data
;
719 int nfs_create(struct nfs_context
*nfs
, const char *path
, int flags
, int mode
, struct nfsfh
**nfsfh
)
721 struct sync_cb_data cb_data
;
723 cb_data
.is_finished
= 0;
724 cb_data
.return_data
= nfsfh
;
726 if (nfs_create_async(nfs
, path
, flags
, mode
, creat_cb
, &cb_data
) != 0) {
727 nfs_set_error(nfs
, "nfs_create_async failed");
731 wait_for_nfs_reply(nfs
, &cb_data
);
733 return cb_data
.status
;
736 int nfs_creat(struct nfs_context
*nfs
, const char *path
, int mode
, struct nfsfh
**nfsfh
)
738 return nfs_create(nfs
, path
, 0, mode
, nfsfh
);
744 static void mknod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
746 struct sync_cb_data
*cb_data
= private_data
;
748 cb_data
->is_finished
= 1;
749 cb_data
->status
= status
;
752 nfs_set_error(nfs
, "mknod call failed with \"%s\"", (char *)data
);
757 int nfs_mknod(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
)
759 struct sync_cb_data cb_data
;
761 cb_data
.is_finished
= 0;
763 if (nfs_mknod_async(nfs
, path
, mode
, dev
, mknod_cb
, &cb_data
) != 0) {
764 nfs_set_error(nfs
, "nfs_creat_async failed");
768 wait_for_nfs_reply(nfs
, &cb_data
);
770 return cb_data
.status
;
777 static void unlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
779 struct sync_cb_data
*cb_data
= private_data
;
781 cb_data
->is_finished
= 1;
782 cb_data
->status
= status
;
785 nfs_set_error(nfs
, "unlink call failed with \"%s\"", (char *)data
);
790 int nfs_unlink(struct nfs_context
*nfs
, const char *path
)
792 struct sync_cb_data cb_data
;
794 cb_data
.is_finished
= 0;
796 if (nfs_unlink_async(nfs
, path
, unlink_cb
, &cb_data
) != 0) {
797 nfs_set_error(nfs
, "nfs_unlink_async failed");
801 wait_for_nfs_reply(nfs
, &cb_data
);
803 return cb_data
.status
;
811 static void opendir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
813 struct sync_cb_data
*cb_data
= private_data
;
814 struct nfsdir
*dir
, **nfsdir
;
816 cb_data
->is_finished
= 1;
817 cb_data
->status
= status
;
820 nfs_set_error(nfs
, "opendir call failed with \"%s\"", (char *)data
);
825 nfsdir
= cb_data
->return_data
;
829 int nfs_opendir(struct nfs_context
*nfs
, const char *path
, struct nfsdir
**nfsdir
)
831 struct sync_cb_data cb_data
;
833 cb_data
.is_finished
= 0;
834 cb_data
.return_data
= nfsdir
;
836 if (nfs_opendir_async(nfs
, path
, opendir_cb
, &cb_data
) != 0) {
837 nfs_set_error(nfs
, "nfs_opendir_async failed");
841 wait_for_nfs_reply(nfs
, &cb_data
);
843 return cb_data
.status
;
850 static void lseek_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
852 struct sync_cb_data
*cb_data
= private_data
;
854 cb_data
->is_finished
= 1;
855 cb_data
->status
= status
;
858 nfs_set_error(nfs
, "lseek call failed with \"%s\"", (char *)data
);
862 if (cb_data
->return_data
!= NULL
) {
863 memcpy(cb_data
->return_data
, data
, sizeof(uint64_t));
867 int nfs_lseek(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int64_t offset
, int whence
, uint64_t *current_offset
)
869 struct sync_cb_data cb_data
;
871 cb_data
.is_finished
= 0;
872 cb_data
.return_data
= current_offset
;
874 if (nfs_lseek_async(nfs
, nfsfh
, offset
, whence
, lseek_cb
, &cb_data
) != 0) {
875 nfs_set_error(nfs
, "nfs_lseek_async failed");
879 wait_for_nfs_reply(nfs
, &cb_data
);
881 return cb_data
.status
;
889 static void statvfs_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
891 struct sync_cb_data
*cb_data
= private_data
;
893 cb_data
->is_finished
= 1;
894 cb_data
->status
= status
;
897 nfs_set_error(nfs
, "statvfs call failed with \"%s\"", (char *)data
);
901 memcpy(cb_data
->return_data
, data
, sizeof(struct statvfs
));
904 int nfs_statvfs(struct nfs_context
*nfs
, const char *path
, struct statvfs
*svfs
)
906 struct sync_cb_data cb_data
;
908 cb_data
.is_finished
= 0;
909 cb_data
.return_data
= svfs
;
911 if (nfs_statvfs_async(nfs
, path
, statvfs_cb
, &cb_data
) != 0) {
912 nfs_set_error(nfs
, "nfs_statvfs_async failed");
916 wait_for_nfs_reply(nfs
, &cb_data
);
918 return cb_data
.status
;
928 static void readlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
930 struct sync_cb_data
*cb_data
= private_data
;
932 cb_data
->is_finished
= 1;
933 cb_data
->status
= status
;
936 nfs_set_error(nfs
, "readlink call failed with \"%s\"", (char *)data
);
940 if (strlen(data
) > (size_t)cb_data
->return_int
) {
941 nfs_set_error(nfs
, "Too small buffer for readlink");
942 cb_data
->status
= -ENAMETOOLONG
;
946 memcpy(cb_data
->return_data
, data
, strlen(data
)+1);
949 int nfs_readlink(struct nfs_context
*nfs
, const char *path
, char *buf
, int bufsize
)
951 struct sync_cb_data cb_data
;
953 cb_data
.is_finished
= 0;
954 cb_data
.return_data
= buf
;
955 cb_data
.return_int
= bufsize
;
957 if (nfs_readlink_async(nfs
, path
, readlink_cb
, &cb_data
) != 0) {
958 nfs_set_error(nfs
, "nfs_readlink_async failed");
962 wait_for_nfs_reply(nfs
, &cb_data
);
964 return cb_data
.status
;
972 static void chmod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
974 struct sync_cb_data
*cb_data
= private_data
;
976 cb_data
->is_finished
= 1;
977 cb_data
->status
= status
;
980 nfs_set_error(nfs
, "chmod call failed with \"%s\"", (char *)data
);
985 int nfs_chmod(struct nfs_context
*nfs
, const char *path
, int mode
)
987 struct sync_cb_data cb_data
;
989 cb_data
.is_finished
= 0;
991 if (nfs_chmod_async(nfs
, path
, mode
, chmod_cb
, &cb_data
) != 0) {
992 nfs_set_error(nfs
, "nfs_chmod_async failed");
996 wait_for_nfs_reply(nfs
, &cb_data
);
998 return cb_data
.status
;
1007 static void fchmod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1009 struct sync_cb_data
*cb_data
= private_data
;
1011 cb_data
->is_finished
= 1;
1012 cb_data
->status
= status
;
1015 nfs_set_error(nfs
, "fchmod call failed with \"%s\"", (char *)data
);
1020 int nfs_fchmod(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
)
1022 struct sync_cb_data cb_data
;
1024 cb_data
.is_finished
= 0;
1026 if (nfs_fchmod_async(nfs
, nfsfh
, mode
, fchmod_cb
, &cb_data
) != 0) {
1027 nfs_set_error(nfs
, "nfs_fchmod_async failed");
1031 wait_for_nfs_reply(nfs
, &cb_data
);
1033 return cb_data
.status
;
1042 static void chown_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1044 struct sync_cb_data
*cb_data
= private_data
;
1046 cb_data
->is_finished
= 1;
1047 cb_data
->status
= status
;
1050 nfs_set_error(nfs
, "chown call failed with \"%s\"", (char *)data
);
1055 int nfs_chown(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
)
1057 struct sync_cb_data cb_data
;
1059 cb_data
.is_finished
= 0;
1061 if (nfs_chown_async(nfs
, path
, uid
, gid
, chown_cb
, &cb_data
) != 0) {
1062 nfs_set_error(nfs
, "nfs_chown_async failed");
1066 wait_for_nfs_reply(nfs
, &cb_data
);
1068 return cb_data
.status
;
1074 static void fchown_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1076 struct sync_cb_data
*cb_data
= private_data
;
1078 cb_data
->is_finished
= 1;
1079 cb_data
->status
= status
;
1082 nfs_set_error(nfs
, "fchown call failed with \"%s\"", (char *)data
);
1087 int nfs_fchown(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
)
1089 struct sync_cb_data cb_data
;
1091 cb_data
.is_finished
= 0;
1093 if (nfs_fchown_async(nfs
, nfsfh
, uid
, gid
, fchown_cb
, &cb_data
) != 0) {
1094 nfs_set_error(nfs
, "nfs_fchown_async failed");
1098 wait_for_nfs_reply(nfs
, &cb_data
);
1100 return cb_data
.status
;
1108 static void utimes_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1110 struct sync_cb_data
*cb_data
= private_data
;
1112 cb_data
->is_finished
= 1;
1113 cb_data
->status
= status
;
1116 nfs_set_error(nfs
, "utimes call failed with \"%s\"", (char *)data
);
1121 int nfs_utimes(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
)
1123 struct sync_cb_data cb_data
;
1125 cb_data
.is_finished
= 0;
1127 if (nfs_utimes_async(nfs
, path
, times
, utimes_cb
, &cb_data
) != 0) {
1128 nfs_set_error(nfs
, "nfs_utimes_async failed");
1132 wait_for_nfs_reply(nfs
, &cb_data
);
1134 return cb_data
.status
;
1142 static void utime_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1144 struct sync_cb_data
*cb_data
= private_data
;
1146 cb_data
->is_finished
= 1;
1147 cb_data
->status
= status
;
1150 nfs_set_error(nfs
, "utime call failed with \"%s\"", (char *)data
);
1155 int nfs_utime(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
)
1157 struct sync_cb_data cb_data
;
1159 cb_data
.is_finished
= 0;
1161 if (nfs_utime_async(nfs
, path
, times
, utime_cb
, &cb_data
) != 0) {
1162 nfs_set_error(nfs
, "nfs_utimes_async failed");
1166 wait_for_nfs_reply(nfs
, &cb_data
);
1168 return cb_data
.status
;
1177 static void access_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1179 struct sync_cb_data
*cb_data
= private_data
;
1181 cb_data
->is_finished
= 1;
1182 cb_data
->status
= status
;
1185 nfs_set_error(nfs
, "access call failed with \"%s\"", (char *)data
);
1190 int nfs_access(struct nfs_context
*nfs
, const char *path
, int mode
)
1192 struct sync_cb_data cb_data
;
1194 cb_data
.is_finished
= 0;
1196 if (nfs_access_async(nfs
, path
, mode
, access_cb
, &cb_data
) != 0) {
1197 nfs_set_error(nfs
, "nfs_access_async failed");
1201 wait_for_nfs_reply(nfs
, &cb_data
);
1203 return cb_data
.status
;
1211 static void symlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1213 struct sync_cb_data
*cb_data
= private_data
;
1215 cb_data
->is_finished
= 1;
1216 cb_data
->status
= status
;
1219 nfs_set_error(nfs
, "symlink call failed with \"%s\"", (char *)data
);
1224 int nfs_symlink(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1226 struct sync_cb_data cb_data
;
1228 cb_data
.is_finished
= 0;
1230 if (nfs_symlink_async(nfs
, oldpath
, newpath
, symlink_cb
, &cb_data
) != 0) {
1231 nfs_set_error(nfs
, "nfs_symlink_async failed");
1235 wait_for_nfs_reply(nfs
, &cb_data
);
1237 return cb_data
.status
;
1245 static void rename_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1247 struct sync_cb_data
*cb_data
= private_data
;
1249 cb_data
->is_finished
= 1;
1250 cb_data
->status
= status
;
1253 nfs_set_error(nfs
, "rename call failed with \"%s\"", (char *)data
);
1258 int nfs_rename(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1260 struct sync_cb_data cb_data
;
1262 cb_data
.is_finished
= 0;
1264 if (nfs_rename_async(nfs
, oldpath
, newpath
, rename_cb
, &cb_data
) != 0) {
1265 nfs_set_error(nfs
, "nfs_rename_async failed");
1269 wait_for_nfs_reply(nfs
, &cb_data
);
1271 return cb_data
.status
;
1279 static void link_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1281 struct sync_cb_data
*cb_data
= private_data
;
1283 cb_data
->is_finished
= 1;
1284 cb_data
->status
= status
;
1287 nfs_set_error(nfs
, "link call failed with \"%s\"", (char *)data
);
1292 int nfs_link(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1294 struct sync_cb_data cb_data
;
1296 cb_data
.is_finished
= 0;
1298 if (nfs_link_async(nfs
, oldpath
, newpath
, link_cb
, &cb_data
) != 0) {
1299 nfs_set_error(nfs
, "nfs_link_async failed");
1303 wait_for_nfs_reply(nfs
, &cb_data
);
1305 return cb_data
.status
;
1308 void mount_getexports_cb(struct rpc_context
*mount_context
, int status
, void *data
, void *private_data
)
1310 struct sync_cb_data
*cb_data
= private_data
;
1313 assert(mount_context
->magic
== RPC_CONTEXT_MAGIC
);
1315 cb_data
->is_finished
= 1;
1316 cb_data
->status
= status
;
1317 cb_data
->return_data
= NULL
;
1320 rpc_set_error(mount_context
, "mount/export call failed with \"%s\"", (char *)data
);
1324 export
= *(exports
*)data
;
1325 while (export
!= NULL
) {
1328 new_export
= malloc(sizeof(*new_export
));
1329 memset(new_export
, 0, sizeof(*new_export
));
1330 new_export
->ex_dir
= strdup(export
->ex_dir
);
1331 new_export
->ex_next
= cb_data
->return_data
;
1333 cb_data
->return_data
= new_export
;
1335 export
= export
->ex_next
;
1339 struct exportnode
*mount_getexports(const char *server
)
1341 struct sync_cb_data cb_data
;
1342 struct rpc_context
*rpc
;
1345 cb_data
.is_finished
= 0;
1346 cb_data
.return_data
= NULL
;
1348 rpc
= rpc_init_context();
1349 if (mount_getexports_async(rpc
, server
, mount_getexports_cb
, &cb_data
) != 0) {
1350 rpc_destroy_context(rpc
);
1354 wait_for_reply(rpc
, &cb_data
);
1355 rpc_destroy_context(rpc
);
1357 return cb_data
.return_data
;
1360 void mount_free_export_list(struct exportnode
*exports
)
1362 struct exportnode
*tmp
;
1364 while ((tmp
= exports
)) {
1365 exports
= exports
->ex_next
;
1373 void free_nfs_srvr_list(struct nfs_server_list
*srv
)
1375 while (srv
!= NULL
) {
1376 struct nfs_server_list
*next
= srv
->next
;
1384 struct nfs_list_data
{
1386 struct nfs_server_list
*srvrs
;
1389 void callit_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
1391 struct nfs_list_data
*srv_data
= private_data
;
1392 struct sockaddr
*sin
;
1394 struct nfs_server_list
*srvr
;
1396 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1398 if (status
== RPC_STATUS_CANCEL
) {
1402 srv_data
->status
= -1;
1406 sin
= rpc_get_recv_sockaddr(rpc
);
1408 rpc_set_error(rpc
, "failed to get sockaddr in CALLIT callback");
1409 srv_data
->status
= -1;
1413 if (getnameinfo(sin
, sizeof(struct sockaddr_in
), &hostdd
[0], sizeof(hostdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
1414 rpc_set_error(rpc
, "getnameinfo failed in CALLIT callback");
1415 srv_data
->status
= -1;
1419 /* check for dupes */
1420 for (srvr
= srv_data
->srvrs
; srvr
; srvr
= srvr
->next
) {
1421 if (!strcmp(hostdd
, srvr
->addr
)) {
1426 srvr
= malloc(sizeof(struct nfs_server_list
));
1428 rpc_set_error(rpc
, "Malloc failed when allocating server structure");
1429 srv_data
->status
= -1;
1433 srvr
->addr
= strdup(hostdd
);
1434 if (srvr
->addr
== NULL
) {
1435 rpc_set_error(rpc
, "Strdup failed when allocating server structure");
1437 srv_data
->status
= -1;
1441 srvr
->next
= srv_data
->srvrs
;
1442 srv_data
->srvrs
= srvr
;
1447 static int send_nfsd_probes(struct rpc_context
*rpc
, INTERFACE_INFO
*InterfaceList
, int numIfs
, struct nfs_list_data
*data
)
1451 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1453 for(i
= 0; i
< numIfs
; i
++)
1457 unsigned long nFlags
= 0;
1459 pAddress
= (SOCKADDR
*) & (InterfaceList
[i
].iiBroadcastAddress
);
1461 if(pAddress
->sa_family
!= AF_INET
)
1464 nFlags
= InterfaceList
[i
].iiFlags
;
1466 if (!(nFlags
& IFF_UP
))
1471 if (nFlags
& IFF_LOOPBACK
)
1476 if (!(nFlags
& IFF_BROADCAST
))
1481 if (getnameinfo(pAddress
, sizeof(struct sockaddr_in
), &bcdd
[0], sizeof(bcdd
), NULL
, 0, NI_NUMERICHOST
) < 0)
1486 if (rpc_set_udp_destination(rpc
, bcdd
, 111, 1) < 0)
1491 if (rpc_pmap2_callit_async(rpc
, MOUNT_PROGRAM
, 2, 0, NULL
, 0, callit_cb
, data
) < 0)
1499 struct nfs_server_list
*nfs_find_local_servers(void)
1501 struct rpc_context
*rpc
;
1502 struct nfs_list_data data
= {0, NULL
};
1503 struct timeval tv_start
, tv_current
;
1506 INTERFACE_INFO InterfaceList
[20];
1507 unsigned long nBytesReturned
;
1508 int nNumInterfaces
= 0;
1510 rpc
= rpc_init_udp_context();
1516 if (rpc_bind_udp(rpc
, "0.0.0.0", 0) < 0)
1518 rpc_destroy_context(rpc
);
1522 if (WSAIoctl(rpc_get_fd(rpc
), SIO_GET_INTERFACE_LIST
, 0, 0, &InterfaceList
, sizeof(InterfaceList
), &nBytesReturned
, 0, 0) == SOCKET_ERROR
)
1527 nNumInterfaces
= nBytesReturned
/ sizeof(INTERFACE_INFO
);
1529 for (loop
=0; loop
<3; loop
++)
1531 if (send_nfsd_probes(rpc
, InterfaceList
, nNumInterfaces
, &data
) != 0)
1533 rpc_destroy_context(rpc
);
1537 win32_gettimeofday(&tv_start
, NULL
);
1542 pfd
.fd
= rpc_get_fd(rpc
);
1543 pfd
.events
= rpc_which_events(rpc
);
1545 win32_gettimeofday(&tv_current
, NULL
);
1547 - (tv_current
.tv_sec
*1000 + tv_current
.tv_usec
/ 1000)
1548 + (tv_start
.tv_sec
*1000 + tv_start
.tv_usec
/ 1000);
1550 if (poll(&pfd
, 1, mpt
) < 0)
1552 free_nfs_srvr_list(data
.srvrs
);
1553 rpc_destroy_context(rpc
);
1556 if (pfd
.revents
== 0)
1561 if (rpc_service(rpc
, pfd
.revents
) < 0)
1568 rpc_destroy_context(rpc
);
1570 if (data
.status
!= 0)
1572 free_nfs_srvr_list(data
.srvrs
);
1579 static int send_nfsd_probes(struct rpc_context
*rpc
, struct ifconf
*ifc
, struct nfs_list_data
*data
)
1583 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1585 for (ptr
=(char *)(ifc
->ifc_buf
); ptr
< (char *)(ifc
->ifc_buf
) + ifc
->ifc_len
; ) {
1589 ifr
= (struct ifreq
*)ptr
;
1590 #ifdef HAVE_SOCKADDR_LEN
1591 if (ifr
->ifr_addr
.sa_len
> sizeof(struct sockaddr
)) {
1592 ptr
+= sizeof(ifr
->ifr_name
) + ifr
->ifr_addr
.sa_len
;
1594 ptr
+= sizeof(ifr
->ifr_name
) + sizeof(struct sockaddr
);
1597 ptr
+= sizeof(struct ifreq
);
1600 if (ifr
->ifr_addr
.sa_family
!= AF_INET
) {
1603 if (ioctl(rpc_get_fd(rpc
), SIOCGIFFLAGS
, ifr
) < 0) {
1606 if (!(ifr
->ifr_flags
& IFF_UP
)) {
1609 if (ifr
->ifr_flags
& IFF_LOOPBACK
) {
1612 if (!(ifr
->ifr_flags
& IFF_BROADCAST
)) {
1615 if (ioctl(rpc_get_fd(rpc
), SIOCGIFBRDADDR
, ifr
) < 0) {
1618 if (getnameinfo(&ifr
->ifr_broadaddr
, sizeof(struct sockaddr_in
), &bcdd
[0], sizeof(bcdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
1621 if (rpc_set_udp_destination(rpc
, bcdd
, 111, 1) < 0) {
1625 if (rpc_pmap2_callit_async(rpc
, MOUNT_PROGRAM
, 2, 0, NULL
, 0, callit_cb
, data
) < 0) {
1633 struct nfs_server_list
*nfs_find_local_servers(void)
1635 struct rpc_context
*rpc
;
1636 struct nfs_list_data data
= {0, NULL
};
1637 struct timeval tv_start
, tv_current
;
1642 rpc
= rpc_init_udp_context();
1647 if (rpc_bind_udp(rpc
, "0.0.0.0", 0) < 0) {
1648 rpc_destroy_context(rpc
);
1653 /* get list of all interfaces */
1654 size
= sizeof(struct ifreq
);
1658 while(ifc
.ifc_len
> (size
- sizeof(struct ifreq
))) {
1663 ifc
.ifc_buf
= malloc(size
);
1664 memset(ifc
.ifc_buf
, 0, size
);
1665 if (ioctl(rpc_get_fd(rpc
), SIOCGIFCONF
, (caddr_t
)&ifc
) < 0) {
1666 rpc_destroy_context(rpc
);
1672 for (loop
=0; loop
<3; loop
++) {
1673 if (send_nfsd_probes(rpc
, &ifc
, &data
) != 0) {
1674 rpc_destroy_context(rpc
);
1679 gettimeofday(&tv_start
, NULL
);
1683 pfd
.fd
= rpc_get_fd(rpc
);
1684 pfd
.events
= rpc_which_events(rpc
);
1686 gettimeofday(&tv_current
, NULL
);
1688 - (tv_current
.tv_sec
*1000 + tv_current
.tv_usec
/ 1000)
1689 + (tv_start
.tv_sec
*1000 + tv_start
.tv_usec
/ 1000);
1691 if (poll(&pfd
, 1, mpt
) < 0) {
1692 free_nfs_srvr_list(data
.srvrs
);
1693 rpc_destroy_context(rpc
);
1696 if (pfd
.revents
== 0) {
1700 if (rpc_service(rpc
, pfd
.revents
) < 0) {
1707 rpc_destroy_context(rpc
);
1709 if (data
.status
!= 0) {
1710 free_nfs_srvr_list(data
.srvrs
);