main.c revision 8711765099ae73ec6eb3cd5d1134031d8f11d4f7
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* lofiadm - administer lofi(7d). Very simple, add and remove file<->device
* associations, and display status. All the ioctls are private between
* lofi and lofiadm, and so are very simple - device information is
* communicated via a minor number.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <fcntl.h>
#include <locale.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <stropts.h>
#include <libdevinfo.h>
#include <libgen.h>
#include <ctype.h>
#include <zlib.h>
#include "utils.h"
static const char USAGE[] =
"Usage: %s -a file [ device ]\n"
" %s -d file | device \n"
" %s -C [algorithm] [-s segment_size] file \n"
" %s -U file \n"
" %s [ device | file ]\n";
static const char *pname;
static int addflag = 0;
static int deleteflag = 0;
static int errflag = 0;
static int compressflag = 0;
static int uncompressflag = 0;
};
#define FORMAT "%-20s %-30s %s\n"
#define NONE "-"
#define COMPRESS "Compressed"
#define COMPRESS_ALGORITHM "gzip"
#define COMPRESS_THRESHOLD 2048
#define SEGSIZE 131072
#define BLOCK_SIZE 512
#define KILOBYTE 1024
{
return (-1);
return (0);
}
/*
* Print the list of all the mappings. Including a header.
*/
static void
print_mappings(int fd)
{
struct lofi_ioctl li;
int minor;
int maxminor;
char path[MAXPATHLEN];
char options[MAXPATHLEN];
perror("ioctl");
}
continue;
perror("ioctl");
break;
}
else
}
}
static void
usage(void)
{
}
/*
* Translate a lofi device name to a minor number. We might be asked
* to do this when there is no association (such as when the user specifies
* a particular device), so we can only look at the string.
*/
static int
name_to_minor(const char *devicename)
{
int minor;
return (minor);
}
return (minor);
}
return (0);
}
/*
* This might be the first time we've used this minor number. If so,
* it might also be that the /dev links are in the process of being created
* by devfsadmd (or that they'll be created "soon"). We cannot return
* until they're there or the invoker of lofiadm might try to use them
* and not find them. This can happen if a shell script is running on
* an MP.
*/
static void
{
int cursleep;
char blkpath[MAXPATHLEN];
char charpath[MAXPATHLEN];
/* Check if links already present */
return;
/* First use di_devlink_init() */
(void) di_devlink_fini(&hdl);
goto out;
}
/*
* Under normal conditions, di_devlink_init(DI_MAKE_LINK) above will
* only fail if the caller is non-root. In that case, wait for
* link creation via sysevents.
*/
cursleep = 0;
continue;
}
return;
}
/* one last try */
out:
}
}
}
/*
* Add a device association. If devicename is NULL, let the driver
* pick a device.
*/
static void
int *minor_created, int suppress)
{
struct lofi_ioctl li;
int minor;
if (devicename == NULL) {
/* pick one */
sizeof (li.li_filename));
if (minor == -1) {
}
/* print one picked */
if (!suppress)
/* fill in the minor if needed */
if (minor_created != NULL) {
*minor_created = minor;
}
return;
}
/* use device we were given */
if (minor == 0) {
}
}
}
/*
* Remove an association. Delete by device name if non-NULL, or by
* filename otherwise.
*/
static void
{
struct lofi_ioctl li;
if (devicename == NULL) {
/* delete by filename */
sizeof (li.li_filename));
}
return;
}
/* delete by device */
}
}
}
static void
{
struct lofi_ioctl li;
if (devicename == NULL) {
/* given filename, print devicename */
sizeof (li.li_filename));
}
return;
}
/* given devicename, print filename */
}
}
}
/*
* Uncompress a file.
*
* First map the file in to establish a device
* association, then read from it. On-the-fly
* decompression will automatically uncompress
* the file if it's compressed
*
* If the file is mapped and a device association
* has been established, disallow uncompressing
* the file until it is unmapped.
*/
static void
{
struct lofi_ioctl li;
char devicename[32];
char tmpfilename[MAXPATHLEN];
int minor = 0;
int compfd = -1;
int uncompfd = -1;
/*
* Disallow uncompressing the file if it is
* already mapped.
*/
filename);
/* Zero length files don't need to be uncompressed */
return;
/* If the file isn't compressed, we just return */
return;
}
}
/* Create a temp file in the same directory */
return;
}
/*
* Set the mode bits and the owner of this temporary
* file to be that of the original uncompressed file
*/
return;
}
/* Now read from the device in MAXBSIZE-sized chunks */
for (;;) {
if (rbytes <= 0)
break;
rbytes = -1;
break;
}
}
/* Delete the mapping */
/*
* If an error occured while reading or writing, rbytes will
* be negative
*/
if (rbytes < 0) {
(void) unlink(tmpfilename);
}
/* Rename the temp file to the actual file */
(void) unlink(tmpfilename);
}
/*
* Compress a file
*/
static void
{
struct lofi_ioctl lic;
char tmpfilename[MAXPATHLEN];
char comp_filename[MAXPATHLEN];
int tfd = -1;
int i, type;
/*
* Disallow compressing the file if it is
* already mapped
*/
filename);
/*
* The size of the buffer to hold compressed data must
* be slightly larger than the compressed segment size.
*
* The compress functions use part of the buffer as
* scratch space to do calculations.
* Ref: http://www.zlib.net/manual.html#compress2
*/
}
/* Zero length files don't need to be compressed */
return;
}
/*
* Create temporary files in the same directory that
* will hold the intermediate data
*/
goto cleanup;
goto cleanup;
/*
* Set the mode bits and owner of the compressed
* file to be that of the original uncompressed file
*/
goto cleanup;
/*
* Calculate the number of index entries required.
* index entries are stored as an array. adding
* a '2' here accounts for the fact that the last
* segment may not be a multiple of the segment size
*/
goto cleanup;
offset = 0;
count = 0;
/*
* Now read from the uncompressed file in 'segsize'
* sized chunks, compress what was read in and
* write it out to a temporary file
*/
for (;;) {
if (rbytes <= 0)
break;
goto cleanup;
/*
* Account for the first byte that
* indicates whether a segment is
* compressed or not
*/
/*
* If the length of the compressed data is more
* than a threshold then there isn't any benefit
* to be had from compressing this segment - leave
* it uncompressed.
*
* NB. In case an error occurs during compression (above)
* the 'real_segsize' isn't changed. The logic below
* ensures that that segment is left uncompressed.
*/
rbytes);
type = UNCOMPRESSED;
} else {
type = COMPRESSED;
}
/*
* Set the first byte or the SEGHDR to
* indicate if it's compressed or not
*/
*compressed_seg = type;
rbytes = -1;
break;
}
count++;
}
if (rbytes < 0)
goto cleanup;
/*
* The last index entry is a sentinel entry. It does not point to
* an actual compressed segment but helps in computing the size of
* the compressed segment. The size of each compressed segment is
* computed by subtracting the current index value from the next
* one (the compressed blocks are stored sequentially)
*/
/*
* Now write the compressed data along with the
* header information to this file which will
* later be renamed to the original uncompressed
* file name
*
* The header is as follows -
*
* Signature (name of the compression algorithm)
* Compression segment size (a multiple of 512)
* Number of index entries
* Size of the last block
* The array containing the index entries
*
* the header is always stored in network byte
* order
*/
!= sizeof (algorithm))
goto cleanup;
goto cleanup;
sizeof (index_entries))
goto cleanup;
goto cleanup;
for (i = 0; i < count; i++) {
sizeof (*index))
goto cleanup;
}
/* Header is written, now write the compressed data */
goto cleanup;
for (;;) {
if (rbytes <= 0)
break;
goto cleanup;
}
goto cleanup;
/*
* Round up the compressed file size to be a multiple of
* DEV_BSIZE. lofi(7D) likes it that way.
*/
for (i = 0; i < offset; i++)
uncompressed_seg[i] = '\0';
goto cleanup;
}
(void) unlink(tmpfilename);
if (rbytes < 0) {
if (tfd != -1)
(void) unlink(tmpfilename);
if (compfd != -1)
(void) unlink(comp_filename);
} else {
/* Rename the compressed file to the actual file */
(void) unlink(comp_filename);
}
}
if (compressed_seg != NULL)
if (uncompressed_seg != NULL)
if (compfd != -1)
if (uncompfd != -1)
if (tfd != -1)
}
static int
lofi_compress_select(const char *algname)
{
int i;
for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) {
return (i);
}
return (-1);
}
static void
{
if (*compress_index < 0)
}
static void
check_file_validity(const char *filename)
{
int error;
int fd = -1;
if (fd == -1) {
}
if (error == -1) {
"block, or character device\n"),
filename);
"of %d\n"),
}
if (name_to_minor(filename) != 0) {
" on itself\n"), NULL);
}
}
static uint32_t
convert_to_num(const char *str)
{
int len;
case 'k':
case 'K':
break;
case 'b':
case 'B':
mult = BLOCK_SIZE;
break;
case 'm':
case 'M':
break;
case 'g':
case 'G':
break;
default:
}
}
return (segsize);
}
int
{
int lfd;
int c;
const char *devicename = NULL;
const char *algname = COMPRESS_ALGORITHM;
int openflag;
int minor;
int compress_index;
(void) textdomain(TEXT_DOMAIN);
switch (c) {
case 'a':
addflag = 1;
/* optional device */
optind++;
}
break;
case 'C':
compressflag = 1;
(*optarg == '-')) {
optind--;
break;
optind++;
break;
} else {
}
break;
case 'd':
deleteflag = 1;
if (minor != 0)
devicename = optarg;
else
break;
case 'f':
break;
case 's':
"or not a multiple of minimum block "
optind++;
break;
case 'U':
uncompressflag = 1;
break;
case '?':
default:
errflag = 1;
break;
}
}
if (errflag ||
(addflag && deleteflag) ||
usage();
case 0: /* no more args */
break;
case 1: /* one arg without options means print the association */
if (addflag || deleteflag)
usage();
if (compressflag || uncompressflag)
usage();
if (minor != 0)
else
break;
default:
usage();
break;
}
/*
* Here, we know the arguments are correct, the filename is an
* absolute path, it exists and is a regular file. We don't yet
* know that the device name is ok or not.
*/
/*
* Now to the real work.
*/
else
if (lfd == -1) {
die("you do not have permission to perform "
"that operation.\n");
} else {
}
/*NOTREACHED*/
}
if (addflag)
else if (compressflag)
else if (uncompressflag)
else if (deleteflag)
else if (filename || devicename)
else
return (E_SUCCESS);
}