xref: /netbsd-src/sys/fs/filecorefs/filecore_node.c (revision 93562e3f53a41fea679c96d9422f34f904308709)
1 /*	$NetBSD: filecore_node.c,v 1.31 2017/05/26 14:34:19 riastradh 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.31 2017/05/26 14:34:19 riastradh 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 static const struct genfs_ops filecore_genfsops = {
93         .gop_size = genfs_size,
94 };
95 
96 /*
97  * Initialize hash links for inodes and dnodes.
98  */
99 void
filecore_init(void)100 filecore_init(void)
101 {
102 
103 	pool_init(&filecore_node_pool, sizeof(struct filecore_node), 0, 0, 0,
104 	    "filecrnopl", &pool_allocator_nointr, IPL_NONE);
105 }
106 
107 /*
108  * Reinitialize inode hash table.
109  */
110 void
filecore_reinit(void)111 filecore_reinit(void)
112 {
113 
114 }
115 
116 /*
117  * Destroy node pool and hash table.
118  */
119 void
filecore_done(void)120 filecore_done(void)
121 {
122 
123 	pool_destroy(&filecore_node_pool);
124 }
125 
126 /*
127  * Initialize this vnode / filecore node pair.
128  * Caller assures no other thread will try to load this node.
129  */
130 int
filecore_loadvnode(struct mount * mp,struct vnode * vp,const void * key,size_t key_len,const void ** new_key)131 filecore_loadvnode(struct mount *mp, struct vnode *vp,
132     const void *key, size_t key_len, const void **new_key)
133 {
134 	ino_t ino;
135 	struct filecore_mnt *fcmp;
136 	struct filecore_node *ip;
137 	struct buf *bp;
138 	int error;
139 
140 	KASSERT(key_len == sizeof(ino));
141 	memcpy(&ino, key, key_len);
142 	fcmp = VFSTOFILECORE(mp);
143 
144 	ip = pool_get(&filecore_node_pool, PR_WAITOK);
145 	memset(ip, 0, sizeof(struct filecore_node));
146 	ip->i_vnode = vp;
147 	ip->i_dev = fcmp->fc_dev;
148 	ip->i_number = ino;
149 	ip->i_block = -1;
150 	ip->i_parent = -2;
151 
152 	if (ino == FILECORE_ROOTINO) {
153 		/* Here we need to construct a root directory inode */
154 		memcpy(ip->i_dirent.name, "root", 4);
155 		ip->i_dirent.load = 0;
156 		ip->i_dirent.exec = 0;
157 		ip->i_dirent.len = FILECORE_DIR_SIZE;
158 		ip->i_dirent.addr = fcmp->drec.root;
159 		ip->i_dirent.attr = FILECORE_ATTR_DIR | FILECORE_ATTR_READ;
160 
161 	} else {
162 		/* Read in Data from Directory Entry */
163 		if ((error = filecore_bread(fcmp, ino & FILECORE_INO_MASK,
164 		    FILECORE_DIR_SIZE, NOCRED, &bp)) != 0) {
165 			pool_put(&filecore_node_pool, ip);
166 			return error;
167 		}
168 
169 		memcpy(&ip->i_dirent,
170 		    fcdirentry(bp->b_data, ino >> FILECORE_INO_INDEX),
171 		    sizeof(struct filecore_direntry));
172 #ifdef FILECORE_DEBUG_BR
173 		printf("brelse(%p) vf5\n", bp);
174 #endif
175 		brelse(bp, 0);
176 	}
177 
178 	ip->i_mnt = fcmp;
179 	ip->i_devvp = fcmp->fc_devvp;
180 	ip->i_diroff = 0;
181 	vref(ip->i_devvp);
182 
183 	/*
184 	 * Initialize the associated vnode
185 	 */
186 
187 	vp->v_tag = VT_FILECORE;
188 	vp->v_op = filecore_vnodeop_p;
189 	vp->v_data = ip;
190 	if (ip->i_dirent.attr & FILECORE_ATTR_DIR)
191 		vp->v_type = VDIR;
192 	else
193 		vp->v_type = VREG;
194 	if (ino == FILECORE_ROOTINO)
195 		vp->v_vflag |= VV_ROOT;
196 	genfs_node_init(vp, &filecore_genfsops);
197 
198 	/*
199 	 * XXX need generation number?
200 	 */
201 
202 	uvm_vnp_setsize(vp, ip->i_size);
203 	*new_key = &ip->i_number;
204 	return 0;
205 }
206 
207 /*
208  * Last reference to an inode, write the inode out and if necessary,
209  * truncate and deallocate the file.
210  */
211 int
filecore_inactive(void * v)212 filecore_inactive(void *v)
213 {
214 	struct vop_inactive_v2_args /* {
215 		struct vnode *a_vp;
216 		bool *a_recycle;
217 	} */ *ap = v;
218 	struct vnode *vp = ap->a_vp;
219 	struct filecore_node *ip = VTOI(vp);
220 	int error = 0;
221 
222 	/*
223 	 * If we are done with the inode, reclaim it
224 	 * so that it can be reused immediately.
225 	 */
226 	ip->i_flag = 0;
227 	*ap->a_recycle = (filecore_staleinode(ip) != 0);
228 
229 	return error;
230 }
231 
232 /*
233  * Reclaim an inode so that it can be used for other purposes.
234  */
235 int
filecore_reclaim(void * v)236 filecore_reclaim(void *v)
237 {
238 	struct vop_reclaim_v2_args /* {
239 		struct vnode *a_vp;
240 		struct lwp *a_l;
241 	} */ *ap = v;
242 	struct vnode *vp = ap->a_vp;
243 	struct filecore_node *ip = VTOI(vp);
244 
245 	VOP_UNLOCK(vp);
246 
247 	/*
248 	 * Purge old data structures associated with the inode.
249 	 */
250 	if (ip->i_devvp) {
251 		vrele(ip->i_devvp);
252 		ip->i_devvp = 0;
253 	}
254 	genfs_node_destroy(vp);
255 	pool_put(&filecore_node_pool, vp->v_data);
256 	vp->v_data = NULL;
257 	return (0);
258 }
259