1 /* $NetBSD: filecore_node.c,v 1.27 2014/10/04 13:27:24 hannken Exp $ */ 2 3 /*- 4 * Copyright (c) 1982, 1986, 1989, 1994 5 * The Regents of the University of California. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * filecore_node.c 1.0 1998/6/4 33 */ 34 35 /*- 36 * Copyright (c) 1998 Andrew McMurry 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by the University of 49 * California, Berkeley and its contributors. 50 * 4. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 * 66 * filecore_node.c 1.0 1998/6/4 67 */ 68 69 #include <sys/cdefs.h> 70 __KERNEL_RCSID(0, "$NetBSD: filecore_node.c,v 1.27 2014/10/04 13:27:24 hannken Exp $"); 71 72 #include <sys/param.h> 73 #include <sys/systm.h> 74 #include <sys/mount.h> 75 #include <sys/proc.h> 76 #include <sys/file.h> 77 #include <sys/buf.h> 78 #include <sys/vnode.h> 79 #include <sys/namei.h> 80 #include <sys/kernel.h> 81 #include <sys/pool.h> 82 #include <sys/stat.h> 83 #include <sys/mutex.h> 84 85 #include <fs/filecorefs/filecore.h> 86 #include <fs/filecorefs/filecore_extern.h> 87 #include <fs/filecorefs/filecore_node.h> 88 #include <fs/filecorefs/filecore_mount.h> 89 90 struct pool filecore_node_pool; 91 92 extern int prtactive; /* 1 => print out reclaim of active vnodes */ 93 94 static const struct genfs_ops filecore_genfsops = { 95 .gop_size = genfs_size, 96 }; 97 98 /* 99 * Initialize hash links for inodes and dnodes. 100 */ 101 void 102 filecore_init(void) 103 { 104 105 pool_init(&filecore_node_pool, sizeof(struct filecore_node), 0, 0, 0, 106 "filecrnopl", &pool_allocator_nointr, IPL_NONE); 107 } 108 109 /* 110 * Reinitialize inode hash table. 111 */ 112 void 113 filecore_reinit(void) 114 { 115 116 } 117 118 /* 119 * Destroy node pool and hash table. 120 */ 121 void 122 filecore_done(void) 123 { 124 125 pool_destroy(&filecore_node_pool); 126 } 127 128 /* 129 * Initialize this vnode / filecore node pair. 130 * Caller assures no other thread will try to load this node. 131 */ 132 int 133 filecore_loadvnode(struct mount *mp, struct vnode *vp, 134 const void *key, size_t key_len, const void **new_key) 135 { 136 ino_t ino; 137 struct filecore_mnt *fcmp; 138 struct filecore_node *ip; 139 struct buf *bp; 140 int error; 141 142 KASSERT(key_len == sizeof(ino)); 143 memcpy(&ino, key, key_len); 144 fcmp = VFSTOFILECORE(mp); 145 146 ip = pool_get(&filecore_node_pool, PR_WAITOK); 147 memset(ip, 0, sizeof(struct filecore_node)); 148 ip->i_vnode = vp; 149 ip->i_dev = fcmp->fc_dev; 150 ip->i_number = ino; 151 ip->i_block = -1; 152 ip->i_parent = -2; 153 154 if (ino == FILECORE_ROOTINO) { 155 /* Here we need to construct a root directory inode */ 156 memcpy(ip->i_dirent.name, "root", 4); 157 ip->i_dirent.load = 0; 158 ip->i_dirent.exec = 0; 159 ip->i_dirent.len = FILECORE_DIR_SIZE; 160 ip->i_dirent.addr = fcmp->drec.root; 161 ip->i_dirent.attr = FILECORE_ATTR_DIR | FILECORE_ATTR_READ; 162 163 } else { 164 /* Read in Data from Directory Entry */ 165 if ((error = filecore_bread(fcmp, ino & FILECORE_INO_MASK, 166 FILECORE_DIR_SIZE, NOCRED, &bp)) != 0) { 167 pool_put(&filecore_node_pool, ip); 168 return error; 169 } 170 171 memcpy(&ip->i_dirent, 172 fcdirentry(bp->b_data, ino >> FILECORE_INO_INDEX), 173 sizeof(struct filecore_direntry)); 174 #ifdef FILECORE_DEBUG_BR 175 printf("brelse(%p) vf5\n", bp); 176 #endif 177 brelse(bp, 0); 178 } 179 180 ip->i_mnt = fcmp; 181 ip->i_devvp = fcmp->fc_devvp; 182 ip->i_diroff = 0; 183 vref(ip->i_devvp); 184 185 /* 186 * Initialize the associated vnode 187 */ 188 189 vp->v_tag = VT_FILECORE; 190 vp->v_op = filecore_vnodeop_p; 191 vp->v_data = ip; 192 if (ip->i_dirent.attr & FILECORE_ATTR_DIR) 193 vp->v_type = VDIR; 194 else 195 vp->v_type = VREG; 196 if (ino == FILECORE_ROOTINO) 197 vp->v_vflag |= VV_ROOT; 198 genfs_node_init(vp, &filecore_genfsops); 199 200 /* 201 * XXX need generation number? 202 */ 203 204 uvm_vnp_setsize(vp, ip->i_size); 205 *new_key = &ip->i_number; 206 return 0; 207 } 208 209 /* 210 * Last reference to an inode, write the inode out and if necessary, 211 * truncate and deallocate the file. 212 */ 213 int 214 filecore_inactive(void *v) 215 { 216 struct vop_inactive_args /* { 217 struct vnode *a_vp; 218 bool *a_recycle; 219 } */ *ap = v; 220 struct vnode *vp = ap->a_vp; 221 struct filecore_node *ip = VTOI(vp); 222 int error = 0; 223 224 /* 225 * If we are done with the inode, reclaim it 226 * so that it can be reused immediately. 227 */ 228 ip->i_flag = 0; 229 *ap->a_recycle = (filecore_staleinode(ip) != 0); 230 VOP_UNLOCK(vp); 231 return error; 232 } 233 234 /* 235 * Reclaim an inode so that it can be used for other purposes. 236 */ 237 int 238 filecore_reclaim(void *v) 239 { 240 struct vop_reclaim_args /* { 241 struct vnode *a_vp; 242 struct lwp *a_l; 243 } */ *ap = v; 244 struct vnode *vp = ap->a_vp; 245 struct filecore_node *ip = VTOI(vp); 246 247 if (prtactive && vp->v_usecount > 1) 248 vprint("filecore_reclaim: pushing active", vp); 249 /* 250 * Remove the inode from the vnode cache. 251 */ 252 vcache_remove(vp->v_mount, &ip->i_number, sizeof(ip->i_number)); 253 254 /* 255 * Purge old data structures associated with the inode. 256 */ 257 if (ip->i_devvp) { 258 vrele(ip->i_devvp); 259 ip->i_devvp = 0; 260 } 261 genfs_node_destroy(vp); 262 pool_put(&filecore_node_pool, vp->v_data); 263 vp->v_data = NULL; 264 return (0); 265 } 266