]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/error.c
3f7458c96ed0574ff3237ba0ac77a077642b3606
[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 /**
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
84  *
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).
87  */
88 void rpc_mount_errors(char *server, int will_retry, int bg)
89 {
90         int pos = 0;
91         char *tmp;
92         static int onlyonce = 0;
93
94         tmp = &errbuf[pos];
95         if (bg)
96                 pos = snprintf(tmp, (erreob - tmp),
97                                 _("mount to NFS server '%s' failed: "),
98                                         server);
99         else
100                 pos = snprintf(tmp, (erreob - tmp),
101                                 _("%s: mount to NFS server '%s' failed: "),
102                                         progname, server);
103
104         tmp = &errbuf[pos];
105         if (rpc_createerr.cf_stat == RPC_TIMEDOUT) {
106                 if (will_retry)
107                         pos = snprintf(tmp, (erreob - tmp),
108                                         _("timed out, retrying"));
109                 else
110                         pos = snprintf(tmp, (erreob - tmp),
111                                         _("timed out, giving up"));
112         } else {
113                 pos += rpc_strerror(pos);
114                 tmp = &errbuf[pos];
115                 if (bg) {
116                         if (will_retry)
117                                 pos = snprintf(tmp, (erreob - tmp),
118                                                 _(", retrying"));
119                         else
120                                 pos = snprintf(tmp, (erreob - tmp),
121                                                 _(", giving up"));
122                 }
123         }
124
125         if (bg) {
126                 if (onlyonce++ < 1)
127                         openlog("mount", LOG_CONS|LOG_PID, LOG_AUTH);
128                 syslog(LOG_ERR, "%s", errbuf);
129         } else
130                 fprintf(stderr, "%s\n", errbuf);
131 }
132
133 /*
134  * mount_error - report a foreground mount error
135  * @spec: C string containing the device name being mounted
136  * @mount_point: C string containing the pathname of the local mounted on dir
137  * @error: errno value to report
138  *
139  */
140 void mount_error(const char *spec, const char *mount_point, int error)
141 {
142         switch(error) {
143         case ENOTDIR:
144                 nfs_error(_("%s: mount point %s is not a directory"),
145                                 progname, mount_point);
146                 break;
147         case EBUSY:
148                 nfs_error(_("%s: %s is already mounted or busy"),
149                         progname, mount_point);
150                 break;
151         case ENOENT:
152                 if (spec)
153                         nfs_error(_("%s: mounting %s failed, "
154                                 "reason given by server:\n  %s"),
155                                 progname, spec, strerror(error));
156                 else
157                         nfs_error(_("%s: mount point %s does not exist"),
158                                 progname, mount_point);
159                 break;
160         default:
161                 nfs_error(_("%s: %s"),
162                         progname, strerror(error));
163         }
164 }
165
166 /*
167  * umount_error - report a failed umount request
168  * @err: errno value to report
169  * @dev: C string containing the pathname of the local mounted on dir
170  *
171  */
172 void umount_error(int err, const char *dev)
173 {
174         switch (err) {
175         case ENXIO:
176                 nfs_error(_("%s: %s: invalid block device"),
177                         progname, dev);
178                 break;
179         case EINVAL:
180                 nfs_error(_("%s: %s: not mounted"),
181                         progname, dev);
182                 break;
183         case EIO:
184                 nfs_error(_("%s: %s: can't write superblock"),
185                         progname, dev);
186                 break;
187         case EBUSY:
188                 nfs_error(_("%s: %s: device is busy"),
189                         progname, dev);
190                 break;
191         case ENOENT:
192                 nfs_error(_("%s: %s: not found"),
193                         progname, dev);
194                 break;
195         case EPERM:
196                 nfs_error(_("%s: %s: must be superuser to umount"),
197                         progname, dev);
198                 break;
199         case EACCES:
200                 nfs_error(_("%s: %s: block devices not permitted on fs"),
201                         progname, dev);
202                 break;
203         default:
204                 nfs_error(_("%s: %s: %s"),
205                         progname, dev, strerror(err));
206                 break;
207         }
208 }
209
210 /*
211  * We need to translate between nfs status return values and
212  * the local errno values which may not be the same.
213  *
214  * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
215  * "after #include <errno.h> the symbol errno is reserved for any use,
216  *  it cannot even be used as a struct tag or field name".
217  */
218
219 #ifndef EDQUOT
220 #define EDQUOT  ENOSPC
221 #endif
222
223 static struct {
224         enum nfsstat stat;
225         int errnum;
226 } nfs_errtbl[] = {
227         { NFS_OK,               0               },
228         { NFSERR_PERM,          EPERM           },
229         { NFSERR_NOENT,         ENOENT          },
230         { NFSERR_IO,            EIO             },
231         { NFSERR_NXIO,          ENXIO           },
232         { NFSERR_ACCES,         EACCES          },
233         { NFSERR_EXIST,         EEXIST          },
234         { NFSERR_NODEV,         ENODEV          },
235         { NFSERR_NOTDIR,        ENOTDIR         },
236         { NFSERR_ISDIR,         EISDIR          },
237 #ifdef NFSERR_INVAL
238         { NFSERR_INVAL,         EINVAL          },      /* that Sun forgot */
239 #endif
240         { NFSERR_FBIG,          EFBIG           },
241         { NFSERR_NOSPC,         ENOSPC          },
242         { NFSERR_ROFS,          EROFS           },
243         { NFSERR_NAMETOOLONG,   ENAMETOOLONG    },
244         { NFSERR_NOTEMPTY,      ENOTEMPTY       },
245         { NFSERR_DQUOT,         EDQUOT          },
246         { NFSERR_STALE,         ESTALE          },
247 #ifdef EWFLUSH
248         { NFSERR_WFLUSH,        EWFLUSH         },
249 #endif
250         /* Throw in some NFSv3 values for even more fun (HP returns these) */
251         { 71,                   EREMOTE         },
252
253         { -1,                   EIO             }
254 };
255
256 char *nfs_strerror(int stat)
257 {
258         int i;
259         static char buf[256];
260
261         for (i = 0; nfs_errtbl[i].stat != -1; i++) {
262                 if (nfs_errtbl[i].stat == stat)
263                         return strerror(nfs_errtbl[i].errnum);
264         }
265         sprintf(buf, _("unknown nfs status return value: %d"), stat);
266         return buf;
267 }