]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/nfsumount.c
Use correct UMNT protocol 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 nfs_umount(dirpath *argp, CLIENT *clnt)
59 {
60         return clnt_call(clnt, MOUNTPROC_UMNT,
61                          (xdrproc_t) xdr_dirpath, (caddr_t)argp,
62                          (xdrproc_t) xdr_void, NULL,
63                          TIMEOUT);
64 }
65
66 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
67 {
68         CLIENT *clnt;
69         enum clnt_stat res = 0;
70         int msock;
71
72         switch (mnt_server->pmap.pm_vers) {
73         case 3:
74         case 2:
75         case 1:
76                 if (!probe_mntport(mnt_server))
77                         goto out_bad;
78                 clnt = mnt_openclnt(mnt_server, &msock);
79                 if (!clnt)
80                         goto out_bad;
81                 res = nfs_umount(argp, clnt);
82                 mnt_closeclnt(clnt, msock);
83                 if (res == RPC_SUCCESS)
84                         return 1;
85                 break;
86         default:
87                 res = 1;
88                 break;
89         }
90  out_bad:
91         return res;
92 }
93
94 struct mntentchn *
95 getmntentchn(const char *dir)
96 {
97         mntFILE *mfp;
98         nfs_mntent_t *mnt;
99         struct mntentchn *mc = NULL;
100
101         mfp = nfs_setmntent ("/proc/mounts", "r");
102         if (mfp == NULL || mfp->mntent_fp == NULL)
103                 return NULL;
104
105         while ((mnt = nfs_getmntent(mfp)) != NULL) {
106                 if(strcmp(mnt->mnt_fsname, dir) && strcmp(mnt->mnt_dir, dir))
107                         continue;
108
109                 if (!mc)
110                         mc = (struct mntentchn *)xmalloc(sizeof(*mc));
111                 mc->m = *mnt;
112         }
113
114         nfs_endmntent(mfp);
115         return mc;
116 }
117
118 /* complain about a failed umount */
119 static void complain(int err, const char *dev) {
120   switch (err) {
121     case ENXIO:
122       nfs_error (_("umount: %s: invalid block device"), dev); break;
123     case EINVAL:
124       nfs_error (_("umount: %s: not mounted"), dev); break;
125     case EIO:
126       nfs_error (_("umount: %s: can't write superblock"), dev); break;
127     case EBUSY:
128      /* Let us hope fstab has a line "proc /proc ..."
129         and not "none /proc ..."*/
130       nfs_error (_("umount: %s: device is busy"), dev); break;
131     case ENOENT:
132       nfs_error (_("umount: %s: not found"), dev); break;
133     case EPERM:
134       nfs_error (_("umount: %s: must be superuser to umount"), dev); break;
135     case EACCES:
136       nfs_error (_("umount: %s: block devices not permitted on fs"), dev); break;
137     default:
138       nfs_error (_("umount: %s: %s"), dev, strerror (err)); break;
139   }
140 }
141
142 int add_mtab2(const char *spec, const char *node, const char *type,
143                 const char *opts, struct mntentchn *mc)
144 {
145         int umnt_err, umnt_err2, res;
146
147         umnt_err = umnt_err2 = 0;
148         if (lazy) {
149                 res = umount2 (node, MNT_DETACH);
150                 if (res < 0)
151                         umnt_err = errno;
152                 goto writemtab;
153         }
154
155         if (force) {
156                 res = umount2 (node, MNT_FORCE);
157                 if (res == -1) {
158                         int errsv = errno;
159                         perror("umount2");
160                         errno = errsv;
161                         if (errno == ENOSYS) {
162                                 if (verbose)
163                                         printf(_("no umount2, trying umount...\n"));
164                                 res = umount (node);
165                         }
166                 }
167         } else
168                 res = umount (node);
169
170         if (res < 0) {
171                 umnt_err = errno;
172                 /* A device might have been mounted on a node that has since
173                    been deleted or renamed, so if node fails, also try spec. */
174                 /* Note that this is incorrect in case spec was mounted
175                    several times. */
176                 /* if (umnt_err == ENOENT || umnt_err == EINVAL) */
177                 if (umnt_err != EBUSY && strcmp(node, spec)) {
178                         if (verbose)
179                                 printf (_("could not umount %s - trying %s instead\n"),
180                                         node, spec);
181                         res = umount (spec);
182                         if (res < 0)
183                                 umnt_err2 = errno;
184                        /* Do not complain about remote NFS mount points */
185                         if (errno == ENOENT && index(spec, ':'))
186                                 umnt_err2 = 0;
187                 }
188         }
189
190         if (res < 0 && remount && (umnt_err == EBUSY || umnt_err2 == EBUSY)) {
191                 /* Umount failed - let us try a remount */
192                 res = mount(spec, node, NULL,
193                             MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
194                 if (res == 0) {
195                         nfs_mntent_t remnt;
196                         fprintf(stderr,
197                                 _("umount: %s busy - remounted read-only\n"),
198                                 spec);
199                         remnt.mnt_type = remnt.mnt_fsname = NULL;
200                         remnt.mnt_dir = xstrdup(node);
201                         remnt.mnt_opts = xstrdup("ro");
202                         if (!nomtab)
203                                 update_mtab(node, &remnt);
204                         return 0;
205                 } else if (errno != EBUSY) {    /* hmm ... */
206                         perror("remount");
207                         fprintf(stderr,
208                                 _("umount: could not remount %s read-only\n"),
209                                 spec);
210                 }
211         }
212
213         if (res >= 0) {
214                 /* Umount succeeded */
215                 if (verbose)
216                         printf (_("%s umounted\n"), spec);
217         }
218
219  writemtab:
220         if (!nomtab &&
221             (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
222                update_mtab (node, NULL);
223         }
224
225         if (res >= 0)
226                 return 0;
227
228         if (umnt_err2)
229                 complain(umnt_err2, spec);
230         if (umnt_err && umnt_err != umnt_err2)
231                 complain(umnt_err, node);
232         return 1;
233 }
234
235 /*
236  * Returns 1 if everything went well, else 0.
237  */
238 int _nfsumount(const char *spec, const char *opts)
239 {
240         char *hostname;
241         char *dirname;
242         clnt_addr_t mnt_server = { &hostname, };
243         struct mntent mnt = { .mnt_opts = opts };
244         struct pmap *pmap = &mnt_server.pmap;
245         char *p;
246
247         nfs_mount_version = find_kernel_nfs_mount_version();
248         if (spec == NULL || (p = strchr(spec,':')) == NULL)
249                 goto out_bad;
250         hostname = xstrndup(spec, p-spec);
251         dirname = xstrdup(p+1);
252 #ifdef NFS_MOUNT_DEBUG
253         printf(_("host: %s, directory: %s\n"), hostname, dirname);
254 #endif
255
256         if (opts && (p = strstr(opts, "addr="))) {
257                 char *q;
258
259                 free(hostname);
260                 p += 5;
261                 q = p;
262                 while (*q && *q != ',') q++;
263                 hostname = xstrndup(p,q-p);
264         }
265
266         if (opts && (p = strstr(opts, "mounthost="))) {
267                 char *q;
268
269                 free(hostname);
270                 p += 10;
271                 q = p;
272                 while (*q && *q != ',') q++;
273                 hostname = xstrndup(p,q-p);
274         }
275
276         pmap->pm_prog = MOUNTPROG;
277         pmap->pm_vers = MOUNTVERS_NFSV3;
278         pmap->pm_prot = IPPROTO_TCP;
279         if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
280                 pmap->pm_prog = atoi(p+10);
281         if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
282                 pmap->pm_port = atoi(p+10);
283         if (opts && hasmntopt(&mnt, "v2"))
284                 pmap->pm_vers = nfsvers_to_mnt(2);
285         if (opts && hasmntopt(&mnt, "v3"))
286                 pmap->pm_vers = nfsvers_to_mnt(3);
287         if (opts && hasmntopt(&mnt, "v4"))
288                 pmap->pm_vers = nfsvers_to_mnt(4);
289         if (opts && (p = strstr(opts, "vers=")) && isdigit(*(p+5)))
290                 pmap->pm_vers = nfsvers_to_mnt(atoi(p+5));
291         if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
292                 pmap->pm_vers = atoi(p+10);
293         if (opts && (hasmntopt(&mnt, "udp") || hasmntopt(&mnt, "proto=udp")))
294                 pmap->pm_prot = IPPROTO_UDP;
295
296         if (!nfs_gethostbyname(hostname, &mnt_server.saddr))
297                 goto out_bad;
298         return nfs_call_umount(&mnt_server, &dirname);
299  out_bad:
300         printf("%s: %s: not found or not mounted\n", progname, spec);
301         return 0;
302 }
303
304 static struct option umount_longopts[] =
305 {
306   { "force", 0, 0, 'f' },
307   { "help", 0, 0, 'h' },
308   { "no-mtab", 0, 0, 'n' },
309   { "verbose", 0, 0, 'v' },
310   { "read-only", 0, 0, 'r' },
311   { NULL, 0, 0, 0 }
312 };
313
314 void umount_usage()
315 {
316         printf("usage: %s dir [-fvnrlh]\n", progname);
317         printf("options:\n\t-f\t\tforce unmount\n");
318         printf("\t-v\t\tverbose\n");
319         printf("\t-n\t\tDo not update /etc/mtab\n");
320         printf("\t-r\t\tremount\n");
321         printf("\t-l\t\tlazy unmount\n");
322         printf("\t-h\t\tprint this help\n\n");
323 }
324
325 int nfsumount(int argc, char *argv[])
326 {
327         int c, ret;
328         char *spec;
329         struct mntentchn *mc;
330
331         spec = argv[1];
332
333         argv += 1;
334         argc -= 1;
335
336         while ((c = getopt_long (argc, argv, "fvnrlh",
337                                 umount_longopts, NULL)) != -1) {
338
339                 switch (c) {
340                 case 'f':
341                         ++force;
342                         break;
343                 case 'v':
344                         ++verbose;
345                         break;
346                 case 'n':
347                         ++nomtab;
348                         break;
349                 case 'r':
350                         ++remount;
351                         break;
352                 case 'l':
353                         ++lazy;
354                         break;
355                 case 'h':
356                 default:
357                         umount_usage();
358                         return 0;
359                 }
360         }
361         
362         if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
363                 printf(_("umount: %s: not found\n"), spec);
364                 return 0;
365         }
366
367         mc = getmntentchn(spec);
368         if (!mc)
369                 mc = getmntdirbackward(spec, NULL);
370         if (!mc)
371                 mc = getmntdevbackward(spec, NULL);
372         if (!mc && verbose)
373                 printf(_("Could not find %s in mtab\n"), spec);
374
375         if(mc) {
376                 ret = _nfsumount(mc->m.mnt_fsname, mc->m.mnt_opts);
377                 if(ret)
378                         ret = add_mtab2(mc->m.mnt_fsname, mc->m.mnt_dir,
379                                 mc->m.mnt_type, mc->m.mnt_opts, mc);
380         }
381         else {
382                 ret = _nfsumount(spec, NULL);
383                 if(ret)
384                         ret = add_mtab2(spec, spec, spec, spec, NULL);
385         }
386
387         return(ret);
388 }
389