xref: /titanic_41/usr/src/cmd/fs.d/cachefs/common/subr.c (revision 47644099886aa8d3f43120b9eede3044342be473)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*47644099Sgt29601  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Common subroutines used by the programs in these subdirectories.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <locale.h>
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <assert.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <limits.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <wait.h>
427c478bd9Sstevel@tonic-gate #include <ctype.h>
437c478bd9Sstevel@tonic-gate #include <fcntl.h>
447c478bd9Sstevel@tonic-gate #include <ftw.h>
457c478bd9Sstevel@tonic-gate #include <dirent.h>
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <sys/time.h>
487c478bd9Sstevel@tonic-gate #include <utmpx.h>
497c478bd9Sstevel@tonic-gate #include <sys/uio.h>
507c478bd9Sstevel@tonic-gate #include <sys/param.h>
517c478bd9Sstevel@tonic-gate #include <sys/stat.h>
527c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
537c478bd9Sstevel@tonic-gate #include <sys/mount.h>
547c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
557c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
567c478bd9Sstevel@tonic-gate #include <sys/mman.h>
577c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_fs.h>
587c478bd9Sstevel@tonic-gate #include "subr.h"
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  *			cachefs_dir_lock
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  * Description:
657c478bd9Sstevel@tonic-gate  *	Gets a lock on the cache directory.
667c478bd9Sstevel@tonic-gate  *	To release the lock, call cachefs_dir_unlock
677c478bd9Sstevel@tonic-gate  *	with the returned value.
687c478bd9Sstevel@tonic-gate  * Arguments:
697c478bd9Sstevel@tonic-gate  *	cachedirp	name of the cache directory
707c478bd9Sstevel@tonic-gate  *	shared		1 if shared, 0 if not
717c478bd9Sstevel@tonic-gate  * Returns:
727c478bd9Sstevel@tonic-gate  *	Returns -1 if the lock cannot be obtained immediatly.
737c478bd9Sstevel@tonic-gate  *	If the lock is obtained, returns a value >= 0.
747c478bd9Sstevel@tonic-gate  * Preconditions:
757c478bd9Sstevel@tonic-gate  *	precond(cachedirp)
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate int
cachefs_dir_lock(const char * cachedirp,int shared)797c478bd9Sstevel@tonic-gate cachefs_dir_lock(const char *cachedirp, int shared)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	int fd;
827c478bd9Sstevel@tonic-gate 	int xx;
837c478bd9Sstevel@tonic-gate 	int len;
847c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
857c478bd9Sstevel@tonic-gate 	struct flock fl;
867c478bd9Sstevel@tonic-gate 	char *strp;
877c478bd9Sstevel@tonic-gate 	struct stat statb;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	/* make a path prefix to the cache directory lock file */
907c478bd9Sstevel@tonic-gate 	strp = CACHEFS_ROOTRUN;
917c478bd9Sstevel@tonic-gate 	xx = stat(strp, &statb);
927c478bd9Sstevel@tonic-gate 	if ((xx < 0) || ((statb.st_mode & S_IFMT) != S_IFDIR))
937c478bd9Sstevel@tonic-gate 		strp = "/tmp";
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	/* won't overflow */
967c478bd9Sstevel@tonic-gate 	len = snprintf(buf, sizeof (buf), "%s/%s", strp, CACHEFS_LOCKDIR_PRE);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	if (strlcat(buf, cachedirp, sizeof (buf)) >= sizeof (buf)) {
997c478bd9Sstevel@tonic-gate 		pr_err(gettext("Cache directory name %s is too long"),
1007c478bd9Sstevel@tonic-gate 			cachedirp);
1017c478bd9Sstevel@tonic-gate 		return (-1);
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	strp = &buf[len];
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	while (strp = strchr(strp, '/')) { 	/* convert path to a file */
1077c478bd9Sstevel@tonic-gate 		*strp = '_';
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/*
1117c478bd9Sstevel@tonic-gate 	 * Create and open the cache directory lock file.
1127c478bd9Sstevel@tonic-gate 	 * This file will be <2G.
1137c478bd9Sstevel@tonic-gate 	 */
1147c478bd9Sstevel@tonic-gate 	fd = open(buf, O_RDWR | O_CREAT, 0700);
1157c478bd9Sstevel@tonic-gate 	if (fd == -1) {
1167c478bd9Sstevel@tonic-gate 		pr_err(gettext("Cannot open lock file %s"), buf);
1177c478bd9Sstevel@tonic-gate 		return (-1);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/* try to set the lock */
1217c478bd9Sstevel@tonic-gate 	fl.l_type = (shared == 1) ? F_RDLCK : F_WRLCK;
1227c478bd9Sstevel@tonic-gate 	fl.l_whence = 0;
1237c478bd9Sstevel@tonic-gate 	fl.l_start = 1024;
1247c478bd9Sstevel@tonic-gate 	fl.l_len = 1024;
1257c478bd9Sstevel@tonic-gate 	fl.l_sysid = 0;
1267c478bd9Sstevel@tonic-gate 	fl.l_pid = 0;
1277c478bd9Sstevel@tonic-gate 	/* CACHEFS_LOCK_FILE will be <2GB */
1287c478bd9Sstevel@tonic-gate 	xx = fcntl(fd, F_SETLKW, &fl);
1297c478bd9Sstevel@tonic-gate 	if (xx == -1) {
1307c478bd9Sstevel@tonic-gate 		if (errno == EAGAIN) {
1317c478bd9Sstevel@tonic-gate 			pr_err(gettext("Cannot gain access to the "
1327c478bd9Sstevel@tonic-gate 			    "cache directory %s."), cachedirp);
1337c478bd9Sstevel@tonic-gate 		} else {
1347c478bd9Sstevel@tonic-gate 			pr_err(gettext("Unexpected failure on lock file %s %s"),
1357c478bd9Sstevel@tonic-gate 			    buf, strerror(errno));
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 		close(fd);
1387c478bd9Sstevel@tonic-gate 		return (-1);
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	/* return the file descriptor which can be used to release the lock */
1427c478bd9Sstevel@tonic-gate 	return (fd);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  *
1477c478bd9Sstevel@tonic-gate  *			cachefs_dir_unlock
1487c478bd9Sstevel@tonic-gate  *
1497c478bd9Sstevel@tonic-gate  * Description:
1507c478bd9Sstevel@tonic-gate  *	Releases an advisory lock on the cache directory.
1517c478bd9Sstevel@tonic-gate  * Arguments:
1527c478bd9Sstevel@tonic-gate  *	fd	cookie returned by cachefs_dir_lock
1537c478bd9Sstevel@tonic-gate  * Returns:
1547c478bd9Sstevel@tonic-gate  *	Returns -1 if the lock cannot be released or 0 for success.
1557c478bd9Sstevel@tonic-gate  * Preconditions:
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate int
cachefs_dir_unlock(int fd)1597c478bd9Sstevel@tonic-gate cachefs_dir_unlock(int fd)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	struct flock fl;
1627c478bd9Sstevel@tonic-gate 	int error = 0;
1637c478bd9Sstevel@tonic-gate 	int xx;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/* release the lock */
1667c478bd9Sstevel@tonic-gate 	fl.l_type = F_UNLCK;
1677c478bd9Sstevel@tonic-gate 	fl.l_whence = 0;
1687c478bd9Sstevel@tonic-gate 	fl.l_start = 1024;
1697c478bd9Sstevel@tonic-gate 	fl.l_len = 1024;
1707c478bd9Sstevel@tonic-gate 	fl.l_sysid = 0;
1717c478bd9Sstevel@tonic-gate 	fl.l_pid = 0;
1727c478bd9Sstevel@tonic-gate 	/* fd will be <2GB */
1737c478bd9Sstevel@tonic-gate 	xx = fcntl(fd, F_SETLK, &fl);
1747c478bd9Sstevel@tonic-gate 	if (xx == -1) {
1757c478bd9Sstevel@tonic-gate 		pr_err(gettext("Unexpected failure releasing lock file %s"),
1767c478bd9Sstevel@tonic-gate 			strerror(errno));
1777c478bd9Sstevel@tonic-gate 		error = -1;
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	/* close the lock file */
1817c478bd9Sstevel@tonic-gate 	close(fd);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	return (error);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate  *
1887c478bd9Sstevel@tonic-gate  *			cachefs_label_file_get
1897c478bd9Sstevel@tonic-gate  *
1907c478bd9Sstevel@tonic-gate  * Description:
1917c478bd9Sstevel@tonic-gate  *	Gets the contents of a cache label file.
1927c478bd9Sstevel@tonic-gate  *	Performs error checking on the file.
1937c478bd9Sstevel@tonic-gate  * Arguments:
1947c478bd9Sstevel@tonic-gate  *	filep	name of the cache label file
1957c478bd9Sstevel@tonic-gate  *	clabelp	where to put the file contents
1967c478bd9Sstevel@tonic-gate  * Returns:
1977c478bd9Sstevel@tonic-gate  *	Returns 0 for success or -1 if an error occurs.
1987c478bd9Sstevel@tonic-gate  * Preconditions:
1997c478bd9Sstevel@tonic-gate  *	precond(filep)
2007c478bd9Sstevel@tonic-gate  *	precond(clabelp)
2017c478bd9Sstevel@tonic-gate  */
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate int
cachefs_label_file_get(const char * filep,struct cache_label * clabelp)2047c478bd9Sstevel@tonic-gate cachefs_label_file_get(const char *filep, struct cache_label *clabelp)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	int xx;
2077c478bd9Sstevel@tonic-gate 	int fd;
2087c478bd9Sstevel@tonic-gate 	struct stat64 statinfo;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/* get info on the file */
2117c478bd9Sstevel@tonic-gate 	xx = lstat64(filep, &statinfo);
2127c478bd9Sstevel@tonic-gate 	if (xx == -1) {
2137c478bd9Sstevel@tonic-gate 		if (errno != ENOENT) {
2147c478bd9Sstevel@tonic-gate 			pr_err(gettext("Cannot stat file %s: %s"),
2157c478bd9Sstevel@tonic-gate 			    filep, strerror(errno));
2167c478bd9Sstevel@tonic-gate 		} else {
2177c478bd9Sstevel@tonic-gate 			pr_err(gettext("File %s does not exist."), filep);
2187c478bd9Sstevel@tonic-gate 		}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		return (-1);
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/* if the file is the wrong type */
2247c478bd9Sstevel@tonic-gate 	if (!S_ISREG(statinfo.st_mode)) {
2257c478bd9Sstevel@tonic-gate 		pr_err(gettext("Cache label file %s corrupted"), filep);
2267c478bd9Sstevel@tonic-gate 		return (-1);
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/* if the file is the wrong size; it will be <2GB */
2307c478bd9Sstevel@tonic-gate 	if (statinfo.st_size != (offset_t)sizeof (struct cache_label)) {
2317c478bd9Sstevel@tonic-gate 		pr_err(gettext("Cache label file %s wrong size"), filep);
2327c478bd9Sstevel@tonic-gate 		return (-1);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/* open the cache label file */
2367c478bd9Sstevel@tonic-gate 	fd = open(filep, O_RDONLY);
2377c478bd9Sstevel@tonic-gate 	if (fd == -1) {
2387c478bd9Sstevel@tonic-gate 		pr_err(gettext("Error opening %s: %s"), filep,
2397c478bd9Sstevel@tonic-gate 		    strerror(errno));
2407c478bd9Sstevel@tonic-gate 		return (-1);
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	/* read the current set of parameters */
2447c478bd9Sstevel@tonic-gate 	xx = read(fd, clabelp, sizeof (struct cache_label));
2457c478bd9Sstevel@tonic-gate 	if (xx != sizeof (struct cache_label)) {
2467c478bd9Sstevel@tonic-gate 		pr_err(gettext("Reading %s failed: %s\n"), filep,
2477c478bd9Sstevel@tonic-gate 		    strerror(errno));
2487c478bd9Sstevel@tonic-gate 		close(fd);
2497c478bd9Sstevel@tonic-gate 		return (-1);
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 	close(fd);
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	/* return success */
2547c478bd9Sstevel@tonic-gate 	return (0);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate  *
2597c478bd9Sstevel@tonic-gate  *			cachefs_label_file_put
2607c478bd9Sstevel@tonic-gate  *
2617c478bd9Sstevel@tonic-gate  * Description:
2627c478bd9Sstevel@tonic-gate  *	Outputs the contents of a cache label object to a file.
2637c478bd9Sstevel@tonic-gate  * Arguments:
2647c478bd9Sstevel@tonic-gate  *	filep	name of the cache label file
2657c478bd9Sstevel@tonic-gate  *	clabelp	where to get the file contents
2667c478bd9Sstevel@tonic-gate  * Returns:
2677c478bd9Sstevel@tonic-gate  *	Returns 0 for success or -1 if an error occurs.
2687c478bd9Sstevel@tonic-gate  * Preconditions:
2697c478bd9Sstevel@tonic-gate  *	precond(filep)
2707c478bd9Sstevel@tonic-gate  *	precond(clabelp)
2717c478bd9Sstevel@tonic-gate  */
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate int
cachefs_label_file_put(const char * filep,struct cache_label * clabelp)2747c478bd9Sstevel@tonic-gate cachefs_label_file_put(const char *filep, struct cache_label *clabelp)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	int xx;
2777c478bd9Sstevel@tonic-gate 	int fd;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	/* get rid of the file if it already exists */
2807c478bd9Sstevel@tonic-gate 	xx = unlink(filep);
2817c478bd9Sstevel@tonic-gate 	if ((xx == -1) && (errno != ENOENT)) {
2827c478bd9Sstevel@tonic-gate 		pr_err(gettext("Could not remove %s: %s"), filep,
2837c478bd9Sstevel@tonic-gate 		    strerror(errno));
2847c478bd9Sstevel@tonic-gate 		return (-1);
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/* open the cache label file; this file will be <2GB */
2887c478bd9Sstevel@tonic-gate 	fd = open(filep, O_CREAT | O_RDWR, 0600);
2897c478bd9Sstevel@tonic-gate 	if (fd == -1) {
2907c478bd9Sstevel@tonic-gate 		pr_err(gettext("Error creating %s: %s"), filep,
2917c478bd9Sstevel@tonic-gate 		    strerror(errno));
2927c478bd9Sstevel@tonic-gate 		return (-1);
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	/* write out the cache label object */
2967c478bd9Sstevel@tonic-gate 	xx = write(fd, clabelp, sizeof (struct cache_label));
2977c478bd9Sstevel@tonic-gate 	if (xx != sizeof (struct cache_label)) {
2987c478bd9Sstevel@tonic-gate 		pr_err(gettext("Writing %s failed: %s"), filep,
2997c478bd9Sstevel@tonic-gate 		    strerror(errno));
3007c478bd9Sstevel@tonic-gate 		close(fd);
3017c478bd9Sstevel@tonic-gate 		return (-1);
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/* make sure the contents get to disk */
3057c478bd9Sstevel@tonic-gate 	if (fsync(fd) != 0) {
3067c478bd9Sstevel@tonic-gate 		pr_err(gettext("Writing %s failed on sync: %s"), filep,
3077c478bd9Sstevel@tonic-gate 		    strerror(errno));
3087c478bd9Sstevel@tonic-gate 		close(fd);
3097c478bd9Sstevel@tonic-gate 		return (-1);
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	close(fd);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	/* return success */
3157c478bd9Sstevel@tonic-gate 	return (0);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate int
cachefs_label_file_vcheck(char * filep,struct cache_label * clabelp)3197c478bd9Sstevel@tonic-gate cachefs_label_file_vcheck(char *filep, struct cache_label *clabelp)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	/* check for an invalid version number */
3227c478bd9Sstevel@tonic-gate 	if (clabelp->cl_cfsversion != CFSVERSION) {
3237c478bd9Sstevel@tonic-gate 		pr_err(gettext("Cache label file %s corrupted"), filep);
3247c478bd9Sstevel@tonic-gate 		return (-1);
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	return (0);
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate /*
3317c478bd9Sstevel@tonic-gate  *
3327c478bd9Sstevel@tonic-gate  *			cachefs_inuse
3337c478bd9Sstevel@tonic-gate  *
3347c478bd9Sstevel@tonic-gate  * Description:
3357c478bd9Sstevel@tonic-gate  *	Tests whether or not the cache directory is in use by
3367c478bd9Sstevel@tonic-gate  *	the cache file system.
3377c478bd9Sstevel@tonic-gate  * Arguments:
3387c478bd9Sstevel@tonic-gate  *	cachedirp	name of the file system cache directory
3397c478bd9Sstevel@tonic-gate  * Returns:
3407c478bd9Sstevel@tonic-gate  *	Returns 1 if the cache is in use or an error, 0 if not.
3417c478bd9Sstevel@tonic-gate  * Preconditions:
3427c478bd9Sstevel@tonic-gate  *	precond(cachedirp)
3437c478bd9Sstevel@tonic-gate  */
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate int
cachefs_inuse(const char * cachedirp)3467c478bd9Sstevel@tonic-gate cachefs_inuse(const char *cachedirp)
3477c478bd9Sstevel@tonic-gate {
3487c478bd9Sstevel@tonic-gate 	int fd;
3497c478bd9Sstevel@tonic-gate 	int xx;
3507c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
3517c478bd9Sstevel@tonic-gate 	char *lockp = CACHEFS_LOCK_FILE;
3527c478bd9Sstevel@tonic-gate 	struct flock fl;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	/* see if path name is too long */
3557c478bd9Sstevel@tonic-gate 	xx = strlen(cachedirp) + strlen(lockp) + 3;
3567c478bd9Sstevel@tonic-gate 	if (xx >= MAXPATHLEN) {
3577c478bd9Sstevel@tonic-gate 		pr_err(gettext("Cache directory name %s is too long"),
3587c478bd9Sstevel@tonic-gate 		    cachedirp);
3597c478bd9Sstevel@tonic-gate 		return (1);
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	/* make a path to the cache directory lock file */
3637c478bd9Sstevel@tonic-gate 	snprintf(buf, sizeof (buf), "%s/%s", cachedirp, lockp);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	/* Open the kernel in use lock file.  This file will be <2GB. */
3667c478bd9Sstevel@tonic-gate 	fd = open(buf, O_RDWR, 0700);
3677c478bd9Sstevel@tonic-gate 	if (fd == -1) {
3687c478bd9Sstevel@tonic-gate 		pr_err(gettext("Cannot open lock file %s"), buf);
3697c478bd9Sstevel@tonic-gate 		return (1);
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	/* test the lock status */
3737c478bd9Sstevel@tonic-gate 	fl.l_type = F_WRLCK;
3747c478bd9Sstevel@tonic-gate 	fl.l_whence = 0;
3757c478bd9Sstevel@tonic-gate 	fl.l_start = 0;
3767c478bd9Sstevel@tonic-gate 	fl.l_len = 1024;
3777c478bd9Sstevel@tonic-gate 	fl.l_sysid = 0;
3787c478bd9Sstevel@tonic-gate 	fl.l_pid = 0;
3797c478bd9Sstevel@tonic-gate 	xx = fcntl(fd, F_GETLK, &fl);
3807c478bd9Sstevel@tonic-gate 	if (xx == -1) {
3817c478bd9Sstevel@tonic-gate 		pr_err(gettext("Unexpected failure on lock file %s %s"),
3827c478bd9Sstevel@tonic-gate 		    buf, strerror(errno));
3837c478bd9Sstevel@tonic-gate 		close(fd);
3847c478bd9Sstevel@tonic-gate 		return (1);
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 	close(fd);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if (fl.l_type == F_UNLCK)
3897c478bd9Sstevel@tonic-gate 		xx = 0;
3907c478bd9Sstevel@tonic-gate 	else
3917c478bd9Sstevel@tonic-gate 		xx = 1;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	/* return whether or not the cache is in use */
3947c478bd9Sstevel@tonic-gate 	return (xx);
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate /*
3987c478bd9Sstevel@tonic-gate  *
3997c478bd9Sstevel@tonic-gate  *			cachefs_resouce_size
4007c478bd9Sstevel@tonic-gate  *
4017c478bd9Sstevel@tonic-gate  * Description:
4027c478bd9Sstevel@tonic-gate  *	Returns information about a resource file.
4037c478bd9Sstevel@tonic-gate  * Arguments:
4047c478bd9Sstevel@tonic-gate  *	maxinodes	number of inodes to be managed by the resource file
4057c478bd9Sstevel@tonic-gate  *	rinfop		set to info about the resource file
4067c478bd9Sstevel@tonic-gate  * Returns:
4077c478bd9Sstevel@tonic-gate  * Preconditions:
4087c478bd9Sstevel@tonic-gate  *	precond(rinfop)
4097c478bd9Sstevel@tonic-gate  */
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate void
cachefs_resource_size(int maxinodes,struct cachefs_rinfo * rinfop)4127c478bd9Sstevel@tonic-gate cachefs_resource_size(int maxinodes, struct cachefs_rinfo *rinfop)
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	int fsize;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	fsize = MAXBSIZE;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	rinfop->r_ptroffset = fsize;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	fsize += MAXBSIZE * (maxinodes / CACHEFS_RLPMBS);
4217c478bd9Sstevel@tonic-gate 	if ((maxinodes % CACHEFS_RLPMBS) != 0)
4227c478bd9Sstevel@tonic-gate 		fsize += MAXBSIZE;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	rinfop->r_fsize = fsize;
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate /*
4287c478bd9Sstevel@tonic-gate  *
4297c478bd9Sstevel@tonic-gate  *			cachefs_create_cache
4307c478bd9Sstevel@tonic-gate  *
4317c478bd9Sstevel@tonic-gate  * Description:
4327c478bd9Sstevel@tonic-gate  *	Creates the specified cache directory and populates it as
4337c478bd9Sstevel@tonic-gate  *	needed by CFS.
4347c478bd9Sstevel@tonic-gate  * Arguments:
4357c478bd9Sstevel@tonic-gate  *	dirp		the name of the cache directory
4367c478bd9Sstevel@tonic-gate  *	uv		user values (may be NULL)
4377c478bd9Sstevel@tonic-gate  *	clabel		label file contents, or placeholder for this
4387c478bd9Sstevel@tonic-gate  * Returns:
4397c478bd9Sstevel@tonic-gate  *	Returns 0 for success or:
4407c478bd9Sstevel@tonic-gate  *		-1 for an error
4417c478bd9Sstevel@tonic-gate  *		-2 for an error and cache directory partially created
4427c478bd9Sstevel@tonic-gate  * Preconditions:
4437c478bd9Sstevel@tonic-gate  *	precond(dirp)
4447c478bd9Sstevel@tonic-gate  */
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate int
cachefs_create_cache(char * dirp,struct cachefs_user_values * uv,struct cache_label * clabel)4477c478bd9Sstevel@tonic-gate cachefs_create_cache(char *dirp, struct cachefs_user_values *uv,
4487c478bd9Sstevel@tonic-gate     struct cache_label *clabel)
4497c478bd9Sstevel@tonic-gate {
4507c478bd9Sstevel@tonic-gate 	int xx;
4517c478bd9Sstevel@tonic-gate 	char path[CACHEFS_XMAXPATH];
4527c478bd9Sstevel@tonic-gate 	int fd;
4537c478bd9Sstevel@tonic-gate 	void *bufp;
4547c478bd9Sstevel@tonic-gate 	int cnt;
4557c478bd9Sstevel@tonic-gate 	struct cache_usage cu;
4567c478bd9Sstevel@tonic-gate 	FILE *fp;
4577c478bd9Sstevel@tonic-gate 	char *parent;
4587c478bd9Sstevel@tonic-gate 	struct statvfs64 svfs;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	cu.cu_blksused = 0;
4617c478bd9Sstevel@tonic-gate 	cu.cu_filesused = 0;
4627c478bd9Sstevel@tonic-gate 	cu.cu_flags = 0;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	/* make sure cache dir name is not too long */
4657c478bd9Sstevel@tonic-gate 	if (strlen(dirp) > (size_t)PATH_MAX) {
4667c478bd9Sstevel@tonic-gate 		pr_err(gettext("path name %s is too long."), dirp);
4677c478bd9Sstevel@tonic-gate 		return (-1);
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/* ensure the path isn't in cachefs */
4717c478bd9Sstevel@tonic-gate 	parent = cachefs_file_to_dir(dirp);
4727c478bd9Sstevel@tonic-gate 	if (parent == NULL) {
4737c478bd9Sstevel@tonic-gate 		pr_err(gettext("Out of memory"));
4747c478bd9Sstevel@tonic-gate 		return (-1);
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 	if (statvfs64(parent, &svfs) != 0) {
4777c478bd9Sstevel@tonic-gate 		pr_err(gettext("%s: %s"), parent, strerror(errno));
4787c478bd9Sstevel@tonic-gate 		free(parent);
4797c478bd9Sstevel@tonic-gate 		return (-1);
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 	if (strcmp(svfs.f_basetype, CACHEFS_BASETYPE) == 0) {
4827c478bd9Sstevel@tonic-gate 		pr_err(gettext("Cannot create cache in cachefs filesystem"));
4837c478bd9Sstevel@tonic-gate 		free(parent);
4847c478bd9Sstevel@tonic-gate 		return (-1);
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 	free(parent);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	/* make the directory */
4897c478bd9Sstevel@tonic-gate 	if (mkdir(dirp, 0) == -1) {
4907c478bd9Sstevel@tonic-gate 		switch (errno) {
4917c478bd9Sstevel@tonic-gate 		case EEXIST:
4927c478bd9Sstevel@tonic-gate 			pr_err(gettext("%s already exists."), dirp);
4937c478bd9Sstevel@tonic-gate 			break;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 		default:
4967c478bd9Sstevel@tonic-gate 			pr_err(gettext("mkdir %s failed: %s"),
4977c478bd9Sstevel@tonic-gate 			    dirp, strerror(errno));
4987c478bd9Sstevel@tonic-gate 		}
4997c478bd9Sstevel@tonic-gate 		return (-1);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 	cu.cu_filesused += 1;
5027c478bd9Sstevel@tonic-gate 	cu.cu_blksused += 1;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	/* convert user values to a cache_label */
5057c478bd9Sstevel@tonic-gate 	if (uv != NULL) {
5067c478bd9Sstevel@tonic-gate 		xx = cachefs_convert_uv2cl(uv, clabel, dirp);
5077c478bd9Sstevel@tonic-gate 		if (xx)
5087c478bd9Sstevel@tonic-gate 			return (-2);
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	/*
5127c478bd9Sstevel@tonic-gate 	 * Create the cache directory lock file.
5137c478bd9Sstevel@tonic-gate 	 * Used by the kernel module to indicate the cache is in use.
5147c478bd9Sstevel@tonic-gate 	 * This file will be <2G.
5157c478bd9Sstevel@tonic-gate 	 */
5167c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, CACHEFS_LOCK_FILE);
5177c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDWR | O_CREAT, 0700);
5187c478bd9Sstevel@tonic-gate 	if (fd == -1) {
5197c478bd9Sstevel@tonic-gate 		pr_err(gettext("Cannot create lock file %s"), path);
5207c478bd9Sstevel@tonic-gate 		return (-1);
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 	close(fd);
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/* make the directory for the back file system mount points */
5257c478bd9Sstevel@tonic-gate 	/* note: we do not count this directory in the resources */
5267c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, BACKMNT_NAME);
5277c478bd9Sstevel@tonic-gate 	if (mkdir(path, 0700) == -1) {
5287c478bd9Sstevel@tonic-gate 		pr_err(gettext("mkdir %s failed: %s"), path,
5297c478bd9Sstevel@tonic-gate 		    strerror(errno));
5307c478bd9Sstevel@tonic-gate 		return (-2);
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	/* make the directory for lost+found */
5347c478bd9Sstevel@tonic-gate 	/* note: we do not count this directory in the resources */
5357c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, CACHEFS_LOSTFOUND_NAME);
5367c478bd9Sstevel@tonic-gate 	if (mkdir(path, 0700) == -1) {
5377c478bd9Sstevel@tonic-gate 		pr_err(gettext("mkdir %s failed: %s"), path,
5387c478bd9Sstevel@tonic-gate 		    strerror(errno));
5397c478bd9Sstevel@tonic-gate 		return (-2);
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/* make the networker "don't back up" file; this file is <2GB */
5437c478bd9Sstevel@tonic-gate 	xx = 0;
5447c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, NOBACKUP_NAME);
5457c478bd9Sstevel@tonic-gate 	if ((fp = fopen(path, "w")) != NULL) {
5467c478bd9Sstevel@tonic-gate 		if (realpath(dirp, path) != NULL) {
547*47644099Sgt29601 			fprintf(fp, "<< ./ >>\n");
5487c478bd9Sstevel@tonic-gate 			fprintf(fp, "+skip: .?* *\n");
5497c478bd9Sstevel@tonic-gate 			if (fclose(fp) == 0)
5507c478bd9Sstevel@tonic-gate 				xx = 1;
5517c478bd9Sstevel@tonic-gate 		}
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 	if (xx == 0) {
5547c478bd9Sstevel@tonic-gate 		snprintf(path, sizeof (path), "%s/%s", dirp, NOBACKUP_NAME);
5557c478bd9Sstevel@tonic-gate 		pr_err(gettext("can't create %s"), path);
5567c478bd9Sstevel@tonic-gate 		(void) unlink(path);
5577c478bd9Sstevel@tonic-gate 	} else {
5587c478bd9Sstevel@tonic-gate 		cu.cu_filesused += 1;
5597c478bd9Sstevel@tonic-gate 		cu.cu_blksused += 1;
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	/* create the unmount file */
5637c478bd9Sstevel@tonic-gate 	xx = 0;
5647c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, CACHEFS_UNMNT_FILE);
5657c478bd9Sstevel@tonic-gate 	if ((fp = fopen(path, "w")) != NULL) {
5667c478bd9Sstevel@tonic-gate 		time32_t btime;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 		btime = get_boottime();
5697c478bd9Sstevel@tonic-gate 		fwrite((void *)&btime, sizeof (btime), 1, fp);
5707c478bd9Sstevel@tonic-gate 		if (fclose(fp) == 0)
5717c478bd9Sstevel@tonic-gate 			xx = 1;
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate 	if (xx == 0)
5747c478bd9Sstevel@tonic-gate 		pr_err(gettext("can't create .cfs_unmnt file"));
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	/* create the cache label file */
5777c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, CACHELABEL_NAME);
5787c478bd9Sstevel@tonic-gate 	xx = cachefs_label_file_put(path, clabel);
5797c478bd9Sstevel@tonic-gate 	if (xx == -1) {
5807c478bd9Sstevel@tonic-gate 		pr_err(gettext("creating %s failed."), path);
5817c478bd9Sstevel@tonic-gate 		return (-2);
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 	cu.cu_filesused += 1;
5847c478bd9Sstevel@tonic-gate 	cu.cu_blksused += 1;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/* create the cache label duplicate file */
5877c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s.dup", dirp, CACHELABEL_NAME);
5887c478bd9Sstevel@tonic-gate 	xx = cachefs_label_file_put(path, clabel);
5897c478bd9Sstevel@tonic-gate 	if (xx == -1) {
5907c478bd9Sstevel@tonic-gate 		pr_err(gettext("creating %s failed."), path);
5917c478bd9Sstevel@tonic-gate 		return (-2);
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 	cu.cu_filesused += 1;
5947c478bd9Sstevel@tonic-gate 	cu.cu_blksused += 1;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	/* create the resouce file; this file will be <2GB */
5977c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, RESOURCE_NAME);
5987c478bd9Sstevel@tonic-gate 	fd = open(path, O_CREAT | O_RDWR, 0600);
5997c478bd9Sstevel@tonic-gate 	if (fd == -1) {
6007c478bd9Sstevel@tonic-gate 		pr_err(gettext("create %s failed: %s"), path,
6017c478bd9Sstevel@tonic-gate 		    strerror(errno));
6027c478bd9Sstevel@tonic-gate 		return (-2);
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 	cu.cu_filesused += 1;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	/* allocate a zeroed buffer for filling the resouce file */
6077c478bd9Sstevel@tonic-gate 	bufp = calloc(1, MAXBSIZE);
6087c478bd9Sstevel@tonic-gate 	if (bufp == NULL) {
6097c478bd9Sstevel@tonic-gate 		pr_err(gettext("out of space %d."), MAXBSIZE);
6107c478bd9Sstevel@tonic-gate 		close(fd);
6117c478bd9Sstevel@tonic-gate 		return (-2);
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	/* determine number of MAXBSIZE chunks to make the file */
6157c478bd9Sstevel@tonic-gate 	cnt = 1;	/* for the header */
6167c478bd9Sstevel@tonic-gate 	cnt += clabel->cl_maxinodes / CACHEFS_RLPMBS;
6177c478bd9Sstevel@tonic-gate 	if ((clabel->cl_maxinodes % CACHEFS_RLPMBS) != 0)
6187c478bd9Sstevel@tonic-gate 		++cnt;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	/* fill up the file with zeros */
6217c478bd9Sstevel@tonic-gate 	for (xx = 0; xx < cnt; xx++) {
6227c478bd9Sstevel@tonic-gate 		if (write(fd, bufp, MAXBSIZE) != MAXBSIZE) {
6237c478bd9Sstevel@tonic-gate 			pr_err(gettext("write %s failed: %s"), path,
6247c478bd9Sstevel@tonic-gate 			    strerror(errno));
6257c478bd9Sstevel@tonic-gate 			close(fd);
6267c478bd9Sstevel@tonic-gate 			free(bufp);
6277c478bd9Sstevel@tonic-gate 			return (-2);
6287c478bd9Sstevel@tonic-gate 		}
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 	free(bufp);
6317c478bd9Sstevel@tonic-gate 	cu.cu_blksused += cnt;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	/* position to the begining of the file */
6347c478bd9Sstevel@tonic-gate 	if (lseek(fd, 0, SEEK_SET) == -1) {
6357c478bd9Sstevel@tonic-gate 		pr_err(gettext("lseek %s failed: %s"), path,
6367c478bd9Sstevel@tonic-gate 		    strerror(errno));
6377c478bd9Sstevel@tonic-gate 		close(fd);
6387c478bd9Sstevel@tonic-gate 		return (-2);
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	/* write the cache usage structure */
6427c478bd9Sstevel@tonic-gate 	xx = sizeof (struct cache_usage);
6437c478bd9Sstevel@tonic-gate 	if (write(fd, &cu, xx) != xx) {
6447c478bd9Sstevel@tonic-gate 		pr_err(gettext("cu write %s failed: %s"), path,
6457c478bd9Sstevel@tonic-gate 		    strerror(errno));
6467c478bd9Sstevel@tonic-gate 		close(fd);
6477c478bd9Sstevel@tonic-gate 		return (-2);
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	/* make sure the contents get to disk */
6517c478bd9Sstevel@tonic-gate 	if (fsync(fd) != 0) {
6527c478bd9Sstevel@tonic-gate 		pr_err(gettext("fsync %s failed: %s"), path,
6537c478bd9Sstevel@tonic-gate 		    strerror(errno));
6547c478bd9Sstevel@tonic-gate 		close(fd);
6557c478bd9Sstevel@tonic-gate 		return (-2);
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate 	close(fd);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	/* return success */
6607c478bd9Sstevel@tonic-gate 	return (0);
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate /*
6647c478bd9Sstevel@tonic-gate  *
6657c478bd9Sstevel@tonic-gate  *			cachefs_delete_all_cache
6667c478bd9Sstevel@tonic-gate  *
6677c478bd9Sstevel@tonic-gate  * Description:
6687c478bd9Sstevel@tonic-gate  *	Delete all caches in cache directory.
6697c478bd9Sstevel@tonic-gate  * Arguments:
6707c478bd9Sstevel@tonic-gate  *	dirp	the pathname of of the cache directory to delete
6717c478bd9Sstevel@tonic-gate  * Returns:
6727c478bd9Sstevel@tonic-gate  *	Returns 0 for success or -1 for an error.
6737c478bd9Sstevel@tonic-gate  * Preconditions:
6747c478bd9Sstevel@tonic-gate  *	precond(dirp)
6757c478bd9Sstevel@tonic-gate  */
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate int
cachefs_delete_all_cache(char * dirp)6787c478bd9Sstevel@tonic-gate cachefs_delete_all_cache(char *dirp)
6797c478bd9Sstevel@tonic-gate {
6807c478bd9Sstevel@tonic-gate 	DIR *dp;
6817c478bd9Sstevel@tonic-gate 	struct dirent64 *dep;
6827c478bd9Sstevel@tonic-gate 	int xx;
6837c478bd9Sstevel@tonic-gate 	char path[CACHEFS_XMAXPATH];
6847c478bd9Sstevel@tonic-gate 	struct stat64 statinfo;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/* make sure cache dir name is not too long */
6877c478bd9Sstevel@tonic-gate 	if (strlen(dirp) > (size_t)PATH_MAX) {
6887c478bd9Sstevel@tonic-gate 		pr_err(gettext("path name %s is too long."),
6897c478bd9Sstevel@tonic-gate 		    dirp);
6907c478bd9Sstevel@tonic-gate 		return (-1);
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	/* check that dirp is probably a cachefs directory */
6947c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, BACKMNT_NAME);
6957c478bd9Sstevel@tonic-gate 	xx = access(path, R_OK | W_OK | X_OK);
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, CACHELABEL_NAME);
6987c478bd9Sstevel@tonic-gate 	xx |= access(path, R_OK | W_OK);
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	if (xx) {
7017c478bd9Sstevel@tonic-gate 		pr_err(gettext("%s does not appear to be a "
7027c478bd9Sstevel@tonic-gate 		    "cachefs cache directory."), dirp);
7037c478bd9Sstevel@tonic-gate 		return (-1);
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	/* remove the lost+found directory if it exists and is empty */
7077c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, CACHEFS_LOSTFOUND_NAME);
7087c478bd9Sstevel@tonic-gate 	xx = rmdir(path);
7097c478bd9Sstevel@tonic-gate 	if (xx == -1) {
7107c478bd9Sstevel@tonic-gate 		if (errno == EEXIST) {
7117c478bd9Sstevel@tonic-gate 			pr_err(gettext("Cannot delete cache '%s'.  "
7127c478bd9Sstevel@tonic-gate 			    "First move files in '%s' to a safe location."),
7137c478bd9Sstevel@tonic-gate 			    dirp, path);
7147c478bd9Sstevel@tonic-gate 			return (-1);
7157c478bd9Sstevel@tonic-gate 		} else if (errno != ENOENT) {
7167c478bd9Sstevel@tonic-gate 			pr_err(gettext("rmdir %s failed: %s"), path,
7177c478bd9Sstevel@tonic-gate 			    strerror(errno));
7187c478bd9Sstevel@tonic-gate 			return (-1);
7197c478bd9Sstevel@tonic-gate 		}
7207c478bd9Sstevel@tonic-gate 	}
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	/* delete the back FS mount point directory if it exists */
7237c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, BACKMNT_NAME);
7247c478bd9Sstevel@tonic-gate 	xx = lstat64(path, &statinfo);
7257c478bd9Sstevel@tonic-gate 	if (xx == -1) {
7267c478bd9Sstevel@tonic-gate 		if (errno != ENOENT) {
7277c478bd9Sstevel@tonic-gate 			pr_err(gettext("lstat %s failed: %s"), path,
7287c478bd9Sstevel@tonic-gate 			    strerror(errno));
7297c478bd9Sstevel@tonic-gate 			return (-1);
7307c478bd9Sstevel@tonic-gate 		}
7317c478bd9Sstevel@tonic-gate 	} else {
7327c478bd9Sstevel@tonic-gate 		xx = nftw64(path, cachefs_delete_file, 16,
7337c478bd9Sstevel@tonic-gate 		    FTW_PHYS | FTW_DEPTH | FTW_MOUNT);
7347c478bd9Sstevel@tonic-gate 		if (xx == -1) {
7357c478bd9Sstevel@tonic-gate 			pr_err(gettext("unable to delete %s"), path);
7367c478bd9Sstevel@tonic-gate 			return (-1);
7377c478bd9Sstevel@tonic-gate 		}
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	/* open the cache directory specified */
7417c478bd9Sstevel@tonic-gate 	if ((dp = opendir(dirp)) == NULL) {
7427c478bd9Sstevel@tonic-gate 		pr_err(gettext("cannot open cache directory %s: %s"),
7437c478bd9Sstevel@tonic-gate 		    dirp, strerror(errno));
7447c478bd9Sstevel@tonic-gate 		return (-1);
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	/* read the file names in the cache directory */
7487c478bd9Sstevel@tonic-gate 	while ((dep = readdir64(dp)) != NULL) {
7497c478bd9Sstevel@tonic-gate 		/* ignore . and .. */
7507c478bd9Sstevel@tonic-gate 		if (strcmp(dep->d_name, ".") == 0 ||
7517c478bd9Sstevel@tonic-gate 				strcmp(dep->d_name, "..") == 0)
7527c478bd9Sstevel@tonic-gate 			continue;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 		/* stat the file */
7557c478bd9Sstevel@tonic-gate 		snprintf(path, sizeof (path), "%s/%s", dirp, dep->d_name);
7567c478bd9Sstevel@tonic-gate 		xx = lstat64(path, &statinfo);
7577c478bd9Sstevel@tonic-gate 		if (xx == -1) {
7587c478bd9Sstevel@tonic-gate 			if (errno == ENOENT) {
7597c478bd9Sstevel@tonic-gate 				/* delete_cache may have nuked a directory */
7607c478bd9Sstevel@tonic-gate 				continue;
7617c478bd9Sstevel@tonic-gate 			}
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 			pr_err(gettext("lstat %s failed: %s"),
7647c478bd9Sstevel@tonic-gate 			    path, strerror(errno));
7657c478bd9Sstevel@tonic-gate 			closedir(dp);
7667c478bd9Sstevel@tonic-gate 			return (-1);
7677c478bd9Sstevel@tonic-gate 		}
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 		/* ignore anything that is not a link */
7707c478bd9Sstevel@tonic-gate 		if (!S_ISLNK(statinfo.st_mode))
7717c478bd9Sstevel@tonic-gate 			continue;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 		/* delete the file system cache directory */
7747c478bd9Sstevel@tonic-gate 		xx = cachefs_delete_cache(dirp, dep->d_name);
7757c478bd9Sstevel@tonic-gate 		if (xx) {
7767c478bd9Sstevel@tonic-gate 			closedir(dp);
7777c478bd9Sstevel@tonic-gate 			return (-1);
7787c478bd9Sstevel@tonic-gate 		}
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate 	closedir(dp);
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	/* remove the cache dir unmount file */
7837c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, CACHEFS_UNMNT_FILE);
7847c478bd9Sstevel@tonic-gate 	xx = unlink(path);
7857c478bd9Sstevel@tonic-gate 	if ((xx == -1) && (errno != ENOENT)) {
7867c478bd9Sstevel@tonic-gate 		pr_err(gettext("unlink %s failed: %s"), path,
7877c478bd9Sstevel@tonic-gate 		    strerror(errno));
7887c478bd9Sstevel@tonic-gate 		return (-1);
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	/* remove the cache label file */
7927c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, CACHELABEL_NAME);
7937c478bd9Sstevel@tonic-gate 	xx = unlink(path);
7947c478bd9Sstevel@tonic-gate 	if ((xx == -1) && (errno != ENOENT)) {
7957c478bd9Sstevel@tonic-gate 		pr_err(gettext("unlink %s failed: %s"), path,
7967c478bd9Sstevel@tonic-gate 		    strerror(errno));
7977c478bd9Sstevel@tonic-gate 		return (-1);
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	/* remove the cache label duplicate file */
8017c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s.dup", dirp, CACHELABEL_NAME);
8027c478bd9Sstevel@tonic-gate 	xx = unlink(path);
8037c478bd9Sstevel@tonic-gate 	if ((xx == -1) && (errno != ENOENT)) {
8047c478bd9Sstevel@tonic-gate 		pr_err(gettext("unlink %s failed: %s"), path,
8057c478bd9Sstevel@tonic-gate 		    strerror(errno));
8067c478bd9Sstevel@tonic-gate 		return (-1);
8077c478bd9Sstevel@tonic-gate 	}
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	/* remove the resource file */
8107c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, RESOURCE_NAME);
8117c478bd9Sstevel@tonic-gate 	xx = unlink(path);
8127c478bd9Sstevel@tonic-gate 	if ((xx == -1) && (errno != ENOENT)) {
8137c478bd9Sstevel@tonic-gate 		pr_err(gettext("unlink %s failed: %s"), path,
8147c478bd9Sstevel@tonic-gate 		    strerror(errno));
8157c478bd9Sstevel@tonic-gate 		return (-1);
8167c478bd9Sstevel@tonic-gate 	}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	/* remove the cachefslog file if it exists */
8197c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, LOG_STATUS_NAME);
8207c478bd9Sstevel@tonic-gate 	(void) unlink(path);
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	/* remove the networker "don't back up" file if it exists */
8237c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, NOBACKUP_NAME);
8247c478bd9Sstevel@tonic-gate 	(void) unlink(path);
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	/* remove the lock file */
8277c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, CACHEFS_LOCK_FILE);
8287c478bd9Sstevel@tonic-gate 	xx = unlink(path);
8297c478bd9Sstevel@tonic-gate 	if ((xx == -1) && (errno != ENOENT)) {
8307c478bd9Sstevel@tonic-gate 		pr_err(gettext("unlink %s failed: %s"), path,
8317c478bd9Sstevel@tonic-gate 		    strerror(errno));
8327c478bd9Sstevel@tonic-gate 		return (-1);
8337c478bd9Sstevel@tonic-gate 	}
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	/* remove the directory */
8367c478bd9Sstevel@tonic-gate 	xx = rmdir(dirp);
8377c478bd9Sstevel@tonic-gate 	if (xx == -1) {
8387c478bd9Sstevel@tonic-gate 		pr_err(gettext("rmdir %s failed: %s"), dirp,
8397c478bd9Sstevel@tonic-gate 		    strerror(errno));
8407c478bd9Sstevel@tonic-gate 		return (-1);
8417c478bd9Sstevel@tonic-gate 	}
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	/* return success */
8447c478bd9Sstevel@tonic-gate 	return (0);
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate /*
8487c478bd9Sstevel@tonic-gate  *
8497c478bd9Sstevel@tonic-gate  *			cachefs_delete_cache
8507c478bd9Sstevel@tonic-gate  *
8517c478bd9Sstevel@tonic-gate  * Description:
8527c478bd9Sstevel@tonic-gate  *	Deletes the specified file system cache.
8537c478bd9Sstevel@tonic-gate  * Arguments:
8547c478bd9Sstevel@tonic-gate  *	dirp	cache directory name
8557c478bd9Sstevel@tonic-gate  *	namep	file system cache directory to delete
8567c478bd9Sstevel@tonic-gate  * Returns:
8577c478bd9Sstevel@tonic-gate  *	Returns 0 for success, -1 for failure.
8587c478bd9Sstevel@tonic-gate  * Preconditions:
8597c478bd9Sstevel@tonic-gate  *	precond(dirp)
8607c478bd9Sstevel@tonic-gate  *	precond(namep)
8617c478bd9Sstevel@tonic-gate  */
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate int
cachefs_delete_cache(char * dirp,char * namep)8647c478bd9Sstevel@tonic-gate cachefs_delete_cache(char *dirp, char *namep)
8657c478bd9Sstevel@tonic-gate {
8667c478bd9Sstevel@tonic-gate 	char path[CACHEFS_XMAXPATH];
8677c478bd9Sstevel@tonic-gate 	char buf[CACHEFS_XMAXPATH];
8687c478bd9Sstevel@tonic-gate 	int xx;
8697c478bd9Sstevel@tonic-gate 	struct stat64 statinfo;
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	/* make sure cache dir name is not too long */
8727c478bd9Sstevel@tonic-gate 	if (strlen(dirp) > (size_t)PATH_MAX) {
8737c478bd9Sstevel@tonic-gate 		pr_err(gettext("path name %s is too long."),
8747c478bd9Sstevel@tonic-gate 		    dirp);
8757c478bd9Sstevel@tonic-gate 		return (-1);
8767c478bd9Sstevel@tonic-gate 	}
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	/* construct the path name of the file system cache directory */
8797c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, namep);
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	/* stat the specified file */
8827c478bd9Sstevel@tonic-gate 	xx = lstat64(path, &statinfo);
8837c478bd9Sstevel@tonic-gate 	if (xx == -1) {
8847c478bd9Sstevel@tonic-gate 		pr_err(gettext("lstat %s failed: %s"), path,
8857c478bd9Sstevel@tonic-gate 		    strerror(errno));
8867c478bd9Sstevel@tonic-gate 		return (-1);
8877c478bd9Sstevel@tonic-gate 	}
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	/* make sure name is a symbolic link */
8907c478bd9Sstevel@tonic-gate 	if (!S_ISLNK(statinfo.st_mode)) {
8917c478bd9Sstevel@tonic-gate 		pr_err(gettext("\"%s\" is not a valid cache id."), namep);
8927c478bd9Sstevel@tonic-gate 		return (-1);
8937c478bd9Sstevel@tonic-gate 	}
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	/* read the contents of the symbolic link */
8967c478bd9Sstevel@tonic-gate 	xx = readlink(path, buf, sizeof (buf));
8977c478bd9Sstevel@tonic-gate 	if (xx == -1) {
8987c478bd9Sstevel@tonic-gate 		pr_err(gettext("Readlink of %s failed: %s"), path,
8997c478bd9Sstevel@tonic-gate 		    strerror(errno));
9007c478bd9Sstevel@tonic-gate 		return (-1);
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 	buf[xx] = '\0';
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	/* remove the directory */
9057c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, buf);
9067c478bd9Sstevel@tonic-gate 	xx = nftw64(path, cachefs_delete_file, 16,
9077c478bd9Sstevel@tonic-gate 	    FTW_PHYS | FTW_DEPTH | FTW_MOUNT);
9087c478bd9Sstevel@tonic-gate 	if (xx == -1) {
9097c478bd9Sstevel@tonic-gate 		pr_err(gettext("directory walk of %s failed."), dirp);
9107c478bd9Sstevel@tonic-gate 		return (-1);
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	/* delete the link */
9147c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "%s/%s", dirp, namep);
9157c478bd9Sstevel@tonic-gate 	if (unlink(path) == -1) {
9167c478bd9Sstevel@tonic-gate 		pr_err(gettext("unlink %s failed: %s"), path,
9177c478bd9Sstevel@tonic-gate 		    strerror(errno));
9187c478bd9Sstevel@tonic-gate 		return (-1);
9197c478bd9Sstevel@tonic-gate 	}
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	/* return success */
9227c478bd9Sstevel@tonic-gate 	return (0);
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate /*
9267c478bd9Sstevel@tonic-gate  *
9277c478bd9Sstevel@tonic-gate  *			cachefs_delete_file
9287c478bd9Sstevel@tonic-gate  *
9297c478bd9Sstevel@tonic-gate  * Description:
9307c478bd9Sstevel@tonic-gate  *	Remove a file or directory; called by nftw64().
9317c478bd9Sstevel@tonic-gate  * Arguments:
9327c478bd9Sstevel@tonic-gate  *	namep	pathname of the file
9337c478bd9Sstevel@tonic-gate  *	statp	stat info about the file
9347c478bd9Sstevel@tonic-gate  *	flg	info about file
9357c478bd9Sstevel@tonic-gate  *	ftwp	depth information
9367c478bd9Sstevel@tonic-gate  * Returns:
9377c478bd9Sstevel@tonic-gate  *	Returns 0 for success, -1 for failure.
9387c478bd9Sstevel@tonic-gate  * Preconditions:
9397c478bd9Sstevel@tonic-gate  *	precond(namep)
9407c478bd9Sstevel@tonic-gate  */
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate int
cachefs_delete_file(const char * namep,const struct stat64 * statp,int flg,struct FTW * ftwp)9437c478bd9Sstevel@tonic-gate cachefs_delete_file(const char *namep, const struct stat64 *statp, int flg,
9447c478bd9Sstevel@tonic-gate     struct FTW *ftwp)
9457c478bd9Sstevel@tonic-gate {
9467c478bd9Sstevel@tonic-gate 	/* ignore . and .. */
9477c478bd9Sstevel@tonic-gate 	if (strcmp(namep, ".") == 0 || strcmp(namep, "..") == 0)
9487c478bd9Sstevel@tonic-gate 		return (0);
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	switch (flg) {
9517c478bd9Sstevel@tonic-gate 	case FTW_F:	/* files */
9527c478bd9Sstevel@tonic-gate 	case FTW_SL:
9537c478bd9Sstevel@tonic-gate 		if (unlink(namep) == -1) {
9547c478bd9Sstevel@tonic-gate 			pr_err(gettext("unlink %s failed: %s"),
9557c478bd9Sstevel@tonic-gate 			    namep, strerror(errno));
9567c478bd9Sstevel@tonic-gate 			return (-1);
9577c478bd9Sstevel@tonic-gate 		}
9587c478bd9Sstevel@tonic-gate 		break;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	case FTW_DP:	/* directories that have their children processed */
9617c478bd9Sstevel@tonic-gate 		if (rmdir(namep) == -1) {
9627c478bd9Sstevel@tonic-gate 			pr_err(gettext("rmdir %s failed: %s"),
9637c478bd9Sstevel@tonic-gate 			    namep, strerror(errno));
9647c478bd9Sstevel@tonic-gate 			return (-1);
9657c478bd9Sstevel@tonic-gate 		}
9667c478bd9Sstevel@tonic-gate 		break;
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	case FTW_D:	/* ignore directories if children not processed */
9697c478bd9Sstevel@tonic-gate 		break;
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	default:
9727c478bd9Sstevel@tonic-gate 		pr_err(gettext("failure on file %s, flg %d."),
9737c478bd9Sstevel@tonic-gate 		    namep, flg);
9747c478bd9Sstevel@tonic-gate 		return (-1);
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	/* return success */
9787c478bd9Sstevel@tonic-gate 	return (0);
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate /*
9827c478bd9Sstevel@tonic-gate  *
9837c478bd9Sstevel@tonic-gate  *			cachefs_convert_uv2cl
9847c478bd9Sstevel@tonic-gate  *
9857c478bd9Sstevel@tonic-gate  * Description:
9867c478bd9Sstevel@tonic-gate  *	Copies the contents of a cachefs_user_values object into a
9877c478bd9Sstevel@tonic-gate  *	cache_label object, performing the necessary conversions.
9887c478bd9Sstevel@tonic-gate  * Arguments:
9897c478bd9Sstevel@tonic-gate  *	uvp	cachefs_user_values to copy from
9907c478bd9Sstevel@tonic-gate  *	clp	cache_label to copy into
9917c478bd9Sstevel@tonic-gate  *	dirp	cache directory
9927c478bd9Sstevel@tonic-gate  * Returns:
9937c478bd9Sstevel@tonic-gate  *	Returns 0 for success, -1 for an error.
9947c478bd9Sstevel@tonic-gate  * Preconditions:
9957c478bd9Sstevel@tonic-gate  *	precond(uvp)
9967c478bd9Sstevel@tonic-gate  *	precond(clp)
9977c478bd9Sstevel@tonic-gate  *	precond(dirp)
9987c478bd9Sstevel@tonic-gate  */
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate int
cachefs_convert_uv2cl(const struct cachefs_user_values * uvp,struct cache_label * clp,const char * dirp)10017c478bd9Sstevel@tonic-gate cachefs_convert_uv2cl(const struct cachefs_user_values *uvp,
10027c478bd9Sstevel@tonic-gate     struct cache_label *clp, const char *dirp)
10037c478bd9Sstevel@tonic-gate {
10047c478bd9Sstevel@tonic-gate 	struct statvfs64 fs;
10057c478bd9Sstevel@tonic-gate 	int xx;
10067c478bd9Sstevel@tonic-gate 	double ftmp;
10077c478bd9Sstevel@tonic-gate 	double temp;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	/* get file system information */
10107c478bd9Sstevel@tonic-gate 	xx = statvfs64(dirp, &fs);
10117c478bd9Sstevel@tonic-gate 	if (xx == -1) {
10127c478bd9Sstevel@tonic-gate 		pr_err(gettext("statvfs %s failed: %s"), dirp,
10137c478bd9Sstevel@tonic-gate 		    strerror(errno));
10147c478bd9Sstevel@tonic-gate 		return (-1);
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	ftmp = (double)fs.f_frsize / (double)MAXBSIZE;
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	/* front fs is less than 1 terabyte */
10207c478bd9Sstevel@tonic-gate 	temp = (double)uvp->uv_maxblocks / 100.0 *
10217c478bd9Sstevel@tonic-gate 	    (double)fs.f_blocks * ftmp + .5;
10227c478bd9Sstevel@tonic-gate 	clp->cl_maxblks = temp < (double)INT_MAX ? (int)temp : INT_MAX;
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	temp = (double)uvp->uv_minblocks / 100.0 *
10257c478bd9Sstevel@tonic-gate 	    (double)fs.f_blocks * ftmp + .5;
10267c478bd9Sstevel@tonic-gate 	clp->cl_blockmin = temp < (double)INT_MAX ? (int)temp : INT_MAX;
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	temp = (double)uvp->uv_threshblocks / 100.0 *
10297c478bd9Sstevel@tonic-gate 	    (double)fs.f_blocks * ftmp + .5;
10307c478bd9Sstevel@tonic-gate 	clp->cl_blocktresh = temp < (double)INT_MAX ? (int)temp : INT_MAX;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	temp = (double)uvp->uv_maxfiles / 100.0 * (double)fs.f_files + .5;
10337c478bd9Sstevel@tonic-gate 	clp->cl_maxinodes = temp < (double)INT_MAX ? (int)temp : INT_MAX;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	temp = (double)uvp->uv_minfiles / 100.0 * (double)fs.f_files + .5;
10367c478bd9Sstevel@tonic-gate 	clp->cl_filemin = temp < (double)INT_MAX ? (int)temp : INT_MAX;
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	temp = (double)uvp->uv_threshfiles / 100.0 * (double)fs.f_files +.5;
10397c478bd9Sstevel@tonic-gate 	clp->cl_filetresh = temp < (double)INT_MAX ? (int)temp : INT_MAX;
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	ftmp = (double)(1024 * 1024) / (double)MAXBSIZE;
10427c478bd9Sstevel@tonic-gate 	clp->cl_maxfiles = uvp->uv_maxfilesize * ftmp + .5;
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	clp->cl_blkhiwat = uvp->uv_hiblocks / 100.0 * clp->cl_maxblks + .5;
10457c478bd9Sstevel@tonic-gate 	clp->cl_blklowat = uvp->uv_lowblocks / 100.0 * clp->cl_maxblks + .5;
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	clp->cl_filehiwat = uvp->uv_hifiles / 100.0 * clp->cl_maxinodes + .5;
10487c478bd9Sstevel@tonic-gate 	clp->cl_filelowat = uvp->uv_lowfiles / 100.0 * clp->cl_maxinodes + .5;
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	clp->cl_cfsversion = CFSVERSION;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	/* return success */
10537c478bd9Sstevel@tonic-gate 	return (0);
10547c478bd9Sstevel@tonic-gate }
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate /*
10577c478bd9Sstevel@tonic-gate  *
10587c478bd9Sstevel@tonic-gate  *			cachefs_convert_cl2uv
10597c478bd9Sstevel@tonic-gate  *
10607c478bd9Sstevel@tonic-gate  * Description:
10617c478bd9Sstevel@tonic-gate  *	Copies the contents of a cache_label object into a
10627c478bd9Sstevel@tonic-gate  *	cachefs_user_values object, performing the necessary conversions.
10637c478bd9Sstevel@tonic-gate  * Arguments:
10647c478bd9Sstevel@tonic-gate  *	clp	cache_label to copy from
10657c478bd9Sstevel@tonic-gate  *	uvp	cachefs_user_values to copy into
10667c478bd9Sstevel@tonic-gate  *	dirp	cache directory
10677c478bd9Sstevel@tonic-gate  * Returns:
10687c478bd9Sstevel@tonic-gate  *	Returns 0 for success, -1 for an error.
10697c478bd9Sstevel@tonic-gate  * Preconditions:
10707c478bd9Sstevel@tonic-gate  *	precond(clp)
10717c478bd9Sstevel@tonic-gate  *	precond(uvp)
10727c478bd9Sstevel@tonic-gate  *	precond(dirp)
10737c478bd9Sstevel@tonic-gate  */
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate int
cachefs_convert_cl2uv(const struct cache_label * clp,struct cachefs_user_values * uvp,const char * dirp)10767c478bd9Sstevel@tonic-gate cachefs_convert_cl2uv(const struct cache_label *clp,
10777c478bd9Sstevel@tonic-gate     struct cachefs_user_values *uvp, const char *dirp)
10787c478bd9Sstevel@tonic-gate {
10797c478bd9Sstevel@tonic-gate 	struct statvfs64 fs;
10807c478bd9Sstevel@tonic-gate 	int xx;
10817c478bd9Sstevel@tonic-gate 	double temp;
10827c478bd9Sstevel@tonic-gate 	double ftmp;
10837c478bd9Sstevel@tonic-gate 	long long ltmp;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	/* get file system information */
10867c478bd9Sstevel@tonic-gate 	xx = statvfs64(dirp, &fs);
10877c478bd9Sstevel@tonic-gate 	if (xx == -1) {
10887c478bd9Sstevel@tonic-gate 		pr_err(gettext("statvfs %s failed: %s"), dirp,
10897c478bd9Sstevel@tonic-gate 		    strerror(errno));
10907c478bd9Sstevel@tonic-gate 		return (-1);
10917c478bd9Sstevel@tonic-gate 	}
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate #define	BOUND(yy) \
10947c478bd9Sstevel@tonic-gate 	yy = (yy < 0) ? 0 : yy; \
10957c478bd9Sstevel@tonic-gate 	yy = (yy > 100) ? 100 : yy;
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	ftmp = (double)MAXBSIZE / (double)fs.f_frsize;
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	temp = (double)clp->cl_maxblks * ftmp /
11007c478bd9Sstevel@tonic-gate 	    (double)fs.f_blocks * 100. + .5;
11017c478bd9Sstevel@tonic-gate 	BOUND(temp);
11027c478bd9Sstevel@tonic-gate 	uvp->uv_maxblocks = (int)temp;
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	temp = (double)clp->cl_blockmin * ftmp /
11057c478bd9Sstevel@tonic-gate 	    (double)fs.f_blocks * 100. + .5;
11067c478bd9Sstevel@tonic-gate 	BOUND(temp);
11077c478bd9Sstevel@tonic-gate 	uvp->uv_minblocks = (int)temp;
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	temp = (double)clp->cl_blocktresh * ftmp /
11107c478bd9Sstevel@tonic-gate 	    (double)fs.f_blocks * 100. + .5;
11117c478bd9Sstevel@tonic-gate 	BOUND(temp);
11127c478bd9Sstevel@tonic-gate 	uvp->uv_threshblocks = (int)temp;
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	temp = ((double)clp->cl_maxinodes / fs.f_files) * 100. + .5;
11157c478bd9Sstevel@tonic-gate 	BOUND(temp);
11167c478bd9Sstevel@tonic-gate 	uvp->uv_maxfiles = (int)temp;
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	temp = ((double)clp->cl_filemin / fs.f_files) * 100. + .5;
11197c478bd9Sstevel@tonic-gate 	BOUND(temp);
11207c478bd9Sstevel@tonic-gate 	uvp->uv_minfiles = (int)temp;
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	temp = ((double)clp->cl_filetresh / fs.f_files) * 100. + .5;
11237c478bd9Sstevel@tonic-gate 	BOUND(temp);
11247c478bd9Sstevel@tonic-gate 	uvp->uv_threshfiles = (int)temp;
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	ltmp = ((long long)clp->cl_maxfiles * MAXBSIZE);
11277c478bd9Sstevel@tonic-gate 	uvp->uv_maxfilesize = (ltmp + (MAXBSIZE / 2)) / (1024 * 1024);
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	xx = ((double)clp->cl_blkhiwat / clp->cl_maxblks) * 100. + .5;
11307c478bd9Sstevel@tonic-gate 	BOUND(xx);
11317c478bd9Sstevel@tonic-gate 	uvp->uv_hiblocks = xx;
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	xx = ((double)clp->cl_blklowat / clp->cl_maxblks) * 100. + .5;
11347c478bd9Sstevel@tonic-gate 	BOUND(xx);
11357c478bd9Sstevel@tonic-gate 	uvp->uv_lowblocks = xx;
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	xx = ((double)clp->cl_filehiwat / clp->cl_maxinodes) * 100. + .5;
11387c478bd9Sstevel@tonic-gate 	BOUND(xx);
11397c478bd9Sstevel@tonic-gate 	uvp->uv_hifiles = xx;
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	xx = ((double)clp->cl_filelowat / clp->cl_maxinodes) * 100. + .5;
11427c478bd9Sstevel@tonic-gate 	BOUND(xx);
11437c478bd9Sstevel@tonic-gate 	uvp->uv_lowfiles = xx;
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	/* return success */
11467c478bd9Sstevel@tonic-gate 	return (0);
11477c478bd9Sstevel@tonic-gate }
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate /*
11507c478bd9Sstevel@tonic-gate  * cachefs_file_to_dir
11517c478bd9Sstevel@tonic-gate  *
11527c478bd9Sstevel@tonic-gate  * takes in a path, and returns the parent directory of that path.
11537c478bd9Sstevel@tonic-gate  *
11547c478bd9Sstevel@tonic-gate  * it's the caller's responsibility to free the pointer returned by
11557c478bd9Sstevel@tonic-gate  * this function.
11567c478bd9Sstevel@tonic-gate  */
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate char *
cachefs_file_to_dir(const char * path)11597c478bd9Sstevel@tonic-gate cachefs_file_to_dir(const char *path)
11607c478bd9Sstevel@tonic-gate {
11617c478bd9Sstevel@tonic-gate 	char *rc, *cp;
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	if (path == NULL)
11647c478bd9Sstevel@tonic-gate 		return (NULL);
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	rc = strdup(path);
11677c478bd9Sstevel@tonic-gate 	if (rc == NULL)
11687c478bd9Sstevel@tonic-gate 		return (NULL);
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	if ((cp = strrchr(rc, '/')) == NULL) {
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 		/*
11737c478bd9Sstevel@tonic-gate 		 * if no slashes at all, return "." (current directory).
11747c478bd9Sstevel@tonic-gate 		 */
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 		(void) free(rc);
11777c478bd9Sstevel@tonic-gate 		rc = strdup(".");
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	} else if (cp == rc) {
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 		/*
11827c478bd9Sstevel@tonic-gate 		 * else, if the last '/' is the first character, chop
11837c478bd9Sstevel@tonic-gate 		 * off from there (i.e. return "/").
11847c478bd9Sstevel@tonic-gate 		 */
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 		rc[1] = '\0';
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	} else {
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 		/*
11917c478bd9Sstevel@tonic-gate 		 * else, we have a path like /foo/bar or foo/bar.
11927c478bd9Sstevel@tonic-gate 		 * chop off from the last '/'.
11937c478bd9Sstevel@tonic-gate 		 */
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 		*cp = '\0';
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	}
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	return (rc);
12007c478bd9Sstevel@tonic-gate }
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate /*
12037c478bd9Sstevel@tonic-gate  *			cachefs_clean_flag_test
12047c478bd9Sstevel@tonic-gate  *
12057c478bd9Sstevel@tonic-gate  * Description:
12067c478bd9Sstevel@tonic-gate  *	Tests whether or not the clean flag on the file system
12077c478bd9Sstevel@tonic-gate  *	is set.
12087c478bd9Sstevel@tonic-gate  * Arguments:
12097c478bd9Sstevel@tonic-gate  *	cachedirp	name of the the file system cache directory
12107c478bd9Sstevel@tonic-gate  * Returns:
12117c478bd9Sstevel@tonic-gate  *	Returns 1 if the cache was shut down cleanly, 0 if not.
12127c478bd9Sstevel@tonic-gate  * Preconditions:
12137c478bd9Sstevel@tonic-gate  *	precond(cachedirp)
12147c478bd9Sstevel@tonic-gate  */
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate int
cachefs_clean_flag_test(const char * cachedirp)12177c478bd9Sstevel@tonic-gate cachefs_clean_flag_test(const char *cachedirp)
12187c478bd9Sstevel@tonic-gate {
12197c478bd9Sstevel@tonic-gate 	char *namep;
12207c478bd9Sstevel@tonic-gate 	int xx;
12217c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
12227c478bd9Sstevel@tonic-gate 	int fd;
12237c478bd9Sstevel@tonic-gate 	struct cache_usage cu;
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	/* construct the path name of the resource file */
12267c478bd9Sstevel@tonic-gate 	namep = RESOURCE_NAME;
12277c478bd9Sstevel@tonic-gate 	xx = strlen(cachedirp) + strlen(namep) + 3;
12287c478bd9Sstevel@tonic-gate 	if (xx >= MAXPATHLEN) {
12297c478bd9Sstevel@tonic-gate 		pr_err(gettext("Path name too long %s/%s"),
12307c478bd9Sstevel@tonic-gate 		    cachedirp, namep);
12317c478bd9Sstevel@tonic-gate 		return (39);
12327c478bd9Sstevel@tonic-gate 	}
12337c478bd9Sstevel@tonic-gate 	snprintf(buf, sizeof (buf), "%s/%s", cachedirp, namep);
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	/* open the file; it will be <2GB */
12367c478bd9Sstevel@tonic-gate 	fd = open(buf, O_RDONLY);
12377c478bd9Sstevel@tonic-gate 	if (fd == -1) {
12387c478bd9Sstevel@tonic-gate 		pr_err(gettext("Cannot open %s: %s"), buf, strerror(errno));
12397c478bd9Sstevel@tonic-gate 		return (0);
12407c478bd9Sstevel@tonic-gate 	}
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 	/* read the cache_usage structure */
12437c478bd9Sstevel@tonic-gate 	xx = read(fd, &cu, sizeof (cu));
12447c478bd9Sstevel@tonic-gate 	if (xx != sizeof (cu)) {
12457c478bd9Sstevel@tonic-gate 		pr_err(gettext("Error reading %s: %d %s"), buf,
12467c478bd9Sstevel@tonic-gate 		    xx, strerror(errno));
12477c478bd9Sstevel@tonic-gate 		close(fd);
12487c478bd9Sstevel@tonic-gate 		return (0);
12497c478bd9Sstevel@tonic-gate 	}
12507c478bd9Sstevel@tonic-gate 	close(fd);
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	/* return state of the cache */
12537c478bd9Sstevel@tonic-gate 	return ((cu.cu_flags & CUSAGE_ACTIVE) == 0);
12547c478bd9Sstevel@tonic-gate }
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate time32_t
get_boottime()12577c478bd9Sstevel@tonic-gate get_boottime()
12587c478bd9Sstevel@tonic-gate {
12597c478bd9Sstevel@tonic-gate 	struct utmpx id, *putmp;
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	id.ut_type = BOOT_TIME;
12627c478bd9Sstevel@tonic-gate 	setutxent();
12637c478bd9Sstevel@tonic-gate 	if ((putmp = getutxid(&id)) != NULL)
12647c478bd9Sstevel@tonic-gate 		return ((time32_t)putmp->ut_tv.tv_sec);
12657c478bd9Sstevel@tonic-gate 	return (-1);
12667c478bd9Sstevel@tonic-gate }
1267