xref: /onnv-gate/usr/src/uts/common/sys/lofi.h (revision 8081:cb8ad0d5637f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54451Seschrock  * Common Development and Distribution License (the "License").
64451Seschrock  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
226734Sjohnlev  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #ifndef	_SYS_LOFI_H
280Sstevel@tonic-gate #define	_SYS_LOFI_H
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/time.h>
320Sstevel@tonic-gate #include <sys/taskq.h>
330Sstevel@tonic-gate #include <sys/vtoc.h>
340Sstevel@tonic-gate #include <sys/dkio.h>
350Sstevel@tonic-gate #include <sys/vnode.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #ifdef	__cplusplus
380Sstevel@tonic-gate extern "C" {
390Sstevel@tonic-gate #endif
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * /dev names:
430Sstevel@tonic-gate  *	/dev/lofictl	- master control device
440Sstevel@tonic-gate  *	/dev/lofi	- block devices, named by minor number
450Sstevel@tonic-gate  *	/dev/rlofi	- character devices, named by minor number
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate #define	LOFI_DRIVER_NAME	"lofi"
480Sstevel@tonic-gate #define	LOFI_CTL_NODE		"ctl"
490Sstevel@tonic-gate #define	LOFI_CTL_NAME		LOFI_DRIVER_NAME LOFI_CTL_NODE
500Sstevel@tonic-gate #define	LOFI_BLOCK_NAME		LOFI_DRIVER_NAME
510Sstevel@tonic-gate #define	LOFI_CHAR_NAME		"r" LOFI_DRIVER_NAME
520Sstevel@tonic-gate 
535643Saalok #define	SEGHDR		1
545643Saalok #define	COMPRESSED	1
555643Saalok #define	UNCOMPRESSED	0
565643Saalok #define	MAXALGLEN	36
575643Saalok 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  *
600Sstevel@tonic-gate  * Use is:
610Sstevel@tonic-gate  *	ld = open("/dev/lofictl", O_RDWR | O_EXCL);
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  * lofi must be opened exclusively. Access is controlled by permissions on
640Sstevel@tonic-gate  * the device, which is 644 by default. Write-access is required for ioctls
650Sstevel@tonic-gate  * that change state, but only read-access is required for the ioctls that
660Sstevel@tonic-gate  * return information. Basically, only root can add and remove files, but
670Sstevel@tonic-gate  * non-root can look at the current lists.
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * ioctl usage:
700Sstevel@tonic-gate  *
710Sstevel@tonic-gate  * kernel ioctls
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  *	strcpy(li.li_filename, "somefilename");
740Sstevel@tonic-gate  *	ioctl(ld, LOFI_MAP_FILE, &li);
750Sstevel@tonic-gate  *	newminor = li.li_minor;
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  *	strcpy(li.li_filename, "somefilename");
780Sstevel@tonic-gate  *	ioctl(ld, LOFI_UNMAP_FILE, &li);
790Sstevel@tonic-gate  *
800Sstevel@tonic-gate  *	strcpy(li.li_filename, "somefilename");
810Sstevel@tonic-gate  *	li.li_minor = minor_number;
820Sstevel@tonic-gate  *	ioctl(ld, LOFI_MAP_FILE_MINOR, &li);
830Sstevel@tonic-gate  *
840Sstevel@tonic-gate  *	li.li_minor = minor_number;
850Sstevel@tonic-gate  *	ioctl(ld, LOFI_UNMAP_FILE_MINOR, &li);
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  *	li.li_minor = minor_number;
880Sstevel@tonic-gate  *	ioctl(ld, LOFI_GET_FILENAME, &li);
890Sstevel@tonic-gate  *
900Sstevel@tonic-gate  *	strcpy(li.li_filename, "somefilename");
910Sstevel@tonic-gate  *	ioctl(ld, LOFI_GET_MINOR, &li);
920Sstevel@tonic-gate  *
930Sstevel@tonic-gate  *	li.li_minor = 0;
940Sstevel@tonic-gate  *	ioctl(ld, LOFI_GET_MAXMINOR, &li);
950Sstevel@tonic-gate  *	maxminor = li.li_minor;
960Sstevel@tonic-gate  *
975643Saalok  *	strcpy(li.li_filename, "somefilename");
985643Saalok  *	li.li_minor = 0;
995643Saalok  *	ioctl(ld, LOFI_CHECK_COMPRESSED, &li);
1005643Saalok  *
1014451Seschrock  * If the 'li_force' flag is set for any of the LOFI_UNMAP_* commands, then if
1024451Seschrock  * the device is busy, the underlying vnode will be closed, and any subsequent
1034451Seschrock  * operations will fail.  It will behave as if the device had been forcibly
1044451Seschrock  * removed, so the DKIOCSTATE ioctl will return DKIO_DEV_GONE.  When the device
1054451Seschrock  * is last closed, it will be torn down.
1064451Seschrock  *
1076734Sjohnlev  * If the 'li_cleanup' flag is set for any of the LOFI_UNMAP_* commands, then
1086734Sjohnlev  * if the device is busy, it is marked for removal at the next time it is
1096734Sjohnlev  * no longer held open by anybody.  When the device is last closed, it will be
1106734Sjohnlev  * torn down.
1116734Sjohnlev  *
1120Sstevel@tonic-gate  * Oh, and last but not least: these ioctls are totally private and only
1130Sstevel@tonic-gate  * for use by lofiadm(1M).
1140Sstevel@tonic-gate  *
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate struct lofi_ioctl {
1184451Seschrock 	uint32_t 	li_minor;
1194451Seschrock 	boolean_t	li_force;
1206734Sjohnlev 	boolean_t	li_cleanup;
121*8081SDina.Nimeh@Sun.Com 	char	li_filename[MAXPATHLEN];
1225643Saalok 	char	li_algorithm[MAXALGLEN];
1230Sstevel@tonic-gate };
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate #define	LOFI_IOC_BASE		(('L' << 16) | ('F' << 8))
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate #define	LOFI_MAP_FILE		(LOFI_IOC_BASE | 0x01)
1280Sstevel@tonic-gate #define	LOFI_MAP_FILE_MINOR	(LOFI_IOC_BASE | 0x02)
1290Sstevel@tonic-gate #define	LOFI_UNMAP_FILE		(LOFI_IOC_BASE | 0x03)
1300Sstevel@tonic-gate #define	LOFI_UNMAP_FILE_MINOR	(LOFI_IOC_BASE | 0x04)
1310Sstevel@tonic-gate #define	LOFI_GET_FILENAME	(LOFI_IOC_BASE | 0x05)
1320Sstevel@tonic-gate #define	LOFI_GET_MINOR		(LOFI_IOC_BASE | 0x06)
1330Sstevel@tonic-gate #define	LOFI_GET_MAXMINOR	(LOFI_IOC_BASE | 0x07)
1345643Saalok #define	LOFI_CHECK_COMPRESSED	(LOFI_IOC_BASE | 0x08)
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate  * file types that might be usable with lofi, maybe. Only regular
1380Sstevel@tonic-gate  * files are documented though.
1390Sstevel@tonic-gate  */
1400Sstevel@tonic-gate #define	S_ISLOFIABLE(mode) \
1410Sstevel@tonic-gate 	(S_ISREG(mode) || S_ISBLK(mode) || S_ISCHR(mode))
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate #if defined(_KERNEL)
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate  * We limit the maximum number of active lofi devices to 128, which seems very
1470Sstevel@tonic-gate  * large. You can tune this by changing lofi_max_files in /etc/system.
1480Sstevel@tonic-gate  * If you change it dynamically, which you probably shouldn't do, make sure
1490Sstevel@tonic-gate  * to only _increase_ it.
1500Sstevel@tonic-gate  */
1510Sstevel@tonic-gate #define	LOFI_MAX_FILES	128
1520Sstevel@tonic-gate extern uint32_t lofi_max_files;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate #define	V_ISLOFIABLE(vtype) \
1550Sstevel@tonic-gate 	((vtype == VREG) || (vtype == VBLK) || (vtype == VCHR))
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate struct lofi_state {
1584451Seschrock 	char		*ls_filename;	/* filename to open */
1594451Seschrock 	size_t		ls_filename_sz;
1604451Seschrock 	struct vnode	*ls_vp;		/* open vnode */
1614451Seschrock 	kmutex_t	ls_vp_lock;	/* protects ls_vp */
1624451Seschrock 	kcondvar_t	ls_vp_cv;	/* signal changes to ls_vp */
1634451Seschrock 	uint32_t	ls_vp_iocount;	/* # pending I/O requests */
1644451Seschrock 	boolean_t	ls_vp_closereq;	/* force close requested */
1650Sstevel@tonic-gate 	u_offset_t	ls_vp_size;
1660Sstevel@tonic-gate 	uint32_t	ls_blk_open;
1670Sstevel@tonic-gate 	uint32_t	ls_chr_open;
1680Sstevel@tonic-gate 	uint32_t	ls_lyr_open_count;
1690Sstevel@tonic-gate 	int		ls_openflag;
1706734Sjohnlev 	boolean_t	ls_cleanup;	/* cleanup on close */
1710Sstevel@tonic-gate 	taskq_t		*ls_taskq;
1720Sstevel@tonic-gate 	kstat_t		*ls_kstat;
1730Sstevel@tonic-gate 	kmutex_t	ls_kstat_lock;
1740Sstevel@tonic-gate 	struct dk_geom	ls_dkg;
1750Sstevel@tonic-gate 	struct vtoc	ls_vtoc;
1760Sstevel@tonic-gate 	struct dk_cinfo	ls_ci;
1775643Saalok 
1785643Saalok 	/* the following fields are required for compression support */
1795643Saalok 	int		ls_comp_algorithm_index; /* idx into compress_table */
1805643Saalok 	char		ls_comp_algorithm[MAXALGLEN];
1815643Saalok 	uint32_t	ls_uncomp_seg_sz; /* sz of uncompressed segment */
1825643Saalok 	uint32_t	ls_comp_index_sz; /* number of index entries */
1835643Saalok 	uint32_t	ls_comp_seg_shift; /* exponent for byte shift */
1845643Saalok 	uint32_t	ls_uncomp_last_seg_sz; /* sz of last uncomp segment */
1855643Saalok 	uint64_t	ls_comp_offbase; /* offset of actual compressed data */
1865643Saalok 	uint64_t	*ls_comp_seg_index; /* array of index entries */
1875643Saalok 	caddr_t		ls_comp_index_data; /* index pages loaded from file */
1885643Saalok 	uint32_t	ls_comp_index_data_sz;
1895643Saalok 	u_offset_t	ls_vp_comp_size; /* actual compressed file size */
1900Sstevel@tonic-gate };
1910Sstevel@tonic-gate 
1925643Saalok #endif	/* _KERNEL */
1935643Saalok 
1945643Saalok /*
1955643Saalok  * Common signature for all lofi compress functions
1965643Saalok  */
1975643Saalok typedef int lofi_compress_func_t(void *src, size_t srclen, void *dst,
1985643Saalok 	size_t *destlen, int level);
1995643Saalok 
2005643Saalok /*
2015643Saalok  * Information about each compression function
2025643Saalok  */
2035643Saalok typedef struct lofi_compress_info {
2045643Saalok 	lofi_compress_func_t	*l_decompress;
2055643Saalok 	lofi_compress_func_t	*l_compress;
2065643Saalok 	int			l_level;
2075643Saalok 	char			*l_name;	/* algorithm name */
2085643Saalok } lofi_compress_info_t;
2095643Saalok 
2105643Saalok enum lofi_compress {
2115643Saalok 	LOFI_COMPRESS_GZIP = 0,
2125643Saalok 	LOFI_COMPRESS_GZIP_6 = 1,
2135643Saalok 	LOFI_COMPRESS_GZIP_9 = 2,
2145643Saalok 	LOFI_COMPRESS_FUNCTIONS
2155643Saalok };
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate #ifdef	__cplusplus
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate #endif
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate #endif	/* _SYS_LOFI_H */
222