summaryrefslogtreecommitdiff
path: root/ccan/nfs/mount.c
blob: b79b7f70735076262b4e29dc57a6a6a55baab2d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
   Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <errno.h>
#include <rpc/xdr.h>
#include "nfs.h"
#include "libnfs-raw.h"
#include "libnfs-private.h"
#include "rpc/mount.h"


int rpc_mount_null_async(struct rpc_context *rpc, rpc_cb cb, void *private_data)
{
	struct rpc_pdu *pdu;

	pdu = rpc_allocate_pdu(rpc, MOUNT_PROGRAM, MOUNT_V3, MOUNT3_NULL, cb, private_data, (xdrproc_t)xdr_void, 0);
	if (pdu == NULL) {
		rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for mount/null call");
		return -1;
	}

	if (rpc_queue_pdu(rpc, pdu) != 0) {
		rpc_set_error(rpc, "Out of memory. Failed to queue pdu for mount/null call");
		rpc_free_pdu(rpc, pdu);
		return -2;
	}

	return 0;
}

int rpc_mount_mnt_async(struct rpc_context *rpc, rpc_cb cb, char *export, void *private_data)
{
	struct rpc_pdu *pdu;

	pdu = rpc_allocate_pdu(rpc, MOUNT_PROGRAM, MOUNT_V3, MOUNT3_MNT, cb, private_data, (xdrproc_t)xdr_mountres3, sizeof(mountres3));
	if (pdu == NULL) {
		rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for mount/mnt call");
		return -1;
	}

	if (xdr_dirpath(&pdu->xdr, &export) == 0) {
		rpc_set_error(rpc, "XDR error. Failed to encode mount/mnt call");
		rpc_free_pdu(rpc, pdu);
		return -2;
	}

	if (rpc_queue_pdu(rpc, pdu) != 0) {
		rpc_set_error(rpc, "Out of memory. Failed to queue pdu for mount/mnt call");
		rpc_free_pdu(rpc, pdu);
		return -2;
	}

	return 0;
}

int rpc_mount_dump_async(struct rpc_context *rpc, rpc_cb cb, void *private_data)
{
	struct rpc_pdu *pdu;

	pdu = rpc_allocate_pdu(rpc, MOUNT_PROGRAM, MOUNT_V3, MOUNT3_DUMP, cb, private_data, (xdrproc_t)xdr_mountlist, sizeof(mountlist));
	if (pdu == NULL) {
		printf("Failed to allocate pdu for mount/dump\n");
		return -1;
	}

	if (rpc_queue_pdu(rpc, pdu) != 0) {
		printf("Failed to queue mount/dump pdu\n");
		rpc_free_pdu(rpc, pdu);
		return -2;
	}

	return 0;
}

int rpc_mount_umnt_async(struct rpc_context *rpc, rpc_cb cb, char *export, void *private_data)
{
	struct rpc_pdu *pdu;

	pdu = rpc_allocate_pdu(rpc, MOUNT_PROGRAM, MOUNT_V3, MOUNT3_UMNT, cb, private_data, (xdrproc_t)xdr_void, 0);
	if (pdu == NULL) {
		printf("Failed to allocate pdu for mount/umnt\n");
		return -1;
	}

	if (xdr_dirpath(&pdu->xdr, &export) == 0) {
		printf("failed to encode dirpath for mount/umnt\n");
		rpc_free_pdu(rpc, pdu);
		return -2;
	}

	if (rpc_queue_pdu(rpc, pdu) != 0) {
		printf("Failed to queue mount/umnt pdu\n");
		rpc_free_pdu(rpc, pdu);
		return -2;
	}

	return 0;
}

int rpc_mount_umntall_async(struct rpc_context *rpc, rpc_cb cb, void *private_data)
{
	struct rpc_pdu *pdu;

	pdu = rpc_allocate_pdu(rpc, MOUNT_PROGRAM, MOUNT_V3, MOUNT3_UMNTALL, cb, private_data, (xdrproc_t)xdr_void, 0);
	if (pdu == NULL) {
		printf("Failed to allocate pdu for mount/umntall\n");
		return -1;
	}

	if (rpc_queue_pdu(rpc, pdu) != 0) {
		printf("Failed to queue mount/umntall pdu\n");
		rpc_free_pdu(rpc, pdu);
		return -2;
	}

	return 0;
}

int rpc_mount_export_async(struct rpc_context *rpc, rpc_cb cb, void *private_data)
{
	struct rpc_pdu *pdu;

	pdu = rpc_allocate_pdu(rpc, MOUNT_PROGRAM, MOUNT_V3, MOUNT3_EXPORT, cb, private_data, (xdrproc_t)xdr_exports, sizeof(exports));
	if (pdu == NULL) {
		printf("Failed to allocate pdu for mount/export\n");
		return -1;
	}

	if (rpc_queue_pdu(rpc, pdu) != 0) {
		printf("Failed to queue mount/export pdu\n");
		rpc_free_pdu(rpc, pdu);
		return -2;
	}

	return 0;
}

char *mountstat3_to_str(int st)
{
	enum mountstat3 stat = st;

	char *str = "unknown mount stat";
	switch (stat) {
	case MNT3_OK:		  str="MNT3_OK"; break;
	case MNT3ERR_PERM:	  str="MNT3ERR_PERM"; break;
	case MNT3ERR_NOENT:	  str="MNT3ERR_NOENT"; break;
	case MNT3ERR_IO:	  str="MNT3ERR_IO"; break;
	case MNT3ERR_ACCES:	  str="MNT3ERR_ACCES"; break;
	case MNT3ERR_NOTDIR:	  str="MNT3ERR_NOTDIR"; break;
	case MNT3ERR_INVAL:	  str="MNT3ERR_INVAL"; break;
	case MNT3ERR_NAMETOOLONG: str="MNT3ERR_NAMETOOLONG"; break;
	case MNT3ERR_NOTSUPP:	  str="MNT3ERR_NOTSUPP"; break;
	case MNT3ERR_SERVERFAULT: str="MNT3ERR_SERVERFAULT"; break;
	}
	return str;
}


int mountstat3_to_errno(int st)
{
	enum mountstat3 stat = st;

	switch (stat) {
	case MNT3_OK:		  return 0; break;
	case MNT3ERR_PERM:	  return -EPERM; break;
	case MNT3ERR_NOENT:	  return -EPERM; break;
	case MNT3ERR_IO:	  return -EIO; break;
	case MNT3ERR_ACCES:	  return -EACCES; break;
	case MNT3ERR_NOTDIR:	  return -ENOTDIR; break;
	case MNT3ERR_INVAL:	  return -EINVAL; break;
	case MNT3ERR_NAMETOOLONG: return -E2BIG; break;
	case MNT3ERR_NOTSUPP:	  return -EINVAL; break;
	case MNT3ERR_SERVERFAULT: return -EIO; break;
	}
	return -ERANGE;
}