summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fs.d/cachefs/umount/umount.c
blob: d2718f01e07b88485731ef456cc5d2be03004c21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * -----------------------------------------------------------------
 *
 *			umount.c
 *
 * CFS specific umount command.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <wait.h>
#include <ctype.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <sys/mount.h>
#include <sys/mntent.h>
#include <sys/mnttab.h>
#include <sys/fs/cachefs_fs.h>
#include <fslib.h>
#include <sys/utsname.h>
#include <rpc/rpc.h>
#include "../common/subr.h"
#include "../common/cachefsd.h"

/* forward references */
void pr_err(char *fmt, ...);
void usage(char *msgp);
int daemon_unmount(char *mntptp, int);

/*
 *
 *			main
 *
 * Description:
 *	Main routine for the cfs umount program.
 * Arguments:
 *	argc	number of command line arguments
 *	argv	list of command line arguments
 * Returns:
 *	Returns 0 for success or 1 if an error occurs.
 * Preconditions:
 */

int
main(int argc, char **argv)
{
	char *strp;
	int xx;
	int ret;
	struct mnttab mget;
	struct mnttab mref;
	FILE *finp;
	char mnt_front[PATH_MAX];
	char mnt_back[PATH_MAX];
	char *p, mnt_frontns[PATH_MAX];
	pid_t pid;
	int stat_loc;
	char *cachedirp, *s;
	int ufd, c;
	int flag = 0;

	(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN	"SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	/*
	 * Set options
	 */
	while ((c = getopt(argc, argv, "f")) != EOF) {
		switch (c) {
		case 'f':
			flag |= MS_FORCE; /* forced unmount is desired */
			break;
		default:
			usage("Incorrect number of arguments specified");
			return (1);
		}
	}
	if (flag & MS_FORCE)
		strcpy(mnt_front, argv[2]);
	else
		strcpy(mnt_front, argv[1]);
	mnt_back[0] = '\0';

	if (strlen(argv[1]) >= (size_t)PATH_MAX) {
		pr_err(gettext("name too long"));
		return (1);
	}

	/*
	 * An unmount from autofs may have a
	 * space appended to the path.
	 * Trim it off before attempting to
	 * match the mountpoint in mnttab
	 */
	strcpy(mnt_frontns, mnt_front);
	p = &mnt_frontns[strlen(mnt_frontns) - 1];
	if (*p == ' ')
		*p = '\0';

	/* get the mount point and the back file system mount point */
	finp = fopen(MNTTAB, "r");
	if (finp) {
		mntnull(&mref);
		mref.mnt_mountp = mnt_frontns;
		mref.mnt_fstype = "cachefs";
		ret = getmntany(finp, &mget, &mref);

		cachedirp = (char *)malloc(strlen(mget.mnt_special) + 1);
		strcpy(cachedirp, mget.mnt_special);
		/*
		 * AutoClient mounts do not have .cfs_mnt_points string in
		 * them, so strstr would return NULL. So .cfs_unmnt file
		 * is not updated.
		 */
		if ((s = strstr(cachedirp, ".cfs_mnt_points")) != NULL) {
			time32_t btime;

			xx = -1;
			cachedirp[s-cachedirp] = '\0';
			strcat(cachedirp, CACHEFS_UNMNT_FILE);
			if ((ufd = open(cachedirp, O_WRONLY|O_CREAT|O_TRUNC,
					0600)) > 0) {
				if ((btime = get_boottime()) != -1)
				    xx = write(ufd, &btime, sizeof (time32_t));
				close(ufd);
			}
			if (xx < 0)
				pr_err(gettext(".cfs_unmnt error"));
		}

		if (ret != -1) {
			if (mget.mnt_special)
				strcpy(mnt_back, mget.mnt_special);
		} else {
			mref.mnt_special = mref.mnt_mountp;
			mref.mnt_mountp = NULL;
			rewind(finp);
			ret = getmntany(finp, &mget, &mref);
			if (ret != -1) {
				strcpy(mnt_front, mget.mnt_mountp);
				if (mget.mnt_special)
					strcpy(mnt_back, mget.mnt_special);
			} else {
				pr_err(gettext("warning: %s not in mnttab"),
				    mref.mnt_special);
			}
		}
		fclose(finp);
	}

	/* try to get the daemon to unmount this file system for us */
	xx = daemon_unmount(mnt_front, flag);
	if (xx == ENOTSUP)
		return (1);
	if (xx == EBUSY) {
		pr_err(gettext("%s %s"), mnt_front, strerror(xx));
		return (1);
	}
	if (xx == EIO) {
		/* try to unmount the file system directly */
		if (umount2(mnt_front, flag) == -1) {
			pr_err(gettext("%s %s"), mnt_front, strerror(errno));
			return (1);
		}
	}

	/* if we do not know the name of the back file system mount point */
	if (mnt_back[0] == '\0') {
		/* all done */
		return (0);
	}

	/*
	 * If the back file system was mounted on a directory with a
	 * parent name of BACKMNT_NAME then we assume that we
	 * mounted it and that it is okay to try to umount it.
	 */
	if (strstr(mnt_back, BACKMNT_NAME) == NULL)
		return (0);

	/* if no back file system mounted */
	if (strcmp(mnt_back, "nobackfs") == 0)
		return (0);

	/* fork */
	if ((pid = fork()) == -1) {
		pr_err(gettext("could not fork %s"), strerror(errno));
		return (1);
	}

	/* if the child */
	if (pid == 0) {
		/* invoke the umount command on the back file system */
		xx = execl("/sbin/umount", "/sbin/umount", mnt_back, NULL);
		pr_err(gettext("could not exec /sbin/umount"
		    " on back file system %s"), strerror(errno));
	}

	/* else if the parent */
	else {
		/* wait for the child to exit */
		if (wait(&stat_loc) == -1) {
			pr_err(gettext("wait failed %s"), strerror(errno));
			return (1);
		}

		if (!WIFEXITED(stat_loc)) {
			pr_err(gettext("back umount did not exit"));
			return (1);
		}

		xx = WEXITSTATUS(stat_loc);
		if (xx) {
			pr_err(gettext("back umount failed"));
			return (xx);
		}
	}

	/* delete the back file system mount point since we created it */
	rmdir(mnt_back);

	return (xx);
}

/*
 *
 *			pr_err
 *
 * Description:
 *	Prints an error message to stderr.
 * Arguments:
 *	fmt	printf style format
 *	...	arguments for fmt
 * Returns:
 * Preconditions:
 *	precond(fmt)
 */

void
pr_err(char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	(void) fprintf(stderr, gettext("umount -F cachefs: "));
	(void) vfprintf(stderr, fmt, ap);
	(void) fprintf(stderr, "\n");
	va_end(ap);
}

/*
 *
 *			usage
 *
 * Description:
 *	Prints a usage message.
 * Arguments:
 *	An optional additional message to be displayed.
 * Returns:
 * Preconditions:
 */

void
usage(char *msgp)
{
	if (msgp)
		pr_err(gettext("%s"), msgp);
	fprintf(stderr, gettext("Usage: umount -F cachefs dir"));
}

/*
 *
 *			daemon_unmount
 *
 * Description:
 *	Notifies the cachefsd of an unmount request.
 * Arguments:
 *	Mount point to unmount.
 * Returns:
 *	Returns 0 if the cachefsd unmounted the file system
 *		EIO if should try unmount directly
 *		EBUSY if did not unmount because busy
 *		EAGAIN if umounted but should not unmount nfs mount
 *
 * Preconditions:
 *	precond(mntptp)
 */

int
daemon_unmount(char *mntptp, int flag)
{
	CLIENT *clnt;
	enum clnt_stat retval;
	int xx;
	int result;
	char *hostp;
	struct utsname info;
	static struct timeval TIMEOUT = { 60*60, 0 };
	static struct timeval create_timeout = { 5, 0};
	struct cachefsd_fs_unmounted uargs;

	/* get the host name */
	xx = uname(&info);
	if (xx == -1) {
		pr_err(gettext("cannot get host name, errno %d"), errno);
		return (EIO);
	}
	hostp = info.nodename;
	uargs.mntpt = mntptp;
	uargs.flag = flag;

	/* creat the connection to the daemon */
	clnt = clnt_create_timed(hostp, CACHEFSDPROG, CACHEFSDVERS, "local",
	    &create_timeout);
	if (clnt == NULL) {
		pr_err(gettext("cachefsd is not running"));
		return (EIO);
	}
	clnt_control(clnt, CLSET_TIMEOUT, (char *)&TIMEOUT);

	retval = cachefsd_fs_unmounted_1(&uargs, &result, clnt);
	if (retval != RPC_SUCCESS) {
		clnt_perror(clnt, gettext("cachefsd is not responding"));
		clnt_destroy(clnt);
		return (EIO);
	}

	clnt_destroy(clnt);

	return (result);
}