]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/nfsumount.c
Use correct UMNT version to do umount
[nfs-utils.git] / utils / mount / nfsumount.c
1 /*
2  * nfsumount.c -- Linux NFS umount
3  * Copyright (C) 2006 Amit Gud <agud@redhat.com>
4  *
5  * - Basic code and wrapper around NFS umount code originally
6  *   in util-linux/mount/nfsmount.c
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19
20 #include <stdio.h>
21 #include <errno.h>
22 #include <getopt.h>
23 #include <mntent.h>
24 #include <sys/mount.h>
25 #include <ctype.h>
26
27 #include "xcommon.h"
28 #include "fstab.h"
29 #include "nls.h"
30 #include "conn.h"
31
32 #include "mount_constants.h"
33 #include "mount.h"
34 #include "nfsumount.h"
35
36 #if !defined(MNT_FORCE)
37 /* dare not try to include <linux/mount.h> -- lots of errors */
38 #define MNT_FORCE 1
39 #endif
40
41 #if !defined(MNT_DETACH)
42 #define MNT_DETACH 2
43 #endif
44
45 extern char *progname;
46 extern int nfs_mount_version;
47 extern int nomtab;
48 extern int verbose;
49 int force;
50 int lazy;
51 int remount;
52
53 extern int find_kernel_nfs_mount_version(void);
54 extern int probe_mntport(clnt_addr_t *);
55 extern int nfs_gethostbyname(const char *, struct sockaddr_in *);
56
57 static inline enum clnt_stat
58 nfs3_umount(dirpath *argp, CLIENT *clnt)
59 {
60         static char clnt_res;
61         memset (&clnt_res, 0, sizeof(clnt_res));
62         return clnt_call(clnt, MOUNTPROC_UMNT,
63                          (xdrproc_t) xdr_dirpath, (caddr_t)argp,
64                          (xdrproc_t) xdr_void, (caddr_t) &clnt_res,
65                          TIMEOUT);
66 }
67
68 static inline enum clnt_stat
69 nfs2_umount(dirpath *argp, CLIENT *clnt)
70 {
71         static char clnt_res;
72         memset (&clnt_res, 0, sizeof(clnt_res));
73         return clnt_call(clnt, MOUNTPROC_UMNT,
74                          (xdrproc_t) xdr_dirpath, (caddr_t)argp,
75                          (xdrproc_t) xdr_void, (caddr_t) &clnt_res,
76                          TIMEOUT);
77 }
78
79 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
80 {
81         CLIENT *clnt;
82         enum clnt_stat res = 0;
83         int msock;
84
85         clnt = mnt_openclnt(mnt_server, &msock);
86         if (!clnt)
87                 goto out_bad;
88         switch (mnt_server->pmap.pm_vers) {
89         case 3:
90                 res = nfs3_umount(argp, clnt);
91                 break;
92         case 2:
93         case 1:
94                 res = nfs2_umount(argp, clnt);
95                 break;
96         default:
97                 break;
98         }
99         mnt_closeclnt(clnt, msock);
100         if (res == RPC_SUCCESS)
101                 return 1;
102  out_bad:
103         return 0;
104 }
105
106 u_int get_mntproto(const char *);
107 u_int
108 get_mntproto(const char *dirname)
109 {
110         FILE *mtab;
111         struct mntent mntbuf;
112         char tmpbuf[BUFSIZ];
113         u_int proto = IPPROTO_TCP; /* assume tcp */
114
115          mtab = setmntent ("/proc/mounts", "r");
116          if (mtab == NULL)
117                 mtab = setmntent (_PATH_MOUNTED, "r");
118         if (mtab == NULL)
119                 return proto;
120
121         while(getmntent_r(mtab, &mntbuf, tmpbuf, sizeof (tmpbuf))) {
122                 if (strcmp(mntbuf.mnt_type, "nfs"))
123                         continue;
124                 if (strcmp(dirname,  mntbuf.mnt_fsname))
125                         continue;
126                 if (hasmntopt(&mntbuf, "udp"))
127                         proto = IPPROTO_UDP;
128                 break;
129         }
130         endmntent (mtab);
131
132         return proto;
133 }
134
135 /* complain about a failed umount */
136 static void complain(int err, const char *dev) {
137   switch (err) {
138     case ENXIO:
139       nfs_error (_("umount: %s: invalid block device"), dev); break;
140     case EINVAL:
141       nfs_error (_("umount: %s: not mounted"), dev); break;
142     case EIO:
143       nfs_error (_("umount: %s: can't write superblock"), dev); break;
144     case EBUSY:
145      /* Let us hope fstab has a line "proc /proc ..."
146         and not "none /proc ..."*/
147       nfs_error (_("umount: %s: device is busy"), dev); break;
148     case ENOENT:
149       nfs_error (_("umount: %s: not found"), dev); break;
150     case EPERM:
151       nfs_error (_("umount: %s: must be superuser to umount"), dev); break;
152     case EACCES:
153       nfs_error (_("umount: %s: block devices not permitted on fs"), dev); break;
154     default:
155       nfs_error (_("umount: %s: %s"), dev, strerror (err)); break;
156   }
157 }
158
159 int add_mtab2(const char *spec, const char *node, const char *type,
160                 const char *opts, struct mntentchn *mc)
161 {
162         int umnt_err, umnt_err2, res;
163
164         umnt_err = umnt_err2 = 0;
165         if (lazy) {
166                 res = umount2 (node, MNT_DETACH);
167                 if (res < 0)
168                         umnt_err = errno;
169                 goto writemtab;
170         }
171
172         if (force) {
173                 res = umount2 (node, MNT_FORCE);
174                 if (res == -1) {
175                         int errsv = errno;
176                         perror("umount2");
177                         errno = errsv;
178                         if (errno == ENOSYS) {
179                                 if (verbose)
180                                         printf(_("no umount2, trying umount...\n"));
181                                 res = umount (node);
182                         }
183                 }
184         } else
185                 res = umount (node);
186
187         if (res < 0) {
188                 umnt_err = errno;
189                 /* A device might have been mounted on a node that has since
190                    been deleted or renamed, so if node fails, also try spec. */
191                 /* Note that this is incorrect in case spec was mounted
192                    several times. */
193                 /* if (umnt_err == ENOENT || umnt_err == EINVAL) */
194                 if (umnt_err != EBUSY && strcmp(node, spec)) {
195                         if (verbose)
196                                 printf (_("could not umount %s - trying %s instead\n"),
197                                         node, spec);
198                         res = umount (spec);
199                         if (res < 0)
200                                 umnt_err2 = errno;
201                        /* Do not complain about remote NFS mount points */
202                         if (errno == ENOENT && index(spec, ':'))
203                                 umnt_err2 = 0;
204                 }
205         }
206
207         if (res < 0 && remount && (umnt_err == EBUSY || umnt_err2 == EBUSY)) {
208                 /* Umount failed - let us try a remount */
209                 res = mount(spec, node, NULL,
210                             MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
211                 if (res == 0) {
212                         nfs_mntent_t remnt;
213                         fprintf(stderr,
214                                 _("umount: %s busy - remounted read-only\n"),
215                                 spec);
216                         remnt.mnt_type = remnt.mnt_fsname = NULL;
217                         remnt.mnt_dir = xstrdup(node);
218                         remnt.mnt_opts = xstrdup("ro");
219                         if (!nomtab)
220                                 update_mtab(node, &remnt);
221                         return 0;
222                 } else if (errno != EBUSY) {    /* hmm ... */
223                         perror("remount");
224                         fprintf(stderr,
225                                 _("umount: could not remount %s read-only\n"),
226                                 spec);
227                 }
228         }
229
230         if (res >= 0) {
231                 /* Umount succeeded */
232                 if (verbose)
233                         printf (_("%s umounted\n"), spec);
234         }
235
236  writemtab:
237         if (!nomtab &&
238             (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
239                update_mtab (node, NULL);
240         }
241
242         if (res >= 0)
243                 return 0;
244
245         if (umnt_err2)
246                 complain(umnt_err2, spec);
247         if (umnt_err && umnt_err != umnt_err2)
248                 complain(umnt_err, node);
249         return 1;
250 }
251
252 /*
253  * Returns 1 if everything went well, else 0.
254  */
255 int _nfsumount(const char *spec, const char *opts)
256 {
257         char *hostname;
258         char *dirname;
259         clnt_addr_t mnt_server = { &hostname, };
260         struct mntent mnt = { .mnt_opts = opts };
261         struct pmap *pmap = &mnt_server.pmap;
262         char *p;
263
264         nfs_mount_version = find_kernel_nfs_mount_version();
265         if (spec == NULL || (p = strchr(spec,':')) == NULL)
266                 goto out_bad;
267         hostname = xstrndup(spec, p-spec);
268         dirname = xstrdup(p+1);
269 #ifdef NFS_MOUNT_DEBUG
270         printf(_("host: %s, directory: %s\n"), hostname, dirname);
271 #endif
272
273         if (opts && (p = strstr(opts, "addr="))) {
274                 char *q;
275
276                 free(hostname);
277                 p += 5;
278                 q = p;
279                 while (*q && *q != ',') q++;
280                 hostname = xstrndup(p,q-p);
281         }
282
283         if (opts && (p = strstr(opts, "mounthost="))) {
284                 char *q;
285
286                 free(hostname);
287                 p += 10;
288                 q = p;
289                 while (*q && *q != ',') q++;
290                 hostname = xstrndup(p,q-p);
291         }
292
293         pmap->pm_prog = MOUNTPROG;
294         pmap->pm_vers = MOUNTVERS_NFSV3;
295         pmap->pm_prot = get_mntproto(spec);
296         if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
297                 pmap->pm_prog = atoi(p+10);
298         if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
299                 pmap->pm_port = atoi(p+10);
300         if (opts && hasmntopt(&mnt, "v2"))
301                 pmap->pm_vers = nfsvers_to_mnt(2);
302         if (opts && hasmntopt(&mnt, "v3"))
303                 pmap->pm_vers = nfsvers_to_mnt(3);
304         if (opts && hasmntopt(&mnt, "v4"))
305                 pmap->pm_vers = nfsvers_to_mnt(4);
306         if (opts && (p = strstr(opts, "vers=")) && isdigit(*(p+5)))
307                 pmap->pm_vers = nfsvers_to_mnt(atoi(p+5));
308         if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
309                 pmap->pm_vers = atoi(p+10);
310
311         if (!nfs_gethostbyname(hostname, &mnt_server.saddr))
312                 goto out_bad;
313         if (!probe_mntport(&mnt_server))
314                 goto out_bad;
315         return nfs_call_umount(&mnt_server, &dirname);
316  out_bad:
317         printf("%s: %s: not found or not mounted\n", progname, spec);
318         return 0;
319 }
320
321 static struct option umount_longopts[] =
322 {
323   { "force", 0, 0, 'f' },
324   { "help", 0, 0, 'h' },
325   { "no-mtab", 0, 0, 'n' },
326   { "verbose", 0, 0, 'v' },
327   { "read-only", 0, 0, 'r' },
328   { NULL, 0, 0, 0 }
329 };
330
331 void umount_usage()
332 {
333         printf("usage: %s dir [-fvnrlh]\n", progname);
334         printf("options:\n\t-f\t\tforce unmount\n");
335         printf("\t-v\t\tverbose\n");
336         printf("\t-n\t\tDo not update /etc/mtab\n");
337         printf("\t-r\t\tremount\n");
338         printf("\t-l\t\tlazy unmount\n");
339         printf("\t-h\t\tprint this help\n\n");
340 }
341
342 int nfsumount(int argc, char *argv[])
343 {
344         int c, ret;
345         char *spec;
346         struct mntentchn *mc;
347
348         spec = argv[1];
349
350         argv += 1;
351         argc -= 1;
352
353         while ((c = getopt_long (argc, argv, "fvnrlh",
354                                 umount_longopts, NULL)) != -1) {
355
356                 switch (c) {
357                 case 'f':
358                         ++force;
359                         break;
360                 case 'v':
361                         ++verbose;
362                         break;
363                 case 'n':
364                         ++nomtab;
365                         break;
366                 case 'r':
367                         ++remount;
368                         break;
369                 case 'l':
370                         ++lazy;
371                         break;
372                 case 'h':
373                 default:
374                         umount_usage();
375                         return 0;
376                 }
377         }
378
379         mc = getmntdirbackward(spec, NULL);
380         if (!mc)
381                 mc = getmntdevbackward(spec, NULL);
382         if (!mc && verbose)
383                 printf(_("Could not find %s in mtab\n"), spec);
384
385         if(mc) {
386                 ret = _nfsumount(mc->m.mnt_fsname, mc->m.mnt_opts);
387                 if(ret)
388                         ret = add_mtab2(mc->m.mnt_fsname, mc->m.mnt_dir,
389                                 mc->m.mnt_type, mc->m.mnt_opts, mc);
390         }
391         else {
392                 ret = _nfsumount(spec, NULL);
393                 if(ret)
394                         ret = add_mtab2(spec, spec, spec, spec, NULL);
395         }
396
397         return(ret);
398 }
399