1 /*- 2 * Copyright (c) 2001 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #ifndef _PSEUDOFS_H_INCLUDED 32 #define _PSEUDOFS_H_INCLUDED 33 34 /* 35 * Opaque structures 36 */ 37 struct mntarg; 38 struct mount; 39 struct nameidata; 40 struct proc; 41 struct sbuf; 42 struct statfs; 43 struct thread; 44 struct uio; 45 struct vfsconf; 46 struct vnode; 47 48 /* 49 * Limits and constants 50 */ 51 #define PFS_NAMELEN 24 52 #define PFS_FSNAMELEN 16 /* equal to MFSNAMELEN */ 53 #define PFS_DELEN (8 + PFS_NAMELEN) 54 55 typedef enum { 56 pfstype_none = 0, 57 pfstype_root, 58 pfstype_dir, 59 pfstype_this, 60 pfstype_parent, 61 pfstype_file, 62 pfstype_symlink, 63 pfstype_procdir 64 } pfs_type_t; 65 66 /* 67 * Flags 68 */ 69 #define PFS_RD 0x0001 /* readable */ 70 #define PFS_WR 0x0002 /* writeable */ 71 #define PFS_RDWR (PFS_RD|PFS_WR) 72 #define PFS_RAWRD 0x0004 /* raw reader */ 73 #define PFS_RAWWR 0x0008 /* raw writer */ 74 #define PFS_RAW (PFS_RAWRD|PFS_RAWWR) 75 #define PFS_PROCDEP 0x0010 /* process-dependent */ 76 #define PFS_DISABLED 0x8000 /* node is disabled */ 77 78 /* 79 * Data structures 80 */ 81 struct pfs_info; 82 struct pfs_node; 83 struct pfs_bitmap; 84 85 /* 86 * Init / uninit callback 87 */ 88 #define PFS_INIT_ARGS \ 89 struct pfs_info *pi, struct vfsconf *vfc 90 #define PFS_INIT_PROTO(name) \ 91 int name(PFS_INIT_ARGS); 92 typedef int (*pfs_init_t)(PFS_INIT_ARGS); 93 94 /* 95 * Filler callback 96 */ 97 #define PFS_FILL_ARGS \ 98 struct thread *td, struct proc *p, struct pfs_node *pn, \ 99 struct sbuf *sb, struct uio *uio 100 #define PFS_FILL_PROTO(name) \ 101 int name(PFS_FILL_ARGS); 102 typedef int (*pfs_fill_t)(PFS_FILL_ARGS); 103 104 /* 105 * Attribute callback 106 */ 107 struct vattr; 108 #define PFS_ATTR_ARGS \ 109 struct thread *td, struct proc *p, struct pfs_node *pn, \ 110 struct vattr *vap 111 #define PFS_ATTR_PROTO(name) \ 112 int name(PFS_ATTR_ARGS); 113 typedef int (*pfs_attr_t)(PFS_ATTR_ARGS); 114 115 struct pfs_bitmap; /* opaque */ 116 117 /* 118 * Visibility callback 119 */ 120 #define PFS_VIS_ARGS \ 121 struct thread *td, struct proc *p, struct pfs_node *pn 122 #define PFS_VIS_PROTO(name) \ 123 int name(PFS_VIS_ARGS); 124 typedef int (*pfs_vis_t)(PFS_VIS_ARGS); 125 126 /* 127 * Ioctl callback 128 */ 129 #define PFS_IOCTL_ARGS \ 130 struct thread *td, struct proc *p, struct pfs_node *pn, \ 131 unsigned long cmd, void *data 132 #define PFS_IOCTL_PROTO(name) \ 133 int name(PFS_IOCTL_ARGS); 134 typedef int (*pfs_ioctl_t)(PFS_IOCTL_ARGS); 135 136 /* 137 * Getextattr callback 138 */ 139 #define PFS_GETEXTATTR_ARGS \ 140 struct thread *td, struct proc *p, struct pfs_node *pn, \ 141 int attrnamespace, const char *name, struct uio *uio, \ 142 size_t *size, struct ucred *cred 143 #define PFS_GETEXTATTR_PROTO(name) \ 144 int name(PFS_GETEXTATTR_ARGS); 145 struct ucred; 146 typedef int (*pfs_getextattr_t)(PFS_GETEXTATTR_ARGS); 147 148 /* 149 * Last-close callback 150 */ 151 #define PFS_CLOSE_ARGS \ 152 struct thread *td, struct proc *p, struct pfs_node *pn 153 #define PFS_CLOSE_PROTO(name) \ 154 int name(PFS_CLOSE_ARGS); 155 typedef int (*pfs_close_t)(PFS_CLOSE_ARGS); 156 157 /* 158 * pfs_info: describes a pseudofs instance 159 */ 160 struct pfs_info { 161 char pi_name[PFS_FSNAMELEN]; 162 pfs_init_t pi_init; 163 pfs_init_t pi_uninit; 164 /* members below this line aren't initialized */ 165 struct pfs_node *pi_root; 166 /* currently, the mutex is only used to protect the bitmap */ 167 struct mtx pi_mutex; 168 struct unrhdr *pi_unrhdr; 169 }; 170 171 /* 172 * pfs_node: describes a node (file or directory) within a pseudofs 173 */ 174 struct pfs_node { 175 char pn_name[PFS_NAMELEN]; 176 pfs_type_t pn_type; 177 union { 178 void *_pn_dummy; 179 pfs_fill_t _pn_func; 180 struct pfs_node *_pn_nodes; 181 } u1; 182 #define pn_func u1._pn_func 183 #define pn_nodes u1._pn_nodes 184 pfs_ioctl_t pn_ioctl; 185 pfs_close_t pn_close; 186 pfs_attr_t pn_attr; 187 pfs_vis_t pn_vis; 188 pfs_getextattr_t pn_getextattr; 189 void *pn_data; 190 int pn_flags; 191 192 struct pfs_info *pn_info; 193 struct pfs_node *pn_parent; 194 struct pfs_node *pn_next; 195 u_int32_t pn_fileno; 196 }; 197 198 /* 199 * VFS interface 200 */ 201 int pfs_mount (struct pfs_info *pi, struct mount *mp, 202 struct thread *td); 203 int pfs_cmount (struct mntarg *ma, void *data, int flags, 204 struct thread *td); 205 int pfs_unmount (struct mount *mp, int mntflags, 206 struct thread *td); 207 int pfs_root (struct mount *mp, int flags, 208 struct vnode **vpp, struct thread *td); 209 int pfs_statfs (struct mount *mp, struct statfs *sbp, 210 struct thread *td); 211 int pfs_init (struct pfs_info *pi, struct vfsconf *vfc); 212 int pfs_uninit (struct pfs_info *pi, struct vfsconf *vfc); 213 214 /* 215 * Directory structure construction and manipulation 216 */ 217 struct pfs_node *pfs_create_dir (struct pfs_node *parent, const char *name, 218 pfs_attr_t attr, pfs_vis_t vis, int flags); 219 struct pfs_node *pfs_create_file(struct pfs_node *parent, const char *name, 220 pfs_fill_t fill, pfs_attr_t attr, 221 pfs_vis_t vis, int flags); 222 struct pfs_node *pfs_create_link(struct pfs_node *parent, const char *name, 223 pfs_fill_t fill, pfs_attr_t attr, 224 pfs_vis_t vis, int flags); 225 struct pfs_node *pfs_find_node (struct pfs_node *parent, const char *name); 226 int pfs_disable (struct pfs_node *pn); 227 int pfs_enable (struct pfs_node *pn); 228 int pfs_destroy (struct pfs_node *pn); 229 230 /* 231 * Now for some initialization magic... 232 */ 233 #define PSEUDOFS(name, version) \ 234 \ 235 static struct pfs_info name##_info = { \ 236 #name, \ 237 name##_init, \ 238 name##_uninit, \ 239 }; \ 240 \ 241 static int \ 242 _##name##_mount(struct mount *mp, struct thread *td) { \ 243 return pfs_mount(&name##_info, mp, td); \ 244 } \ 245 \ 246 static int \ 247 _##name##_init(struct vfsconf *vfc) { \ 248 return pfs_init(&name##_info, vfc); \ 249 } \ 250 \ 251 static int \ 252 _##name##_uninit(struct vfsconf *vfc) { \ 253 return pfs_uninit(&name##_info, vfc); \ 254 } \ 255 \ 256 static struct vfsops name##_vfsops = { \ 257 .vfs_cmount = pfs_cmount, \ 258 .vfs_init = _##name##_init, \ 259 .vfs_mount = _##name##_mount, \ 260 .vfs_root = pfs_root, \ 261 .vfs_statfs = pfs_statfs, \ 262 .vfs_uninit = _##name##_uninit, \ 263 .vfs_unmount = pfs_unmount, \ 264 }; \ 265 VFS_SET(name##_vfsops, name, VFCF_SYNTHETIC); \ 266 MODULE_VERSION(name, version); \ 267 MODULE_DEPEND(name, pseudofs, 1, 1, 1); 268 269 #endif 270