1*93562e3fSriastradh /* $NetBSD: filecore_node.c,v 1.31 2017/05/26 14:34:19 riastradh Exp $ */
25356de15Sjdolecek
35356de15Sjdolecek /*-
45356de15Sjdolecek * Copyright (c) 1982, 1986, 1989, 1994
55356de15Sjdolecek * The Regents of the University of California.
65356de15Sjdolecek * All rights reserved.
75356de15Sjdolecek *
85356de15Sjdolecek * Redistribution and use in source and binary forms, with or without
95356de15Sjdolecek * modification, are permitted provided that the following conditions
105356de15Sjdolecek * are met:
115356de15Sjdolecek * 1. Redistributions of source code must retain the above copyright
125356de15Sjdolecek * notice, this list of conditions and the following disclaimer.
135356de15Sjdolecek * 2. Redistributions in binary form must reproduce the above copyright
145356de15Sjdolecek * notice, this list of conditions and the following disclaimer in the
155356de15Sjdolecek * documentation and/or other materials provided with the distribution.
16aad01611Sagc * 3. Neither the name of the University nor the names of its contributors
17aad01611Sagc * may be used to endorse or promote products derived from this software
18aad01611Sagc * without specific prior written permission.
19aad01611Sagc *
20aad01611Sagc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21aad01611Sagc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22aad01611Sagc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23aad01611Sagc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24aad01611Sagc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25aad01611Sagc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26aad01611Sagc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27aad01611Sagc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28aad01611Sagc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29aad01611Sagc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30aad01611Sagc * SUCH DAMAGE.
31aad01611Sagc *
32aad01611Sagc * filecore_node.c 1.0 1998/6/4
33aad01611Sagc */
34aad01611Sagc
35aad01611Sagc /*-
36aad01611Sagc * Copyright (c) 1998 Andrew McMurry
37aad01611Sagc *
38aad01611Sagc * Redistribution and use in source and binary forms, with or without
39aad01611Sagc * modification, are permitted provided that the following conditions
40aad01611Sagc * are met:
41aad01611Sagc * 1. Redistributions of source code must retain the above copyright
42aad01611Sagc * notice, this list of conditions and the following disclaimer.
43aad01611Sagc * 2. Redistributions in binary form must reproduce the above copyright
44aad01611Sagc * notice, this list of conditions and the following disclaimer in the
45aad01611Sagc * documentation and/or other materials provided with the distribution.
465356de15Sjdolecek * 3. All advertising materials mentioning features or use of this software
475356de15Sjdolecek * must display the following acknowledgement:
485356de15Sjdolecek * This product includes software developed by the University of
495356de15Sjdolecek * California, Berkeley and its contributors.
505356de15Sjdolecek * 4. Neither the name of the University nor the names of its contributors
515356de15Sjdolecek * may be used to endorse or promote products derived from this software
525356de15Sjdolecek * without specific prior written permission.
535356de15Sjdolecek *
545356de15Sjdolecek * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
555356de15Sjdolecek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
565356de15Sjdolecek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
575356de15Sjdolecek * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
585356de15Sjdolecek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
595356de15Sjdolecek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
605356de15Sjdolecek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
615356de15Sjdolecek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
625356de15Sjdolecek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
635356de15Sjdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
645356de15Sjdolecek * SUCH DAMAGE.
655356de15Sjdolecek *
665356de15Sjdolecek * filecore_node.c 1.0 1998/6/4
675356de15Sjdolecek */
685356de15Sjdolecek
695356de15Sjdolecek #include <sys/cdefs.h>
70*93562e3fSriastradh __KERNEL_RCSID(0, "$NetBSD: filecore_node.c,v 1.31 2017/05/26 14:34:19 riastradh Exp $");
715356de15Sjdolecek
725356de15Sjdolecek #include <sys/param.h>
735356de15Sjdolecek #include <sys/systm.h>
745356de15Sjdolecek #include <sys/mount.h>
755356de15Sjdolecek #include <sys/proc.h>
765356de15Sjdolecek #include <sys/file.h>
775356de15Sjdolecek #include <sys/buf.h>
785356de15Sjdolecek #include <sys/vnode.h>
795356de15Sjdolecek #include <sys/namei.h>
805356de15Sjdolecek #include <sys/kernel.h>
815356de15Sjdolecek #include <sys/pool.h>
825356de15Sjdolecek #include <sys/stat.h>
835cee5af5Srmind #include <sys/mutex.h>
845356de15Sjdolecek
855356de15Sjdolecek #include <fs/filecorefs/filecore.h>
865356de15Sjdolecek #include <fs/filecorefs/filecore_extern.h>
875356de15Sjdolecek #include <fs/filecorefs/filecore_node.h>
885356de15Sjdolecek #include <fs/filecorefs/filecore_mount.h>
895356de15Sjdolecek
90835b0326Spooka struct pool filecore_node_pool;
915356de15Sjdolecek
92e716e7d8Shannken static const struct genfs_ops filecore_genfsops = {
93e716e7d8Shannken .gop_size = genfs_size,
94e716e7d8Shannken };
95e716e7d8Shannken
965356de15Sjdolecek /*
975356de15Sjdolecek * Initialize hash links for inodes and dnodes.
985356de15Sjdolecek */
995356de15Sjdolecek void
filecore_init(void)100164477c6Scegger filecore_init(void)
1015356de15Sjdolecek {
102835b0326Spooka
1031d3a6a32Satatat pool_init(&filecore_node_pool, sizeof(struct filecore_node), 0, 0, 0,
1044b1d78c0Sad "filecrnopl", &pool_allocator_nointr, IPL_NONE);
1055356de15Sjdolecek }
1065356de15Sjdolecek
1075356de15Sjdolecek /*
1085356de15Sjdolecek * Reinitialize inode hash table.
1095356de15Sjdolecek */
1105356de15Sjdolecek void
filecore_reinit(void)111164477c6Scegger filecore_reinit(void)
1125356de15Sjdolecek {
1135356de15Sjdolecek
1145356de15Sjdolecek }
1155356de15Sjdolecek
1165356de15Sjdolecek /*
1175356de15Sjdolecek * Destroy node pool and hash table.
1185356de15Sjdolecek */
1195356de15Sjdolecek void
filecore_done(void)120164477c6Scegger filecore_done(void)
1215356de15Sjdolecek {
1225cee5af5Srmind
1231d3a6a32Satatat pool_destroy(&filecore_node_pool);
1245356de15Sjdolecek }
1255356de15Sjdolecek
1265356de15Sjdolecek /*
127e716e7d8Shannken * Initialize this vnode / filecore node pair.
128e716e7d8Shannken * Caller assures no other thread will try to load this node.
1295356de15Sjdolecek */
130e716e7d8Shannken int
filecore_loadvnode(struct mount * mp,struct vnode * vp,const void * key,size_t key_len,const void ** new_key)131e716e7d8Shannken filecore_loadvnode(struct mount *mp, struct vnode *vp,
132e716e7d8Shannken const void *key, size_t key_len, const void **new_key)
1335356de15Sjdolecek {
134e716e7d8Shannken ino_t ino;
135e716e7d8Shannken struct filecore_mnt *fcmp;
1365356de15Sjdolecek struct filecore_node *ip;
137e716e7d8Shannken struct buf *bp;
138e716e7d8Shannken int error;
1395356de15Sjdolecek
140e716e7d8Shannken KASSERT(key_len == sizeof(ino));
141e716e7d8Shannken memcpy(&ino, key, key_len);
142e716e7d8Shannken fcmp = VFSTOFILECORE(mp);
143e716e7d8Shannken
144e716e7d8Shannken ip = pool_get(&filecore_node_pool, PR_WAITOK);
145e716e7d8Shannken memset(ip, 0, sizeof(struct filecore_node));
146e716e7d8Shannken ip->i_vnode = vp;
147e716e7d8Shannken ip->i_dev = fcmp->fc_dev;
148e716e7d8Shannken ip->i_number = ino;
149e716e7d8Shannken ip->i_block = -1;
150e716e7d8Shannken ip->i_parent = -2;
151e716e7d8Shannken
152e716e7d8Shannken if (ino == FILECORE_ROOTINO) {
153e716e7d8Shannken /* Here we need to construct a root directory inode */
154e716e7d8Shannken memcpy(ip->i_dirent.name, "root", 4);
155e716e7d8Shannken ip->i_dirent.load = 0;
156e716e7d8Shannken ip->i_dirent.exec = 0;
157e716e7d8Shannken ip->i_dirent.len = FILECORE_DIR_SIZE;
158e716e7d8Shannken ip->i_dirent.addr = fcmp->drec.root;
159e716e7d8Shannken ip->i_dirent.attr = FILECORE_ATTR_DIR | FILECORE_ATTR_READ;
160e716e7d8Shannken
161e716e7d8Shannken } else {
162e716e7d8Shannken /* Read in Data from Directory Entry */
163e716e7d8Shannken if ((error = filecore_bread(fcmp, ino & FILECORE_INO_MASK,
164e716e7d8Shannken FILECORE_DIR_SIZE, NOCRED, &bp)) != 0) {
165e716e7d8Shannken pool_put(&filecore_node_pool, ip);
166e716e7d8Shannken return error;
1675356de15Sjdolecek }
168e716e7d8Shannken
169e716e7d8Shannken memcpy(&ip->i_dirent,
170e716e7d8Shannken fcdirentry(bp->b_data, ino >> FILECORE_INO_INDEX),
171e716e7d8Shannken sizeof(struct filecore_direntry));
172e716e7d8Shannken #ifdef FILECORE_DEBUG_BR
173e716e7d8Shannken printf("brelse(%p) vf5\n", bp);
174e716e7d8Shannken #endif
175e716e7d8Shannken brelse(bp, 0);
1765356de15Sjdolecek }
177e716e7d8Shannken
178e716e7d8Shannken ip->i_mnt = fcmp;
179e716e7d8Shannken ip->i_devvp = fcmp->fc_devvp;
180e716e7d8Shannken ip->i_diroff = 0;
181e716e7d8Shannken vref(ip->i_devvp);
1825356de15Sjdolecek
1835356de15Sjdolecek /*
184e716e7d8Shannken * Initialize the associated vnode
1855356de15Sjdolecek */
1865356de15Sjdolecek
187e716e7d8Shannken vp->v_tag = VT_FILECORE;
188e716e7d8Shannken vp->v_op = filecore_vnodeop_p;
189e716e7d8Shannken vp->v_data = ip;
190e716e7d8Shannken if (ip->i_dirent.attr & FILECORE_ATTR_DIR)
191e716e7d8Shannken vp->v_type = VDIR;
192e716e7d8Shannken else
193e716e7d8Shannken vp->v_type = VREG;
194e716e7d8Shannken if (ino == FILECORE_ROOTINO)
195e716e7d8Shannken vp->v_vflag |= VV_ROOT;
196e716e7d8Shannken genfs_node_init(vp, &filecore_genfsops);
1975356de15Sjdolecek
1985356de15Sjdolecek /*
199e716e7d8Shannken * XXX need generation number?
2005356de15Sjdolecek */
201e716e7d8Shannken
202e716e7d8Shannken uvm_vnp_setsize(vp, ip->i_size);
203e716e7d8Shannken *new_key = &ip->i_number;
204e716e7d8Shannken return 0;
2055356de15Sjdolecek }
2065356de15Sjdolecek
2075356de15Sjdolecek /*
2085356de15Sjdolecek * Last reference to an inode, write the inode out and if necessary,
2095356de15Sjdolecek * truncate and deallocate the file.
2105356de15Sjdolecek */
2115356de15Sjdolecek int
filecore_inactive(void * v)212454af1c0Sdsl filecore_inactive(void *v)
2135356de15Sjdolecek {
21487fb3229Sriastradh struct vop_inactive_v2_args /* {
2155356de15Sjdolecek struct vnode *a_vp;
2164a780c9aSad bool *a_recycle;
2175356de15Sjdolecek } */ *ap = v;
2185356de15Sjdolecek struct vnode *vp = ap->a_vp;
2195356de15Sjdolecek struct filecore_node *ip = VTOI(vp);
2205356de15Sjdolecek int error = 0;
2215356de15Sjdolecek
2225356de15Sjdolecek /*
2235356de15Sjdolecek * If we are done with the inode, reclaim it
2245356de15Sjdolecek * so that it can be reused immediately.
2255356de15Sjdolecek */
2264a780c9aSad ip->i_flag = 0;
2274a780c9aSad *ap->a_recycle = (filecore_staleinode(ip) != 0);
22887fb3229Sriastradh
2295356de15Sjdolecek return error;
2305356de15Sjdolecek }
2315356de15Sjdolecek
2325356de15Sjdolecek /*
2335356de15Sjdolecek * Reclaim an inode so that it can be used for other purposes.
2345356de15Sjdolecek */
2355356de15Sjdolecek int
filecore_reclaim(void * v)236454af1c0Sdsl filecore_reclaim(void *v)
2375356de15Sjdolecek {
2387f7aad09Sriastradh struct vop_reclaim_v2_args /* {
2395356de15Sjdolecek struct vnode *a_vp;
24095e1ffb1Schristos struct lwp *a_l;
2415356de15Sjdolecek } */ *ap = v;
2425356de15Sjdolecek struct vnode *vp = ap->a_vp;
2435356de15Sjdolecek struct filecore_node *ip = VTOI(vp);
2445356de15Sjdolecek
2457f7aad09Sriastradh VOP_UNLOCK(vp);
2467f7aad09Sriastradh
2475356de15Sjdolecek /*
2485356de15Sjdolecek * Purge old data structures associated with the inode.
2495356de15Sjdolecek */
2505356de15Sjdolecek if (ip->i_devvp) {
2515356de15Sjdolecek vrele(ip->i_devvp);
2525356de15Sjdolecek ip->i_devvp = 0;
2535356de15Sjdolecek }
254adbb9ec2Sad genfs_node_destroy(vp);
2555356de15Sjdolecek pool_put(&filecore_node_pool, vp->v_data);
2565356de15Sjdolecek vp->v_data = NULL;
2575356de15Sjdolecek return (0);
2585356de15Sjdolecek }
259