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.
25 #include <sys/mount.h>
34 #include "mount_constants.h"
36 #include "nfsumount.h"
39 #if !defined(MNT_FORCE)
40 /* dare not try to include <linux/mount.h> -- lots of errors */
44 #if !defined(MNT_DETACH)
48 extern char *progname;
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 nfs_umount(dirpath *argp, CLIENT *clnt)
61 return clnt_call(clnt, MOUNTPROC_UMNT,
62 (xdrproc_t) xdr_dirpath, (caddr_t)argp,
63 (xdrproc_t) xdr_void, NULL,
67 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
70 enum clnt_stat res = 0;
73 switch (mnt_server->pmap.pm_vers) {
77 if (!probe_mntport(mnt_server))
79 clnt = mnt_openclnt(mnt_server, &msock);
82 res = nfs_umount(argp, clnt);
83 mnt_closeclnt(clnt, msock);
84 if (res == RPC_SUCCESS)
95 int del_mtab(const char *spec, const char *node)
101 res = umount2 (node, MNT_DETACH);
108 res = umount2 (node, MNT_FORCE);
113 if (errno == ENOSYS) {
115 printf(_("no umount2, trying umount...\n"));
122 if (res < 0 && remount && errno == EBUSY && spec) {
123 /* Umount failed - let us try a remount */
124 res = mount(spec, node, NULL,
125 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
129 _("umount: %s busy - remounted read-only\n"),
131 remnt.mnt_type = remnt.mnt_fsname = NULL;
132 remnt.mnt_dir = xstrdup(node);
133 remnt.mnt_opts = xstrdup("ro");
135 update_mtab(node, &remnt);
137 } else if (errno != EBUSY) { /* hmm ... */
140 _("umount: could not remount %s read-only\n"),
146 /* Umount succeeded */
148 printf (_("%s umounted\n"), spec ? spec : node);
153 (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
154 update_mtab(node, NULL);
161 umount_error(umnt_err, node);
166 * Returns 1 if everything went well, else 0.
168 int _nfsumount(const char *spec, char *opts)
172 clnt_addr_t mnt_server = { &hostname, };
173 struct mntent mnt = { .mnt_opts = opts };
174 struct pmap *pmap = &mnt_server.pmap;
177 if (spec == NULL || (p = strchr(spec,':')) == NULL)
179 hostname = xstrndup(spec, p-spec);
180 dirname = xstrdup(p+1);
181 #ifdef NFS_MOUNT_DEBUG
182 printf(_("host: %s, directory: %s\n"), hostname, dirname);
185 if (opts && (p = strstr(opts, "addr="))) {
191 while (*q && *q != ',') q++;
192 hostname = xstrndup(p,q-p);
195 if (opts && (p = strstr(opts, "mounthost="))) {
201 while (*q && *q != ',') q++;
202 hostname = xstrndup(p,q-p);
205 pmap->pm_prog = MOUNTPROG;
206 pmap->pm_vers = MOUNTVERS_NFSV3;
207 pmap->pm_prot = IPPROTO_TCP;
208 if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
209 pmap->pm_prog = atoi(p+10);
210 if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
211 pmap->pm_port = atoi(p+10);
212 if (opts && hasmntopt(&mnt, "v2"))
213 pmap->pm_vers = nfsvers_to_mnt(2);
214 if (opts && hasmntopt(&mnt, "v3"))
215 pmap->pm_vers = nfsvers_to_mnt(3);
216 if (opts && hasmntopt(&mnt, "v4"))
217 pmap->pm_vers = nfsvers_to_mnt(4);
218 if (opts && (p = strstr(opts, "vers=")) && isdigit(*(p+5)))
219 pmap->pm_vers = nfsvers_to_mnt(atoi(p+5));
220 if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
221 pmap->pm_vers = atoi(p+10);
222 if (opts && (hasmntopt(&mnt, "udp") || hasmntopt(&mnt, "proto=udp")))
223 pmap->pm_prot = IPPROTO_UDP;
225 if (!nfs_gethostbyname(hostname, &mnt_server.saddr))
227 return nfs_call_umount(&mnt_server, &dirname);
229 fprintf(stderr, "%s: %s: not found or not mounted\n", progname, spec);
233 static struct option umount_longopts[] =
235 { "force", 0, 0, 'f' },
236 { "help", 0, 0, 'h' },
237 { "no-mtab", 0, 0, 'n' },
238 { "verbose", 0, 0, 'v' },
239 { "read-only", 0, 0, 'r' },
245 printf("usage: %s dir [-fvnrlh]\n", progname);
246 printf("options:\n\t-f\t\tforce unmount\n");
247 printf("\t-v\t\tverbose\n");
248 printf("\t-n\t\tDo not update /etc/mtab\n");
249 printf("\t-r\t\tremount\n");
250 printf("\t-l\t\tlazy unmount\n");
251 printf("\t-h\t\tprint this help\n\n");
254 int nfsumount(int argc, char *argv[])
258 struct mntentchn *mc;
265 argv[0] = argv[-1]; /* So that getopt error messages are correct */
266 while ((c = getopt_long (argc, argv, "fvnrlh",
267 umount_longopts, NULL)) != -1) {
291 if (optind != argc) {
296 if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
297 printf(_("umount: %s: not found\n"), spec);
302 mc = getmntdirbackward(spec, NULL);
304 mc = getmntdevbackward(spec, NULL);
306 printf(_("Could not find %s in mtab\n"), spec);
308 if (mc && strcmp(mc->m.mnt_type, "nfs") != 0 &&
309 strcmp(mc->m.mnt_type, "nfs4") != 0) {
310 fprintf(stderr, "umount.nfs: %s on %s it not an nfs filesystem\n",
311 mc->m.mnt_fsname, mc->m.mnt_dir);
316 /* only permitted if "user=" or "users" is in mount options */
318 /* umount might call us twice. The second time there will
319 * be no entry in mtab and we should just exit quietly
324 fprintf(stderr,"%s: You are not permitted to unmount %s\n",
328 if (hasmntopt(&mc->m, "users") == NULL) {
329 char *opt = hasmntopt(&mc->m, "user");
337 comma = strchr(opt, ',');
339 len = comma - (opt + 5);
342 pw = getpwuid(getuid());
343 if (pw == NULL || strlen(pw->pw_name) != len
344 || strncmp(pw->pw_name, opt+5, len) != 0)
352 _nfsumount(mc->m.mnt_fsname, mc->m.mnt_opts);
353 ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
354 } else if (*spec != '/') {
356 _nfsumount(spec, "tcp,v3");
358 ret = del_mtab(NULL, spec);