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.
24 #include <sys/mount.h>
32 #include "mount_constants.h"
34 #include "nfsumount.h"
36 #if !defined(MNT_FORCE)
37 /* dare not try to include <linux/mount.h> -- lots of errors */
41 #if !defined(MNT_DETACH)
45 extern char *progname;
46 extern int nfs_mount_version;
53 extern int find_kernel_nfs_mount_version(void);
54 extern int probe_mntport(clnt_addr_t *);
55 extern int nfs_gethostbyname(const char *, struct sockaddr_in *);
57 static inline enum clnt_stat
58 nfs_umount(dirpath *argp, CLIENT *clnt)
60 return clnt_call(clnt, MOUNTPROC_UMNT,
61 (xdrproc_t) xdr_dirpath, (caddr_t)argp,
62 (xdrproc_t) xdr_void, NULL,
66 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
69 enum clnt_stat res = 0;
72 switch (mnt_server->pmap.pm_vers) {
76 if (!probe_mntport(mnt_server))
78 clnt = mnt_openclnt(mnt_server, &msock);
81 res = nfs_umount(argp, clnt);
82 mnt_closeclnt(clnt, msock);
83 if (res == RPC_SUCCESS)
94 /* complain about a failed umount */
95 static void complain(int err, const char *dev) {
98 nfs_error (_("umount: %s: invalid block device"), dev); break;
100 nfs_error (_("umount: %s: not mounted"), dev); break;
102 nfs_error (_("umount: %s: can't write superblock"), dev); break;
104 /* Let us hope fstab has a line "proc /proc ..."
105 and not "none /proc ..."*/
106 nfs_error (_("umount: %s: device is busy"), dev); break;
108 nfs_error (_("umount: %s: not found"), dev); break;
110 nfs_error (_("umount: %s: must be superuser to umount"), dev); break;
112 nfs_error (_("umount: %s: block devices not permitted on fs"), dev); break;
114 nfs_error (_("umount: %s: %s"), dev, strerror (err)); break;
118 int del_mtab(const char *spec, const char *node)
124 res = umount2 (node, MNT_DETACH);
131 res = umount2 (node, MNT_FORCE);
136 if (errno == ENOSYS) {
138 printf(_("no umount2, trying umount...\n"));
145 if (res < 0 && remount && errno == EBUSY && spec) {
146 /* Umount failed - let us try a remount */
147 res = mount(spec, node, NULL,
148 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
152 _("umount: %s busy - remounted read-only\n"),
154 remnt.mnt_type = remnt.mnt_fsname = NULL;
155 remnt.mnt_dir = xstrdup(node);
156 remnt.mnt_opts = xstrdup("ro");
158 update_mtab(node, &remnt);
160 } else if (errno != EBUSY) { /* hmm ... */
163 _("umount: could not remount %s read-only\n"),
169 /* Umount succeeded */
171 printf (_("%s umounted\n"), spec ? spec : node);
176 (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
177 update_mtab(node, NULL);
184 complain(umnt_err, node);
189 * Returns 1 if everything went well, else 0.
191 int _nfsumount(const char *spec, const char *opts)
195 clnt_addr_t mnt_server = { &hostname, };
196 struct mntent mnt = { .mnt_opts = opts };
197 struct pmap *pmap = &mnt_server.pmap;
200 nfs_mount_version = find_kernel_nfs_mount_version();
201 if (spec == NULL || (p = strchr(spec,':')) == NULL)
203 hostname = xstrndup(spec, p-spec);
204 dirname = xstrdup(p+1);
205 #ifdef NFS_MOUNT_DEBUG
206 printf(_("host: %s, directory: %s\n"), hostname, dirname);
209 if (opts && (p = strstr(opts, "addr="))) {
215 while (*q && *q != ',') q++;
216 hostname = xstrndup(p,q-p);
219 if (opts && (p = strstr(opts, "mounthost="))) {
225 while (*q && *q != ',') q++;
226 hostname = xstrndup(p,q-p);
229 pmap->pm_prog = MOUNTPROG;
230 pmap->pm_vers = MOUNTVERS_NFSV3;
231 pmap->pm_prot = IPPROTO_TCP;
232 if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
233 pmap->pm_prog = atoi(p+10);
234 if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
235 pmap->pm_port = atoi(p+10);
236 if (opts && hasmntopt(&mnt, "v2"))
237 pmap->pm_vers = nfsvers_to_mnt(2);
238 if (opts && hasmntopt(&mnt, "v3"))
239 pmap->pm_vers = nfsvers_to_mnt(3);
240 if (opts && hasmntopt(&mnt, "v4"))
241 pmap->pm_vers = nfsvers_to_mnt(4);
242 if (opts && (p = strstr(opts, "vers=")) && isdigit(*(p+5)))
243 pmap->pm_vers = nfsvers_to_mnt(atoi(p+5));
244 if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
245 pmap->pm_vers = atoi(p+10);
246 if (opts && (hasmntopt(&mnt, "udp") || hasmntopt(&mnt, "proto=udp")))
247 pmap->pm_prot = IPPROTO_UDP;
249 if (!nfs_gethostbyname(hostname, &mnt_server.saddr))
251 return nfs_call_umount(&mnt_server, &dirname);
253 printf("%s: %s: not found or not mounted\n", progname, spec);
257 static struct option umount_longopts[] =
259 { "force", 0, 0, 'f' },
260 { "help", 0, 0, 'h' },
261 { "no-mtab", 0, 0, 'n' },
262 { "verbose", 0, 0, 'v' },
263 { "read-only", 0, 0, 'r' },
269 printf("usage: %s dir [-fvnrlh]\n", progname);
270 printf("options:\n\t-f\t\tforce unmount\n");
271 printf("\t-v\t\tverbose\n");
272 printf("\t-n\t\tDo not update /etc/mtab\n");
273 printf("\t-r\t\tremount\n");
274 printf("\t-l\t\tlazy unmount\n");
275 printf("\t-h\t\tprint this help\n\n");
278 int nfsumount(int argc, char *argv[])
282 struct mntentchn *mc;
289 while ((c = getopt_long (argc, argv, "fvnrlh",
290 umount_longopts, NULL)) != -1) {
315 if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
316 printf(_("umount: %s: not found\n"), spec);
321 mc = getmntdirbackward(spec, NULL);
323 mc = getmntdevbackward(spec, NULL);
325 printf(_("Could not find %s in mtab\n"), spec);
328 ret = _nfsumount(mc->m.mnt_fsname, mc->m.mnt_opts);
330 ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
333 ret = _nfsumount(spec, "tcp,v3");
335 ret = del_mtab(NULL, spec);