subr.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Common subroutines used by the programs in these subdirectories.
*/
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <wait.h>
#include <ctype.h>
#include <fcntl.h>
#include <ftw.h>
#include <dirent.h>
#include <utmpx.h>
#include "subr.h"
/*
*
* cachefs_dir_lock
*
* Description:
* Gets a lock on the cache directory.
* To release the lock, call cachefs_dir_unlock
* with the returned value.
* Arguments:
* cachedirp name of the cache directory
* shared 1 if shared, 0 if not
* Returns:
* Returns -1 if the lock cannot be obtained immediatly.
* If the lock is obtained, returns a value >= 0.
* Preconditions:
* precond(cachedirp)
*/
int
{
int fd;
int xx;
int len;
char buf[MAXPATHLEN];
char *strp;
/* make a path prefix to the cache directory lock file */
strp = "/tmp";
/* won't overflow */
return (-1);
}
*strp = '_';
}
/*
* Create and open the cache directory lock file.
* This file will be <2G.
*/
if (fd == -1) {
return (-1);
}
/* try to set the lock */
/* CACHEFS_LOCK_FILE will be <2GB */
if (xx == -1) {
"cache directory %s."), cachedirp);
} else {
}
return (-1);
}
/* return the file descriptor which can be used to release the lock */
return (fd);
}
/*
*
* cachefs_dir_unlock
*
* Description:
* Releases an advisory lock on the cache directory.
* Arguments:
* fd cookie returned by cachefs_dir_lock
* Returns:
* Returns -1 if the lock cannot be released or 0 for success.
* Preconditions:
*/
int
cachefs_dir_unlock(int fd)
{
int error = 0;
int xx;
/* release the lock */
/* fd will be <2GB */
if (xx == -1) {
error = -1;
}
/* close the lock file */
return (error);
}
/*
*
* cachefs_label_file_get
*
* Description:
* Gets the contents of a cache label file.
* Performs error checking on the file.
* Arguments:
* filep name of the cache label file
* clabelp where to put the file contents
* Returns:
* Returns 0 for success or -1 if an error occurs.
* Preconditions:
* precond(filep)
* precond(clabelp)
*/
int
{
int xx;
int fd;
/* get info on the file */
if (xx == -1) {
} else {
}
return (-1);
}
/* if the file is the wrong type */
return (-1);
}
/* if the file is the wrong size; it will be <2GB */
return (-1);
}
/* open the cache label file */
if (fd == -1) {
return (-1);
}
/* read the current set of parameters */
if (xx != sizeof (struct cache_label)) {
return (-1);
}
/* return success */
return (0);
}
/*
*
* cachefs_label_file_put
*
* Description:
* Outputs the contents of a cache label object to a file.
* Arguments:
* filep name of the cache label file
* clabelp where to get the file contents
* Returns:
* Returns 0 for success or -1 if an error occurs.
* Preconditions:
* precond(filep)
* precond(clabelp)
*/
int
{
int xx;
int fd;
/* get rid of the file if it already exists */
return (-1);
}
/* open the cache label file; this file will be <2GB */
if (fd == -1) {
return (-1);
}
/* write out the cache label object */
if (xx != sizeof (struct cache_label)) {
return (-1);
}
/* make sure the contents get to disk */
return (-1);
}
/* return success */
return (0);
}
int
{
/* check for an invalid version number */
return (-1);
}
return (0);
}
/*
*
* cachefs_inuse
*
* Description:
* Tests whether or not the cache directory is in use by
* the cache file system.
* Arguments:
* cachedirp name of the file system cache directory
* Returns:
* Returns 1 if the cache is in use or an error, 0 if not.
* Preconditions:
* precond(cachedirp)
*/
int
cachefs_inuse(const char *cachedirp)
{
int fd;
int xx;
char buf[MAXPATHLEN];
char *lockp = CACHEFS_LOCK_FILE;
/* see if path name is too long */
if (xx >= MAXPATHLEN) {
return (1);
}
/* make a path to the cache directory lock file */
/* Open the kernel in use lock file. This file will be <2GB. */
if (fd == -1) {
return (1);
}
/* test the lock status */
if (xx == -1) {
return (1);
}
xx = 0;
else
xx = 1;
/* return whether or not the cache is in use */
return (xx);
}
/*
*
* cachefs_resouce_size
*
* Description:
* Returns information about a resource file.
* Arguments:
* maxinodes number of inodes to be managed by the resource file
* rinfop set to info about the resource file
* Returns:
* Preconditions:
* precond(rinfop)
*/
void
{
int fsize;
if ((maxinodes % CACHEFS_RLPMBS) != 0)
}
/*
*
* cachefs_create_cache
*
* Description:
* Creates the specified cache directory and populates it as
* needed by CFS.
* Arguments:
* dirp the name of the cache directory
* uv user values (may be NULL)
* clabel label file contents, or placeholder for this
* Returns:
* Returns 0 for success or:
* -1 for an error
* -2 for an error and cache directory partially created
* Preconditions:
* precond(dirp)
*/
int
struct cache_label *clabel)
{
int xx;
char path[CACHEFS_XMAXPATH];
int fd;
void *bufp;
int cnt;
struct cache_usage cu;
char *parent;
cu.cu_blksused = 0;
cu.cu_filesused = 0;
/* make sure cache dir name is not too long */
return (-1);
}
/* ensure the path isn't in cachefs */
return (-1);
}
return (-1);
}
return (-1);
}
/* make the directory */
switch (errno) {
case EEXIST:
break;
default:
}
return (-1);
}
/* convert user values to a cache_label */
if (xx)
return (-2);
}
/*
* Create the cache directory lock file.
* Used by the kernel module to indicate the cache is in use.
* This file will be <2G.
*/
if (fd == -1) {
return (-1);
}
/* make the directory for the back file system mount points */
/* note: we do not count this directory in the resources */
return (-2);
}
/* make the directory for lost+found */
/* note: we do not count this directory in the resources */
return (-2);
}
/* make the networker "don't back up" file; this file is <2GB */
xx = 0;
xx = 1;
}
}
if (xx == 0) {
} else {
}
/* create the unmount file */
xx = 0;
btime = get_boottime();
xx = 1;
}
if (xx == 0)
/* create the cache label file */
if (xx == -1) {
return (-2);
}
/* create the cache label duplicate file */
if (xx == -1) {
return (-2);
}
/* create the resouce file; this file will be <2GB */
if (fd == -1) {
return (-2);
}
/* allocate a zeroed buffer for filling the resouce file */
return (-2);
}
/* determine number of MAXBSIZE chunks to make the file */
++cnt;
/* fill up the file with zeros */
return (-2);
}
}
/* position to the begining of the file */
return (-2);
}
/* write the cache usage structure */
xx = sizeof (struct cache_usage);
return (-2);
}
/* make sure the contents get to disk */
return (-2);
}
/* return success */
return (0);
}
/*
*
* cachefs_delete_all_cache
*
* Description:
* Delete all caches in cache directory.
* Arguments:
* dirp the pathname of of the cache directory to delete
* Returns:
* Returns 0 for success or -1 for an error.
* Preconditions:
* precond(dirp)
*/
int
cachefs_delete_all_cache(char *dirp)
{
int xx;
char path[CACHEFS_XMAXPATH];
/* make sure cache dir name is not too long */
dirp);
return (-1);
}
/* check that dirp is probably a cachefs directory */
if (xx) {
"cachefs cache directory."), dirp);
return (-1);
}
/* remove the lost+found directory if it exists and is empty */
if (xx == -1) {
"First move files in '%s' to a safe location."),
return (-1);
return (-1);
}
}
/* delete the back FS mount point directory if it exists */
if (xx == -1) {
return (-1);
}
} else {
if (xx == -1) {
return (-1);
}
}
/* open the cache directory specified */
return (-1);
}
/* read the file names in the cache directory */
/* ignore . and .. */
continue;
/* stat the file */
if (xx == -1) {
/* delete_cache may have nuked a directory */
continue;
}
return (-1);
}
/* ignore anything that is not a link */
continue;
/* delete the file system cache directory */
if (xx) {
return (-1);
}
}
/* remove the cache dir unmount file */
return (-1);
}
/* remove the cache label file */
return (-1);
}
/* remove the cache label duplicate file */
return (-1);
}
/* remove the resource file */
return (-1);
}
/* remove the cachefslog file if it exists */
/* remove the networker "don't back up" file if it exists */
/* remove the lock file */
return (-1);
}
/* remove the directory */
if (xx == -1) {
return (-1);
}
/* return success */
return (0);
}
/*
*
* cachefs_delete_cache
*
* Description:
* Deletes the specified file system cache.
* Arguments:
* dirp cache directory name
* namep file system cache directory to delete
* Returns:
* Returns 0 for success, -1 for failure.
* Preconditions:
* precond(dirp)
* precond(namep)
*/
int
{
char path[CACHEFS_XMAXPATH];
char buf[CACHEFS_XMAXPATH];
int xx;
/* make sure cache dir name is not too long */
dirp);
return (-1);
}
/* construct the path name of the file system cache directory */
/* stat the specified file */
if (xx == -1) {
return (-1);
}
/* make sure name is a symbolic link */
return (-1);
}
/* read the contents of the symbolic link */
if (xx == -1) {
return (-1);
}
/* remove the directory */
if (xx == -1) {
return (-1);
}
/* delete the link */
return (-1);
}
/* return success */
return (0);
}
/*
*
* cachefs_delete_file
*
* Description:
* Remove a file or directory; called by nftw64().
* Arguments:
* namep pathname of the file
* statp stat info about the file
* flg info about file
* ftwp depth information
* Returns:
* Returns 0 for success, -1 for failure.
* Preconditions:
* precond(namep)
*/
int
{
/* ignore . and .. */
return (0);
switch (flg) {
case FTW_F: /* files */
case FTW_SL:
return (-1);
}
break;
case FTW_DP: /* directories that have their children processed */
return (-1);
}
break;
case FTW_D: /* ignore directories if children not processed */
break;
default:
return (-1);
}
/* return success */
return (0);
}
/*
*
* cachefs_convert_uv2cl
*
* Description:
* Copies the contents of a cachefs_user_values object into a
* cache_label object, performing the necessary conversions.
* Arguments:
* uvp cachefs_user_values to copy from
* clp cache_label to copy into
* dirp cache directory
* Returns:
* Returns 0 for success, -1 for an error.
* Preconditions:
* precond(uvp)
* precond(clp)
* precond(dirp)
*/
int
{
int xx;
double ftmp;
double temp;
/* get file system information */
if (xx == -1) {
return (-1);
}
/* front fs is less than 1 terabyte */
/* return success */
return (0);
}
/*
*
* cachefs_convert_cl2uv
*
* Description:
* Copies the contents of a cache_label object into a
* cachefs_user_values object, performing the necessary conversions.
* Arguments:
* clp cache_label to copy from
* uvp cachefs_user_values to copy into
* dirp cache directory
* Returns:
* Returns 0 for success, -1 for an error.
* Preconditions:
* precond(clp)
* precond(uvp)
* precond(dirp)
*/
int
{
int xx;
double temp;
double ftmp;
long long ltmp;
/* get file system information */
if (xx == -1) {
return (-1);
}
/* return success */
return (0);
}
/*
* cachefs_file_to_dir
*
* takes in a path, and returns the parent directory of that path.
*
* it's the caller's responsibility to free the pointer returned by
* this function.
*/
char *
cachefs_file_to_dir(const char *path)
{
return (NULL);
return (NULL);
/*
* if no slashes at all, return "." (current directory).
*/
/*
* else, if the last '/' is the first character, chop
* off from there (i.e. return "/").
*/
} else {
/*
* chop off from the last '/'.
*/
*cp = '\0';
}
return (rc);
}
/*
* cachefs_clean_flag_test
*
* Description:
* Tests whether or not the clean flag on the file system
* is set.
* Arguments:
* cachedirp name of the the file system cache directory
* Returns:
* Returns 1 if the cache was shut down cleanly, 0 if not.
* Preconditions:
* precond(cachedirp)
*/
int
cachefs_clean_flag_test(const char *cachedirp)
{
char *namep;
int xx;
char buf[MAXPATHLEN];
int fd;
struct cache_usage cu;
/* construct the path name of the resource file */
if (xx >= MAXPATHLEN) {
return (39);
}
/* open the file; it will be <2GB */
if (fd == -1) {
return (0);
}
/* read the cache_usage structure */
return (0);
}
/* return state of the cache */
}
{
setutxent();
return (-1);
}