xref: /onnv-gate/usr/src/cmd/fs.d/cachefs/cfsadmin/cfsadmin.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  *
31*0Sstevel@tonic-gate  *			cfsadmin.c
32*0Sstevel@tonic-gate  *
33*0Sstevel@tonic-gate  * Cache FS admin utility.
34*0Sstevel@tonic-gate  */
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <assert.h>
37*0Sstevel@tonic-gate #include <locale.h>
38*0Sstevel@tonic-gate #include <stdio.h>
39*0Sstevel@tonic-gate #include <stdlib.h>
40*0Sstevel@tonic-gate #include <unistd.h>
41*0Sstevel@tonic-gate #include <string.h>
42*0Sstevel@tonic-gate #include <errno.h>
43*0Sstevel@tonic-gate #include <limits.h>
44*0Sstevel@tonic-gate #include <dirent.h>
45*0Sstevel@tonic-gate #include <ftw.h>
46*0Sstevel@tonic-gate #include <fcntl.h>
47*0Sstevel@tonic-gate #include <ctype.h>
48*0Sstevel@tonic-gate #include <stdarg.h>
49*0Sstevel@tonic-gate #include <sys/param.h>
50*0Sstevel@tonic-gate #include <sys/types.h>
51*0Sstevel@tonic-gate #include <sys/stat.h>
52*0Sstevel@tonic-gate #include <sys/statvfs.h>
53*0Sstevel@tonic-gate #include <sys/mman.h>
54*0Sstevel@tonic-gate #include <sys/mnttab.h>
55*0Sstevel@tonic-gate #include <sys/fs/cachefs_fs.h>
56*0Sstevel@tonic-gate #include <sys/fs/cachefs_dir.h>
57*0Sstevel@tonic-gate #include <sys/utsname.h>
58*0Sstevel@tonic-gate #include <rpc/rpc.h>
59*0Sstevel@tonic-gate #include <priv.h>
60*0Sstevel@tonic-gate #include "../common/subr.h"
61*0Sstevel@tonic-gate #include "../common/cachefsd.h"
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate char *cfsadmin_opts[] = {
64*0Sstevel@tonic-gate #define		COPT_MAXBLOCKS		0
65*0Sstevel@tonic-gate 		"maxblocks",
66*0Sstevel@tonic-gate #define		COPT_MINBLOCKS		1
67*0Sstevel@tonic-gate 		"minblocks",
68*0Sstevel@tonic-gate #define		COPT_THRESHBLOCKS	2
69*0Sstevel@tonic-gate 		"threshblocks",
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate #define		COPT_MAXFILES 		3
72*0Sstevel@tonic-gate 		"maxfiles",
73*0Sstevel@tonic-gate #define		COPT_MINFILES		4
74*0Sstevel@tonic-gate 		"minfiles",
75*0Sstevel@tonic-gate #define		COPT_THRESHFILES	5
76*0Sstevel@tonic-gate 		"threshfiles",
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate #define		COPT_MAXFILESIZE	6
79*0Sstevel@tonic-gate 		"maxfilesize",
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate #define		COPT_HIBLOCKS		7
82*0Sstevel@tonic-gate 		"hiblocks",
83*0Sstevel@tonic-gate #define		COPT_LOWBLOCKS		8
84*0Sstevel@tonic-gate 		"lowblocks",
85*0Sstevel@tonic-gate #define		COPT_HIFILES		9
86*0Sstevel@tonic-gate 		"hifiles",
87*0Sstevel@tonic-gate #define		COPT_LOWFILES		10
88*0Sstevel@tonic-gate 		"lowfiles",
89*0Sstevel@tonic-gate 		NULL
90*0Sstevel@tonic-gate };
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate #define	bad(val)	((val) == NULL || !isdigit(*(val)))
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate /* numbers must be valid percentages ranging from 0 to 100 */
95*0Sstevel@tonic-gate #define	badpercent(val) \
96*0Sstevel@tonic-gate 	((val) == NULL || !isdigit(*(val)) || \
97*0Sstevel@tonic-gate 	    atoi((val)) < 0 || atoi((val)) > 100)
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate /* forward references */
100*0Sstevel@tonic-gate void usage(char *msg);
101*0Sstevel@tonic-gate void pr_err(char *fmt, ...);
102*0Sstevel@tonic-gate int cfs_get_opts(char *oarg, struct cachefs_user_values *uvp);
103*0Sstevel@tonic-gate int update_cachelabel(char *dirp, char *optionp);
104*0Sstevel@tonic-gate void user_values_defaults(struct cachefs_user_values *uvp);
105*0Sstevel@tonic-gate int check_user_values_for_sanity(const struct cachefs_user_values *uvp);
106*0Sstevel@tonic-gate int cache_stats(char *dirp);
107*0Sstevel@tonic-gate int resource_file_grow(char *dirp, int oldcnt, int newcnt);
108*0Sstevel@tonic-gate int resource_file_dirty(char *dirp);
109*0Sstevel@tonic-gate void simulate_disconnection(char *namep, int disconnect);
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate /*
112*0Sstevel@tonic-gate  *
113*0Sstevel@tonic-gate  *			main
114*0Sstevel@tonic-gate  *
115*0Sstevel@tonic-gate  * Description:
116*0Sstevel@tonic-gate  *	Main routine for the cfsadmin program.
117*0Sstevel@tonic-gate  * Arguments:
118*0Sstevel@tonic-gate  *	argc	number of command line arguments
119*0Sstevel@tonic-gate  *	argv	command line arguments
120*0Sstevel@tonic-gate  * Returns:
121*0Sstevel@tonic-gate  *	Returns 0 for failure, > 0 for an error.
122*0Sstevel@tonic-gate  * Preconditions:
123*0Sstevel@tonic-gate  */
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate int
main(int argc,char ** argv)126*0Sstevel@tonic-gate main(int argc, char **argv)
127*0Sstevel@tonic-gate {
128*0Sstevel@tonic-gate 	int c;
129*0Sstevel@tonic-gate 	int xx;
130*0Sstevel@tonic-gate 	int lockid;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	char *cacheid;
133*0Sstevel@tonic-gate 	char *cachedir;
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	int cflag;
136*0Sstevel@tonic-gate 	int uflag;
137*0Sstevel@tonic-gate 	int dflag;
138*0Sstevel@tonic-gate 	int sflag;
139*0Sstevel@tonic-gate 	int allflag;
140*0Sstevel@tonic-gate 	int lflag;
141*0Sstevel@tonic-gate 	char *optionp;
142*0Sstevel@tonic-gate 	int Cflag;
143*0Sstevel@tonic-gate 	int Dflag;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	priv_set_t *priv_needed, *priv_effective;
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
150*0Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
151*0Sstevel@tonic-gate #endif
152*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	/* set defaults for command line options */
155*0Sstevel@tonic-gate 	cflag = 0;
156*0Sstevel@tonic-gate 	uflag = 0;
157*0Sstevel@tonic-gate 	dflag = 0;
158*0Sstevel@tonic-gate 	sflag = 0;
159*0Sstevel@tonic-gate 	allflag = 0;
160*0Sstevel@tonic-gate 	lflag = 0;
161*0Sstevel@tonic-gate 	optionp = NULL;
162*0Sstevel@tonic-gate 	Cflag = 0;
163*0Sstevel@tonic-gate 	Dflag = 0;
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	/* parse the command line arguments */
166*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "cCDuo:d:sl")) != EOF) {
167*0Sstevel@tonic-gate 		switch (c) {
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 		case 'c':		/* create */
170*0Sstevel@tonic-gate 			cflag = 1;
171*0Sstevel@tonic-gate 			break;
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 			/*
174*0Sstevel@tonic-gate 			 * -C and -D are undocumented calls used
175*0Sstevel@tonic-gate 			 * to simulate disconnection on a file system.
176*0Sstevel@tonic-gate 			 */
177*0Sstevel@tonic-gate 		case 'C':		/* connect file system */
178*0Sstevel@tonic-gate 			Cflag = 1;
179*0Sstevel@tonic-gate 			break;
180*0Sstevel@tonic-gate 		case 'D':		/* disconnect file system */
181*0Sstevel@tonic-gate 			Dflag = 1;
182*0Sstevel@tonic-gate 			break;
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 		case 'u':		/* update */
185*0Sstevel@tonic-gate 			uflag = 1;
186*0Sstevel@tonic-gate 			break;
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 		case 'd':		/* delete */
189*0Sstevel@tonic-gate 			dflag = 1;
190*0Sstevel@tonic-gate 			if (strcmp(optarg, "all") == 0)
191*0Sstevel@tonic-gate 				allflag = 1;
192*0Sstevel@tonic-gate 			else
193*0Sstevel@tonic-gate 				cacheid = optarg;
194*0Sstevel@tonic-gate 			break;
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 		case 's':		/* consistency on demand */
197*0Sstevel@tonic-gate 			sflag = 1;
198*0Sstevel@tonic-gate 			break;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 		case 'l':		/* list cache ids */
201*0Sstevel@tonic-gate 			lflag = 1;
202*0Sstevel@tonic-gate 			break;
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 		case 'o':		/* options for update and create */
205*0Sstevel@tonic-gate 			optionp = optarg;
206*0Sstevel@tonic-gate 			break;
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 		default:
209*0Sstevel@tonic-gate 			usage(gettext("illegal option"));
210*0Sstevel@tonic-gate 			return (1);
211*0Sstevel@tonic-gate 		}
212*0Sstevel@tonic-gate 	}
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	if ((cflag + dflag + lflag + sflag + uflag + Cflag + Dflag) == 0) {
215*0Sstevel@tonic-gate 		usage(gettext("no options specified"));
216*0Sstevel@tonic-gate 		return (1);
217*0Sstevel@tonic-gate 	}
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	if (cflag || uflag || dflag || Cflag || Dflag)
220*0Sstevel@tonic-gate 		priv_needed = priv_str_to_set("all", ",", NULL);
221*0Sstevel@tonic-gate 	if ((cflag || uflag) && getuid() != 0) {
222*0Sstevel@tonic-gate 		/* These options create files. We want them to be root owned */
223*0Sstevel@tonic-gate 		pr_err(gettext("must be run by root"));
224*0Sstevel@tonic-gate 		return (1);
225*0Sstevel@tonic-gate 	}
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	else if (lflag)
228*0Sstevel@tonic-gate 		priv_needed = priv_str_to_set("file_dac_search,file_dac_read",
229*0Sstevel@tonic-gate 		    ",", NULL);
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 	else if (sflag)
232*0Sstevel@tonic-gate 		priv_needed = priv_str_to_set("sys_config", ",", NULL);
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	priv_effective = priv_allocset();
235*0Sstevel@tonic-gate 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
236*0Sstevel@tonic-gate 	if (priv_issubset(priv_needed, priv_effective) == 0) {
237*0Sstevel@tonic-gate 		pr_err(gettext("Not privileged."));
238*0Sstevel@tonic-gate 		return (1);
239*0Sstevel@tonic-gate 	}
240*0Sstevel@tonic-gate 	priv_freeset(priv_effective);
241*0Sstevel@tonic-gate 	priv_freeset(priv_needed);
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	if ((sflag + Cflag + Dflag) == 0) {
244*0Sstevel@tonic-gate 		/* make sure cachedir is specified */
245*0Sstevel@tonic-gate 		if (argc - 1 != optind) {
246*0Sstevel@tonic-gate 			usage(gettext("cache directory not specified"));
247*0Sstevel@tonic-gate 			return (1);
248*0Sstevel@tonic-gate 		}
249*0Sstevel@tonic-gate 		cachedir = argv[argc-1];
250*0Sstevel@tonic-gate 	} else {
251*0Sstevel@tonic-gate 		/* make sure at least one mount point is specified */
252*0Sstevel@tonic-gate 		if (argc - 1 < optind) {
253*0Sstevel@tonic-gate 			usage(gettext("mount points not specified"));
254*0Sstevel@tonic-gate 			return (1);
255*0Sstevel@tonic-gate 		}
256*0Sstevel@tonic-gate 	}
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 	/* make sure a reasonable set of flags were specified */
259*0Sstevel@tonic-gate 	if ((cflag + uflag + dflag + sflag + lflag + Cflag + Dflag) != 1) {
260*0Sstevel@tonic-gate 		/* flags are mutually exclusive, at least one must be set */
261*0Sstevel@tonic-gate 		usage(gettext(
262*0Sstevel@tonic-gate 		    "exactly one of -c, -u, -d, -s, -l must be specified"));
263*0Sstevel@tonic-gate 		return (1);
264*0Sstevel@tonic-gate 	}
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	/* make sure -o specified with -c or -u */
267*0Sstevel@tonic-gate 	if (optionp && !(cflag|uflag)) {
268*0Sstevel@tonic-gate 		usage(gettext("-o can only be used with -c or -u"));
269*0Sstevel@tonic-gate 		return (1);
270*0Sstevel@tonic-gate 	}
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	/* if creating a cache */
273*0Sstevel@tonic-gate 	if (cflag) {
274*0Sstevel@tonic-gate 		struct cachefs_user_values uv;
275*0Sstevel@tonic-gate 		struct cache_label clabel;
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 		/* get default cache paramaters */
278*0Sstevel@tonic-gate 		user_values_defaults(&uv);
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 		/* parse the options if specified */
281*0Sstevel@tonic-gate 		if (optionp) {
282*0Sstevel@tonic-gate 			xx = cfs_get_opts(optionp, &uv);
283*0Sstevel@tonic-gate 			if (xx)
284*0Sstevel@tonic-gate 				return (1);
285*0Sstevel@tonic-gate 		}
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 		/* verify options are reasonable */
288*0Sstevel@tonic-gate 		xx = check_user_values_for_sanity(&uv);
289*0Sstevel@tonic-gate 		if (xx)
290*0Sstevel@tonic-gate 			return (1);
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 		/* lock the cache directory non-shared */
293*0Sstevel@tonic-gate 		lockid = cachefs_dir_lock(cachedir, 0);
294*0Sstevel@tonic-gate 		if (lockid == -1) {
295*0Sstevel@tonic-gate 			/* quit if could not get the lock */
296*0Sstevel@tonic-gate 			return (1);
297*0Sstevel@tonic-gate 		}
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 		/* create the cache */
300*0Sstevel@tonic-gate 		xx = cachefs_create_cache(cachedir, &uv, &clabel);
301*0Sstevel@tonic-gate 		if (xx != 0) {
302*0Sstevel@tonic-gate 			if (xx == -2) {
303*0Sstevel@tonic-gate 				/* remove a partially created cache dir */
304*0Sstevel@tonic-gate 				(void) cachefs_delete_all_cache(cachedir);
305*0Sstevel@tonic-gate 			}
306*0Sstevel@tonic-gate 			cachefs_dir_unlock(lockid);
307*0Sstevel@tonic-gate 			return (1);
308*0Sstevel@tonic-gate 		}
309*0Sstevel@tonic-gate 		cachefs_dir_unlock(lockid);
310*0Sstevel@tonic-gate 	}
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 	/* else if updating resource parameters */
313*0Sstevel@tonic-gate 	else if (uflag) {
314*0Sstevel@tonic-gate 		/* lock the cache directory non-shared */
315*0Sstevel@tonic-gate 		lockid = cachefs_dir_lock(cachedir, 0);
316*0Sstevel@tonic-gate 		if (lockid == -1) {
317*0Sstevel@tonic-gate 			/* quit if could not get the lock */
318*0Sstevel@tonic-gate 			return (1);
319*0Sstevel@tonic-gate 		}
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate 		xx = update_cachelabel(cachedir, optionp);
322*0Sstevel@tonic-gate 		cachefs_dir_unlock(lockid);
323*0Sstevel@tonic-gate 		if (xx != 0) {
324*0Sstevel@tonic-gate 			return (1);
325*0Sstevel@tonic-gate 		}
326*0Sstevel@tonic-gate 	}
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 	/* else if deleting a specific cacheID (or all caches) */
329*0Sstevel@tonic-gate 	else if (dflag) {
330*0Sstevel@tonic-gate 		/* lock the cache directory non-shared */
331*0Sstevel@tonic-gate 		lockid = cachefs_dir_lock(cachedir, 0);
332*0Sstevel@tonic-gate 		if (lockid == -1) {
333*0Sstevel@tonic-gate 			/* quit if could not get the lock */
334*0Sstevel@tonic-gate 			return (1);
335*0Sstevel@tonic-gate 		}
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 		/* if the cache is in use */
338*0Sstevel@tonic-gate 		if (cachefs_inuse(cachedir)) {
339*0Sstevel@tonic-gate 			pr_err(gettext("Cache %s is in use and "
340*0Sstevel@tonic-gate 			    "cannot be modified."), cachedir);
341*0Sstevel@tonic-gate 			cachefs_dir_unlock(lockid);
342*0Sstevel@tonic-gate 			return (1);
343*0Sstevel@tonic-gate 		}
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 		if (allflag)
346*0Sstevel@tonic-gate 			xx = cachefs_delete_all_cache(cachedir);
347*0Sstevel@tonic-gate 		else {
348*0Sstevel@tonic-gate 			/* mark resource file as dirty */
349*0Sstevel@tonic-gate 			xx = resource_file_dirty(cachedir);
350*0Sstevel@tonic-gate 			if (xx == 0)
351*0Sstevel@tonic-gate 				xx = cachefs_delete_cache(cachedir, cacheid);
352*0Sstevel@tonic-gate 		}
353*0Sstevel@tonic-gate 		cachefs_dir_unlock(lockid);
354*0Sstevel@tonic-gate 		if (xx != 0) {
355*0Sstevel@tonic-gate 			return (1);
356*0Sstevel@tonic-gate 		}
357*0Sstevel@tonic-gate 	}
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	/* else if listing cache statistics */
360*0Sstevel@tonic-gate 	else if (lflag) {
361*0Sstevel@tonic-gate 		xx = cache_stats(cachedir);
362*0Sstevel@tonic-gate 		if (xx != 0)
363*0Sstevel@tonic-gate 			return (1);
364*0Sstevel@tonic-gate 	}
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate 	/* else if issuing a check event to cached file systems */
367*0Sstevel@tonic-gate 	else if (sflag) {
368*0Sstevel@tonic-gate 		for (xx = optind; xx < argc; xx++) {
369*0Sstevel@tonic-gate 			issue_cod(argv[xx]);
370*0Sstevel@tonic-gate 		}
371*0Sstevel@tonic-gate 	}
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 	/* else if simulating a disconnection */
374*0Sstevel@tonic-gate 	else if (Dflag) {
375*0Sstevel@tonic-gate 		for (xx = optind; xx < argc; xx++) {
376*0Sstevel@tonic-gate 			simulate_disconnection(argv[xx], 1);
377*0Sstevel@tonic-gate 		}
378*0Sstevel@tonic-gate 	}
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 	/* else if connection after a simulated disconnection */
381*0Sstevel@tonic-gate 	else if (Cflag) {
382*0Sstevel@tonic-gate 		for (xx = optind; xx < argc; xx++) {
383*0Sstevel@tonic-gate 			simulate_disconnection(argv[xx], 0);
384*0Sstevel@tonic-gate 		}
385*0Sstevel@tonic-gate 	}
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 	/* return success */
388*0Sstevel@tonic-gate 	return (0);
389*0Sstevel@tonic-gate }
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate /*
393*0Sstevel@tonic-gate  *
394*0Sstevel@tonic-gate  *			usage
395*0Sstevel@tonic-gate  *
396*0Sstevel@tonic-gate  * Description:
397*0Sstevel@tonic-gate  *	Prints a usage message for this utility.
398*0Sstevel@tonic-gate  * Arguments:
399*0Sstevel@tonic-gate  *	msgp	message to include with the usage message
400*0Sstevel@tonic-gate  * Returns:
401*0Sstevel@tonic-gate  * Preconditions:
402*0Sstevel@tonic-gate  *	precond(msgp)
403*0Sstevel@tonic-gate  */
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate void
usage(char * msgp)406*0Sstevel@tonic-gate usage(char *msgp)
407*0Sstevel@tonic-gate {
408*0Sstevel@tonic-gate 	fprintf(stderr, gettext("cfsadmin: %s\n"), msgp);
409*0Sstevel@tonic-gate 	fprintf(stderr, gettext(
410*0Sstevel@tonic-gate 	    "usage: cfsadmin -[cu] [-o parameter-list] cachedir\n"));
411*0Sstevel@tonic-gate 	fprintf(stderr, gettext("       cfsadmin -d [CacheID|all] cachedir\n"));
412*0Sstevel@tonic-gate 	fprintf(stderr, gettext("       cfsadmin -l cachedir\n"));
413*0Sstevel@tonic-gate 	fprintf(stderr, gettext("       cfsadmin -s [mntpnt1 ... | all]\n"));
414*0Sstevel@tonic-gate }
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate /*
417*0Sstevel@tonic-gate  *
418*0Sstevel@tonic-gate  *			pr_err
419*0Sstevel@tonic-gate  *
420*0Sstevel@tonic-gate  * Description:
421*0Sstevel@tonic-gate  *	Prints an error message to stderr.
422*0Sstevel@tonic-gate  * Arguments:
423*0Sstevel@tonic-gate  *	fmt	printf style format
424*0Sstevel@tonic-gate  *	...	arguments for fmt
425*0Sstevel@tonic-gate  * Returns:
426*0Sstevel@tonic-gate  * Preconditions:
427*0Sstevel@tonic-gate  *	precond(fmt)
428*0Sstevel@tonic-gate  */
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate void
pr_err(char * fmt,...)431*0Sstevel@tonic-gate pr_err(char *fmt, ...)
432*0Sstevel@tonic-gate {
433*0Sstevel@tonic-gate 	va_list ap;
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 	va_start(ap, fmt);
436*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("cfsadmin: "));
437*0Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
438*0Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
439*0Sstevel@tonic-gate 	va_end(ap);
440*0Sstevel@tonic-gate }
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate /*
443*0Sstevel@tonic-gate  *
444*0Sstevel@tonic-gate  *			cfs_get_opts
445*0Sstevel@tonic-gate  *
446*0Sstevel@tonic-gate  * Description:
447*0Sstevel@tonic-gate  *	Decodes cfs options specified with -o.
448*0Sstevel@tonic-gate  *	Only the fields referenced by the options are modified.
449*0Sstevel@tonic-gate  * Arguments:
450*0Sstevel@tonic-gate  *	oarg	options from -o option
451*0Sstevel@tonic-gate  *	uvp	place to put options
452*0Sstevel@tonic-gate  * Returns:
453*0Sstevel@tonic-gate  *	Returns 0 for success, -1 for an error.
454*0Sstevel@tonic-gate  * Preconditions:
455*0Sstevel@tonic-gate  *	precond(oarg)
456*0Sstevel@tonic-gate  *	precond(uvp)
457*0Sstevel@tonic-gate  */
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate int
cfs_get_opts(char * oarg,struct cachefs_user_values * uvp)460*0Sstevel@tonic-gate cfs_get_opts(char *oarg, struct cachefs_user_values *uvp)
461*0Sstevel@tonic-gate {
462*0Sstevel@tonic-gate 	char *optstr, *opts, *val;
463*0Sstevel@tonic-gate 	char *saveopts;
464*0Sstevel@tonic-gate 	int badopt;
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 	/* make a copy of the options because getsubopt modifies it */
467*0Sstevel@tonic-gate 	optstr = opts = strdup(oarg);
468*0Sstevel@tonic-gate 	if (opts == NULL) {
469*0Sstevel@tonic-gate 		pr_err(gettext("no memory"));
470*0Sstevel@tonic-gate 		return (-1);
471*0Sstevel@tonic-gate 	}
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 	/* process the options */
474*0Sstevel@tonic-gate 	badopt = 0;
475*0Sstevel@tonic-gate 	while (*opts && !badopt) {
476*0Sstevel@tonic-gate 		saveopts = opts;
477*0Sstevel@tonic-gate 		switch (getsubopt(&opts, cfsadmin_opts, &val)) {
478*0Sstevel@tonic-gate 		case COPT_MAXBLOCKS:
479*0Sstevel@tonic-gate 			if (badpercent(val))
480*0Sstevel@tonic-gate 				badopt = 1;
481*0Sstevel@tonic-gate 			else
482*0Sstevel@tonic-gate 				uvp->uv_maxblocks = atoi(val);
483*0Sstevel@tonic-gate 			break;
484*0Sstevel@tonic-gate 		case COPT_MINBLOCKS:
485*0Sstevel@tonic-gate 			if (badpercent(val))
486*0Sstevel@tonic-gate 				badopt = 1;
487*0Sstevel@tonic-gate 			else
488*0Sstevel@tonic-gate 				uvp->uv_minblocks = atoi(val);
489*0Sstevel@tonic-gate 			break;
490*0Sstevel@tonic-gate 		case COPT_THRESHBLOCKS:
491*0Sstevel@tonic-gate 			if (badpercent(val))
492*0Sstevel@tonic-gate 				badopt = 1;
493*0Sstevel@tonic-gate 			else
494*0Sstevel@tonic-gate 				uvp->uv_threshblocks = atoi(val);
495*0Sstevel@tonic-gate 			break;
496*0Sstevel@tonic-gate 
497*0Sstevel@tonic-gate 		case COPT_MAXFILES:
498*0Sstevel@tonic-gate 			if (badpercent(val))
499*0Sstevel@tonic-gate 				badopt = 1;
500*0Sstevel@tonic-gate 			else
501*0Sstevel@tonic-gate 				uvp->uv_maxfiles = atoi(val);
502*0Sstevel@tonic-gate 			break;
503*0Sstevel@tonic-gate 		case COPT_MINFILES:
504*0Sstevel@tonic-gate 			if (badpercent(val))
505*0Sstevel@tonic-gate 				badopt = 1;
506*0Sstevel@tonic-gate 			else
507*0Sstevel@tonic-gate 				uvp->uv_minfiles = atoi(val);
508*0Sstevel@tonic-gate 			break;
509*0Sstevel@tonic-gate 		case COPT_THRESHFILES:
510*0Sstevel@tonic-gate 			if (badpercent(val))
511*0Sstevel@tonic-gate 				badopt = 1;
512*0Sstevel@tonic-gate 			else
513*0Sstevel@tonic-gate 				uvp->uv_threshfiles = atoi(val);
514*0Sstevel@tonic-gate 			break;
515*0Sstevel@tonic-gate 
516*0Sstevel@tonic-gate 		case COPT_MAXFILESIZE:
517*0Sstevel@tonic-gate 			if (bad(val))
518*0Sstevel@tonic-gate 				badopt = 1;
519*0Sstevel@tonic-gate 			else
520*0Sstevel@tonic-gate 				uvp->uv_maxfilesize = atoi(val);
521*0Sstevel@tonic-gate 			break;
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate 		case COPT_HIBLOCKS:
524*0Sstevel@tonic-gate 			if (badpercent(val))
525*0Sstevel@tonic-gate 				badopt = 1;
526*0Sstevel@tonic-gate 			else
527*0Sstevel@tonic-gate 				uvp->uv_hiblocks = atoi(val);
528*0Sstevel@tonic-gate 			break;
529*0Sstevel@tonic-gate 		case COPT_LOWBLOCKS:
530*0Sstevel@tonic-gate 			if (badpercent(val))
531*0Sstevel@tonic-gate 				badopt = 1;
532*0Sstevel@tonic-gate 			else
533*0Sstevel@tonic-gate 				uvp->uv_lowblocks = atoi(val);
534*0Sstevel@tonic-gate 			break;
535*0Sstevel@tonic-gate 		case COPT_HIFILES:
536*0Sstevel@tonic-gate 			if (badpercent(val))
537*0Sstevel@tonic-gate 				badopt = 1;
538*0Sstevel@tonic-gate 			else
539*0Sstevel@tonic-gate 				uvp->uv_hifiles = atoi(val);
540*0Sstevel@tonic-gate 			break;
541*0Sstevel@tonic-gate 		case COPT_LOWFILES:
542*0Sstevel@tonic-gate 			if (badpercent(val))
543*0Sstevel@tonic-gate 				badopt = 1;
544*0Sstevel@tonic-gate 			else
545*0Sstevel@tonic-gate 				uvp->uv_lowfiles = atoi(val);
546*0Sstevel@tonic-gate 			break;
547*0Sstevel@tonic-gate 		default:
548*0Sstevel@tonic-gate 			/* if a bad option argument */
549*0Sstevel@tonic-gate 			pr_err(gettext("Invalid option %s"), saveopts);
550*0Sstevel@tonic-gate 			return (-1);
551*0Sstevel@tonic-gate 		}
552*0Sstevel@tonic-gate 	}
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate 	/* if a bad value for an option, display an error message */
555*0Sstevel@tonic-gate 	if (badopt) {
556*0Sstevel@tonic-gate 		pr_err(gettext("invalid argument to option: \"%s\""),
557*0Sstevel@tonic-gate 		    saveopts);
558*0Sstevel@tonic-gate 	}
559*0Sstevel@tonic-gate 
560*0Sstevel@tonic-gate 	/* free the duplicated option string */
561*0Sstevel@tonic-gate 	free(optstr);
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate 	/* return the result */
564*0Sstevel@tonic-gate 	return (badopt ? -1 : 0);
565*0Sstevel@tonic-gate }
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate /*
568*0Sstevel@tonic-gate  *
569*0Sstevel@tonic-gate  *			update_cachelabel
570*0Sstevel@tonic-gate  *
571*0Sstevel@tonic-gate  * Description:
572*0Sstevel@tonic-gate  *	Changes the parameters of the cache_label.
573*0Sstevel@tonic-gate  *	If optionp is NULL then the cache_label is set to
574*0Sstevel@tonic-gate  *	default values.
575*0Sstevel@tonic-gate  * Arguments:
576*0Sstevel@tonic-gate  *	dirp		the name of the cache directory
577*0Sstevel@tonic-gate  *	optionp		comma delimited options
578*0Sstevel@tonic-gate  * Returns:
579*0Sstevel@tonic-gate  *	Returns 0 for success and -1 for an error.
580*0Sstevel@tonic-gate  * Preconditions:
581*0Sstevel@tonic-gate  *	precond(dirp)
582*0Sstevel@tonic-gate  */
583*0Sstevel@tonic-gate 
584*0Sstevel@tonic-gate int
update_cachelabel(char * dirp,char * optionp)585*0Sstevel@tonic-gate update_cachelabel(char *dirp, char *optionp)
586*0Sstevel@tonic-gate {
587*0Sstevel@tonic-gate 	char path[CACHEFS_XMAXPATH];
588*0Sstevel@tonic-gate 	struct cache_label clabel_new;
589*0Sstevel@tonic-gate 	struct cache_label clabel_orig;
590*0Sstevel@tonic-gate 	struct cachefs_user_values uv_orig, uv_new;
591*0Sstevel@tonic-gate 	int xx;
592*0Sstevel@tonic-gate 
593*0Sstevel@tonic-gate 	/* if the cache is in use */
594*0Sstevel@tonic-gate 	if (cachefs_inuse(dirp)) {
595*0Sstevel@tonic-gate 		pr_err(gettext("Cache %s is in use and cannot be modified."),
596*0Sstevel@tonic-gate 		    dirp);
597*0Sstevel@tonic-gate 		return (-1);
598*0Sstevel@tonic-gate 	}
599*0Sstevel@tonic-gate 
600*0Sstevel@tonic-gate 	/* make sure we don't overwrite path */
601*0Sstevel@tonic-gate 	if (strlen(dirp) > (size_t)PATH_MAX) {
602*0Sstevel@tonic-gate 		pr_err(gettext("name of label file %s is too long."),
603*0Sstevel@tonic-gate 		    dirp);
604*0Sstevel@tonic-gate 		return (-1);
605*0Sstevel@tonic-gate 	}
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate 	/* construct the pathname to the cach_label file */
608*0Sstevel@tonic-gate 	sprintf(path, "%s/%s", dirp, CACHELABEL_NAME);
609*0Sstevel@tonic-gate 
610*0Sstevel@tonic-gate 	/* read the current set of parameters */
611*0Sstevel@tonic-gate 	xx = cachefs_label_file_get(path, &clabel_orig);
612*0Sstevel@tonic-gate 	if (xx == -1) {
613*0Sstevel@tonic-gate 		pr_err(gettext("reading %s failed"), path);
614*0Sstevel@tonic-gate 		return (-1);
615*0Sstevel@tonic-gate 	}
616*0Sstevel@tonic-gate 	xx = cachefs_label_file_vcheck(path, &clabel_orig);
617*0Sstevel@tonic-gate 	if (xx != 0) {
618*0Sstevel@tonic-gate 		pr_err(gettext("version mismatch on %s"), path);
619*0Sstevel@tonic-gate 		return (-1);
620*0Sstevel@tonic-gate 	}
621*0Sstevel@tonic-gate 
622*0Sstevel@tonic-gate 	/* convert the cache_label to user values */
623*0Sstevel@tonic-gate 	xx = cachefs_convert_cl2uv(&clabel_orig, &uv_orig, dirp);
624*0Sstevel@tonic-gate 	if (xx) {
625*0Sstevel@tonic-gate 		return (-1);
626*0Sstevel@tonic-gate 	}
627*0Sstevel@tonic-gate 
628*0Sstevel@tonic-gate 	/* if options were specified */
629*0Sstevel@tonic-gate 	if (optionp) {
630*0Sstevel@tonic-gate 		/* start with the original values */
631*0Sstevel@tonic-gate 		uv_new = uv_orig;
632*0Sstevel@tonic-gate 
633*0Sstevel@tonic-gate 		/* parse the options */
634*0Sstevel@tonic-gate 		xx = cfs_get_opts(optionp, &uv_new);
635*0Sstevel@tonic-gate 		if (xx) {
636*0Sstevel@tonic-gate 			return (-1);
637*0Sstevel@tonic-gate 		}
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate 		/* verify options are reasonable */
640*0Sstevel@tonic-gate 		xx = check_user_values_for_sanity(&uv_new);
641*0Sstevel@tonic-gate 		if (xx) {
642*0Sstevel@tonic-gate 			return (-1);
643*0Sstevel@tonic-gate 		}
644*0Sstevel@tonic-gate 	}
645*0Sstevel@tonic-gate 
646*0Sstevel@tonic-gate 	/* else if options where not specified, get defaults */
647*0Sstevel@tonic-gate 	else {
648*0Sstevel@tonic-gate 		user_values_defaults(&uv_new);
649*0Sstevel@tonic-gate 	}
650*0Sstevel@tonic-gate 
651*0Sstevel@tonic-gate 	/* convert user values to a cache_label */
652*0Sstevel@tonic-gate 	xx = cachefs_convert_uv2cl(&uv_new, &clabel_new, dirp);
653*0Sstevel@tonic-gate 	if (xx) {
654*0Sstevel@tonic-gate 		return (-1);
655*0Sstevel@tonic-gate 	}
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate 	/* do not allow the cache size to shrink */
658*0Sstevel@tonic-gate 	if (uv_orig.uv_maxblocks > uv_new.uv_maxblocks) {
659*0Sstevel@tonic-gate 		pr_err(gettext("Cache size cannot be reduced,"
660*0Sstevel@tonic-gate 			" maxblocks current %d%%, requested %d%%"),
661*0Sstevel@tonic-gate 			uv_orig.uv_maxblocks, uv_new.uv_maxblocks);
662*0Sstevel@tonic-gate 		return (-1);
663*0Sstevel@tonic-gate 	}
664*0Sstevel@tonic-gate 	if (clabel_orig.cl_maxinodes > clabel_new.cl_maxinodes) {
665*0Sstevel@tonic-gate 		pr_err(gettext("Cache size cannot be reduced,"
666*0Sstevel@tonic-gate 			" maxfiles current %d%% requested %d%%"),
667*0Sstevel@tonic-gate 			uv_orig.uv_maxfiles, uv_new.uv_maxfiles);
668*0Sstevel@tonic-gate 		return (-1);
669*0Sstevel@tonic-gate 	}
670*0Sstevel@tonic-gate 
671*0Sstevel@tonic-gate 	/* write back the new values */
672*0Sstevel@tonic-gate 	xx = cachefs_label_file_put(path, &clabel_new);
673*0Sstevel@tonic-gate 	if (xx == -1) {
674*0Sstevel@tonic-gate 		pr_err(gettext("writing %s failed"), path);
675*0Sstevel@tonic-gate 		return (-1);
676*0Sstevel@tonic-gate 	}
677*0Sstevel@tonic-gate 
678*0Sstevel@tonic-gate 	/* put the new values in the duplicate cache label file also */
679*0Sstevel@tonic-gate 	sprintf(path, "%s/%s.dup", dirp, CACHELABEL_NAME);
680*0Sstevel@tonic-gate 	xx = cachefs_label_file_put(path, &clabel_new);
681*0Sstevel@tonic-gate 	if (xx == -1) {
682*0Sstevel@tonic-gate 		pr_err(gettext("writing %s failed"), path);
683*0Sstevel@tonic-gate 		return (-1);
684*0Sstevel@tonic-gate 	}
685*0Sstevel@tonic-gate 
686*0Sstevel@tonic-gate 	/* grow resouces file if necessary */
687*0Sstevel@tonic-gate 	xx = 0;
688*0Sstevel@tonic-gate 	if (clabel_orig.cl_maxinodes != clabel_new.cl_maxinodes) {
689*0Sstevel@tonic-gate 		xx = resource_file_grow(dirp, clabel_orig.cl_maxinodes,
690*0Sstevel@tonic-gate 			clabel_new.cl_maxinodes);
691*0Sstevel@tonic-gate 	}
692*0Sstevel@tonic-gate 
693*0Sstevel@tonic-gate 	/* return status */
694*0Sstevel@tonic-gate 	return (xx);
695*0Sstevel@tonic-gate }
696*0Sstevel@tonic-gate 
697*0Sstevel@tonic-gate /*
698*0Sstevel@tonic-gate  *
699*0Sstevel@tonic-gate  *			user_values_defaults
700*0Sstevel@tonic-gate  *
701*0Sstevel@tonic-gate  * Description:
702*0Sstevel@tonic-gate  *	Sets default values in the cachefs_user_values object.
703*0Sstevel@tonic-gate  * Arguments:
704*0Sstevel@tonic-gate  *	uvp	cachefs_user_values object to set values for
705*0Sstevel@tonic-gate  * Returns:
706*0Sstevel@tonic-gate  * Preconditions:
707*0Sstevel@tonic-gate  *	precond(uvp)
708*0Sstevel@tonic-gate  */
709*0Sstevel@tonic-gate 
710*0Sstevel@tonic-gate void
user_values_defaults(struct cachefs_user_values * uvp)711*0Sstevel@tonic-gate user_values_defaults(struct cachefs_user_values *uvp)
712*0Sstevel@tonic-gate {
713*0Sstevel@tonic-gate 	uvp->uv_maxblocks = 90;
714*0Sstevel@tonic-gate 	uvp->uv_minblocks = 0;
715*0Sstevel@tonic-gate 	uvp->uv_threshblocks = 85;
716*0Sstevel@tonic-gate 	uvp->uv_maxfiles = 90;
717*0Sstevel@tonic-gate 	uvp->uv_minfiles = 0;
718*0Sstevel@tonic-gate 	uvp->uv_threshfiles = 85;
719*0Sstevel@tonic-gate 	uvp->uv_maxfilesize = 3;
720*0Sstevel@tonic-gate 	uvp->uv_hiblocks = 85;
721*0Sstevel@tonic-gate 	uvp->uv_lowblocks = 75;
722*0Sstevel@tonic-gate 	uvp->uv_hifiles = 85;
723*0Sstevel@tonic-gate 	uvp->uv_lowfiles = 75;
724*0Sstevel@tonic-gate }
725*0Sstevel@tonic-gate 
726*0Sstevel@tonic-gate /*
727*0Sstevel@tonic-gate  *
728*0Sstevel@tonic-gate  *			check_user_values_for_sanity
729*0Sstevel@tonic-gate  *
730*0Sstevel@tonic-gate  * Description:
731*0Sstevel@tonic-gate  *	Check the cachefs_user_values for sanity.
732*0Sstevel@tonic-gate  * Arguments:
733*0Sstevel@tonic-gate  *	uvp	cachefs_user_values object to check
734*0Sstevel@tonic-gate  * Returns:
735*0Sstevel@tonic-gate  *	Returns 0 if okay, -1 if not.
736*0Sstevel@tonic-gate  * Preconditions:
737*0Sstevel@tonic-gate  *	precond(uvp)
738*0Sstevel@tonic-gate  */
739*0Sstevel@tonic-gate 
740*0Sstevel@tonic-gate int
check_user_values_for_sanity(const struct cachefs_user_values * uvp)741*0Sstevel@tonic-gate check_user_values_for_sanity(const struct cachefs_user_values *uvp)
742*0Sstevel@tonic-gate {
743*0Sstevel@tonic-gate 	int ret;
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate 	ret = 0;
746*0Sstevel@tonic-gate 
747*0Sstevel@tonic-gate 	if (uvp->uv_lowblocks >= uvp->uv_hiblocks) {
748*0Sstevel@tonic-gate 		pr_err(gettext("lowblocks can't be >= hiblocks."));
749*0Sstevel@tonic-gate 		ret = -1;
750*0Sstevel@tonic-gate 	}
751*0Sstevel@tonic-gate 	if (uvp->uv_lowfiles >= uvp->uv_hifiles) {
752*0Sstevel@tonic-gate 		pr_err(gettext("lowfiles can't be >= hifiles."));
753*0Sstevel@tonic-gate 		ret = -1;
754*0Sstevel@tonic-gate 	}
755*0Sstevel@tonic-gate 
756*0Sstevel@tonic-gate 	/* XXX more conditions to check here? */
757*0Sstevel@tonic-gate 
758*0Sstevel@tonic-gate 	/* XXX make sure thresh values are between min and max values */
759*0Sstevel@tonic-gate 
760*0Sstevel@tonic-gate 	/* return status */
761*0Sstevel@tonic-gate 	return (ret);
762*0Sstevel@tonic-gate }
763*0Sstevel@tonic-gate 
764*0Sstevel@tonic-gate /*
765*0Sstevel@tonic-gate  *
766*0Sstevel@tonic-gate  *			cache_stats
767*0Sstevel@tonic-gate  *
768*0Sstevel@tonic-gate  * Description:
769*0Sstevel@tonic-gate  *	Show each cache in the directory, cache resource statistics,
770*0Sstevel@tonic-gate  *	and, for each fs in the cache, the name of the fs, and the
771*0Sstevel@tonic-gate  *	cache resource parameters.
772*0Sstevel@tonic-gate  * Arguments:
773*0Sstevel@tonic-gate  *	dirp	name of the cache directory
774*0Sstevel@tonic-gate  * Returns:
775*0Sstevel@tonic-gate  *	Returns 0 for success, -1 for an error.
776*0Sstevel@tonic-gate  * Errors:
777*0Sstevel@tonic-gate  * Preconditions:
778*0Sstevel@tonic-gate  */
779*0Sstevel@tonic-gate 
780*0Sstevel@tonic-gate int
cache_stats(char * dirp)781*0Sstevel@tonic-gate cache_stats(char *dirp)
782*0Sstevel@tonic-gate {
783*0Sstevel@tonic-gate 	DIR *dp;
784*0Sstevel@tonic-gate 	struct dirent64 *dep;
785*0Sstevel@tonic-gate 	char path[CACHEFS_XMAXPATH];
786*0Sstevel@tonic-gate 	struct stat64 statinfo;
787*0Sstevel@tonic-gate 	int ret;
788*0Sstevel@tonic-gate 	int xx;
789*0Sstevel@tonic-gate 	struct cache_label clabel;
790*0Sstevel@tonic-gate 	struct cachefs_user_values uv;
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate 	/* make sure cache dir name is not too long */
793*0Sstevel@tonic-gate 	if (strlen(dirp) > (size_t)PATH_MAX) {
794*0Sstevel@tonic-gate 		pr_err(gettext("path name %s is too long."), dirp);
795*0Sstevel@tonic-gate 		return (-1);
796*0Sstevel@tonic-gate 	}
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate 	/* read the cache label file */
799*0Sstevel@tonic-gate 	sprintf(path, "%s/%s", dirp, CACHELABEL_NAME);
800*0Sstevel@tonic-gate 	xx = cachefs_label_file_get(path, &clabel);
801*0Sstevel@tonic-gate 	if (xx == -1) {
802*0Sstevel@tonic-gate 		pr_err(gettext("Reading %s failed."), path);
803*0Sstevel@tonic-gate 		return (-1);
804*0Sstevel@tonic-gate 	}
805*0Sstevel@tonic-gate 	xx = cachefs_label_file_vcheck(path, &clabel);
806*0Sstevel@tonic-gate 	if (xx != 0) {
807*0Sstevel@tonic-gate 		pr_err(gettext("Version mismatch on %s."), path);
808*0Sstevel@tonic-gate 		return (-1);
809*0Sstevel@tonic-gate 	}
810*0Sstevel@tonic-gate 
811*0Sstevel@tonic-gate 	/* convert the cache_label to user values */
812*0Sstevel@tonic-gate 	xx = cachefs_convert_cl2uv(&clabel, &uv, dirp);
813*0Sstevel@tonic-gate 	if (xx)
814*0Sstevel@tonic-gate 		return (-1);
815*0Sstevel@tonic-gate 
816*0Sstevel@tonic-gate 	/* display the parameters */
817*0Sstevel@tonic-gate 	printf(gettext("cfsadmin: list cache FS information\n"));
818*0Sstevel@tonic-gate #if 0
819*0Sstevel@tonic-gate 	printf(gettext("   Version      %3d\n"), clabel.cl_cfsversion);
820*0Sstevel@tonic-gate #endif
821*0Sstevel@tonic-gate 	printf(gettext("   maxblocks    %3d%%\n"), uv.uv_maxblocks);
822*0Sstevel@tonic-gate 	printf(gettext("   minblocks    %3d%%\n"), uv.uv_minblocks);
823*0Sstevel@tonic-gate 	printf(gettext("   threshblocks %3d%%\n"), uv.uv_threshblocks);
824*0Sstevel@tonic-gate 	printf(gettext("   maxfiles     %3d%%\n"), uv.uv_maxfiles);
825*0Sstevel@tonic-gate 	printf(gettext("   minfiles     %3d%%\n"), uv.uv_minfiles);
826*0Sstevel@tonic-gate 	printf(gettext("   threshfiles  %3d%%\n"), uv.uv_threshfiles);
827*0Sstevel@tonic-gate 	printf(gettext("   maxfilesize  %3dMB\n"), uv.uv_maxfilesize);
828*0Sstevel@tonic-gate 
829*0Sstevel@tonic-gate 	/* open the directory */
830*0Sstevel@tonic-gate 	if ((dp = opendir(dirp)) == NULL) {
831*0Sstevel@tonic-gate 		pr_err(gettext("opendir %s failed: %s"), dirp,
832*0Sstevel@tonic-gate 		    strerror(errno));
833*0Sstevel@tonic-gate 		return (-1);
834*0Sstevel@tonic-gate 	}
835*0Sstevel@tonic-gate 
836*0Sstevel@tonic-gate 	/* loop reading the contents of the directory */
837*0Sstevel@tonic-gate 	ret = 0;
838*0Sstevel@tonic-gate 	while ((dep = readdir64(dp)) != NULL) {
839*0Sstevel@tonic-gate 		/* ignore . and .. */
840*0Sstevel@tonic-gate 		if ((strcmp(dep->d_name, ".") == 0) ||
841*0Sstevel@tonic-gate 		    (strcmp(dep->d_name, "..") == 0))
842*0Sstevel@tonic-gate 			continue;
843*0Sstevel@tonic-gate 
844*0Sstevel@tonic-gate 		/* stat the file */
845*0Sstevel@tonic-gate 		sprintf(path, "%s/%s", dirp, dep->d_name);
846*0Sstevel@tonic-gate 		xx = lstat64(path, &statinfo);
847*0Sstevel@tonic-gate 		if (xx == -1) {
848*0Sstevel@tonic-gate 			pr_err(gettext("lstat %s failed: %s"),
849*0Sstevel@tonic-gate 			    path, strerror(errno));
850*0Sstevel@tonic-gate 			closedir(dp);
851*0Sstevel@tonic-gate 			return (-1);
852*0Sstevel@tonic-gate 		}
853*0Sstevel@tonic-gate 
854*0Sstevel@tonic-gate 		/* ignore anything that is not a link */
855*0Sstevel@tonic-gate 		if (!S_ISLNK(statinfo.st_mode))
856*0Sstevel@tonic-gate 			continue;
857*0Sstevel@tonic-gate 
858*0Sstevel@tonic-gate 		/* print the file system cache directory name */
859*0Sstevel@tonic-gate 		printf(gettext("  %s\n"), dep->d_name);
860*0Sstevel@tonic-gate 
861*0Sstevel@tonic-gate 		/* XXX anything else */
862*0Sstevel@tonic-gate 	}
863*0Sstevel@tonic-gate 
864*0Sstevel@tonic-gate 	/* XXX what about stats */
865*0Sstevel@tonic-gate 
866*0Sstevel@tonic-gate 	/* return status */
867*0Sstevel@tonic-gate 	return (ret);
868*0Sstevel@tonic-gate }
869*0Sstevel@tonic-gate 
870*0Sstevel@tonic-gate /*
871*0Sstevel@tonic-gate  *
872*0Sstevel@tonic-gate  *			resource_file_grow
873*0Sstevel@tonic-gate  *
874*0Sstevel@tonic-gate  * Description:
875*0Sstevel@tonic-gate  *	Grows the resource file in the specified directory
876*0Sstevel@tonic-gate  *	to its new size.
877*0Sstevel@tonic-gate  * Arguments:
878*0Sstevel@tonic-gate  *	dirp	cache directory resource file is in
879*0Sstevel@tonic-gate  *	oldcnt	previous number of files in resource file
880*0Sstevel@tonic-gate  *	newcnt	new number of files in resource file
881*0Sstevel@tonic-gate  * Returns:
882*0Sstevel@tonic-gate  *	Returns 0 for success, -1 for an error.
883*0Sstevel@tonic-gate  * Preconditions:
884*0Sstevel@tonic-gate  *	precond(dirp)
885*0Sstevel@tonic-gate  *	precond(oldcnt <= newcnt)
886*0Sstevel@tonic-gate  *	precond(cache is locked exclusively)
887*0Sstevel@tonic-gate  *	precond(cache is not in use)
888*0Sstevel@tonic-gate  */
889*0Sstevel@tonic-gate 
890*0Sstevel@tonic-gate int
resource_file_grow(char * dirp,int oldcnt,int newcnt)891*0Sstevel@tonic-gate resource_file_grow(char *dirp, int oldcnt, int newcnt)
892*0Sstevel@tonic-gate {
893*0Sstevel@tonic-gate 	int fd;
894*0Sstevel@tonic-gate 	char path[CACHEFS_XMAXPATH];
895*0Sstevel@tonic-gate 	int xx;
896*0Sstevel@tonic-gate 	struct stat64 st;
897*0Sstevel@tonic-gate 	static struct cachefs_rinfo rold, rnew;
898*0Sstevel@tonic-gate 	struct cache_usage cusage, *cusagep;
899*0Sstevel@tonic-gate 	char buf[MAXBSIZE];
900*0Sstevel@tonic-gate 	int cnt;
901*0Sstevel@tonic-gate 	caddr_t addrp;
902*0Sstevel@tonic-gate 	int dirty;
903*0Sstevel@tonic-gate 
904*0Sstevel@tonic-gate 	/* get info about the resouce file for the various sizes */
905*0Sstevel@tonic-gate 	cachefs_resource_size(oldcnt, &rold);
906*0Sstevel@tonic-gate 	cachefs_resource_size(newcnt, &rnew);
907*0Sstevel@tonic-gate 
908*0Sstevel@tonic-gate 	/* open the resource file for writing */
909*0Sstevel@tonic-gate 	/* this file is < 2GB */
910*0Sstevel@tonic-gate 	sprintf(path, "%s/%s", dirp, RESOURCE_NAME);
911*0Sstevel@tonic-gate 	fd = open(path, O_RDWR);
912*0Sstevel@tonic-gate 	if (fd == -1) {
913*0Sstevel@tonic-gate 		pr_err(gettext("Could not open %s: %s, run fsck"), path,
914*0Sstevel@tonic-gate 		    strerror(errno));
915*0Sstevel@tonic-gate 		return (-1);
916*0Sstevel@tonic-gate 	}
917*0Sstevel@tonic-gate 
918*0Sstevel@tonic-gate 	/* get info on the file */
919*0Sstevel@tonic-gate 	xx = fstat64(fd, &st);
920*0Sstevel@tonic-gate 	if (xx == -1) {
921*0Sstevel@tonic-gate 		pr_err(gettext("Could not stat %s: %s"), path,
922*0Sstevel@tonic-gate 		    strerror(errno));
923*0Sstevel@tonic-gate 		close(fd);
924*0Sstevel@tonic-gate 		return (-1);
925*0Sstevel@tonic-gate 	}
926*0Sstevel@tonic-gate 
927*0Sstevel@tonic-gate 	/* make sure the size is the correct */
928*0Sstevel@tonic-gate 	if ((off_t)st.st_size != rold.r_fsize) {
929*0Sstevel@tonic-gate 		pr_err(gettext("Resource file has wrong size %d %d, run fsck"),
930*0Sstevel@tonic-gate 			(off_t)st.st_size, rold.r_fsize);
931*0Sstevel@tonic-gate 		close(fd);
932*0Sstevel@tonic-gate 		return (-1);
933*0Sstevel@tonic-gate 	}
934*0Sstevel@tonic-gate 
935*0Sstevel@tonic-gate 	/* read the cache usage structure */
936*0Sstevel@tonic-gate 	xx = read(fd, &cusage, sizeof (cusage));
937*0Sstevel@tonic-gate 	if (xx != sizeof (cusage)) {
938*0Sstevel@tonic-gate 		pr_err(gettext("Could not read cache_usage, %d, run fsck"),
939*0Sstevel@tonic-gate 			xx);
940*0Sstevel@tonic-gate 		close(fd);
941*0Sstevel@tonic-gate 		return (-1);
942*0Sstevel@tonic-gate 	}
943*0Sstevel@tonic-gate 
944*0Sstevel@tonic-gate 	/* rewind */
945*0Sstevel@tonic-gate 	xx = lseek(fd, 0, SEEK_SET);
946*0Sstevel@tonic-gate 	if (xx == -1) {
947*0Sstevel@tonic-gate 		pr_err(gettext("Could not lseek %s: %s"), path,
948*0Sstevel@tonic-gate 			strerror(errno));
949*0Sstevel@tonic-gate 		close(fd);
950*0Sstevel@tonic-gate 		return (-1);
951*0Sstevel@tonic-gate 	}
952*0Sstevel@tonic-gate 
953*0Sstevel@tonic-gate 	/* indicate cache is dirty if necessary */
954*0Sstevel@tonic-gate 	dirty = 1;
955*0Sstevel@tonic-gate 	if ((cusage.cu_flags & CUSAGE_ACTIVE) == 0) {
956*0Sstevel@tonic-gate 		dirty = 0;
957*0Sstevel@tonic-gate 		cusage.cu_flags |= CUSAGE_ACTIVE;
958*0Sstevel@tonic-gate 		xx = write(fd, &cusage, sizeof (cusage));
959*0Sstevel@tonic-gate 		if (xx != sizeof (cusage)) {
960*0Sstevel@tonic-gate 			pr_err(gettext(
961*0Sstevel@tonic-gate 				"Could not write cache_usage, %d, run fsck"),
962*0Sstevel@tonic-gate 				xx);
963*0Sstevel@tonic-gate 			close(fd);
964*0Sstevel@tonic-gate 			return (-1);
965*0Sstevel@tonic-gate 		}
966*0Sstevel@tonic-gate 	}
967*0Sstevel@tonic-gate 
968*0Sstevel@tonic-gate 	/* go to the end of the file */
969*0Sstevel@tonic-gate 	xx = lseek(fd, 0, SEEK_END);
970*0Sstevel@tonic-gate 	if (xx == -1) {
971*0Sstevel@tonic-gate 		pr_err(gettext("Could not lseek %s: %s"), path,
972*0Sstevel@tonic-gate 			strerror(errno));
973*0Sstevel@tonic-gate 		close(fd);
974*0Sstevel@tonic-gate 		return (-1);
975*0Sstevel@tonic-gate 	}
976*0Sstevel@tonic-gate 
977*0Sstevel@tonic-gate 	/* grow the file to the new size */
978*0Sstevel@tonic-gate 	memset(buf, 0, sizeof (buf));
979*0Sstevel@tonic-gate 	cnt = rnew.r_fsize - rold.r_fsize;
980*0Sstevel@tonic-gate 	assert((cnt % MAXBSIZE) == 0);
981*0Sstevel@tonic-gate 	cnt /= MAXBSIZE;
982*0Sstevel@tonic-gate 	while (cnt-- > 0) {
983*0Sstevel@tonic-gate 		xx = write(fd, buf, sizeof (buf));
984*0Sstevel@tonic-gate 		if (xx != sizeof (buf)) {
985*0Sstevel@tonic-gate 			pr_err(gettext("Could not write file, %d, run fsck"),
986*0Sstevel@tonic-gate 				xx);
987*0Sstevel@tonic-gate 			close(fd);
988*0Sstevel@tonic-gate 			return (-1);
989*0Sstevel@tonic-gate 		}
990*0Sstevel@tonic-gate 	}
991*0Sstevel@tonic-gate 
992*0Sstevel@tonic-gate 	/* mmap the file into our address space */
993*0Sstevel@tonic-gate 	addrp = mmap(NULL, rnew.r_fsize, PROT_READ | PROT_WRITE, MAP_SHARED,
994*0Sstevel@tonic-gate 		fd, 0);
995*0Sstevel@tonic-gate 	if (addrp == (void *)-1) {
996*0Sstevel@tonic-gate 		pr_err(gettext("Could not mmap file %s: %s"), path,
997*0Sstevel@tonic-gate 			strerror(errno));
998*0Sstevel@tonic-gate 		close(fd);
999*0Sstevel@tonic-gate 		return (-1);
1000*0Sstevel@tonic-gate 	}
1001*0Sstevel@tonic-gate 
1002*0Sstevel@tonic-gate 	/* close the file descriptor, we do not need it anymore */
1003*0Sstevel@tonic-gate 	close(fd);
1004*0Sstevel@tonic-gate 
1005*0Sstevel@tonic-gate 	/* move the idents region to its new location */
1006*0Sstevel@tonic-gate 	memmove(addrp + rnew.r_identoffset, addrp + rold.r_identoffset,
1007*0Sstevel@tonic-gate 		rold.r_identsize);
1008*0Sstevel@tonic-gate 
1009*0Sstevel@tonic-gate 	/* zero out the old idents region that is now in the pointers region */
1010*0Sstevel@tonic-gate 	memset(addrp + rold.r_identoffset, 0,
1011*0Sstevel@tonic-gate 		rnew.r_identoffset - rold.r_identoffset);
1012*0Sstevel@tonic-gate 
1013*0Sstevel@tonic-gate 	/* sync the data to the file */
1014*0Sstevel@tonic-gate 	xx = msync(addrp, rnew.r_fsize, MS_SYNC);
1015*0Sstevel@tonic-gate 	if (xx == -1) {
1016*0Sstevel@tonic-gate 		pr_err(gettext("Could not sync file %s: %s"), path,
1017*0Sstevel@tonic-gate 			strerror(errno));
1018*0Sstevel@tonic-gate 		munmap(addrp, rnew.r_fsize);
1019*0Sstevel@tonic-gate 		return (-1);
1020*0Sstevel@tonic-gate 	}
1021*0Sstevel@tonic-gate 
1022*0Sstevel@tonic-gate 	/* mark the file as clean if it was not dirty originally */
1023*0Sstevel@tonic-gate 	if (!dirty) {
1024*0Sstevel@tonic-gate 		cusagep = (struct cache_usage *)addrp;
1025*0Sstevel@tonic-gate 		cusagep->cu_flags &= ~CUSAGE_ACTIVE;
1026*0Sstevel@tonic-gate 
1027*0Sstevel@tonic-gate 		/* sync the data to the file */
1028*0Sstevel@tonic-gate 		xx = msync(addrp, rnew.r_fsize, MS_SYNC);
1029*0Sstevel@tonic-gate 		if (xx == -1) {
1030*0Sstevel@tonic-gate 			pr_err(gettext("Could not sync file %s: %s"), path,
1031*0Sstevel@tonic-gate 				strerror(errno));
1032*0Sstevel@tonic-gate 			munmap(addrp, rnew.r_fsize);
1033*0Sstevel@tonic-gate 			return (-1);
1034*0Sstevel@tonic-gate 		}
1035*0Sstevel@tonic-gate 	}
1036*0Sstevel@tonic-gate 
1037*0Sstevel@tonic-gate 	/* unmap the file */
1038*0Sstevel@tonic-gate 	munmap(addrp, rnew.r_fsize);
1039*0Sstevel@tonic-gate 
1040*0Sstevel@tonic-gate 	/* return success */
1041*0Sstevel@tonic-gate 	return (0);
1042*0Sstevel@tonic-gate }
1043*0Sstevel@tonic-gate 
1044*0Sstevel@tonic-gate /*
1045*0Sstevel@tonic-gate  *
1046*0Sstevel@tonic-gate  *			resource_file_dirty
1047*0Sstevel@tonic-gate  *
1048*0Sstevel@tonic-gate  * Description:
1049*0Sstevel@tonic-gate  *	Marks the resource file as dirty.
1050*0Sstevel@tonic-gate  *	This will cause fsck to fix it up the next time it
1051*0Sstevel@tonic-gate  *	is run.
1052*0Sstevel@tonic-gate  * Arguments:
1053*0Sstevel@tonic-gate  *	dirp	cache directory resource file is in
1054*0Sstevel@tonic-gate  * Returns:
1055*0Sstevel@tonic-gate  *	Returns 0 for success, -1 for an error.
1056*0Sstevel@tonic-gate  * Preconditions:
1057*0Sstevel@tonic-gate  *	precond(dirp)
1058*0Sstevel@tonic-gate  *	precond(cache is locked exclusively)
1059*0Sstevel@tonic-gate  *	precond(cache is not in use)
1060*0Sstevel@tonic-gate  */
1061*0Sstevel@tonic-gate 
1062*0Sstevel@tonic-gate int
resource_file_dirty(char * dirp)1063*0Sstevel@tonic-gate resource_file_dirty(char *dirp)
1064*0Sstevel@tonic-gate {
1065*0Sstevel@tonic-gate 	int fd;
1066*0Sstevel@tonic-gate 	char path[CACHEFS_XMAXPATH];
1067*0Sstevel@tonic-gate 	int xx;
1068*0Sstevel@tonic-gate 	struct cache_usage cusage;
1069*0Sstevel@tonic-gate 
1070*0Sstevel@tonic-gate 	/* open the resource file for writing */
1071*0Sstevel@tonic-gate 	/* this file is < 2GB */
1072*0Sstevel@tonic-gate 	sprintf(path, "%s/%s", dirp, RESOURCE_NAME);
1073*0Sstevel@tonic-gate 	fd = open(path, O_RDWR);
1074*0Sstevel@tonic-gate 	if (fd == -1) {
1075*0Sstevel@tonic-gate 		pr_err(gettext("Could not open %s: %s, run fsck"), path,
1076*0Sstevel@tonic-gate 		    strerror(errno));
1077*0Sstevel@tonic-gate 		return (-1);
1078*0Sstevel@tonic-gate 	}
1079*0Sstevel@tonic-gate 
1080*0Sstevel@tonic-gate 	/* read the cache usage structure */
1081*0Sstevel@tonic-gate 	xx = read(fd, &cusage, sizeof (cusage));
1082*0Sstevel@tonic-gate 	if (xx != sizeof (cusage)) {
1083*0Sstevel@tonic-gate 		pr_err(gettext("Could not read cache_usage, %d, run fsck"),
1084*0Sstevel@tonic-gate 			xx);
1085*0Sstevel@tonic-gate 		close(fd);
1086*0Sstevel@tonic-gate 		return (-1);
1087*0Sstevel@tonic-gate 	}
1088*0Sstevel@tonic-gate 
1089*0Sstevel@tonic-gate 	/* rewind */
1090*0Sstevel@tonic-gate 	xx = lseek(fd, 0, SEEK_SET);
1091*0Sstevel@tonic-gate 	if (xx == -1) {
1092*0Sstevel@tonic-gate 		pr_err(gettext("Could not lseek %s: %s"), path,
1093*0Sstevel@tonic-gate 			strerror(errno));
1094*0Sstevel@tonic-gate 		close(fd);
1095*0Sstevel@tonic-gate 		return (-1);
1096*0Sstevel@tonic-gate 	}
1097*0Sstevel@tonic-gate 
1098*0Sstevel@tonic-gate 	/* indicate cache is dirty if necessary */
1099*0Sstevel@tonic-gate 	if ((cusage.cu_flags & CUSAGE_ACTIVE) == 0) {
1100*0Sstevel@tonic-gate 		cusage.cu_flags |= CUSAGE_ACTIVE;
1101*0Sstevel@tonic-gate 		xx = write(fd, &cusage, sizeof (cusage));
1102*0Sstevel@tonic-gate 		if (xx != sizeof (cusage)) {
1103*0Sstevel@tonic-gate 			pr_err(gettext(
1104*0Sstevel@tonic-gate 				"Could not write cache_usage, %d, run fsck"),
1105*0Sstevel@tonic-gate 				xx);
1106*0Sstevel@tonic-gate 			close(fd);
1107*0Sstevel@tonic-gate 			return (-1);
1108*0Sstevel@tonic-gate 		}
1109*0Sstevel@tonic-gate 	}
1110*0Sstevel@tonic-gate 
1111*0Sstevel@tonic-gate 	xx = close(fd);
1112*0Sstevel@tonic-gate 	if (xx == -1) {
1113*0Sstevel@tonic-gate 		pr_err(gettext("Could not successfully close %s: %s"), path,
1114*0Sstevel@tonic-gate 			strerror(errno));
1115*0Sstevel@tonic-gate 	}
1116*0Sstevel@tonic-gate 	return (xx);
1117*0Sstevel@tonic-gate }
1118*0Sstevel@tonic-gate 
1119*0Sstevel@tonic-gate /*
1120*0Sstevel@tonic-gate  *
1121*0Sstevel@tonic-gate  *			issue_cod
1122*0Sstevel@tonic-gate  *
1123*0Sstevel@tonic-gate  * Description:
1124*0Sstevel@tonic-gate  *	Executes the _FIOCOD ioctl on the specified file.
1125*0Sstevel@tonic-gate  * Arguments:
1126*0Sstevel@tonic-gate  *	name	filename to issue ioctl on (or "all")
1127*0Sstevel@tonic-gate  * Returns:
1128*0Sstevel@tonic-gate  *	Returns 0 for success, -1 for an error.
1129*0Sstevel@tonic-gate  * Preconditions:
1130*0Sstevel@tonic-gate  *	precond(dirp)
1131*0Sstevel@tonic-gate  */
1132*0Sstevel@tonic-gate 
1133*0Sstevel@tonic-gate int
issue_cod(char * name)1134*0Sstevel@tonic-gate issue_cod(char *name)
1135*0Sstevel@tonic-gate {
1136*0Sstevel@tonic-gate 	int fd;
1137*0Sstevel@tonic-gate 	int xx;
1138*0Sstevel@tonic-gate 	int arg;
1139*0Sstevel@tonic-gate 	char *dirp;
1140*0Sstevel@tonic-gate 	FILE *mfp;
1141*0Sstevel@tonic-gate 	struct mnttab mt, mtpref;
1142*0Sstevel@tonic-gate 
1143*0Sstevel@tonic-gate #ifndef MNTTYPE_CACHEFS
1144*0Sstevel@tonic-gate #define	MNTTYPE_CACHEFS	"cachefs"
1145*0Sstevel@tonic-gate #endif
1146*0Sstevel@tonic-gate 
1147*0Sstevel@tonic-gate 	arg = 0;
1148*0Sstevel@tonic-gate 	if (strcmp(name, "all") == 0) {
1149*0Sstevel@tonic-gate 		/*
1150*0Sstevel@tonic-gate 		 * if "all" was specified rather than a mount point,
1151*0Sstevel@tonic-gate 		 * we locate a cachefs mount in /etc/mnttab (any cachefs
1152*0Sstevel@tonic-gate 		 * mount will do).  We issue the ioctl on this mount point,
1153*0Sstevel@tonic-gate 		 * and specify a non-zero argument to the ioctl.  The non-zero
1154*0Sstevel@tonic-gate 		 * arg tells the kernel to do demandconst on all relevant
1155*0Sstevel@tonic-gate 		 * cachefs mounts
1156*0Sstevel@tonic-gate 		 */
1157*0Sstevel@tonic-gate 		if ((mfp = fopen(MNTTAB, "r")) == NULL) {
1158*0Sstevel@tonic-gate 			pr_err(gettext("Could not open %s."), MNTTAB);
1159*0Sstevel@tonic-gate 			return (-1);
1160*0Sstevel@tonic-gate 		}
1161*0Sstevel@tonic-gate 		mtpref.mnt_special = NULL;
1162*0Sstevel@tonic-gate 		mtpref.mnt_mountp = NULL;
1163*0Sstevel@tonic-gate 		mtpref.mnt_mntopts = NULL;
1164*0Sstevel@tonic-gate 		mtpref.mnt_time = NULL;
1165*0Sstevel@tonic-gate 		mtpref.mnt_fstype = MNTTYPE_CACHEFS;
1166*0Sstevel@tonic-gate 		if (getmntany(mfp, &mt, &mtpref) != 0) {
1167*0Sstevel@tonic-gate 			(void) fclose(mfp);
1168*0Sstevel@tonic-gate 			return (-1);
1169*0Sstevel@tonic-gate 		}
1170*0Sstevel@tonic-gate 		(void) fclose(mfp);
1171*0Sstevel@tonic-gate 		dirp = mt.mnt_mountp;
1172*0Sstevel@tonic-gate 		arg = 1;
1173*0Sstevel@tonic-gate 	} else {
1174*0Sstevel@tonic-gate 		dirp = name;
1175*0Sstevel@tonic-gate 	}
1176*0Sstevel@tonic-gate 
1177*0Sstevel@tonic-gate 	/* open the file */
1178*0Sstevel@tonic-gate 	fd = open(dirp, O_RDONLY);
1179*0Sstevel@tonic-gate 	if (fd == -1) {
1180*0Sstevel@tonic-gate 		pr_err(gettext("Could not open %s, %s."),
1181*0Sstevel@tonic-gate 			dirp, strerror(errno));
1182*0Sstevel@tonic-gate 		return (-1);
1183*0Sstevel@tonic-gate 	}
1184*0Sstevel@tonic-gate 
1185*0Sstevel@tonic-gate 	/* issue the ioctl */
1186*0Sstevel@tonic-gate 	xx = ioctl(fd, _FIOCOD, arg);
1187*0Sstevel@tonic-gate 	if (xx) {
1188*0Sstevel@tonic-gate 		if (errno == ENOTTY) {
1189*0Sstevel@tonic-gate 			pr_err(gettext("%s is not a CacheFS file system"),
1190*0Sstevel@tonic-gate 				dirp);
1191*0Sstevel@tonic-gate 		} else if (errno == EBUSY) {
1192*0Sstevel@tonic-gate 			if (arg == 0)
1193*0Sstevel@tonic-gate 				/* we're quiet if "all" was specified */
1194*0Sstevel@tonic-gate 				pr_err(gettext("CacheFS file system %s is not"
1195*0Sstevel@tonic-gate 					" mounted demandconst."), dirp);
1196*0Sstevel@tonic-gate 		} else {
1197*0Sstevel@tonic-gate 			pr_err(gettext("Could not issue consistency request"
1198*0Sstevel@tonic-gate 				" on %s\n    %s."), dirp, strerror(errno));
1199*0Sstevel@tonic-gate 		}
1200*0Sstevel@tonic-gate 	}
1201*0Sstevel@tonic-gate 	close(fd);
1202*0Sstevel@tonic-gate 	return (xx);
1203*0Sstevel@tonic-gate }
1204*0Sstevel@tonic-gate 
1205*0Sstevel@tonic-gate /*
1206*0Sstevel@tonic-gate  *
1207*0Sstevel@tonic-gate  *			simulate_disconnection
1208*0Sstevel@tonic-gate  *
1209*0Sstevel@tonic-gate  * Description:
1210*0Sstevel@tonic-gate  *	Sends the rpc message to the cachefsd to turn simulated
1211*0Sstevel@tonic-gate  *	disconnection on or off
1212*0Sstevel@tonic-gate  * Arguments:
1213*0Sstevel@tonic-gate  *	namep		name of file system or "all"
1214*0Sstevel@tonic-gate  *	disconnect	1 means disconnect, 0 means connect
1215*0Sstevel@tonic-gate  * Returns:
1216*0Sstevel@tonic-gate  * Preconditions:
1217*0Sstevel@tonic-gate  *	precond(name)
1218*0Sstevel@tonic-gate  */
1219*0Sstevel@tonic-gate 
1220*0Sstevel@tonic-gate void
simulate_disconnection(char * namep,int disconnect)1221*0Sstevel@tonic-gate simulate_disconnection(char *namep, int disconnect)
1222*0Sstevel@tonic-gate {
1223*0Sstevel@tonic-gate 	CLIENT *clnt;
1224*0Sstevel@tonic-gate 	enum clnt_stat retval;
1225*0Sstevel@tonic-gate 	int ret;
1226*0Sstevel@tonic-gate 	int xx;
1227*0Sstevel@tonic-gate 	int result;
1228*0Sstevel@tonic-gate 	char *hostp;
1229*0Sstevel@tonic-gate 	struct utsname info;
1230*0Sstevel@tonic-gate 	struct cachefsd_disconnection_args args;
1231*0Sstevel@tonic-gate 	char *msgp;
1232*0Sstevel@tonic-gate 	struct timeval tval;
1233*0Sstevel@tonic-gate 
1234*0Sstevel@tonic-gate 	/* get the host name */
1235*0Sstevel@tonic-gate 	xx = uname(&info);
1236*0Sstevel@tonic-gate 	if (xx == -1) {
1237*0Sstevel@tonic-gate 		pr_err(gettext("cannot get host name, errno %d"), errno);
1238*0Sstevel@tonic-gate 		return;
1239*0Sstevel@tonic-gate 	}
1240*0Sstevel@tonic-gate 	hostp = info.nodename;
1241*0Sstevel@tonic-gate 
1242*0Sstevel@tonic-gate 	/* creat the connection to the daemon */
1243*0Sstevel@tonic-gate 	clnt = clnt_create(hostp, CACHEFSDPROG, CACHEFSDVERS, "local");
1244*0Sstevel@tonic-gate 	if (clnt == NULL) {
1245*0Sstevel@tonic-gate 		pr_err(gettext("cachefsd is not running"));
1246*0Sstevel@tonic-gate 		return;
1247*0Sstevel@tonic-gate 	}
1248*0Sstevel@tonic-gate 
1249*0Sstevel@tonic-gate 	/* give it a chance to complete */
1250*0Sstevel@tonic-gate 	tval.tv_sec = 60 * 60 * 24;
1251*0Sstevel@tonic-gate 	tval.tv_usec = 0;
1252*0Sstevel@tonic-gate 	clnt_control(clnt, CLSET_TIMEOUT, (char *)&tval);
1253*0Sstevel@tonic-gate 
1254*0Sstevel@tonic-gate 	/* perform the operation */
1255*0Sstevel@tonic-gate 	args.cda_mntpt = namep;
1256*0Sstevel@tonic-gate 	args.cda_disconnect = disconnect;
1257*0Sstevel@tonic-gate 	retval = cachefsd_disconnection_1(&args, &ret, clnt);
1258*0Sstevel@tonic-gate 	if (retval != RPC_SUCCESS) {
1259*0Sstevel@tonic-gate 		clnt_perror(clnt, gettext("cachefsd is not responding"));
1260*0Sstevel@tonic-gate 		clnt_destroy(clnt);
1261*0Sstevel@tonic-gate 		return;
1262*0Sstevel@tonic-gate 	}
1263*0Sstevel@tonic-gate 
1264*0Sstevel@tonic-gate 	/* check for error from daemon */
1265*0Sstevel@tonic-gate 	if (ret != 0) {
1266*0Sstevel@tonic-gate 		if (disconnect) {
1267*0Sstevel@tonic-gate 			switch (ret) {
1268*0Sstevel@tonic-gate 			default:
1269*0Sstevel@tonic-gate 				msgp = "unknown error";
1270*0Sstevel@tonic-gate 				break;
1271*0Sstevel@tonic-gate 			case 1:
1272*0Sstevel@tonic-gate 				msgp = "not mounted disconnectable";
1273*0Sstevel@tonic-gate 				break;
1274*0Sstevel@tonic-gate 			case 2:
1275*0Sstevel@tonic-gate 				msgp = "already disconnected";
1276*0Sstevel@tonic-gate 				break;
1277*0Sstevel@tonic-gate 			case 3:
1278*0Sstevel@tonic-gate 				msgp = "not a cached file system";
1279*0Sstevel@tonic-gate 				break;
1280*0Sstevel@tonic-gate 			}
1281*0Sstevel@tonic-gate 			pr_err(gettext("Could not disconnect %s: %s"),
1282*0Sstevel@tonic-gate 			    namep, msgp);
1283*0Sstevel@tonic-gate 		} else {
1284*0Sstevel@tonic-gate 			switch (ret) {
1285*0Sstevel@tonic-gate 			default:
1286*0Sstevel@tonic-gate 				msgp = "unknown error";
1287*0Sstevel@tonic-gate 				break;
1288*0Sstevel@tonic-gate 			case 1:
1289*0Sstevel@tonic-gate 				msgp = "already connected";
1290*0Sstevel@tonic-gate 				break;
1291*0Sstevel@tonic-gate 			case 2:
1292*0Sstevel@tonic-gate 				msgp = "not simulated disconnection";
1293*0Sstevel@tonic-gate 				break;
1294*0Sstevel@tonic-gate 			case 3:
1295*0Sstevel@tonic-gate 				msgp = "not a cached file system";
1296*0Sstevel@tonic-gate 				break;
1297*0Sstevel@tonic-gate 			}
1298*0Sstevel@tonic-gate 			pr_err(gettext("Could not reconnect %s: %s"),
1299*0Sstevel@tonic-gate 			    namep, msgp);
1300*0Sstevel@tonic-gate 		}
1301*0Sstevel@tonic-gate 	}
1302*0Sstevel@tonic-gate 
1303*0Sstevel@tonic-gate 	ret = 0;
1304*0Sstevel@tonic-gate 
1305*0Sstevel@tonic-gate 	clnt_destroy(clnt);
1306*0Sstevel@tonic-gate }
1307