lfs_alloc.c revision 1.106.10.1 1 /* $NetBSD: lfs_alloc.c,v 1.106.10.1 2008/05/16 02:26:00 yamt 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 /*
32 * Copyright (c) 1991, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * @(#)lfs_alloc.c 8.4 (Berkeley) 1/4/94
60 */
61
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.106.10.1 2008/05/16 02:26:00 yamt Exp $");
64
65 #if defined(_KERNEL_OPT)
66 #include "opt_quota.h"
67 #endif
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/buf.h>
73 #include <sys/lock.h>
74 #include <sys/vnode.h>
75 #include <sys/syslog.h>
76 #include <sys/mount.h>
77 #include <sys/malloc.h>
78 #include <sys/pool.h>
79 #include <sys/proc.h>
80 #include <sys/tree.h>
81 #include <sys/kauth.h>
82
83 #include <ufs/ufs/quota.h>
84 #include <ufs/ufs/inode.h>
85 #include <ufs/ufs/ufsmount.h>
86 #include <ufs/ufs/ufs_extern.h>
87
88 #include <ufs/lfs/lfs.h>
89 #include <ufs/lfs/lfs_extern.h>
90
91 extern kmutex_t ufs_hashlock;
92
93 /* Constants for inode free bitmap */
94 #define BMSHIFT 5 /* 2 ** 5 = 32 */
95 #define BMMASK ((1 << BMSHIFT) - 1)
96 #define SET_BITMAP_FREE(F, I) do { \
97 DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d set\n", (int)(I), \
98 (int)((I) >> BMSHIFT), (int)((I) & BMMASK))); \
99 (F)->lfs_ino_bitmap[(I) >> BMSHIFT] |= (1 << ((I) & BMMASK)); \
100 } while (0)
101 #define CLR_BITMAP_FREE(F, I) do { \
102 DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d clr\n", (int)(I), \
103 (int)((I) >> BMSHIFT), (int)((I) & BMMASK))); \
104 (F)->lfs_ino_bitmap[(I) >> BMSHIFT] &= ~(1 << ((I) & BMMASK)); \
105 } while(0)
106
107 #define ISSET_BITMAP_FREE(F, I) \
108 ((F)->lfs_ino_bitmap[(I) >> BMSHIFT] & (1 << ((I) & BMMASK)))
109
110 /*
111 * Add a new block to the Ifile, to accommodate future file creations.
112 * Called with the segment lock held.
113 */
114 int
115 lfs_extend_ifile(struct lfs *fs, kauth_cred_t cred)
116 {
117 struct vnode *vp;
118 struct inode *ip;
119 IFILE *ifp;
120 IFILE_V1 *ifp_v1;
121 struct buf *bp, *cbp;
122 int error;
123 daddr_t i, blkno, xmax;
124 ino_t oldlast, maxino;
125 CLEANERINFO *cip;
126
127 ASSERT_SEGLOCK(fs);
128
129 vp = fs->lfs_ivnode;
130 ip = VTOI(vp);
131 blkno = lblkno(fs, ip->i_size);
132 if ((error = lfs_balloc(vp, ip->i_size, fs->lfs_bsize, cred, 0,
133 &bp)) != 0) {
134 return (error);
135 }
136 ip->i_size += fs->lfs_bsize;
137 ip->i_ffs1_size = ip->i_size;
138 uvm_vnp_setsize(vp, ip->i_size);
139
140 maxino = ((ip->i_size >> fs->lfs_bshift) - fs->lfs_cleansz -
141 fs->lfs_segtabsz) * fs->lfs_ifpb;
142 fs->lfs_ino_bitmap = (lfs_bm_t *)
143 realloc(fs->lfs_ino_bitmap, ((maxino + BMMASK) >> BMSHIFT) *
144 sizeof(lfs_bm_t), M_SEGMENT, M_WAITOK);
145 KASSERT(fs->lfs_ino_bitmap != NULL);
146
147 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
148 fs->lfs_ifpb;
149
150 /*
151 * We insert the new inodes at the head of the free list.
152 * Under normal circumstances, the free list is empty here,
153 * so we are also incidentally placing them at the end (which
154 * we must do if we are to keep them in order).
155 */
156 LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
157 LFS_PUT_HEADFREE(fs, cip, cbp, i);
158 #ifdef DIAGNOSTIC
159 if (fs->lfs_freehd == LFS_UNUSED_INUM)
160 panic("inode 0 allocated [2]");
161 #endif /* DIAGNOSTIC */
162 xmax = i + fs->lfs_ifpb;
163
164 if (fs->lfs_version == 1) {
165 for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) {
166 SET_BITMAP_FREE(fs, i);
167 ifp_v1->if_version = 1;
168 ifp_v1->if_daddr = LFS_UNUSED_DADDR;
169 ifp_v1->if_nextfree = ++i;
170 }
171 ifp_v1--;
172 ifp_v1->if_nextfree = oldlast;
173 } else {
174 for (ifp = (IFILE *)bp->b_data; i < xmax; ++ifp) {
175 SET_BITMAP_FREE(fs, i);
176 ifp->if_version = 1;
177 ifp->if_daddr = LFS_UNUSED_DADDR;
178 ifp->if_nextfree = ++i;
179 }
180 ifp--;
181 ifp->if_nextfree = oldlast;
182 }
183 LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1);
184
185 (void) LFS_BWRITE_LOG(bp); /* Ifile */
186
187 return 0;
188 }
189
190 /* Allocate a new inode. */
191 /* ARGSUSED */
192 /* VOP_BWRITE 2i times */
193 int
194 lfs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred,
195 struct vnode **vpp)
196 {
197 struct lfs *fs;
198 struct buf *bp, *cbp;
199 struct ifile *ifp;
200 ino_t new_ino;
201 int error;
202 int new_gen;
203 CLEANERINFO *cip;
204
205 fs = VTOI(pvp)->i_lfs;
206 if (fs->lfs_ronly)
207 return EROFS;
208
209 ASSERT_NO_SEGLOCK(fs);
210
211 lfs_seglock(fs, SEGM_PROT);
212 vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
213
214 /* Get the head of the freelist. */
215 LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
216 KASSERT(new_ino != LFS_UNUSED_INUM && new_ino != LFS_IFILE_INUM);
217
218 DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %lld\n",
219 (long long)new_ino));
220
221 /*
222 * Remove the inode from the free list and write the new start
223 * of the free list into the superblock.
224 */
225 CLR_BITMAP_FREE(fs, new_ino);
226 LFS_IENTRY(ifp, fs, new_ino, bp);
227 if (ifp->if_daddr != LFS_UNUSED_DADDR)
228 panic("lfs_valloc: inuse inode %llu on the free list",
229 (unsigned long long)new_ino);
230 LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
231 DLOG((DLOG_ALLOC, "lfs_valloc: headfree %lld -> %lld\n",
232 (long long)new_ino, (long long)ifp->if_nextfree));
233
234 new_gen = ifp->if_version; /* version was updated by vfree */
235 brelse(bp, 0);
236
237 /* Extend IFILE so that the next lfs_valloc will succeed. */
238 if (fs->lfs_freehd == LFS_UNUSED_INUM) {
239 if ((error = lfs_extend_ifile(fs, cred)) != 0) {
240 LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
241 VOP_UNLOCK(fs->lfs_ivnode, 0);
242 lfs_segunlock(fs);
243 return error;
244 }
245 }
246 #ifdef DIAGNOSTIC
247 if (fs->lfs_freehd == LFS_UNUSED_INUM)
248 panic("inode 0 allocated [3]");
249 #endif /* DIAGNOSTIC */
250
251 /* Set superblock modified bit and increment file count. */
252 mutex_enter(&lfs_lock);
253 fs->lfs_fmod = 1;
254 mutex_exit(&lfs_lock);
255 ++fs->lfs_nfiles;
256
257 VOP_UNLOCK(fs->lfs_ivnode, 0);
258 lfs_segunlock(fs);
259
260 return lfs_ialloc(fs, pvp, new_ino, new_gen, vpp);
261 }
262
263 /*
264 * Finish allocating a new inode, given an inode and generation number.
265 */
266 int
267 lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
268 struct vnode **vpp)
269 {
270 struct inode *ip;
271 struct vnode *vp;
272
273 ASSERT_NO_SEGLOCK(fs);
274
275 vp = *vpp;
276 mutex_enter(&ufs_hashlock);
277 /* Create an inode to associate with the vnode. */
278 lfs_vcreate(pvp->v_mount, new_ino, vp);
279
280 ip = VTOI(vp);
281 mutex_enter(&lfs_lock);
282 LFS_SET_UINO(ip, IN_CHANGE);
283 mutex_exit(&lfs_lock);
284 /* on-disk structure has been zeroed out by lfs_vcreate */
285 ip->i_din.ffs1_din->di_inumber = new_ino;
286
287 /* Note no blocks yet */
288 ip->i_lfs_hiblk = -1;
289
290 /* Set a new generation number for this inode. */
291 if (new_gen) {
292 ip->i_gen = new_gen;
293 ip->i_ffs1_gen = new_gen;
294 }
295
296 /* Insert into the inode hash table. */
297 ufs_ihashins(ip);
298 mutex_exit(&ufs_hashlock);
299
300 ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, vpp);
301 vp = *vpp;
302 ip = VTOI(vp);
303
304 memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
305
306 uvm_vnp_setsize(vp, 0);
307 lfs_mark_vnode(vp);
308 genfs_node_init(vp, &lfs_genfsops);
309 VREF(ip->i_devvp);
310 return (0);
311 }
312
313 /* Create a new vnode/inode pair and initialize what fields we can. */
314 void
315 lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp)
316 {
317 struct inode *ip;
318 struct ufs1_dinode *dp;
319 struct ufsmount *ump;
320
321 /* Get a pointer to the private mount structure. */
322 ump = VFSTOUFS(mp);
323
324 ASSERT_NO_SEGLOCK(ump->um_lfs);
325
326 /* Initialize the inode. */
327 ip = pool_get(&lfs_inode_pool, PR_WAITOK);
328 memset(ip, 0, sizeof(*ip));
329 dp = pool_get(&lfs_dinode_pool, PR_WAITOK);
330 memset(dp, 0, sizeof(*dp));
331 ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK);
332 memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs));
333 vp->v_data = ip;
334 ip->i_din.ffs1_din = dp;
335 ip->i_ump = ump;
336 ip->i_vnode = vp;
337 ip->i_devvp = ump->um_devvp;
338 ip->i_dev = ump->um_dev;
339 ip->i_number = dp->di_inumber = ino;
340 ip->i_lfs = ump->um_lfs;
341 ip->i_lfs_effnblks = 0;
342 SPLAY_INIT(&ip->i_lfs_lbtree);
343 ip->i_lfs_nbtree = 0;
344 LIST_INIT(&ip->i_lfs_segdhd);
345 #ifdef QUOTA
346 ufsquota_init(ip);
347 #endif
348 }
349
350 #if 0
351 /*
352 * Find the highest-numbered allocated inode.
353 * This will be used to shrink the Ifile.
354 */
355 static inline ino_t
356 lfs_last_alloc_ino(struct lfs *fs)
357 {
358 ino_t ino, maxino;
359
360 maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
361 fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
362 for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) {
363 if (ISSET_BITMAP_FREE(fs, ino) == 0)
364 break;
365 }
366 return ino;
367 }
368 #endif
369
370 /*
371 * Find the previous (next lowest numbered) free inode, if any.
372 * If there is none, return LFS_UNUSED_INUM.
373 */
374 static inline ino_t
375 lfs_freelist_prev(struct lfs *fs, ino_t ino)
376 {
377 ino_t tino, bound, bb, freehdbb;
378
379 if (fs->lfs_freehd == LFS_UNUSED_INUM) /* No free inodes at all */
380 return LFS_UNUSED_INUM;
381
382 /* Search our own word first */
383 bound = ino & ~BMMASK;
384 for (tino = ino - 1; tino >= bound && tino > LFS_UNUSED_INUM; tino--)
385 if (ISSET_BITMAP_FREE(fs, tino))
386 return tino;
387 /* If there are no lower words to search, just return */
388 if (ino >> BMSHIFT == 0)
389 return LFS_UNUSED_INUM;
390
391 /*
392 * Find a word with a free inode in it. We have to be a bit
393 * careful here since ino_t is unsigned.
394 */
395 freehdbb = (fs->lfs_freehd >> BMSHIFT);
396 for (bb = (ino >> BMSHIFT) - 1; bb >= freehdbb && bb > 0; --bb)
397 if (fs->lfs_ino_bitmap[bb])
398 break;
399 if (fs->lfs_ino_bitmap[bb] == 0)
400 return LFS_UNUSED_INUM;
401
402 /* Search the word we found */
403 for (tino = (bb << BMSHIFT) | BMMASK; tino >= (bb << BMSHIFT) &&
404 tino > LFS_UNUSED_INUM; tino--)
405 if (ISSET_BITMAP_FREE(fs, tino))
406 break;
407
408 if (tino <= LFS_IFILE_INUM)
409 tino = LFS_UNUSED_INUM;
410
411 return tino;
412 }
413
414 /* Free an inode. */
415 /* ARGUSED */
416 /* VOP_BWRITE 2i times */
417 int
418 lfs_vfree(struct vnode *vp, ino_t ino, int mode)
419 {
420 SEGUSE *sup;
421 CLEANERINFO *cip;
422 struct buf *cbp, *bp;
423 struct ifile *ifp;
424 struct inode *ip;
425 struct lfs *fs;
426 daddr_t old_iaddr;
427 ino_t otail;
428
429 /* Get the inode number and file system. */
430 ip = VTOI(vp);
431 fs = ip->i_lfs;
432 ino = ip->i_number;
433
434 ASSERT_NO_SEGLOCK(fs);
435 DLOG((DLOG_ALLOC, "lfs_vfree: free ino %lld\n", (long long)ino));
436
437 /* Drain of pending writes */
438 mutex_enter(&vp->v_interlock);
439 while (fs->lfs_version > 1 && WRITEINPROG(vp)) {
440 cv_wait(&vp->v_cv, &vp->v_interlock);
441 }
442 mutex_exit(&vp->v_interlock);
443
444 lfs_seglock(fs, SEGM_PROT);
445 vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
446
447 lfs_unmark_vnode(vp);
448 mutex_enter(&lfs_lock);
449 if (vp->v_uflag & VU_DIROP) {
450 vp->v_uflag &= ~VU_DIROP;
451 --lfs_dirvcount;
452 --fs->lfs_dirvcount;
453 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
454 wakeup(&fs->lfs_dirvcount);
455 wakeup(&lfs_dirvcount);
456 mutex_exit(&lfs_lock);
457 lfs_vunref(vp);
458
459 /*
460 * If this inode is not going to be written any more, any
461 * segment accounting left over from its truncation needs
462 * to occur at the end of the next dirops flush. Attach
463 * them to the fs-wide list for that purpose.
464 */
465 if (LIST_FIRST(&ip->i_lfs_segdhd) != NULL) {
466 struct segdelta *sd;
467
468 while((sd = LIST_FIRST(&ip->i_lfs_segdhd)) != NULL) {
469 LIST_REMOVE(sd, list);
470 LIST_INSERT_HEAD(&fs->lfs_segdhd, sd, list);
471 }
472 }
473 } else {
474 /*
475 * If it's not a dirop, we can finalize right away.
476 */
477 mutex_exit(&lfs_lock);
478 lfs_finalize_ino_seguse(fs, ip);
479 }
480
481 mutex_enter(&lfs_lock);
482 LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
483 mutex_exit(&lfs_lock);
484 ip->i_flag &= ~IN_ALLMOD;
485 ip->i_lfs_iflags |= LFSI_DELETED;
486
487 /*
488 * Set the ifile's inode entry to unused, increment its version number
489 * and link it onto the free chain.
490 */
491 SET_BITMAP_FREE(fs, ino);
492 LFS_IENTRY(ifp, fs, ino, bp);
493 old_iaddr = ifp->if_daddr;
494 ifp->if_daddr = LFS_UNUSED_DADDR;
495 ++ifp->if_version;
496 if (fs->lfs_version == 1) {
497 LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
498 LFS_PUT_HEADFREE(fs, cip, cbp, ino);
499 (void) LFS_BWRITE_LOG(bp); /* Ifile */
500 } else {
501 ino_t tino, onf;
502
503 ifp->if_nextfree = LFS_UNUSED_INUM;
504 (void) LFS_BWRITE_LOG(bp); /* Ifile */
505
506 tino = lfs_freelist_prev(fs, ino);
507 if (tino == LFS_UNUSED_INUM) {
508 /* Nothing free below us, put us on the head */
509 LFS_IENTRY(ifp, fs, ino, bp);
510 LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
511 LFS_PUT_HEADFREE(fs, cip, cbp, ino);
512 DLOG((DLOG_ALLOC, "lfs_vfree: headfree %lld -> %lld\n",
513 (long long)ifp->if_nextfree, (long long)ino));
514 LFS_BWRITE_LOG(bp); /* Ifile */
515
516 /* If the list was empty, set tail too */
517 LFS_GET_TAILFREE(fs, cip, cbp, &otail);
518 if (otail == LFS_UNUSED_INUM) {
519 LFS_PUT_TAILFREE(fs, cip, cbp, ino);
520 DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
521 "-> %lld\n", (long long)otail,
522 (long long)ino));
523 }
524 } else {
525 /*
526 * Insert this inode into the list after tino.
527 * We hold the segment lock so we don't have to
528 * worry about blocks being written out of order.
529 */
530 DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld "
531 " after %lld\n", ino, tino));
532
533 LFS_IENTRY(ifp, fs, tino, bp);
534 onf = ifp->if_nextfree;
535 ifp->if_nextfree = ino;
536 LFS_BWRITE_LOG(bp); /* Ifile */
537
538 LFS_IENTRY(ifp, fs, ino, bp);
539 ifp->if_nextfree = onf;
540 LFS_BWRITE_LOG(bp); /* Ifile */
541
542 /* If we're last, put us on the tail */
543 if (onf == LFS_UNUSED_INUM) {
544 LFS_GET_TAILFREE(fs, cip, cbp, &otail);
545 LFS_PUT_TAILFREE(fs, cip, cbp, ino);
546 DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
547 "-> %lld\n", (long long)otail,
548 (long long)ino));
549 }
550 }
551 }
552 #ifdef DIAGNOSTIC
553 if (ino == LFS_UNUSED_INUM) {
554 panic("inode 0 freed");
555 }
556 #endif /* DIAGNOSTIC */
557 if (old_iaddr != LFS_UNUSED_DADDR) {
558 LFS_SEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp);
559 #ifdef DIAGNOSTIC
560 if (sup->su_nbytes < sizeof (struct ufs1_dinode)) {
561 printf("lfs_vfree: negative byte count"
562 " (segment %" PRIu32 " short by %d)\n",
563 dtosn(fs, old_iaddr),
564 (int)sizeof (struct ufs1_dinode) -
565 sup->su_nbytes);
566 panic("lfs_vfree: negative byte count");
567 sup->su_nbytes = sizeof (struct ufs1_dinode);
568 }
569 #endif
570 sup->su_nbytes -= sizeof (struct ufs1_dinode);
571 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp); /* Ifile */
572 }
573
574 /* Set superblock modified bit and decrement file count. */
575 mutex_enter(&lfs_lock);
576 fs->lfs_fmod = 1;
577 mutex_exit(&lfs_lock);
578 --fs->lfs_nfiles;
579
580 VOP_UNLOCK(fs->lfs_ivnode, 0);
581 lfs_segunlock(fs);
582
583 return (0);
584 }
585
586 /*
587 * Sort the freelist and set up the free-inode bitmap.
588 * To be called by lfs_mountfs().
589 */
590 void
591 lfs_order_freelist(struct lfs *fs)
592 {
593 CLEANERINFO *cip;
594 IFILE *ifp = NULL;
595 struct buf *bp;
596 ino_t ino, firstino, lastino, maxino;
597 #ifdef notyet
598 struct vnode *vp;
599 #endif
600
601 ASSERT_NO_SEGLOCK(fs);
602 lfs_seglock(fs, SEGM_PROT);
603
604 maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
605 fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
606 fs->lfs_ino_bitmap = (lfs_bm_t *)
607 malloc(((maxino + BMMASK) >> BMSHIFT) * sizeof(lfs_bm_t),
608 M_SEGMENT, M_WAITOK | M_ZERO);
609 KASSERT(fs->lfs_ino_bitmap != NULL);
610
611 firstino = lastino = LFS_UNUSED_INUM;
612 for (ino = 0; ino < maxino; ino++) {
613 if (ino % fs->lfs_ifpb == 0)
614 LFS_IENTRY(ifp, fs, ino, bp);
615 else
616 ++ifp;
617
618 /* Don't put zero or ifile on the free list */
619 if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM)
620 continue;
621
622 #ifdef notyet
623 /* Address orphaned files */
624 if (ifp->if_nextfree == LFS_ORPHAN_NEXTFREE &&
625 VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp) == 0) {
626 lfs_truncate(vp, 0, 0, NOCRED);
627 vput(vp);
628 LFS_SEGENTRY(sup, fs, dtosn(fs, ifp->if_daddr), bp);
629 KASSERT(sup->su_nbytes >= DINODE1_SIZE);
630 sup->su_nbytes -= DINODE1_SIZE;
631 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, ifp->if_daddr), bp);
632
633 /* Set up to fall through to next section */
634 ifp->if_daddr = LFS_UNUSED_DADDR;
635 LFS_BWRITE_LOG(bp);
636 LFS_IENTRY(ifp, fs, ino, bp);
637 }
638 #endif
639
640 if (ifp->if_daddr == LFS_UNUSED_DADDR) {
641 if (firstino == LFS_UNUSED_INUM)
642 firstino = ino;
643 else {
644 brelse(bp, 0);
645
646 LFS_IENTRY(ifp, fs, lastino, bp);
647 ifp->if_nextfree = ino;
648 LFS_BWRITE_LOG(bp);
649
650 LFS_IENTRY(ifp, fs, ino, bp);
651 }
652 lastino = ino;
653
654 SET_BITMAP_FREE(fs, ino);
655 }
656
657 if ((ino + 1) % fs->lfs_ifpb == 0)
658 brelse(bp, 0);
659 }
660
661 LFS_PUT_HEADFREE(fs, cip, bp, firstino);
662 LFS_PUT_TAILFREE(fs, cip, bp, lastino);
663
664 lfs_segunlock(fs);
665 }
666
667 void
668 lfs_orphan(struct lfs *fs, ino_t ino)
669 {
670 IFILE *ifp;
671 struct buf *bp;
672
673 LFS_IENTRY(ifp, fs, ino, bp);
674 ifp->if_nextfree = LFS_ORPHAN_NEXTFREE;
675 LFS_BWRITE_LOG(bp);
676 }
677