lfs_alloc.c revision 1.18 1 /* $NetBSD: lfs_alloc.c,v 1.18 1999/03/24 05:51:30 mrg Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*
39 * Copyright (c) 1991, 1993
40 * The Regents of the University of California. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)lfs_alloc.c 8.4 (Berkeley) 1/4/94
71 */
72
73 #if defined(_KERNEL) && !defined(_LKM)
74 #include "opt_quota.h"
75 #endif
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/buf.h>
81 #include <sys/vnode.h>
82 #include <sys/syslog.h>
83 #include <sys/mount.h>
84 #include <sys/malloc.h>
85 #include <sys/pool.h>
86
87 #include <vm/vm.h>
88
89 #include <ufs/ufs/quota.h>
90 #include <ufs/ufs/inode.h>
91 #include <ufs/ufs/ufsmount.h>
92 #include <ufs/ufs/ufs_extern.h>
93
94 #include <ufs/lfs/lfs.h>
95 #include <ufs/lfs/lfs_extern.h>
96
97 #ifndef USE_UFS_HASHLOCK
98 int free_lock = 0;
99 #else
100 extern struct lock ufs_hashlock;
101 #endif
102
103 /* Allocate a new inode. */
104 /* ARGSUSED */
105 int
106 lfs_valloc(v)
107 void *v;
108 {
109 struct vop_valloc_args /* {
110 struct vnode *a_pvp;
111 int a_mode;
112 struct ucred *a_cred;
113 struct vnode **a_vpp;
114 } */ *ap = v;
115 struct lfs *fs;
116 struct buf *bp;
117 struct ifile *ifp;
118 struct inode *ip;
119 struct vnode *vp;
120 ufs_daddr_t blkno;
121 ino_t new_ino;
122 u_long i, max;
123 int error;
124
125 fs = VTOI(ap->a_pvp)->i_lfs;
126
127 /*
128 * Prevent a race getting lfs_free - XXX - KS
129 * (this should be a proper lock, in struct lfs)
130 */
131
132 #ifndef USE_UFS_HASHLOCK
133 while(free_lock)
134 tsleep(&free_lock, PRIBIO+1, "lfs_free", 0);
135 free_lock++;
136 #else
137 while(lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0))
138 ;
139 #endif
140
141 /* Get the head of the freelist. */
142 new_ino = fs->lfs_free;
143 #ifdef DIAGNOSTIC
144 if(new_ino == LFS_UNUSED_INUM) {
145 #ifdef DEBUG
146 lfs_dump_super(fs);
147 #endif /* DEBUG */
148 panic("inode 0 allocated [1]");
149 }
150 #endif /* DIAGNOSTIC */
151 #ifdef ALLOCPRINT
152 printf("lfs_ialloc: allocate inode %d\n", new_ino);
153 #endif
154
155 /*
156 * Remove the inode from the free list and write the new start
157 * of the free list into the superblock.
158 */
159 LFS_IENTRY(ifp, fs, new_ino, bp);
160 if (ifp->if_daddr != LFS_UNUSED_DADDR)
161 panic("lfs_ialloc: inuse inode %d on the free list", new_ino);
162 fs->lfs_free = ifp->if_nextfree;
163 brelse(bp);
164
165 /* Extend IFILE so that the next lfs_valloc will succeed. */
166 if (fs->lfs_free == LFS_UNUSED_INUM) {
167 vp = fs->lfs_ivnode;
168 VOP_LOCK(vp,LK_EXCLUSIVE);
169 ip = VTOI(vp);
170 blkno = lblkno(fs, ip->i_ffs_size);
171 lfs_balloc(vp, 0, fs->lfs_bsize, blkno, &bp);
172 ip->i_ffs_size += fs->lfs_bsize;
173 uvm_vnp_setsize(vp, ip->i_ffs_size);
174 (void)uvm_vnp_uncache(vp);
175
176 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
177 fs->lfs_ifpb;
178 fs->lfs_free = i;
179 #ifdef DIAGNOSTIC
180 if(fs->lfs_free == LFS_UNUSED_INUM)
181 panic("inode 0 allocated [2]");
182 #endif /* DIAGNOSTIC */
183 max = i + fs->lfs_ifpb;
184 for (ifp = (struct ifile *)bp->b_data; i < max; ++ifp) {
185 ifp->if_version = 1;
186 ifp->if_daddr = LFS_UNUSED_DADDR;
187 ifp->if_nextfree = ++i;
188 }
189 ifp--;
190 ifp->if_nextfree = LFS_UNUSED_INUM;
191 VOP_UNLOCK(vp,0);
192 if ((error = VOP_BWRITE(bp)) != 0) {
193 #ifndef USE_UFS_HASHLOCK
194 free_lock--;
195 wakeup(&free_lock);
196 #else
197 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
198 #endif
199 return (error);
200 }
201 }
202
203 #ifndef USE_UFS_HASHLOCK
204 free_lock--;
205 wakeup(&free_lock);
206 #else
207 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
208 #endif
209
210 #ifdef DIAGNOSTIC
211 if(fs->lfs_free == LFS_UNUSED_INUM)
212 panic("inode 0 allocated [3]");
213 #endif /* DIAGNOSTIC */
214
215 /* Create a vnode to associate with the inode. */
216 if ((error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp)) != 0)
217 return (error);
218
219 ip = VTOI(vp);
220 /* Zero out the direct and indirect block addresses. */
221 bzero(&ip->i_din, sizeof(ip->i_din));
222 ip->i_din.ffs_din.di_inumber = new_ino;
223
224 /* Set a new generation number for this inode. */
225 ip->i_ffs_gen++;
226
227 /* Insert into the inode hash table. */
228 ufs_ihashins(ip);
229
230 error = ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
231 if (error) {
232 vput(vp);
233 *ap->a_vpp = NULL;
234 return (error);
235 }
236
237 *ap->a_vpp = vp;
238 if(!(vp->v_flag & VDIROP)) {
239 lfs_vref(vp);
240 ++fs->lfs_dirvcount;
241 }
242 vp->v_flag |= VDIROP;
243 VREF(ip->i_devvp);
244
245 /* Set superblock modified bit and increment file count. */
246 fs->lfs_fmod = 1;
247 ++fs->lfs_nfiles;
248 return (0);
249 }
250
251 /* Create a new vnode/inode pair and initialize what fields we can. */
252 int
253 lfs_vcreate(mp, ino, vpp)
254 struct mount *mp;
255 ino_t ino;
256 struct vnode **vpp;
257 {
258 extern int (**lfs_vnodeop_p) __P((void *));
259 struct inode *ip;
260 struct ufsmount *ump;
261 int error;
262 #ifdef QUOTA
263 int i;
264 #endif
265
266 /* Create the vnode. */
267 if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) != 0) {
268 *vpp = NULL;
269 return (error);
270 }
271
272 /* Get a pointer to the private mount structure. */
273 ump = VFSTOUFS(mp);
274
275 /* Initialize the inode. */
276 ip = pool_get(&lfs_inode_pool, PR_WAITOK);
277 lockinit(&ip->i_lock, PINOD, "lfsinode", 0, 0);
278 (*vpp)->v_data = ip;
279 ip->i_vnode = *vpp;
280 ip->i_devvp = ump->um_devvp;
281 ip->i_flag = IN_MODIFIED;
282 ip->i_dev = ump->um_dev;
283 ip->i_number = ip->i_din.ffs_din.di_inumber = ino;
284 ip->i_lfs = ump->um_lfs;
285 #ifdef QUOTA
286 for (i = 0; i < MAXQUOTAS; i++)
287 ip->i_dquot[i] = NODQUOT;
288 #endif
289 ip->i_lockf = 0;
290 ip->i_diroff = 0;
291 ip->i_ffs_mode = 0;
292 ip->i_ffs_size = 0;
293 ip->i_ffs_blocks = 0;
294 ++ump->um_lfs->lfs_uinodes;
295 return (0);
296 }
297
298 /* Free an inode. */
299 /* ARGUSED */
300 int
301 lfs_vfree(v)
302 void *v;
303 {
304 struct vop_vfree_args /* {
305 struct vnode *a_pvp;
306 ino_t a_ino;
307 int a_mode;
308 } */ *ap = v;
309 SEGUSE *sup;
310 struct buf *bp;
311 struct ifile *ifp;
312 struct inode *ip;
313 struct lfs *fs;
314 ufs_daddr_t old_iaddr;
315 ino_t ino;
316
317 /* Get the inode number and file system. */
318 ip = VTOI(ap->a_pvp);
319 fs = ip->i_lfs;
320 ino = ip->i_number;
321
322 while(WRITEINPROG(ap->a_pvp)
323 || fs->lfs_seglock
324 #ifndef USE_UFS_HASHLOCK
325 || free_lock
326 #else
327 || lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0)
328 #endif
329 )
330 {
331 if (WRITEINPROG(ap->a_pvp)) {
332 tsleep(ap->a_pvp, (PRIBIO+1), "lfs_vfree", 0);
333 }
334 if(free_lock)
335 tsleep(&free_lock, PRIBIO+1, "free_lock", 0);
336 if (fs->lfs_seglock) {
337 if (fs->lfs_lockpid == curproc->p_pid) {
338 #if 0
339 panic("lfs_vfree: we hold the seglock");
340 #else
341 break;
342 #endif
343 } else {
344 tsleep(&fs->lfs_seglock, PRIBIO + 1, "lfs_vfr1", 0);
345 }
346 }
347 }
348 #ifndef USE_UFS_HASHLOCK
349 free_lock++;
350 #endif
351
352 if (ip->i_flag & IN_CLEANING) {
353 --fs->lfs_uinodes;
354 ip->i_flag &= ~IN_CLEANING;
355 }
356 if (ip->i_flag & IN_MODIFIED) {
357 --fs->lfs_uinodes;
358 ip->i_flag &=
359 ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
360 }
361 #ifdef DEBUG_LFS
362 if((int32_t)fs->lfs_uinodes<0) {
363 printf("U1");
364 fs->lfs_uinodes=0;
365 }
366 #endif
367 /*
368 * Set the ifile's inode entry to unused, increment its version number
369 * and link it into the free chain.
370 */
371 LFS_IENTRY(ifp, fs, ino, bp);
372 old_iaddr = ifp->if_daddr;
373 ifp->if_daddr = LFS_UNUSED_DADDR;
374 ++ifp->if_version;
375 ifp->if_nextfree = fs->lfs_free;
376 fs->lfs_free = ino;
377 (void) VOP_BWRITE(bp);
378 #ifdef DIAGNOSTIC
379 if(fs->lfs_free == LFS_UNUSED_INUM) {
380 panic("inode 0 freed");
381 }
382 #endif /* DIAGNOSTIC */
383 if (old_iaddr != LFS_UNUSED_DADDR) {
384 LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
385 if (sup->su_nbytes < DINODE_SIZE) {
386 sup->su_nbytes = DINODE_SIZE;
387 #ifdef DIAGNOSTIC
388 panic("lfs_vfree: negative byte count (segment %d)\n",
389 datosn(fs, old_iaddr));
390 #endif /* DIAGNOSTIC */
391 }
392 sup->su_nbytes -= DINODE_SIZE;
393 (void) VOP_BWRITE(bp);
394 }
395 #ifndef USE_UFS_HASHLOCK
396 free_lock--;
397 wakeup(&free_lock);
398 #else
399 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
400 #endif
401
402 /* Set superblock modified bit and decrement file count. */
403 fs->lfs_fmod = 1;
404 --fs->lfs_nfiles;
405
406 return (0);
407 }
408