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)
95 getmntentchn(const char *dir)
99 struct mntentchn *mc = NULL;
101 mfp = nfs_setmntent ("/proc/mounts", "r");
102 if (mfp == NULL || mfp->mntent_fp == NULL)
105 while ((mnt = nfs_getmntent(mfp)) != NULL) {
106 if(strcmp(mnt->mnt_fsname, dir) && strcmp(mnt->mnt_dir, dir))
110 mc = (struct mntentchn *)xmalloc(sizeof(*mc));
118 /* complain about a failed umount */
119 static void complain(int err, const char *dev) {
122 nfs_error (_("umount: %s: invalid block device"), dev); break;
124 nfs_error (_("umount: %s: not mounted"), dev); break;
126 nfs_error (_("umount: %s: can't write superblock"), dev); break;
128 /* Let us hope fstab has a line "proc /proc ..."
129 and not "none /proc ..."*/
130 nfs_error (_("umount: %s: device is busy"), dev); break;
132 nfs_error (_("umount: %s: not found"), dev); break;
134 nfs_error (_("umount: %s: must be superuser to umount"), dev); break;
136 nfs_error (_("umount: %s: block devices not permitted on fs"), dev); break;
138 nfs_error (_("umount: %s: %s"), dev, strerror (err)); break;
142 int add_mtab2(const char *spec, const char *node, const char *type,
143 const char *opts, struct mntentchn *mc)
145 int umnt_err, umnt_err2, res;
147 umnt_err = umnt_err2 = 0;
149 res = umount2 (node, MNT_DETACH);
156 res = umount2 (node, MNT_FORCE);
161 if (errno == ENOSYS) {
163 printf(_("no umount2, trying umount...\n"));
172 /* A device might have been mounted on a node that has since
173 been deleted or renamed, so if node fails, also try spec. */
174 /* Note that this is incorrect in case spec was mounted
176 /* if (umnt_err == ENOENT || umnt_err == EINVAL) */
177 if (umnt_err != EBUSY && strcmp(node, spec)) {
179 printf (_("could not umount %s - trying %s instead\n"),
184 /* Do not complain about remote NFS mount points */
185 if (errno == ENOENT && index(spec, ':'))
190 if (res < 0 && remount && (umnt_err == EBUSY || umnt_err2 == EBUSY)) {
191 /* Umount failed - let us try a remount */
192 res = mount(spec, node, NULL,
193 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
197 _("umount: %s busy - remounted read-only\n"),
199 remnt.mnt_type = remnt.mnt_fsname = NULL;
200 remnt.mnt_dir = xstrdup(node);
201 remnt.mnt_opts = xstrdup("ro");
203 update_mtab(node, &remnt);
205 } else if (errno != EBUSY) { /* hmm ... */
208 _("umount: could not remount %s read-only\n"),
214 /* Umount succeeded */
216 printf (_("%s umounted\n"), spec);
221 (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
222 update_mtab (node, NULL);
229 complain(umnt_err2, spec);
230 if (umnt_err && umnt_err != umnt_err2)
231 complain(umnt_err, node);
236 * Returns 1 if everything went well, else 0.
238 int _nfsumount(const char *spec, const char *opts)
242 clnt_addr_t mnt_server = { &hostname, };
243 struct mntent mnt = { .mnt_opts = opts };
244 struct pmap *pmap = &mnt_server.pmap;
247 nfs_mount_version = find_kernel_nfs_mount_version();
248 if (spec == NULL || (p = strchr(spec,':')) == NULL)
250 hostname = xstrndup(spec, p-spec);
251 dirname = xstrdup(p+1);
252 #ifdef NFS_MOUNT_DEBUG
253 printf(_("host: %s, directory: %s\n"), hostname, dirname);
256 if (opts && (p = strstr(opts, "addr="))) {
262 while (*q && *q != ',') q++;
263 hostname = xstrndup(p,q-p);
266 if (opts && (p = strstr(opts, "mounthost="))) {
272 while (*q && *q != ',') q++;
273 hostname = xstrndup(p,q-p);
276 pmap->pm_prog = MOUNTPROG;
277 pmap->pm_vers = MOUNTVERS_NFSV3;
278 pmap->pm_prot = IPPROTO_TCP;
279 if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
280 pmap->pm_prog = atoi(p+10);
281 if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
282 pmap->pm_port = atoi(p+10);
283 if (opts && hasmntopt(&mnt, "v2"))
284 pmap->pm_vers = nfsvers_to_mnt(2);
285 if (opts && hasmntopt(&mnt, "v3"))
286 pmap->pm_vers = nfsvers_to_mnt(3);
287 if (opts && hasmntopt(&mnt, "v4"))
288 pmap->pm_vers = nfsvers_to_mnt(4);
289 if (opts && (p = strstr(opts, "vers=")) && isdigit(*(p+5)))
290 pmap->pm_vers = nfsvers_to_mnt(atoi(p+5));
291 if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
292 pmap->pm_vers = atoi(p+10);
293 if (opts && (hasmntopt(&mnt, "udp") || hasmntopt(&mnt, "proto=udp")))
294 pmap->pm_prot = IPPROTO_UDP;
296 if (!nfs_gethostbyname(hostname, &mnt_server.saddr))
298 return nfs_call_umount(&mnt_server, &dirname);
300 printf("%s: %s: not found or not mounted\n", progname, spec);
304 static struct option umount_longopts[] =
306 { "force", 0, 0, 'f' },
307 { "help", 0, 0, 'h' },
308 { "no-mtab", 0, 0, 'n' },
309 { "verbose", 0, 0, 'v' },
310 { "read-only", 0, 0, 'r' },
316 printf("usage: %s dir [-fvnrlh]\n", progname);
317 printf("options:\n\t-f\t\tforce unmount\n");
318 printf("\t-v\t\tverbose\n");
319 printf("\t-n\t\tDo not update /etc/mtab\n");
320 printf("\t-r\t\tremount\n");
321 printf("\t-l\t\tlazy unmount\n");
322 printf("\t-h\t\tprint this help\n\n");
325 int nfsumount(int argc, char *argv[])
329 struct mntentchn *mc;
336 while ((c = getopt_long (argc, argv, "fvnrlh",
337 umount_longopts, NULL)) != -1) {
362 if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
363 printf(_("umount: %s: not found\n"), spec);
367 mc = getmntentchn(spec);
369 mc = getmntdirbackward(spec, NULL);
371 mc = getmntdevbackward(spec, NULL);
373 printf(_("Could not find %s in mtab\n"), spec);
376 ret = _nfsumount(mc->m.mnt_fsname, mc->m.mnt_opts);
378 ret = add_mtab2(mc->m.mnt_fsname, mc->m.mnt_dir,
379 mc->m.mnt_type, mc->m.mnt_opts, mc);
382 ret = _nfsumount(spec, NULL);
384 ret = add_mtab2(spec, spec, spec, spec, NULL);