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