xref: /titanic_51/usr/src/uts/common/sys/lofi.h (revision 9f7c4232898c7267aead321833a9ff322daa257d)
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 2008 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  * If the 'li_cleanup' flag is set for any of the LOFI_UNMAP_* commands, then
110  * if the device is busy, it is marked for removal at the next time it is
111  * no longer held open by anybody.  When the device is last closed, it will be
112  * torn down.
113  *
114  * Oh, and last but not least: these ioctls are totally private and only
115  * for use by lofiadm(1M).
116  *
117  */
118 
119 struct lofi_ioctl {
120 	uint32_t 	li_minor;
121 	boolean_t	li_force;
122 	boolean_t	li_cleanup;
123 	char	li_filename[MAXPATHLEN + 1];
124 	char	li_algorithm[MAXALGLEN];
125 };
126 
127 #define	LOFI_IOC_BASE		(('L' << 16) | ('F' << 8))
128 
129 #define	LOFI_MAP_FILE		(LOFI_IOC_BASE | 0x01)
130 #define	LOFI_MAP_FILE_MINOR	(LOFI_IOC_BASE | 0x02)
131 #define	LOFI_UNMAP_FILE		(LOFI_IOC_BASE | 0x03)
132 #define	LOFI_UNMAP_FILE_MINOR	(LOFI_IOC_BASE | 0x04)
133 #define	LOFI_GET_FILENAME	(LOFI_IOC_BASE | 0x05)
134 #define	LOFI_GET_MINOR		(LOFI_IOC_BASE | 0x06)
135 #define	LOFI_GET_MAXMINOR	(LOFI_IOC_BASE | 0x07)
136 #define	LOFI_CHECK_COMPRESSED	(LOFI_IOC_BASE | 0x08)
137 
138 /*
139  * file types that might be usable with lofi, maybe. Only regular
140  * files are documented though.
141  */
142 #define	S_ISLOFIABLE(mode) \
143 	(S_ISREG(mode) || S_ISBLK(mode) || S_ISCHR(mode))
144 
145 #if defined(_KERNEL)
146 
147 /*
148  * We limit the maximum number of active lofi devices to 128, which seems very
149  * large. You can tune this by changing lofi_max_files in /etc/system.
150  * If you change it dynamically, which you probably shouldn't do, make sure
151  * to only _increase_ it.
152  */
153 #define	LOFI_MAX_FILES	128
154 extern uint32_t lofi_max_files;
155 
156 #define	V_ISLOFIABLE(vtype) \
157 	((vtype == VREG) || (vtype == VBLK) || (vtype == VCHR))
158 
159 struct lofi_state {
160 	char		*ls_filename;	/* filename to open */
161 	size_t		ls_filename_sz;
162 	struct vnode	*ls_vp;		/* open vnode */
163 	kmutex_t	ls_vp_lock;	/* protects ls_vp */
164 	kcondvar_t	ls_vp_cv;	/* signal changes to ls_vp */
165 	uint32_t	ls_vp_iocount;	/* # pending I/O requests */
166 	boolean_t	ls_vp_closereq;	/* force close requested */
167 	u_offset_t	ls_vp_size;
168 	uint32_t	ls_blk_open;
169 	uint32_t	ls_chr_open;
170 	uint32_t	ls_lyr_open_count;
171 	int		ls_openflag;
172 	boolean_t	ls_cleanup;	/* cleanup on close */
173 	taskq_t		*ls_taskq;
174 	kstat_t		*ls_kstat;
175 	kmutex_t	ls_kstat_lock;
176 	struct dk_geom	ls_dkg;
177 	struct vtoc	ls_vtoc;
178 	struct dk_cinfo	ls_ci;
179 
180 	/* the following fields are required for compression support */
181 	int		ls_comp_algorithm_index; /* idx into compress_table */
182 	char		ls_comp_algorithm[MAXALGLEN];
183 	uint32_t	ls_uncomp_seg_sz; /* sz of uncompressed segment */
184 	uint32_t	ls_comp_index_sz; /* number of index entries */
185 	uint32_t	ls_comp_seg_shift; /* exponent for byte shift */
186 	uint32_t	ls_uncomp_last_seg_sz; /* sz of last uncomp segment */
187 	uint64_t	ls_comp_offbase; /* offset of actual compressed data */
188 	uint64_t	*ls_comp_seg_index; /* array of index entries */
189 	caddr_t		ls_comp_index_data; /* index pages loaded from file */
190 	uint32_t	ls_comp_index_data_sz;
191 	u_offset_t	ls_vp_comp_size; /* actual compressed file size */
192 };
193 
194 #endif	/* _KERNEL */
195 
196 /*
197  * Common signature for all lofi compress functions
198  */
199 typedef int lofi_compress_func_t(void *src, size_t srclen, void *dst,
200 	size_t *destlen, int level);
201 
202 /*
203  * Information about each compression function
204  */
205 typedef struct lofi_compress_info {
206 	lofi_compress_func_t	*l_decompress;
207 	lofi_compress_func_t	*l_compress;
208 	int			l_level;
209 	char			*l_name;	/* algorithm name */
210 } lofi_compress_info_t;
211 
212 enum lofi_compress {
213 	LOFI_COMPRESS_GZIP = 0,
214 	LOFI_COMPRESS_GZIP_6 = 1,
215 	LOFI_COMPRESS_GZIP_9 = 2,
216 	LOFI_COMPRESS_FUNCTIONS
217 };
218 
219 #ifdef	__cplusplus
220 }
221 #endif
222 
223 #endif	/* _SYS_LOFI_H */
224