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 nfs3_umount(dirpath *argp, CLIENT *clnt)
61 memset (&clnt_res, 0, sizeof(clnt_res));
62 return clnt_call(clnt, MOUNTPROC_UMNT,
63 (xdrproc_t) xdr_dirpath, (caddr_t)argp,
64 (xdrproc_t) xdr_void, (caddr_t) &clnt_res,
68 static inline enum clnt_stat
69 nfs2_umount(dirpath *argp, CLIENT *clnt)
72 memset (&clnt_res, 0, sizeof(clnt_res));
73 return clnt_call(clnt, MOUNTPROC_UMNT,
74 (xdrproc_t) xdr_dirpath, (caddr_t)argp,
75 (xdrproc_t) xdr_void, (caddr_t) &clnt_res,
79 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
82 enum clnt_stat res = 0;
85 clnt = mnt_openclnt(mnt_server, &msock);
88 switch (mnt_server->pmap.pm_vers) {
90 res = nfs3_umount(argp, clnt);
94 res = nfs2_umount(argp, clnt);
99 mnt_closeclnt(clnt, msock);
100 if (res == RPC_SUCCESS)
106 u_int get_mntproto(const char *);
108 get_mntproto(const char *dirname)
111 struct mntent mntbuf;
113 u_int proto = IPPROTO_TCP; /* assume tcp */
115 mtab = setmntent ("/proc/mounts", "r");
117 mtab = setmntent (_PATH_MOUNTED, "r");
121 while(getmntent_r(mtab, &mntbuf, tmpbuf, sizeof (tmpbuf))) {
122 if (strcmp(mntbuf.mnt_type, "nfs"))
124 if (strcmp(dirname, mntbuf.mnt_fsname))
126 if (hasmntopt(&mntbuf, "udp"))
135 /* complain about a failed umount */
136 static void complain(int err, const char *dev) {
139 nfs_error (_("umount: %s: invalid block device"), dev); break;
141 nfs_error (_("umount: %s: not mounted"), dev); break;
143 nfs_error (_("umount: %s: can't write superblock"), dev); break;
145 /* Let us hope fstab has a line "proc /proc ..."
146 and not "none /proc ..."*/
147 nfs_error (_("umount: %s: device is busy"), dev); break;
149 nfs_error (_("umount: %s: not found"), dev); break;
151 nfs_error (_("umount: %s: must be superuser to umount"), dev); break;
153 nfs_error (_("umount: %s: block devices not permitted on fs"), dev); break;
155 nfs_error (_("umount: %s: %s"), dev, strerror (err)); break;
159 int add_mtab2(const char *spec, const char *node, const char *type,
160 const char *opts, struct mntentchn *mc)
162 int umnt_err, umnt_err2, res;
164 umnt_err = umnt_err2 = 0;
166 res = umount2 (node, MNT_DETACH);
173 res = umount2 (node, MNT_FORCE);
178 if (errno == ENOSYS) {
180 printf(_("no umount2, trying umount...\n"));
189 /* A device might have been mounted on a node that has since
190 been deleted or renamed, so if node fails, also try spec. */
191 /* Note that this is incorrect in case spec was mounted
193 /* if (umnt_err == ENOENT || umnt_err == EINVAL) */
194 if (umnt_err != EBUSY && strcmp(node, spec)) {
196 printf (_("could not umount %s - trying %s instead\n"),
201 /* Do not complain about remote NFS mount points */
202 if (errno == ENOENT && index(spec, ':'))
207 if (res < 0 && remount && (umnt_err == EBUSY || umnt_err2 == EBUSY)) {
208 /* Umount failed - let us try a remount */
209 res = mount(spec, node, NULL,
210 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
214 _("umount: %s busy - remounted read-only\n"),
216 remnt.mnt_type = remnt.mnt_fsname = NULL;
217 remnt.mnt_dir = xstrdup(node);
218 remnt.mnt_opts = xstrdup("ro");
220 update_mtab(node, &remnt);
222 } else if (errno != EBUSY) { /* hmm ... */
225 _("umount: could not remount %s read-only\n"),
231 /* Umount succeeded */
233 printf (_("%s umounted\n"), spec);
238 (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
239 update_mtab (node, NULL);
246 complain(umnt_err2, spec);
247 if (umnt_err && umnt_err != umnt_err2)
248 complain(umnt_err, node);
253 * Returns 1 if everything went well, else 0.
255 int _nfsumount(const char *spec, const char *opts)
259 clnt_addr_t mnt_server = { &hostname, };
260 struct pmap *pmap = &mnt_server.pmap;
263 nfs_mount_version = find_kernel_nfs_mount_version();
264 if (spec == NULL || (p = strchr(spec,':')) == NULL)
266 hostname = xstrndup(spec, p-spec);
267 dirname = xstrdup(p+1);
268 #ifdef NFS_MOUNT_DEBUG
269 printf(_("host: %s, directory: %s\n"), hostname, dirname);
272 if (opts && (p = strstr(opts, "addr="))) {
278 while (*q && *q != ',') q++;
279 hostname = xstrndup(p,q-p);
282 if (opts && (p = strstr(opts, "mounthost="))) {
288 while (*q && *q != ',') q++;
289 hostname = xstrndup(p,q-p);
292 pmap->pm_prog = MOUNTPROG;
293 pmap->pm_vers = MOUNTVERS;
294 pmap->pm_prot = get_mntproto(spec);
295 if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
296 pmap->pm_prog = atoi(p+10);
297 if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
298 pmap->pm_port = atoi(p+10);
299 if (opts && (p = strstr(opts, "nfsvers=")) && isdigit(*(p+8)))
300 pmap->pm_vers = nfsvers_to_mnt(atoi(p+8));
301 if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
302 pmap->pm_vers = atoi(p+10);
304 if (!nfs_gethostbyname(hostname, &mnt_server.saddr))
306 if (!probe_mntport(&mnt_server))
308 return nfs_call_umount(&mnt_server, &dirname);
310 printf("%s: %s: not found or not mounted\n", progname, spec);
314 static struct option umount_longopts[] =
316 { "force", 0, 0, 'f' },
317 { "help", 0, 0, 'h' },
318 { "no-mtab", 0, 0, 'n' },
319 { "verbose", 0, 0, 'v' },
320 { "read-only", 0, 0, 'r' },
326 printf("usage: %s dir [-fvnrlh]\n", progname);
327 printf("options:\n\t-f\t\tforce unmount\n");
328 printf("\t-v\t\tverbose\n");
329 printf("\t-n\t\tDo not update /etc/mtab\n");
330 printf("\t-r\t\tremount\n");
331 printf("\t-l\t\tlazy unmount\n");
332 printf("\t-h\t\tprint this help\n\n");
335 int nfsumount(int argc, char *argv[])
339 struct mntentchn *mc;
346 while ((c = getopt_long (argc, argv, "fvnrlh",
347 umount_longopts, NULL)) != -1) {
372 mc = getmntdirbackward(spec, NULL);
374 mc = getmntdevbackward(spec, NULL);
376 printf(_("Could not find %s in mtab\n"), spec);
379 ret = _nfsumount(mc->m.mnt_fsname, mc->m.mnt_opts);
381 ret = add_mtab2(mc->m.mnt_fsname, mc->m.mnt_dir,
382 mc->m.mnt_type, mc->m.mnt_opts, mc);
385 ret = _nfsumount(spec, NULL);
387 ret = add_mtab2(spec, spec, spec, spec, NULL);