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;
49 extern int nfs_mount_version;
56 extern int find_kernel_nfs_mount_version(void);
57 extern int probe_mntport(clnt_addr_t *);
58 extern int nfs_gethostbyname(const char *, struct sockaddr_in *);
60 static inline enum clnt_stat
61 nfs_umount(dirpath *argp, CLIENT *clnt)
63 return clnt_call(clnt, MOUNTPROC_UMNT,
64 (xdrproc_t) xdr_dirpath, (caddr_t)argp,
65 (xdrproc_t) xdr_void, NULL,
69 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
72 enum clnt_stat res = 0;
75 switch (mnt_server->pmap.pm_vers) {
79 if (!probe_mntport(mnt_server))
81 clnt = mnt_openclnt(mnt_server, &msock);
84 res = nfs_umount(argp, clnt);
85 mnt_closeclnt(clnt, msock);
86 if (res == RPC_SUCCESS)
97 int del_mtab(const char *spec, const char *node)
103 res = umount2 (node, MNT_DETACH);
110 res = umount2 (node, MNT_FORCE);
115 if (errno == ENOSYS) {
117 printf(_("no umount2, trying umount...\n"));
124 if (res < 0 && remount && errno == EBUSY && spec) {
125 /* Umount failed - let us try a remount */
126 res = mount(spec, node, NULL,
127 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
131 _("umount: %s busy - remounted read-only\n"),
133 remnt.mnt_type = remnt.mnt_fsname = NULL;
134 remnt.mnt_dir = xstrdup(node);
135 remnt.mnt_opts = xstrdup("ro");
137 update_mtab(node, &remnt);
139 } else if (errno != EBUSY) { /* hmm ... */
142 _("umount: could not remount %s read-only\n"),
148 /* Umount succeeded */
150 printf (_("%s umounted\n"), spec ? spec : node);
155 (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
156 update_mtab(node, NULL);
163 umount_error(umnt_err, node);
168 * Returns 1 if everything went well, else 0.
170 int _nfsumount(const char *spec, char *opts)
174 clnt_addr_t mnt_server = { &hostname, };
175 struct mntent mnt = { .mnt_opts = opts };
176 struct pmap *pmap = &mnt_server.pmap;
179 nfs_mount_version = find_kernel_nfs_mount_version();
180 if (spec == NULL || (p = strchr(spec,':')) == NULL)
182 hostname = xstrndup(spec, p-spec);
183 dirname = xstrdup(p+1);
184 #ifdef NFS_MOUNT_DEBUG
185 printf(_("host: %s, directory: %s\n"), hostname, dirname);
188 if (opts && (p = strstr(opts, "addr="))) {
194 while (*q && *q != ',') q++;
195 hostname = xstrndup(p,q-p);
198 if (opts && (p = strstr(opts, "mounthost="))) {
204 while (*q && *q != ',') q++;
205 hostname = xstrndup(p,q-p);
208 pmap->pm_prog = MOUNTPROG;
209 pmap->pm_vers = MOUNTVERS_NFSV3;
210 pmap->pm_prot = IPPROTO_TCP;
211 if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
212 pmap->pm_prog = atoi(p+10);
213 if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
214 pmap->pm_port = atoi(p+10);
215 if (opts && hasmntopt(&mnt, "v2"))
216 pmap->pm_vers = nfsvers_to_mnt(2);
217 if (opts && hasmntopt(&mnt, "v3"))
218 pmap->pm_vers = nfsvers_to_mnt(3);
219 if (opts && hasmntopt(&mnt, "v4"))
220 pmap->pm_vers = nfsvers_to_mnt(4);
221 if (opts && (p = strstr(opts, "vers=")) && isdigit(*(p+5)))
222 pmap->pm_vers = nfsvers_to_mnt(atoi(p+5));
223 if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
224 pmap->pm_vers = atoi(p+10);
225 if (opts && (hasmntopt(&mnt, "udp") || hasmntopt(&mnt, "proto=udp")))
226 pmap->pm_prot = IPPROTO_UDP;
228 if (!nfs_gethostbyname(hostname, &mnt_server.saddr))
230 return nfs_call_umount(&mnt_server, &dirname);
232 fprintf(stderr, "%s: %s: not found or not mounted\n", progname, spec);
236 static struct option umount_longopts[] =
238 { "force", 0, 0, 'f' },
239 { "help", 0, 0, 'h' },
240 { "no-mtab", 0, 0, 'n' },
241 { "verbose", 0, 0, 'v' },
242 { "read-only", 0, 0, 'r' },
248 printf("usage: %s dir [-fvnrlh]\n", progname);
249 printf("options:\n\t-f\t\tforce unmount\n");
250 printf("\t-v\t\tverbose\n");
251 printf("\t-n\t\tDo not update /etc/mtab\n");
252 printf("\t-r\t\tremount\n");
253 printf("\t-l\t\tlazy unmount\n");
254 printf("\t-h\t\tprint this help\n\n");
257 int nfsumount(int argc, char *argv[])
261 struct mntentchn *mc;
268 argv[0] = argv[-1]; /* So that getopt error messages are correct */
269 while ((c = getopt_long (argc, argv, "fvnrlh",
270 umount_longopts, NULL)) != -1) {
294 if (optind != argc) {
299 if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
300 printf(_("umount: %s: not found\n"), spec);
305 mc = getmntdirbackward(spec, NULL);
307 mc = getmntdevbackward(spec, NULL);
309 printf(_("Could not find %s in mtab\n"), spec);
311 if (mc && strcmp(mc->m.mnt_type, "nfs") != 0 &&
312 strcmp(mc->m.mnt_type, "nfs4") != 0) {
313 fprintf(stderr, "umount.nfs: %s on %s it not an nfs filesystem\n",
314 mc->m.mnt_fsname, mc->m.mnt_dir);
319 /* only permitted if "user=" or "users" is in mount options */
321 /* umount might call us twice. The second time there will
322 * be no entry in mtab and we should just exit quietly
327 fprintf(stderr,"%s: You are not permitted to unmount %s\n",
331 if (hasmntopt(&mc->m, "users") == NULL) {
332 char *opt = hasmntopt(&mc->m, "user");
340 comma = strchr(opt, ',');
342 len = comma - (opt + 5);
345 pw = getpwuid(getuid());
346 if (pw == NULL || strlen(pw->pw_name) != len
347 || strncmp(pw->pw_name, opt+5, len) != 0)
355 _nfsumount(mc->m.mnt_fsname, mc->m.mnt_opts);
356 ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
357 } else if (*spec != '/') {
359 _nfsumount(spec, "tcp,v3");
361 ret = del_mtab(NULL, spec);