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
26 #include <sys/types.h>
28 #include <sys/statvfs.h>
29 #include <sys/ioctl.h>
34 #include <sys/socket.h>
38 #include "libnfs-raw.h"
39 #include "libnfs-raw-mount.h"
40 #include "libnfs-raw-nfs.h"
41 #include "libnfs-private.h"
52 static void wait_for_reply(struct rpc_context
*rpc
, struct sync_cb_data
*cb_data
)
57 if (cb_data
->is_finished
) {
60 pfd
.fd
= rpc_get_fd(rpc
);
61 pfd
.events
= rpc_which_events(rpc
);
63 if (poll(&pfd
, 1, -1) < 0) {
64 rpc_set_error(rpc
, "Poll failed");
65 cb_data
->status
= -EIO
;
68 if (rpc_service(rpc
, pfd
.revents
) < 0) {
69 rpc_set_error(rpc
, "rpc_service failed");
70 cb_data
->status
= -EIO
;
82 * connect to the server and mount the export
84 static void mount_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
86 struct sync_cb_data
*cb_data
= private_data
;
88 cb_data
->is_finished
= 1;
89 cb_data
->status
= status
;
92 nfs_set_error(nfs
, "mount/mnt call failed with \"%s\"", (char *)data
);
97 int nfs_mount(struct nfs_context
*nfs
, const char *server
, const char *export
)
99 struct sync_cb_data cb_data
;
101 cb_data
.is_finished
= 0;
103 if (nfs_mount_async(nfs
, server
, export
, mount_cb
, &cb_data
) != 0) {
104 nfs_set_error(nfs
, "nfs_mount_async failed");
108 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
110 return cb_data
.status
;
117 static void stat_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
119 struct sync_cb_data
*cb_data
= private_data
;
121 cb_data
->is_finished
= 1;
122 cb_data
->status
= status
;
125 nfs_set_error(nfs
, "stat call failed with \"%s\"", (char *)data
);
129 memcpy(cb_data
->return_data
, data
, sizeof(struct stat
));
132 int nfs_stat(struct nfs_context
*nfs
, const char *path
, struct stat
*st
)
134 struct sync_cb_data cb_data
;
136 cb_data
.is_finished
= 0;
137 cb_data
.return_data
= st
;
139 if (nfs_stat_async(nfs
, path
, stat_cb
, &cb_data
) != 0) {
140 nfs_set_error(nfs
, "nfs_stat_async failed");
144 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
146 return cb_data
.status
;
155 static void open_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
157 struct sync_cb_data
*cb_data
= private_data
;
158 struct nfsfh
*fh
, **nfsfh
;
160 cb_data
->is_finished
= 1;
161 cb_data
->status
= status
;
164 nfs_set_error(nfs
, "open call failed with \"%s\"", (char *)data
);
169 nfsfh
= cb_data
->return_data
;
173 int nfs_open(struct nfs_context
*nfs
, const char *path
, int mode
, struct nfsfh
**nfsfh
)
175 struct sync_cb_data cb_data
;
177 cb_data
.is_finished
= 0;
178 cb_data
.return_data
= nfsfh
;
180 if (nfs_open_async(nfs
, path
, mode
, open_cb
, &cb_data
) != 0) {
181 nfs_set_error(nfs
, "nfs_open_async failed");
185 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
187 return cb_data
.status
;
196 static void pread_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
198 struct sync_cb_data
*cb_data
= private_data
;
200 cb_data
->is_finished
= 1;
201 cb_data
->status
= status
;
204 nfs_set_error(nfs
, "pread call failed with \"%s\"", (char *)data
);
208 buffer
= cb_data
->return_data
;
209 memcpy(buffer
, (char *)data
, status
);
212 int nfs_pread(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, off_t offset
, size_t count
, char *buffer
)
214 struct sync_cb_data cb_data
;
216 cb_data
.is_finished
= 0;
217 cb_data
.return_data
= buffer
;
219 if (nfs_pread_async(nfs
, nfsfh
, offset
, count
, pread_cb
, &cb_data
) != 0) {
220 nfs_set_error(nfs
, "nfs_pread_async failed");
224 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
226 return cb_data
.status
;
232 int nfs_read(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, size_t count
, char *buffer
)
234 return nfs_pread(nfs
, nfsfh
, nfs_get_current_offset(nfsfh
), count
, buffer
);
240 static void close_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
242 struct sync_cb_data
*cb_data
= private_data
;
243 cb_data
->is_finished
= 1;
244 cb_data
->status
= status
;
247 nfs_set_error(nfs
, "close call failed with \"%s\"", (char *)data
);
252 int nfs_close(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
)
254 struct sync_cb_data cb_data
;
256 cb_data
.is_finished
= 0;
258 if (nfs_close_async(nfs
, nfsfh
, close_cb
, &cb_data
) != 0) {
259 nfs_set_error(nfs
, "nfs_close_async failed");
263 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
265 return cb_data
.status
;
274 int nfs_fstat(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, struct stat
*st
)
276 struct sync_cb_data cb_data
;
278 cb_data
.is_finished
= 0;
279 cb_data
.return_data
= st
;
281 if (nfs_fstat_async(nfs
, nfsfh
, stat_cb
, &cb_data
) != 0) {
282 nfs_set_error(nfs
, "nfs_fstat_async failed");
286 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
288 return cb_data
.status
;
295 static void pwrite_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
297 struct sync_cb_data
*cb_data
= private_data
;
298 cb_data
->is_finished
= 1;
299 cb_data
->status
= status
;
302 nfs_set_error(nfs
, "pwrite call failed with \"%s\"", (char *)data
);
307 int nfs_pwrite(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, off_t offset
, size_t count
, char *buf
)
309 struct sync_cb_data cb_data
;
311 cb_data
.is_finished
= 0;
313 if (nfs_pwrite_async(nfs
, nfsfh
, offset
, count
, buf
, pwrite_cb
, &cb_data
) != 0) {
314 nfs_set_error(nfs
, "nfs_pwrite_async failed");
318 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
320 return cb_data
.status
;
326 int nfs_write(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, size_t count
, char *buf
)
328 return nfs_pwrite(nfs
, nfsfh
, nfs_get_current_offset(nfsfh
), count
, buf
);
335 static void fsync_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
337 struct sync_cb_data
*cb_data
= private_data
;
338 cb_data
->is_finished
= 1;
339 cb_data
->status
= status
;
342 nfs_set_error(nfs
, "fsync call failed with \"%s\"", (char *)data
);
347 int nfs_fsync(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
)
349 struct sync_cb_data cb_data
;
351 cb_data
.is_finished
= 0;
353 if (nfs_fsync_async(nfs
, nfsfh
, fsync_cb
, &cb_data
) != 0) {
354 nfs_set_error(nfs
, "nfs_fsync_async failed");
358 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
360 return cb_data
.status
;
369 static void ftruncate_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
371 struct sync_cb_data
*cb_data
= private_data
;
372 cb_data
->is_finished
= 1;
373 cb_data
->status
= status
;
376 nfs_set_error(nfs
, "ftruncate call failed with \"%s\"", (char *)data
);
381 int nfs_ftruncate(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, off_t length
)
383 struct sync_cb_data cb_data
;
385 cb_data
.is_finished
= 0;
387 if (nfs_ftruncate_async(nfs
, nfsfh
, length
, ftruncate_cb
, &cb_data
) != 0) {
388 nfs_set_error(nfs
, "nfs_ftruncate_async failed");
392 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
394 return cb_data
.status
;
402 static void truncate_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
404 struct sync_cb_data
*cb_data
= private_data
;
405 cb_data
->is_finished
= 1;
406 cb_data
->status
= status
;
409 nfs_set_error(nfs
, "truncate call failed with \"%s\"", (char *)data
);
414 int nfs_truncate(struct nfs_context
*nfs
, const char *path
, off_t length
)
416 struct sync_cb_data cb_data
;
418 cb_data
.is_finished
= 0;
420 if (nfs_truncate_async(nfs
, path
, length
, truncate_cb
, &cb_data
) != 0) {
421 nfs_set_error(nfs
, "nfs_ftruncate_async failed");
425 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
427 return cb_data
.status
;
437 static void mkdir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
439 struct sync_cb_data
*cb_data
= private_data
;
440 cb_data
->is_finished
= 1;
441 cb_data
->status
= status
;
444 nfs_set_error(nfs
, "mkdir call failed with \"%s\"", (char *)data
);
449 int nfs_mkdir(struct nfs_context
*nfs
, const char *path
)
451 struct sync_cb_data cb_data
;
453 cb_data
.is_finished
= 0;
455 if (nfs_mkdir_async(nfs
, path
, mkdir_cb
, &cb_data
) != 0) {
456 nfs_set_error(nfs
, "nfs_mkdir_async failed");
460 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
462 return cb_data
.status
;
472 static void rmdir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
474 struct sync_cb_data
*cb_data
= private_data
;
475 cb_data
->is_finished
= 1;
476 cb_data
->status
= status
;
479 nfs_set_error(nfs
, "rmdir call failed with \"%s\"", (char *)data
);
484 int nfs_rmdir(struct nfs_context
*nfs
, const char *path
)
486 struct sync_cb_data cb_data
;
488 cb_data
.is_finished
= 0;
490 if (nfs_rmdir_async(nfs
, path
, rmdir_cb
, &cb_data
) != 0) {
491 nfs_set_error(nfs
, "nfs_rmdir_async failed");
495 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
497 return cb_data
.status
;
505 static void creat_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
507 struct sync_cb_data
*cb_data
= private_data
;
508 struct nfsfh
*fh
, **nfsfh
;
510 cb_data
->is_finished
= 1;
511 cb_data
->status
= status
;
514 nfs_set_error(nfs
, "creat call failed with \"%s\"", (char *)data
);
519 nfsfh
= cb_data
->return_data
;
523 int nfs_creat(struct nfs_context
*nfs
, const char *path
, int mode
, struct nfsfh
**nfsfh
)
525 struct sync_cb_data cb_data
;
527 cb_data
.is_finished
= 0;
528 cb_data
.return_data
= nfsfh
;
530 if (nfs_creat_async(nfs
, path
, mode
, creat_cb
, &cb_data
) != 0) {
531 nfs_set_error(nfs
, "nfs_creat_async failed");
535 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
537 return cb_data
.status
;
546 static void unlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
548 struct sync_cb_data
*cb_data
= private_data
;
550 cb_data
->is_finished
= 1;
551 cb_data
->status
= status
;
554 nfs_set_error(nfs
, "unlink call failed with \"%s\"", (char *)data
);
559 int nfs_unlink(struct nfs_context
*nfs
, const char *path
)
561 struct sync_cb_data cb_data
;
563 cb_data
.is_finished
= 0;
565 if (nfs_unlink_async(nfs
, path
, unlink_cb
, &cb_data
) != 0) {
566 nfs_set_error(nfs
, "nfs_unlink_async failed");
570 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
572 return cb_data
.status
;
580 static void opendir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
582 struct sync_cb_data
*cb_data
= private_data
;
583 struct nfsdir
*dir
, **nfsdir
;
585 cb_data
->is_finished
= 1;
586 cb_data
->status
= status
;
589 nfs_set_error(nfs
, "opendir call failed with \"%s\"", (char *)data
);
594 nfsdir
= cb_data
->return_data
;
598 int nfs_opendir(struct nfs_context
*nfs
, const char *path
, struct nfsdir
**nfsdir
)
600 struct sync_cb_data cb_data
;
602 cb_data
.is_finished
= 0;
603 cb_data
.return_data
= nfsdir
;
605 if (nfs_opendir_async(nfs
, path
, opendir_cb
, &cb_data
) != 0) {
606 nfs_set_error(nfs
, "nfs_opendir_async failed");
610 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
612 return cb_data
.status
;
619 static void lseek_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
621 struct sync_cb_data
*cb_data
= private_data
;
623 cb_data
->is_finished
= 1;
624 cb_data
->status
= status
;
627 nfs_set_error(nfs
, "lseek call failed with \"%s\"", (char *)data
);
631 if (cb_data
->return_data
!= NULL
) {
632 memcpy(cb_data
->return_data
, data
, sizeof(off_t
));
636 int nfs_lseek(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, off_t offset
, int whence
, off_t
*current_offset
)
638 struct sync_cb_data cb_data
;
640 cb_data
.is_finished
= 0;
641 cb_data
.return_data
= current_offset
;
643 if (nfs_lseek_async(nfs
, nfsfh
, offset
, whence
, lseek_cb
, &cb_data
) != 0) {
644 nfs_set_error(nfs
, "nfs_lseek_async failed");
648 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
650 return cb_data
.status
;
658 static void statvfs_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
, "statvfs call failed with \"%s\"", (char *)data
);
670 memcpy(cb_data
->return_data
, data
, sizeof(struct statvfs
));
673 int nfs_statvfs(struct nfs_context
*nfs
, const char *path
, struct statvfs
*svfs
)
675 struct sync_cb_data cb_data
;
677 cb_data
.is_finished
= 0;
678 cb_data
.return_data
= svfs
;
680 if (nfs_statvfs_async(nfs
, path
, statvfs_cb
, &cb_data
) != 0) {
681 nfs_set_error(nfs
, "nfs_statvfs_async failed");
685 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
687 return cb_data
.status
;
697 static void readlink_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
, "readlink call failed with \"%s\"", (char *)data
);
709 if (strlen(data
) > (size_t)cb_data
->return_int
) {
710 nfs_set_error(nfs
, "Too small buffer for readlink");
711 cb_data
->status
= -ENAMETOOLONG
;
715 memcpy(cb_data
->return_data
, data
, strlen(data
)+1);
718 int nfs_readlink(struct nfs_context
*nfs
, const char *path
, char *buf
, int bufsize
)
720 struct sync_cb_data cb_data
;
722 cb_data
.is_finished
= 0;
723 cb_data
.return_data
= buf
;
724 cb_data
.return_int
= bufsize
;
726 if (nfs_readlink_async(nfs
, path
, readlink_cb
, &cb_data
) != 0) {
727 nfs_set_error(nfs
, "nfs_readlink_async failed");
731 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
733 return cb_data
.status
;
741 static void chmod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
743 struct sync_cb_data
*cb_data
= private_data
;
745 cb_data
->is_finished
= 1;
746 cb_data
->status
= status
;
749 nfs_set_error(nfs
, "chmod call failed with \"%s\"", (char *)data
);
754 int nfs_chmod(struct nfs_context
*nfs
, const char *path
, int mode
)
756 struct sync_cb_data cb_data
;
758 cb_data
.is_finished
= 0;
760 if (nfs_chmod_async(nfs
, path
, mode
, chmod_cb
, &cb_data
) != 0) {
761 nfs_set_error(nfs
, "nfs_chmod_async failed");
765 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
767 return cb_data
.status
;
776 static void fchmod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
778 struct sync_cb_data
*cb_data
= private_data
;
780 cb_data
->is_finished
= 1;
781 cb_data
->status
= status
;
784 nfs_set_error(nfs
, "fchmod call failed with \"%s\"", (char *)data
);
789 int nfs_fchmod(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
)
791 struct sync_cb_data cb_data
;
793 cb_data
.is_finished
= 0;
795 if (nfs_fchmod_async(nfs
, nfsfh
, mode
, fchmod_cb
, &cb_data
) != 0) {
796 nfs_set_error(nfs
, "nfs_fchmod_async failed");
800 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
802 return cb_data
.status
;
811 static void chown_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
813 struct sync_cb_data
*cb_data
= private_data
;
815 cb_data
->is_finished
= 1;
816 cb_data
->status
= status
;
819 nfs_set_error(nfs
, "chown call failed with \"%s\"", (char *)data
);
824 int nfs_chown(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
)
826 struct sync_cb_data cb_data
;
828 cb_data
.is_finished
= 0;
830 if (nfs_chown_async(nfs
, path
, uid
, gid
, chown_cb
, &cb_data
) != 0) {
831 nfs_set_error(nfs
, "nfs_chown_async failed");
835 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
837 return cb_data
.status
;
843 static void fchown_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
845 struct sync_cb_data
*cb_data
= private_data
;
847 cb_data
->is_finished
= 1;
848 cb_data
->status
= status
;
851 nfs_set_error(nfs
, "fchown call failed with \"%s\"", (char *)data
);
856 int nfs_fchown(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
)
858 struct sync_cb_data cb_data
;
860 cb_data
.is_finished
= 0;
862 if (nfs_fchown_async(nfs
, nfsfh
, uid
, gid
, fchown_cb
, &cb_data
) != 0) {
863 nfs_set_error(nfs
, "nfs_fchown_async failed");
867 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
869 return cb_data
.status
;
877 static void utimes_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
879 struct sync_cb_data
*cb_data
= private_data
;
881 cb_data
->is_finished
= 1;
882 cb_data
->status
= status
;
885 nfs_set_error(nfs
, "utimes call failed with \"%s\"", (char *)data
);
890 int nfs_utimes(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
)
892 struct sync_cb_data cb_data
;
894 cb_data
.is_finished
= 0;
896 if (nfs_utimes_async(nfs
, path
, times
, utimes_cb
, &cb_data
) != 0) {
897 nfs_set_error(nfs
, "nfs_utimes_async failed");
901 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
903 return cb_data
.status
;
911 static void utime_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
913 struct sync_cb_data
*cb_data
= private_data
;
915 cb_data
->is_finished
= 1;
916 cb_data
->status
= status
;
919 nfs_set_error(nfs
, "utime call failed with \"%s\"", (char *)data
);
924 int nfs_utime(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
)
926 struct sync_cb_data cb_data
;
928 cb_data
.is_finished
= 0;
930 if (nfs_utime_async(nfs
, path
, times
, utime_cb
, &cb_data
) != 0) {
931 nfs_set_error(nfs
, "nfs_utimes_async failed");
935 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
937 return cb_data
.status
;
946 static void access_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
948 struct sync_cb_data
*cb_data
= private_data
;
950 cb_data
->is_finished
= 1;
951 cb_data
->status
= status
;
954 nfs_set_error(nfs
, "access call failed with \"%s\"", (char *)data
);
959 int nfs_access(struct nfs_context
*nfs
, const char *path
, int mode
)
961 struct sync_cb_data cb_data
;
963 cb_data
.is_finished
= 0;
965 if (nfs_access_async(nfs
, path
, mode
, access_cb
, &cb_data
) != 0) {
966 nfs_set_error(nfs
, "nfs_access_async failed");
970 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
972 return cb_data
.status
;
980 static void symlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
982 struct sync_cb_data
*cb_data
= private_data
;
984 cb_data
->is_finished
= 1;
985 cb_data
->status
= status
;
988 nfs_set_error(nfs
, "symlink call failed with \"%s\"", (char *)data
);
993 int nfs_symlink(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
995 struct sync_cb_data cb_data
;
997 cb_data
.is_finished
= 0;
999 if (nfs_symlink_async(nfs
, oldpath
, newpath
, symlink_cb
, &cb_data
) != 0) {
1000 nfs_set_error(nfs
, "nfs_symlink_async failed");
1004 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
1006 return cb_data
.status
;
1014 static void rename_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1016 struct sync_cb_data
*cb_data
= private_data
;
1018 cb_data
->is_finished
= 1;
1019 cb_data
->status
= status
;
1022 nfs_set_error(nfs
, "rename call failed with \"%s\"", (char *)data
);
1027 int nfs_rename(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1029 struct sync_cb_data cb_data
;
1031 cb_data
.is_finished
= 0;
1033 if (nfs_rename_async(nfs
, oldpath
, newpath
, rename_cb
, &cb_data
) != 0) {
1034 nfs_set_error(nfs
, "nfs_rename_async failed");
1038 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
1040 return cb_data
.status
;
1048 static void link_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1050 struct sync_cb_data
*cb_data
= private_data
;
1052 cb_data
->is_finished
= 1;
1053 cb_data
->status
= status
;
1056 nfs_set_error(nfs
, "link call failed with \"%s\"", (char *)data
);
1061 int nfs_link(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1063 struct sync_cb_data cb_data
;
1065 cb_data
.is_finished
= 0;
1067 if (nfs_link_async(nfs
, oldpath
, newpath
, link_cb
, &cb_data
) != 0) {
1068 nfs_set_error(nfs
, "nfs_link_async failed");
1072 wait_for_reply(nfs_get_rpc_context(nfs
), &cb_data
);
1074 return cb_data
.status
;
1077 void mount_getexports_cb(struct rpc_context
*mount_context
, int status
, void *data
, void *private_data
)
1079 struct sync_cb_data
*cb_data
= private_data
;
1080 exports export
= *(exports
*)data
;
1082 cb_data
->is_finished
= 1;
1083 cb_data
->status
= status
;
1084 cb_data
->return_data
= NULL
;
1087 rpc_set_error(mount_context
, "mount/export call failed with \"%s\"", (char *)data
);
1091 while (export
!= NULL
) {
1094 new_export
= malloc(sizeof(*new_export
));
1095 memset(new_export
, 0, sizeof(*new_export
));
1096 new_export
->ex_dir
= strdup(export
->ex_dir
);
1097 new_export
->ex_next
= cb_data
->return_data
;
1099 cb_data
->return_data
= new_export
;
1101 export
= export
->ex_next
;
1105 struct exportnode
*mount_getexports(const char *server
)
1107 struct sync_cb_data cb_data
;
1108 struct rpc_context
*rpc
;
1111 cb_data
.is_finished
= 0;
1112 cb_data
.return_data
= NULL
;
1114 rpc
= rpc_init_context();
1115 if (mount_getexports_async(rpc
, server
, mount_getexports_cb
, &cb_data
) != 0) {
1116 rpc_destroy_context(rpc
);
1120 wait_for_reply(rpc
, &cb_data
);
1121 rpc_destroy_context(rpc
);
1123 return cb_data
.return_data
;
1126 void mount_free_export_list(struct exportnode
*exports
)
1128 struct exportnode
*tmp
;
1130 while ((tmp
= exports
)) {
1131 exports
= exports
->ex_next
;
1140 void free_nfs_srvr_list(struct nfs_server_list
*srv
)
1142 while (srv
!= NULL
) {
1143 struct nfs_server_list
*next
= srv
->next
;
1151 struct nfs_list_data
{
1153 struct nfs_server_list
*srvrs
;
1156 void callit_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
1158 struct nfs_list_data
*srv_data
= private_data
;
1159 struct sockaddr
*sin
;
1161 struct nfs_server_list
*srvr
;
1163 if (status
== RPC_STATUS_CANCEL
) {
1167 srv_data
->status
= -1;
1171 sin
= rpc_get_recv_sockaddr(rpc
);
1173 rpc_set_error(rpc
, "failed to get sockaddr in CALLIT callback");
1174 srv_data
->status
= -1;
1178 if (getnameinfo(sin
, sizeof(struct sockaddr_in
), &hostdd
[0], sizeof(hostdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
1179 rpc_set_error(rpc
, "getnameinfo failed in CALLIT callback");
1180 srv_data
->status
= -1;
1184 /* check for dupes */
1185 for (srvr
= srv_data
->srvrs
; srvr
; srvr
= srvr
->next
) {
1186 if (!strcmp(hostdd
, srvr
->addr
)) {
1191 srvr
= malloc(sizeof(struct nfs_server_list
));
1193 rpc_set_error(rpc
, "Malloc failed when allocating server structure");
1194 srv_data
->status
= -1;
1198 srvr
->addr
= strdup(hostdd
);
1199 if (srvr
->addr
== NULL
) {
1200 rpc_set_error(rpc
, "Strdup failed when allocating server structure");
1202 srv_data
->status
= -1;
1206 srvr
->next
= srv_data
->srvrs
;
1207 srv_data
->srvrs
= srvr
;
1210 static int send_nfsd_probes(struct rpc_context
*rpc
, struct ifconf
*ifc
, struct nfs_list_data
*data
)
1214 for (ptr
=(char *)(ifc
->ifc_buf
); ptr
< (char *)(ifc
->ifc_buf
) + ifc
->ifc_len
; ) {
1218 ifr
= (struct ifreq
*)ptr
;
1219 #ifdef HAVE_SOCKADDR_LEN
1220 if (ifr
->ifr_addr
.sa_len
> sizeof(struct sockaddr
)) {
1221 ptr
+= sizeof(ifr
->ifr_name
) + ifr
->ifr_addr
.sa_len
;
1223 ptr
+= sizeof(ifr
->ifr_name
) + sizeof(struct sockaddr
);
1226 ptr
+= sizeof(struct ifreq
);
1229 if (ifr
->ifr_addr
.sa_family
!= AF_INET
) {
1232 if (ioctl(rpc_get_fd(rpc
), SIOCGIFFLAGS
, ifr
) < 0) {
1235 if (!(ifr
->ifr_flags
& IFF_UP
)) {
1238 if (ifr
->ifr_flags
& IFF_LOOPBACK
) {
1241 if (!(ifr
->ifr_flags
& IFF_BROADCAST
)) {
1244 if (ioctl(rpc_get_fd(rpc
), SIOCGIFBRDADDR
, ifr
) < 0) {
1247 if (getnameinfo(&ifr
->ifr_broadaddr
, sizeof(struct sockaddr_in
), &bcdd
[0], sizeof(bcdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
1250 if (rpc_set_udp_destination(rpc
, bcdd
, 111, 1) < 0) {
1254 if (rpc_pmap_callit_async(rpc
, MOUNT_PROGRAM
, 2, 0, NULL
, 0, callit_cb
, data
) < 0) {
1262 struct nfs_server_list
*nfs_find_local_servers(void)
1264 struct rpc_context
*rpc
;
1265 struct nfs_list_data data
= {0, NULL
};
1266 struct timeval tv_start
, tv_current
;
1271 rpc
= rpc_init_udp_context();
1276 if (rpc_bind_udp(rpc
, "0.0.0.0", 0) < 0) {
1277 rpc_destroy_context(rpc
);
1282 /* get list of all interfaces */
1283 size
= sizeof(struct ifreq
);
1287 while(ifc
.ifc_len
> (size
- sizeof(struct ifreq
))) {
1292 ifc
.ifc_buf
= malloc(size
);
1293 memset(ifc
.ifc_buf
, 0, size
);
1294 if (ioctl(rpc_get_fd(rpc
), SIOCGIFCONF
, (caddr_t
)&ifc
) < 0) {
1295 rpc_destroy_context(rpc
);
1301 for (loop
=0; loop
<3; loop
++) {
1302 if (send_nfsd_probes(rpc
, &ifc
, &data
) != 0) {
1303 rpc_destroy_context(rpc
);
1308 gettimeofday(&tv_start
, NULL
);
1312 pfd
.fd
= rpc_get_fd(rpc
);
1313 pfd
.events
= rpc_which_events(rpc
);
1315 gettimeofday(&tv_current
, NULL
);
1317 - (tv_current
.tv_sec
*1000 + tv_current
.tv_usec
/ 1000)
1318 + (tv_start
.tv_sec
*1000 + tv_start
.tv_usec
/ 1000);
1320 if (poll(&pfd
, 1, mpt
) < 0) {
1321 free_nfs_srvr_list(data
.srvrs
);
1322 rpc_destroy_context(rpc
);
1325 if (pfd
.revents
== 0) {
1329 if (rpc_service(rpc
, pfd
.revents
) < 0) {
1336 rpc_destroy_context(rpc
);
1338 if (data
.status
!= 0) {
1339 free_nfs_srvr_list(data
.srvrs
);