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"
41 #define MOUNTSFILE "/proc/mounts"
42 #define LINELEN (4096)
44 #if !defined(MNT_FORCE)
45 /* dare not try to include <linux/mount.h> -- lots of errors */
49 #if !defined(MNT_DETACH)
53 extern char *progname;
61 static int try_remount(const char *spec, const char *node)
65 res = mount(spec, node, NULL,
66 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
69 nfs_error(_("%s: %s busy - remounted read-only"),
71 remnt.mnt_type = remnt.mnt_fsname = NULL;
72 remnt.mnt_dir = xstrdup(node);
73 remnt.mnt_opts = xstrdup("ro");
75 update_mtab(node, &remnt);
76 } else if (errno != EBUSY) { /* hmm ... */
78 nfs_error(_("%s: could not remount %s read-only"),
84 static int del_mtab(const char *spec, const char *node)
90 res = umount2 (node, MNT_DETACH);
97 res = umount2 (node, MNT_FORCE);
100 perror(_("umount2"));
102 if (errno == ENOSYS) {
104 printf(_("no umount2, trying umount...\n"));
112 if (remount && errno == EBUSY && spec) {
113 res = try_remount(spec, node);
122 /* Umount succeeded */
124 printf(_("%s umounted\n"), spec ? spec : node);
129 (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
130 update_mtab(node, NULL);
137 umount_error(umnt_err, node);
142 * Discover mount server's hostname/address by examining mount options
144 * Returns a pointer to a string that the caller must free, on
145 * success; otherwise NULL is returned.
147 static char *nfs_umount_hostname(struct mount_options *options,
152 option = po_get(options, "mountaddr");
155 option = po_get(options, "mounthost");
158 option = po_get(options, "addr");
166 return strdup(option);
170 * Returns EX_SUCCESS if mount options and device name have been
171 * parsed successfully; otherwise EX_FAIL.
173 static int nfs_umount_do_umnt(struct mount_options *options,
174 char **hostname, char **dirname)
178 struct sockaddr_in s4;
179 struct sockaddr_in6 s6;
181 struct sockaddr *sap = &address.sa;
182 socklen_t salen = sizeof(address);
183 struct pmap nfs_pmap, mnt_pmap;
186 if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap))
189 /* Skip UMNT call for vers=4 mounts */
190 if (nfs_pmap.pm_vers == 4)
193 *hostname = nfs_umount_hostname(options, *hostname);
195 nfs_error(_("%s: out of memory"), progname);
199 if (!nfs_mount_proto_family(options, &family))
201 if (!nfs_lookup(*hostname, family, sap, &salen))
202 /* nfs_lookup reports any errors */
205 if (nfs_advise_umount(sap, salen, &mnt_pmap, dirname) == 0)
206 /* nfs_advise_umount reports any errors */
213 * Pick up certain mount options used during the original mount
214 * from /etc/mtab. The basics include the server's IP address and
215 * the server pathname of the share to unregister.
217 * These options might also describe the mount port, mount protocol
218 * version, and transport protocol used to punch through a firewall.
219 * We will need this information to get through the firewall again
222 * Note that option parsing failures won't necessarily cause the
223 * umount request to fail. Those values will be left zero in the
224 * pmap tuple. If the GETPORT call later fails to disambiguate them,
227 static int nfs_umount23(const char *devname, char *string)
229 char *hostname, *dirname;
230 struct mount_options *options;
231 int result = EX_FAIL;
233 if (!nfs_parse_devname(devname, &hostname, &dirname))
236 options = po_split(string);
238 result = nfs_umount_do_umnt(options, &hostname, &dirname);
241 nfs_error(_("%s: option parsing error"), progname);
249 * Detect NFSv4 mounts.
251 * Consult /proc/mounts to determine if the mount point
252 * is an NFSv4 mount. The kernel is authoritative about
253 * what type of mount this is.
255 * Returns 1 if "mc" is an NFSv4 mount, zero if not, and
256 * -1 if some error occurred.
258 static int nfs_umount_is_vers4(const struct mntentchn *mc)
260 char buffer[LINELEN], *next;
264 if ((f = fopen(MOUNTSFILE, "r")) == NULL) {
265 fprintf(stderr, "%s: %s\n",
266 MOUNTSFILE, strerror(errno));
271 while (fgets(buffer, sizeof(buffer), f) != NULL) {
272 char *device, *mntdir, *type, *flags;
273 struct mount_options *options;
276 next = strchr(line, '\n');
280 device = strtok(line, " \t");
283 mntdir = strtok(NULL, " \t");
286 if (strcmp(device, mc->m.mnt_fsname) != 0 &&
287 strcmp(mntdir, mc->m.mnt_dir) != 0)
290 type = strtok(NULL, " \t");
293 if (strcmp(type, "nfs4") == 0)
296 flags = strtok(NULL, " \t");
299 options = po_split(flags);
300 if (options != NULL) {
301 unsigned long version;
304 rc = nfs_nfs_version(options, &version);
306 if (rc && version == 4)
313 fprintf(stderr, "%s was not found in %s\n",
314 mc->m.mnt_dir, MOUNTSFILE);
322 fprintf(stderr, "NFSv4 mount point detected\n");
328 fprintf(stderr, "Legacy NFS mount point detected\n");
333 static struct option umount_longopts[] =
335 { "force", 0, 0, 'f' },
336 { "help", 0, 0, 'h' },
337 { "no-mtab", 0, 0, 'n' },
338 { "verbose", 0, 0, 'v' },
339 { "read-only", 0, 0, 'r' },
343 static void umount_usage(void)
345 printf(_("usage: %s dir [-fvnrlh]\n"), progname);
346 printf(_("options:\n\t-f\t\tforce unmount\n"));
347 printf(_("\t-v\tverbose\n"));
348 printf(_("\t-n\tDo not update /etc/mtab\n"));
349 printf(_("\t-r\tremount\n"));
350 printf(_("\t-l\tlazy unmount\n"));
351 printf(_("\t-h\tprint this help\n\n"));
354 int nfsumount(int argc, char *argv[])
358 struct mntentchn *mc;
370 argv[0] = argv[-1]; /* So that getopt error messages are correct */
371 while ((c = getopt_long (argc, argv, "fvnrlh",
372 umount_longopts, NULL)) != -1) {
396 if (optind != argc) {
401 if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
402 nfs_error(_("%s: %s: not found\n"), progname, spec);
407 mc = getmntdirbackward(spec, NULL);
409 mc = getmntdevbackward(spec, NULL);
411 printf(_("Could not find %s in mtab\n"), spec);
413 if (mc && strcmp(mc->m.mnt_type, "nfs") != 0 &&
414 strcmp(mc->m.mnt_type, "nfs4") != 0) {
415 nfs_error(_("%s: %s on %s is not an NFS filesystem"),
416 progname, mc->m.mnt_fsname, mc->m.mnt_dir);
421 /* only permitted if "user=" or "users" is in mount options */
423 /* umount might call us twice. The second time there will
424 * be no entry in mtab and we should just exit quietly
429 nfs_error(_("%s: You are not permitted to unmount %s"),
433 if (hasmntopt(&mc->m, "users") == NULL) {
434 char *opt = hasmntopt(&mc->m, "user");
442 comma = strchr(opt, ',');
444 len = comma - (opt + 5);
447 pw = getpwuid(getuid());
448 if (pw == NULL || strlen(pw->pw_name) != len
449 || strncmp(pw->pw_name, opt+5, len) != 0)
457 switch (nfs_umount_is_vers4(mc)) {
459 /* We ignore the error from nfs_umount23.
460 * If the actual umount succeeds (in del_mtab),
461 * we don't want to signal an error, as that
462 * could cause /sbin/mount to retry!
464 nfs_umount23(mc->m.mnt_fsname, mc->m.mnt_opts);
472 ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
473 } else if (*spec != '/') {
475 ret = nfs_umount23(spec, "tcp,v3");
477 ret = del_mtab(NULL, spec);