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"
38 #if !defined(MNT_FORCE)
39 /* dare not try to include <linux/mount.h> -- lots of errors */
43 #if !defined(MNT_DETACH)
47 extern char *progname;
48 extern int nfs_mount_version;
55 extern int find_kernel_nfs_mount_version(void);
56 extern int probe_mntport(clnt_addr_t *);
57 extern int nfs_gethostbyname(const char *, struct sockaddr_in *);
59 static inline enum clnt_stat
60 nfs_umount(dirpath *argp, CLIENT *clnt)
62 return clnt_call(clnt, MOUNTPROC_UMNT,
63 (xdrproc_t) xdr_dirpath, (caddr_t)argp,
64 (xdrproc_t) xdr_void, NULL,
68 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
71 enum clnt_stat res = 0;
74 switch (mnt_server->pmap.pm_vers) {
78 if (!probe_mntport(mnt_server))
80 clnt = mnt_openclnt(mnt_server, &msock);
83 res = nfs_umount(argp, clnt);
84 mnt_closeclnt(clnt, msock);
85 if (res == RPC_SUCCESS)
96 /* complain about a failed umount */
97 static void complain(int err, const char *dev) {
100 nfs_error (_("umount: %s: invalid block device"), dev); break;
102 nfs_error (_("umount: %s: not mounted"), dev); break;
104 nfs_error (_("umount: %s: can't write superblock"), dev); break;
106 /* Let us hope fstab has a line "proc /proc ..."
107 and not "none /proc ..."*/
108 nfs_error (_("umount: %s: device is busy"), dev); break;
110 nfs_error (_("umount: %s: not found"), dev); break;
112 nfs_error (_("umount: %s: must be superuser to umount"), dev); break;
114 nfs_error (_("umount: %s: block devices not permitted on fs"), dev); break;
116 nfs_error (_("umount: %s: %s"), dev, strerror (err)); break;
120 int del_mtab(const char *spec, const char *node)
126 res = umount2 (node, MNT_DETACH);
133 res = umount2 (node, MNT_FORCE);
138 if (errno == ENOSYS) {
140 printf(_("no umount2, trying umount...\n"));
147 if (res < 0 && remount && errno == EBUSY && spec) {
148 /* Umount failed - let us try a remount */
149 res = mount(spec, node, NULL,
150 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
154 _("umount: %s busy - remounted read-only\n"),
156 remnt.mnt_type = remnt.mnt_fsname = NULL;
157 remnt.mnt_dir = xstrdup(node);
158 remnt.mnt_opts = xstrdup("ro");
160 update_mtab(node, &remnt);
162 } else if (errno != EBUSY) { /* hmm ... */
165 _("umount: could not remount %s read-only\n"),
171 /* Umount succeeded */
173 printf (_("%s umounted\n"), spec ? spec : node);
178 (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
179 update_mtab(node, NULL);
186 complain(umnt_err, node);
191 * Returns 1 if everything went well, else 0.
193 int _nfsumount(const char *spec, char *opts)
197 clnt_addr_t mnt_server = { &hostname, };
198 struct mntent mnt = { .mnt_opts = opts };
199 struct pmap *pmap = &mnt_server.pmap;
202 nfs_mount_version = find_kernel_nfs_mount_version();
203 if (spec == NULL || (p = strchr(spec,':')) == NULL)
205 hostname = xstrndup(spec, p-spec);
206 dirname = xstrdup(p+1);
207 #ifdef NFS_MOUNT_DEBUG
208 printf(_("host: %s, directory: %s\n"), hostname, dirname);
211 if (opts && (p = strstr(opts, "addr="))) {
217 while (*q && *q != ',') q++;
218 hostname = xstrndup(p,q-p);
221 if (opts && (p = strstr(opts, "mounthost="))) {
227 while (*q && *q != ',') q++;
228 hostname = xstrndup(p,q-p);
231 pmap->pm_prog = MOUNTPROG;
232 pmap->pm_vers = MOUNTVERS_NFSV3;
233 pmap->pm_prot = IPPROTO_TCP;
234 if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
235 pmap->pm_prog = atoi(p+10);
236 if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
237 pmap->pm_port = atoi(p+10);
238 if (opts && hasmntopt(&mnt, "v2"))
239 pmap->pm_vers = nfsvers_to_mnt(2);
240 if (opts && hasmntopt(&mnt, "v3"))
241 pmap->pm_vers = nfsvers_to_mnt(3);
242 if (opts && hasmntopt(&mnt, "v4"))
243 pmap->pm_vers = nfsvers_to_mnt(4);
244 if (opts && (p = strstr(opts, "vers=")) && isdigit(*(p+5)))
245 pmap->pm_vers = nfsvers_to_mnt(atoi(p+5));
246 if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
247 pmap->pm_vers = atoi(p+10);
248 if (opts && (hasmntopt(&mnt, "udp") || hasmntopt(&mnt, "proto=udp")))
249 pmap->pm_prot = IPPROTO_UDP;
251 if (!nfs_gethostbyname(hostname, &mnt_server.saddr))
253 return nfs_call_umount(&mnt_server, &dirname);
255 fprintf(stderr, "%s: %s: not found or not mounted\n", progname, spec);
259 static struct option umount_longopts[] =
261 { "force", 0, 0, 'f' },
262 { "help", 0, 0, 'h' },
263 { "no-mtab", 0, 0, 'n' },
264 { "verbose", 0, 0, 'v' },
265 { "read-only", 0, 0, 'r' },
271 printf("usage: %s dir [-fvnrlh]\n", progname);
272 printf("options:\n\t-f\t\tforce unmount\n");
273 printf("\t-v\t\tverbose\n");
274 printf("\t-n\t\tDo not update /etc/mtab\n");
275 printf("\t-r\t\tremount\n");
276 printf("\t-l\t\tlazy unmount\n");
277 printf("\t-h\t\tprint this help\n\n");
280 int nfsumount(int argc, char *argv[])
284 struct mntentchn *mc;
291 while ((c = getopt_long (argc, argv, "fvnrlh",
292 umount_longopts, NULL)) != -1) {
317 if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
318 printf(_("umount: %s: not found\n"), spec);
323 mc = getmntdirbackward(spec, NULL);
325 mc = getmntdevbackward(spec, NULL);
327 printf(_("Could not find %s in mtab\n"), spec);
330 /* only permitted if "user=" or "users" is in mount options */
333 fprintf(stderr,"%s: You are not permitted to unmount %s\n",
337 if (hasmntopt(&mc->m, "users") == NULL) {
338 char *opt = hasmntopt(&mc->m, "user");
346 comma = strchr(opt, ',');
348 len = comma - (opt + 5);
351 pw = getpwuid(getuid());
352 if (pw == NULL || strlen(pw->pw_name) != len
353 || strncmp(pw->pw_name, opt+5, len) != 0)
361 ret = _nfsumount(mc->m.mnt_fsname, mc->m.mnt_opts);
363 ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
364 } else if (*spec != '/') {
366 ret = _nfsumount(spec, "tcp,v3");
368 ret = del_mtab(NULL, spec);