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