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