151188Sbostic /* 251188Sbostic * Copyright (c) 1991 Regents of the University of California. 351188Sbostic * All rights reserved. 451188Sbostic * 551188Sbostic * %sccs.include.redist.c% 651188Sbostic * 7*54690Sbostic * @(#)lfs_segment.c 7.21 (Berkeley) 07/05/92 851188Sbostic */ 951188Sbostic 1051490Sbostic #include <sys/param.h> 1151490Sbostic #include <sys/systm.h> 1251490Sbostic #include <sys/namei.h> 1352085Sbostic #include <sys/kernel.h> 1451490Sbostic #include <sys/resourcevar.h> 1551490Sbostic #include <sys/file.h> 1651490Sbostic #include <sys/stat.h> 1751490Sbostic #include <sys/buf.h> 1851490Sbostic #include <sys/proc.h> 1951490Sbostic #include <sys/conf.h> 2051490Sbostic #include <sys/vnode.h> 2151490Sbostic #include <sys/specdev.h> 2251490Sbostic #include <sys/fifo.h> 2351490Sbostic #include <sys/malloc.h> 2451490Sbostic #include <sys/mount.h> 2551188Sbostic 2651499Sbostic #include <ufs/ufs/quota.h> 2751499Sbostic #include <ufs/ufs/inode.h> 2851499Sbostic #include <ufs/ufs/dir.h> 2951499Sbostic #include <ufs/ufs/ufsmount.h> 3051490Sbostic 3151499Sbostic #include <ufs/lfs/lfs.h> 3251499Sbostic #include <ufs/lfs/lfs_extern.h> 3351490Sbostic 3451860Sbostic /* In-memory description of a segment about to be written. */ 3551860Sbostic struct segment { 3652085Sbostic struct buf **bpp; /* pointer to buffer array */ 3752085Sbostic struct buf **cbpp; /* pointer to next available bp */ 3852085Sbostic struct buf *ibp; /* buffer pointer to inode page */ 3952085Sbostic struct finfo *fip; /* current fileinfo pointer */ 4051860Sbostic void *segsum; /* segment summary info */ 4151860Sbostic u_long ninodes; /* number of inodes in this segment */ 4251860Sbostic u_long seg_bytes_left; /* bytes left in segment */ 4351860Sbostic u_long sum_bytes_left; /* bytes left in summary block */ 4451860Sbostic u_long seg_number; /* number of this segment */ 4551860Sbostic #define SEGM_CKP 0x01 /* doing a checkpoint */ 4651860Sbostic u_long seg_flags; /* run-time flags for this segment */ 4751860Sbostic }; 4851860Sbostic 4951188Sbostic /* 5051860Sbostic * Determine if it's OK to start a partial in this segment, or if we need 5151860Sbostic * to go on to a new segment. 5251301Sbostic */ 5351860Sbostic #define LFS_PARTIAL_FITS(fs) \ 5451860Sbostic ((fs)->lfs_dbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \ 5551860Sbostic 1 << (fs)->lfs_fsbtodb) 5651188Sbostic 5753347Sbostic void lfs_callback __P((struct buf *)); 5852085Sbostic void lfs_gather __P((struct lfs *, struct segment *, 5952085Sbostic struct vnode *, int (*) __P((struct lfs *, struct buf *)))); 6052085Sbostic void lfs_initseg __P((struct lfs *, struct segment *)); 6152085Sbostic void lfs_iset __P((struct inode *, daddr_t, time_t)); 6252085Sbostic int lfs_match_data __P((struct lfs *, struct buf *)); 6352085Sbostic int lfs_match_dindir __P((struct lfs *, struct buf *)); 6452085Sbostic int lfs_match_indir __P((struct lfs *, struct buf *)); 6552085Sbostic int lfs_match_tindir __P((struct lfs *, struct buf *)); 6652085Sbostic struct buf * 6752688Sbostic lfs_newbuf __P((struct lfs *, daddr_t, size_t)); 6852077Sbostic void lfs_newseg __P((struct lfs *)); 6952085Sbostic void lfs_shellsort __P((struct buf **, daddr_t *, register int)); 7052077Sbostic void lfs_updatemeta __P((struct lfs *, 7152085Sbostic struct segment *, struct vnode *, daddr_t *, struct buf **, int)); 7252085Sbostic void lfs_writefile __P((struct lfs *, struct segment *, struct vnode *)); 7354264Sbostic int lfs_writeinode __P((struct lfs *, struct segment *, struct inode *)); 7454264Sbostic int lfs_writeseg __P((struct lfs *, struct segment *)); 7552085Sbostic void lfs_writesuper __P((struct lfs *, struct segment *)); 7654264Sbostic void lfs_writevnodes __P((struct lfs *fs, struct mount *mp, 7754264Sbostic struct segment *sp, int dirops)); 7851188Sbostic 7951860Sbostic int lfs_allclean_wakeup; /* Cleaner wakeup address. */ 8051860Sbostic 8152328Sbostic /* 8252328Sbostic * Ifile and meta data blocks are not marked busy, so segment writes MUST be 8352328Sbostic * single threaded. Currently, there are two paths into lfs_segwrite, sync() 8452328Sbostic * and getnewbuf(). They both mark the file system busy. Lfs_vflush() 8552328Sbostic * explicitly marks the file system busy. So lfs_segwrite is safe. I think. 8652328Sbostic */ 8752328Sbostic 8851188Sbostic int 8952328Sbostic lfs_vflush(vp) 9052328Sbostic struct vnode *vp; 9152328Sbostic { 9252328Sbostic struct inode *ip; 9352328Sbostic struct lfs *fs; 9452328Sbostic struct segment *sp; 9552328Sbostic int error, s; 9652328Sbostic 9752328Sbostic #ifdef VERBOSE 9852328Sbostic printf("lfs_vflush\n"); 9952328Sbostic #endif 100*54690Sbostic fs = VFSTOUFS(vp->v_mount)->um_lfs; 101*54690Sbostic lfs_seglock(fs); 10252328Sbostic 10352328Sbostic /* 10452328Sbostic * Allocate a segment structure and enough space to hold pointers to 10552328Sbostic * the maximum possible number of buffers which can be described in a 10652328Sbostic * single summary block. 10752328Sbostic */ 10852328Sbostic sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK); 10952328Sbostic sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) / 11052328Sbostic sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK); 11152328Sbostic sp->seg_flags = SEGM_CKP; 11252328Sbostic lfs_initseg(fs, sp); 11352328Sbostic 11452328Sbostic /* 11552328Sbostic * Keep a cumulative count of the outstanding I/O operations. If the 11652328Sbostic * disk drive catches up with us it could go to zero before we finish, 11752328Sbostic * so we artificially increment it by one until we've scheduled all of 11852328Sbostic * the writes we intend to do. 11952328Sbostic */ 12052328Sbostic s = splbio(); 12152688Sbostic ++fs->lfs_iocount; 12252328Sbostic splx(s); 12352328Sbostic 12452328Sbostic if (vp->v_dirtyblkhd != NULL) 12552328Sbostic lfs_writefile(fs, sp, vp); 12652328Sbostic ip = VTOI(vp); 12754264Sbostic (void) lfs_writeinode(fs, sp, ip); 12852328Sbostic ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG); 12952328Sbostic 130*54690Sbostic (void)lfs_writeseg(fs, sp); 13152328Sbostic 13252328Sbostic /* 13352328Sbostic * If the I/O count is non-zero, sleep until it reaches zero. At the 13452328Sbostic * moment, the user's process hangs around so we can sleep. 13552328Sbostic */ 13652328Sbostic s = splbio(); 13752328Sbostic if (--fs->lfs_iocount && (error = 13852995Sbostic tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs vflush", 0))) { 13952995Sbostic free(sp->bpp, M_SEGMENT); 14052995Sbostic free(sp, M_SEGMENT); 14152328Sbostic return (error); 14252995Sbostic } 14352328Sbostic splx(s); 144*54690Sbostic lfs_segunlock(fs); 14552328Sbostic 14652995Sbostic /* 14752995Sbostic * XXX 14852995Sbostic * Should be writing a checkpoint? 14952995Sbostic */ 15052328Sbostic free(sp->bpp, M_SEGMENT); 15152328Sbostic free(sp, M_SEGMENT); 15252328Sbostic 15352328Sbostic return (0); 15452328Sbostic } 15552328Sbostic 15654264Sbostic void 15754264Sbostic lfs_writevnodes(fs, mp, sp, dirops) 15854264Sbostic struct lfs *fs; 15954264Sbostic struct mount *mp; 16054264Sbostic struct segment *sp; 16154264Sbostic int dirops; 16254264Sbostic { 16354264Sbostic struct inode *ip; 16454264Sbostic struct vnode *vp; 16554264Sbostic int error, s; 16654264Sbostic 16754264Sbostic loop: for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) { 16854264Sbostic /* 16954264Sbostic * If the vnode that we are about to sync is no longer 17054264Sbostic * associated with this mount point, start over. 17154264Sbostic */ 17254264Sbostic if (vp->v_mount != mp) 17354264Sbostic goto loop; 17454264Sbostic 17554264Sbostic if (dirops && !(vp->v_flag & VDIROP) || 17654264Sbostic !dirops && (vp->v_flag & VDIROP)) 17754264Sbostic continue; 17854264Sbostic /* 17954264Sbostic * XXX 18054264Sbostic * Up the ref count so we don't get tossed out of 18154264Sbostic * memory. 18254264Sbostic */ 18354264Sbostic VREF(vp); 18454264Sbostic 18554264Sbostic /* 18654264Sbostic * Write the inode/file if dirty and it's not the 18754264Sbostic * the IFILE. 18854264Sbostic */ 18954264Sbostic ip = VTOI(vp); 19054264Sbostic if ((ip->i_flag & (IMOD | IACC | IUPD | ICHG) || 19154264Sbostic vp->v_dirtyblkhd != NULL) && 19254264Sbostic ip->i_number != LFS_IFILE_INUM) { 19354264Sbostic if (vp->v_dirtyblkhd != NULL) 19454264Sbostic lfs_writefile(fs, sp, vp); 19554264Sbostic (void) lfs_writeinode(fs, sp, ip); 19654264Sbostic ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG); 19754264Sbostic } 19854264Sbostic vp->v_flag &= ~VDIROP; 19954264Sbostic vrele(vp); 20054264Sbostic } 20154264Sbostic } 20254264Sbostic 20352328Sbostic int 20451215Sbostic lfs_segwrite(mp, do_ckp) 20552085Sbostic struct mount *mp; 20651860Sbostic int do_ckp; /* Do a checkpoint. */ 20751188Sbostic { 20852085Sbostic struct inode *ip; 20951499Sbostic struct lfs *fs; 21052085Sbostic struct segment *sp; 21152085Sbostic struct vnode *vp; 21254264Sbostic int error, s; 21351188Sbostic 21451860Sbostic #ifdef VERBOSE 21551860Sbostic printf("lfs_segwrite\n"); 21651860Sbostic #endif 21752328Sbostic fs = VFSTOUFS(mp)->um_lfs; 218*54690Sbostic lfs_seglock(fs); 21952085Sbostic 22051860Sbostic /* 22152328Sbostic * Allocate a segment structure and enough space to hold pointers to 22252328Sbostic * the maximum possible number of buffers which can be described in a 22352328Sbostic * single summary block. 22452328Sbostic */ 22552328Sbostic sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK); 22652328Sbostic sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) / 22752328Sbostic sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK); 22852328Sbostic sp->seg_flags = do_ckp ? SEGM_CKP : 0; 22952328Sbostic lfs_initseg(fs, sp); 23052328Sbostic 23152328Sbostic /* 23252688Sbostic * Keep a cumulative count of the outstanding I/O operations. If the 23352688Sbostic * disk drive catches up with us it could go to zero before we finish, 23452688Sbostic * so we artificially increment it by one until we've scheduled all of 23552688Sbostic * the writes we intend to do. If not a checkpoint, we never do the 23652688Sbostic * final decrement, avoiding the wakeup in the callback routine. 23751860Sbostic */ 23852688Sbostic s = splbio(); 23954264Sbostic fs->lfs_iocount++; 24052688Sbostic splx(s); 24151342Sbostic 24254264Sbostic lfs_writevnodes(fs, mp, sp, 0); 24354264Sbostic s = splbio(); 24454264Sbostic fs->lfs_writer = 1; 24554264Sbostic if (fs->lfs_dirops && (error = 24654264Sbostic tsleep(&fs->lfs_writer, PRIBIO + 1, "lfs writer", 0))) { 24754264Sbostic free(sp->bpp, M_SEGMENT); 24854264Sbostic free(sp, M_SEGMENT); 24954264Sbostic fs->lfs_writer = 0; 25054264Sbostic splx(s); 25154264Sbostic return(error); 25254264Sbostic } 25354264Sbostic splx(s); 25451860Sbostic 25554264Sbostic lfs_writevnodes(fs, mp, sp, 1); 25651860Sbostic 25754264Sbostic /* 25854264Sbostic * If this is a checkpoint, we need to loop on both the ifile and 25954264Sbostic * the writeseg to make sure that we don't end up with any dirty 26054264Sbostic * buffers left when this is all over. 26154264Sbostic */ 26254264Sbostic if (do_ckp || fs->lfs_doifile) { 26354264Sbostic redo: 26454264Sbostic vp = fs->lfs_ivnode; 26554264Sbostic while (vget(vp)); 26652328Sbostic ip = VTOI(vp); 26754264Sbostic do { 26852328Sbostic if (vp->v_dirtyblkhd != NULL) 26952328Sbostic lfs_writefile(fs, sp, vp); 27054264Sbostic } while (lfs_writeinode(fs, sp, ip) && do_ckp); 27152077Sbostic ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG); 27252077Sbostic vput(vp); 27354264Sbostic if (lfs_writeseg(fs, sp) && do_ckp) { 27454264Sbostic lfs_initseg(fs, sp); 27554264Sbostic goto redo; 27654264Sbostic } 27754264Sbostic } else 27854264Sbostic (void) lfs_writeseg(fs, sp); 27951342Sbostic 28051215Sbostic /* 28151860Sbostic * If the I/O count is non-zero, sleep until it reaches zero. At the 28251860Sbostic * moment, the user's process hangs around so we can sleep. 28351215Sbostic */ 28452688Sbostic s = splbio(); 28552688Sbostic --fs->lfs_iocount; 28654264Sbostic fs->lfs_writer = 0; 28754264Sbostic fs->lfs_doifile = 0; 28854264Sbostic wakeup(&fs->lfs_dirops); 28954264Sbostic 29051860Sbostic if (do_ckp) { 29152688Sbostic if (fs->lfs_iocount && (error = 29252995Sbostic tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs sync", 0))) { 29352995Sbostic free(sp->bpp, M_SEGMENT); 29452995Sbostic free(sp, M_SEGMENT); 29551915Sbostic return (error); 29652995Sbostic } 29751860Sbostic splx(s); 29851860Sbostic lfs_writesuper(fs, sp); 29952688Sbostic } else 30052688Sbostic splx(s); 30151215Sbostic 302*54690Sbostic lfs_segunlock(fs); 303*54690Sbostic 30451927Sbostic free(sp->bpp, M_SEGMENT); 30551927Sbostic free(sp, M_SEGMENT); 30651215Sbostic 30751860Sbostic return (0); 30851188Sbostic } 30951188Sbostic 31051860Sbostic /* 31151860Sbostic * Write the dirty blocks associated with a vnode. 31251860Sbostic */ 31352077Sbostic void 31451860Sbostic lfs_writefile(fs, sp, vp) 31551499Sbostic struct lfs *fs; 31652085Sbostic struct segment *sp; 31752085Sbostic struct vnode *vp; 31851188Sbostic { 31951860Sbostic struct buf *bp; 32052085Sbostic struct finfo *fip; 32151860Sbostic IFILE *ifp; 32251188Sbostic 32351860Sbostic #ifdef VERBOSE 32451860Sbostic printf("lfs_writefile\n"); 32551860Sbostic #endif 32652085Sbostic if (sp->seg_bytes_left < fs->lfs_bsize || 32752085Sbostic sp->sum_bytes_left < sizeof(struct finfo)) { 32854264Sbostic (void) lfs_writeseg(fs, sp); 32952085Sbostic lfs_initseg(fs, sp); 33052085Sbostic } 33152085Sbostic sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(daddr_t); 33251215Sbostic 33352085Sbostic fip = sp->fip; 33452085Sbostic fip->fi_nblocks = 0; 33552085Sbostic fip->fi_ino = VTOI(vp)->i_number; 33652085Sbostic LFS_IENTRY(ifp, fs, fip->fi_ino, bp); 33752085Sbostic fip->fi_version = ifp->if_version; 33852085Sbostic brelse(bp); 33951188Sbostic 34052085Sbostic /* 34152085Sbostic * It may not be necessary to write the meta-data blocks at this point, 34252085Sbostic * as the roll-forward recovery code should be able to reconstruct the 34352085Sbostic * list. 34452085Sbostic */ 34552085Sbostic lfs_gather(fs, sp, vp, lfs_match_data); 34652085Sbostic lfs_gather(fs, sp, vp, lfs_match_indir); 34752085Sbostic lfs_gather(fs, sp, vp, lfs_match_dindir); 34851860Sbostic #ifdef TRIPLE 34952085Sbostic lfs_gather(fs, sp, vp, lfs_match_tindir); 35051860Sbostic #endif 35151342Sbostic 35252085Sbostic fip = sp->fip; 35351860Sbostic #ifdef META 35452085Sbostic printf("lfs_writefile: adding %d blocks\n", fip->fi_nblocks); 35551860Sbostic #endif 35652085Sbostic if (fip->fi_nblocks != 0) { 35752085Sbostic ++((SEGSUM *)(sp->segsum))->ss_nfinfo; 35852085Sbostic sp->fip = 35952085Sbostic (struct finfo *)((caddr_t)fip + sizeof(struct finfo) + 36052085Sbostic sizeof(daddr_t) * (fip->fi_nblocks - 1)); 36152682Sstaelin } else 36252682Sstaelin sp->sum_bytes_left += sizeof(struct finfo) - sizeof(daddr_t); 36351215Sbostic } 36451215Sbostic 36554264Sbostic int 36651915Sbostic lfs_writeinode(fs, sp, ip) 36751915Sbostic struct lfs *fs; 36852085Sbostic struct segment *sp; 36952085Sbostic struct inode *ip; 37051915Sbostic { 37152085Sbostic struct buf *bp, *ibp; 37252077Sbostic IFILE *ifp; 37352682Sstaelin SEGUSE *sup; 37452682Sstaelin daddr_t daddr; 37552077Sbostic ino_t ino; 37651915Sbostic int ndx; 37754264Sbostic int redo_ifile = 0; 37851915Sbostic 37951915Sbostic #ifdef VERBOSE 38051915Sbostic printf("lfs_writeinode\n"); 38151915Sbostic #endif 38251915Sbostic /* Allocate a new inode block if necessary. */ 38351915Sbostic if (sp->ibp == NULL) { 38451915Sbostic /* Allocate a new segment if necessary. */ 38551915Sbostic if (sp->seg_bytes_left < fs->lfs_bsize || 38651915Sbostic sp->sum_bytes_left < sizeof(daddr_t)) { 38754264Sbostic (void) lfs_writeseg(fs, sp); 38851915Sbostic lfs_initseg(fs, sp); 38951915Sbostic } 39051915Sbostic 39151915Sbostic /* Get next inode block. */ 39252682Sstaelin daddr = fs->lfs_offset; 39351915Sbostic fs->lfs_offset += fsbtodb(fs, 1); 39451915Sbostic sp->ibp = *sp->cbpp++ = 39552688Sbostic lfs_newbuf(fs, daddr, fs->lfs_bsize); 39651915Sbostic 39752688Sbostic /* Set remaining space counters. */ 39851915Sbostic sp->seg_bytes_left -= fs->lfs_bsize; 39951915Sbostic sp->sum_bytes_left -= sizeof(daddr_t); 40052077Sbostic ndx = LFS_SUMMARY_SIZE / sizeof(daddr_t) - 40151915Sbostic sp->ninodes / INOPB(fs) - 1; 40252682Sstaelin ((daddr_t *)(sp->segsum))[ndx] = daddr; 40351915Sbostic } 40451915Sbostic 40552085Sbostic /* Update the inode times and copy the inode onto the inode page. */ 40652077Sbostic ITIMES(ip, &time, &time); 40751915Sbostic bp = sp->ibp; 40852085Sbostic bp->b_un.b_dino[sp->ninodes % INOPB(fs)] = ip->i_din; 40951915Sbostic 41051915Sbostic /* Increment inode count in segment summary block. */ 41151915Sbostic ++((SEGSUM *)(sp->segsum))->ss_ninos; 41251915Sbostic 41351915Sbostic /* If this page is full, set flag to allocate a new page. */ 41451915Sbostic if (++sp->ninodes % INOPB(fs) == 0) 41551915Sbostic sp->ibp = NULL; 41651915Sbostic 41751915Sbostic /* 41852077Sbostic * If updating the ifile, update the super-block. Update the disk 41952077Sbostic * address and access times for this inode in the ifile. 42051915Sbostic */ 42152077Sbostic ino = ip->i_number; 42252077Sbostic if (ino == LFS_IFILE_INUM) 42351915Sbostic fs->lfs_idaddr = bp->b_blkno; 42452077Sbostic 42552077Sbostic LFS_IENTRY(ifp, fs, ino, ibp); 42652682Sstaelin daddr = ifp->if_daddr; 42752077Sbostic ifp->if_daddr = bp->b_blkno; 42852085Sbostic LFS_UBWRITE(ibp); 42954264Sbostic redo_ifile = (ino == LFS_IFILE_INUM && !(ibp->b_flags & B_GATHERED)); 43052682Sstaelin 43154264Sbostic /* 43254264Sbostic * No need to update segment usage if there was no former inode address 43354264Sbostic * or if the last inode address is in the current partial segment. 43454264Sbostic */ 43554264Sbostic if (daddr != LFS_UNUSED_DADDR && 43654264Sbostic !(daddr >= fs->lfs_curseg && daddr <= ifp->if_daddr) ) { 43752682Sstaelin LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp); 43852682Sstaelin #ifdef DIAGNOSTIC 43954264Sbostic if (sup->su_nbytes < sizeof(struct dinode)) { 44052819Sbostic /* XXX -- Change to a panic. */ 44152819Sbostic printf("lfs: negative bytes (segment %d)\n", 44252682Sstaelin datosn(fs, daddr)); 44354264Sbostic panic("negative bytes"); 44454264Sbostic } 44552682Sstaelin #endif 44652682Sstaelin sup->su_nbytes -= sizeof(struct dinode); 44752682Sstaelin LFS_UBWRITE(bp); 44854264Sbostic redo_ifile |= 44954264Sbostic (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED)); 45052682Sstaelin } 45154264Sbostic return(redo_ifile); 45251915Sbostic } 45351915Sbostic 45452077Sbostic void 45551215Sbostic lfs_gather(fs, sp, vp, match) 45651499Sbostic struct lfs *fs; 45752085Sbostic struct segment *sp; 45852085Sbostic struct vnode *vp; 45952085Sbostic int (*match) __P((struct lfs *, struct buf *)); 46051215Sbostic { 46154264Sbostic struct buf **bpp, *bp; 46254264Sbostic struct buf *lastbp; 46352085Sbostic struct finfo *fip; 46452085Sbostic struct inode *ip; 46551215Sbostic daddr_t *lbp, *start_lbp; 46651342Sbostic u_long version; 46751342Sbostic int s; 46851215Sbostic 46951860Sbostic #ifdef VERBOSE 47051860Sbostic printf("lfs_gather\n"); 47151860Sbostic #endif 47251215Sbostic ip = VTOI(vp); 47351215Sbostic bpp = sp->cbpp; 47451215Sbostic fip = sp->fip; 47551215Sbostic start_lbp = lbp = &fip->fi_blocks[fip->fi_nblocks]; 47651215Sbostic 47753145Sstaelin loop: s = splbio(); 47854264Sbostic lastbp = NULL; 47954264Sbostic for (bp = vp->v_dirtyblkhd; bp; lastbp = bp, bp = bp->b_blockf) { 48054264Sbostic if (bp->b_flags & B_BUSY || !match(fs, bp) || 48154264Sbostic bp->b_flags & B_GATHERED) 48251215Sbostic continue; 48351342Sbostic #ifdef DIAGNOSTIC 48451860Sbostic if (!(bp->b_flags & B_DELWRI)) 48551915Sbostic panic("lfs_gather: bp not B_DELWRI"); 48651860Sbostic if (!(bp->b_flags & B_LOCKED)) 48751915Sbostic panic("lfs_gather: bp not B_LOCKED"); 48851342Sbostic #endif 48951860Sbostic /* 49051860Sbostic * If full, finish this segment. We may be doing I/O, so 49151860Sbostic * release and reacquire the splbio(). 49251860Sbostic */ 49351342Sbostic if (sp->sum_bytes_left < sizeof(daddr_t) || 49451215Sbostic sp->seg_bytes_left < fs->lfs_bsize) { 49551215Sbostic splx(s); 49651342Sbostic lfs_updatemeta(fs, 49751860Sbostic sp, vp, start_lbp, bpp, lbp - start_lbp); 49851215Sbostic 49951342Sbostic /* Add the current file to the segment summary. */ 50051342Sbostic ++((SEGSUM *)(sp->segsum))->ss_nfinfo; 50151215Sbostic 50251342Sbostic version = fip->fi_version; 50354264Sbostic (void) lfs_writeseg(fs, sp); 50451915Sbostic lfs_initseg(fs, sp); 50551342Sbostic 50651215Sbostic fip = sp->fip; 50751342Sbostic fip->fi_version = version; 50851215Sbostic fip->fi_ino = ip->i_number; 50951342Sbostic start_lbp = lbp = fip->fi_blocks; 51051342Sbostic 51152682Sstaelin sp->sum_bytes_left -= 51252682Sstaelin sizeof(struct finfo) - sizeof(daddr_t); 51352682Sstaelin 51451215Sbostic bpp = sp->cbpp; 51553145Sstaelin goto loop; 51651215Sbostic } 51752682Sstaelin 51852682Sstaelin /* Insert into the buffer list, update the FINFO block. */ 51954264Sbostic bp->b_flags |= B_GATHERED; 52052682Sstaelin *sp->cbpp++ = bp; 52152682Sstaelin ++fip->fi_nblocks; 52252682Sstaelin *lbp++ = bp->b_lblkno; 52352682Sstaelin 52452682Sstaelin sp->sum_bytes_left -= sizeof(daddr_t); 52552682Sstaelin sp->seg_bytes_left -= bp->b_bufsize; 52651188Sbostic } 52751215Sbostic splx(s); 52851860Sbostic lfs_updatemeta(fs, sp, vp, start_lbp, bpp, lbp - start_lbp); 52951188Sbostic } 53051188Sbostic 53151342Sbostic /* 53251342Sbostic * Update the metadata that points to the blocks listed in the FINFO 53351188Sbostic * array. 53451188Sbostic */ 53552077Sbostic void 53651860Sbostic lfs_updatemeta(fs, sp, vp, lbp, bpp, nblocks) 53751499Sbostic struct lfs *fs; 53852085Sbostic struct segment *sp; 53952085Sbostic struct vnode *vp; 54051215Sbostic daddr_t *lbp; 54152085Sbostic struct buf **bpp; 54251215Sbostic int nblocks; 54351188Sbostic { 54451915Sbostic SEGUSE *sup; 54552085Sbostic struct buf *bp; 54651860Sbostic INDIR a[NIADDR], *ap; 54752085Sbostic struct inode *ip; 54851915Sbostic daddr_t daddr, lbn, off; 54951860Sbostic int db_per_fsb, error, i, num; 55051188Sbostic 55151860Sbostic #ifdef VERBOSE 55251860Sbostic printf("lfs_updatemeta\n"); 55351860Sbostic #endif 55451342Sbostic if (nblocks == 0) 55551215Sbostic return; 55651215Sbostic 55751915Sbostic /* Sort the blocks. */ 55852077Sbostic lfs_shellsort(bpp, lbp, nblocks); 55951215Sbostic 56051915Sbostic /* 56151915Sbostic * Assign disk addresses, and update references to the logical 56251915Sbostic * block and the segment usage information. 56351915Sbostic */ 56451860Sbostic db_per_fsb = fsbtodb(fs, 1); 56551915Sbostic for (i = nblocks; i--; ++bpp) { 56651915Sbostic lbn = *lbp++; 56751915Sbostic (*bpp)->b_blkno = off = fs->lfs_offset; 56851860Sbostic fs->lfs_offset += db_per_fsb; 56951215Sbostic 57051860Sbostic if (error = lfs_bmaparray(vp, lbn, &daddr, a, &num)) 57152085Sbostic panic("lfs_updatemeta: lfs_bmaparray %d", error); 57251860Sbostic ip = VTOI(vp); 57351860Sbostic switch (num) { 57451860Sbostic case 0: 57551915Sbostic ip->i_db[lbn] = off; 57651860Sbostic break; 57751860Sbostic case 1: 57851915Sbostic ip->i_ib[a[0].in_off] = off; 57951860Sbostic break; 58051860Sbostic default: 58151860Sbostic ap = &a[num - 1]; 58251860Sbostic if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 58351860Sbostic panic("lfs_updatemeta: bread bno %d", 58451860Sbostic ap->in_lbn); 58551915Sbostic bp->b_un.b_daddr[ap->in_off] = off; 58653530Sheideman VOP_BWRITE(bp); 58751188Sbostic } 58851915Sbostic 58951915Sbostic /* Update segment usage information. */ 59051915Sbostic if (daddr != UNASSIGNED) { 59151915Sbostic LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp); 59251915Sbostic #ifdef DIAGNOSTIC 59354264Sbostic if (sup->su_nbytes < fs->lfs_bsize) { 59452819Sbostic /* XXX -- Change to a panic. */ 59552819Sbostic printf("lfs: negative bytes (segment %d)\n", 59651915Sbostic datosn(fs, daddr)); 59754264Sbostic panic ("Negative Bytes"); 59854264Sbostic } 59951915Sbostic #endif 60051915Sbostic sup->su_nbytes -= fs->lfs_bsize; 60152085Sbostic LFS_UBWRITE(bp); 60251915Sbostic } 60351188Sbostic } 60451188Sbostic } 60551188Sbostic 60651915Sbostic /* 60751915Sbostic * Start a new segment. 60851915Sbostic */ 60952077Sbostic void 61051915Sbostic lfs_initseg(fs, sp) 61151499Sbostic struct lfs *fs; 61252085Sbostic struct segment *sp; 61351188Sbostic { 61451915Sbostic SEGUSE *sup; 61551915Sbostic SEGSUM *ssp; 61651915Sbostic struct buf *bp; 61751915Sbostic daddr_t lbn, *lbnp; 61851215Sbostic 61951860Sbostic #ifdef VERBOSE 62051915Sbostic printf("lfs_initseg\n"); 62151860Sbostic #endif 62251915Sbostic /* Advance to the next segment. */ 62351927Sbostic if (!LFS_PARTIAL_FITS(fs)) { 62452682Sstaelin /* Wake up any cleaning procs waiting on this file system. */ 62552688Sbostic wakeup(&fs->lfs_nextseg); 62652688Sbostic wakeup(&lfs_allclean_wakeup); 62752682Sstaelin 62851927Sbostic lfs_newseg(fs); 62951927Sbostic fs->lfs_offset = fs->lfs_curseg; 63051915Sbostic sp->seg_number = datosn(fs, fs->lfs_curseg); 63151915Sbostic sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE; 63251915Sbostic 63351915Sbostic /* 63451927Sbostic * If the segment contains a superblock, update the offset 63551927Sbostic * and summary address to skip over it. 63651915Sbostic */ 63752077Sbostic LFS_SEGENTRY(sup, fs, sp->seg_number, bp); 63851927Sbostic if (sup->su_flags & SEGUSE_SUPERBLOCK) { 63951915Sbostic fs->lfs_offset += LFS_SBPAD / DEV_BSIZE; 64051915Sbostic sp->seg_bytes_left -= LFS_SBPAD; 64151215Sbostic } 64252085Sbostic brelse(bp); 64351915Sbostic } else { 64451915Sbostic sp->seg_number = datosn(fs, fs->lfs_curseg); 64551915Sbostic sp->seg_bytes_left = (fs->lfs_dbpseg - 64651915Sbostic (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE; 64751915Sbostic } 64854264Sbostic fs->lfs_lastpseg = fs->lfs_offset; 64951342Sbostic 65051915Sbostic sp->ibp = NULL; 65151915Sbostic sp->ninodes = 0; 65251342Sbostic 65351915Sbostic /* Get a new buffer for SEGSUM and enter it into the buffer list. */ 65451915Sbostic sp->cbpp = sp->bpp; 65552688Sbostic *sp->cbpp = lfs_newbuf(fs, fs->lfs_offset, LFS_SUMMARY_SIZE); 65651915Sbostic sp->segsum = (*sp->cbpp)->b_un.b_addr; 65751915Sbostic ++sp->cbpp; 65851915Sbostic fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE; 65951342Sbostic 66051915Sbostic /* Set point to SEGSUM, initialize it. */ 66151915Sbostic ssp = sp->segsum; 66251915Sbostic ssp->ss_next = fs->lfs_nextseg; 66351915Sbostic ssp->ss_nfinfo = ssp->ss_ninos = 0; 66451342Sbostic 66551915Sbostic /* Set pointer to first FINFO, initialize it. */ 66652085Sbostic sp->fip = (struct finfo *)(sp->segsum + sizeof(SEGSUM)); 66751915Sbostic sp->fip->fi_nblocks = 0; 66851342Sbostic 66951915Sbostic sp->seg_bytes_left -= LFS_SUMMARY_SIZE; 67051915Sbostic sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM); 67151915Sbostic } 67251342Sbostic 67351915Sbostic /* 67451915Sbostic * Return the next segment to write. 67551915Sbostic */ 67652077Sbostic void 67751915Sbostic lfs_newseg(fs) 67851915Sbostic struct lfs *fs; 67951915Sbostic { 68051927Sbostic CLEANERINFO *cip; 68151915Sbostic SEGUSE *sup; 68251915Sbostic struct buf *bp; 68351927Sbostic int curseg, isdirty, sn; 68451915Sbostic 68551915Sbostic #ifdef VERBOSE 68651915Sbostic printf("lfs_newseg\n"); 68751915Sbostic #endif 68851927Sbostic /* 68951927Sbostic * Turn off the active bit for the current segment, turn on the 69051927Sbostic * active and dirty bits for the next segment, update the cleaner 69151927Sbostic * info. Set the current segment to the next segment, get a new 69251927Sbostic * next segment. 69351927Sbostic */ 69451927Sbostic LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_curseg), bp); 69551927Sbostic sup->su_flags &= ~SEGUSE_ACTIVE; 69652085Sbostic LFS_UBWRITE(bp); 69751927Sbostic 69851927Sbostic LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp); 69954264Sbostic sup->su_flags |= SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_LIVELOG; 70052085Sbostic LFS_UBWRITE(bp); 70151927Sbostic 70251927Sbostic LFS_CLEANERINFO(cip, fs, bp); 70351927Sbostic --cip->clean; 70451927Sbostic ++cip->dirty; 70552085Sbostic LFS_UBWRITE(bp); 70651927Sbostic 70751927Sbostic fs->lfs_lastseg = fs->lfs_curseg; 70851927Sbostic fs->lfs_curseg = fs->lfs_nextseg; 70951927Sbostic for (sn = curseg = datosn(fs, fs->lfs_curseg);;) { 71051915Sbostic sn = (sn + 1) % fs->lfs_nseg; 71151927Sbostic if (sn == curseg) 71251915Sbostic panic("lfs_nextseg: no clean segments"); 71351915Sbostic LFS_SEGENTRY(sup, fs, sn, bp); 71451915Sbostic isdirty = sup->su_flags & SEGUSE_DIRTY; 71552085Sbostic brelse(bp); 71651915Sbostic if (!isdirty) 71751915Sbostic break; 71851915Sbostic } 71951927Sbostic fs->lfs_nextseg = sntoda(fs, sn); 72051188Sbostic } 72151188Sbostic 72254264Sbostic int 72351188Sbostic lfs_writeseg(fs, sp) 72451499Sbostic struct lfs *fs; 72552085Sbostic struct segment *sp; 72651188Sbostic { 72752688Sbostic struct buf **bpp, *bp, *cbp; 72851188Sbostic SEGUSE *sup; 72952085Sbostic SEGSUM *ssp; 73051860Sbostic dev_t i_dev; 73154264Sbostic size_t size; 73251860Sbostic u_long *datap, *dp; 73354264Sbostic int ch_per_blk, do_again, i, nblocks, num, s; 73454264Sbostic int (*strategy)__P((struct vop_strategy_args *)); 735*54690Sbostic struct vop_strategy_args vop_strategy_a; 73652688Sbostic char *p; 73751188Sbostic 73851860Sbostic #ifdef VERBOSE 73951860Sbostic printf("lfs_writeseg\n"); 74051860Sbostic #endif 74154264Sbostic /* Checkpoint always writes superblock, even if no data blocks. */ 74254264Sbostic if ((nblocks = sp->cbpp - sp->bpp) == 0 && !(sp->seg_flags & SEGM_CKP)) 74352085Sbostic return; 74452085Sbostic 74551188Sbostic /* 74652085Sbostic * Compute checksum across data and then across summary; the first 74752085Sbostic * block (the summary block) is skipped. Set the create time here 74852085Sbostic * so that it's guaranteed to be later than the inode mod times. 74951860Sbostic * 75051860Sbostic * XXX 75151860Sbostic * Fix this to do it inline, instead of malloc/copy. 75251188Sbostic */ 75351860Sbostic datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK); 75451915Sbostic for (bpp = sp->bpp, i = nblocks - 1; i--;) 75551915Sbostic *dp++ = (*++bpp)->b_un.b_words[0]; 75652085Sbostic ssp = (SEGSUM *)sp->segsum; 75752103Sbostic ssp->ss_create = time.tv_sec; 75852085Sbostic ssp->ss_datasum = cksum(datap, nblocks * sizeof(u_long)); 75952085Sbostic ssp->ss_sumsum = 76052085Sbostic cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum)); 76151927Sbostic free(datap, M_SEGMENT); 76251188Sbostic 76354264Sbostic /* Update the segment usage information. */ 76454264Sbostic LFS_SEGENTRY(sup, fs, sp->seg_number, bp); 76554264Sbostic sup->su_nbytes += nblocks - 1 - 76654264Sbostic (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs) << fs->lfs_bshift; 76754264Sbostic sup->su_nbytes += ssp->ss_ninos * sizeof(struct dinode); 76854264Sbostic sup->su_lastmod = time.tv_sec; 76954264Sbostic LFS_UBWRITE(bp); 77054264Sbostic do_again = !(bp->b_flags & B_GATHERED); 77154264Sbostic 77251860Sbostic i_dev = VTOI(fs->lfs_ivnode)->i_dev; 77353574Sheideman strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)]; 77451301Sbostic 77552688Sbostic /* 77652688Sbostic * When we simply write the blocks we lose a rotation for every block 77752688Sbostic * written. To avoid this problem, we allocate memory in chunks, copy 77852688Sbostic * the buffers into the chunk and write the chunk. 56K was chosen as 77952688Sbostic * some driver/controllers can't handle unsigned 16 bit transfers. 78052688Sbostic * When the data is copied to the chunk, turn off the the B_LOCKED bit 78152688Sbostic * and brelse the buffer (which will move them to the LRU list). Add 78252688Sbostic * the B_CALL flag to the buffer header so we can count I/O's for the 78352688Sbostic * checkpoints and so we can release the allocated memory. 78452688Sbostic * 78552688Sbostic * XXX 78652688Sbostic * This should be removed if the new virtual memory system allows us to 78752688Sbostic * easily make the buffers contiguous in kernel memory and if that's 78852688Sbostic * fast enough. 78952688Sbostic */ 79052688Sbostic #define LFS_CHUNKSIZE (56 * 1024) 79152688Sbostic ch_per_blk = LFS_CHUNKSIZE / fs->lfs_bsize; 79252688Sbostic for (bpp = sp->bpp, i = nblocks; i;) { 79352688Sbostic num = ch_per_blk; 79452688Sbostic if (num > i) 79552688Sbostic num = i; 79652688Sbostic i -= num; 79752688Sbostic size = num * fs->lfs_bsize; 79852688Sbostic 79952688Sbostic cbp = lfs_newbuf(fs, (*bpp)->b_blkno, 0); 80052688Sbostic cbp->b_dev = i_dev; 80152688Sbostic cbp->b_flags = B_ASYNC | B_BUSY | B_CALL; 80252688Sbostic cbp->b_iodone = lfs_callback; 80352688Sbostic cbp->b_saveaddr = cbp->b_un.b_addr; 80452688Sbostic cbp->b_un.b_addr = malloc(size, M_SEGMENT, M_WAITOK); 80552688Sbostic 80652688Sbostic s = splbio(); 80752688Sbostic ++fs->lfs_iocount; 80852688Sbostic for (p = cbp->b_un.b_addr; num--;) { 80952688Sbostic bp = *bpp++; 81052688Sbostic bcopy(bp->b_un.b_addr, p, bp->b_bcount); 81152688Sbostic p += bp->b_bcount; 81254264Sbostic bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI | 81354264Sbostic B_LOCKED | B_GATHERED); 81454264Sbostic if (!(bp->b_flags & (B_NOCACHE | B_INVAL))) { 81552688Sbostic bremfree(bp); 81652688Sbostic reassignbuf(bp, bp->b_vp); 81752688Sbostic } 81852688Sbostic brelse(bp); 81951860Sbostic } 82052688Sbostic splx(s); 82152688Sbostic cbp->b_bcount = p - cbp->b_un.b_addr; 82253574Sheideman vop_strategy_a.a_desc = VDESC(vop_strategy); 82353574Sheideman vop_strategy_a.a_bp = cbp; 82453574Sheideman (strategy)(&vop_strategy_a); 82551860Sbostic } 82654264Sbostic return(do_again); 82751188Sbostic } 82851188Sbostic 82952077Sbostic void 83051860Sbostic lfs_writesuper(fs, sp) 83151499Sbostic struct lfs *fs; 83252085Sbostic struct segment *sp; 83351301Sbostic { 83452085Sbostic struct buf *bp; 83551860Sbostic dev_t i_dev; 83653574Sheideman int (*strategy) __P((struct vop_strategy_args *)); 837*54690Sbostic struct vop_strategy_args vop_strategy_a; 83851301Sbostic 83951860Sbostic #ifdef VERBOSE 84051860Sbostic printf("lfs_writesuper\n"); 84151860Sbostic #endif 84251860Sbostic i_dev = VTOI(fs->lfs_ivnode)->i_dev; 84353574Sheideman strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)]; 84451356Sbostic 84551342Sbostic /* Checksum the superblock and copy it into a buffer. */ 84651499Sbostic fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum)); 84752688Sbostic bp = lfs_newbuf(fs, fs->lfs_sboffs[0], LFS_SBPAD); 84851860Sbostic *bp->b_un.b_lfs = *fs; 84951215Sbostic 85051356Sbostic /* Write the first superblock (wait). */ 85151860Sbostic bp->b_dev = i_dev; 85251915Sbostic bp->b_flags |= B_BUSY; 85351860Sbostic bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI); 85453574Sheideman vop_strategy_a.a_desc = VDESC(vop_strategy); 85553574Sheideman vop_strategy_a.a_bp = bp; 85653574Sheideman (strategy)(&vop_strategy_a); 85751215Sbostic biowait(bp); 85851342Sbostic 85951356Sbostic /* Write the second superblock (don't wait). */ 86051215Sbostic bp->b_blkno = bp->b_lblkno = fs->lfs_sboffs[1]; 86151915Sbostic bp->b_flags |= B_ASYNC | B_BUSY; 86251860Sbostic bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI); 86353574Sheideman (strategy)(&vop_strategy_a); 86451215Sbostic } 86551215Sbostic 86651342Sbostic /* 86751342Sbostic * Logical block number match routines used when traversing the dirty block 86851342Sbostic * chain. 86951342Sbostic */ 87052077Sbostic int 87152077Sbostic lfs_match_data(fs, bp) 87251860Sbostic struct lfs *fs; 87352085Sbostic struct buf *bp; 87451215Sbostic { 87551342Sbostic return (bp->b_lblkno >= 0); 87651215Sbostic } 87751215Sbostic 87852077Sbostic int 87952077Sbostic lfs_match_indir(fs, bp) 88051860Sbostic struct lfs *fs; 88152085Sbostic struct buf *bp; 88251215Sbostic { 88351860Sbostic int lbn; 88451860Sbostic 88551860Sbostic lbn = bp->b_lblkno; 88651860Sbostic return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0); 88751215Sbostic } 88851215Sbostic 88952077Sbostic int 89052077Sbostic lfs_match_dindir(fs, bp) 89151860Sbostic struct lfs *fs; 89252085Sbostic struct buf *bp; 89351215Sbostic { 89451860Sbostic int lbn; 89551860Sbostic 89651860Sbostic lbn = bp->b_lblkno; 89751860Sbostic return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1); 89851215Sbostic } 89951215Sbostic 90052077Sbostic int 90152077Sbostic lfs_match_tindir(fs, bp) 90251499Sbostic struct lfs *fs; 90352085Sbostic struct buf *bp; 90451342Sbostic { 90551860Sbostic int lbn; 90651342Sbostic 90751860Sbostic lbn = bp->b_lblkno; 90851860Sbostic return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2); 90951860Sbostic } 91051342Sbostic 91151860Sbostic /* 91251860Sbostic * Allocate a new buffer header. 91351860Sbostic */ 91452085Sbostic struct buf * 91552688Sbostic lfs_newbuf(fs, daddr, size) 91651860Sbostic struct lfs *fs; 91751860Sbostic daddr_t daddr; 91851860Sbostic size_t size; 91951860Sbostic { 92052085Sbostic struct buf *bp; 92151342Sbostic 92251860Sbostic #ifdef VERBOSE 92351860Sbostic printf("lfs_newbuf\n"); 92451860Sbostic #endif 92551860Sbostic bp = getnewbuf(); 92651860Sbostic bremhash(bp); 92751860Sbostic bgetvp(fs->lfs_ivnode, bp); 92851860Sbostic bp->b_bcount = 0; 92951860Sbostic bp->b_lblkno = daddr; 93051860Sbostic bp->b_blkno = daddr; 93151860Sbostic bp->b_error = 0; 93251860Sbostic bp->b_resid = 0; 93352688Sbostic if (size) 93452688Sbostic allocbuf(bp, size); 93551860Sbostic bp->b_flags |= B_NOCACHE; 93652688Sbostic bp->b_saveaddr = NULL; 93751915Sbostic binshash(bp, &bfreelist[BQ_AGE]); 93851860Sbostic return (bp); 93951860Sbostic } 94051342Sbostic 94153347Sbostic void 94251860Sbostic lfs_callback(bp) 94352085Sbostic struct buf *bp; 94451860Sbostic { 94551860Sbostic struct lfs *fs; 94651342Sbostic 94751860Sbostic fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs; 94851860Sbostic #ifdef DIAGNOSTIC 94951860Sbostic if (fs->lfs_iocount == 0) 95051860Sbostic panic("lfs_callback: zero iocount\n"); 95151860Sbostic #endif 95251860Sbostic if (--fs->lfs_iocount == 0) 95352688Sbostic wakeup(&fs->lfs_iocount); 95451915Sbostic 95552688Sbostic if (bp->b_saveaddr) { 95652688Sbostic free(bp->b_un.b_addr, M_SEGMENT); 95752688Sbostic bp->b_un.b_addr = bp->b_saveaddr; 95852819Sbostic bp->b_saveaddr = NULL; 95952688Sbostic } 96051860Sbostic brelse(bp); 96151860Sbostic } 96251342Sbostic 96351215Sbostic /* 96451188Sbostic * Shellsort (diminishing increment sort) from Data Structures and 96551188Sbostic * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290; 96651188Sbostic * see also Knuth Vol. 3, page 84. The increments are selected from 96751188Sbostic * formula (8), page 95. Roughly O(N^3/2). 96851188Sbostic */ 96951188Sbostic /* 97051188Sbostic * This is our own private copy of shellsort because we want to sort 97151188Sbostic * two parallel arrays (the array of buffer pointers and the array of 97251188Sbostic * logical block numbers) simultaneously. Note that we cast the array 97351188Sbostic * of logical block numbers to a unsigned in this routine so that the 97451188Sbostic * negative block numbers (meta data blocks) sort AFTER the data blocks. 97551188Sbostic */ 97652077Sbostic void 97752077Sbostic lfs_shellsort(bp_array, lb_array, nmemb) 97852085Sbostic struct buf **bp_array; 97951215Sbostic daddr_t *lb_array; 98051188Sbostic register int nmemb; 98151188Sbostic { 98251188Sbostic static int __rsshell_increments[] = { 4, 1, 0 }; 98351188Sbostic register int incr, *incrp, t1, t2; 98452085Sbostic struct buf *bp_temp; 98551188Sbostic u_long lb_temp; 98651188Sbostic 98751188Sbostic for (incrp = __rsshell_increments; incr = *incrp++;) 98851188Sbostic for (t1 = incr; t1 < nmemb; ++t1) 98951188Sbostic for (t2 = t1 - incr; t2 >= 0;) 99051188Sbostic if (lb_array[t2] > lb_array[t2 + incr]) { 99151188Sbostic lb_temp = lb_array[t2]; 99251188Sbostic lb_array[t2] = lb_array[t2 + incr]; 99351188Sbostic lb_array[t2 + incr] = lb_temp; 99451188Sbostic bp_temp = bp_array[t2]; 99551188Sbostic bp_array[t2] = bp_array[t2 + incr]; 99651188Sbostic bp_array[t2 + incr] = bp_temp; 99751188Sbostic t2 -= incr; 99851188Sbostic } else 99951188Sbostic break; 100051188Sbostic } 1001