lfs_alloc.c revision 1.76.2.5 1 /* $NetBSD: lfs_alloc.c,v 1.76.2.5 2006/05/20 22:09:28 riz 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.76.2.5 2006/05/20 22:09:28 riz 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
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 extern struct lock ufs_hashlock;
98
99 static int extend_ifile(struct lfs *, struct ucred *);
100 static int lfs_ialloc(struct lfs *, struct vnode *, ino_t, int,
101 struct vnode **);
102
103 /* Constants for inode free bitmap */
104 #define BYTESHIFT 3 /* 8 = 2 ** 3 */
105 #define BYTEMASK 0x7 /* 8 - 1 */
106 #define SET_BITMAP_FREE(F, I) \
107 (F)->lfs_ino_bitmap[(I) >> BYTESHIFT] |= (1 << ((I) & BYTEMASK))
108 #define CLR_BITMAP_FREE(F, I) \
109 (F)->lfs_ino_bitmap[(I) >> BYTESHIFT] &= ~(1 << ((I) & BYTEMASK))
110 #define ISSET_BITMAP_FREE(F, I) \
111 ((F)->lfs_ino_bitmap[(I) >> BYTESHIFT] & (1 << ((I) & BYTEMASK)))
112
113 /*
114 * Allocate a particular inode with a particular version number, freeing
115 * any previous versions of this inode that may have gone before.
116 * Used by the roll-forward code.
117 *
118 * XXX this function does not have appropriate locking to be used on a live fs;
119 * XXX but something similar could probably be used for an "undelete" call.
120 *
121 * Called with the Ifile inode locked.
122 */
123 int
124 lfs_rf_valloc(struct lfs *fs, ino_t ino, int vers, struct proc *p,
125 struct vnode **vpp)
126 {
127 IFILE *ifp;
128 struct buf *bp, *cbp;
129 struct vnode *vp;
130 struct inode *ip;
131 ino_t tino, oldnext;
132 int error;
133 CLEANERINFO *cip;
134
135 ASSERT_SEGLOCK(fs); /* XXX it doesn't, really */
136
137 /*
138 * First, just try a vget. If the version number is the one we want,
139 * we don't have to do anything else. If the version number is wrong,
140 * take appropriate action.
141 */
142 error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
143 if (error == 0) {
144 DLOG((DLOG_RF, "lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp));
145
146 *vpp = vp;
147 ip = VTOI(vp);
148 if (ip->i_gen == vers)
149 return 0;
150 else if (ip->i_gen < vers) {
151 VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, p);
152 ip->i_gen = ip->i_ffs1_gen = vers;
153 LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
154 return 0;
155 } else {
156 DLOG((DLOG_RF, "ino %d: sought version %d, got %d\n",
157 ino, vers, ip->i_ffs1_gen));
158 vput(vp);
159 *vpp = NULLVP;
160 return EEXIST;
161 }
162 }
163
164 /*
165 * The inode is not in use. Find it on the free list.
166 */
167 /* If the Ifile is too short to contain this inum, extend it */
168 while (VTOI(fs->lfs_ivnode)->i_size <= (ino /
169 fs->lfs_ifpb + fs->lfs_cleansz + fs->lfs_segtabsz)
170 << fs->lfs_bshift) {
171 extend_ifile(fs, NOCRED);
172 }
173
174 LFS_IENTRY(ifp, fs, ino, bp);
175 oldnext = ifp->if_nextfree;
176 ifp->if_version = vers;
177 brelse(bp);
178
179 LFS_GET_HEADFREE(fs, cip, cbp, &ino);
180 if (ino) {
181 LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
182 } else {
183 tino = ino;
184 while (1) {
185 LFS_IENTRY(ifp, fs, tino, bp);
186 if (ifp->if_nextfree == ino ||
187 ifp->if_nextfree == LFS_UNUSED_INUM)
188 break;
189 tino = ifp->if_nextfree;
190 brelse(bp);
191 }
192 if (ifp->if_nextfree == LFS_UNUSED_INUM) {
193 brelse(bp);
194 return ENOENT;
195 }
196 ifp->if_nextfree = oldnext;
197 LFS_BWRITE_LOG(bp);
198 }
199
200 error = lfs_ialloc(fs, fs->lfs_ivnode, ino, vers, &vp);
201 if (error == 0) {
202 /*
203 * Make it VREG so we can put blocks on it. We will change
204 * this later if it turns out to be some other kind of file.
205 */
206 ip = VTOI(vp);
207 ip->i_mode = ip->i_ffs1_mode = IFREG;
208 ip->i_nlink = ip->i_ffs1_nlink = 1;
209 ip->i_ffs_effnlink = 1;
210 ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
211 ip = VTOI(vp);
212
213 DLOG((DLOG_RF, "lfs_rf_valloc: ino %d vp %p\n", ino, vp));
214
215 /* The dirop-nature of this vnode is past */
216 lfs_unmark_vnode(vp);
217 (void)lfs_vunref(vp);
218 vp->v_flag &= ~VDIROP;
219 simple_lock(&fs->lfs_interlock);
220 simple_lock(&lfs_subsys_lock);
221 --lfs_dirvcount;
222 simple_unlock(&lfs_subsys_lock);
223 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
224 simple_unlock(&fs->lfs_interlock);
225 }
226 *vpp = vp;
227 return error;
228 }
229
230 /*
231 * Add a new block to the Ifile, to accommodate future file creations.
232 * Called with the segment lock held.
233 */
234 static int
235 extend_ifile(struct lfs *fs, struct ucred *cred)
236 {
237 struct vnode *vp;
238 struct inode *ip;
239 IFILE *ifp;
240 IFILE_V1 *ifp_v1;
241 struct buf *bp, *cbp;
242 int error;
243 daddr_t i, blkno, xmax;
244 ino_t oldlast, maxino;
245 CLEANERINFO *cip;
246
247 ASSERT_SEGLOCK(fs);
248
249 vp = fs->lfs_ivnode;
250 ip = VTOI(vp);
251 blkno = lblkno(fs, ip->i_size);
252 if ((error = VOP_BALLOC(vp, ip->i_size, fs->lfs_bsize, cred, 0,
253 &bp)) != 0) {
254 return (error);
255 }
256 ip->i_size += fs->lfs_bsize;
257 ip->i_ffs1_size = ip->i_size;
258 uvm_vnp_setsize(vp, ip->i_size);
259
260 maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
261 fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
262 fs->lfs_ino_bitmap = realloc(fs->lfs_ino_bitmap,
263 (maxino + BYTEMASK) >> BYTESHIFT,
264 M_SEGMENT, M_WAITOK);
265
266 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
267 fs->lfs_ifpb;
268
269 /*
270 * We insert the new inodes at the head of the free list.
271 * Under normal circumstances, the free list is empty here,
272 * so we are also incidentally placing them at the end (which
273 * we must do if we are to keep them in order).
274 */
275 LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
276 LFS_PUT_HEADFREE(fs, cip, cbp, i);
277 #ifdef DIAGNOSTIC
278 if (fs->lfs_freehd == LFS_UNUSED_INUM)
279 panic("inode 0 allocated [2]");
280 #endif /* DIAGNOSTIC */
281 xmax = i + fs->lfs_ifpb;
282
283 if (fs->lfs_version == 1) {
284 for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) {
285 ifp_v1->if_version = 1;
286 ifp_v1->if_daddr = LFS_UNUSED_DADDR;
287 ifp_v1->if_nextfree = ++i;
288 SET_BITMAP_FREE(fs, i);
289 }
290 ifp_v1--;
291 ifp_v1->if_nextfree = oldlast;
292 } else {
293 for (ifp = (IFILE *)bp->b_data; i < xmax; ++ifp) {
294 ifp->if_version = 1;
295 ifp->if_daddr = LFS_UNUSED_DADDR;
296 ifp->if_nextfree = ++i;
297 SET_BITMAP_FREE(fs, i);
298 }
299 ifp--;
300 ifp->if_nextfree = oldlast;
301 }
302 LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1);
303
304 (void) LFS_BWRITE_LOG(bp); /* Ifile */
305
306 return 0;
307 }
308
309 /* Allocate a new inode. */
310 /* ARGSUSED */
311 /* VOP_BWRITE 2i times */
312 int
313 lfs_valloc(void *v)
314 {
315 struct vop_valloc_args /* {
316 struct vnode *a_pvp;
317 int a_mode;
318 struct ucred *a_cred;
319 struct vnode **a_vpp;
320 } */ *ap = v;
321 struct lfs *fs;
322 struct buf *bp, *cbp;
323 struct ifile *ifp;
324 ino_t new_ino;
325 int error;
326 int new_gen;
327 CLEANERINFO *cip;
328
329 fs = VTOI(ap->a_pvp)->i_lfs;
330 if (fs->lfs_ronly)
331 return EROFS;
332
333 ASSERT_NO_SEGLOCK(fs);
334
335 lfs_seglock(fs, SEGM_PROT);
336 vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
337
338 /* Get the head of the freelist. */
339 LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
340 KASSERT(new_ino != LFS_UNUSED_INUM && new_ino != LFS_IFILE_INUM);
341
342 DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %lld\n",
343 (long long)new_ino));
344
345 /*
346 * Remove the inode from the free list and write the new start
347 * of the free list into the superblock.
348 */
349 CLR_BITMAP_FREE(fs, new_ino);
350 LFS_IENTRY(ifp, fs, new_ino, bp);
351 if (ifp->if_daddr != LFS_UNUSED_DADDR)
352 panic("lfs_valloc: inuse inode %llu on the free list",
353 (unsigned long long)new_ino);
354 LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
355 DLOG((DLOG_ALLOC, "lfs_valloc: headfree %lld -> %lld\n",
356 (long long)new_ino, (long long)ifp->if_nextfree));
357
358 new_gen = ifp->if_version; /* version was updated by vfree */
359 brelse(bp);
360
361 /* Extend IFILE so that the next lfs_valloc will succeed. */
362 if (fs->lfs_freehd == LFS_UNUSED_INUM) {
363 if ((error = extend_ifile(fs, ap->a_cred)) != 0) {
364 LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
365 VOP_UNLOCK(fs->lfs_ivnode, 0);
366 lfs_segunlock(fs);
367 return error;
368 }
369 }
370 #ifdef DIAGNOSTIC
371 if (fs->lfs_freehd == LFS_UNUSED_INUM)
372 panic("inode 0 allocated [3]");
373 #endif /* DIAGNOSTIC */
374
375 /* Set superblock modified bit and increment file count. */
376 simple_lock(&fs->lfs_interlock);
377 fs->lfs_fmod = 1;
378 simple_unlock(&fs->lfs_interlock);
379 ++fs->lfs_nfiles;
380
381 VOP_UNLOCK(fs->lfs_ivnode, 0);
382 lfs_segunlock(fs);
383
384 return lfs_ialloc(fs, ap->a_pvp, new_ino, new_gen, ap->a_vpp);
385 }
386
387 /*
388 * Finish allocating a new inode, given an inode and generation number.
389 */
390 static int
391 lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
392 struct vnode **vpp)
393 {
394 struct inode *ip;
395 struct vnode *vp;
396
397 ASSERT_NO_SEGLOCK(fs);
398
399 vp = *vpp;
400 lockmgr(&ufs_hashlock, LK_EXCLUSIVE, 0);
401 /* Create an inode to associate with the vnode. */
402 lfs_vcreate(pvp->v_mount, new_ino, vp);
403
404 ip = VTOI(vp);
405 LFS_SET_UINO(ip, IN_CHANGE);
406 /* on-disk structure has been zeroed out by lfs_vcreate */
407 ip->i_din.ffs1_din->di_inumber = new_ino;
408
409 /* Note no blocks yet */
410 ip->i_lfs_hiblk = -1;
411
412 /* Set a new generation number for this inode. */
413 if (new_gen) {
414 ip->i_gen = new_gen;
415 ip->i_ffs1_gen = new_gen;
416 }
417
418 /* Insert into the inode hash table. */
419 ufs_ihashins(ip);
420 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
421
422 ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, vpp);
423 vp = *vpp;
424 ip = VTOI(vp);
425
426 memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
427
428 uvm_vnp_setsize(vp, 0);
429 lfs_mark_vnode(vp);
430 genfs_node_init(vp, &lfs_genfsops);
431 VREF(ip->i_devvp);
432 return (0);
433 }
434
435 /* Create a new vnode/inode pair and initialize what fields we can. */
436 void
437 lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp)
438 {
439 struct inode *ip;
440 struct ufs1_dinode *dp;
441 struct ufsmount *ump;
442 #ifdef QUOTA
443 int i;
444 #endif
445
446 /* Get a pointer to the private mount structure. */
447 ump = VFSTOUFS(mp);
448
449 ASSERT_NO_SEGLOCK(ump->um_lfs);
450
451 /* Initialize the inode. */
452 ip = pool_get(&lfs_inode_pool, PR_WAITOK);
453 memset(ip, 0, sizeof(*ip));
454 dp = pool_get(&lfs_dinode_pool, PR_WAITOK);
455 memset(dp, 0, sizeof(*dp));
456 ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK);
457 memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs));
458 vp->v_data = ip;
459 ip->i_din.ffs1_din = dp;
460 ip->i_ump = ump;
461 ip->i_vnode = vp;
462 ip->i_devvp = ump->um_devvp;
463 ip->i_dev = ump->um_dev;
464 ip->i_number = dp->di_inumber = ino;
465 ip->i_lfs = ump->um_lfs;
466 ip->i_lfs_effnblks = 0;
467 SPLAY_INIT(&ip->i_lfs_lbtree);
468 ip->i_lfs_nbtree = 0;
469 #ifdef QUOTA
470 for (i = 0; i < MAXQUOTAS; i++)
471 ip->i_dquot[i] = NODQUOT;
472 #endif
473 #ifdef DEBUG
474 if (ino == LFS_IFILE_INUM)
475 vp->v_vnlock->lk_wmesg = "inlock";
476 #endif
477 }
478
479 #if 0
480 /*
481 * Find the highest-numbered allocated inode.
482 * This will be used to shrink the Ifile.
483 */
484 static inline ino_t
485 lfs_last_alloc_ino(struct lfs *fs)
486 {
487 ino_t ino, maxino;
488
489 maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
490 fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
491 for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) {
492 if (ISSET_BITMAP_FREE(fs, ino) == 0)
493 break;
494 }
495 return ino;
496 }
497 #endif
498
499 /*
500 * Find the previous (next lowest numbered) free inode, if any.
501 * If there is none, return LFS_UNUSED_INUM.
502 */
503 static inline ino_t
504 lfs_freelist_prev(struct lfs *fs, ino_t ino)
505 {
506 ino_t tino;
507 struct buf *bp;
508 IFILE *ifp;
509
510 for (tino = ino - 1; tino > LFS_UNUSED_INUM; tino--) {
511 if (fs->lfs_ino_bitmap) {
512 if (ISSET_BITMAP_FREE(fs, tino))
513 break;
514 } else {
515 LFS_IENTRY(ifp, fs, tino, bp);
516 if (ifp->if_daddr == LFS_UNUSED_DADDR) {
517 brelse(bp);
518 break;
519 }
520 brelse(bp);
521 }
522 }
523 if (tino <= LFS_IFILE_INUM)
524 tino = LFS_UNUSED_INUM;
525
526 return tino;
527 }
528
529 /* Free an inode. */
530 /* ARGUSED */
531 /* VOP_BWRITE 2i times */
532 int
533 lfs_vfree(void *v)
534 {
535 struct vop_vfree_args /* {
536 struct vnode *a_pvp;
537 ino_t a_ino;
538 int a_mode;
539 } */ *ap = v;
540 SEGUSE *sup;
541 CLEANERINFO *cip;
542 struct buf *cbp, *bp;
543 struct ifile *ifp;
544 struct inode *ip;
545 struct vnode *vp;
546 struct lfs *fs;
547 daddr_t old_iaddr;
548 ino_t ino, otail;
549 int s;
550
551 /* Get the inode number and file system. */
552 vp = ap->a_pvp;
553 ip = VTOI(vp);
554 fs = ip->i_lfs;
555 ino = ip->i_number;
556
557 ASSERT_NO_SEGLOCK(fs);
558
559 /* Drain of pending writes */
560 simple_lock(&vp->v_interlock);
561 s = splbio();
562 if (fs->lfs_version > 1 && WRITEINPROG(vp))
563 ltsleep(vp, (PRIBIO+1), "lfs_vfree", 0, &vp->v_interlock);
564 splx(s);
565 simple_unlock(&vp->v_interlock);
566
567 lfs_seglock(fs, SEGM_PROT);
568 vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
569
570 lfs_unmark_vnode(vp);
571 if (vp->v_flag & VDIROP) {
572 vp->v_flag &= ~VDIROP;
573 simple_lock(&fs->lfs_interlock);
574 simple_lock(&lfs_subsys_lock);
575 --lfs_dirvcount;
576 simple_unlock(&lfs_subsys_lock);
577 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
578 simple_unlock(&fs->lfs_interlock);
579 wakeup(&lfs_dirvcount);
580 lfs_vunref(vp);
581 }
582
583 LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
584 ip->i_flag &= ~IN_ALLMOD;
585
586 /*
587 * Set the ifile's inode entry to unused, increment its version number
588 * and link it onto the free chain.
589 */
590 SET_BITMAP_FREE(fs, ino);
591 LFS_IENTRY(ifp, fs, ino, bp);
592 old_iaddr = ifp->if_daddr;
593 ifp->if_daddr = LFS_UNUSED_DADDR;
594 ++ifp->if_version;
595 if (fs->lfs_version == 1) {
596 LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
597 LFS_PUT_HEADFREE(fs, cip, cbp, ino);
598 (void) LFS_BWRITE_LOG(bp); /* Ifile */
599 } else {
600 ino_t tino, onf;
601
602 ifp->if_nextfree = LFS_UNUSED_INUM;
603 (void) LFS_BWRITE_LOG(bp); /* Ifile */
604
605 tino = lfs_freelist_prev(fs, ino);
606 if (tino == LFS_UNUSED_INUM) {
607 /* Nothing free below us, put us on the head */
608 LFS_IENTRY(ifp, fs, ino, bp);
609 LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
610 LFS_PUT_HEADFREE(fs, cip, cbp, ino);
611 DLOG((DLOG_ALLOC, "lfs_valloc: headfree %lld -> %lld\n",
612 (long long)ifp->if_nextfree, (long long)ino));
613 LFS_BWRITE_LOG(bp); /* Ifile */
614
615 /* If the list was empty, set tail too */
616 LFS_GET_TAILFREE(fs, cip, cbp, &otail);
617 if (otail == LFS_UNUSED_INUM) {
618 LFS_PUT_TAILFREE(fs, cip, cbp, ino);
619 DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
620 "-> %lld\n", (long long)otail,
621 (long long)ino));
622 }
623 } else {
624 /*
625 * Insert this inode into the list after tino.
626 * We hold the segment lock so we don't have to
627 * worry about blocks being written out of order.
628 */
629 DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld "
630 " after %lld\n", ino, tino));
631
632 LFS_IENTRY(ifp, fs, tino, bp);
633 onf = ifp->if_nextfree;
634 ifp->if_nextfree = ino;
635 LFS_BWRITE_LOG(bp); /* Ifile */
636
637 LFS_IENTRY(ifp, fs, ino, bp);
638 ifp->if_nextfree = onf;
639 LFS_BWRITE_LOG(bp); /* Ifile */
640
641 /* If we're last, put us on the tail */
642 if (onf == LFS_UNUSED_INUM) {
643 LFS_GET_TAILFREE(fs, cip, cbp, &otail);
644 LFS_PUT_TAILFREE(fs, cip, cbp, ino);
645 DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
646 "-> %lld\n", (long long)otail,
647 (long long)ino));
648 }
649 }
650 }
651 #ifdef DIAGNOSTIC
652 if (ino == LFS_UNUSED_INUM) {
653 panic("inode 0 freed");
654 }
655 #endif /* DIAGNOSTIC */
656 if (old_iaddr != LFS_UNUSED_DADDR) {
657 LFS_SEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp);
658 #ifdef DIAGNOSTIC
659 if (sup->su_nbytes < sizeof (struct ufs1_dinode)) {
660 printf("lfs_vfree: negative byte count"
661 " (segment %" PRIu32 " short by %d)\n",
662 dtosn(fs, old_iaddr),
663 (int)sizeof (struct ufs1_dinode) -
664 sup->su_nbytes);
665 panic("lfs_vfree: negative byte count");
666 sup->su_nbytes = sizeof (struct ufs1_dinode);
667 }
668 #endif
669 sup->su_nbytes -= sizeof (struct ufs1_dinode);
670 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp); /* Ifile */
671 }
672
673 /* Set superblock modified bit and decrement file count. */
674 simple_lock(&fs->lfs_interlock);
675 fs->lfs_fmod = 1;
676 simple_unlock(&fs->lfs_interlock);
677 --fs->lfs_nfiles;
678
679 VOP_UNLOCK(fs->lfs_ivnode, 0);
680 lfs_segunlock(fs);
681
682 return (0);
683 }
684
685 /*
686 * Sort the freelist and set up the free-inode bitmap.
687 * To be called by lfs_mountfs().
688 */
689 void
690 lfs_order_freelist(struct lfs *fs)
691 {
692 CLEANERINFO *cip;
693 IFILE *ifp = NULL;
694 struct buf *bp;
695 ino_t ino, firstino, lastino, maxino;
696
697 maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
698 fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
699 fs->lfs_ino_bitmap = malloc((maxino + BYTEMASK) >> BYTESHIFT,
700 M_SEGMENT, M_WAITOK | M_ZERO);
701
702 firstino = lastino = LFS_UNUSED_INUM;
703 for (ino = 0; ino < maxino; ino++) {
704 if (ino % fs->lfs_ifpb == 0)
705 LFS_IENTRY(ifp, fs, ino, bp);
706 else
707 ++ifp;
708
709 /* Don't put zero or ifile on the free list */
710 if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM)
711 continue;
712
713 if (ifp->if_daddr == LFS_UNUSED_DADDR) {
714 if (firstino == LFS_UNUSED_INUM)
715 firstino = ino;
716 else {
717 brelse(bp);
718
719 LFS_IENTRY(ifp, fs, lastino, bp);
720 ifp->if_nextfree = ino;
721 LFS_BWRITE_LOG(bp);
722
723 LFS_IENTRY(ifp, fs, ino, bp);
724 }
725 lastino = ino;
726
727 SET_BITMAP_FREE(fs, ino);
728 }
729
730 if ((ino + 1) % fs->lfs_ifpb == 0)
731 brelse(bp);
732 }
733
734 LFS_PUT_HEADFREE(fs, cip, bp, firstino);
735 LFS_PUT_TAILFREE(fs, cip, bp, lastino);
736 }
737