]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/nfsumount.c
umount.nfs: fix nfs4 check
[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 <unistd.h>
21 #include <stdio.h>
22 #include <errno.h>
23 #include <getopt.h>
24 #include <mntent.h>
25 #include <sys/mount.h>
26 #include <ctype.h>
27 #include <pwd.h>
28
29 #include "xcommon.h"
30 #include "fstab.h"
31 #include "nls.h"
32
33 #include "mount_constants.h"
34 #include "nfs_mount.h"
35 #include "mount.h"
36 #include "error.h"
37 #include "network.h"
38 #include "parse_opt.h"
39 #include "parse_dev.h"
40 #include "utils.h"
41
42 #define MOUNTSFILE      "/proc/mounts"
43 #define LINELEN         (4096)
44
45 #if !defined(MNT_FORCE)
46 /* dare not try to include <linux/mount.h> -- lots of errors */
47 #define MNT_FORCE 1
48 #endif
49
50 #if !defined(MNT_DETACH)
51 #define MNT_DETACH 2
52 #endif
53
54 extern char *progname;
55 extern int nomtab;
56 extern int verbose;
57 int force;
58 int lazy;
59 int remount;
60
61
62 static int try_remount(const char *spec, const char *node)
63 {
64         int res;
65
66         res = mount(spec, node, NULL,
67                     MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
68         if (res == 0) {
69                 struct mntent remnt;
70                 nfs_error(_("%s: %s busy - remounted read-only"),
71                                 progname, spec);
72                 remnt.mnt_type = remnt.mnt_fsname = NULL;
73                 remnt.mnt_dir = xstrdup(node);
74                 remnt.mnt_opts = xstrdup("ro");
75                 if (!nomtab)
76                         update_mtab(node, &remnt);
77         } else if (errno != EBUSY) {    /* hmm ... */
78                 perror(_("remount"));
79                 nfs_error(_("%s: could not remount %s read-only"),
80                                 progname, spec);
81         }
82         return res;
83 }
84
85 static int del_mtab(const char *spec, const char *node)
86 {
87         int umnt_err, res;
88
89         umnt_err = 0;
90         if (lazy) {
91                 res = umount2 (node, MNT_DETACH);
92                 if (res < 0)
93                         umnt_err = errno;
94                 goto writemtab;
95         }
96
97         if (force) {
98                 res = umount2 (node, MNT_FORCE);
99                 if (res == -1) {
100                         int errsv = errno;
101                         perror(_("umount2"));
102                         errno = errsv;
103                         if (errno == ENOSYS) {
104                                 if (verbose)
105                                         printf(_("no umount2, trying umount...\n"));
106                                 res = umount (node);
107                         }
108                 }
109         } else
110                 res = umount (node);
111
112         if (res < 0) {
113                 if (remount && errno == EBUSY && spec) {
114                         res = try_remount(spec, node);
115                         if (res)
116                                 goto writemtab;
117                         return EX_SUCCESS;
118                 } else
119                         umnt_err = errno;
120         }
121
122         if (res >= 0) {
123                 /* Umount succeeded */
124                 if (verbose)
125                         printf(_("%s umounted\n"), spec ? spec : node);
126         }
127
128  writemtab:
129         if (!nomtab &&
130             (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
131                 update_mtab(node, NULL);
132         }
133
134         if (res >= 0)
135                 return EX_SUCCESS;
136
137         if (umnt_err)
138                 umount_error(umnt_err, node);
139         return EX_FILEIO;
140 }
141
142 /*
143  * Detect NFSv4 mounts.
144  *
145  * Consult /proc/mounts to determine if the mount point
146  * is an NFSv4 mount.  The kernel is authoritative about
147  * what type of mount this is.
148  *
149  * Returns 1 if "mc" is an NFSv4 mount, zero if not, and
150  * -1 if some error occurred.
151  */
152 static int nfs_umount_is_vers4(const struct mntentchn *mc)
153 {
154         struct mntentchn *pmc;
155         struct mount_options *options;
156         int retval;
157
158         retval = -1;
159         pmc = getprocmntdirbackward(mc->m.mnt_dir, NULL);
160         if (!pmc)
161                 goto not_found;
162
163         do {
164                 int nlen = strlen(pmc->m.mnt_fsname);
165                 /*
166                  * It's possible the mount location string in /proc/mounts
167                  * ends with a '/'. In this case, if the entry came from
168                  * /etc/mtab, it won't have the trailing '/' so deal with
169                  * it.
170                  */
171                 while (pmc->m.mnt_fsname[nlen - 1] == '/')
172                         nlen--;
173                 if (strncmp(pmc->m.mnt_fsname, mc->m.mnt_fsname, nlen) != 0)
174                         continue;
175
176                 if (strcmp(pmc->m.mnt_type, "nfs4") == 0)
177                         goto out_nfs4;
178
179                 options = po_split(pmc->m.mnt_opts);
180                 if (options != NULL) {
181                         unsigned long version;
182                         int rc = nfs_nfs_version(options, &version);
183                         po_destroy(options);
184                         if (rc && version == 4)
185                                 goto out_nfs4;
186                 }
187
188                 if (strcmp(pmc->m.mnt_type, "nfs") == 0)
189                         goto out_nfs;
190         } while ((pmc = getprocmntdirbackward(mc->m.mnt_dir, pmc)) != NULL);
191
192         if (retval == -1) {
193 not_found:
194                 fprintf(stderr, "%s was not found in %s\n",
195                         mc->m.mnt_dir, MOUNTSFILE);
196                 goto out;
197         }
198
199 out_nfs4:
200         if (verbose)
201                 fprintf(stderr, "NFSv4 mount point detected\n");
202         retval = 1;
203         goto out;
204
205 out_nfs:
206         if (verbose)
207                 fprintf(stderr, "Legacy NFS mount point detected\n");
208         retval = 0;
209
210 out:
211         return retval;
212 }
213
214 static struct option umount_longopts[] =
215 {
216   { "force", 0, 0, 'f' },
217   { "help", 0, 0, 'h' },
218   { "no-mtab", 0, 0, 'n' },
219   { "verbose", 0, 0, 'v' },
220   { "read-only", 0, 0, 'r' },
221   { NULL, 0, 0, 0 }
222 };
223
224 int nfsumount(int argc, char *argv[])
225 {
226         int c, ret;
227         char *spec;
228         struct mntentchn *mc;
229
230         if (argc < 2) {
231                 umount_usage();
232                 return EX_USAGE;
233         }
234
235         spec = argv[1];
236
237         argv += 1;
238         argc -= 1;
239
240         argv[0] = argv[-1]; /* So that getopt error messages are correct */
241         while ((c = getopt_long (argc, argv, "fvnrlh",
242                                 umount_longopts, NULL)) != -1) {
243
244                 switch (c) {
245                 case 'f':
246                         ++force;
247                         break;
248                 case 'v':
249                         ++verbose;
250                         break;
251                 case 'n':
252                         ++nomtab;
253                         break;
254                 case 'r':
255                         ++remount;
256                         break;
257                 case 'l':
258                         ++lazy;
259                         break;
260                 case 'h':
261                 default:
262                         umount_usage();
263                         return EX_USAGE;
264                 }
265         }
266         if (optind != argc) {
267                 umount_usage();
268                 return EX_USAGE;
269         }
270         
271         if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
272                 nfs_error(_("%s: %s: not found\n"), progname, spec);
273                 return EX_USAGE;
274         }
275
276         if (*spec == '/')
277                 mc = getmntdirbackward(spec, NULL);
278         else
279                 mc = getmntdevbackward(spec, NULL);
280         if (!mc && verbose)
281                 printf(_("Could not find %s in mtab\n"), spec);
282
283         if (mc && strcmp(mc->m.mnt_type, "nfs") != 0 &&
284             strcmp(mc->m.mnt_type, "nfs4") != 0) {
285                 nfs_error(_("%s: %s on %s is not an NFS filesystem"),
286                                 progname, mc->m.mnt_fsname, mc->m.mnt_dir);
287                 return EX_USAGE;
288         }
289
290         if (getuid() != 0) {
291                 /* only permitted if "user=" or "users" is in mount options */
292                 if (!mc) {
293                         /* umount might call us twice.  The second time there will
294                          * be no entry in mtab and we should just exit quietly
295                          */
296                         return EX_SUCCESS;
297
298                 only_root:
299                         nfs_error(_("%s: You are not permitted to unmount %s"),
300                                         progname, spec);
301                         return EX_USAGE;
302                 }
303                 if (hasmntopt(&mc->m, "users") == NULL) {
304                         char *opt = hasmntopt(&mc->m, "user");
305                         struct passwd *pw;
306                         char *comma;
307                         size_t len;
308                         if (!opt)
309                                 goto only_root;
310                         if (opt[4] != '=')
311                                 goto only_root;
312                         comma = strchr(opt, ',');
313                         if (comma)
314                                 len = comma - (opt + 5);
315                         else
316                                 len = strlen(opt+5);
317                         pw = getpwuid(getuid());
318                         if (pw == NULL || strlen(pw->pw_name) != len
319                             || strncmp(pw->pw_name, opt+5, len) != 0)
320                                 goto only_root;
321                 }
322         }
323
324         ret = EX_SUCCESS;
325         if (mc) {
326                 if (!lazy) {
327                         switch (nfs_umount_is_vers4(mc)) {
328                         case 0:
329                                 /* We ignore the error from nfs_umount23.
330                                  * If the actual umount succeeds (in del_mtab),
331                                  * we don't want to signal an error, as that
332                                  * could cause /sbin/mount to retry!
333                                  */
334                                 nfs_umount23(mc->m.mnt_fsname, mc->m.mnt_opts);
335                                 break;
336                         case 1:
337                                 break;
338                         default:
339                                 return EX_FAIL;
340                         }
341                 }
342                 ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
343         } else if (*spec != '/') {
344                 if (!lazy)
345                         ret = nfs_umount23(spec, "tcp,v3");
346         } else
347                 ret = del_mtab(NULL, spec);
348
349         return ret;
350 }