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>
34 #include "mount_constants.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;
55 static int del_mtab(const char *spec, const char *node)
61 res = umount2 (node, MNT_DETACH);
68 res = umount2 (node, MNT_FORCE);
73 if (errno == ENOSYS) {
75 printf(_("no umount2, trying umount...\n"));
82 if (res < 0 && remount && errno == EBUSY && spec) {
83 /* Umount failed - let us try a remount */
84 res = mount(spec, node, NULL,
85 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
88 nfs_error(_("%s: %s busy - remounted read-only"),
90 remnt.mnt_type = remnt.mnt_fsname = NULL;
91 remnt.mnt_dir = xstrdup(node);
92 remnt.mnt_opts = xstrdup("ro");
94 update_mtab(node, &remnt);
96 } else if (errno != EBUSY) { /* hmm ... */
98 nfs_error(_("%s: could not remount %s read-only"),
104 /* Umount succeeded */
106 printf (_("%s umounted\n"), spec ? spec : node);
111 (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
112 update_mtab(node, NULL);
119 umount_error(umnt_err, node);
123 static int do_nfs_umount(const char *spec, char *opts)
127 clnt_addr_t mnt_server = { &hostname, };
128 struct mntent mnt = { .mnt_opts = opts };
129 struct pmap *pmap = &mnt_server.pmap;
133 nfs_error(_("%s: No NFS export name was provided"),
138 p = strchr(spec, ':');
140 nfs_error(_("%s: '%s' is not a legal NFS export name"),
144 hostname = xstrndup(spec, p - spec);
145 dirname = xstrdup(p + 1);
146 #ifdef NFS_MOUNT_DEBUG
147 printf(_("host: %s, directory: %s\n"), hostname, dirname);
150 if (opts && (p = strstr(opts, "addr="))) {
156 while (*q && *q != ',') q++;
157 hostname = xstrndup(p,q-p);
160 if (opts && (p = strstr(opts, "mounthost="))) {
166 while (*q && *q != ',') q++;
167 hostname = xstrndup(p,q-p);
170 pmap->pm_prog = MOUNTPROG;
171 pmap->pm_vers = MOUNTVERS_NFSV3;
172 if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
173 pmap->pm_prog = atoi(p+10);
174 if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
175 pmap->pm_port = atoi(p+10);
176 if (opts && hasmntopt(&mnt, "v2"))
177 pmap->pm_vers = nfsvers_to_mnt(2);
178 if (opts && hasmntopt(&mnt, "v3"))
179 pmap->pm_vers = nfsvers_to_mnt(3);
180 if (opts && hasmntopt(&mnt, "v4"))
181 pmap->pm_vers = nfsvers_to_mnt(4);
182 if (opts && (p = strstr(opts, "vers=")) && isdigit(*(p+5)))
183 pmap->pm_vers = nfsvers_to_mnt(atoi(p+5));
184 if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
185 pmap->pm_vers = atoi(p+10);
186 if (opts && (hasmntopt(&mnt, "udp") || hasmntopt(&mnt, "proto=udp")))
187 pmap->pm_prot = IPPROTO_UDP;
189 if (!nfs_gethostbyname(hostname, &mnt_server.saddr)) {
190 nfs_error(_("%s: '%s' does not contain a recognized hostname"),
195 if (!nfs_call_umount(&mnt_server, &dirname)) {
196 nfs_error(_("%s: Server failed to unmount '%s'"),
204 static struct option umount_longopts[] =
206 { "force", 0, 0, 'f' },
207 { "help", 0, 0, 'h' },
208 { "no-mtab", 0, 0, 'n' },
209 { "verbose", 0, 0, 'v' },
210 { "read-only", 0, 0, 'r' },
214 static void umount_usage(void)
216 printf("usage: %s dir [-fvnrlh]\n", progname);
217 printf("options:\n\t-f\t\tforce unmount\n");
218 printf("\t-v\t\tverbose\n");
219 printf("\t-n\t\tDo not update /etc/mtab\n");
220 printf("\t-r\t\tremount\n");
221 printf("\t-l\t\tlazy unmount\n");
222 printf("\t-h\t\tprint this help\n\n");
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 it 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 ret = do_nfs_umount(mc->m.mnt_fsname, mc->m.mnt_opts);
330 ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
331 } else if (*spec != '/') {
333 ret = do_nfs_umount(spec, "tcp,v3");
335 ret = del_mtab(NULL, spec);