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>
33 #include "mount_constants.h"
35 #include "nfsumount.h"
37 #if !defined(MNT_FORCE)
38 /* dare not try to include <linux/mount.h> -- lots of errors */
42 #if !defined(MNT_DETACH)
46 extern char *progname;
47 extern int nfs_mount_version;
54 extern int find_kernel_nfs_mount_version(void);
55 extern int probe_mntport(clnt_addr_t *);
56 extern int nfs_gethostbyname(const char *, struct sockaddr_in *);
58 static inline enum clnt_stat
59 nfs3_umount(dirpath *argp, CLIENT *clnt)
62 memset (&clnt_res, 0, sizeof(clnt_res));
63 return clnt_call(clnt, MOUNTPROC_UMNT,
64 (xdrproc_t) xdr_dirpath, (caddr_t)argp,
65 (xdrproc_t) xdr_void, (caddr_t) &clnt_res,
69 static inline enum clnt_stat
70 nfs2_umount(dirpath *argp, CLIENT *clnt)
73 memset (&clnt_res, 0, sizeof(clnt_res));
74 return clnt_call(clnt, MOUNTPROC_UMNT,
75 (xdrproc_t) xdr_dirpath, (caddr_t)argp,
76 (xdrproc_t) xdr_void, (caddr_t) &clnt_res,
80 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
83 enum clnt_stat res = 0;
86 clnt = mnt_openclnt(mnt_server, &msock);
89 switch (mnt_server->pmap.pm_vers) {
91 res = nfs3_umount(argp, clnt);
95 res = nfs2_umount(argp, clnt);
100 mnt_closeclnt(clnt, msock);
101 if (res == RPC_SUCCESS)
107 u_int get_mntproto(const char *);
109 get_mntproto(const char *dirname)
112 struct mntent mntbuf;
114 u_int proto = IPPROTO_TCP; /* assume tcp */
116 mtab = setmntent ("/proc/mounts", "r");
118 mtab = setmntent (_PATH_MOUNTED, "r");
122 while(getmntent_r(mtab, &mntbuf, tmpbuf, sizeof (tmpbuf))) {
123 if (strcmp(mntbuf.mnt_type, "nfs"))
125 if (strcmp(dirname, mntbuf.mnt_fsname))
127 if (hasmntopt(&mntbuf, "udp"))
136 /* complain about a failed umount */
137 static void complain(int err, const char *dev) {
140 nfs_error (_("umount: %s: invalid block device"), dev); break;
142 nfs_error (_("umount: %s: not mounted"), dev); break;
144 nfs_error (_("umount: %s: can't write superblock"), dev); break;
146 /* Let us hope fstab has a line "proc /proc ..."
147 and not "none /proc ..."*/
148 nfs_error (_("umount: %s: device is busy"), dev); break;
150 nfs_error (_("umount: %s: not found"), dev); break;
152 nfs_error (_("umount: %s: must be superuser to umount"), dev); break;
154 nfs_error (_("umount: %s: block devices not permitted on fs"), dev); break;
156 nfs_error (_("umount: %s: %s"), dev, strerror (err)); break;
161 * Look for an option in a comma-separated list
164 contains(const char *list, const char *s) {
168 if (strncmp(list, s, n) == 0 &&
169 (list[n] == 0 || list[n] == ','))
171 while (*list && *list++ != ',') ;
177 * If list contains "user=peter" and we ask for "user=", return "peter"
180 get_value(const char *list, const char *s) {
185 if (strncmp(list, s, n) == 0) {
187 while (*s && *s != ',')
189 return xstrndup(t, s-t);
191 while (*list && *list++ != ',') ;
196 int add_mtab2(const char *spec, const char *node, const char *type,
197 const char *opts, struct mntentchn *mc)
199 int umnt_err, umnt_err2, res;
201 umnt_err = umnt_err2 = 0;
203 res = umount2 (node, MNT_DETACH);
210 res = umount2 (node, MNT_FORCE);
215 if (errno == ENOSYS) {
217 printf(_("no umount2, trying umount...\n"));
226 /* A device might have been mounted on a node that has since
227 been deleted or renamed, so if node fails, also try spec. */
228 /* Note that this is incorrect in case spec was mounted
230 /* if (umnt_err == ENOENT || umnt_err == EINVAL) */
231 if (umnt_err != EBUSY && strcmp(node, spec)) {
233 printf (_("could not umount %s - trying %s instead\n"),
238 /* Do not complain about remote NFS mount points */
239 if (errno == ENOENT && index(spec, ':'))
244 if (res < 0 && remount && (umnt_err == EBUSY || umnt_err2 == EBUSY)) {
245 /* Umount failed - let us try a remount */
246 res = mount(spec, node, NULL,
247 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
251 _("umount: %s busy - remounted read-only\n"),
253 remnt.mnt_type = remnt.mnt_fsname = NULL;
254 remnt.mnt_dir = xstrdup(node);
255 remnt.mnt_opts = xstrdup("ro");
257 update_mtab(node, &remnt);
259 } else if (errno != EBUSY) { /* hmm ... */
262 _("umount: could not remount %s read-only\n"),
268 /* Umount succeeded */
270 printf (_("%s umounted\n"), spec);
275 (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
276 update_mtab (node, NULL);
283 complain(umnt_err2, spec);
284 if (umnt_err && umnt_err != umnt_err2)
285 complain(umnt_err, node);
290 * Returns 1 if everything went well, else 0.
292 int _nfsumount(const char *spec, const char *opts)
296 clnt_addr_t mnt_server = { &hostname, };
297 struct pmap *pmap = &mnt_server.pmap;
300 nfs_mount_version = find_kernel_nfs_mount_version();
301 if (spec == NULL || (p = strchr(spec,':')) == NULL)
303 hostname = xstrndup(spec, p-spec);
304 dirname = xstrdup(p+1);
305 #ifdef NFS_MOUNT_DEBUG
306 printf(_("host: %s, directory: %s\n"), hostname, dirname);
309 if (opts && (p = strstr(opts, "addr="))) {
315 while (*q && *q != ',') q++;
316 hostname = xstrndup(p,q-p);
319 if (opts && (p = strstr(opts, "mounthost="))) {
325 while (*q && *q != ',') q++;
326 hostname = xstrndup(p,q-p);
329 pmap->pm_prog = MOUNTPROG;
330 pmap->pm_vers = MOUNTVERS;
331 pmap->pm_prot = get_mntproto(spec);
332 if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
333 pmap->pm_prog = atoi(p+10);
334 if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
335 pmap->pm_port = atoi(p+10);
336 if (opts && (p = strstr(opts, "nfsvers=")) && isdigit(*(p+8)))
337 pmap->pm_vers = nfsvers_to_mnt(atoi(p+8));
338 if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
339 pmap->pm_vers = atoi(p+10);
341 if (!nfs_gethostbyname(hostname, &mnt_server.saddr))
343 if (!probe_mntport(&mnt_server))
345 return nfs_call_umount(&mnt_server, &dirname);
347 fprintf(stderr, "%s: %s: not found or not mounted\n", progname, spec);
351 static struct option umount_longopts[] =
353 { "force", 0, 0, 'f' },
354 { "help", 0, 0, 'h' },
355 { "no-mtab", 0, 0, 'n' },
356 { "verbose", 0, 0, 'v' },
357 { "read-only", 0, 0, 'r' },
363 printf("usage: %s dir [-fvnrlh]\n", progname);
364 printf("options:\n\t-f\t\tforce unmount\n");
365 printf("\t-v\t\tverbose\n");
366 printf("\t-n\t\tDo not update /etc/mtab\n");
367 printf("\t-r\t\tremount\n");
368 printf("\t-l\t\tlazy unmount\n");
369 printf("\t-h\t\tprint this help\n\n");
372 int nfsumount(int argc, char *argv[])
376 struct mntentchn *mc;
383 while ((c = getopt_long (argc, argv, "fvnrlh",
384 umount_longopts, NULL)) != -1) {
409 mc = getmntdirbackward(spec, NULL);
411 mc = getmntdevbackward(spec, NULL);
413 printf(_("Could not find %s in mtab\n"), spec);
416 if(contains(mc->m.mnt_opts, "user") && getuid() != 0) {
417 struct passwd *pw = getpwuid(getuid());
418 if(!pw || strcmp(pw->pw_name, get_value(mc->m.mnt_opts, "user="))) {
419 fprintf(stderr, "%s: permission denied to unmount %s\n",
424 if(!contains(mc->m.mnt_opts, "users") && getuid() != 0) {
425 fprintf(stderr, "%s: only root can unmount %s from %s\n",
426 progname, mc->m.mnt_fsname, mc->m.mnt_dir);
431 ret = _nfsumount(mc->m.mnt_fsname, mc->m.mnt_opts);
433 ret = add_mtab2(mc->m.mnt_fsname, mc->m.mnt_dir,
434 mc->m.mnt_type, mc->m.mnt_opts, mc);
437 ret = _nfsumount(spec, NULL);
439 ret = add_mtab2(spec, spec, spec, spec, NULL);