]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/nfsumount.c
Move NFS mount code from util-linux to nfs-utils - part 1
[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 "nfsmount.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 pmap *pmap = &mnt_server.pmap;
261         char *p;
262
263         nfs_mount_version = find_kernel_nfs_mount_version();
264         if (spec == NULL || (p = strchr(spec,':')) == NULL)
265                 goto out_bad;
266         hostname = xstrndup(spec, p-spec);
267         dirname = xstrdup(p+1);
268 #ifdef NFS_MOUNT_DEBUG
269         printf(_("host: %s, directory: %s\n"), hostname, dirname);
270 #endif
271
272         if (opts && (p = strstr(opts, "addr="))) {
273                 char *q;
274
275                 free(hostname);
276                 p += 5;
277                 q = p;
278                 while (*q && *q != ',') q++;
279                 hostname = xstrndup(p,q-p);
280         }
281
282         if (opts && (p = strstr(opts, "mounthost="))) {
283                 char *q;
284
285                 free(hostname);
286                 p += 10;
287                 q = p;
288                 while (*q && *q != ',') q++;
289                 hostname = xstrndup(p,q-p);
290         }
291
292         pmap->pm_prog = MOUNTPROG;
293         pmap->pm_vers = MOUNTVERS;
294         pmap->pm_prot = get_mntproto(spec);
295         if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
296                 pmap->pm_prog = atoi(p+10);
297         if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
298                 pmap->pm_port = atoi(p+10);
299         if (opts && (p = strstr(opts, "nfsvers=")) && isdigit(*(p+8)))
300                 pmap->pm_vers = nfsvers_to_mnt(atoi(p+8));
301         if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
302                 pmap->pm_vers = atoi(p+10);
303
304         if (!nfs_gethostbyname(hostname, &mnt_server.saddr))
305                 goto out_bad;
306         if (!probe_mntport(&mnt_server))
307                 goto out_bad;
308         return nfs_call_umount(&mnt_server, &dirname);
309  out_bad:
310         printf("%s: %s: not found or not mounted\n", progname, spec);
311         return 0;
312 }
313
314 static struct option umount_longopts[] =
315 {
316   { "force", 0, 0, 'f' },
317   { "help", 0, 0, 'h' },
318   { "no-mtab", 0, 0, 'n' },
319   { "verbose", 0, 0, 'v' },
320   { "read-only", 0, 0, 'r' },
321   { NULL, 0, 0, 0 }
322 };
323
324 void umount_usage()
325 {
326         printf("usage: %s dir [-fvnrlh]\n", progname);
327         printf("options:\n\t-f\t\tforce unmount\n");
328         printf("\t-v\t\tverbose\n");
329         printf("\t-n\t\tDo not update /etc/mtab\n");
330         printf("\t-r\t\tremount\n");
331         printf("\t-l\t\tlazy unmount\n");
332         printf("\t-h\t\tprint this help\n\n");
333 }
334
335 int nfsumount(int argc, char *argv[])
336 {
337         int c, ret;
338         char *spec;
339         struct mntentchn *mc;
340
341         spec = argv[1];
342
343         argv += 1;
344         argc -= 1;
345
346         while ((c = getopt_long (argc, argv, "fvnrlh",
347                                 umount_longopts, NULL)) != -1) {
348
349                 switch (c) {
350                 case 'f':
351                         ++force;
352                         break;
353                 case 'v':
354                         ++verbose;
355                         break;
356                 case 'n':
357                         ++nomtab;
358                         break;
359                 case 'r':
360                         ++remount;
361                         break;
362                 case 'l':
363                         ++lazy;
364                         break;
365                 case 'h':
366                 default:
367                         umount_usage();
368                         return 0;
369                 }
370         }
371
372         mc = getmntdirbackward(spec, NULL);
373         if (!mc)
374                 mc = getmntdevbackward(spec, NULL);
375         if (!mc && verbose)
376                 printf(_("Could not find %s in mtab\n"), spec);
377
378         if(mc) {
379                 ret = _nfsumount(mc->m.mnt_fsname, mc->m.mnt_opts);
380                 if(ret)
381                         ret = add_mtab2(mc->m.mnt_fsname, mc->m.mnt_dir,
382                                 mc->m.mnt_type, mc->m.mnt_opts, mc);
383         }
384         else {
385                 ret = _nfsumount(spec, NULL);
386                 if(ret)
387                         ret = add_mtab2(spec, spec, spec, spec, NULL);
388         }
389
390         return(ret);
391 }
392