]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/error.c
1f60b79f2ebadb6a86ea29684240fcff7c57df93
[nfs-utils.git] / utils / mount / error.c
1 /*
2  * error.c -- Common error handling functions
3  *
4  * Copyright (C) 2007 Oracle.  All rights reserved.
5  * Copyright (C) 2007 Chuck Lever <chuck.lever@oracle.com>
6  *
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.
11  *
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.
16  *
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.
21  *
22  * To Do:
23  *  + Proper support for internationalization
24  */
25
26 #include "config.h"
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <syslog.h>
34 #include <rpc/rpc.h>
35 #include <rpc/pmap_prot.h>
36 #include <rpc/pmap_clnt.h>
37
38 #include "xcommon.h"
39 #include "nls.h"
40 #include "mount.h"
41 #include "error.h"
42
43 #ifdef HAVE_RPCSVC_NFS_PROT_H
44 #include <rpcsvc/nfs_prot.h>
45 #else
46 #include <linux/nfs.h>
47 #define nfsstat nfs_stat
48 #endif
49
50 extern char *progname;
51
52 static char errbuf[BUFSIZ];
53 static char *erreob = &errbuf[BUFSIZ];
54
55 /* Convert RPC errors into strings */
56 static int rpc_strerror(int spos)
57 {
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);
61         char *tmp;
62
63         if (estr) {
64                 if ((ptr = index(estr, ':')))
65                         estr = ++ptr;
66
67                 tmp = &errbuf[spos];
68                 if (cf_stat == RPC_SYSTEMERROR)
69                         pos = snprintf(tmp, (erreob - tmp),
70                                         _("System Error: %s"),
71                                                 strerror(cf_errno));
72                 else
73                         pos = snprintf(tmp, (erreob - tmp),
74                                         _("RPC Error:%s"), estr);
75         }
76         return pos;
77 }
78
79 void mount_errors(char *server, int will_retry, int bg)
80 {
81         int pos = 0;
82         char *tmp;
83         static int onlyonce = 0;
84
85         tmp = &errbuf[pos];
86         if (bg)
87                 pos = snprintf(tmp, (erreob - tmp),
88                                 _("mount to NFS server '%s' failed: "),
89                                         server);
90         else
91                 pos = snprintf(tmp, (erreob - tmp),
92                                 _("%s: mount to NFS server '%s' failed: "),
93                                         progname, server);
94
95         tmp = &errbuf[pos];
96         if (rpc_createerr.cf_stat == RPC_TIMEDOUT) {
97                 if (will_retry)
98                         pos = snprintf(tmp, (erreob - tmp),
99                                         _("timed out, retrying"));
100                 else
101                         pos = snprintf(tmp, (erreob - tmp),
102                                         _("timed out, giving up"));
103         } else {
104                 pos += rpc_strerror(pos);
105                 tmp = &errbuf[pos];
106                 if (bg) {
107                         if (will_retry)
108                                 pos = snprintf(tmp, (erreob - tmp),
109                                                 _(", retrying"));
110                         else
111                                 pos = snprintf(tmp, (erreob - tmp),
112                                                 _(", giving up"));
113                 }
114         }
115
116         if (bg) {
117                 if (onlyonce++ < 1)
118                         openlog("mount", LOG_CONS|LOG_PID, LOG_AUTH);
119                 syslog(LOG_ERR, "%s", errbuf);
120         } else
121                 fprintf(stderr, "%s\n", errbuf);
122 }
123
124 /*
125  * mount_error - report a foreground mount error
126  * @spec: C string containing the device name being mounted
127  * @mount_point: C string containing the pathname of the local mounted on dir
128  * @error: errno value to report
129  *
130  */
131 void mount_error(const char *spec, const char *mount_point, int error)
132 {
133         switch(error) {
134         case ENOTDIR:
135                 nfs_error(_("%s: mount point %s is not a directory"),
136                                 progname, mount_point);
137                 break;
138         case EBUSY:
139                 nfs_error(_("%s: %s is already mounted or busy"),
140                         progname, mount_point);
141                 break;
142         case ENOENT:
143                 if (spec)
144                         nfs_error(_("%s: mounting %s failed, "
145                                 "reason given by server:\n  %s"),
146                                 progname, spec, strerror(error));
147                 else
148                         nfs_error(_("%s: mount point %s does not exist"),
149                                 progname, mount_point);
150                 break;
151         default:
152                 nfs_error(_("%s: %s"),
153                         progname, strerror(error));
154         }
155 }
156
157 /*
158  * umount_error - report a failed umount request
159  * @err: errno value to report
160  * @dev: C string containing the pathname of the local mounted on dir
161  *
162  */
163 void umount_error(int err, const char *dev)
164 {
165         switch (err) {
166         case ENXIO:
167                 nfs_error(_("%s: %s: invalid block device"),
168                         progname, dev);
169                 break;
170         case EINVAL:
171                 nfs_error(_("%s: %s: not mounted"),
172                         progname, dev);
173                 break;
174         case EIO:
175                 nfs_error(_("%s: %s: can't write superblock"),
176                         progname, dev);
177                 break;
178         case EBUSY:
179                 nfs_error(_("%s: %s: device is busy"),
180                         progname, dev);
181                 break;
182         case ENOENT:
183                 nfs_error(_("%s: %s: not found"),
184                         progname, dev);
185                 break;
186         case EPERM:
187                 nfs_error(_("%s: %s: must be superuser to umount"),
188                         progname, dev);
189                 break;
190         case EACCES:
191                 nfs_error(_("%s: %s: block devices not permitted on fs"),
192                         progname, dev);
193                 break;
194         default:
195                 nfs_error(_("%s: %s: %s"),
196                         progname, dev, strerror(err));
197                 break;
198         }
199 }
200
201 /*
202  * We need to translate between nfs status return values and
203  * the local errno values which may not be the same.
204  *
205  * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
206  * "after #include <errno.h> the symbol errno is reserved for any use,
207  *  it cannot even be used as a struct tag or field name".
208  */
209
210 #ifndef EDQUOT
211 #define EDQUOT  ENOSPC
212 #endif
213
214 static struct {
215         enum nfsstat stat;
216         int errnum;
217 } nfs_errtbl[] = {
218         { NFS_OK,               0               },
219         { NFSERR_PERM,          EPERM           },
220         { NFSERR_NOENT,         ENOENT          },
221         { NFSERR_IO,            EIO             },
222         { NFSERR_NXIO,          ENXIO           },
223         { NFSERR_ACCES,         EACCES          },
224         { NFSERR_EXIST,         EEXIST          },
225         { NFSERR_NODEV,         ENODEV          },
226         { NFSERR_NOTDIR,        ENOTDIR         },
227         { NFSERR_ISDIR,         EISDIR          },
228 #ifdef NFSERR_INVAL
229         { NFSERR_INVAL,         EINVAL          },      /* that Sun forgot */
230 #endif
231         { NFSERR_FBIG,          EFBIG           },
232         { NFSERR_NOSPC,         ENOSPC          },
233         { NFSERR_ROFS,          EROFS           },
234         { NFSERR_NAMETOOLONG,   ENAMETOOLONG    },
235         { NFSERR_NOTEMPTY,      ENOTEMPTY       },
236         { NFSERR_DQUOT,         EDQUOT          },
237         { NFSERR_STALE,         ESTALE          },
238 #ifdef EWFLUSH
239         { NFSERR_WFLUSH,        EWFLUSH         },
240 #endif
241         /* Throw in some NFSv3 values for even more fun (HP returns these) */
242         { 71,                   EREMOTE         },
243
244         { -1,                   EIO             }
245 };
246
247 char *nfs_strerror(int stat)
248 {
249         int i;
250         static char buf[256];
251
252         for (i = 0; nfs_errtbl[i].stat != -1; i++) {
253                 if (nfs_errtbl[i].stat == stat)
254                         return strerror(nfs_errtbl[i].errnum);
255         }
256         sprintf(buf, _("unknown nfs status return value: %d"), stat);
257         return buf;
258 }