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"
37 #include "parse_dev.h"
39 #if !defined(MNT_FORCE)
40 /* dare not try to include <linux/mount.h> -- lots of errors */
44 #if !defined(MNT_DETACH)
48 extern char *progname;
56 static int try_remount(const char *spec, const char *node)
60 res = mount(spec, node, NULL,
61 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
64 nfs_error(_("%s: %s busy - remounted read-only"),
66 remnt.mnt_type = remnt.mnt_fsname = NULL;
67 remnt.mnt_dir = xstrdup(node);
68 remnt.mnt_opts = xstrdup("ro");
70 update_mtab(node, &remnt);
71 } else if (errno != EBUSY) { /* hmm ... */
73 nfs_error(_("%s: could not remount %s read-only"),
79 static int del_mtab(const char *spec, const char *node)
85 res = umount2 (node, MNT_DETACH);
92 res = umount2 (node, MNT_FORCE);
97 if (errno == ENOSYS) {
99 printf(_("no umount2, trying umount...\n"));
107 if (remount && errno == EBUSY && spec) {
108 res = try_remount(spec, node);
117 /* Umount succeeded */
119 printf(_("%s umounted\n"), spec ? spec : node);
124 (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
125 update_mtab(node, NULL);
132 umount_error(umnt_err, node);
137 * Pick up certain mount options used during the original mount
138 * from /etc/mtab. The basics include the server's IP address and
139 * the server pathname of the share to unregister.
141 * These options might also describe the mount port, mount protocol
142 * version, and transport protocol used to punch through a firewall.
143 * We will need this information to get through the firewall again
146 static int do_nfs_umount23(const char *spec, char *opts)
150 clnt_addr_t mnt_server = { &hostname, };
151 struct mntent mnt = { .mnt_opts = opts };
152 struct pmap *pmap = &mnt_server.pmap;
154 int result = EX_USAGE;
156 if (!nfs_parse_devname(spec, &hostname, &dirname))
159 #ifdef NFS_MOUNT_DEBUG
160 printf(_("host: %s, directory: %s\n"), hostname, dirname);
163 if (opts && (p = strstr(opts, "addr="))) {
169 while (*q && *q != ',') q++;
170 hostname = xstrndup(p,q-p);
173 if (opts && (p = strstr(opts, "mounthost="))) {
179 while (*q && *q != ',') q++;
180 hostname = xstrndup(p,q-p);
183 pmap->pm_prog = MOUNTPROG;
184 pmap->pm_vers = 0; /* unknown */
185 if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
186 pmap->pm_prog = atoi(p+10);
187 if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
188 pmap->pm_port = atoi(p+10);
189 if (opts && hasmntopt(&mnt, "v2"))
190 pmap->pm_vers = nfsvers_to_mnt(2);
191 if (opts && hasmntopt(&mnt, "v3"))
192 pmap->pm_vers = nfsvers_to_mnt(3);
193 if (opts && (p = strstr(opts, "vers=")) && isdigit(*(p+5)))
194 pmap->pm_vers = nfsvers_to_mnt(atoi(p+5));
195 if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
196 pmap->pm_vers = atoi(p+10);
197 if (opts && (hasmntopt(&mnt, "udp")
198 || hasmntopt(&mnt, "proto=udp")
199 || hasmntopt(&mnt, "mountproto=udp")
201 pmap->pm_prot = IPPROTO_UDP;
202 if (opts && (hasmntopt(&mnt, "tcp")
203 || hasmntopt(&mnt, "proto=tcp")
204 || hasmntopt(&mnt, "mountproto=tcp")
206 pmap->pm_prot = IPPROTO_TCP;
208 if (!nfs_gethostbyname(hostname, &mnt_server.saddr)) {
209 nfs_error(_("%s: DNS resolution of '%s' failed"),
214 if (!nfs_call_umount(&mnt_server, &dirname)) {
215 nfs_error(_("%s: Server failed to unmount '%s'"),
229 static struct option umount_longopts[] =
231 { "force", 0, 0, 'f' },
232 { "help", 0, 0, 'h' },
233 { "no-mtab", 0, 0, 'n' },
234 { "verbose", 0, 0, 'v' },
235 { "read-only", 0, 0, 'r' },
239 static void umount_usage(void)
241 printf(_("usage: %s dir [-fvnrlh]\n"), progname);
242 printf(_("options:\n\t-f\t\tforce unmount\n"));
243 printf(_("\t-v\tverbose\n"));
244 printf(_("\t-n\tDo not update /etc/mtab\n"));
245 printf(_("\t-r\tremount\n"));
246 printf(_("\t-l\tlazy unmount\n"));
247 printf(_("\t-h\tprint this help\n\n"));
250 int nfsumount(int argc, char *argv[])
254 struct mntentchn *mc;
266 argv[0] = argv[-1]; /* So that getopt error messages are correct */
267 while ((c = getopt_long (argc, argv, "fvnrlh",
268 umount_longopts, NULL)) != -1) {
292 if (optind != argc) {
297 if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
298 nfs_error(_("%s: %s: not found\n"), progname, spec);
303 mc = getmntdirbackward(spec, NULL);
305 mc = getmntdevbackward(spec, NULL);
307 printf(_("Could not find %s in mtab\n"), spec);
309 if (mc && strcmp(mc->m.mnt_type, "nfs") != 0 &&
310 strcmp(mc->m.mnt_type, "nfs4") != 0) {
311 nfs_error(_("%s: %s on %s is not an NFS filesystem"),
312 progname, mc->m.mnt_fsname, mc->m.mnt_dir);
317 /* only permitted if "user=" or "users" is in mount options */
319 /* umount might call us twice. The second time there will
320 * be no entry in mtab and we should just exit quietly
325 nfs_error(_("%s: You are not permitted to unmount %s"),
329 if (hasmntopt(&mc->m, "users") == NULL) {
330 char *opt = hasmntopt(&mc->m, "user");
338 comma = strchr(opt, ',');
340 len = comma - (opt + 5);
343 pw = getpwuid(getuid());
344 if (pw == NULL || strlen(pw->pw_name) != len
345 || strncmp(pw->pw_name, opt+5, len) != 0)
352 if (!lazy && strcmp(mc->m.mnt_type, "nfs4") != 0)
353 /* We ignore the error from do_nfs_umount23.
354 * If the actual umount succeeds (in del_mtab),
355 * we don't want to signal an error, as that
356 * could cause /sbin/mount to retry!
358 do_nfs_umount23(mc->m.mnt_fsname, mc->m.mnt_opts);
359 ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
360 } else if (*spec != '/') {
362 ret = do_nfs_umount23(spec, "tcp,v3");
364 ret = del_mtab(NULL, spec);