1 /* $NetBSD: kernfs.h,v 1.31 2006/06/23 20:54:21 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 42 /* 43 * The different types of node in a kernfs filesystem 44 */ 45 typedef enum { 46 KFSkern, /* the filesystem itself (.) */ 47 KFSroot, /* the filesystem root (..) */ 48 KFSnull, /* none aplicable */ 49 KFStime, /* boottime */ 50 KFSint, /* integer */ 51 KFSstring, /* string */ 52 KFShostname, /* hostname */ 53 KFSavenrun, /* loadavg */ 54 KFSdevice, /* device file (rootdev/rrootdev) */ 55 KFSmsgbuf, /* msgbuf */ 56 KFSipsecsadir, /* ipsec security association (top dir) */ 57 KFSipsecspdir, /* ipsec security policy (top dir) */ 58 KFSipsecsa, /* ipsec security association entry */ 59 KFSipsecsp, /* ipsec security policy entry */ 60 KFSsubdir, /* directory */ 61 KFSlasttype, /* last used type */ 62 KFSmaxtype = (1<<6) - 1 /* last possible type */ 63 } kfstype; 64 65 /* 66 * Control data for the kern file system. 67 */ 68 struct kern_target { 69 u_char kt_type; 70 u_char kt_namlen; 71 const char *kt_name; 72 void *kt_data; 73 kfstype kt_tag; 74 u_char kt_vtype; 75 mode_t kt_mode; 76 }; 77 78 struct dyn_kern_target { 79 struct kern_target dkt_kt; 80 SIMPLEQ_ENTRY(dyn_kern_target) dkt_queue; 81 }; 82 83 struct kernfs_subdir { 84 SIMPLEQ_HEAD(,dyn_kern_target) ks_entries; 85 unsigned int ks_nentries; 86 unsigned int ks_dirs; 87 const struct kern_target *ks_parent; 88 }; 89 90 struct kernfs_node { 91 LIST_ENTRY(kernfs_node) kfs_hash; /* hash chain */ 92 TAILQ_ENTRY(kernfs_node) kfs_list; /* flat list */ 93 struct vnode *kfs_vnode; /* vnode associated with this pfsnode */ 94 kfstype kfs_type; /* type of procfs node */ 95 mode_t kfs_mode; /* mode bits for stat() */ 96 long kfs_fileno; /* unique file id */ 97 u_int32_t kfs_value; /* SA id or SP id (KFSint) */ 98 const struct kern_target *kfs_kt; 99 void *kfs_v; /* pointer to secasvar/secpolicy/mbuf */ 100 long kfs_cookie; /* fileno cookie */ 101 }; 102 103 struct kernfs_mount { 104 TAILQ_HEAD(, kernfs_node) nodelist; 105 long fileno_cookie; 106 }; 107 108 #define UIO_MX 32 109 110 #define KERNFS_FILENO(kt, typ, cookie) \ 111 ((kt >= &kern_targets[0] && kt < &kern_targets[static_nkern_targets]) \ 112 ? 2 + ((kt) - &kern_targets[0]) \ 113 : (((cookie + 1) << 6) | (typ))) 114 #define KERNFS_TYPE_FILENO(typ, cookie) \ 115 (((cookie + 1) << 6) | (typ)) 116 117 #define VFSTOKERNFS(mp) ((struct kernfs_mount *)((mp)->mnt_data)) 118 #define VTOKERN(vp) ((struct kernfs_node *)(vp)->v_data) 119 #define KERNFSTOV(kfs) ((kfs)->kfs_vnode) 120 121 extern const struct kern_target kern_targets[]; 122 extern int nkern_targets; 123 extern const int static_nkern_targets; 124 extern int (**kernfs_vnodeop_p)(void *); 125 extern struct vfsops kernfs_vfsops; 126 extern dev_t rrootdev; 127 128 struct secasvar; 129 struct secpolicy; 130 131 int kernfs_root(struct mount *, struct vnode **); 132 133 void kernfs_hashinit(void); 134 void kernfs_hashreinit(void); 135 void kernfs_hashdone(void); 136 int kernfs_freevp(struct vnode *); 137 int kernfs_allocvp(struct mount *, struct vnode **, kfstype, 138 const struct kern_target *, u_int32_t); 139 140 void kernfs_revoke_sa(struct secasvar *); 141 void kernfs_revoke_sp(struct secpolicy *); 142 143 /* 144 * Data types for the kernfs file operations. 145 */ 146 typedef enum { 147 KERNFS_XREAD, 148 KERNFS_XWRITE, 149 KERNFS_FILEOP_CLOSE, 150 KERNFS_FILEOP_GETATTR, 151 KERNFS_FILEOP_IOCTL, 152 KERNFS_FILEOP_OPEN, 153 KERNFS_FILEOP_READ, 154 KERNFS_FILEOP_WRITE, 155 } kfsfileop; 156 157 struct kernfs_fileop { 158 kfstype kf_type; 159 kfsfileop kf_fileop; 160 union { 161 int (*_kf_vop)(void *); 162 int (*_kf_xread) 163 (const struct kernfs_node *, char **, size_t); 164 int (*_kf_xwrite) 165 (const struct kernfs_node *, char *, size_t); 166 } _kf_opfn; 167 SPLAY_ENTRY(kernfs_fileop) kf_node; 168 }; 169 170 #define kf_vop _kf_opfn._kf_vop 171 #define kf_xread _kf_opfn._kf_xread 172 #define kf_xwrite _kf_opfn._kf_xwrite 173 174 typedef struct kern_target kernfs_parentdir_t; 175 typedef struct dyn_kern_target kernfs_entry_t; 176 177 /* 178 * Functions for adding kernfs datatypes and nodes. 179 */ 180 kfstype kernfs_alloctype(int, const struct kernfs_fileop *); 181 #define KERNFS_ALLOCTYPE(kf) kernfs_alloctype(sizeof((kf)) / \ 182 sizeof((kf)[0]), (kf)) 183 #define KERNFS_ALLOCENTRY(dkt, m_type, m_flags) \ 184 dkt = (struct dyn_kern_target *)malloc( \ 185 sizeof(struct dyn_kern_target), (m_type), (m_flags)) 186 #define KERNFS_INITENTRY(dkt, type, name, data, tag, vtype, mode) do { \ 187 (dkt)->dkt_kt.kt_type = (type); \ 188 (dkt)->dkt_kt.kt_namlen = strlen((name)); \ 189 (dkt)->dkt_kt.kt_name = (name); \ 190 (dkt)->dkt_kt.kt_data = (data); \ 191 (dkt)->dkt_kt.kt_tag = (tag); \ 192 (dkt)->dkt_kt.kt_vtype = (vtype); \ 193 (dkt)->dkt_kt.kt_mode = (mode); \ 194 } while (/*CONSTCOND*/0) 195 #define KERNFS_ENTOPARENTDIR(dkt) &(dkt)->dkt_kt 196 int kernfs_addentry(kernfs_parentdir_t *, kernfs_entry_t *); 197 198 #ifdef SYSCTL_SETUP_PROTO 199 SYSCTL_SETUP_PROTO(sysctl_vfs_kernfs_setup); 200 #endif /* SYSCTL_SETUP_PROTO */ 201 202 #endif /* _KERNEL */ 203