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 u_int get_mntproto(const char *);
96 get_mntproto(const char *dirname)
101 u_int proto = IPPROTO_TCP; /* assume tcp */
103 mtab = setmntent ("/proc/mounts", "r");
105 mtab = setmntent (_PATH_MOUNTED, "r");
109 while(getmntent_r(mtab, &mntbuf, tmpbuf, sizeof (tmpbuf))) {
110 if (strcmp(mntbuf.mnt_type, "nfs"))
112 if (strcmp(dirname, mntbuf.mnt_fsname))
114 if (hasmntopt(&mntbuf, "udp"))
123 /* complain about a failed umount */
124 static void complain(int err, const char *dev) {
127 nfs_error (_("umount: %s: invalid block device"), dev); break;
129 nfs_error (_("umount: %s: not mounted"), dev); break;
131 nfs_error (_("umount: %s: can't write superblock"), dev); break;
133 /* Let us hope fstab has a line "proc /proc ..."
134 and not "none /proc ..."*/
135 nfs_error (_("umount: %s: device is busy"), dev); break;
137 nfs_error (_("umount: %s: not found"), dev); break;
139 nfs_error (_("umount: %s: must be superuser to umount"), dev); break;
141 nfs_error (_("umount: %s: block devices not permitted on fs"), dev); break;
143 nfs_error (_("umount: %s: %s"), dev, strerror (err)); break;
147 int add_mtab2(const char *spec, const char *node, const char *type,
148 const char *opts, struct mntentchn *mc)
150 int umnt_err, umnt_err2, res;
152 umnt_err = umnt_err2 = 0;
154 res = umount2 (node, MNT_DETACH);
161 res = umount2 (node, MNT_FORCE);
166 if (errno == ENOSYS) {
168 printf(_("no umount2, trying umount...\n"));
177 /* A device might have been mounted on a node that has since
178 been deleted or renamed, so if node fails, also try spec. */
179 /* Note that this is incorrect in case spec was mounted
181 /* if (umnt_err == ENOENT || umnt_err == EINVAL) */
182 if (umnt_err != EBUSY && strcmp(node, spec)) {
184 printf (_("could not umount %s - trying %s instead\n"),
189 /* Do not complain about remote NFS mount points */
190 if (errno == ENOENT && index(spec, ':'))
195 if (res < 0 && remount && (umnt_err == EBUSY || umnt_err2 == EBUSY)) {
196 /* Umount failed - let us try a remount */
197 res = mount(spec, node, NULL,
198 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
202 _("umount: %s busy - remounted read-only\n"),
204 remnt.mnt_type = remnt.mnt_fsname = NULL;
205 remnt.mnt_dir = xstrdup(node);
206 remnt.mnt_opts = xstrdup("ro");
208 update_mtab(node, &remnt);
210 } else if (errno != EBUSY) { /* hmm ... */
213 _("umount: could not remount %s read-only\n"),
219 /* Umount succeeded */
221 printf (_("%s umounted\n"), spec);
226 (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
227 update_mtab (node, NULL);
234 complain(umnt_err2, spec);
235 if (umnt_err && umnt_err != umnt_err2)
236 complain(umnt_err, node);
241 * Returns 1 if everything went well, else 0.
243 int _nfsumount(const char *spec, const char *opts)
247 clnt_addr_t mnt_server = { &hostname, };
248 struct mntent mnt = { .mnt_opts = opts };
249 struct pmap *pmap = &mnt_server.pmap;
252 nfs_mount_version = find_kernel_nfs_mount_version();
253 if (spec == NULL || (p = strchr(spec,':')) == NULL)
255 hostname = xstrndup(spec, p-spec);
256 dirname = xstrdup(p+1);
257 #ifdef NFS_MOUNT_DEBUG
258 printf(_("host: %s, directory: %s\n"), hostname, dirname);
261 if (opts && (p = strstr(opts, "addr="))) {
267 while (*q && *q != ',') q++;
268 hostname = xstrndup(p,q-p);
271 if (opts && (p = strstr(opts, "mounthost="))) {
277 while (*q && *q != ',') q++;
278 hostname = xstrndup(p,q-p);
281 pmap->pm_prog = MOUNTPROG;
282 pmap->pm_vers = MOUNTVERS_NFSV3;
283 pmap->pm_prot = get_mntproto(spec);
284 if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
285 pmap->pm_prog = atoi(p+10);
286 if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
287 pmap->pm_port = atoi(p+10);
288 if (opts && hasmntopt(&mnt, "v2"))
289 pmap->pm_vers = nfsvers_to_mnt(2);
290 if (opts && hasmntopt(&mnt, "v3"))
291 pmap->pm_vers = nfsvers_to_mnt(3);
292 if (opts && hasmntopt(&mnt, "v4"))
293 pmap->pm_vers = nfsvers_to_mnt(4);
294 if (opts && (p = strstr(opts, "vers=")) && isdigit(*(p+5)))
295 pmap->pm_vers = nfsvers_to_mnt(atoi(p+5));
296 if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
297 pmap->pm_vers = atoi(p+10);
299 if (!nfs_gethostbyname(hostname, &mnt_server.saddr))
301 return nfs_call_umount(&mnt_server, &dirname);
303 printf("%s: %s: not found or not mounted\n", progname, spec);
307 static struct option umount_longopts[] =
309 { "force", 0, 0, 'f' },
310 { "help", 0, 0, 'h' },
311 { "no-mtab", 0, 0, 'n' },
312 { "verbose", 0, 0, 'v' },
313 { "read-only", 0, 0, 'r' },
319 printf("usage: %s dir [-fvnrlh]\n", progname);
320 printf("options:\n\t-f\t\tforce unmount\n");
321 printf("\t-v\t\tverbose\n");
322 printf("\t-n\t\tDo not update /etc/mtab\n");
323 printf("\t-r\t\tremount\n");
324 printf("\t-l\t\tlazy unmount\n");
325 printf("\t-h\t\tprint this help\n\n");
328 int nfsumount(int argc, char *argv[])
332 struct mntentchn *mc;
339 while ((c = getopt_long (argc, argv, "fvnrlh",
340 umount_longopts, NULL)) != -1) {
365 mc = getmntdirbackward(spec, NULL);
367 mc = getmntdevbackward(spec, NULL);
369 printf(_("Could not find %s in mtab\n"), spec);
372 ret = _nfsumount(mc->m.mnt_fsname, mc->m.mnt_opts);
374 ret = add_mtab2(mc->m.mnt_fsname, mc->m.mnt_dir,
375 mc->m.mnt_type, mc->m.mnt_opts, mc);
378 ret = _nfsumount(spec, NULL);
380 ret = add_mtab2(spec, spec, spec, spec, NULL);