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"
36 #include "nfsumount.h"
40 #if !defined(MNT_FORCE)
41 /* dare not try to include <linux/mount.h> -- lots of errors */
45 #if !defined(MNT_DETACH)
49 extern char *progname;
56 static inline enum clnt_stat
57 nfs_umount(dirpath *argp, CLIENT *clnt)
59 return clnt_call(clnt, MOUNTPROC_UMNT,
60 (xdrproc_t) xdr_dirpath, (caddr_t)argp,
61 (xdrproc_t) xdr_void, NULL,
65 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
68 enum clnt_stat res = 0;
71 switch (mnt_server->pmap.pm_vers) {
75 if (!probe_mntport(mnt_server))
77 clnt = mnt_openclnt(mnt_server, &msock);
80 res = nfs_umount(argp, clnt);
81 mnt_closeclnt(clnt, msock);
82 if (res == RPC_SUCCESS)
93 int del_mtab(const char *spec, const char *node)
99 res = umount2 (node, MNT_DETACH);
106 res = umount2 (node, MNT_FORCE);
111 if (errno == ENOSYS) {
113 printf(_("no umount2, trying umount...\n"));
120 if (res < 0 && remount && errno == EBUSY && spec) {
121 /* Umount failed - let us try a remount */
122 res = mount(spec, node, NULL,
123 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
127 _("umount: %s busy - remounted read-only\n"),
129 remnt.mnt_type = remnt.mnt_fsname = NULL;
130 remnt.mnt_dir = xstrdup(node);
131 remnt.mnt_opts = xstrdup("ro");
133 update_mtab(node, &remnt);
135 } else if (errno != EBUSY) { /* hmm ... */
138 _("umount: could not remount %s read-only\n"),
144 /* Umount succeeded */
146 printf (_("%s umounted\n"), spec ? spec : node);
151 (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
152 update_mtab(node, NULL);
159 umount_error(umnt_err, node);
164 * Returns 1 if everything went well, else 0.
166 int _nfsumount(const char *spec, char *opts)
170 clnt_addr_t mnt_server = { &hostname, };
171 struct mntent mnt = { .mnt_opts = opts };
172 struct pmap *pmap = &mnt_server.pmap;
175 if (spec == NULL || (p = strchr(spec,':')) == NULL)
177 hostname = xstrndup(spec, p-spec);
178 dirname = xstrdup(p+1);
179 #ifdef NFS_MOUNT_DEBUG
180 printf(_("host: %s, directory: %s\n"), hostname, dirname);
183 if (opts && (p = strstr(opts, "addr="))) {
189 while (*q && *q != ',') q++;
190 hostname = xstrndup(p,q-p);
193 if (opts && (p = strstr(opts, "mounthost="))) {
199 while (*q && *q != ',') q++;
200 hostname = xstrndup(p,q-p);
203 pmap->pm_prog = MOUNTPROG;
204 pmap->pm_vers = MOUNTVERS_NFSV3;
205 pmap->pm_prot = IPPROTO_TCP;
206 if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
207 pmap->pm_prog = atoi(p+10);
208 if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
209 pmap->pm_port = atoi(p+10);
210 if (opts && hasmntopt(&mnt, "v2"))
211 pmap->pm_vers = nfsvers_to_mnt(2);
212 if (opts && hasmntopt(&mnt, "v3"))
213 pmap->pm_vers = nfsvers_to_mnt(3);
214 if (opts && hasmntopt(&mnt, "v4"))
215 pmap->pm_vers = nfsvers_to_mnt(4);
216 if (opts && (p = strstr(opts, "vers=")) && isdigit(*(p+5)))
217 pmap->pm_vers = nfsvers_to_mnt(atoi(p+5));
218 if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
219 pmap->pm_vers = atoi(p+10);
220 if (opts && (hasmntopt(&mnt, "udp") || hasmntopt(&mnt, "proto=udp")))
221 pmap->pm_prot = IPPROTO_UDP;
223 if (!nfs_gethostbyname(hostname, &mnt_server.saddr))
225 return nfs_call_umount(&mnt_server, &dirname);
227 fprintf(stderr, "%s: %s: not found or not mounted\n", progname, spec);
231 static struct option umount_longopts[] =
233 { "force", 0, 0, 'f' },
234 { "help", 0, 0, 'h' },
235 { "no-mtab", 0, 0, 'n' },
236 { "verbose", 0, 0, 'v' },
237 { "read-only", 0, 0, 'r' },
243 printf("usage: %s dir [-fvnrlh]\n", progname);
244 printf("options:\n\t-f\t\tforce unmount\n");
245 printf("\t-v\t\tverbose\n");
246 printf("\t-n\t\tDo not update /etc/mtab\n");
247 printf("\t-r\t\tremount\n");
248 printf("\t-l\t\tlazy unmount\n");
249 printf("\t-h\t\tprint this help\n\n");
252 int nfsumount(int argc, char *argv[])
256 struct mntentchn *mc;
263 argv[0] = argv[-1]; /* So that getopt error messages are correct */
264 while ((c = getopt_long (argc, argv, "fvnrlh",
265 umount_longopts, NULL)) != -1) {
289 if (optind != argc) {
294 if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
295 printf(_("umount: %s: not found\n"), spec);
300 mc = getmntdirbackward(spec, NULL);
302 mc = getmntdevbackward(spec, NULL);
304 printf(_("Could not find %s in mtab\n"), spec);
306 if (mc && strcmp(mc->m.mnt_type, "nfs") != 0 &&
307 strcmp(mc->m.mnt_type, "nfs4") != 0) {
308 fprintf(stderr, "umount.nfs: %s on %s it not an nfs filesystem\n",
309 mc->m.mnt_fsname, mc->m.mnt_dir);
314 /* only permitted if "user=" or "users" is in mount options */
316 /* umount might call us twice. The second time there will
317 * be no entry in mtab and we should just exit quietly
322 fprintf(stderr,"%s: You are not permitted to unmount %s\n",
326 if (hasmntopt(&mc->m, "users") == NULL) {
327 char *opt = hasmntopt(&mc->m, "user");
335 comma = strchr(opt, ',');
337 len = comma - (opt + 5);
340 pw = getpwuid(getuid());
341 if (pw == NULL || strlen(pw->pw_name) != len
342 || strncmp(pw->pw_name, opt+5, len) != 0)
350 _nfsumount(mc->m.mnt_fsname, mc->m.mnt_opts);
351 ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
352 } else if (*spec != '/') {
354 _nfsumount(spec, "tcp,v3");
356 ret = del_mtab(NULL, spec);