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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_SYS_DEVCACHE_IMPL_H
27 #define	_SYS_DEVCACHE_IMPL_H
28 
29 #ifdef	__cplusplus
30 extern "C" {
31 #endif
32 
33 #include <sys/list.h>
34 
35 /*
36  * /etc/devices cache files format
37  * Leave some padding for easy extension in the future
38  */
39 
40 #define	NVPF_HDR_MAGIC		0xdeb1dcac
41 #define	NVPF_HDR_VERSION	1
42 #define	NVPF_HDR_SIZE		128
43 
44 typedef struct nvpacked_file_hdr {
45 	union {
46 		struct nvfp_hdr {
47 			uint32_t	magic;
48 			int32_t		version;
49 			int64_t		size;
50 			uint16_t	hdr_chksum;
51 			uint16_t	chksum;
52 		} nvpf;
53 		uchar_t		nvpf_pad[NVPF_HDR_SIZE];
54 	} un;
55 } nvpf_hdr_t;
56 
57 #define	nvpf_magic		un.nvpf.magic
58 #define	nvpf_version		un.nvpf.version
59 #define	nvpf_size		un.nvpf.size
60 #define	nvpf_hdr_chksum		un.nvpf.hdr_chksum
61 #define	nvpf_chksum		un.nvpf.chksum
62 
63 
64 #ifdef	_KERNEL
65 
66 /*
67  * Descriptor used for kernel-level file i/o
68  */
69 typedef struct kfile {
70 	struct vnode	*kf_vp;
71 	int		kf_vnflags;
72 	char		*kf_fname;
73 	offset_t	kf_fpos;
74 	int		kf_state;
75 } kfile_t;
76 
77 /*
78  * File descriptor for files in the nvlist format
79  */
80 typedef struct nvfiledesc nvfd_t;
81 
82 /*
83  * Descriptor for a file managed as a backing store for some
84  * kernel-generated device state such as device devids,
85  * vhci-to-phci mapping, etc.
86  * Each client can manage the data in any form convenient.
87  * providing functions to unpack (read) and pack (write)
88  * the data as an nvlist.
89  *
90  * Clients should not reference this structure directly
91  * but through the handle returned when registering.
92  */
93 struct nvfiledesc {
94 	nvf_ops_t	*nvf_ops;		/* client ops vectors */
95 	int		nvf_flags;		/* flags */
96 	list_t		nvf_data_list;		/* data list */
97 	krwlock_t	nvf_lock;		/* lock for data list */
98 	list_node_t	nvf_link;		/* link to next file desc */
99 };
100 
101 /*
102  * nvf_flags
103  */
104 #define	NVF_F_DIRTY		0x01	/* needs to be flushed */
105 #define	NVF_F_FLUSHING		0x02	/* in process of being flushed */
106 #define	NVF_F_ERROR		0x04	/* most recent flush failed */
107 #define	NVF_F_READONLY		0x10	/* file is read-only */
108 #define	NVF_F_CREATE_MSG	0x20	/* file not found on boot, emit msg */
109 #define	NVF_F_REBUILD_MSG	0x40	/* file was found corrupted, emit msg */
110 
111 #define	NVF_IS_DIRTY(nvfd)	((nvfd)->nvf_flags & NVF_F_DIRTY)
112 #define	NVF_MARK_DIRTY(nvfd)	((nvfd)->nvf_flags |= NVF_F_DIRTY)
113 #define	NVF_CLEAR_DIRTY(nvfd)	((nvfd)->nvf_flags &= ~NVF_F_DIRTY)
114 
115 #define	NVF_IS_READONLY(nvfd)	((nvfd)->nvf_flags & NVF_F_READONLY)
116 #define	NVF_MARK_READONLY(nvfd)	((nvfd)->nvf_flags |= NVF_F_READONLY)
117 
118 /* shorthand to client ops */
119 #define	nvf_cache_path		nvf_ops->nvfr_cache_path
120 #define	nvf_unpack_nvlist	nvf_ops->nvfr_unpack_nvlist
121 #define	nvf_pack_list		nvf_ops->nvfr_pack_list
122 #define	nvf_list_free		nvf_ops->nvfr_list_free
123 #define	nvf_write_complete	nvf_ops->nvfr_write_complete
124 
125 
126 /*
127  * More thorough error reporting available both debug &
128  * non-debug kernels, but turned off by default.
129  */
130 extern int kfio_report_error;		/* kernel file i/o operations */
131 
132 /*
133  * Suffix of temporary file for updates
134  */
135 #define	MAX_SUFFIX_LEN		4
136 #define	NEW_FILENAME_SUFFIX	"new"
137 
138 
139 #ifdef	DEBUG
140 
141 #define	NVPDAEMON_DEBUG(args)	{ if (nvpdaemon_debug) cmn_err args; }
142 #define	KFDEBUG(args)		{ if (kfio_debug) cmn_err args; }
143 #define	KFDEBUG1(args)		{ if (kfio_debug > 1) cmn_err args; }
144 #define	KFDEBUG2(args)		{ if (kfio_debug > 2) cmn_err args; }
145 #define	KFDUMP(args)		{ if (kfio_debug > 2) args; }
146 
147 #else
148 
149 #define	NVPDAEMON_DEBUG(args)
150 #define	KFDEBUG(args)
151 #define	KFDEBUG1(args)
152 #define	KFDEBUG2(args)
153 #define	KFDUMP(args)
154 
155 #endif	/* DEBUG */
156 
157 
158 #endif	/* _KERNEL */
159 
160 #ifdef	__cplusplus
161 }
162 #endif
163 
164 #endif	/* _SYS_DEVCACHE_IMPL_H */
165