lfs_alloc.c revision 1.100.6.3 1 /* $NetBSD: lfs_alloc.c,v 1.100.6.3 2007/06/17 21:32:11 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003, 2007 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.100.6.3 2007/06/17 21:32:11 ad 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 kmutex_t 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,
202 struct vnode **vpp)
203 {
204 struct lfs *fs;
205 struct buf *bp, *cbp;
206 struct ifile *ifp;
207 ino_t new_ino;
208 int error;
209 int new_gen;
210 CLEANERINFO *cip;
211
212 fs = VTOI(pvp)->i_lfs;
213 if (fs->lfs_ronly)
214 return EROFS;
215
216 ASSERT_NO_SEGLOCK(fs);
217
218 lfs_seglock(fs, SEGM_PROT);
219 vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
220
221 /* Get the head of the freelist. */
222 LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
223 KASSERT(new_ino != LFS_UNUSED_INUM && new_ino != LFS_IFILE_INUM);
224
225 DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %lld\n",
226 (long long)new_ino));
227
228 /*
229 * Remove the inode from the free list and write the new start
230 * of the free list into the superblock.
231 */
232 CLR_BITMAP_FREE(fs, new_ino);
233 LFS_IENTRY(ifp, fs, new_ino, bp);
234 if (ifp->if_daddr != LFS_UNUSED_DADDR)
235 panic("lfs_valloc: inuse inode %llu on the free list",
236 (unsigned long long)new_ino);
237 LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
238 DLOG((DLOG_ALLOC, "lfs_valloc: headfree %lld -> %lld\n",
239 (long long)new_ino, (long long)ifp->if_nextfree));
240
241 new_gen = ifp->if_version; /* version was updated by vfree */
242 brelse(bp, 0);
243
244 /* Extend IFILE so that the next lfs_valloc will succeed. */
245 if (fs->lfs_freehd == LFS_UNUSED_INUM) {
246 if ((error = lfs_extend_ifile(fs, cred)) != 0) {
247 LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
248 VOP_UNLOCK(fs->lfs_ivnode, 0);
249 lfs_segunlock(fs);
250 return error;
251 }
252 }
253 #ifdef DIAGNOSTIC
254 if (fs->lfs_freehd == LFS_UNUSED_INUM)
255 panic("inode 0 allocated [3]");
256 #endif /* DIAGNOSTIC */
257
258 /* Set superblock modified bit and increment file count. */
259 mutex_enter(&fs->lfs_interlock);
260 fs->lfs_fmod = 1;
261 mutex_exit(&fs->lfs_interlock);
262 ++fs->lfs_nfiles;
263
264 VOP_UNLOCK(fs->lfs_ivnode, 0);
265 lfs_segunlock(fs);
266
267 return lfs_ialloc(fs, pvp, new_ino, new_gen, vpp);
268 }
269
270 /*
271 * Finish allocating a new inode, given an inode and generation number.
272 */
273 int
274 lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
275 struct vnode **vpp)
276 {
277 struct inode *ip;
278 struct vnode *vp;
279
280 ASSERT_NO_SEGLOCK(fs);
281
282 vp = *vpp;
283 mutex_enter(&ufs_hashlock);
284 /* Create an inode to associate with the vnode. */
285 lfs_vcreate(pvp->v_mount, new_ino, vp);
286
287 ip = VTOI(vp);
288 mutex_enter(&ip->i_lfs->lfs_interlock);
289 LFS_SET_UINO(ip, IN_CHANGE);
290 mutex_exit(&ip->i_lfs->lfs_interlock);
291 /* on-disk structure has been zeroed out by lfs_vcreate */
292 ip->i_din.ffs1_din->di_inumber = new_ino;
293
294 /* Note no blocks yet */
295 ip->i_lfs_hiblk = -1;
296
297 /* Set a new generation number for this inode. */
298 if (new_gen) {
299 ip->i_gen = new_gen;
300 ip->i_ffs1_gen = new_gen;
301 }
302
303 /* Insert into the inode hash table. */
304 ufs_ihashins(ip);
305 mutex_exit(&ufs_hashlock);
306
307 ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, vpp);
308 vp = *vpp;
309 ip = VTOI(vp);
310
311 memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
312
313 uvm_vnp_setsize(vp, 0);
314 lfs_mark_vnode(vp);
315 genfs_node_init(vp, &lfs_genfsops);
316 VREF(ip->i_devvp);
317 return (0);
318 }
319
320 /* Create a new vnode/inode pair and initialize what fields we can. */
321 void
322 lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp)
323 {
324 struct inode *ip;
325 struct ufs1_dinode *dp;
326 struct ufsmount *ump;
327 #ifdef QUOTA
328 int i;
329 #endif
330
331 /* Get a pointer to the private mount structure. */
332 ump = VFSTOUFS(mp);
333
334 ASSERT_NO_SEGLOCK(ump->um_lfs);
335
336 /* Initialize the inode. */
337 ip = pool_get(&lfs_inode_pool, PR_WAITOK);
338 memset(ip, 0, sizeof(*ip));
339 dp = pool_get(&lfs_dinode_pool, PR_WAITOK);
340 memset(dp, 0, sizeof(*dp));
341 ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK);
342 memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs));
343 vp->v_data = ip;
344 ip->i_din.ffs1_din = dp;
345 ip->i_ump = ump;
346 ip->i_vnode = vp;
347 ip->i_devvp = ump->um_devvp;
348 ip->i_dev = ump->um_dev;
349 ip->i_number = dp->di_inumber = ino;
350 ip->i_lfs = ump->um_lfs;
351 ip->i_lfs_effnblks = 0;
352 SPLAY_INIT(&ip->i_lfs_lbtree);
353 ip->i_lfs_nbtree = 0;
354 LIST_INIT(&ip->i_lfs_segdhd);
355 #ifdef QUOTA
356 for (i = 0; i < MAXQUOTAS; i++)
357 ip->i_dquot[i] = NODQUOT;
358 #endif
359 #ifdef DEBUG
360 if (ino == LFS_IFILE_INUM)
361 vp->v_vnlock->lk_wmesg = "inlock";
362 #endif
363 }
364
365 #if 0
366 /*
367 * Find the highest-numbered allocated inode.
368 * This will be used to shrink the Ifile.
369 */
370 static inline ino_t
371 lfs_last_alloc_ino(struct lfs *fs)
372 {
373 ino_t ino, maxino;
374
375 maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
376 fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
377 for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) {
378 if (ISSET_BITMAP_FREE(fs, ino) == 0)
379 break;
380 }
381 return ino;
382 }
383 #endif
384
385 /*
386 * Find the previous (next lowest numbered) free inode, if any.
387 * If there is none, return LFS_UNUSED_INUM.
388 */
389 static inline ino_t
390 lfs_freelist_prev(struct lfs *fs, ino_t ino)
391 {
392 ino_t tino, bound, bb, freehdbb;
393
394 if (fs->lfs_freehd == LFS_UNUSED_INUM) /* No free inodes at all */
395 return LFS_UNUSED_INUM;
396
397 /* Search our own word first */
398 bound = ino & ~BMMASK;
399 for (tino = ino - 1; tino >= bound && tino > LFS_UNUSED_INUM; tino--)
400 if (ISSET_BITMAP_FREE(fs, tino))
401 return tino;
402 /* If there are no lower words to search, just return */
403 if (ino >> BMSHIFT == 0)
404 return LFS_UNUSED_INUM;
405
406 /*
407 * Find a word with a free inode in it. We have to be a bit
408 * careful here since ino_t is unsigned.
409 */
410 freehdbb = (fs->lfs_freehd >> BMSHIFT);
411 for (bb = (ino >> BMSHIFT) - 1; bb >= freehdbb && bb > 0; --bb)
412 if (fs->lfs_ino_bitmap[bb])
413 break;
414 if (fs->lfs_ino_bitmap[bb] == 0)
415 return LFS_UNUSED_INUM;
416
417 /* Search the word we found */
418 for (tino = (bb << BMSHIFT) | BMMASK; tino >= (bb << BMSHIFT) &&
419 tino > LFS_UNUSED_INUM; tino--)
420 if (ISSET_BITMAP_FREE(fs, tino))
421 break;
422
423 if (tino <= LFS_IFILE_INUM)
424 tino = LFS_UNUSED_INUM;
425
426 return tino;
427 }
428
429 /* Free an inode. */
430 /* ARGUSED */
431 /* VOP_BWRITE 2i times */
432 int
433 lfs_vfree(struct vnode *vp, ino_t ino, int mode)
434 {
435 SEGUSE *sup;
436 CLEANERINFO *cip;
437 struct buf *cbp, *bp;
438 struct ifile *ifp;
439 struct inode *ip;
440 struct lfs *fs;
441 daddr_t old_iaddr;
442 ino_t otail;
443 int s;
444
445 /* Get the inode number and file system. */
446 ip = VTOI(vp);
447 fs = ip->i_lfs;
448 ino = ip->i_number;
449
450 ASSERT_NO_SEGLOCK(fs);
451 DLOG((DLOG_ALLOC, "lfs_vfree: free ino %lld\n", (long long)ino));
452
453 /* Drain of pending writes */
454 mutex_enter(&vp->v_interlock);
455 s = splbio();
456 if (fs->lfs_version > 1 && WRITEINPROG(vp))
457 mtsleep(vp, (PRIBIO+1), "lfs_vfree", 0, &vp->v_interlock);
458 splx(s);
459 mutex_exit(&vp->v_interlock);
460
461 lfs_seglock(fs, SEGM_PROT);
462 vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
463
464 lfs_unmark_vnode(vp);
465 mutex_enter(&fs->lfs_interlock);
466 if (vp->v_uflag & VU_DIROP) {
467 vp->v_uflag &= ~VU_DIROP;
468 mutex_enter(&lfs_subsys_lock);
469 --lfs_dirvcount;
470 mutex_exit(&lfs_subsys_lock);
471 --fs->lfs_dirvcount;
472 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
473 mutex_exit(&fs->lfs_interlock);
474 wakeup(&fs->lfs_dirvcount);
475 wakeup(&lfs_dirvcount);
476 lfs_vunref(vp);
477
478 /*
479 * If this inode is not going to be written any more, any
480 * segment accounting left over from its truncation needs
481 * to occur at the end of the next dirops flush. Attach
482 * them to the fs-wide list for that purpose.
483 */
484 if (LIST_FIRST(&ip->i_lfs_segdhd) != NULL) {
485 struct segdelta *sd;
486
487 while((sd = LIST_FIRST(&ip->i_lfs_segdhd)) != NULL) {
488 LIST_REMOVE(sd, list);
489 LIST_INSERT_HEAD(&fs->lfs_segdhd, sd, list);
490 }
491 }
492 } else {
493 /*
494 * If it's not a dirop, we can finalize right away.
495 */
496 mutex_exit(&fs->lfs_interlock);
497 lfs_finalize_ino_seguse(fs, ip);
498 }
499
500 mutex_enter(&fs->lfs_interlock);
501 LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
502 mutex_exit(&fs->lfs_interlock);
503 ip->i_flag &= ~IN_ALLMOD;
504 ip->i_lfs_iflags |= LFSI_DELETED;
505
506 /*
507 * Set the ifile's inode entry to unused, increment its version number
508 * and link it onto the free chain.
509 */
510 SET_BITMAP_FREE(fs, ino);
511 LFS_IENTRY(ifp, fs, ino, bp);
512 old_iaddr = ifp->if_daddr;
513 ifp->if_daddr = LFS_UNUSED_DADDR;
514 ++ifp->if_version;
515 if (fs->lfs_version == 1) {
516 LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
517 LFS_PUT_HEADFREE(fs, cip, cbp, ino);
518 (void) LFS_BWRITE_LOG(bp); /* Ifile */
519 } else {
520 ino_t tino, onf;
521
522 ifp->if_nextfree = LFS_UNUSED_INUM;
523 (void) LFS_BWRITE_LOG(bp); /* Ifile */
524
525 tino = lfs_freelist_prev(fs, ino);
526 if (tino == LFS_UNUSED_INUM) {
527 /* Nothing free below us, put us on the head */
528 LFS_IENTRY(ifp, fs, ino, bp);
529 LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
530 LFS_PUT_HEADFREE(fs, cip, cbp, ino);
531 DLOG((DLOG_ALLOC, "lfs_vfree: headfree %lld -> %lld\n",
532 (long long)ifp->if_nextfree, (long long)ino));
533 LFS_BWRITE_LOG(bp); /* Ifile */
534
535 /* If the list was empty, set tail too */
536 LFS_GET_TAILFREE(fs, cip, cbp, &otail);
537 if (otail == LFS_UNUSED_INUM) {
538 LFS_PUT_TAILFREE(fs, cip, cbp, ino);
539 DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
540 "-> %lld\n", (long long)otail,
541 (long long)ino));
542 }
543 } else {
544 /*
545 * Insert this inode into the list after tino.
546 * We hold the segment lock so we don't have to
547 * worry about blocks being written out of order.
548 */
549 DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld "
550 " after %lld\n", ino, tino));
551
552 LFS_IENTRY(ifp, fs, tino, bp);
553 onf = ifp->if_nextfree;
554 ifp->if_nextfree = ino;
555 LFS_BWRITE_LOG(bp); /* Ifile */
556
557 LFS_IENTRY(ifp, fs, ino, bp);
558 ifp->if_nextfree = onf;
559 LFS_BWRITE_LOG(bp); /* Ifile */
560
561 /* If we're last, put us on the tail */
562 if (onf == LFS_UNUSED_INUM) {
563 LFS_GET_TAILFREE(fs, cip, cbp, &otail);
564 LFS_PUT_TAILFREE(fs, cip, cbp, ino);
565 DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
566 "-> %lld\n", (long long)otail,
567 (long long)ino));
568 }
569 }
570 }
571 #ifdef DIAGNOSTIC
572 if (ino == LFS_UNUSED_INUM) {
573 panic("inode 0 freed");
574 }
575 #endif /* DIAGNOSTIC */
576 if (old_iaddr != LFS_UNUSED_DADDR) {
577 LFS_SEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp);
578 #ifdef DIAGNOSTIC
579 if (sup->su_nbytes < sizeof (struct ufs1_dinode)) {
580 printf("lfs_vfree: negative byte count"
581 " (segment %" PRIu32 " short by %d)\n",
582 dtosn(fs, old_iaddr),
583 (int)sizeof (struct ufs1_dinode) -
584 sup->su_nbytes);
585 panic("lfs_vfree: negative byte count");
586 sup->su_nbytes = sizeof (struct ufs1_dinode);
587 }
588 #endif
589 sup->su_nbytes -= sizeof (struct ufs1_dinode);
590 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp); /* Ifile */
591 }
592
593 /* Set superblock modified bit and decrement file count. */
594 mutex_enter(&fs->lfs_interlock);
595 fs->lfs_fmod = 1;
596 mutex_exit(&fs->lfs_interlock);
597 --fs->lfs_nfiles;
598
599 VOP_UNLOCK(fs->lfs_ivnode, 0);
600 lfs_segunlock(fs);
601
602 return (0);
603 }
604
605 /*
606 * Sort the freelist and set up the free-inode bitmap.
607 * To be called by lfs_mountfs().
608 */
609 void
610 lfs_order_freelist(struct lfs *fs)
611 {
612 CLEANERINFO *cip;
613 IFILE *ifp = NULL;
614 struct buf *bp;
615 ino_t ino, firstino, lastino, maxino;
616 #ifdef notyet
617 struct vnode *vp;
618 #endif
619
620 ASSERT_NO_SEGLOCK(fs);
621 lfs_seglock(fs, SEGM_PROT);
622
623 maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
624 fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
625 fs->lfs_ino_bitmap = (lfs_bm_t *)
626 malloc(((maxino + BMMASK) >> BMSHIFT) * sizeof(lfs_bm_t),
627 M_SEGMENT, M_WAITOK | M_ZERO);
628 KASSERT(fs->lfs_ino_bitmap != NULL);
629
630 firstino = lastino = LFS_UNUSED_INUM;
631 for (ino = 0; ino < maxino; ino++) {
632 if (ino % fs->lfs_ifpb == 0)
633 LFS_IENTRY(ifp, fs, ino, bp);
634 else
635 ++ifp;
636
637 /* Don't put zero or ifile on the free list */
638 if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM)
639 continue;
640
641 #ifdef notyet
642 /* Address orphaned files */
643 if (ifp->if_nextfree == LFS_ORPHAN_NEXTFREE &&
644 VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp) == 0) {
645 lfs_truncate(vp, 0, 0, NOCRED, curlwp);
646 vput(vp);
647 LFS_SEGENTRY(sup, fs, dtosn(fs, ifp->if_daddr), bp);
648 KASSERT(sup->su_nbytes >= DINODE1_SIZE);
649 sup->su_nbytes -= DINODE1_SIZE;
650 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, ifp->if_daddr), bp);
651
652 /* Set up to fall through to next section */
653 ifp->if_daddr = LFS_UNUSED_DADDR;
654 LFS_BWRITE_LOG(bp);
655 LFS_IENTRY(ifp, fs, ino, bp);
656 }
657 #endif
658
659 if (ifp->if_daddr == LFS_UNUSED_DADDR) {
660 if (firstino == LFS_UNUSED_INUM)
661 firstino = ino;
662 else {
663 brelse(bp, 0);
664
665 LFS_IENTRY(ifp, fs, lastino, bp);
666 ifp->if_nextfree = ino;
667 LFS_BWRITE_LOG(bp);
668
669 LFS_IENTRY(ifp, fs, ino, bp);
670 }
671 lastino = ino;
672
673 SET_BITMAP_FREE(fs, ino);
674 }
675
676 if ((ino + 1) % fs->lfs_ifpb == 0)
677 brelse(bp, 0);
678 }
679
680 LFS_PUT_HEADFREE(fs, cip, bp, firstino);
681 LFS_PUT_TAILFREE(fs, cip, bp, lastino);
682
683 lfs_segunlock(fs);
684 }
685
686 void
687 lfs_orphan(struct lfs *fs, ino_t ino)
688 {
689 IFILE *ifp;
690 struct buf *bp;
691
692 LFS_IENTRY(ifp, fs, ino, bp);
693 ifp->if_nextfree = LFS_ORPHAN_NEXTFREE;
694 LFS_BWRITE_LOG(bp);
695 }
696