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
31 #include <sys/types.h>
38 #include <rpc/pmap_prot.h>
39 #include <rpc/pmap_clnt.h>
46 #ifdef HAVE_RPCSVC_NFS_PROT_H
47 #include <rpcsvc/nfs_prot.h>
49 #include <linux/nfs.h>
50 #define nfsstat nfs_stat
53 extern char *progname;
55 static char errbuf[BUFSIZ];
56 static char *erreob = &errbuf[BUFSIZ];
58 /* Convert RPC errors into strings */
59 static int rpc_strerror(int spos)
61 int cf_stat = rpc_createerr.cf_stat;
62 int pos = 0, cf_errno = rpc_createerr.cf_error.re_errno;
63 char *ptr, *estr = clnt_sperrno(cf_stat);
67 if ((ptr = index(estr, ':')))
71 if (cf_stat == RPC_SYSTEMERROR)
72 pos = snprintf(tmp, (erreob - tmp),
73 _("System Error: %s"),
76 pos = snprintf(tmp, (erreob - tmp),
77 _("RPC Error:%s"), estr);
83 * rpc_mount_errors - log an RPC error that occurred during a user-space mount
84 * @server: C string containing name of server we are attempting to mount
85 * @will_retry: one indicates mount will retry at some later point
86 * @bg: one indicates this is a background mount
88 * Extracts the error code from the user-space RPC library, and reports it
89 * on stderr (fg mount) or in the system log (bg mount).
91 void rpc_mount_errors(char *server, int will_retry, int bg)
95 static int onlyonce = 0;
99 pos = snprintf(tmp, (erreob - tmp),
100 _("mount to NFS server '%s' failed: "),
103 pos = snprintf(tmp, (erreob - tmp),
104 _("%s: mount to NFS server '%s' failed: "),
108 if (rpc_createerr.cf_stat == RPC_TIMEDOUT) {
110 pos = snprintf(tmp, (erreob - tmp),
111 _("timed out, retrying"));
113 pos = snprintf(tmp, (erreob - tmp),
114 _("timed out, giving up"));
116 pos += rpc_strerror(pos);
120 pos = snprintf(tmp, (erreob - tmp),
123 pos = snprintf(tmp, (erreob - tmp),
130 openlog("mount", LOG_CONS|LOG_PID, LOG_AUTH);
131 syslog(LOG_ERR, "%s", errbuf);
133 fprintf(stderr, "%s\n", errbuf);
137 * sys_mount_errors - log an error that occurred during a mount system call
138 * @server: C string containing name of server we are attempting to mount
139 * @error: errno value to report
140 * @will_retry: one indicates mount will retry at some later point
141 * @bg: one indicates this is a background mount
143 * Passed an errno value generated by a mount system call, and reports it
144 * on stderr (fg mount) or in the system log (bg mount).
146 void sys_mount_errors(char *server, int error, int will_retry, int bg)
150 static int onlyonce = 0;
154 pos = snprintf(tmp, (erreob - tmp),
155 _("mount to NFS server '%s' failed: "),
158 pos = snprintf(tmp, (erreob - tmp),
159 _("%s: mount to NFS server '%s' failed: "),
163 if (error == ETIMEDOUT) {
165 pos = snprintf(tmp, (erreob - tmp),
166 _("timed out, retrying"));
168 pos = snprintf(tmp, (erreob - tmp),
169 _("timed out, giving up"));
173 pos = snprintf(tmp, (erreob - tmp),
177 pos = snprintf(tmp, (erreob - tmp),
185 openlog("mount", LOG_CONS|LOG_PID, LOG_AUTH);
186 syslog(LOG_ERR, "%s", errbuf);
188 fprintf(stderr, "%s\n", errbuf);
192 * mount_error - report a foreground mount error
193 * @spec: C string containing the device name being mounted
194 * @mount_point: C string containing the pathname of the local mounted on dir
195 * @error: errno value to report
198 void mount_error(const char *spec, const char *mount_point, int error)
202 nfs_error(_("%s: access denied by server while mounting %s"),
206 nfs_error(_("%s: an incorrect mount option was specified"), progname);
209 nfs_error(_("%s: requested NFS version or transport"
210 " protocol is not supported"),
214 nfs_error(_("%s: mount point %s is not a directory"),
215 progname, mount_point);
218 nfs_error(_("%s: %s is busy or already mounted"),
219 progname, mount_point);
223 nfs_error(_("%s: mounting %s failed, "
224 "reason given by server:\n %s"),
225 progname, spec, strerror(error));
227 nfs_error(_("%s: mount point %s does not exist"),
228 progname, mount_point);
232 nfs_error(_("%s: internal error"), progname);
235 nfs_error(_("%s: %s"),
236 progname, strerror(error));
241 * umount_error - report a failed umount request
242 * @err: errno value to report
243 * @dev: C string containing the pathname of the local mounted on dir
246 void umount_error(int err, const char *dev)
250 nfs_error(_("%s: %s: invalid block device"),
254 nfs_error(_("%s: %s: not mounted"),
258 nfs_error(_("%s: %s: can't write superblock"),
262 nfs_error(_("%s: %s: device is busy"),
266 nfs_error(_("%s: %s: not found"),
270 nfs_error(_("%s: %s: must be superuser to umount"),
274 nfs_error(_("%s: %s: block devices not permitted on fs"),
278 nfs_error(_("%s: %s: %s"),
279 progname, dev, strerror(err));
285 * We need to translate between nfs status return values and
286 * the local errno values which may not be the same.
288 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
289 * "after #include <errno.h> the symbol errno is reserved for any use,
290 * it cannot even be used as a struct tag or field name".
294 #define EDQUOT ENOSPC
302 { NFSERR_PERM, EPERM },
303 { NFSERR_NOENT, ENOENT },
305 { NFSERR_NXIO, ENXIO },
306 { NFSERR_ACCES, EACCES },
307 { NFSERR_EXIST, EEXIST },
308 { NFSERR_NODEV, ENODEV },
309 { NFSERR_NOTDIR, ENOTDIR },
310 { NFSERR_ISDIR, EISDIR },
312 { NFSERR_INVAL, EINVAL }, /* that Sun forgot */
314 { NFSERR_FBIG, EFBIG },
315 { NFSERR_NOSPC, ENOSPC },
316 { NFSERR_ROFS, EROFS },
317 { NFSERR_NAMETOOLONG, ENAMETOOLONG },
318 { NFSERR_NOTEMPTY, ENOTEMPTY },
319 { NFSERR_DQUOT, EDQUOT },
320 { NFSERR_STALE, ESTALE },
322 { NFSERR_WFLUSH, EWFLUSH },
324 /* Throw in some NFSv3 values for even more fun (HP returns these) */
330 char *nfs_strerror(int stat)
333 static char buf[256];
335 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
336 if (nfs_errtbl[i].stat == stat)
337 return strerror(nfs_errtbl[i].errnum);
339 sprintf(buf, _("unknown nfs status return value: %d"), stat);