2 * nfsumount.c -- Linux NFS umount
3 * Copyright (C) 2006 Amit Gud <agud@redhat.com>
5 * - Basic code and wrapper around NFS umount code originally
6 * in util-linux/mount/nfsmount.c
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)
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.
25 #include <sys/mount.h>
33 #include "mount_constants.h"
34 #include "nfs_mount.h"
38 #include "parse_opt.h"
39 #include "parse_dev.h"
42 #define MOUNTSFILE "/proc/mounts"
43 #define LINELEN (4096)
45 #if !defined(MNT_FORCE)
46 /* dare not try to include <linux/mount.h> -- lots of errors */
50 #if !defined(MNT_DETACH)
54 extern char *progname;
62 static int try_remount(const char *spec, const char *node)
66 res = mount(spec, node, NULL,
67 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
70 nfs_error(_("%s: %s busy - remounted read-only"),
72 remnt.mnt_type = remnt.mnt_fsname = NULL;
73 remnt.mnt_dir = xstrdup(node);
74 remnt.mnt_opts = xstrdup("ro");
76 update_mtab(node, &remnt);
77 } else if (errno != EBUSY) { /* hmm ... */
79 nfs_error(_("%s: could not remount %s read-only"),
85 static int del_mtab(const char *spec, const char *node)
91 res = umount2 (node, MNT_DETACH);
98 res = umount2 (node, MNT_FORCE);
101 perror(_("umount2"));
103 if (errno == ENOSYS) {
105 printf(_("no umount2, trying umount...\n"));
113 if (remount && errno == EBUSY && spec) {
114 res = try_remount(spec, node);
123 /* Umount succeeded */
125 printf(_("%s umounted\n"), spec ? spec : node);
130 (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
131 update_mtab(node, NULL);
138 umount_error(umnt_err, node);
143 * Detect NFSv4 mounts.
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.
149 * Returns 1 if "mc" is an NFSv4 mount, zero if not, and
150 * -1 if some error occurred.
152 static int nfs_umount_is_vers4(const struct mntentchn *mc)
154 struct mntentchn *pmc;
155 struct mount_options *options;
159 pmc = getprocmntdirbackward(mc->m.mnt_dir, NULL);
164 size_t nlen = strlen(pmc->m.mnt_fsname);
167 * It's possible the mount location string in /proc/mounts
168 * ends with a '/'. In this case, if the entry came from
169 * /etc/mtab, it won't have the trailing '/' so deal with
172 while (pmc->m.mnt_fsname[nlen - 1] == '/')
174 if (strncmp(pmc->m.mnt_fsname, mc->m.mnt_fsname, nlen) != 0)
177 if (strcmp(pmc->m.mnt_type, "nfs4") == 0)
180 options = po_split(pmc->m.mnt_opts);
181 if (options != NULL) {
182 unsigned long version;
183 int rc = nfs_nfs_version(options, &version);
185 if (rc && version == 4)
189 if (strcmp(pmc->m.mnt_type, "nfs") == 0)
191 } while ((pmc = getprocmntdirbackward(mc->m.mnt_dir, pmc)) != NULL);
195 fprintf(stderr, "%s was not found in %s\n",
196 mc->m.mnt_dir, MOUNTSFILE);
202 fprintf(stderr, "NFSv4 mount point detected\n");
208 fprintf(stderr, "Legacy NFS mount point detected\n");
215 static struct option umount_longopts[] =
217 { "force", 0, 0, 'f' },
218 { "help", 0, 0, 'h' },
219 { "no-mtab", 0, 0, 'n' },
220 { "verbose", 0, 0, 'v' },
221 { "read-only", 0, 0, 'r' },
225 int nfsumount(int argc, char *argv[])
229 struct mntentchn *mc;
241 argv[0] = argv[-1]; /* So that getopt error messages are correct */
242 while ((c = getopt_long (argc, argv, "fvnrlh",
243 umount_longopts, NULL)) != -1) {
267 if (optind != argc) {
272 if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
273 nfs_error(_("%s: %s: not found\n"), progname, spec);
278 mc = getmntdirbackward(spec, NULL);
280 mc = getmntdevbackward(spec, NULL);
282 printf(_("Could not find %s in mtab\n"), spec);
284 if (mc && strcmp(mc->m.mnt_type, "nfs") != 0 &&
285 strcmp(mc->m.mnt_type, "nfs4") != 0) {
286 nfs_error(_("%s: %s on %s is not an NFS filesystem"),
287 progname, mc->m.mnt_fsname, mc->m.mnt_dir);
292 /* only permitted if "user=" or "users" is in mount options */
294 /* umount might call us twice. The second time there will
295 * be no entry in mtab and we should just exit quietly
300 nfs_error(_("%s: You are not permitted to unmount %s"),
304 if (hasmntopt(&mc->m, "users") == NULL) {
305 char *opt = hasmntopt(&mc->m, "user");
313 comma = strchr(opt, ',');
315 len = comma - (opt + 5);
318 pw = getpwuid(getuid());
319 if (pw == NULL || strlen(pw->pw_name) != len
320 || strncmp(pw->pw_name, opt+5, len) != 0)
328 switch (nfs_umount_is_vers4(mc)) {
330 /* We ignore the error from nfs_umount23.
331 * If the actual umount succeeds (in del_mtab),
332 * we don't want to signal an error, as that
333 * could cause /sbin/mount to retry!
335 nfs_umount23(mc->m.mnt_fsname, mc->m.mnt_opts);
343 ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
344 } else if (*spec != '/') {
346 ret = nfs_umount23(spec, "tcp,v3");
348 ret = del_mtab(NULL, spec);