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 #include <sys/param.h> 32 #include <sys/kernel.h> 33 #include <sys/systm.h> 34 #include <sys/malloc.h> 35 #include <sys/module.h> 36 #include <sys/mount.h> 37 #include <sys/mutex.h> 38 #include <sys/proc.h> 39 #include <sys/sbuf.h> 40 #include <sys/sysctl.h> 41 #include <sys/vnode.h> 42 43 #include <fs/pseudofs/pseudofs.h> 44 #include <fs/pseudofs/pseudofs_internal.h> 45 46 SYSCTL_NODE(_vfs, OID_AUTO, pfs, CTLFLAG_RW, 0, 47 "pseudofs"); 48 49 /* 50 * Mount a pseudofs instance 51 */ 52 int 53 pfs_mount(struct pfs_info *pi, struct mount *mp, char *path, caddr_t data, 54 struct nameidata *ndp, struct proc *p) 55 { 56 struct statfs *sbp; 57 58 if (mp->mnt_flag & MNT_UPDATE) 59 return (EOPNOTSUPP); 60 61 mp->mnt_flag |= MNT_LOCAL; 62 mp->mnt_data = (qaddr_t)pi; 63 vfs_getnewfsid(mp); 64 65 sbp = &mp->mnt_stat; 66 bcopy(pi->pi_name, sbp->f_mntfromname, sizeof pi->pi_name); 67 sbp->f_bsize = PAGE_SIZE; 68 sbp->f_iosize = PAGE_SIZE; 69 sbp->f_blocks = 1; 70 sbp->f_bfree = 0; 71 sbp->f_bavail = 0; 72 sbp->f_files = 1; 73 sbp->f_ffree = 0; 74 75 return (0); 76 } 77 78 /* 79 * Unmount a pseudofs instance 80 */ 81 int 82 pfs_unmount(struct mount *mp, int mntflags, struct proc *p) 83 { 84 struct pfs_info *pi; 85 int error; 86 87 pi = (struct pfs_info *)mp->mnt_data; 88 89 /* XXX do stuff with pi... */ 90 91 error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0); 92 return (error); 93 } 94 95 /* 96 * Return a root vnode 97 */ 98 int 99 pfs_root(struct mount *mp, struct vnode **vpp) 100 { 101 struct pfs_info *pi; 102 103 pi = (struct pfs_info *)mp->mnt_data; 104 return pfs_vncache_alloc(mp, vpp, pi->pi_root, NO_PID); 105 } 106 107 /* 108 * Return filesystem stats 109 */ 110 int 111 pfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p) 112 { 113 bcopy(&mp->mnt_stat, sbp, sizeof *sbp); 114 return (0); 115 } 116 117 /* 118 * Initialize a pseudofs instance 119 */ 120 int 121 pfs_init(struct pfs_info *pi, struct vfsconf *vfc) 122 { 123 mtx_init(&pi->pi_mutex, "pseudofs", MTX_DEF); 124 pfs_fileno_init(pi); 125 printf("%s registered\n", pi->pi_name); 126 return (0); 127 } 128 129 /* 130 * Destroy a pseudofs instance 131 */ 132 int 133 pfs_uninit(struct pfs_info *pi, struct vfsconf *vfc) 134 { 135 pfs_fileno_uninit(pi); 136 mtx_destroy(&pi->pi_mutex); 137 printf("%s unregistered\n", pi->pi_name); 138 return (0); 139 } 140 141 /* 142 * Handle load / unload events 143 */ 144 static int 145 pfs_modevent(module_t mod, int evt, void *arg) 146 { 147 switch (evt) { 148 case MOD_LOAD: 149 pfs_fileno_load(); 150 pfs_vncache_load(); 151 break; 152 case MOD_UNLOAD: 153 case MOD_SHUTDOWN: 154 pfs_vncache_unload(); 155 pfs_fileno_unload(); 156 break; 157 default: 158 printf("pseudofs: unexpected event type %d\n", evt); 159 break; 160 } 161 return 0; 162 } 163 164 /* 165 * Module declaration 166 */ 167 static moduledata_t pseudofs_data = { 168 "pseudofs", 169 pfs_modevent, 170 NULL 171 }; 172 DECLARE_MODULE(pseudofs, pseudofs_data, SI_SUB_EXEC, SI_ORDER_FIRST); 173 MODULE_VERSION(pseudofs, 1); 174