1 /* $NetBSD: kernfs.h,v 1.36 2011/09/27 01:23:05 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software donated to Berkeley by 8 * Jan-Simon Pendry. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)kernfs.h 8.6 (Berkeley) 3/29/95 35 */ 36 37 #define _PATH_KERNFS "/kern" /* Default mountpoint */ 38 39 #ifdef _KERNEL 40 #include <sys/queue.h> 41 #include <sys/tree.h> 42 43 /* 44 * The different types of node in a kernfs filesystem 45 */ 46 typedef enum { 47 KFSkern, /* the filesystem itself (.) */ 48 KFSroot, /* the filesystem root (..) */ 49 KFSnull, /* none aplicable */ 50 KFStime, /* boottime */ 51 KFSint, /* integer */ 52 KFSstring, /* string */ 53 KFShostname, /* hostname */ 54 KFSavenrun, /* loadavg */ 55 KFSdevice, /* device file (rootdev/rrootdev) */ 56 KFSmsgbuf, /* msgbuf */ 57 KFSipsecsadir, /* ipsec security association (top dir) */ 58 KFSipsecspdir, /* ipsec security policy (top dir) */ 59 KFSipsecsa, /* ipsec security association entry */ 60 KFSipsecsp, /* ipsec security policy entry */ 61 KFSsubdir, /* directory */ 62 KFSlasttype, /* last used type */ 63 KFSmaxtype = (1<<6) - 1 /* last possible type */ 64 } kfstype; 65 66 /* 67 * Control data for the kern file system. 68 */ 69 struct kern_target { 70 u_char kt_type; 71 u_char kt_namlen; 72 const char *kt_name; 73 void *kt_data; 74 kfstype kt_tag; 75 u_char kt_vtype; 76 mode_t kt_mode; 77 }; 78 79 struct dyn_kern_target { 80 struct kern_target dkt_kt; 81 SIMPLEQ_ENTRY(dyn_kern_target) dkt_queue; 82 }; 83 84 struct kernfs_subdir { 85 SIMPLEQ_HEAD(,dyn_kern_target) ks_entries; 86 unsigned int ks_nentries; 87 unsigned int ks_dirs; 88 const struct kern_target *ks_parent; 89 }; 90 91 struct kernfs_node { 92 LIST_ENTRY(kernfs_node) kfs_hash; /* hash chain */ 93 TAILQ_ENTRY(kernfs_node) kfs_list; /* flat list */ 94 struct vnode *kfs_vnode; /* vnode associated with this kernfs_node */ 95 kfstype kfs_type; /* type of kernfs node */ 96 mode_t kfs_mode; /* mode bits for stat() */ 97 long kfs_fileno; /* unique file id */ 98 u_int32_t kfs_value; /* SA id or SP id (KFSint) */ 99 const struct kern_target *kfs_kt; 100 void *kfs_v; /* pointer to secasvar/secpolicy/mbuf */ 101 long kfs_cookie; /* fileno cookie */ 102 }; 103 104 struct kernfs_mount { 105 TAILQ_HEAD(, kernfs_node) nodelist; 106 long fileno_cookie; 107 }; 108 109 #define UIO_MX 32 110 111 #define KERNFS_FILENO(kt, typ, cookie) \ 112 ((kt >= &kern_targets[0] && kt < &kern_targets[static_nkern_targets]) \ 113 ? 2 + ((kt) - &kern_targets[0]) \ 114 : (((cookie + 1) << 6) | (typ))) 115 #define KERNFS_TYPE_FILENO(typ, cookie) \ 116 (((cookie + 1) << 6) | (typ)) 117 118 #define VFSTOKERNFS(mp) ((struct kernfs_mount *)((mp)->mnt_data)) 119 #define VTOKERN(vp) ((struct kernfs_node *)(vp)->v_data) 120 #define KERNFSTOV(kfs) ((kfs)->kfs_vnode) 121 122 #define KERNFS_MAXNAMLEN 255 123 124 extern const struct kern_target kern_targets[]; 125 extern int nkern_targets; 126 extern const int static_nkern_targets; 127 extern int (**kernfs_vnodeop_p)(void *); 128 extern struct vfsops kernfs_vfsops; 129 extern dev_t rrootdev; 130 131 struct secasvar; 132 struct secpolicy; 133 134 int kernfs_root(struct mount *, struct vnode **); 135 136 void kernfs_hashinit(void); 137 void kernfs_hashreinit(void); 138 void kernfs_hashdone(void); 139 int kernfs_freevp(struct vnode *); 140 int kernfs_allocvp(struct mount *, struct vnode **, kfstype, 141 const struct kern_target *, u_int32_t); 142 143 void kernfs_revoke_sa(struct secasvar *); 144 void kernfs_revoke_sp(struct secpolicy *); 145 146 /* 147 * Data types for the kernfs file operations. 148 */ 149 typedef enum { 150 KERNFS_XREAD, 151 KERNFS_XWRITE, 152 KERNFS_FILEOP_CLOSE, 153 KERNFS_FILEOP_GETATTR, 154 KERNFS_FILEOP_IOCTL, 155 KERNFS_FILEOP_OPEN, 156 KERNFS_FILEOP_READ, 157 KERNFS_FILEOP_WRITE, 158 } kfsfileop; 159 160 struct kernfs_fileop { 161 kfstype kf_type; 162 kfsfileop kf_fileop; 163 union { 164 int (*_kf_vop)(void *); 165 int (*_kf_xread) 166 (const struct kernfs_node *, char **, size_t); 167 int (*_kf_xwrite) 168 (const struct kernfs_node *, char *, size_t); 169 } _kf_opfn; 170 SPLAY_ENTRY(kernfs_fileop) kf_node; 171 }; 172 173 #define kf_vop _kf_opfn._kf_vop 174 #define kf_xread _kf_opfn._kf_xread 175 #define kf_xwrite _kf_opfn._kf_xwrite 176 177 typedef struct kern_target kernfs_parentdir_t; 178 typedef struct dyn_kern_target kernfs_entry_t; 179 180 /* 181 * Functions for adding kernfs datatypes and nodes. 182 */ 183 kfstype kernfs_alloctype(int, const struct kernfs_fileop *); 184 #define KERNFS_ALLOCTYPE(kf) kernfs_alloctype(sizeof((kf)) / \ 185 sizeof((kf)[0]), (kf)) 186 #define KERNFS_ALLOCENTRY(dkt, m_type, m_flags) \ 187 dkt = (struct dyn_kern_target *)malloc( \ 188 sizeof(struct dyn_kern_target), (m_type), (m_flags)) 189 #define KERNFS_INITENTRY(dkt, type, name, data, tag, vtype, mode) do { \ 190 (dkt)->dkt_kt.kt_type = (type); \ 191 (dkt)->dkt_kt.kt_namlen = strlen((name)); \ 192 (dkt)->dkt_kt.kt_name = (name); \ 193 (dkt)->dkt_kt.kt_data = (data); \ 194 (dkt)->dkt_kt.kt_tag = (tag); \ 195 (dkt)->dkt_kt.kt_vtype = (vtype); \ 196 (dkt)->dkt_kt.kt_mode = (mode); \ 197 } while (/*CONSTCOND*/0) 198 #define KERNFS_ENTOPARENTDIR(dkt) &(dkt)->dkt_kt 199 int kernfs_addentry(kernfs_parentdir_t *, kernfs_entry_t *); 200 201 #ifdef IPSEC 202 __weak_extern(key_freesp) 203 __weak_extern(key_getspbyid) 204 __weak_extern(key_setdumpsa_spi) 205 __weak_extern(key_setdumpsp) 206 __weak_extern(satailq) 207 __weak_extern(sptailq) 208 #endif 209 210 #endif /* _KERNEL */ 211