summaryrefslogtreecommitdiffstats
path: root/utils/mount/error.c
blob: 83ad1d2f796a29551e5beb797966ff17e475c8e9 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * error.c -- Common error handling functions
 *
 * Copyright (C) 2007 Oracle.  All rights reserved.
 * Copyright (C) 2007 Chuck Lever <chuck.lever@oracle.com>
 *
 * 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 2 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, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 0211-1301 USA
 *
 * To Do:
 *  + Proper support for internationalization
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <syslog.h>
#include <rpc/rpc.h>

#include "xcommon.h"
#include "nls.h"
#include "mount.h"
#include "error.h"

#ifdef HAVE_RPCSVC_NFS_PROT_H
#include <rpcsvc/nfs_prot.h>
#else
#include <linux/nfs.h>
#define nfsstat nfs_stat
#endif

extern char *progname;

static char errbuf[BUFSIZ];
static char *erreob = &errbuf[BUFSIZ];

/* Convert RPC errors into strings */
static int rpc_strerror(int spos)
{
	int cf_stat = rpc_createerr.cf_stat;
	int pos = 0, cf_errno = rpc_createerr.cf_error.re_errno;
	char *ptr, *estr = clnt_sperrno(cf_stat);
	char *tmp;

	if (estr) {
		if ((ptr = index(estr, ':')))
			estr = ++ptr;

		tmp = &errbuf[spos];
		if (cf_stat == RPC_SYSTEMERROR)
			pos = snprintf(tmp, (erreob - tmp),
					_("System Error: %s"),
						strerror(cf_errno));
		else {
			if (cf_errno) 
				pos = snprintf(tmp, (erreob - tmp),
					_("RPC Error:%s; errno = %s"), 
					estr, strerror(cf_errno));
			else
				pos = snprintf(tmp, (erreob - tmp),
						_("RPC Error:%s"), estr);
		}
	}
	return pos;
}

/**
 * rpc_mount_errors - log an RPC error that occurred during a user-space mount
 * @server: C string containing name of server we are attempting to mount
 * @will_retry: one indicates mount will retry at some later point
 * @bg: one indicates this is a background mount
 *
 * Extracts the error code from the user-space RPC library, and reports it
 * on stderr (fg mount) or in the system log (bg mount).
 */
void rpc_mount_errors(char *server, int will_retry, int bg)
{
	int pos = 0;
	char *tmp;
	static int onlyonce = 0;

	tmp = &errbuf[pos];
	if (bg)
		pos = snprintf(tmp, (erreob - tmp),
				_("mount to NFS server '%s' failed: "),
					server);
	else
		pos = snprintf(tmp, (erreob - tmp),
				_("%s: mount to NFS server '%s' failed: "),
					progname, server);

	tmp = &errbuf[pos];
	if (rpc_createerr.cf_stat == RPC_TIMEDOUT) {
		if (will_retry)
			pos = snprintf(tmp, (erreob - tmp),
					_("timed out, retrying"));
		else
			pos = snprintf(tmp, (erreob - tmp),
					_("timed out, giving up"));
	} else {
		pos += rpc_strerror(pos);
		tmp = &errbuf[pos];
		if (bg) {
			if (will_retry)
				pos = snprintf(tmp, (erreob - tmp),
						_(", retrying"));
			else
				pos = snprintf(tmp, (erreob - tmp),
						_(", giving up"));
		}
	}

	if (bg) {
		if (onlyonce++ < 1)
			openlog("mount", LOG_CONS|LOG_PID, LOG_AUTH);
		syslog(LOG_ERR, "%s", errbuf);
	} else
		fprintf(stderr, "%s\n", errbuf);
}

/**
 * sys_mount_errors - log an error that occurred during a mount system call
 * @server: C string containing name of server we are attempting to mount
 * @error: errno value to report
 * @will_retry: one indicates mount will retry at some later point
 * @bg: one indicates this is a background mount
 *
 * Passed an errno value generated by a mount system call, and reports it
 * on stderr (fg mount) or in the system log (bg mount).
 */
void sys_mount_errors(char *server, int error, int will_retry, int bg)
{
	int pos = 0;
	char *tmp;
	static int onlyonce = 0;

	tmp = &errbuf[pos];
	if (bg)
		pos = snprintf(tmp, (erreob - tmp),
				_("mount to NFS server '%s' failed: "),
					server);
	else
		pos = snprintf(tmp, (erreob - tmp),
				_("%s: mount to NFS server '%s' failed: "),
					progname, server);

	tmp = &errbuf[pos];
	if (error == ETIMEDOUT) {
		if (will_retry)
			pos = snprintf(tmp, (erreob - tmp),
					_("timed out, retrying"));
		else
			pos = snprintf(tmp, (erreob - tmp),
					_("timed out, giving up"));
	} else {
		if (bg) {
			if (will_retry)
				pos = snprintf(tmp, (erreob - tmp),
						_("%s, retrying"),
						strerror(error));
			else
				pos = snprintf(tmp, (erreob - tmp),
						_("%s, giving up"),
						strerror(error));
		}
	}

	if (bg) {
		if (onlyonce++ < 1)
			openlog("mount", LOG_CONS|LOG_PID, LOG_AUTH);
		syslog(LOG_ERR, "%s", errbuf);
	} else
		fprintf(stderr, "%s\n", errbuf);
}

