2 * error.c -- Common error handling functions
4 * Copyright (C) 2007 Oracle. All rights reserved.
5 * Copyright (C) 2007 Chuck Lever <chuck.lever@oracle.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public
18 * License along with this program; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 021110-1307, USA.
23 * + Proper support for internationalization
28 #include <sys/types.h>
35 #include <rpc/pmap_prot.h>
36 #include <rpc/pmap_clnt.h>
43 #ifdef HAVE_RPCSVC_NFS_PROT_H
44 #include <rpcsvc/nfs_prot.h>
46 #include <linux/nfs.h>
47 #define nfsstat nfs_stat
50 extern char *progname;
52 static char errbuf[BUFSIZ];
53 static char *erreob = &errbuf[BUFSIZ];
55 /* Convert RPC errors into strings */
56 static int rpc_strerror(int spos)
58 int cf_stat = rpc_createerr.cf_stat;
59 int pos = 0, cf_errno = rpc_createerr.cf_error.re_errno;
60 char *ptr, *estr = clnt_sperrno(cf_stat);
64 if ((ptr = index(estr, ':')))
68 if (cf_stat == RPC_SYSTEMERROR)
69 pos = snprintf(tmp, (erreob - tmp),
70 _("System Error: %s"),
73 pos = snprintf(tmp, (erreob - tmp),
74 _("RPC Error:%s"), estr);
80 * rpc_mount_errors - log an RPC error that occurred during a user-space mount
81 * @server: C string containing name of server we are attempting to mount
82 * @will_retry: one indicates mount will retry at some later point
83 * @bg: one indicates this is a background mount
85 * Extracts the error code from the user-space RPC library, and reports it
86 * on stderr (fg mount) or in the system log (bg mount).
88 void rpc_mount_errors(char *server, int will_retry, int bg)
92 static int onlyonce = 0;
96 pos = snprintf(tmp, (erreob - tmp),
97 _("mount to NFS server '%s' failed: "),
100 pos = snprintf(tmp, (erreob - tmp),
101 _("%s: mount to NFS server '%s' failed: "),
105 if (rpc_createerr.cf_stat == RPC_TIMEDOUT) {
107 pos = snprintf(tmp, (erreob - tmp),
108 _("timed out, retrying"));
110 pos = snprintf(tmp, (erreob - tmp),
111 _("timed out, giving up"));
113 pos += rpc_strerror(pos);
117 pos = snprintf(tmp, (erreob - tmp),
120 pos = snprintf(tmp, (erreob - tmp),
127 openlog("mount", LOG_CONS|LOG_PID, LOG_AUTH);
128 syslog(LOG_ERR, "%s", errbuf);
130 fprintf(stderr, "%s\n", errbuf);
134 * sys_mount_errors - log an error that occurred during a mount system call
135 * @server: C string containing name of server we are attempting to mount
136 * @error: errno value to report
137 * @will_retry: one indicates mount will retry at some later point
138 * @bg: one indicates this is a background mount
140 * Passed an errno value generated by a mount system call, and reports it
141 * on stderr (fg mount) or in the system log (bg mount).
143 void sys_mount_errors(char *server, int error, int will_retry, int bg)
147 static int onlyonce = 0;
151 pos = snprintf(tmp, (erreob - tmp),
152 _("mount to NFS server '%s' failed: "),
155 pos = snprintf(tmp, (erreob - tmp),
156 _("%s: mount to NFS server '%s' failed: "),
160 if (error == ETIMEDOUT) {
162 pos = snprintf(tmp, (erreob - tmp),
163 _("timed out, retrying"));
165 pos = snprintf(tmp, (erreob - tmp),
166 _("timed out, giving up"));
170 pos = snprintf(tmp, (erreob - tmp),
174 pos = snprintf(tmp, (erreob - tmp),
182 openlog("mount", LOG_CONS|LOG_PID, LOG_AUTH);
183 syslog(LOG_ERR, "%s", errbuf);
185 fprintf(stderr, "%s\n", errbuf);
189 * mount_error - report a foreground mount error
190 * @spec: C string containing the device name being mounted
191 * @mount_point: C string containing the pathname of the local mounted on dir
192 * @error: errno value to report
195 void mount_error(const char *spec, const char *mount_point, int error)
199 nfs_error(_("%s: access denied by server while mounting %s"),
203 nfs_error(_("%s: an incorrect mount option was specified"), progname);
206 nfs_error(_("%s: requested NFS version or transport"
207 " protocol is not supported"),
211 nfs_error(_("%s: mount point %s is not a directory"),
212 progname, mount_point);
215 nfs_error(_("%s: %s is busy or already mounted"),
216 progname, mount_point);
220 nfs_error(_("%s: mounting %s failed, "
221 "reason given by server:\n %s"),
222 progname, spec, strerror(error));
224 nfs_error(_("%s: mount point %s does not exist"),
225 progname, mount_point);
229 nfs_error(_("%s: internal error"), progname);
232 nfs_error(_("%s: %s"),
233 progname, strerror(error));
238 * umount_error - report a failed umount request
239 * @err: errno value to report
240 * @dev: C string containing the pathname of the local mounted on dir
243 void umount_error(int err, const char *dev)
247 nfs_error(_("%s: %s: invalid block device"),
251 nfs_error(_("%s: %s: not mounted"),
255 nfs_error(_("%s: %s: can't write superblock"),
259 nfs_error(_("%s: %s: device is busy"),
263 nfs_error(_("%s: %s: not found"),
267 nfs_error(_("%s: %s: must be superuser to umount"),
271 nfs_error(_("%s: %s: block devices not permitted on fs"),
275 nfs_error(_("%s: %s: %s"),
276 progname, dev, strerror(err));
282 * We need to translate between nfs status return values and
283 * the local errno values which may not be the same.
285 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
286 * "after #include <errno.h> the symbol errno is reserved for any use,
287 * it cannot even be used as a struct tag or field name".
291 #define EDQUOT ENOSPC
299 { NFSERR_PERM, EPERM },
300 { NFSERR_NOENT, ENOENT },
302 { NFSERR_NXIO, ENXIO },
303 { NFSERR_ACCES, EACCES },
304 { NFSERR_EXIST, EEXIST },
305 { NFSERR_NODEV, ENODEV },
306 { NFSERR_NOTDIR, ENOTDIR },
307 { NFSERR_ISDIR, EISDIR },
309 { NFSERR_INVAL, EINVAL }, /* that Sun forgot */
311 { NFSERR_FBIG, EFBIG },
312 { NFSERR_NOSPC, ENOSPC },
313 { NFSERR_ROFS, EROFS },
314 { NFSERR_NAMETOOLONG, ENAMETOOLONG },
315 { NFSERR_NOTEMPTY, ENOTEMPTY },
316 { NFSERR_DQUOT, EDQUOT },
317 { NFSERR_STALE, ESTALE },
319 { NFSERR_WFLUSH, EWFLUSH },
321 /* Throw in some NFSv3 values for even more fun (HP returns these) */
327 char *nfs_strerror(int stat)
330 static char buf[256];
332 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
333 if (nfs_errtbl[i].stat == stat)
334 return strerror(nfs_errtbl[i].errnum);
336 sprintf(buf, _("unknown nfs status return value: %d"), stat);