lfs_alloc.c revision 1.96 1 /* $NetBSD: lfs_alloc.c,v 1.96 2006/07/20 23:49:07 perseant Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 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. 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 * @(#)lfs_alloc.c 8.4 (Berkeley) 1/4/94
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.96 2006/07/20 23:49:07 perseant Exp $");
71
72 #if defined(_KERNEL_OPT)
73 #include "opt_quota.h"
74 #endif
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/buf.h>
80 #include <sys/lock.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 #include <sys/proc.h>
87 #include <sys/tree.h>
88 #include <sys/kauth.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 extern struct lock ufs_hashlock;
99
100 /* Constants for inode free bitmap */
101 #define BMSHIFT 5 /* 2 ** 5 = 32 */
102 #define BMMASK ((1 << BMSHIFT) - 1)
103 #define SET_BITMAP_FREE(F, I) do { \
104 DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d set\n", (int)(I), \
105 (int)((I) >> BMSHIFT), (int)((I) & BMMASK))); \
106 (F)->lfs_ino_bitmap[(I) >> BMSHIFT] |= (1 << ((I) & BMMASK)); \
107 } while (0)
108 #define CLR_BITMAP_FREE(F, I) do { \
109 DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d clr\n", (int)(I), \
110 (int)((I) >> BMSHIFT), (int)((I) & BMMASK))); \
111 (F)->lfs_ino_bitmap[(I) >> BMSHIFT] &= ~(1 << ((I) & BMMASK)); \
112 } while(0)
113
114 #define ISSET_BITMAP_FREE(F, I) \
115 ((F)->lfs_ino_bitmap[(I) >> BMSHIFT] & (1 << ((I) & BMMASK)))
116
117 /*
118 * Add a new block to the Ifile, to accommodate future file creations.
119 * Called with the segment lock held.
120 */
121 int
122 lfs_extend_ifile(struct lfs *fs, kauth_cred_t cred)
123 {
124 struct vnode *vp;
125 struct inode *ip;
126 IFILE *ifp;
127 IFILE_V1 *ifp_v1;
128 struct buf *bp, *cbp;
129 int error;
130 daddr_t i, blkno, xmax;
131 ino_t oldlast, maxino;
132 CLEANERINFO *cip;
133
134 ASSERT_SEGLOCK(fs);
135
136 vp = fs->lfs_ivnode;
137 ip = VTOI(vp);
138 blkno = lblkno(fs, ip->i_size);
139 if ((error = lfs_balloc(vp, ip->i_size, fs->lfs_bsize, cred, 0,
140 &bp)) != 0) {
141 return (error);
142 }
143 ip->i_size += fs->lfs_bsize;
144 ip->i_ffs1_size = ip->i_size;
145 uvm_vnp_setsize(vp, ip->i_size);
146
147 maxino = ((ip->i_size >> fs->lfs_bshift) - fs->lfs_cleansz -
148 fs->lfs_segtabsz) * fs->lfs_ifpb;
149 fs->lfs_ino_bitmap = (lfs_bm_t *)
150 realloc(fs->lfs_ino_bitmap, ((maxino + BMMASK) >> BMSHIFT) *
151 sizeof(lfs_bm_t), M_SEGMENT, M_WAITOK);
152 KASSERT(fs->lfs_ino_bitmap != NULL);
153
154 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
155 fs->lfs_ifpb;
156
157 /*
158 * We insert the new inodes at the head of the free list.
159 * Under normal circumstances, the free list is empty here,
160 * so we are also incidentally placing them at the end (which
161 * we must do if we are to keep them in order).
162 */
163 LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
164 LFS_PUT_HEADFREE(fs, cip, cbp, i);
165 #ifdef DIAGNOSTIC
166 if (fs->lfs_freehd == LFS_UNUSED_INUM)
167 panic("inode 0 allocated [2]");
168 #endif /* DIAGNOSTIC */
169 xmax = i + fs->lfs_ifpb;
170
171 if (fs->lfs_version == 1) {
172 for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) {
173 SET_BITMAP_FREE(fs, i);
174 ifp_v1->if_version = 1;
175 ifp_v1->if_daddr = LFS_UNUSED_DADDR;
176 ifp_v1->if_nextfree = ++i;
177 }
178 ifp_v1--;
179 ifp_v1->if_nextfree = oldlast;
180 } else {
181 for (ifp = (IFILE *)bp->b_data; i < xmax; ++ifp) {
182 SET_BITMAP_FREE(fs, i);
183 ifp->if_version = 1;
184 ifp->if_daddr = LFS_UNUSED_DADDR;
185 ifp->if_nextfree = ++i;
186 }
187 ifp--;
188 ifp->if_nextfree = oldlast;
189 }
190 LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1);
191
192 (void) LFS_BWRITE_LOG(bp); /* Ifile */
193
194 return 0;
195 }
196
197 /* Allocate a new inode. */
198 /* ARGSUSED */
199 /* VOP_BWRITE 2i times */
200 int
201 lfs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred, struct vnode **vpp)
202 {
203 struct lfs *fs;
204 struct buf *bp, *cbp;
205 struct ifile *ifp;
206 ino_t new_ino;
207 int error;
208 int new_gen;
209 CLEANERINFO *cip;
210
211 fs = VTOI(pvp)->i_lfs;
212 if (fs->lfs_ronly)
213 return EROFS;
214
215 ASSERT_NO_SEGLOCK(fs);
216
217 lfs_seglock(fs, SEGM_PROT);
218 vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
219
220 /* Get the head of the freelist. */
221 LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
222 KASSERT(new_ino != LFS_UNUSED_INUM && new_ino != LFS_IFILE_INUM);
223
224 DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %lld\n",
225 (long long)new_ino));
226
227 /*
228 * Remove the inode from the free list and write the new start
229 * of the free list into the superblock.
230 */
231 CLR_BITMAP_FREE(fs, new_ino);
232 LFS_IENTRY(ifp, fs, new_ino, bp);
233 if (ifp->if_daddr != LFS_UNUSED_DADDR)
234 panic("lfs_valloc: inuse inode %llu on the free list",
235 (unsigned long long)new_ino);
236 LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
237 DLOG((DLOG_ALLOC, "lfs_valloc: headfree %lld -> %lld\n",
238 (long long)new_ino, (long long)ifp->if_nextfree));
239
240 new_gen = ifp->if_version; /* version was updated by vfree */
241 brelse(bp);
242
243 /* Extend IFILE so that the next lfs_valloc will succeed. */
244 if (fs->lfs_freehd == LFS_UNUSED_INUM) {
245 if ((error = lfs_extend_ifile(fs, cred)) != 0) {
246 LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
247 VOP_UNLOCK(fs->lfs_ivnode, 0);
248 lfs_segunlock(fs);
249 return error;
250 }
251 }
252 #ifdef DIAGNOSTIC
253 if (fs->lfs_freehd == LFS_UNUSED_INUM)
254 panic("inode 0 allocated [3]");
255 #endif /* DIAGNOSTIC */
256
257 /* Set superblock modified bit and increment file count. */
258 simple_lock(&fs->lfs_interlock);
259 fs->lfs_fmod = 1;
260 simple_unlock(&fs->lfs_interlock);
261 ++fs->lfs_nfiles;
262
263 VOP_UNLOCK(fs->lfs_ivnode, 0);
264 lfs_segunlock(fs);
265
266 return lfs_ialloc(fs, pvp, new_ino, new_gen, vpp);
267 }
268
269 /*
270 * Finish allocating a new inode, given an inode and generation number.
271 */
272 int
273 lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
274 struct vnode **vpp)
275 {
276 struct inode *ip;
277 struct vnode *vp;
278
279 ASSERT_NO_SEGLOCK(fs);
280
281 vp = *vpp;
282 lockmgr(&ufs_hashlock, LK_EXCLUSIVE, 0);
283 /* Create an inode to associate with the vnode. */
284 lfs_vcreate(pvp->v_mount, new_ino, vp);
285
286 ip = VTOI(vp);
287 LFS_SET_UINO(ip, IN_CHANGE);
288 /* on-disk structure has been zeroed out by lfs_vcreate */
289 ip->i_din.ffs1_din->di_inumber = new_ino;
290
291 /* Note no blocks yet */
292 ip->i_lfs_hiblk = -1;
293
294 /* Set a new generation number for this inode. */
295 if (new_gen) {
296 ip->i_gen = new_gen;
297 ip->i_ffs1_gen = new_gen;
298 }
299
300 /* Insert into the inode hash table. */
301 ufs_ihashins(ip);
302 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
303
304 ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, vpp);
305 vp = *vpp;
306 ip = VTOI(vp);
307
308 memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
309
310 uvm_vnp_setsize(vp, 0);
311 lfs_mark_vnode(vp);
312 genfs_node_init(vp, &lfs_genfsops);
313 VREF(ip->i_devvp);
314 return (0);
315 }
316
317 /* Create a new vnode/inode pair and initialize what fields we can. */
318 void
319 lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp)
320 {
321 struct inode *ip;
322 struct ufs1_dinode *dp;
323 struct ufsmount *ump;
324 #ifdef QUOTA
325 int i;
326 #endif
327
328 /* Get a pointer to the private mount structure. */
329 ump = VFSTOUFS(mp);
330
331 ASSERT_NO_SEGLOCK(ump->um_lfs);
332
333 /* Initialize the inode. */
334 ip = pool_get(&lfs_inode_pool, PR_WAITOK);
335 memset(ip, 0, sizeof(*ip));
336 dp = pool_get(&lfs_dinode_pool, PR_WAITOK);
337 memset(dp, 0, sizeof(*dp));
338 ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK);
339 memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs));
340 vp->v_data = ip;
341 ip->i_din.ffs1_din = dp;
342 ip->i_ump = ump;
343 ip->i_vnode = vp;
344 ip->i_devvp = ump->um_devvp;
345 ip->i_dev = ump->um_dev;
346 ip->i_number = dp->di_inumber = ino;
347 ip->i_lfs = ump->um_lfs;
348 ip->i_lfs_effnblks = 0;
349 SPLAY_INIT(&ip->i_lfs_lbtree);
350 ip->i_lfs_nbtree = 0;
351 LIST_INIT(&ip->i_lfs_segdhd);
352 #ifdef QUOTA
353 for (i = 0; i < MAXQUOTAS; i++)
354 ip->i_dquot[i] = NODQUOT;
355 #endif
356 #ifdef DEBUG
357 if (ino == LFS_IFILE_INUM)
358 vp->v_vnlock->lk_wmesg = "inlock";
359 #endif
360 }
361
362 #if 0
363 /*
364 * Find the highest-numbered allocated inode.
365 * This will be used to shrink the Ifile.
366 */
367 static inline ino_t
368 lfs_last_alloc_ino(struct lfs *fs)
369 {
370 ino_t ino, maxino;
371
372 maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
373 fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
374 for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) {
375 if (ISSET_BITMAP_FREE(fs, ino) == 0)
376 break;
377 }
378 return ino;
379 }
380 #endif
381
382 /*
383 * Find the previous (next lowest numbered) free inode, if any.
384 * If there is none, return LFS_UNUSED_INUM.
385 */
386 static inline ino_t
387 lfs_freelist_prev(struct lfs *fs, ino_t ino)
388 {
389 ino_t tino, bound, bb, freehdbb;
390
391 if (fs->lfs_freehd == LFS_UNUSED_INUM) /* No free inodes at all */
392 return LFS_UNUSED_INUM;
393
394 /* Search our own word first */
395 bound = ino & ~BMMASK;
396 for (tino = ino - 1; tino >= bound && tino > LFS_UNUSED_INUM; tino--)
397 if (ISSET_BITMAP_FREE(fs, tino))
398 return tino;
399 /* If there are no lower words to search, just return */
400 if (ino >> BMSHIFT == 0)
401 return LFS_UNUSED_INUM;
402
403 /*
404 * Find a word with a free inode in it. We have to be a bit
405 * careful here since ino_t is unsigned.
406 */
407 freehdbb = (fs->lfs_freehd >> BMSHIFT);
408 for (bb = (ino >> BMSHIFT) - 1; bb >= freehdbb && bb > 0; --bb)
409 if (fs->lfs_ino_bitmap[bb])
410 break;
411 if (fs->lfs_ino_bitmap[bb] == 0)
412 return LFS_UNUSED_INUM;
413
414 /* Search the word we found */
415 for (tino = (bb << BMSHIFT) | BMMASK; tino >= (bb << BMSHIFT) &&
416 tino > LFS_UNUSED_INUM; tino--)
417 if (ISSET_BITMAP_FREE(fs, tino))
418 break;
419
420 if (tino <= LFS_IFILE_INUM)
421 tino = LFS_UNUSED_INUM;
422
423 return tino;
424 }
425
426 /* Free an inode. */
427 /* ARGUSED */
428 /* VOP_BWRITE 2i times */
429 int
430 lfs_vfree(struct vnode *vp, ino_t ino, int mode)
431 {
432 SEGUSE *sup;
433 CLEANERINFO *cip;
434 struct buf *cbp, *bp;
435 struct ifile *ifp;
436 struct inode *ip;
437 struct lfs *fs;
438 daddr_t old_iaddr;
439 ino_t otail;
440 int s;
441
442 /* Get the inode number and file system. */
443 ip = VTOI(vp);
444 fs = ip->i_lfs;
445 ino = ip->i_number;
446
447 ASSERT_NO_SEGLOCK(fs);
448 DLOG((DLOG_ALLOC, "lfs_vfree: free ino %lld\n", (long long)ino));
449
450 /* Drain of pending writes */
451 simple_lock(&vp->v_interlock);
452 s = splbio();
453 if (fs->lfs_version > 1 && WRITEINPROG(vp))
454 ltsleep(vp, (PRIBIO+1), "lfs_vfree", 0, &vp->v_interlock);
455 splx(s);
456 simple_unlock(&vp->v_interlock);
457
458 lfs_seglock(fs, SEGM_PROT);
459 vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
460
461 lfs_unmark_vnode(vp);
462 if (vp->v_flag & VDIROP) {
463 vp->v_flag &= ~VDIROP;
464 simple_lock(&fs->lfs_interlock);
465 simple_lock(&lfs_subsys_lock);
466 --lfs_dirvcount;
467 simple_unlock(&lfs_subsys_lock);
468 --fs->lfs_dirvcount;
469 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
470 simple_unlock(&fs->lfs_interlock);
471 wakeup(&fs->lfs_dirvcount);
472 wakeup(&lfs_dirvcount);
473 lfs_vunref(vp);
474
475 /*
476 * If this inode is not going to be written any more, any
477 * segment accounting left over from its truncation needs
478 * to occur at the end of the next dirops flush. Attach
479 * them to the fs-wide list for that purpose.
480 */
481 if (LIST_FIRST(&ip->i_lfs_segdhd) != NULL) {
482 struct segdelta *sd;
483
484 while((sd = LIST_FIRST(&ip->i_lfs_segdhd)) != NULL) {
485 LIST_REMOVE(sd, list);
486 LIST_INSERT_HEAD(&fs->lfs_segdhd, sd, list);
487 }
488 }
489 } else {
490 /*
491 * If it's not a dirop, we can finalize right away.
492 */
493 lfs_finalize_ino_seguse(fs, ip);
494 }
495
496 LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
497 ip->i_flag &= ~IN_ALLMOD;
498 ip->i_lfs_iflags |= LFSI_DELETED;
499
500 /*
501 * Set the ifile's inode entry to unused, increment its version number
502 * and link it onto the free chain.
503 */
504 SET_BITMAP_FREE(fs, ino);
505 LFS_IENTRY(ifp, fs, ino, bp);
506 old_iaddr = ifp->if_daddr;
507 ifp->if_daddr = LFS_UNUSED_DADDR;
508 ++ifp->if_version;
509 if (fs->lfs_version == 1) {
510 LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
511 LFS_PUT_HEADFREE(fs, cip, cbp, ino);
512 (void) LFS_BWRITE_LOG(bp); /* Ifile */
513 } else {
514 ino_t tino, onf;
515
516 ifp->if_nextfree = LFS_UNUSED_INUM;
517 (void) LFS_BWRITE_LOG(bp); /* Ifile */
518
519 tino = lfs_freelist_prev(fs, ino);
520 if (tino == LFS_UNUSED_INUM) {
521 /* Nothing free below us, put us on the head */
522 LFS_IENTRY(ifp, fs, ino, bp);
523 LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
524 LFS_PUT_HEADFREE(fs, cip, cbp, ino);
525 DLOG((DLOG_ALLOC, "lfs_vfree: headfree %lld -> %lld\n",
526 (long long)ifp->if_nextfree, (long long)ino));
527 LFS_BWRITE_LOG(bp); /* Ifile */
528
529 /* If the list was empty, set tail too */
530 LFS_GET_TAILFREE(fs, cip, cbp, &otail);
531 if (otail == LFS_UNUSED_INUM) {
532 LFS_PUT_TAILFREE(fs, cip, cbp, ino);
533 DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
534 "-> %lld\n", (long long)otail,
535 (long long)ino));
536 }
537 } else {
538 /*
539 * Insert this inode into the list after tino.
540 * We hold the segment lock so we don't have to
541 * worry about blocks being written out of order.
542 */
543 DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld "
544 " after %lld\n", ino, tino));
545
546 LFS_IENTRY(ifp, fs, tino, bp);
547 onf = ifp->if_nextfree;
548 ifp->if_nextfree = ino;
549 LFS_BWRITE_LOG(bp); /* Ifile */
550
551 LFS_IENTRY(ifp, fs, ino, bp);
552 ifp->if_nextfree = onf;
553 LFS_BWRITE_LOG(bp); /* Ifile */
554
555 /* If we're last, put us on the tail */
556 if (onf == LFS_UNUSED_INUM) {
557 LFS_GET_TAILFREE(fs, cip, cbp, &otail);
558 LFS_PUT_TAILFREE(fs, cip, cbp, ino);
559 DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
560 "-> %lld\n", (long long)otail,
561 (long long)ino));
562 }
563 }
564 }
565 #ifdef DIAGNOSTIC
566 if (ino == LFS_UNUSED_INUM) {
567 panic("inode 0 freed");
568 }
569 #endif /* DIAGNOSTIC */
570 if (old_iaddr != LFS_UNUSED_DADDR) {
571 LFS_SEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp);
572 #ifdef DIAGNOSTIC
573 if (sup->su_nbytes < sizeof (struct ufs1_dinode)) {
574 printf("lfs_vfree: negative byte count"
575 " (segment %" PRIu32 " short by %d)\n",
576 dtosn(fs, old_iaddr),
577 (int)sizeof (struct ufs1_dinode) -
578 sup->su_nbytes);
579 panic("lfs_vfree: negative byte count");
580 sup->su_nbytes = sizeof (struct ufs1_dinode);
581 }
582 #endif
583 sup->su_nbytes -= sizeof (struct ufs1_dinode);
584 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp); /* Ifile */
585 }
586
587 /* Set superblock modified bit and decrement file count. */
588 simple_lock(&fs->lfs_interlock);
589 fs->lfs_fmod = 1;
590 simple_unlock(&fs->lfs_interlock);
591 --fs->lfs_nfiles;
592
593 VOP_UNLOCK(fs->lfs_ivnode, 0);
594 lfs_segunlock(fs);
595
596 return (0);
597 }
598
599 /*
600 * Sort the freelist and set up the free-inode bitmap.
601 * To be called by lfs_mountfs().
602 */
603 void
604 lfs_order_freelist(struct lfs *fs)
605 {
606 CLEANERINFO *cip;
607 IFILE *ifp = NULL;
608 struct buf *bp;
609 ino_t ino, firstino, lastino, maxino;
610
611 ASSERT_NO_SEGLOCK(fs);
612 lfs_seglock(fs, SEGM_PROT);
613
614 maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
615 fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
616 fs->lfs_ino_bitmap = (lfs_bm_t *)
617 malloc(((maxino + BMMASK) >> BMSHIFT) * sizeof(lfs_bm_t),
618 M_SEGMENT, M_WAITOK | M_ZERO);
619 KASSERT(fs->lfs_ino_bitmap != NULL);
620
621 firstino = lastino = LFS_UNUSED_INUM;
622 for (ino = 0; ino < maxino; ino++) {
623 if (ino % fs->lfs_ifpb == 0)
624 LFS_IENTRY(ifp, fs, ino, bp);
625 else
626 ++ifp;
627
628 /* Don't put zero or ifile on the free list */
629 if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM)
630 continue;
631
632 if (ifp->if_daddr == LFS_UNUSED_DADDR) {
633 if (firstino == LFS_UNUSED_INUM)
634 firstino = ino;
635 else {
636 brelse(bp);
637
638 LFS_IENTRY(ifp, fs, lastino, bp);
639 ifp->if_nextfree = ino;
640 LFS_BWRITE_LOG(bp);
641
642 LFS_IENTRY(ifp, fs, ino, bp);
643 }
644 lastino = ino;
645
646 SET_BITMAP_FREE(fs, ino);
647 }
648
649 if ((ino + 1) % fs->lfs_ifpb == 0)
650 brelse(bp);
651 }
652
653 LFS_PUT_HEADFREE(fs, cip, bp, firstino);
654 LFS_PUT_TAILFREE(fs, cip, bp, lastino);
655
656 lfs_segunlock(fs);
657 }
658