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 char buffer[LINELEN], *next;
158 if ((f = fopen(MOUNTSFILE, "r")) == NULL) {
159 fprintf(stderr, "%s: %s\n",
160 MOUNTSFILE, strerror(errno));
165 while (fgets(buffer, sizeof(buffer), f) != NULL) {
166 char *device, *mntdir, *type, *flags;
167 struct mount_options *options;
170 next = strchr(line, '\n');
174 device = strtok(line, " \t");
177 mntdir = strtok(NULL, " \t");
180 if (strcmp(device, mc->m.mnt_fsname) != 0 &&
181 strcmp(mntdir, mc->m.mnt_dir) != 0)
184 type = strtok(NULL, " \t");
187 if (strcmp(type, "nfs4") == 0)
190 flags = strtok(NULL, " \t");
193 options = po_split(flags);
194 if (options != NULL) {
195 unsigned long version;
198 rc = nfs_nfs_version(options, &version);
200 if (rc && version == 4)
207 fprintf(stderr, "%s was not found in %s\n",
208 mc->m.mnt_dir, MOUNTSFILE);
216 fprintf(stderr, "NFSv4 mount point detected\n");
222 fprintf(stderr, "Legacy NFS mount point detected\n");
227 static struct option umount_longopts[] =
229 { "force", 0, 0, 'f' },
230 { "help", 0, 0, 'h' },
231 { "no-mtab", 0, 0, 'n' },
232 { "verbose", 0, 0, 'v' },
233 { "read-only", 0, 0, 'r' },
237 int nfsumount(int argc, char *argv[])
241 struct mntentchn *mc;
253 argv[0] = argv[-1]; /* So that getopt error messages are correct */
254 while ((c = getopt_long (argc, argv, "fvnrlh",
255 umount_longopts, NULL)) != -1) {
279 if (optind != argc) {
284 if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
285 nfs_error(_("%s: %s: not found\n"), progname, spec);
290 mc = getmntdirbackward(spec, NULL);
292 mc = getmntdevbackward(spec, NULL);
294 printf(_("Could not find %s in mtab\n"), spec);
296 if (mc && strcmp(mc->m.mnt_type, "nfs") != 0 &&
297 strcmp(mc->m.mnt_type, "nfs4") != 0) {
298 nfs_error(_("%s: %s on %s is not an NFS filesystem"),
299 progname, mc->m.mnt_fsname, mc->m.mnt_dir);
304 /* only permitted if "user=" or "users" is in mount options */
306 /* umount might call us twice. The second time there will
307 * be no entry in mtab and we should just exit quietly
312 nfs_error(_("%s: You are not permitted to unmount %s"),
316 if (hasmntopt(&mc->m, "users") == NULL) {
317 char *opt = hasmntopt(&mc->m, "user");
325 comma = strchr(opt, ',');
327 len = comma - (opt + 5);
330 pw = getpwuid(getuid());
331 if (pw == NULL || strlen(pw->pw_name) != len
332 || strncmp(pw->pw_name, opt+5, len) != 0)
340 switch (nfs_umount_is_vers4(mc)) {
342 /* We ignore the error from nfs_umount23.
343 * If the actual umount succeeds (in del_mtab),
344 * we don't want to signal an error, as that
345 * could cause /sbin/mount to retry!
347 nfs_umount23(mc->m.mnt_fsname, mc->m.mnt_opts);
355 ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
356 } else if (*spec != '/') {
358 ret = nfs_umount23(spec, "tcp,v3");
360 ret = del_mtab(NULL, spec);