lfs_alloc.c revision 1.17 1 /* $NetBSD: lfs_alloc.c,v 1.17 1999/03/10 00:20:00 perseant 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 #include "opt_uvm.h"
76 #endif
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/kernel.h>
81 #include <sys/buf.h>
82 #include <sys/vnode.h>
83 #include <sys/syslog.h>
84 #include <sys/mount.h>
85 #include <sys/malloc.h>
86 #include <sys/pool.h>
87
88 #include <vm/vm.h>
89
90 #include <ufs/ufs/quota.h>
91 #include <ufs/ufs/inode.h>
92 #include <ufs/ufs/ufsmount.h>
93 #include <ufs/ufs/ufs_extern.h>
94
95 #include <ufs/lfs/lfs.h>
96 #include <ufs/lfs/lfs_extern.h>
97
98 #ifndef USE_UFS_HASHLOCK
99 int free_lock = 0;
100 #else
101 extern struct lock ufs_hashlock;
102 #endif
103
104 /* Allocate a new inode. */
105 /* ARGSUSED */
106 int
107 lfs_valloc(v)
108 void *v;
109 {
110 struct vop_valloc_args /* {
111 struct vnode *a_pvp;
112 int a_mode;
113 struct ucred *a_cred;
114 struct vnode **a_vpp;
115 } */ *ap = v;
116 struct lfs *fs;
117 struct buf *bp;
118 struct ifile *ifp;
119 struct inode *ip;
120 struct vnode *vp;
121 ufs_daddr_t blkno;
122 ino_t new_ino;
123 u_long i, max;
124 int error;
125
126 fs = VTOI(ap->a_pvp)->i_lfs;
127
128 /*
129 * Prevent a race getting lfs_free - XXX - KS
130 * (this should be a proper lock, in struct lfs)
131 */
132
133 #ifndef USE_UFS_HASHLOCK
134 while(free_lock)
135 tsleep(&free_lock, PRIBIO+1, "lfs_free", 0);
136 free_lock++;
137 #else
138 while(lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0))
139 ;
140 #endif
141
142 /* Get the head of the freelist. */
143 new_ino = fs->lfs_free;
144 #ifdef DIAGNOSTIC
145 if(new_ino == LFS_UNUSED_INUM) {
146 #ifdef DEBUG
147 lfs_dump_super(fs);
148 #endif /* DEBUG */
149 panic("inode 0 allocated [1]");
150 }
151 #endif /* DIAGNOSTIC */
152 #ifdef ALLOCPRINT
153 printf("lfs_ialloc: allocate inode %d\n", new_ino);
154 #endif
155
156 /*
157 * Remove the inode from the free list and write the new start
158 * of the free list into the superblock.
159 */
160 LFS_IENTRY(ifp, fs, new_ino, bp);
161 if (ifp->if_daddr != LFS_UNUSED_DADDR)
162 panic("lfs_ialloc: inuse inode %d on the free list", new_ino);
163 fs->lfs_free = ifp->if_nextfree;
164 brelse(bp);
165
166 /* Extend IFILE so that the next lfs_valloc will succeed. */
167 if (fs->lfs_free == LFS_UNUSED_INUM) {
168 vp = fs->lfs_ivnode;
169 VOP_LOCK(vp,LK_EXCLUSIVE);
170 ip = VTOI(vp);
171 blkno = lblkno(fs, ip->i_ffs_size);
172 lfs_balloc(vp, 0, fs->lfs_bsize, blkno, &bp);
173 ip->i_ffs_size += fs->lfs_bsize;
174 #ifdef UVM
175 uvm_vnp_setsize(vp, ip->i_ffs_size);
176 (void)uvm_vnp_uncache(vp);
177 #else
178 vnode_pager_setsize(vp, ip->i_ffs_size);
179 vnode_pager_uncache(vp);
180 #endif
181
182 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
183 fs->lfs_ifpb;
184 fs->lfs_free = i;
185 #ifdef DIAGNOSTIC
186 if(fs->lfs_free == LFS_UNUSED_INUM)
187 panic("inode 0 allocated [2]");
188 #endif /* DIAGNOSTIC */
189 max = i + fs->lfs_ifpb;
190 for (ifp = (struct ifile *)bp->b_data; i < max; ++ifp) {
191 ifp->if_version = 1;
192 ifp->if_daddr = LFS_UNUSED_DADDR;
193 ifp->if_nextfree = ++i;
194 }
195 ifp--;
196 ifp->if_nextfree = LFS_UNUSED_INUM;
197 VOP_UNLOCK(vp,0);
198 if ((error = VOP_BWRITE(bp)) != 0) {
199 #ifndef USE_UFS_HASHLOCK
200 free_lock--;
201 wakeup(&free_lock);
202 #else
203 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
204 #endif
205 return (error);
206 }
207 }
208
209 #ifndef USE_UFS_HASHLOCK
210 free_lock--;
211 wakeup(&free_lock);
212 #else
213 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
214 #endif
215
216 #ifdef DIAGNOSTIC
217 if(fs->lfs_free == LFS_UNUSED_INUM)
218 panic("inode 0 allocated [3]");
219 #endif /* DIAGNOSTIC */
220
221 /* Create a vnode to associate with the inode. */
222 if ((error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp)) != 0)
223 return (error);
224
225 ip = VTOI(vp);
226 /* Zero out the direct and indirect block addresses. */
227 bzero(&ip->i_din, sizeof(ip->i_din));
228 ip->i_din.ffs_din.di_inumber = new_ino;
229
230 /* Set a new generation number for this inode. */
231 ip->i_ffs_gen++;
232
233 /* Insert into the inode hash table. */
234 ufs_ihashins(ip);
235
236 error = ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
237 if (error) {
238 vput(vp);
239 *ap->a_vpp = NULL;
240 return (error);
241 }
242
243 *ap->a_vpp = vp;
244 if(!(vp->v_flag & VDIROP)) {
245 lfs_vref(vp);
246 ++fs->lfs_dirvcount;
247 }
248 vp->v_flag |= VDIROP;
249 VREF(ip->i_devvp);
250
251 /* Set superblock modified bit and increment file count. */
252 fs->lfs_fmod = 1;
253 ++fs->lfs_nfiles;
254 return (0);
255 }
256
257 /* Create a new vnode/inode pair and initialize what fields we can. */
258 int
259 lfs_vcreate(mp, ino, vpp)
260 struct mount *mp;
261 ino_t ino;
262 struct vnode **vpp;
263 {
264 extern int (**lfs_vnodeop_p) __P((void *));
265 struct inode *ip;
266 struct ufsmount *ump;
267 int error;
268 #ifdef QUOTA
269 int i;
270 #endif
271
272 /* Create the vnode. */
273 if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) != 0) {
274 *vpp = NULL;
275 return (error);
276 }
277
278 /* Get a pointer to the private mount structure. */
279 ump = VFSTOUFS(mp);
280
281 /* Initialize the inode. */
282 ip = pool_get(&lfs_inode_pool, PR_WAITOK);
283 lockinit(&ip->i_lock, PINOD, "lfsinode", 0, 0);
284 (*vpp)->v_data = ip;
285 ip->i_vnode = *vpp;
286 ip->i_devvp = ump->um_devvp;
287 ip->i_flag = IN_MODIFIED;
288 ip->i_dev = ump->um_dev;
289 ip->i_number = ip->i_din.ffs_din.di_inumber = ino;
290 ip->i_lfs = ump->um_lfs;
291 #ifdef QUOTA
292 for (i = 0; i < MAXQUOTAS; i++)
293 ip->i_dquot[i] = NODQUOT;
294 #endif
295 ip->i_lockf = 0;
296 ip->i_diroff = 0;
297 ip->i_ffs_mode = 0;
298 ip->i_ffs_size = 0;
299 ip->i_ffs_blocks = 0;
300 ++ump->um_lfs->lfs_uinodes;
301 return (0);
302 }
303
304 /* Free an inode. */
305 /* ARGUSED */
306 int
307 lfs_vfree(v)
308 void *v;
309 {
310 struct vop_vfree_args /* {
311 struct vnode *a_pvp;
312 ino_t a_ino;
313 int a_mode;
314 } */ *ap = v;
315 SEGUSE *sup;
316 struct buf *bp;
317 struct ifile *ifp;
318 struct inode *ip;
319 struct lfs *fs;
320 ufs_daddr_t old_iaddr;
321 ino_t ino;
322
323 /* Get the inode number and file system. */
324 ip = VTOI(ap->a_pvp);
325 fs = ip->i_lfs;
326 ino = ip->i_number;
327
328 while(WRITEINPROG(ap->a_pvp)
329 || fs->lfs_seglock
330 #ifndef USE_UFS_HASHLOCK
331 || free_lock
332 #else
333 || lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0)
334 #endif
335 )
336 {
337 if (WRITEINPROG(ap->a_pvp)) {
338 tsleep(ap->a_pvp, (PRIBIO+1), "lfs_vfree", 0);
339 }
340 if(free_lock)
341 tsleep(&free_lock, PRIBIO+1, "free_lock", 0);
342 if (fs->lfs_seglock) {
343 if (fs->lfs_lockpid == curproc->p_pid) {
344 #if 0
345 panic("lfs_vfree: we hold the seglock");
346 #else
347 break;
348 #endif
349 } else {
350 tsleep(&fs->lfs_seglock, PRIBIO + 1, "lfs_vfr1", 0);
351 }
352 }
353 }
354 #ifndef USE_UFS_HASHLOCK
355 free_lock++;
356 #endif
357
358 if (ip->i_flag & IN_CLEANING) {
359 --fs->lfs_uinodes;
360 ip->i_flag &= ~IN_CLEANING;
361 }
362 if (ip->i_flag & IN_MODIFIED) {
363 --fs->lfs_uinodes;
364 ip->i_flag &=
365 ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
366 }
367 #ifdef DEBUG_LFS
368 if((int32_t)fs->lfs_uinodes<0) {
369 printf("U1");
370 fs->lfs_uinodes=0;
371 }
372 #endif
373 /*
374 * Set the ifile's inode entry to unused, increment its version number
375 * and link it into the free chain.
376 */
377 LFS_IENTRY(ifp, fs, ino, bp);
378 old_iaddr = ifp->if_daddr;
379 ifp->if_daddr = LFS_UNUSED_DADDR;
380 ++ifp->if_version;
381 ifp->if_nextfree = fs->lfs_free;
382 fs->lfs_free = ino;
383 (void) VOP_BWRITE(bp);
384 #ifdef DIAGNOSTIC
385 if(fs->lfs_free == LFS_UNUSED_INUM) {
386 panic("inode 0 freed");
387 }
388 #endif /* DIAGNOSTIC */
389 if (old_iaddr != LFS_UNUSED_DADDR) {
390 LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
391 if (sup->su_nbytes < DINODE_SIZE) {
392 sup->su_nbytes = DINODE_SIZE;
393 #ifdef DIAGNOSTIC
394 panic("lfs_vfree: negative byte count (segment %d)\n",
395 datosn(fs, old_iaddr));
396 #endif /* DIAGNOSTIC */
397 }
398 sup->su_nbytes -= DINODE_SIZE;
399 (void) VOP_BWRITE(bp);
400 }
401 #ifndef USE_UFS_HASHLOCK
402 free_lock--;
403 wakeup(&free_lock);
404 #else
405 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
406 #endif
407
408 /* Set superblock modified bit and decrement file count. */
409 fs->lfs_fmod = 1;
410 --fs->lfs_nfiles;
411
412 return (0);
413 }
414