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