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
21 #include "win32_compat.h"
26 #include <sys/statvfs.h>
28 #include <sys/ioctl.h>
30 #include <sys/socket.h>
41 #include <sys/types.h>
46 #ifdef HAVE_SYS_SOCKIO_H
47 #include <sys/sockio.h>
51 #include "libnfs-raw.h"
52 #include "libnfs-raw-mount.h"
53 #include "libnfs-raw-nfs.h"
54 #include "libnfs-private.h"
65 static void wait_for_reply(struct rpc_context
*rpc
, struct sync_cb_data
*cb_data
)
69 while (!cb_data
->is_finished
) {
71 pfd
.fd
= rpc_get_fd(rpc
);
72 pfd
.events
= rpc_which_events(rpc
);
73 if (poll(&pfd
, 1, -1) < 0) {
74 rpc_set_error(rpc
, "Poll failed");
75 cb_data
->status
= -EIO
;
78 if (rpc_service(rpc
, pfd
.revents
) < 0) {
79 rpc_set_error(rpc
, "rpc_service failed");
80 cb_data
->status
= -EIO
;
83 if (rpc_get_fd(rpc
) == -1) {
84 rpc_set_error(rpc
, "Socket closed\n");
90 static void wait_for_nfs_reply(struct nfs_context
*nfs
, struct sync_cb_data
*cb_data
)
94 while (!cb_data
->is_finished
) {
96 pfd
.fd
= nfs_get_fd(nfs
);
97 pfd
.events
= nfs_which_events(nfs
);
98 if (poll(&pfd
, 1, -1) < 0) {
99 nfs_set_error(nfs
, "Poll failed");
100 cb_data
->status
= -EIO
;
103 if (nfs_service(nfs
, pfd
.revents
) < 0) {
104 nfs_set_error(nfs
, "nfs_service failed");
105 cb_data
->status
= -EIO
;
117 * connect to the server and mount the export
119 static void mount_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
121 struct sync_cb_data
*cb_data
= private_data
;
123 cb_data
->is_finished
= 1;
124 cb_data
->status
= status
;
127 nfs_set_error(nfs
, "mount/mnt call failed with \"%s\"", (char *)data
);
132 int nfs_mount(struct nfs_context
*nfs
, const char *server
, const char *export
)
134 struct sync_cb_data cb_data
;
135 struct rpc_context
*rpc
= nfs_get_rpc_context(nfs
);
137 cb_data
.is_finished
= 0;
139 if (nfs_mount_async(nfs
, server
, export
, mount_cb
, &cb_data
) != 0) {
140 nfs_set_error(nfs
, "nfs_mount_async failed");
144 wait_for_nfs_reply(nfs
, &cb_data
);
146 /* Dont want any more callbacks even if the socket is closed */
147 rpc
->connect_cb
= NULL
;
149 return cb_data
.status
;
156 static void stat_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
158 struct sync_cb_data
*cb_data
= private_data
;
160 cb_data
->is_finished
= 1;
161 cb_data
->status
= status
;
164 nfs_set_error(nfs
, "stat call failed with \"%s\"", (char *)data
);
168 memcpy(cb_data
->return_data
, data
, sizeof(struct stat
));
171 int nfs_stat(struct nfs_context
*nfs
, const char *path
, struct stat
*st
)
173 struct sync_cb_data cb_data
;
175 cb_data
.is_finished
= 0;
176 cb_data
.return_data
= st
;
178 if (nfs_stat_async(nfs
, path
, stat_cb
, &cb_data
) != 0) {
179 nfs_set_error(nfs
, "nfs_stat_async failed");
183 wait_for_nfs_reply(nfs
, &cb_data
);
185 return cb_data
.status
;
194 static void open_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
196 struct sync_cb_data
*cb_data
= private_data
;
197 struct nfsfh
*fh
, **nfsfh
;
199 cb_data
->is_finished
= 1;
200 cb_data
->status
= status
;
203 nfs_set_error(nfs
, "open call failed with \"%s\"", (char *)data
);
208 nfsfh
= cb_data
->return_data
;
212 int nfs_open(struct nfs_context
*nfs
, const char *path
, int mode
, struct nfsfh
**nfsfh
)
214 struct sync_cb_data cb_data
;
216 cb_data
.is_finished
= 0;
217 cb_data
.return_data
= nfsfh
;
219 if (nfs_open_async(nfs
, path
, mode
, open_cb
, &cb_data
) != 0) {
220 nfs_set_error(nfs
, "nfs_open_async failed");
224 wait_for_nfs_reply(nfs
, &cb_data
);
226 return cb_data
.status
;
235 static void pread_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
237 struct sync_cb_data
*cb_data
= private_data
;
239 cb_data
->is_finished
= 1;
240 cb_data
->status
= status
;
243 nfs_set_error(nfs
, "pread call failed with \"%s\"", (char *)data
);
247 buffer
= cb_data
->return_data
;
248 memcpy(buffer
, (char *)data
, status
);
251 int nfs_pread(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, off_t offset
, size_t count
, char *buffer
)
253 struct sync_cb_data cb_data
;
255 cb_data
.is_finished
= 0;
256 cb_data
.return_data
= buffer
;
258 if (nfs_pread_async(nfs
, nfsfh
, offset
, count
, pread_cb
, &cb_data
) != 0) {
259 nfs_set_error(nfs
, "nfs_pread_async failed");
263 wait_for_nfs_reply(nfs
, &cb_data
);
265 return cb_data
.status
;
271 int nfs_read(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, size_t count
, char *buffer
)
273 return nfs_pread(nfs
, nfsfh
, nfs_get_current_offset(nfsfh
), count
, buffer
);
279 static void close_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
281 struct sync_cb_data
*cb_data
= private_data
;
282 cb_data
->is_finished
= 1;
283 cb_data
->status
= status
;
286 nfs_set_error(nfs
, "close call failed with \"%s\"", (char *)data
);
291 int nfs_close(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
)
293 struct sync_cb_data cb_data
;
295 cb_data
.is_finished
= 0;
297 if (nfs_close_async(nfs
, nfsfh
, close_cb
, &cb_data
) != 0) {
298 nfs_set_error(nfs
, "nfs_close_async failed");
302 wait_for_nfs_reply(nfs
, &cb_data
);
304 return cb_data
.status
;
313 int nfs_fstat(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, struct stat
*st
)
315 struct sync_cb_data cb_data
;
317 cb_data
.is_finished
= 0;
318 cb_data
.return_data
= st
;
320 if (nfs_fstat_async(nfs
, nfsfh
, stat_cb
, &cb_data
) != 0) {
321 nfs_set_error(nfs
, "nfs_fstat_async failed");
325 wait_for_nfs_reply(nfs
, &cb_data
);
327 return cb_data
.status
;
334 static void pwrite_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
336 struct sync_cb_data
*cb_data
= private_data
;
337 cb_data
->is_finished
= 1;
338 cb_data
->status
= status
;
341 nfs_set_error(nfs
, "pwrite call failed with \"%s\"", (char *)data
);
346 int nfs_pwrite(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, off_t offset
, size_t count
, char *buf
)
348 struct sync_cb_data cb_data
;
350 cb_data
.is_finished
= 0;
352 if (nfs_pwrite_async(nfs
, nfsfh
, offset
, count
, buf
, pwrite_cb
, &cb_data
) != 0) {
353 nfs_set_error(nfs
, "nfs_pwrite_async failed");
357 wait_for_nfs_reply(nfs
, &cb_data
);
359 return cb_data
.status
;
365 int nfs_write(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, size_t count
, char *buf
)
367 return nfs_pwrite(nfs
, nfsfh
, nfs_get_current_offset(nfsfh
), count
, buf
);
374 static void fsync_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
376 struct sync_cb_data
*cb_data
= private_data
;
377 cb_data
->is_finished
= 1;
378 cb_data
->status
= status
;
381 nfs_set_error(nfs
, "fsync call failed with \"%s\"", (char *)data
);
386 int nfs_fsync(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
)
388 struct sync_cb_data cb_data
;
390 cb_data
.is_finished
= 0;
392 if (nfs_fsync_async(nfs
, nfsfh
, fsync_cb
, &cb_data
) != 0) {
393 nfs_set_error(nfs
, "nfs_fsync_async failed");
397 wait_for_nfs_reply(nfs
, &cb_data
);
399 return cb_data
.status
;
408 static void ftruncate_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
410 struct sync_cb_data
*cb_data
= private_data
;
411 cb_data
->is_finished
= 1;
412 cb_data
->status
= status
;
415 nfs_set_error(nfs
, "ftruncate call failed with \"%s\"", (char *)data
);
420 int nfs_ftruncate(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, off_t length
)
422 struct sync_cb_data cb_data
;
424 cb_data
.is_finished
= 0;
426 if (nfs_ftruncate_async(nfs
, nfsfh
, length
, ftruncate_cb
, &cb_data
) != 0) {
427 nfs_set_error(nfs
, "nfs_ftruncate_async failed");
431 wait_for_nfs_reply(nfs
, &cb_data
);
433 return cb_data
.status
;
441 static void truncate_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
443 struct sync_cb_data
*cb_data
= private_data
;
444 cb_data
->is_finished
= 1;
445 cb_data
->status
= status
;
448 nfs_set_error(nfs
, "truncate call failed with \"%s\"", (char *)data
);
453 int nfs_truncate(struct nfs_context
*nfs
, const char *path
, off_t length
)
455 struct sync_cb_data cb_data
;
457 cb_data
.is_finished
= 0;
459 if (nfs_truncate_async(nfs
, path
, length
, truncate_cb
, &cb_data
) != 0) {
460 nfs_set_error(nfs
, "nfs_ftruncate_async failed");
464 wait_for_nfs_reply(nfs
, &cb_data
);
466 return cb_data
.status
;
476 static void mkdir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
478 struct sync_cb_data
*cb_data
= private_data
;
479 cb_data
->is_finished
= 1;
480 cb_data
->status
= status
;
483 nfs_set_error(nfs
, "mkdir call failed with \"%s\"", (char *)data
);
488 int nfs_mkdir(struct nfs_context
*nfs
, const char *path
)
490 struct sync_cb_data cb_data
;
492 cb_data
.is_finished
= 0;
494 if (nfs_mkdir_async(nfs
, path
, mkdir_cb
, &cb_data
) != 0) {
495 nfs_set_error(nfs
, "nfs_mkdir_async failed");
499 wait_for_nfs_reply(nfs
, &cb_data
);
501 return cb_data
.status
;
511 static void rmdir_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
, "rmdir call failed with \"%s\"", (char *)data
);
523 int nfs_rmdir(struct nfs_context
*nfs
, const char *path
)
525 struct sync_cb_data cb_data
;
527 cb_data
.is_finished
= 0;
529 if (nfs_rmdir_async(nfs
, path
, rmdir_cb
, &cb_data
) != 0) {
530 nfs_set_error(nfs
, "nfs_rmdir_async failed");
534 wait_for_nfs_reply(nfs
, &cb_data
);
536 return cb_data
.status
;
544 static void creat_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
546 struct sync_cb_data
*cb_data
= private_data
;
547 struct nfsfh
*fh
, **nfsfh
;
549 cb_data
->is_finished
= 1;
550 cb_data
->status
= status
;
553 nfs_set_error(nfs
, "creat call failed with \"%s\"", (char *)data
);
558 nfsfh
= cb_data
->return_data
;
562 int nfs_creat(struct nfs_context
*nfs
, const char *path
, int mode
, struct nfsfh
**nfsfh
)
564 struct sync_cb_data cb_data
;
566 cb_data
.is_finished
= 0;
567 cb_data
.return_data
= nfsfh
;
569 if (nfs_creat_async(nfs
, path
, mode
, creat_cb
, &cb_data
) != 0) {
570 nfs_set_error(nfs
, "nfs_creat_async failed");
574 wait_for_nfs_reply(nfs
, &cb_data
);
576 return cb_data
.status
;
585 static void unlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
587 struct sync_cb_data
*cb_data
= private_data
;
589 cb_data
->is_finished
= 1;
590 cb_data
->status
= status
;
593 nfs_set_error(nfs
, "unlink call failed with \"%s\"", (char *)data
);
598 int nfs_unlink(struct nfs_context
*nfs
, const char *path
)
600 struct sync_cb_data cb_data
;
602 cb_data
.is_finished
= 0;
604 if (nfs_unlink_async(nfs
, path
, unlink_cb
, &cb_data
) != 0) {
605 nfs_set_error(nfs
, "nfs_unlink_async failed");
609 wait_for_nfs_reply(nfs
, &cb_data
);
611 return cb_data
.status
;
619 static void opendir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
621 struct sync_cb_data
*cb_data
= private_data
;
622 struct nfsdir
*dir
, **nfsdir
;
624 cb_data
->is_finished
= 1;
625 cb_data
->status
= status
;
628 nfs_set_error(nfs
, "opendir call failed with \"%s\"", (char *)data
);
633 nfsdir
= cb_data
->return_data
;
637 int nfs_opendir(struct nfs_context
*nfs
, const char *path
, struct nfsdir
**nfsdir
)
639 struct sync_cb_data cb_data
;
641 cb_data
.is_finished
= 0;
642 cb_data
.return_data
= nfsdir
;
644 if (nfs_opendir_async(nfs
, path
, opendir_cb
, &cb_data
) != 0) {
645 nfs_set_error(nfs
, "nfs_opendir_async failed");
649 wait_for_nfs_reply(nfs
, &cb_data
);
651 return cb_data
.status
;
658 static void lseek_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
660 struct sync_cb_data
*cb_data
= private_data
;
662 cb_data
->is_finished
= 1;
663 cb_data
->status
= status
;
666 nfs_set_error(nfs
, "lseek call failed with \"%s\"", (char *)data
);
670 if (cb_data
->return_data
!= NULL
) {
671 memcpy(cb_data
->return_data
, data
, sizeof(off_t
));
675 int nfs_lseek(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, off_t offset
, int whence
, off_t
*current_offset
)
677 struct sync_cb_data cb_data
;
679 cb_data
.is_finished
= 0;
680 cb_data
.return_data
= current_offset
;
682 if (nfs_lseek_async(nfs
, nfsfh
, offset
, whence
, lseek_cb
, &cb_data
) != 0) {
683 nfs_set_error(nfs
, "nfs_lseek_async failed");
687 wait_for_nfs_reply(nfs
, &cb_data
);
689 return cb_data
.status
;
697 static void statvfs_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
699 struct sync_cb_data
*cb_data
= private_data
;
701 cb_data
->is_finished
= 1;
702 cb_data
->status
= status
;
705 nfs_set_error(nfs
, "statvfs call failed with \"%s\"", (char *)data
);
709 memcpy(cb_data
->return_data
, data
, sizeof(struct statvfs
));
712 int nfs_statvfs(struct nfs_context
*nfs
, const char *path
, struct statvfs
*svfs
)
714 struct sync_cb_data cb_data
;
716 cb_data
.is_finished
= 0;
717 cb_data
.return_data
= svfs
;
719 if (nfs_statvfs_async(nfs
, path
, statvfs_cb
, &cb_data
) != 0) {
720 nfs_set_error(nfs
, "nfs_statvfs_async failed");
724 wait_for_nfs_reply(nfs
, &cb_data
);
726 return cb_data
.status
;
736 static void readlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
738 struct sync_cb_data
*cb_data
= private_data
;
740 cb_data
->is_finished
= 1;
741 cb_data
->status
= status
;
744 nfs_set_error(nfs
, "readlink call failed with \"%s\"", (char *)data
);
748 if (strlen(data
) > (size_t)cb_data
->return_int
) {
749 nfs_set_error(nfs
, "Too small buffer for readlink");
750 cb_data
->status
= -ENAMETOOLONG
;
754 memcpy(cb_data
->return_data
, data
, strlen(data
)+1);
757 int nfs_readlink(struct nfs_context
*nfs
, const char *path
, char *buf
, int bufsize
)
759 struct sync_cb_data cb_data
;
761 cb_data
.is_finished
= 0;
762 cb_data
.return_data
= buf
;
763 cb_data
.return_int
= bufsize
;
765 if (nfs_readlink_async(nfs
, path
, readlink_cb
, &cb_data
) != 0) {
766 nfs_set_error(nfs
, "nfs_readlink_async failed");
770 wait_for_nfs_reply(nfs
, &cb_data
);
772 return cb_data
.status
;
780 static void chmod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
782 struct sync_cb_data
*cb_data
= private_data
;
784 cb_data
->is_finished
= 1;
785 cb_data
->status
= status
;
788 nfs_set_error(nfs
, "chmod call failed with \"%s\"", (char *)data
);
793 int nfs_chmod(struct nfs_context
*nfs
, const char *path
, int mode
)
795 struct sync_cb_data cb_data
;
797 cb_data
.is_finished
= 0;
799 if (nfs_chmod_async(nfs
, path
, mode
, chmod_cb
, &cb_data
) != 0) {
800 nfs_set_error(nfs
, "nfs_chmod_async failed");
804 wait_for_nfs_reply(nfs
, &cb_data
);
806 return cb_data
.status
;
815 static void fchmod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
817 struct sync_cb_data
*cb_data
= private_data
;
819 cb_data
->is_finished
= 1;
820 cb_data
->status
= status
;
823 nfs_set_error(nfs
, "fchmod call failed with \"%s\"", (char *)data
);
828 int nfs_fchmod(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
)
830 struct sync_cb_data cb_data
;
832 cb_data
.is_finished
= 0;
834 if (nfs_fchmod_async(nfs
, nfsfh
, mode
, fchmod_cb
, &cb_data
) != 0) {
835 nfs_set_error(nfs
, "nfs_fchmod_async failed");
839 wait_for_nfs_reply(nfs
, &cb_data
);
841 return cb_data
.status
;
850 static void chown_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
, "chown call failed with \"%s\"", (char *)data
);
863 int nfs_chown(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
)
865 struct sync_cb_data cb_data
;
867 cb_data
.is_finished
= 0;
869 if (nfs_chown_async(nfs
, path
, uid
, gid
, chown_cb
, &cb_data
) != 0) {
870 nfs_set_error(nfs
, "nfs_chown_async failed");
874 wait_for_nfs_reply(nfs
, &cb_data
);
876 return cb_data
.status
;
882 static void fchown_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
884 struct sync_cb_data
*cb_data
= private_data
;
886 cb_data
->is_finished
= 1;
887 cb_data
->status
= status
;
890 nfs_set_error(nfs
, "fchown call failed with \"%s\"", (char *)data
);
895 int nfs_fchown(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
)
897 struct sync_cb_data cb_data
;
899 cb_data
.is_finished
= 0;
901 if (nfs_fchown_async(nfs
, nfsfh
, uid
, gid
, fchown_cb
, &cb_data
) != 0) {
902 nfs_set_error(nfs
, "nfs_fchown_async failed");
906 wait_for_nfs_reply(nfs
, &cb_data
);
908 return cb_data
.status
;
916 static void utimes_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
918 struct sync_cb_data
*cb_data
= private_data
;
920 cb_data
->is_finished
= 1;
921 cb_data
->status
= status
;
924 nfs_set_error(nfs
, "utimes call failed with \"%s\"", (char *)data
);
929 int nfs_utimes(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
)
931 struct sync_cb_data cb_data
;
933 cb_data
.is_finished
= 0;
935 if (nfs_utimes_async(nfs
, path
, times
, utimes_cb
, &cb_data
) != 0) {
936 nfs_set_error(nfs
, "nfs_utimes_async failed");
940 wait_for_nfs_reply(nfs
, &cb_data
);
942 return cb_data
.status
;
950 static void utime_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
952 struct sync_cb_data
*cb_data
= private_data
;
954 cb_data
->is_finished
= 1;
955 cb_data
->status
= status
;
958 nfs_set_error(nfs
, "utime call failed with \"%s\"", (char *)data
);
963 int nfs_utime(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
)
965 struct sync_cb_data cb_data
;
967 cb_data
.is_finished
= 0;
969 if (nfs_utime_async(nfs
, path
, times
, utime_cb
, &cb_data
) != 0) {
970 nfs_set_error(nfs
, "nfs_utimes_async failed");
974 wait_for_nfs_reply(nfs
, &cb_data
);
976 return cb_data
.status
;
985 static void access_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
987 struct sync_cb_data
*cb_data
= private_data
;
989 cb_data
->is_finished
= 1;
990 cb_data
->status
= status
;
993 nfs_set_error(nfs
, "access call failed with \"%s\"", (char *)data
);
998 int nfs_access(struct nfs_context
*nfs
, const char *path
, int mode
)
1000 struct sync_cb_data cb_data
;
1002 cb_data
.is_finished
= 0;
1004 if (nfs_access_async(nfs
, path
, mode
, access_cb
, &cb_data
) != 0) {
1005 nfs_set_error(nfs
, "nfs_access_async failed");
1009 wait_for_nfs_reply(nfs
, &cb_data
);
1011 return cb_data
.status
;
1019 static void symlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1021 struct sync_cb_data
*cb_data
= private_data
;
1023 cb_data
->is_finished
= 1;
1024 cb_data
->status
= status
;
1027 nfs_set_error(nfs
, "symlink call failed with \"%s\"", (char *)data
);
1032 int nfs_symlink(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1034 struct sync_cb_data cb_data
;
1036 cb_data
.is_finished
= 0;
1038 if (nfs_symlink_async(nfs
, oldpath
, newpath
, symlink_cb
, &cb_data
) != 0) {
1039 nfs_set_error(nfs
, "nfs_symlink_async failed");
1043 wait_for_nfs_reply(nfs
, &cb_data
);
1045 return cb_data
.status
;
1053 static void rename_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1055 struct sync_cb_data
*cb_data
= private_data
;
1057 cb_data
->is_finished
= 1;
1058 cb_data
->status
= status
;
1061 nfs_set_error(nfs
, "rename call failed with \"%s\"", (char *)data
);
1066 int nfs_rename(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1068 struct sync_cb_data cb_data
;
1070 cb_data
.is_finished
= 0;
1072 if (nfs_rename_async(nfs
, oldpath
, newpath
, rename_cb
, &cb_data
) != 0) {
1073 nfs_set_error(nfs
, "nfs_rename_async failed");
1077 wait_for_nfs_reply(nfs
, &cb_data
);
1079 return cb_data
.status
;
1087 static void link_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1089 struct sync_cb_data
*cb_data
= private_data
;
1091 cb_data
->is_finished
= 1;
1092 cb_data
->status
= status
;
1095 nfs_set_error(nfs
, "link call failed with \"%s\"", (char *)data
);
1100 int nfs_link(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1102 struct sync_cb_data cb_data
;
1104 cb_data
.is_finished
= 0;
1106 if (nfs_link_async(nfs
, oldpath
, newpath
, link_cb
, &cb_data
) != 0) {
1107 nfs_set_error(nfs
, "nfs_link_async failed");
1111 wait_for_nfs_reply(nfs
, &cb_data
);
1113 return cb_data
.status
;
1116 void mount_getexports_cb(struct rpc_context
*mount_context
, int status
, void *data
, void *private_data
)
1118 struct sync_cb_data
*cb_data
= private_data
;
1119 exports export
= *(exports
*)data
;
1121 cb_data
->is_finished
= 1;
1122 cb_data
->status
= status
;
1123 cb_data
->return_data
= NULL
;
1126 rpc_set_error(mount_context
, "mount/export call failed with \"%s\"", (char *)data
);
1130 while (export
!= NULL
) {
1133 new_export
= malloc(sizeof(*new_export
));
1134 memset(new_export
, 0, sizeof(*new_export
));
1135 new_export
->ex_dir
= strdup(export
->ex_dir
);
1136 new_export
->ex_next
= cb_data
->return_data
;
1138 cb_data
->return_data
= new_export
;
1140 export
= export
->ex_next
;
1144 struct exportnode
*mount_getexports(const char *server
)
1146 struct sync_cb_data cb_data
;
1147 struct rpc_context
*rpc
;
1150 cb_data
.is_finished
= 0;
1151 cb_data
.return_data
= NULL
;
1153 rpc
= rpc_init_context();
1154 if (mount_getexports_async(rpc
, server
, mount_getexports_cb
, &cb_data
) != 0) {
1155 rpc_destroy_context(rpc
);
1159 wait_for_reply(rpc
, &cb_data
);
1160 rpc_destroy_context(rpc
);
1162 return cb_data
.return_data
;
1165 void mount_free_export_list(struct exportnode
*exports
)
1167 struct exportnode
*tmp
;
1169 while ((tmp
= exports
)) {
1170 exports
= exports
->ex_next
;
1178 void free_nfs_srvr_list(struct nfs_server_list
*srv
)
1180 while (srv
!= NULL
) {
1181 struct nfs_server_list
*next
= srv
->next
;
1189 struct nfs_list_data
{
1191 struct nfs_server_list
*srvrs
;
1194 void callit_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
1196 struct nfs_list_data
*srv_data
= private_data
;
1197 struct sockaddr
*sin
;
1199 struct nfs_server_list
*srvr
;
1201 if (status
== RPC_STATUS_CANCEL
) {
1205 srv_data
->status
= -1;
1209 sin
= rpc_get_recv_sockaddr(rpc
);
1211 rpc_set_error(rpc
, "failed to get sockaddr in CALLIT callback");
1212 srv_data
->status
= -1;
1216 if (getnameinfo(sin
, sizeof(struct sockaddr_in
), &hostdd
[0], sizeof(hostdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
1217 rpc_set_error(rpc
, "getnameinfo failed in CALLIT callback");
1218 srv_data
->status
= -1;
1222 /* check for dupes */
1223 for (srvr
= srv_data
->srvrs
; srvr
; srvr
= srvr
->next
) {
1224 if (!strcmp(hostdd
, srvr
->addr
)) {
1229 srvr
= malloc(sizeof(struct nfs_server_list
));
1231 rpc_set_error(rpc
, "Malloc failed when allocating server structure");
1232 srv_data
->status
= -1;
1236 srvr
->addr
= strdup(hostdd
);
1237 if (srvr
->addr
== NULL
) {
1238 rpc_set_error(rpc
, "Strdup failed when allocating server structure");
1240 srv_data
->status
= -1;
1244 srvr
->next
= srv_data
->srvrs
;
1245 srv_data
->srvrs
= srvr
;
1250 static int send_nfsd_probes(struct rpc_context
*rpc
, INTERFACE_INFO
*InterfaceList
, int numIfs
, struct nfs_list_data
*data
)
1254 for(i
= 0; i
< numIfs
; i
++)
1258 unsigned long nFlags
= 0;
1260 pAddress
= (SOCKADDR
*) & (InterfaceList
[i
].iiBroadcastAddress
);
1262 if(pAddress
->sa_family
!= AF_INET
)
1265 nFlags
= InterfaceList
[i
].iiFlags
;
1267 if (!(nFlags
& IFF_UP
))
1272 if (nFlags
& IFF_LOOPBACK
)
1277 if (!(nFlags
& IFF_BROADCAST
))
1282 if (getnameinfo(pAddress
, sizeof(struct sockaddr_in
), &bcdd
[0], sizeof(bcdd
), NULL
, 0, NI_NUMERICHOST
) < 0)
1287 if (rpc_set_udp_destination(rpc
, bcdd
, 111, 1) < 0)
1292 if (rpc_pmap_callit_async(rpc
, MOUNT_PROGRAM
, 2, 0, NULL
, 0, callit_cb
, data
) < 0)
1300 struct nfs_server_list
*nfs_find_local_servers(void)
1302 struct rpc_context
*rpc
;
1303 struct nfs_list_data data
= {0, NULL
};
1304 struct timeval tv_start
, tv_current
;
1307 INTERFACE_INFO InterfaceList
[20];
1308 unsigned long nBytesReturned
;
1309 int nNumInterfaces
= 0;
1311 rpc
= rpc_init_udp_context();
1317 if (rpc_bind_udp(rpc
, "0.0.0.0", 0) < 0)
1319 rpc_destroy_context(rpc
);
1323 if (WSAIoctl(rpc_get_fd(rpc
), SIO_GET_INTERFACE_LIST
, 0, 0, &InterfaceList
, sizeof(InterfaceList
), &nBytesReturned
, 0, 0) == SOCKET_ERROR
)
1328 nNumInterfaces
= nBytesReturned
/ sizeof(INTERFACE_INFO
);
1330 for (loop
=0; loop
<3; loop
++)
1332 if (send_nfsd_probes(rpc
, InterfaceList
, nNumInterfaces
, &data
) != 0)
1334 rpc_destroy_context(rpc
);
1338 win32_gettimeofday(&tv_start
, NULL
);
1343 pfd
.fd
= rpc_get_fd(rpc
);
1344 pfd
.events
= rpc_which_events(rpc
);
1346 win32_gettimeofday(&tv_current
, NULL
);
1348 - (tv_current
.tv_sec
*1000 + tv_current
.tv_usec
/ 1000)
1349 + (tv_start
.tv_sec
*1000 + tv_start
.tv_usec
/ 1000);
1351 if (poll(&pfd
, 1, mpt
) < 0)
1353 free_nfs_srvr_list(data
.srvrs
);
1354 rpc_destroy_context(rpc
);
1357 if (pfd
.revents
== 0)
1362 if (rpc_service(rpc
, pfd
.revents
) < 0)
1369 rpc_destroy_context(rpc
);
1371 if (data
.status
!= 0)
1373 free_nfs_srvr_list(data
.srvrs
);
1380 static int send_nfsd_probes(struct rpc_context
*rpc
, struct ifconf
*ifc
, struct nfs_list_data
*data
)
1384 for (ptr
=(char *)(ifc
->ifc_buf
); ptr
< (char *)(ifc
->ifc_buf
) + ifc
->ifc_len
; ) {
1388 ifr
= (struct ifreq
*)ptr
;
1389 #ifdef HAVE_SOCKADDR_LEN
1390 if (ifr
->ifr_addr
.sa_len
> sizeof(struct sockaddr
)) {
1391 ptr
+= sizeof(ifr
->ifr_name
) + ifr
->ifr_addr
.sa_len
;
1393 ptr
+= sizeof(ifr
->ifr_name
) + sizeof(struct sockaddr
);
1396 ptr
+= sizeof(struct ifreq
);
1399 if (ifr
->ifr_addr
.sa_family
!= AF_INET
) {
1402 if (ioctl(rpc_get_fd(rpc
), SIOCGIFFLAGS
, ifr
) < 0) {
1405 if (!(ifr
->ifr_flags
& IFF_UP
)) {
1408 if (ifr
->ifr_flags
& IFF_LOOPBACK
) {
1411 if (!(ifr
->ifr_flags
& IFF_BROADCAST
)) {
1414 if (ioctl(rpc_get_fd(rpc
), SIOCGIFBRDADDR
, ifr
) < 0) {
1417 if (getnameinfo(&ifr
->ifr_broadaddr
, sizeof(struct sockaddr_in
), &bcdd
[0], sizeof(bcdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
1420 if (rpc_set_udp_destination(rpc
, bcdd
, 111, 1) < 0) {
1424 if (rpc_pmap_callit_async(rpc
, MOUNT_PROGRAM
, 2, 0, NULL
, 0, callit_cb
, data
) < 0) {
1432 struct nfs_server_list
*nfs_find_local_servers(void)
1434 struct rpc_context
*rpc
;
1435 struct nfs_list_data data
= {0, NULL
};
1436 struct timeval tv_start
, tv_current
;
1441 rpc
= rpc_init_udp_context();
1446 if (rpc_bind_udp(rpc
, "0.0.0.0", 0) < 0) {
1447 rpc_destroy_context(rpc
);
1452 /* get list of all interfaces */
1453 size
= sizeof(struct ifreq
);
1457 while(ifc
.ifc_len
> (size
- sizeof(struct ifreq
))) {
1462 ifc
.ifc_buf
= malloc(size
);
1463 memset(ifc
.ifc_buf
, 0, size
);
1464 if (ioctl(rpc_get_fd(rpc
), SIOCGIFCONF
, (caddr_t
)&ifc
) < 0) {
1465 rpc_destroy_context(rpc
);
1471 for (loop
=0; loop
<3; loop
++) {
1472 if (send_nfsd_probes(rpc
, &ifc
, &data
) != 0) {
1473 rpc_destroy_context(rpc
);
1478 gettimeofday(&tv_start
, NULL
);
1482 pfd
.fd
= rpc_get_fd(rpc
);
1483 pfd
.events
= rpc_which_events(rpc
);
1485 gettimeofday(&tv_current
, NULL
);
1487 - (tv_current
.tv_sec
*1000 + tv_current
.tv_usec
/ 1000)
1488 + (tv_start
.tv_sec
*1000 + tv_start
.tv_usec
/ 1000);
1490 if (poll(&pfd
, 1, mpt
) < 0) {
1491 free_nfs_srvr_list(data
.srvrs
);
1492 rpc_destroy_context(rpc
);
1495 if (pfd
.revents
== 0) {
1499 if (rpc_service(rpc
, pfd
.revents
) < 0) {
1506 rpc_destroy_context(rpc
);
1508 if (data
.status
!= 0) {
1509 free_nfs_srvr_list(data
.srvrs
);