xref: /titanic_52/usr/src/uts/common/sys/lofi.h (revision 87ba907dbdcf5c1692d70a9b22fe1d0e718d9896)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 
27 #ifndef	_SYS_LOFI_H
28 #define	_SYS_LOFI_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <sys/taskq.h>
35 #include <sys/vtoc.h>
36 #include <sys/dkio.h>
37 #include <sys/vnode.h>
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 /*
44  * /dev names:
45  *	/dev/lofictl	- master control device
46  *	/dev/lofi	- block devices, named by minor number
47  *	/dev/rlofi	- character devices, named by minor number
48  */
49 #define	LOFI_DRIVER_NAME	"lofi"
50 #define	LOFI_CTL_NODE		"ctl"
51 #define	LOFI_CTL_NAME		LOFI_DRIVER_NAME LOFI_CTL_NODE
52 #define	LOFI_BLOCK_NAME		LOFI_DRIVER_NAME
53 #define	LOFI_CHAR_NAME		"r" LOFI_DRIVER_NAME
54 
55 #define	SEGHDR		1
56 #define	COMPRESSED	1
57 #define	UNCOMPRESSED	0
58 #define	MAXALGLEN	36
59 
60 /*
61  *
62  * Use is:
63  *	ld = open("/dev/lofictl", O_RDWR | O_EXCL);
64  *
65  * lofi must be opened exclusively. Access is controlled by permissions on
66  * the device, which is 644 by default. Write-access is required for ioctls
67  * that change state, but only read-access is required for the ioctls that
68  * return information. Basically, only root can add and remove files, but
69  * non-root can look at the current lists.
70  *
71  * ioctl usage:
72  *
73  * kernel ioctls
74  *
75  *	strcpy(li.li_filename, "somefilename");
76  *	ioctl(ld, LOFI_MAP_FILE, &li);
77  *	newminor = li.li_minor;
78  *
79  *	strcpy(li.li_filename, "somefilename");
80  *	ioctl(ld, LOFI_UNMAP_FILE, &li);
81  *
82  *	strcpy(li.li_filename, "somefilename");
83  *	li.li_minor = minor_number;
84  *	ioctl(ld, LOFI_MAP_FILE_MINOR, &li);
85  *
86  *	li.li_minor = minor_number;
87  *	ioctl(ld, LOFI_UNMAP_FILE_MINOR, &li);
88  *
89  *	li.li_minor = minor_number;
90  *	ioctl(ld, LOFI_GET_FILENAME, &li);
91  *
92  *	strcpy(li.li_filename, "somefilename");
93  *	ioctl(ld, LOFI_GET_MINOR, &li);
94  *
95  *	li.li_minor = 0;
96  *	ioctl(ld, LOFI_GET_MAXMINOR, &li);
97  *	maxminor = li.li_minor;
98  *
99  *	strcpy(li.li_filename, "somefilename");
100  *	li.li_minor = 0;
101  *	ioctl(ld, LOFI_CHECK_COMPRESSED, &li);
102  *
103  * If the 'li_force' flag is set for any of the LOFI_UNMAP_* commands, then if
104  * the device is busy, the underlying vnode will be closed, and any subsequent
105  * operations will fail.  It will behave as if the device had been forcibly
106  * removed, so the DKIOCSTATE ioctl will return DKIO_DEV_GONE.  When the device
107  * is last closed, it will be torn down.
108  *
109  * Oh, and last but not least: these ioctls are totally private and only
110  * for use by lofiadm(1M).
111  *
112  */
113 
114 struct lofi_ioctl {
115 	uint32_t 	li_minor;
116 	boolean_t	li_force;
117 	char	li_filename[MAXPATHLEN + 1];
118 	char	li_algorithm[MAXALGLEN];
119 };
120 
121 #define	LOFI_IOC_BASE		(('L' << 16) | ('F' << 8))
122 
123 #define	LOFI_MAP_FILE		(LOFI_IOC_BASE | 0x01)
124 #define	LOFI_MAP_FILE_MINOR	(LOFI_IOC_BASE | 0x02)
125 #define	LOFI_UNMAP_FILE		(LOFI_IOC_BASE | 0x03)
126 #define	LOFI_UNMAP_FILE_MINOR	(LOFI_IOC_BASE | 0x04)
127 #define	LOFI_GET_FILENAME	(LOFI_IOC_BASE | 0x05)
128 #define	LOFI_GET_MINOR		(LOFI_IOC_BASE | 0x06)
129 #define	LOFI_GET_MAXMINOR	(LOFI_IOC_BASE | 0x07)
130 #define	LOFI_CHECK_COMPRESSED	(LOFI_IOC_BASE | 0x08)
131 
132 /*
133  * file types that might be usable with lofi, maybe. Only regular
134  * files are documented though.
135  */
136 #define	S_ISLOFIABLE(mode) \
137 	(S_ISREG(mode) || S_ISBLK(mode) || S_ISCHR(mode))
138 
139 #if defined(_KERNEL)
140 
141 /*
142  * We limit the maximum number of active lofi devices to 128, which seems very
143  * large. You can tune this by changing lofi_max_files in /etc/system.
144  * If you change it dynamically, which you probably shouldn't do, make sure
145  * to only _increase_ it.
146  */
147 #define	LOFI_MAX_FILES	128
148 extern uint32_t lofi_max_files;
149 
150 #define	V_ISLOFIABLE(vtype) \
151 	((vtype == VREG) || (vtype == VBLK) || (vtype == VCHR))
152 
153 struct lofi_state {
154 	char		*ls_filename;	/* filename to open */
155 	size_t		ls_filename_sz;
156 	struct vnode	*ls_vp;		/* open vnode */
157 	kmutex_t	ls_vp_lock;	/* protects ls_vp */
158 	kcondvar_t	ls_vp_cv;	/* signal changes to ls_vp */
159 	uint32_t	ls_vp_iocount;	/* # pending I/O requests */
160 	boolean_t	ls_vp_closereq;	/* force close requested */
161 	u_offset_t	ls_vp_size;
162 	uint32_t	ls_blk_open;
163 	uint32_t	ls_chr_open;
164 	uint32_t	ls_lyr_open_count;
165 	int		ls_openflag;
166 	taskq_t		*ls_taskq;
167 	kstat_t		*ls_kstat;
168 	kmutex_t	ls_kstat_lock;
169 	struct dk_geom	ls_dkg;
170 	struct vtoc	ls_vtoc;
171 	struct dk_cinfo	ls_ci;
172 
173 	/* the following fields are required for compression support */
174 	int		ls_comp_algorithm_index; /* idx into compress_table */
175 	char		ls_comp_algorithm[MAXALGLEN];
176 	uint32_t	ls_uncomp_seg_sz; /* sz of uncompressed segment */
177 	uint32_t	ls_comp_index_sz; /* number of index entries */
178 	uint32_t	ls_comp_seg_shift; /* exponent for byte shift */
179 	uint32_t	ls_uncomp_last_seg_sz; /* sz of last uncomp segment */
180 	uint64_t	ls_comp_offbase; /* offset of actual compressed data */
181 	uint64_t	*ls_comp_seg_index; /* array of index entries */
182 	caddr_t		ls_comp_index_data; /* index pages loaded from file */
183 	uint32_t	ls_comp_index_data_sz;
184 	u_offset_t	ls_vp_comp_size; /* actual compressed file size */
185 };
186 
187 #endif	/* _KERNEL */
188 
189 /*
190  * Common signature for all lofi compress functions
191  */
192 typedef int lofi_compress_func_t(void *src, size_t srclen, void *dst,
193 	size_t *destlen, int level);
194 
195 /*
196  * Information about each compression function
197  */
198 typedef struct lofi_compress_info {
199 	lofi_compress_func_t	*l_decompress;
200 	lofi_compress_func_t	*l_compress;
201 	int			l_level;
202 	char			*l_name;	/* algorithm name */
203 } lofi_compress_info_t;
204 
205 enum lofi_compress {
206 	LOFI_COMPRESS_GZIP = 0,
207 	LOFI_COMPRESS_GZIP_6 = 1,
208 	LOFI_COMPRESS_GZIP_9 = 2,
209 	LOFI_COMPRESS_FUNCTIONS
210 };
211 
212 #ifdef	__cplusplus
213 }
214 #endif
215 
216 #endif	/* _SYS_LOFI_H */
217