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
;
243 static void open_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
245 struct sync_cb_data
*cb_data
= private_data
;
246 struct nfsfh
*fh
, **nfsfh
;
248 cb_data
->is_finished
= 1;
249 cb_data
->status
= status
;
252 nfs_set_error(nfs
, "open call failed with \"%s\"", (char *)data
);
257 nfsfh
= cb_data
->return_data
;
261 int nfs_open(struct nfs_context
*nfs
, const char *path
, int flags
, struct nfsfh
**nfsfh
)
263 struct sync_cb_data cb_data
;
265 cb_data
.is_finished
= 0;
266 cb_data
.return_data
= nfsfh
;
268 if (nfs_open_async(nfs
, path
, flags
, open_cb
, &cb_data
) != 0) {
269 nfs_set_error(nfs
, "nfs_open_async failed");
273 wait_for_nfs_reply(nfs
, &cb_data
);
275 return cb_data
.status
;
281 static void chdir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
283 struct sync_cb_data
*cb_data
= private_data
;
285 cb_data
->is_finished
= 1;
286 cb_data
->status
= status
;
289 nfs_set_error(nfs
, "chdir call failed with \"%s\"", (char *)data
);
294 int nfs_chdir(struct nfs_context
*nfs
, const char *path
)
296 struct sync_cb_data cb_data
;
298 cb_data
.is_finished
= 0;
300 if (nfs_chdir_async(nfs
, path
, chdir_cb
, &cb_data
) != 0) {
301 nfs_set_error(nfs
, "nfs_chdir_async failed with %s",
306 wait_for_nfs_reply(nfs
, &cb_data
);
308 return cb_data
.status
;
316 static void pread_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
318 struct sync_cb_data
*cb_data
= private_data
;
320 cb_data
->is_finished
= 1;
321 cb_data
->status
= status
;
324 nfs_set_error(nfs
, "pread call failed with \"%s\"", (char *)data
);
328 buffer
= cb_data
->return_data
;
329 memcpy(buffer
, (char *)data
, status
);
332 int nfs_pread(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, char *buffer
)
334 struct sync_cb_data cb_data
;
336 cb_data
.is_finished
= 0;
337 cb_data
.return_data
= buffer
;
339 if (nfs_pread_async(nfs
, nfsfh
, offset
, count
, pread_cb
, &cb_data
) != 0) {
340 nfs_set_error(nfs
, "nfs_pread_async failed");
344 wait_for_nfs_reply(nfs
, &cb_data
);
346 return cb_data
.status
;
352 int nfs_read(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buffer
)
354 return nfs_pread(nfs
, nfsfh
, nfs_get_current_offset(nfsfh
), count
, buffer
);
360 static void close_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
362 struct sync_cb_data
*cb_data
= private_data
;
363 cb_data
->is_finished
= 1;
364 cb_data
->status
= status
;
367 nfs_set_error(nfs
, "close call failed with \"%s\"", (char *)data
);
372 int nfs_close(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
)
374 struct sync_cb_data cb_data
;
376 cb_data
.is_finished
= 0;
378 if (nfs_close_async(nfs
, nfsfh
, close_cb
, &cb_data
) != 0) {
379 nfs_set_error(nfs
, "nfs_close_async failed");
383 wait_for_nfs_reply(nfs
, &cb_data
);
385 return cb_data
.status
;
394 int nfs_fstat(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, struct stat
*st
)
396 struct sync_cb_data cb_data
;
398 cb_data
.is_finished
= 0;
399 cb_data
.return_data
= st
;
401 if (nfs_fstat_async(nfs
, nfsfh
, stat_cb
, &cb_data
) != 0) {
402 nfs_set_error(nfs
, "nfs_fstat_async failed");
406 wait_for_nfs_reply(nfs
, &cb_data
);
408 return cb_data
.status
;
415 static void pwrite_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
417 struct sync_cb_data
*cb_data
= private_data
;
418 cb_data
->is_finished
= 1;
419 cb_data
->status
= status
;
422 nfs_set_error(nfs
, "pwrite call failed with \"%s\"", (char *)data
);
427 int nfs_pwrite(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, char *buf
)
429 struct sync_cb_data cb_data
;
431 cb_data
.is_finished
= 0;
433 if (nfs_pwrite_async(nfs
, nfsfh
, offset
, count
, buf
, pwrite_cb
, &cb_data
) != 0) {
434 nfs_set_error(nfs
, "nfs_pwrite_async failed");
438 wait_for_nfs_reply(nfs
, &cb_data
);
440 return cb_data
.status
;
446 int nfs_write(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
)
448 return nfs_pwrite(nfs
, nfsfh
, nfs_get_current_offset(nfsfh
), count
, buf
);
455 static void fsync_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
457 struct sync_cb_data
*cb_data
= private_data
;
458 cb_data
->is_finished
= 1;
459 cb_data
->status
= status
;
462 nfs_set_error(nfs
, "fsync call failed with \"%s\"", (char *)data
);
467 int nfs_fsync(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
)
469 struct sync_cb_data cb_data
;
471 cb_data
.is_finished
= 0;
473 if (nfs_fsync_async(nfs
, nfsfh
, fsync_cb
, &cb_data
) != 0) {
474 nfs_set_error(nfs
, "nfs_fsync_async failed");
478 wait_for_nfs_reply(nfs
, &cb_data
);
480 return cb_data
.status
;
489 static void ftruncate_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
491 struct sync_cb_data
*cb_data
= private_data
;
492 cb_data
->is_finished
= 1;
493 cb_data
->status
= status
;
496 nfs_set_error(nfs
, "ftruncate call failed with \"%s\"", (char *)data
);
501 int nfs_ftruncate(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
)
503 struct sync_cb_data cb_data
;
505 cb_data
.is_finished
= 0;
507 if (nfs_ftruncate_async(nfs
, nfsfh
, length
, ftruncate_cb
, &cb_data
) != 0) {
508 nfs_set_error(nfs
, "nfs_ftruncate_async failed");
512 wait_for_nfs_reply(nfs
, &cb_data
);
514 return cb_data
.status
;
522 static void truncate_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
524 struct sync_cb_data
*cb_data
= private_data
;
525 cb_data
->is_finished
= 1;
526 cb_data
->status
= status
;
529 nfs_set_error(nfs
, "truncate call failed with \"%s\"", (char *)data
);
534 int nfs_truncate(struct nfs_context
*nfs
, const char *path
, uint64_t length
)
536 struct sync_cb_data cb_data
;
538 cb_data
.is_finished
= 0;
540 if (nfs_truncate_async(nfs
, path
, length
, truncate_cb
, &cb_data
) != 0) {
541 nfs_set_error(nfs
, "nfs_ftruncate_async failed");
545 wait_for_nfs_reply(nfs
, &cb_data
);
547 return cb_data
.status
;
557 static void mkdir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
559 struct sync_cb_data
*cb_data
= private_data
;
560 cb_data
->is_finished
= 1;
561 cb_data
->status
= status
;
564 nfs_set_error(nfs
, "mkdir call failed with \"%s\"", (char *)data
);
569 int nfs_mkdir(struct nfs_context
*nfs
, const char *path
)
571 struct sync_cb_data cb_data
;
573 cb_data
.is_finished
= 0;
575 if (nfs_mkdir_async(nfs
, path
, mkdir_cb
, &cb_data
) != 0) {
576 nfs_set_error(nfs
, "nfs_mkdir_async failed");
580 wait_for_nfs_reply(nfs
, &cb_data
);
582 return cb_data
.status
;
592 static void rmdir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
594 struct sync_cb_data
*cb_data
= private_data
;
595 cb_data
->is_finished
= 1;
596 cb_data
->status
= status
;
599 nfs_set_error(nfs
, "rmdir call failed with \"%s\"", (char *)data
);
604 int nfs_rmdir(struct nfs_context
*nfs
, const char *path
)
606 struct sync_cb_data cb_data
;
608 cb_data
.is_finished
= 0;
610 if (nfs_rmdir_async(nfs
, path
, rmdir_cb
, &cb_data
) != 0) {
611 nfs_set_error(nfs
, "nfs_rmdir_async failed");
615 wait_for_nfs_reply(nfs
, &cb_data
);
617 return cb_data
.status
;
625 static void creat_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
627 struct sync_cb_data
*cb_data
= private_data
;
628 struct nfsfh
*fh
, **nfsfh
;
630 cb_data
->is_finished
= 1;
631 cb_data
->status
= status
;
634 nfs_set_error(nfs
, "creat call failed with \"%s\"", (char *)data
);
639 nfsfh
= cb_data
->return_data
;
643 int nfs_creat(struct nfs_context
*nfs
, const char *path
, int mode
, struct nfsfh
**nfsfh
)
645 struct sync_cb_data cb_data
;
647 cb_data
.is_finished
= 0;
648 cb_data
.return_data
= nfsfh
;
650 if (nfs_creat_async(nfs
, path
, mode
, creat_cb
, &cb_data
) != 0) {
651 nfs_set_error(nfs
, "nfs_creat_async failed");
655 wait_for_nfs_reply(nfs
, &cb_data
);
657 return cb_data
.status
;
663 static void mknod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
665 struct sync_cb_data
*cb_data
= private_data
;
667 cb_data
->is_finished
= 1;
668 cb_data
->status
= status
;
671 nfs_set_error(nfs
, "mknod call failed with \"%s\"", (char *)data
);
676 int nfs_mknod(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
)
678 struct sync_cb_data cb_data
;
680 cb_data
.is_finished
= 0;
682 if (nfs_mknod_async(nfs
, path
, mode
, dev
, mknod_cb
, &cb_data
) != 0) {
683 nfs_set_error(nfs
, "nfs_creat_async failed");
687 wait_for_nfs_reply(nfs
, &cb_data
);
689 return cb_data
.status
;
696 static void unlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
698 struct sync_cb_data
*cb_data
= private_data
;
700 cb_data
->is_finished
= 1;
701 cb_data
->status
= status
;
704 nfs_set_error(nfs
, "unlink call failed with \"%s\"", (char *)data
);
709 int nfs_unlink(struct nfs_context
*nfs
, const char *path
)
711 struct sync_cb_data cb_data
;
713 cb_data
.is_finished
= 0;
715 if (nfs_unlink_async(nfs
, path
, unlink_cb
, &cb_data
) != 0) {
716 nfs_set_error(nfs
, "nfs_unlink_async failed");
720 wait_for_nfs_reply(nfs
, &cb_data
);
722 return cb_data
.status
;
730 static void opendir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
732 struct sync_cb_data
*cb_data
= private_data
;
733 struct nfsdir
*dir
, **nfsdir
;
735 cb_data
->is_finished
= 1;
736 cb_data
->status
= status
;
739 nfs_set_error(nfs
, "opendir call failed with \"%s\"", (char *)data
);
744 nfsdir
= cb_data
->return_data
;
748 int nfs_opendir(struct nfs_context
*nfs
, const char *path
, struct nfsdir
**nfsdir
)
750 struct sync_cb_data cb_data
;
752 cb_data
.is_finished
= 0;
753 cb_data
.return_data
= nfsdir
;
755 if (nfs_opendir_async(nfs
, path
, opendir_cb
, &cb_data
) != 0) {
756 nfs_set_error(nfs
, "nfs_opendir_async failed");
760 wait_for_nfs_reply(nfs
, &cb_data
);
762 return cb_data
.status
;
769 static void lseek_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
771 struct sync_cb_data
*cb_data
= private_data
;
773 cb_data
->is_finished
= 1;
774 cb_data
->status
= status
;
777 nfs_set_error(nfs
, "lseek call failed with \"%s\"", (char *)data
);
781 if (cb_data
->return_data
!= NULL
) {
782 memcpy(cb_data
->return_data
, data
, sizeof(uint64_t));
786 int nfs_lseek(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, uint64_t *current_offset
)
788 struct sync_cb_data cb_data
;
790 cb_data
.is_finished
= 0;
791 cb_data
.return_data
= current_offset
;
793 if (nfs_lseek_async(nfs
, nfsfh
, offset
, whence
, lseek_cb
, &cb_data
) != 0) {
794 nfs_set_error(nfs
, "nfs_lseek_async failed");
798 wait_for_nfs_reply(nfs
, &cb_data
);
800 return cb_data
.status
;
808 static void statvfs_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
810 struct sync_cb_data
*cb_data
= private_data
;
812 cb_data
->is_finished
= 1;
813 cb_data
->status
= status
;
816 nfs_set_error(nfs
, "statvfs call failed with \"%s\"", (char *)data
);
820 memcpy(cb_data
->return_data
, data
, sizeof(struct statvfs
));
823 int nfs_statvfs(struct nfs_context
*nfs
, const char *path
, struct statvfs
*svfs
)
825 struct sync_cb_data cb_data
;
827 cb_data
.is_finished
= 0;
828 cb_data
.return_data
= svfs
;
830 if (nfs_statvfs_async(nfs
, path
, statvfs_cb
, &cb_data
) != 0) {
831 nfs_set_error(nfs
, "nfs_statvfs_async failed");
835 wait_for_nfs_reply(nfs
, &cb_data
);
837 return cb_data
.status
;
847 static void readlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
849 struct sync_cb_data
*cb_data
= private_data
;
851 cb_data
->is_finished
= 1;
852 cb_data
->status
= status
;
855 nfs_set_error(nfs
, "readlink call failed with \"%s\"", (char *)data
);
859 if (strlen(data
) > (size_t)cb_data
->return_int
) {
860 nfs_set_error(nfs
, "Too small buffer for readlink");
861 cb_data
->status
= -ENAMETOOLONG
;
865 memcpy(cb_data
->return_data
, data
, strlen(data
)+1);
868 int nfs_readlink(struct nfs_context
*nfs
, const char *path
, char *buf
, int bufsize
)
870 struct sync_cb_data cb_data
;
872 cb_data
.is_finished
= 0;
873 cb_data
.return_data
= buf
;
874 cb_data
.return_int
= bufsize
;
876 if (nfs_readlink_async(nfs
, path
, readlink_cb
, &cb_data
) != 0) {
877 nfs_set_error(nfs
, "nfs_readlink_async failed");
881 wait_for_nfs_reply(nfs
, &cb_data
);
883 return cb_data
.status
;
891 static void chmod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
893 struct sync_cb_data
*cb_data
= private_data
;
895 cb_data
->is_finished
= 1;
896 cb_data
->status
= status
;
899 nfs_set_error(nfs
, "chmod call failed with \"%s\"", (char *)data
);
904 int nfs_chmod(struct nfs_context
*nfs
, const char *path
, int mode
)
906 struct sync_cb_data cb_data
;
908 cb_data
.is_finished
= 0;
910 if (nfs_chmod_async(nfs
, path
, mode
, chmod_cb
, &cb_data
) != 0) {
911 nfs_set_error(nfs
, "nfs_chmod_async failed");
915 wait_for_nfs_reply(nfs
, &cb_data
);
917 return cb_data
.status
;
926 static void fchmod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
928 struct sync_cb_data
*cb_data
= private_data
;
930 cb_data
->is_finished
= 1;
931 cb_data
->status
= status
;
934 nfs_set_error(nfs
, "fchmod call failed with \"%s\"", (char *)data
);
939 int nfs_fchmod(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
)
941 struct sync_cb_data cb_data
;
943 cb_data
.is_finished
= 0;
945 if (nfs_fchmod_async(nfs
, nfsfh
, mode
, fchmod_cb
, &cb_data
) != 0) {
946 nfs_set_error(nfs
, "nfs_fchmod_async failed");
950 wait_for_nfs_reply(nfs
, &cb_data
);
952 return cb_data
.status
;
961 static void chown_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
963 struct sync_cb_data
*cb_data
= private_data
;
965 cb_data
->is_finished
= 1;
966 cb_data
->status
= status
;
969 nfs_set_error(nfs
, "chown call failed with \"%s\"", (char *)data
);
974 int nfs_chown(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
)
976 struct sync_cb_data cb_data
;
978 cb_data
.is_finished
= 0;
980 if (nfs_chown_async(nfs
, path
, uid
, gid
, chown_cb
, &cb_data
) != 0) {
981 nfs_set_error(nfs
, "nfs_chown_async failed");
985 wait_for_nfs_reply(nfs
, &cb_data
);
987 return cb_data
.status
;
993 static void fchown_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
995 struct sync_cb_data
*cb_data
= private_data
;
997 cb_data
->is_finished
= 1;
998 cb_data
->status
= status
;
1001 nfs_set_error(nfs
, "fchown call failed with \"%s\"", (char *)data
);
1006 int nfs_fchown(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
)
1008 struct sync_cb_data cb_data
;
1010 cb_data
.is_finished
= 0;
1012 if (nfs_fchown_async(nfs
, nfsfh
, uid
, gid
, fchown_cb
, &cb_data
) != 0) {
1013 nfs_set_error(nfs
, "nfs_fchown_async failed");
1017 wait_for_nfs_reply(nfs
, &cb_data
);
1019 return cb_data
.status
;
1027 static void utimes_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1029 struct sync_cb_data
*cb_data
= private_data
;
1031 cb_data
->is_finished
= 1;
1032 cb_data
->status
= status
;
1035 nfs_set_error(nfs
, "utimes call failed with \"%s\"", (char *)data
);
1040 int nfs_utimes(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
)
1042 struct sync_cb_data cb_data
;
1044 cb_data
.is_finished
= 0;
1046 if (nfs_utimes_async(nfs
, path
, times
, utimes_cb
, &cb_data
) != 0) {
1047 nfs_set_error(nfs
, "nfs_utimes_async failed");
1051 wait_for_nfs_reply(nfs
, &cb_data
);
1053 return cb_data
.status
;
1061 static void utime_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1063 struct sync_cb_data
*cb_data
= private_data
;
1065 cb_data
->is_finished
= 1;
1066 cb_data
->status
= status
;
1069 nfs_set_error(nfs
, "utime call failed with \"%s\"", (char *)data
);
1074 int nfs_utime(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
)
1076 struct sync_cb_data cb_data
;
1078 cb_data
.is_finished
= 0;
1080 if (nfs_utime_async(nfs
, path
, times
, utime_cb
, &cb_data
) != 0) {
1081 nfs_set_error(nfs
, "nfs_utimes_async failed");
1085 wait_for_nfs_reply(nfs
, &cb_data
);
1087 return cb_data
.status
;
1096 static void access_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1098 struct sync_cb_data
*cb_data
= private_data
;
1100 cb_data
->is_finished
= 1;
1101 cb_data
->status
= status
;
1104 nfs_set_error(nfs
, "access call failed with \"%s\"", (char *)data
);
1109 int nfs_access(struct nfs_context
*nfs
, const char *path
, int mode
)
1111 struct sync_cb_data cb_data
;
1113 cb_data
.is_finished
= 0;
1115 if (nfs_access_async(nfs
, path
, mode
, access_cb
, &cb_data
) != 0) {
1116 nfs_set_error(nfs
, "nfs_access_async failed");
1120 wait_for_nfs_reply(nfs
, &cb_data
);
1122 return cb_data
.status
;
1130 static void symlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1132 struct sync_cb_data
*cb_data
= private_data
;
1134 cb_data
->is_finished
= 1;
1135 cb_data
->status
= status
;
1138 nfs_set_error(nfs
, "symlink call failed with \"%s\"", (char *)data
);
1143 int nfs_symlink(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1145 struct sync_cb_data cb_data
;
1147 cb_data
.is_finished
= 0;
1149 if (nfs_symlink_async(nfs
, oldpath
, newpath
, symlink_cb
, &cb_data
) != 0) {
1150 nfs_set_error(nfs
, "nfs_symlink_async failed");
1154 wait_for_nfs_reply(nfs
, &cb_data
);
1156 return cb_data
.status
;
1164 static void rename_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1166 struct sync_cb_data
*cb_data
= private_data
;
1168 cb_data
->is_finished
= 1;
1169 cb_data
->status
= status
;
1172 nfs_set_error(nfs
, "rename call failed with \"%s\"", (char *)data
);
1177 int nfs_rename(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1179 struct sync_cb_data cb_data
;
1181 cb_data
.is_finished
= 0;
1183 if (nfs_rename_async(nfs
, oldpath
, newpath
, rename_cb
, &cb_data
) != 0) {
1184 nfs_set_error(nfs
, "nfs_rename_async failed");
1188 wait_for_nfs_reply(nfs
, &cb_data
);
1190 return cb_data
.status
;
1198 static void link_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1200 struct sync_cb_data
*cb_data
= private_data
;
1202 cb_data
->is_finished
= 1;
1203 cb_data
->status
= status
;
1206 nfs_set_error(nfs
, "link call failed with \"%s\"", (char *)data
);
1211 int nfs_link(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1213 struct sync_cb_data cb_data
;
1215 cb_data
.is_finished
= 0;
1217 if (nfs_link_async(nfs
, oldpath
, newpath
, link_cb
, &cb_data
) != 0) {
1218 nfs_set_error(nfs
, "nfs_link_async failed");
1222 wait_for_nfs_reply(nfs
, &cb_data
);
1224 return cb_data
.status
;
1227 void mount_getexports_cb(struct rpc_context
*mount_context
, int status
, void *data
, void *private_data
)
1229 struct sync_cb_data
*cb_data
= private_data
;
1232 assert(mount_context
->magic
== RPC_CONTEXT_MAGIC
);
1234 cb_data
->is_finished
= 1;
1235 cb_data
->status
= status
;
1236 cb_data
->return_data
= NULL
;
1239 rpc_set_error(mount_context
, "mount/export call failed with \"%s\"", (char *)data
);
1243 export
= *(exports
*)data
;
1244 while (export
!= NULL
) {
1247 new_export
= malloc(sizeof(*new_export
));
1248 memset(new_export
, 0, sizeof(*new_export
));
1249 new_export
->ex_dir
= strdup(export
->ex_dir
);
1250 new_export
->ex_next
= cb_data
->return_data
;
1252 cb_data
->return_data
= new_export
;
1254 export
= export
->ex_next
;
1258 struct exportnode
*mount_getexports(const char *server
)
1260 struct sync_cb_data cb_data
;
1261 struct rpc_context
*rpc
;
1264 cb_data
.is_finished
= 0;
1265 cb_data
.return_data
= NULL
;
1267 rpc
= rpc_init_context();
1268 if (mount_getexports_async(rpc
, server
, mount_getexports_cb
, &cb_data
) != 0) {
1269 rpc_destroy_context(rpc
);
1273 wait_for_reply(rpc
, &cb_data
);
1274 rpc_destroy_context(rpc
);
1276 return cb_data
.return_data
;
1279 void mount_free_export_list(struct exportnode
*exports
)
1281 struct exportnode
*tmp
;
1283 while ((tmp
= exports
)) {
1284 exports
= exports
->ex_next
;
1292 void free_nfs_srvr_list(struct nfs_server_list
*srv
)
1294 while (srv
!= NULL
) {
1295 struct nfs_server_list
*next
= srv
->next
;
1303 struct nfs_list_data
{
1305 struct nfs_server_list
*srvrs
;
1308 void callit_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
1310 struct nfs_list_data
*srv_data
= private_data
;
1311 struct sockaddr
*sin
;
1313 struct nfs_server_list
*srvr
;
1315 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1317 if (status
== RPC_STATUS_CANCEL
) {
1321 srv_data
->status
= -1;
1325 sin
= rpc_get_recv_sockaddr(rpc
);
1327 rpc_set_error(rpc
, "failed to get sockaddr in CALLIT callback");
1328 srv_data
->status
= -1;
1332 if (getnameinfo(sin
, sizeof(struct sockaddr_in
), &hostdd
[0], sizeof(hostdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
1333 rpc_set_error(rpc
, "getnameinfo failed in CALLIT callback");
1334 srv_data
->status
= -1;
1338 /* check for dupes */
1339 for (srvr
= srv_data
->srvrs
; srvr
; srvr
= srvr
->next
) {
1340 if (!strcmp(hostdd
, srvr
->addr
)) {
1345 srvr
= malloc(sizeof(struct nfs_server_list
));
1347 rpc_set_error(rpc
, "Malloc failed when allocating server structure");
1348 srv_data
->status
= -1;
1352 srvr
->addr
= strdup(hostdd
);
1353 if (srvr
->addr
== NULL
) {
1354 rpc_set_error(rpc
, "Strdup failed when allocating server structure");
1356 srv_data
->status
= -1;
1360 srvr
->next
= srv_data
->srvrs
;
1361 srv_data
->srvrs
= srvr
;
1366 static int send_nfsd_probes(struct rpc_context
*rpc
, INTERFACE_INFO
*InterfaceList
, int numIfs
, struct nfs_list_data
*data
)
1370 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1372 for(i
= 0; i
< numIfs
; i
++)
1376 unsigned long nFlags
= 0;
1378 pAddress
= (SOCKADDR
*) & (InterfaceList
[i
].iiBroadcastAddress
);
1380 if(pAddress
->sa_family
!= AF_INET
)
1383 nFlags
= InterfaceList
[i
].iiFlags
;
1385 if (!(nFlags
& IFF_UP
))
1390 if (nFlags
& IFF_LOOPBACK
)
1395 if (!(nFlags
& IFF_BROADCAST
))
1400 if (getnameinfo(pAddress
, sizeof(struct sockaddr_in
), &bcdd
[0], sizeof(bcdd
), NULL
, 0, NI_NUMERICHOST
) < 0)
1405 if (rpc_set_udp_destination(rpc
, bcdd
, 111, 1) < 0)
1410 if (rpc_pmap_callit_async(rpc
, MOUNT_PROGRAM
, 2, 0, NULL
, 0, callit_cb
, data
) < 0)
1418 struct nfs_server_list
*nfs_find_local_servers(void)
1420 struct rpc_context
*rpc
;
1421 struct nfs_list_data data
= {0, NULL
};
1422 struct timeval tv_start
, tv_current
;
1425 INTERFACE_INFO InterfaceList
[20];
1426 unsigned long nBytesReturned
;
1427 int nNumInterfaces
= 0;
1429 rpc
= rpc_init_udp_context();
1435 if (rpc_bind_udp(rpc
, "0.0.0.0", 0) < 0)
1437 rpc_destroy_context(rpc
);
1441 if (WSAIoctl(rpc_get_fd(rpc
), SIO_GET_INTERFACE_LIST
, 0, 0, &InterfaceList
, sizeof(InterfaceList
), &nBytesReturned
, 0, 0) == SOCKET_ERROR
)
1446 nNumInterfaces
= nBytesReturned
/ sizeof(INTERFACE_INFO
);
1448 for (loop
=0; loop
<3; loop
++)
1450 if (send_nfsd_probes(rpc
, InterfaceList
, nNumInterfaces
, &data
) != 0)
1452 rpc_destroy_context(rpc
);
1456 win32_gettimeofday(&tv_start
, NULL
);
1461 pfd
.fd
= rpc_get_fd(rpc
);
1462 pfd
.events
= rpc_which_events(rpc
);
1464 win32_gettimeofday(&tv_current
, NULL
);
1466 - (tv_current
.tv_sec
*1000 + tv_current
.tv_usec
/ 1000)
1467 + (tv_start
.tv_sec
*1000 + tv_start
.tv_usec
/ 1000);
1469 if (poll(&pfd
, 1, mpt
) < 0)
1471 free_nfs_srvr_list(data
.srvrs
);
1472 rpc_destroy_context(rpc
);
1475 if (pfd
.revents
== 0)
1480 if (rpc_service(rpc
, pfd
.revents
) < 0)
1487 rpc_destroy_context(rpc
);
1489 if (data
.status
!= 0)
1491 free_nfs_srvr_list(data
.srvrs
);
1498 static int send_nfsd_probes(struct rpc_context
*rpc
, struct ifconf
*ifc
, struct nfs_list_data
*data
)
1502 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1504 for (ptr
=(char *)(ifc
->ifc_buf
); ptr
< (char *)(ifc
->ifc_buf
) + ifc
->ifc_len
; ) {
1508 ifr
= (struct ifreq
*)ptr
;
1509 #ifdef HAVE_SOCKADDR_LEN
1510 if (ifr
->ifr_addr
.sa_len
> sizeof(struct sockaddr
)) {
1511 ptr
+= sizeof(ifr
->ifr_name
) + ifr
->ifr_addr
.sa_len
;
1513 ptr
+= sizeof(ifr
->ifr_name
) + sizeof(struct sockaddr
);
1516 ptr
+= sizeof(struct ifreq
);
1519 if (ifr
->ifr_addr
.sa_family
!= AF_INET
) {
1522 if (ioctl(rpc_get_fd(rpc
), SIOCGIFFLAGS
, ifr
) < 0) {
1525 if (!(ifr
->ifr_flags
& IFF_UP
)) {
1528 if (ifr
->ifr_flags
& IFF_LOOPBACK
) {
1531 if (!(ifr
->ifr_flags
& IFF_BROADCAST
)) {
1534 if (ioctl(rpc_get_fd(rpc
), SIOCGIFBRDADDR
, ifr
) < 0) {
1537 if (getnameinfo(&ifr
->ifr_broadaddr
, sizeof(struct sockaddr_in
), &bcdd
[0], sizeof(bcdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
1540 if (rpc_set_udp_destination(rpc
, bcdd
, 111, 1) < 0) {
1544 if (rpc_pmap_callit_async(rpc
, MOUNT_PROGRAM
, 2, 0, NULL
, 0, callit_cb
, data
) < 0) {
1552 struct nfs_server_list
*nfs_find_local_servers(void)
1554 struct rpc_context
*rpc
;
1555 struct nfs_list_data data
= {0, NULL
};
1556 struct timeval tv_start
, tv_current
;
1561 rpc
= rpc_init_udp_context();
1566 if (rpc_bind_udp(rpc
, "0.0.0.0", 0) < 0) {
1567 rpc_destroy_context(rpc
);
1572 /* get list of all interfaces */
1573 size
= sizeof(struct ifreq
);
1577 while(ifc
.ifc_len
> (size
- sizeof(struct ifreq
))) {
1582 ifc
.ifc_buf
= malloc(size
);
1583 memset(ifc
.ifc_buf
, 0, size
);
1584 if (ioctl(rpc_get_fd(rpc
), SIOCGIFCONF
, (caddr_t
)&ifc
) < 0) {
1585 rpc_destroy_context(rpc
);
1591 for (loop
=0; loop
<3; loop
++) {
1592 if (send_nfsd_probes(rpc
, &ifc
, &data
) != 0) {
1593 rpc_destroy_context(rpc
);
1598 gettimeofday(&tv_start
, NULL
);
1602 pfd
.fd
= rpc_get_fd(rpc
);
1603 pfd
.events
= rpc_which_events(rpc
);
1605 gettimeofday(&tv_current
, NULL
);
1607 - (tv_current
.tv_sec
*1000 + tv_current
.tv_usec
/ 1000)
1608 + (tv_start
.tv_sec
*1000 + tv_start
.tv_usec
/ 1000);
1610 if (poll(&pfd
, 1, mpt
) < 0) {
1611 free_nfs_srvr_list(data
.srvrs
);
1612 rpc_destroy_context(rpc
);
1615 if (pfd
.revents
== 0) {
1619 if (rpc_service(rpc
, pfd
.revents
) < 0) {
1626 rpc_destroy_context(rpc
);
1628 if (data
.status
!= 0) {
1629 free_nfs_srvr_list(data
.srvrs
);