/**
 * mount_error - report a foreground mount error
 * @spec: C string containing the device name being mounted
 * @mount_point: C string containing the pathname of the local mounted on dir
 * @error: errno value to report
 *
 */
void mount_error(const char *spec, const char *mount_point, int error)
{
	switch(error) {
	case EACCES:
		nfs_error(_("%s: access denied by server while mounting %s"),
				progname, spec);
		break;
	case EINVAL:
		nfs_error(_("%s: an incorrect mount option was specified"), progname);
		break;
	case EOPNOTSUPP:
		nfs_error(_("%s: requested NFS version or transport"
				" protocol is not supported"),
				progname);
		break;
	case ENOTDIR:
		nfs_error(_("%s: mount point %s is not a directory"),
				progname, mount_point);
		break;
	case EBUSY:
		nfs_error(_("%s: %s is busy or already mounted"),
			progname, mount_point);
		break;
	case ENOENT:
		if (spec)
			nfs_error(_("%s: mounting %s failed, "
				"reason given by server:\n  %s"),
				progname, spec, strerror(error));
		else
			nfs_error(_("%s: mount point %s does not exist"),
				progname, mount_point);
		break;
	case ESPIPE:
		rpc_mount_errors((char *)spec, 0, 0);
		break;
	case EIO:
		nfs_error(_("%s: mount system call failed"), progname);
		break;
	case EFAULT:
		nfs_error(_("%s: encountered unexpected error condition."),
				progname);
		nfs_error(_("%s: please report the error to" PACKAGE_BUGREPORT),
				progname);
		break;
	default:
		nfs_error(_("%s: %s"),
			progname, strerror(error));
	}
}

/*
 * umount_error - report a failed umount request
 * @err: errno value to report
 * @dev: C string containing the pathname of the local mounted on dir
 *
 */
void umount_error(int err, const char *dev)
{
	switch (err) {
	case ENXIO:
		nfs_error(_("%s: %s: invalid block device"),
			progname, dev);
		break;
	case EINVAL:
		nfs_error(_("%s: %s: not mounted"),
			progname, dev);
		break;
	case EIO:
		nfs_error(_("%s: %s: can't write superblock"),
			progname, dev);
		break;
	case EBUSY:
		nfs_error(_("%s: %s: device is busy"),
			progname, dev);
		break;
	case ENOENT:
		nfs_error(_("%s: %s: not found"),
			progname, dev);
		break;
	case EPERM:
		nfs_error(_("%s: %s: must be superuser to umount"),
			progname, dev);
		break;
	case EACCES:
		nfs_error(_("%s: %s: block devices not permitted on fs"),
			progname, dev);
		break;
	default:
		nfs_error(_("%s: %s: %s"),
			progname, dev, strerror(err));
		break;
	}
}

/*
 * We need to translate between nfs status return values and
 * the local errno values which may not be the same.
 *
 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
 * "after #include <errno.h> the symbol errno is reserved for any use,
 *  it cannot even be used as a struct tag or field name".
 */

#ifndef EDQUOT
#define EDQUOT	ENOSPC
#endif

#define ARRAY_SIZE(x)		(sizeof(x) / sizeof((x)[0])) 

static struct {
	enum nfsstat stat;
	int errnum;
} nfs_errtbl[] = {
	{ NFS_OK,		0		},
	{ NFSERR_PERM,		EPERM		},
	{ NFSERR_NOENT,		ENOENT		},
	{ NFSERR_IO,		EIO		},
	{ NFSERR_NXIO,		ENXIO		},
	{ NFSERR_ACCES,		EACCES		},
	{ NFSERR_EXIST,		EEXIST		},
	{ NFSERR_NODEV,		ENODEV		},
	{ NFSERR_NOTDIR,	ENOTDIR		},
	{ NFSERR_ISDIR,		EISDIR		},
#ifdef NFSERR_INVAL
	{ NFSERR_INVAL,		EINVAL		},	/* that Sun forgot */
#endif
	{ NFSERR_FBIG,		EFBIG		},
	{ NFSERR_NOSPC,		ENOSPC		},
	{ NFSERR_ROFS,		EROFS		},
	{ NFSERR_NAMETOOLONG,	ENAMETOOLONG	},
	{ NFSERR_NOTEMPTY,	ENOTEMPTY	},
	{ NFSERR_DQUOT,		EDQUOT		},
	{ NFSERR_STALE,		ESTALE		},
#ifdef EWFLUSH
	{ NFSERR_WFLUSH,	EWFLUSH		},
#endif
	/* Throw in some NFSv3 values for even more fun (HP returns these) */
	{ 71,			EREMOTE		},
};

char *nfs_strerror(unsigned int stat)
{
	unsigned int i;
	static char buf[256];

	for (i = 0; i < ARRAY_SIZE(nfs_errtbl); i++) {
		if (nfs_errtbl[i].stat == stat)
			return strerror(nfs_errtbl[i].errnum);
	}
	sprintf(buf, _("unknown nfs status return value: %u"), stat);
	return buf;
}