151188Sbostic /* 251188Sbostic * Copyright (c) 1991 Regents of the University of California. 351188Sbostic * All rights reserved. 451188Sbostic * 551188Sbostic * %sccs.include.redist.c% 651188Sbostic * 7*55033Smckusick * @(#)lfs_segment.c 7.22 (Berkeley) 07/12/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/malloc.h> 2251490Sbostic #include <sys/mount.h> 2351188Sbostic 24*55033Smckusick #include <miscfs/specfs/specdev.h> 25*55033Smckusick #include <miscfs/fifofs/fifo.h> 26*55033Smckusick 2751499Sbostic #include <ufs/ufs/quota.h> 2851499Sbostic #include <ufs/ufs/inode.h> 2951499Sbostic #include <ufs/ufs/dir.h> 3051499Sbostic #include <ufs/ufs/ufsmount.h> 3151490Sbostic 3251499Sbostic #include <ufs/lfs/lfs.h> 3351499Sbostic #include <ufs/lfs/lfs_extern.h> 3451490Sbostic 3551860Sbostic /* In-memory description of a segment about to be written. */ 3651860Sbostic struct segment { 3752085Sbostic struct buf **bpp; /* pointer to buffer array */ 3852085Sbostic struct buf **cbpp; /* pointer to next available bp */ 3952085Sbostic struct buf *ibp; /* buffer pointer to inode page */ 4052085Sbostic struct finfo *fip; /* current fileinfo pointer */ 4151860Sbostic void *segsum; /* segment summary info */ 4251860Sbostic u_long ninodes; /* number of inodes in this segment */ 4351860Sbostic u_long seg_bytes_left; /* bytes left in segment */ 4451860Sbostic u_long sum_bytes_left; /* bytes left in summary block */ 4551860Sbostic u_long seg_number; /* number of this segment */ 4651860Sbostic #define SEGM_CKP 0x01 /* doing a checkpoint */ 4751860Sbostic u_long seg_flags; /* run-time flags for this segment */ 4851860Sbostic }; 4951860Sbostic 5051188Sbostic /* 5151860Sbostic * Determine if it's OK to start a partial in this segment, or if we need 5251860Sbostic * to go on to a new segment. 5351301Sbostic */ 5451860Sbostic #define LFS_PARTIAL_FITS(fs) \ 5551860Sbostic ((fs)->lfs_dbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \ 5651860Sbostic 1 << (fs)->lfs_fsbtodb) 5751188Sbostic 5853347Sbostic void lfs_callback __P((struct buf *)); 5952085Sbostic void lfs_gather __P((struct lfs *, struct segment *, 6052085Sbostic struct vnode *, int (*) __P((struct lfs *, struct buf *)))); 6152085Sbostic void lfs_initseg __P((struct lfs *, struct segment *)); 6252085Sbostic void lfs_iset __P((struct inode *, daddr_t, time_t)); 6352085Sbostic int lfs_match_data __P((struct lfs *, struct buf *)); 6452085Sbostic int lfs_match_dindir __P((struct lfs *, struct buf *)); 6552085Sbostic int lfs_match_indir __P((struct lfs *, struct buf *)); 6652085Sbostic int lfs_match_tindir __P((struct lfs *, struct buf *)); 6752085Sbostic struct buf * 6852688Sbostic lfs_newbuf __P((struct lfs *, daddr_t, size_t)); 6952077Sbostic void lfs_newseg __P((struct lfs *)); 7052085Sbostic void lfs_shellsort __P((struct buf **, daddr_t *, register int)); 7152077Sbostic void lfs_updatemeta __P((struct lfs *, 7252085Sbostic struct segment *, struct vnode *, daddr_t *, struct buf **, int)); 7352085Sbostic void lfs_writefile __P((struct lfs *, struct segment *, struct vnode *)); 7454264Sbostic int lfs_writeinode __P((struct lfs *, struct segment *, struct inode *)); 7554264Sbostic int lfs_writeseg __P((struct lfs *, struct segment *)); 7652085Sbostic void lfs_writesuper __P((struct lfs *, struct segment *)); 7754264Sbostic void lfs_writevnodes __P((struct lfs *fs, struct mount *mp, 7854264Sbostic struct segment *sp, int dirops)); 7951188Sbostic 8051860Sbostic int lfs_allclean_wakeup; /* Cleaner wakeup address. */ 8151860Sbostic 8252328Sbostic /* 8352328Sbostic * Ifile and meta data blocks are not marked busy, so segment writes MUST be 8452328Sbostic * single threaded. Currently, there are two paths into lfs_segwrite, sync() 8552328Sbostic * and getnewbuf(). They both mark the file system busy. Lfs_vflush() 8652328Sbostic * explicitly marks the file system busy. So lfs_segwrite is safe. I think. 8752328Sbostic */ 8852328Sbostic 8951188Sbostic int 9052328Sbostic lfs_vflush(vp) 9152328Sbostic struct vnode *vp; 9252328Sbostic { 9352328Sbostic struct inode *ip; 9452328Sbostic struct lfs *fs; 9552328Sbostic struct segment *sp; 9652328Sbostic int error, s; 9752328Sbostic 9852328Sbostic #ifdef VERBOSE 9952328Sbostic printf("lfs_vflush\n"); 10052328Sbostic #endif 10154690Sbostic fs = VFSTOUFS(vp->v_mount)->um_lfs; 10254690Sbostic lfs_seglock(fs); 10352328Sbostic 10452328Sbostic /* 10552328Sbostic * Allocate a segment structure and enough space to hold pointers to 10652328Sbostic * the maximum possible number of buffers which can be described in a 10752328Sbostic * single summary block. 10852328Sbostic */ 10952328Sbostic sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK); 11052328Sbostic sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) / 11152328Sbostic sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK); 11252328Sbostic sp->seg_flags = SEGM_CKP; 11352328Sbostic lfs_initseg(fs, sp); 11452328Sbostic 11552328Sbostic /* 11652328Sbostic * Keep a cumulative count of the outstanding I/O operations. If the 11752328Sbostic * disk drive catches up with us it could go to zero before we finish, 11852328Sbostic * so we artificially increment it by one until we've scheduled all of 11952328Sbostic * the writes we intend to do. 12052328Sbostic */ 12152328Sbostic s = splbio(); 12252688Sbostic ++fs->lfs_iocount; 12352328Sbostic splx(s); 12452328Sbostic 12552328Sbostic if (vp->v_dirtyblkhd != NULL) 12652328Sbostic lfs_writefile(fs, sp, vp); 12752328Sbostic ip = VTOI(vp); 12854264Sbostic (void) lfs_writeinode(fs, sp, ip); 12952328Sbostic ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG); 13052328Sbostic 13154690Sbostic (void)lfs_writeseg(fs, sp); 13252328Sbostic 13352328Sbostic /* 13452328Sbostic * If the I/O count is non-zero, sleep until it reaches zero. At the 13552328Sbostic * moment, the user's process hangs around so we can sleep. 13652328Sbostic */ 13752328Sbostic s = splbio(); 13852328Sbostic if (--fs->lfs_iocount && (error = 13952995Sbostic tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs vflush", 0))) { 14052995Sbostic free(sp->bpp, M_SEGMENT); 14152995Sbostic free(sp, M_SEGMENT); 14252328Sbostic return (error); 14352995Sbostic } 14452328Sbostic splx(s); 14554690Sbostic lfs_segunlock(fs); 14652328Sbostic 14752995Sbostic /* 14852995Sbostic * XXX 14952995Sbostic * Should be writing a checkpoint? 15052995Sbostic */ 15152328Sbostic free(sp->bpp, M_SEGMENT); 15252328Sbostic free(sp, M_SEGMENT); 15352328Sbostic 15452328Sbostic return (0); 15552328Sbostic } 15652328Sbostic 15754264Sbostic void 15854264Sbostic lfs_writevnodes(fs, mp, sp, dirops) 15954264Sbostic struct lfs *fs; 16054264Sbostic struct mount *mp; 16154264Sbostic struct segment *sp; 16254264Sbostic int dirops; 16354264Sbostic { 16454264Sbostic struct inode *ip; 16554264Sbostic struct vnode *vp; 16654264Sbostic int error, s; 16754264Sbostic 16854264Sbostic loop: for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) { 16954264Sbostic /* 17054264Sbostic * If the vnode that we are about to sync is no longer 17154264Sbostic * associated with this mount point, start over. 17254264Sbostic */ 17354264Sbostic if (vp->v_mount != mp) 17454264Sbostic goto loop; 17554264Sbostic 17654264Sbostic if (dirops && !(vp->v_flag & VDIROP) || 17754264Sbostic !dirops && (vp->v_flag & VDIROP)) 17854264Sbostic continue; 17954264Sbostic /* 18054264Sbostic * XXX 18154264Sbostic * Up the ref count so we don't get tossed out of 18254264Sbostic * memory. 18354264Sbostic */ 18454264Sbostic VREF(vp); 18554264Sbostic 18654264Sbostic /* 18754264Sbostic * Write the inode/file if dirty and it's not the 18854264Sbostic * the IFILE. 18954264Sbostic */ 19054264Sbostic ip = VTOI(vp); 19154264Sbostic if ((ip->i_flag & (IMOD | IACC | IUPD | ICHG) || 19254264Sbostic vp->v_dirtyblkhd != NULL) && 19354264Sbostic ip->i_number != LFS_IFILE_INUM) { 19454264Sbostic if (vp->v_dirtyblkhd != NULL) 19554264Sbostic lfs_writefile(fs, sp, vp); 19654264Sbostic (void) lfs_writeinode(fs, sp, ip); 19754264Sbostic ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG); 19854264Sbostic } 19954264Sbostic vp->v_flag &= ~VDIROP; 20054264Sbostic vrele(vp); 20154264Sbostic } 20254264Sbostic } 20354264Sbostic 20452328Sbostic int 20551215Sbostic lfs_segwrite(mp, do_ckp) 20652085Sbostic struct mount *mp; 20751860Sbostic int do_ckp; /* Do a checkpoint. */ 20851188Sbostic { 20952085Sbostic struct inode *ip; 21051499Sbostic struct lfs *fs; 21152085Sbostic struct segment *sp; 21252085Sbostic struct vnode *vp; 21354264Sbostic int error, s; 21451188Sbostic 21551860Sbostic #ifdef VERBOSE 21651860Sbostic printf("lfs_segwrite\n"); 21751860Sbostic #endif 21852328Sbostic fs = VFSTOUFS(mp)->um_lfs; 21954690Sbostic lfs_seglock(fs); 22052085Sbostic 22151860Sbostic /* 22252328Sbostic * Allocate a segment structure and enough space to hold pointers to 22352328Sbostic * the maximum possible number of buffers which can be described in a 22452328Sbostic * single summary block. 22552328Sbostic */ 22652328Sbostic sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK); 22752328Sbostic sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) / 22852328Sbostic sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK); 22952328Sbostic sp->seg_flags = do_ckp ? SEGM_CKP : 0; 23052328Sbostic lfs_initseg(fs, sp); 23152328Sbostic 23252328Sbostic /* 23352688Sbostic * Keep a cumulative count of the outstanding I/O operations. If the 23452688Sbostic * disk drive catches up with us it could go to zero before we finish, 23552688Sbostic * so we artificially increment it by one until we've scheduled all of 23652688Sbostic * the writes we intend to do. If not a checkpoint, we never do the 23752688Sbostic * final decrement, avoiding the wakeup in the callback routine. 23851860Sbostic */ 23952688Sbostic s = splbio(); 24054264Sbostic fs->lfs_iocount++; 24152688Sbostic splx(s); 24251342Sbostic 24354264Sbostic lfs_writevnodes(fs, mp, sp, 0); 24454264Sbostic s = splbio(); 24554264Sbostic fs->lfs_writer = 1; 24654264Sbostic if (fs->lfs_dirops && (error = 24754264Sbostic tsleep(&fs->lfs_writer, PRIBIO + 1, "lfs writer", 0))) { 24854264Sbostic free(sp->bpp, M_SEGMENT); 24954264Sbostic free(sp, M_SEGMENT); 25054264Sbostic fs->lfs_writer = 0; 25154264Sbostic splx(s); 25254264Sbostic return(error); 25354264Sbostic } 25454264Sbostic splx(s); 25551860Sbostic 25654264Sbostic lfs_writevnodes(fs, mp, sp, 1); 25751860Sbostic 25854264Sbostic /* 25954264Sbostic * If this is a checkpoint, we need to loop on both the ifile and 26054264Sbostic * the writeseg to make sure that we don't end up with any dirty 26154264Sbostic * buffers left when this is all over. 26254264Sbostic */ 26354264Sbostic if (do_ckp || fs->lfs_doifile) { 26454264Sbostic redo: 26554264Sbostic vp = fs->lfs_ivnode; 26654264Sbostic while (vget(vp)); 26752328Sbostic ip = VTOI(vp); 26854264Sbostic do { 26952328Sbostic if (vp->v_dirtyblkhd != NULL) 27052328Sbostic lfs_writefile(fs, sp, vp); 27154264Sbostic } while (lfs_writeinode(fs, sp, ip) && do_ckp); 27252077Sbostic ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG); 27352077Sbostic vput(vp); 27454264Sbostic if (lfs_writeseg(fs, sp) && do_ckp) { 27554264Sbostic lfs_initseg(fs, sp); 27654264Sbostic goto redo; 27754264Sbostic } 27854264Sbostic } else 27954264Sbostic (void) lfs_writeseg(fs, sp); 28051342Sbostic 28151215Sbostic /* 28251860Sbostic * If the I/O count is non-zero, sleep until it reaches zero. At the 28351860Sbostic * moment, the user's process hangs around so we can sleep. 28451215Sbostic */ 28552688Sbostic s = splbio(); 28652688Sbostic --fs->lfs_iocount; 28754264Sbostic fs->lfs_writer = 0; 28854264Sbostic fs->lfs_doifile = 0; 28954264Sbostic wakeup(&fs->lfs_dirops); 29054264Sbostic 29151860Sbostic if (do_ckp) { 29252688Sbostic if (fs->lfs_iocount && (error = 29352995Sbostic tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs sync", 0))) { 29452995Sbostic free(sp->bpp, M_SEGMENT); 29552995Sbostic free(sp, M_SEGMENT); 29651915Sbostic return (error); 29752995Sbostic } 29851860Sbostic splx(s); 29951860Sbostic lfs_writesuper(fs, sp); 30052688Sbostic } else 30152688Sbostic splx(s); 30251215Sbostic 30354690Sbostic lfs_segunlock(fs); 30454690Sbostic 30551927Sbostic free(sp->bpp, M_SEGMENT); 30651927Sbostic free(sp, M_SEGMENT); 30751215Sbostic 30851860Sbostic return (0); 30951188Sbostic } 31051188Sbostic 31151860Sbostic /* 31251860Sbostic * Write the dirty blocks associated with a vnode. 31351860Sbostic */ 31452077Sbostic void 31551860Sbostic lfs_writefile(fs, sp, vp) 31651499Sbostic struct lfs *fs; 31752085Sbostic struct segment *sp; 31852085Sbostic struct vnode *vp; 31951188Sbostic { 32051860Sbostic struct buf *bp; 32152085Sbostic struct finfo *fip; 32251860Sbostic IFILE *ifp; 32351188Sbostic 32451860Sbostic #ifdef VERBOSE 32551860Sbostic printf("lfs_writefile\n"); 32651860Sbostic #endif 32752085Sbostic if (sp->seg_bytes_left < fs->lfs_bsize || 32852085Sbostic sp->sum_bytes_left < sizeof(struct finfo)) { 32954264Sbostic (void) lfs_writeseg(fs, sp); 33052085Sbostic lfs_initseg(fs, sp); 33152085Sbostic } 33252085Sbostic sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(daddr_t); 33351215Sbostic 33452085Sbostic fip = sp->fip; 33552085Sbostic fip->fi_nblocks = 0; 33652085Sbostic fip->fi_ino = VTOI(vp)->i_number; 33752085Sbostic LFS_IENTRY(ifp, fs, fip->fi_ino, bp); 33852085Sbostic fip->fi_version = ifp->if_version; 33952085Sbostic brelse(bp); 34051188Sbostic 34152085Sbostic /* 34252085Sbostic * It may not be necessary to write the meta-data blocks at this point, 34352085Sbostic * as the roll-forward recovery code should be able to reconstruct the 34452085Sbostic * list. 34552085Sbostic */ 34652085Sbostic lfs_gather(fs, sp, vp, lfs_match_data); 34752085Sbostic lfs_gather(fs, sp, vp, lfs_match_indir); 34852085Sbostic lfs_gather(fs, sp, vp, lfs_match_dindir); 34951860Sbostic #ifdef TRIPLE 35052085Sbostic lfs_gather(fs, sp, vp, lfs_match_tindir); 35151860Sbostic #endif 35251342Sbostic 35352085Sbostic fip = sp->fip; 35451860Sbostic #ifdef META 35552085Sbostic printf("lfs_writefile: adding %d blocks\n", fip->fi_nblocks); 35651860Sbostic #endif 35752085Sbostic if (fip->fi_nblocks != 0) { 35852085Sbostic ++((SEGSUM *)(sp->segsum))->ss_nfinfo; 35952085Sbostic sp->fip = 36052085Sbostic (struct finfo *)((caddr_t)fip + sizeof(struct finfo) + 36152085Sbostic sizeof(daddr_t) * (fip->fi_nblocks - 1)); 36252682Sstaelin } else 36352682Sstaelin sp->sum_bytes_left += sizeof(struct finfo) - sizeof(daddr_t); 36451215Sbostic } 36551215Sbostic 36654264Sbostic int 36751915Sbostic lfs_writeinode(fs, sp, ip) 36851915Sbostic struct lfs *fs; 36952085Sbostic struct segment *sp; 37052085Sbostic struct inode *ip; 37151915Sbostic { 37252085Sbostic struct buf *bp, *ibp; 37352077Sbostic IFILE *ifp; 37452682Sstaelin SEGUSE *sup; 37552682Sstaelin daddr_t daddr; 37652077Sbostic ino_t ino; 37751915Sbostic int ndx; 37854264Sbostic int redo_ifile = 0; 37951915Sbostic 38051915Sbostic #ifdef VERBOSE 38151915Sbostic printf("lfs_writeinode\n"); 38251915Sbostic #endif 38351915Sbostic /* Allocate a new inode block if necessary. */ 38451915Sbostic if (sp->ibp == NULL) { 38551915Sbostic /* Allocate a new segment if necessary. */ 38651915Sbostic if (sp->seg_bytes_left < fs->lfs_bsize || 38751915Sbostic sp->sum_bytes_left < sizeof(daddr_t)) { 38854264Sbostic (void) lfs_writeseg(fs, sp); 38951915Sbostic lfs_initseg(fs, sp); 39051915Sbostic } 39151915Sbostic 39251915Sbostic /* Get next inode block. */ 39352682Sstaelin daddr = fs->lfs_offset; 39451915Sbostic fs->lfs_offset += fsbtodb(fs, 1); 39551915Sbostic sp->ibp = *sp->cbpp++ = 39652688Sbostic lfs_newbuf(fs, daddr, fs->lfs_bsize); 39751915Sbostic 39852688Sbostic /* Set remaining space counters. */ 39951915Sbostic sp->seg_bytes_left -= fs->lfs_bsize; 40051915Sbostic sp->sum_bytes_left -= sizeof(daddr_t); 40152077Sbostic ndx = LFS_SUMMARY_SIZE / sizeof(daddr_t) - 40251915Sbostic sp->ninodes / INOPB(fs) - 1; 40352682Sstaelin ((daddr_t *)(sp->segsum))[ndx] = daddr; 40451915Sbostic } 40551915Sbostic 40652085Sbostic /* Update the inode times and copy the inode onto the inode page. */ 40752077Sbostic ITIMES(ip, &time, &time); 40851915Sbostic bp = sp->ibp; 40952085Sbostic bp->b_un.b_dino[sp->ninodes % INOPB(fs)] = ip->i_din; 41051915Sbostic 41151915Sbostic /* Increment inode count in segment summary block. */ 41251915Sbostic ++((SEGSUM *)(sp->segsum))->ss_ninos; 41351915Sbostic 41451915Sbostic /* If this page is full, set flag to allocate a new page. */ 41551915Sbostic if (++sp->ninodes % INOPB(fs) == 0) 41651915Sbostic sp->ibp = NULL; 41751915Sbostic 41851915Sbostic /* 41952077Sbostic * If updating the ifile, update the super-block. Update the disk 42052077Sbostic * address and access times for this inode in the ifile. 42151915Sbostic */ 42252077Sbostic ino = ip->i_number; 42352077Sbostic if (ino == LFS_IFILE_INUM) 42451915Sbostic fs->lfs_idaddr = bp->b_blkno; 42552077Sbostic 42652077Sbostic LFS_IENTRY(ifp, fs, ino, ibp); 42752682Sstaelin daddr = ifp->if_daddr; 42852077Sbostic ifp->if_daddr = bp->b_blkno; 42952085Sbostic LFS_UBWRITE(ibp); 43054264Sbostic redo_ifile = (ino == LFS_IFILE_INUM && !(ibp->b_flags & B_GATHERED)); 43152682Sstaelin 43254264Sbostic /* 43354264Sbostic * No need to update segment usage if there was no former inode address 43454264Sbostic * or if the last inode address is in the current partial segment. 43554264Sbostic */ 43654264Sbostic if (daddr != LFS_UNUSED_DADDR && 43754264Sbostic !(daddr >= fs->lfs_curseg && daddr <= ifp->if_daddr) ) { 43852682Sstaelin LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp); 43952682Sstaelin #ifdef DIAGNOSTIC 44054264Sbostic if (sup->su_nbytes < sizeof(struct dinode)) { 44152819Sbostic /* XXX -- Change to a panic. */ 44252819Sbostic printf("lfs: negative bytes (segment %d)\n", 44352682Sstaelin datosn(fs, daddr)); 44454264Sbostic panic("negative bytes"); 44554264Sbostic } 44652682Sstaelin #endif 44752682Sstaelin sup->su_nbytes -= sizeof(struct dinode); 44852682Sstaelin LFS_UBWRITE(bp); 44954264Sbostic redo_ifile |= 45054264Sbostic (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED)); 45152682Sstaelin } 45254264Sbostic return(redo_ifile); 45351915Sbostic } 45451915Sbostic 45552077Sbostic void 45651215Sbostic lfs_gather(fs, sp, vp, match) 45751499Sbostic struct lfs *fs; 45852085Sbostic struct segment *sp; 45952085Sbostic struct vnode *vp; 46052085Sbostic int (*match) __P((struct lfs *, struct buf *)); 46151215Sbostic { 46254264Sbostic struct buf **bpp, *bp; 46354264Sbostic struct buf *lastbp; 46452085Sbostic struct finfo *fip; 46552085Sbostic struct inode *ip; 46651215Sbostic daddr_t *lbp, *start_lbp; 46751342Sbostic u_long version; 46851342Sbostic int s; 46951215Sbostic 47051860Sbostic #ifdef VERBOSE 47151860Sbostic printf("lfs_gather\n"); 47251860Sbostic #endif 47351215Sbostic ip = VTOI(vp); 47451215Sbostic bpp = sp->cbpp; 47551215Sbostic fip = sp->fip; 47651215Sbostic start_lbp = lbp = &fip->fi_blocks[fip->fi_nblocks]; 47751215Sbostic 47853145Sstaelin loop: s = splbio(); 47954264Sbostic lastbp = NULL; 48054264Sbostic for (bp = vp->v_dirtyblkhd; bp; lastbp = bp, bp = bp->b_blockf) { 48154264Sbostic if (bp->b_flags & B_BUSY || !match(fs, bp) || 48254264Sbostic bp->b_flags & B_GATHERED) 48351215Sbostic continue; 48451342Sbostic #ifdef DIAGNOSTIC 48551860Sbostic if (!(bp->b_flags & B_DELWRI)) 48651915Sbostic panic("lfs_gather: bp not B_DELWRI"); 48751860Sbostic if (!(bp->b_flags & B_LOCKED)) 48851915Sbostic panic("lfs_gather: bp not B_LOCKED"); 48951342Sbostic #endif 49051860Sbostic /* 49151860Sbostic * If full, finish this segment. We may be doing I/O, so 49251860Sbostic * release and reacquire the splbio(). 49351860Sbostic */ 49451342Sbostic if (sp->sum_bytes_left < sizeof(daddr_t) || 49551215Sbostic sp->seg_bytes_left < fs->lfs_bsize) { 49651215Sbostic splx(s); 49751342Sbostic lfs_updatemeta(fs, 49851860Sbostic sp, vp, start_lbp, bpp, lbp - start_lbp); 49951215Sbostic 50051342Sbostic /* Add the current file to the segment summary. */ 50151342Sbostic ++((SEGSUM *)(sp->segsum))->ss_nfinfo; 50251215Sbostic 50351342Sbostic version = fip->fi_version; 50454264Sbostic (void) lfs_writeseg(fs, sp); 50551915Sbostic lfs_initseg(fs, sp); 50651342Sbostic 50751215Sbostic fip = sp->fip; 50851342Sbostic fip->fi_version = version; 50951215Sbostic fip->fi_ino = ip->i_number; 51051342Sbostic start_lbp = lbp = fip->fi_blocks; 51151342Sbostic 51252682Sstaelin sp->sum_bytes_left -= 51352682Sstaelin sizeof(struct finfo) - sizeof(daddr_t); 51452682Sstaelin 51551215Sbostic bpp = sp->cbpp; 51653145Sstaelin goto loop; 51751215Sbostic } 51852682Sstaelin 51952682Sstaelin /* Insert into the buffer list, update the FINFO block. */ 52054264Sbostic bp->b_flags |= B_GATHERED; 52152682Sstaelin *sp->cbpp++ = bp; 52252682Sstaelin ++fip->fi_nblocks; 52352682Sstaelin *lbp++ = bp->b_lblkno; 52452682Sstaelin 52552682Sstaelin sp->sum_bytes_left -= sizeof(daddr_t); 52652682Sstaelin sp->seg_bytes_left -= bp->b_bufsize; 52751188Sbostic } 52851215Sbostic splx(s); 52951860Sbostic lfs_updatemeta(fs, sp, vp, start_lbp, bpp, lbp - start_lbp); 53051188Sbostic } 53151188Sbostic 53251342Sbostic /* 53351342Sbostic * Update the metadata that points to the blocks listed in the FINFO 53451188Sbostic * array. 53551188Sbostic */ 53652077Sbostic void 53751860Sbostic lfs_updatemeta(fs, sp, vp, lbp, bpp, nblocks) 53851499Sbostic struct lfs *fs; 53952085Sbostic struct segment *sp; 54052085Sbostic struct vnode *vp; 54151215Sbostic daddr_t *lbp; 54252085Sbostic struct buf **bpp; 54351215Sbostic int nblocks; 54451188Sbostic { 54551915Sbostic SEGUSE *sup; 54652085Sbostic struct buf *bp; 54751860Sbostic INDIR a[NIADDR], *ap; 54852085Sbostic struct inode *ip; 54951915Sbostic daddr_t daddr, lbn, off; 55051860Sbostic int db_per_fsb, error, i, num; 55151188Sbostic 55251860Sbostic #ifdef VERBOSE 55351860Sbostic printf("lfs_updatemeta\n"); 55451860Sbostic #endif 55551342Sbostic if (nblocks == 0) 55651215Sbostic return; 55751215Sbostic 55851915Sbostic /* Sort the blocks. */ 55952077Sbostic lfs_shellsort(bpp, lbp, nblocks); 56051215Sbostic 56151915Sbostic /* 56251915Sbostic * Assign disk addresses, and update references to the logical 56351915Sbostic * block and the segment usage information. 56451915Sbostic */ 56551860Sbostic db_per_fsb = fsbtodb(fs, 1); 56651915Sbostic for (i = nblocks; i--; ++bpp) { 56751915Sbostic lbn = *lbp++; 56851915Sbostic (*bpp)->b_blkno = off = fs->lfs_offset; 56951860Sbostic fs->lfs_offset += db_per_fsb; 57051215Sbostic 57151860Sbostic if (error = lfs_bmaparray(vp, lbn, &daddr, a, &num)) 57252085Sbostic panic("lfs_updatemeta: lfs_bmaparray %d", error); 57351860Sbostic ip = VTOI(vp); 57451860Sbostic switch (num) { 57551860Sbostic case 0: 57651915Sbostic ip->i_db[lbn] = off; 57751860Sbostic break; 57851860Sbostic case 1: 57951915Sbostic ip->i_ib[a[0].in_off] = off; 58051860Sbostic break; 58151860Sbostic default: 58251860Sbostic ap = &a[num - 1]; 58351860Sbostic if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 58451860Sbostic panic("lfs_updatemeta: bread bno %d", 58551860Sbostic ap->in_lbn); 58651915Sbostic bp->b_un.b_daddr[ap->in_off] = off; 58753530Sheideman VOP_BWRITE(bp); 58851188Sbostic } 58951915Sbostic 59051915Sbostic /* Update segment usage information. */ 59151915Sbostic if (daddr != UNASSIGNED) { 59251915Sbostic LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp); 59351915Sbostic #ifdef DIAGNOSTIC 59454264Sbostic if (sup->su_nbytes < fs->lfs_bsize) { 59552819Sbostic /* XXX -- Change to a panic. */ 59652819Sbostic printf("lfs: negative bytes (segment %d)\n", 59751915Sbostic datosn(fs, daddr)); 59854264Sbostic panic ("Negative Bytes"); 59954264Sbostic } 60051915Sbostic #endif 60151915Sbostic sup->su_nbytes -= fs->lfs_bsize; 60252085Sbostic LFS_UBWRITE(bp); 60351915Sbostic } 60451188Sbostic } 60551188Sbostic } 60651188Sbostic 60751915Sbostic /* 60851915Sbostic * Start a new segment. 60951915Sbostic */ 61052077Sbostic void 61151915Sbostic lfs_initseg(fs, sp) 61251499Sbostic struct lfs *fs; 61352085Sbostic struct segment *sp; 61451188Sbostic { 61551915Sbostic SEGUSE *sup; 61651915Sbostic SEGSUM *ssp; 61751915Sbostic struct buf *bp; 61851915Sbostic daddr_t lbn, *lbnp; 61951215Sbostic 62051860Sbostic #ifdef VERBOSE 62151915Sbostic printf("lfs_initseg\n"); 62251860Sbostic #endif 62351915Sbostic /* Advance to the next segment. */ 62451927Sbostic if (!LFS_PARTIAL_FITS(fs)) { 62552682Sstaelin /* Wake up any cleaning procs waiting on this file system. */ 62652688Sbostic wakeup(&fs->lfs_nextseg); 62752688Sbostic wakeup(&lfs_allclean_wakeup); 62852682Sstaelin 62951927Sbostic lfs_newseg(fs); 63051927Sbostic fs->lfs_offset = fs->lfs_curseg; 63151915Sbostic sp->seg_number = datosn(fs, fs->lfs_curseg); 63251915Sbostic sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE; 63351915Sbostic 63451915Sbostic /* 63551927Sbostic * If the segment contains a superblock, update the offset 63651927Sbostic * and summary address to skip over it. 63751915Sbostic */ 63852077Sbostic LFS_SEGENTRY(sup, fs, sp->seg_number, bp); 63951927Sbostic if (sup->su_flags & SEGUSE_SUPERBLOCK) { 64051915Sbostic fs->lfs_offset += LFS_SBPAD / DEV_BSIZE; 64151915Sbostic sp->seg_bytes_left -= LFS_SBPAD; 64251215Sbostic } 64352085Sbostic brelse(bp); 64451915Sbostic } else { 64551915Sbostic sp->seg_number = datosn(fs, fs->lfs_curseg); 64651915Sbostic sp->seg_bytes_left = (fs->lfs_dbpseg - 64751915Sbostic (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE; 64851915Sbostic } 64954264Sbostic fs->lfs_lastpseg = fs->lfs_offset; 65051342Sbostic 65151915Sbostic sp->ibp = NULL; 65251915Sbostic sp->ninodes = 0; 65351342Sbostic 65451915Sbostic /* Get a new buffer for SEGSUM and enter it into the buffer list. */ 65551915Sbostic sp->cbpp = sp->bpp; 65652688Sbostic *sp->cbpp = lfs_newbuf(fs, fs->lfs_offset, LFS_SUMMARY_SIZE); 65751915Sbostic sp->segsum = (*sp->cbpp)->b_un.b_addr; 65851915Sbostic ++sp->cbpp; 65951915Sbostic fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE; 66051342Sbostic 66151915Sbostic /* Set point to SEGSUM, initialize it. */ 66251915Sbostic ssp = sp->segsum; 66351915Sbostic ssp->ss_next = fs->lfs_nextseg; 66451915Sbostic ssp->ss_nfinfo = ssp->ss_ninos = 0; 66551342Sbostic 66651915Sbostic /* Set pointer to first FINFO, initialize it. */ 66752085Sbostic sp->fip = (struct finfo *)(sp->segsum + sizeof(SEGSUM)); 66851915Sbostic sp->fip->fi_nblocks = 0; 66951342Sbostic 67051915Sbostic sp->seg_bytes_left -= LFS_SUMMARY_SIZE; 67151915Sbostic sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM); 67251915Sbostic } 67351342Sbostic 67451915Sbostic /* 67551915Sbostic * Return the next segment to write. 67651915Sbostic */ 67752077Sbostic void 67851915Sbostic lfs_newseg(fs) 67951915Sbostic struct lfs *fs; 68051915Sbostic { 68151927Sbostic CLEANERINFO *cip; 68251915Sbostic SEGUSE *sup; 68351915Sbostic struct buf *bp; 68451927Sbostic int curseg, isdirty, sn; 68551915Sbostic 68651915Sbostic #ifdef VERBOSE 68751915Sbostic printf("lfs_newseg\n"); 68851915Sbostic #endif 68951927Sbostic /* 69051927Sbostic * Turn off the active bit for the current segment, turn on the 69151927Sbostic * active and dirty bits for the next segment, update the cleaner 69251927Sbostic * info. Set the current segment to the next segment, get a new 69351927Sbostic * next segment. 69451927Sbostic */ 69551927Sbostic LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_curseg), bp); 69651927Sbostic sup->su_flags &= ~SEGUSE_ACTIVE; 69752085Sbostic LFS_UBWRITE(bp); 69851927Sbostic 69951927Sbostic LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp); 70054264Sbostic sup->su_flags |= SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_LIVELOG; 70152085Sbostic LFS_UBWRITE(bp); 70251927Sbostic 70351927Sbostic LFS_CLEANERINFO(cip, fs, bp); 70451927Sbostic --cip->clean; 70551927Sbostic ++cip->dirty; 70652085Sbostic LFS_UBWRITE(bp); 70751927Sbostic 70851927Sbostic fs->lfs_lastseg = fs->lfs_curseg; 70951927Sbostic fs->lfs_curseg = fs->lfs_nextseg; 71051927Sbostic for (sn = curseg = datosn(fs, fs->lfs_curseg);;) { 71151915Sbostic sn = (sn + 1) % fs->lfs_nseg; 71251927Sbostic if (sn == curseg) 71351915Sbostic panic("lfs_nextseg: no clean segments"); 71451915Sbostic LFS_SEGENTRY(sup, fs, sn, bp); 71551915Sbostic isdirty = sup->su_flags & SEGUSE_DIRTY; 71652085Sbostic brelse(bp); 71751915Sbostic if (!isdirty) 71851915Sbostic break; 71951915Sbostic } 72051927Sbostic fs->lfs_nextseg = sntoda(fs, sn); 72151188Sbostic } 72251188Sbostic 72354264Sbostic int 72451188Sbostic lfs_writeseg(fs, sp) 72551499Sbostic struct lfs *fs; 72652085Sbostic struct segment *sp; 72751188Sbostic { 72852688Sbostic struct buf **bpp, *bp, *cbp; 72951188Sbostic SEGUSE *sup; 73052085Sbostic SEGSUM *ssp; 73151860Sbostic dev_t i_dev; 73254264Sbostic size_t size; 73351860Sbostic u_long *datap, *dp; 73454264Sbostic int ch_per_blk, do_again, i, nblocks, num, s; 73554264Sbostic int (*strategy)__P((struct vop_strategy_args *)); 73654690Sbostic struct vop_strategy_args vop_strategy_a; 73752688Sbostic char *p; 73851188Sbostic 73951860Sbostic #ifdef VERBOSE 74051860Sbostic printf("lfs_writeseg\n"); 74151860Sbostic #endif 74254264Sbostic /* Checkpoint always writes superblock, even if no data blocks. */ 74354264Sbostic if ((nblocks = sp->cbpp - sp->bpp) == 0 && !(sp->seg_flags & SEGM_CKP)) 74452085Sbostic return; 74552085Sbostic 74651188Sbostic /* 74752085Sbostic * Compute checksum across data and then across summary; the first 74852085Sbostic * block (the summary block) is skipped. Set the create time here 74952085Sbostic * so that it's guaranteed to be later than the inode mod times. 75051860Sbostic * 75151860Sbostic * XXX 75251860Sbostic * Fix this to do it inline, instead of malloc/copy. 75351188Sbostic */ 75451860Sbostic datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK); 75551915Sbostic for (bpp = sp->bpp, i = nblocks - 1; i--;) 75651915Sbostic *dp++ = (*++bpp)->b_un.b_words[0]; 75752085Sbostic ssp = (SEGSUM *)sp->segsum; 75852103Sbostic ssp->ss_create = time.tv_sec; 75952085Sbostic ssp->ss_datasum = cksum(datap, nblocks * sizeof(u_long)); 76052085Sbostic ssp->ss_sumsum = 76152085Sbostic cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum)); 76251927Sbostic free(datap, M_SEGMENT); 76351188Sbostic 76454264Sbostic /* Update the segment usage information. */ 76554264Sbostic LFS_SEGENTRY(sup, fs, sp->seg_number, bp); 76654264Sbostic sup->su_nbytes += nblocks - 1 - 76754264Sbostic (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs) << fs->lfs_bshift; 76854264Sbostic sup->su_nbytes += ssp->ss_ninos * sizeof(struct dinode); 76954264Sbostic sup->su_lastmod = time.tv_sec; 77054264Sbostic LFS_UBWRITE(bp); 77154264Sbostic do_again = !(bp->b_flags & B_GATHERED); 77254264Sbostic 77351860Sbostic i_dev = VTOI(fs->lfs_ivnode)->i_dev; 77453574Sheideman strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)]; 77551301Sbostic 77652688Sbostic /* 77752688Sbostic * When we simply write the blocks we lose a rotation for every block 77852688Sbostic * written. To avoid this problem, we allocate memory in chunks, copy 77952688Sbostic * the buffers into the chunk and write the chunk. 56K was chosen as 78052688Sbostic * some driver/controllers can't handle unsigned 16 bit transfers. 78152688Sbostic * When the data is copied to the chunk, turn off the the B_LOCKED bit 78252688Sbostic * and brelse the buffer (which will move them to the LRU list). Add 78352688Sbostic * the B_CALL flag to the buffer header so we can count I/O's for the 78452688Sbostic * checkpoints and so we can release the allocated memory. 78552688Sbostic * 78652688Sbostic * XXX 78752688Sbostic * This should be removed if the new virtual memory system allows us to 78852688Sbostic * easily make the buffers contiguous in kernel memory and if that's 78952688Sbostic * fast enough. 79052688Sbostic */ 79152688Sbostic #define LFS_CHUNKSIZE (56 * 1024) 79252688Sbostic ch_per_blk = LFS_CHUNKSIZE / fs->lfs_bsize; 79352688Sbostic for (bpp = sp->bpp, i = nblocks; i;) { 79452688Sbostic num = ch_per_blk; 79552688Sbostic if (num > i) 79652688Sbostic num = i; 79752688Sbostic i -= num; 79852688Sbostic size = num * fs->lfs_bsize; 79952688Sbostic 80052688Sbostic cbp = lfs_newbuf(fs, (*bpp)->b_blkno, 0); 80152688Sbostic cbp->b_dev = i_dev; 80252688Sbostic cbp->b_flags = B_ASYNC | B_BUSY | B_CALL; 80352688Sbostic cbp->b_iodone = lfs_callback; 80452688Sbostic cbp->b_saveaddr = cbp->b_un.b_addr; 80552688Sbostic cbp->b_un.b_addr = malloc(size, M_SEGMENT, M_WAITOK); 80652688Sbostic 80752688Sbostic s = splbio(); 80852688Sbostic ++fs->lfs_iocount; 80952688Sbostic for (p = cbp->b_un.b_addr; num--;) { 81052688Sbostic bp = *bpp++; 81152688Sbostic bcopy(bp->b_un.b_addr, p, bp->b_bcount); 81252688Sbostic p += bp->b_bcount; 81354264Sbostic bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI | 81454264Sbostic B_LOCKED | B_GATHERED); 81554264Sbostic if (!(bp->b_flags & (B_NOCACHE | B_INVAL))) { 81652688Sbostic bremfree(bp); 81752688Sbostic reassignbuf(bp, bp->b_vp); 81852688Sbostic } 81952688Sbostic brelse(bp); 82051860Sbostic } 82152688Sbostic splx(s); 82252688Sbostic cbp->b_bcount = p - cbp->b_un.b_addr; 82353574Sheideman vop_strategy_a.a_desc = VDESC(vop_strategy); 82453574Sheideman vop_strategy_a.a_bp = cbp; 82553574Sheideman (strategy)(&vop_strategy_a); 82651860Sbostic } 82754264Sbostic return(do_again); 82851188Sbostic } 82951188Sbostic 83052077Sbostic void 83151860Sbostic lfs_writesuper(fs, sp) 83251499Sbostic struct lfs *fs; 83352085Sbostic struct segment *sp; 83451301Sbostic { 83552085Sbostic struct buf *bp; 83651860Sbostic dev_t i_dev; 83753574Sheideman int (*strategy) __P((struct vop_strategy_args *)); 83854690Sbostic struct vop_strategy_args vop_strategy_a; 83951301Sbostic 84051860Sbostic #ifdef VERBOSE 84151860Sbostic printf("lfs_writesuper\n"); 84251860Sbostic #endif 84351860Sbostic i_dev = VTOI(fs->lfs_ivnode)->i_dev; 84453574Sheideman strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)]; 84551356Sbostic 84651342Sbostic /* Checksum the superblock and copy it into a buffer. */ 84751499Sbostic fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum)); 84852688Sbostic bp = lfs_newbuf(fs, fs->lfs_sboffs[0], LFS_SBPAD); 84951860Sbostic *bp->b_un.b_lfs = *fs; 85051215Sbostic 85151356Sbostic /* Write the first superblock (wait). */ 85251860Sbostic bp->b_dev = i_dev; 85351915Sbostic bp->b_flags |= B_BUSY; 85451860Sbostic bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI); 85553574Sheideman vop_strategy_a.a_desc = VDESC(vop_strategy); 85653574Sheideman vop_strategy_a.a_bp = bp; 85753574Sheideman (strategy)(&vop_strategy_a); 85851215Sbostic biowait(bp); 85951342Sbostic 86051356Sbostic /* Write the second superblock (don't wait). */ 86151215Sbostic bp->b_blkno = bp->b_lblkno = fs->lfs_sboffs[1]; 86251915Sbostic bp->b_flags |= B_ASYNC | B_BUSY; 86351860Sbostic bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI); 86453574Sheideman (strategy)(&vop_strategy_a); 86551215Sbostic } 86651215Sbostic 86751342Sbostic /* 86851342Sbostic * Logical block number match routines used when traversing the dirty block 86951342Sbostic * chain. 87051342Sbostic */ 87152077Sbostic int 87252077Sbostic lfs_match_data(fs, bp) 87351860Sbostic struct lfs *fs; 87452085Sbostic struct buf *bp; 87551215Sbostic { 87651342Sbostic return (bp->b_lblkno >= 0); 87751215Sbostic } 87851215Sbostic 87952077Sbostic int 88052077Sbostic lfs_match_indir(fs, bp) 88151860Sbostic struct lfs *fs; 88252085Sbostic struct buf *bp; 88351215Sbostic { 88451860Sbostic int lbn; 88551860Sbostic 88651860Sbostic lbn = bp->b_lblkno; 88751860Sbostic return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0); 88851215Sbostic } 88951215Sbostic 89052077Sbostic int 89152077Sbostic lfs_match_dindir(fs, bp) 89251860Sbostic struct lfs *fs; 89352085Sbostic struct buf *bp; 89451215Sbostic { 89551860Sbostic int lbn; 89651860Sbostic 89751860Sbostic lbn = bp->b_lblkno; 89851860Sbostic return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1); 89951215Sbostic } 90051215Sbostic 90152077Sbostic int 90252077Sbostic lfs_match_tindir(fs, bp) 90351499Sbostic struct lfs *fs; 90452085Sbostic struct buf *bp; 90551342Sbostic { 90651860Sbostic int lbn; 90751342Sbostic 90851860Sbostic lbn = bp->b_lblkno; 90951860Sbostic return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2); 91051860Sbostic } 91151342Sbostic 91251860Sbostic /* 91351860Sbostic * Allocate a new buffer header. 91451860Sbostic */ 91552085Sbostic struct buf * 91652688Sbostic lfs_newbuf(fs, daddr, size) 91751860Sbostic struct lfs *fs; 91851860Sbostic daddr_t daddr; 91951860Sbostic size_t size; 92051860Sbostic { 92152085Sbostic struct buf *bp; 92251342Sbostic 92351860Sbostic #ifdef VERBOSE 92451860Sbostic printf("lfs_newbuf\n"); 92551860Sbostic #endif 92651860Sbostic bp = getnewbuf(); 92751860Sbostic bremhash(bp); 92851860Sbostic bgetvp(fs->lfs_ivnode, bp); 92951860Sbostic bp->b_bcount = 0; 93051860Sbostic bp->b_lblkno = daddr; 93151860Sbostic bp->b_blkno = daddr; 93251860Sbostic bp->b_error = 0; 93351860Sbostic bp->b_resid = 0; 93452688Sbostic if (size) 93552688Sbostic allocbuf(bp, size); 93651860Sbostic bp->b_flags |= B_NOCACHE; 93752688Sbostic bp->b_saveaddr = NULL; 93851915Sbostic binshash(bp, &bfreelist[BQ_AGE]); 93951860Sbostic return (bp); 94051860Sbostic } 94151342Sbostic 94253347Sbostic void 94351860Sbostic lfs_callback(bp) 94452085Sbostic struct buf *bp; 94551860Sbostic { 94651860Sbostic struct lfs *fs; 94751342Sbostic 94851860Sbostic fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs; 94951860Sbostic #ifdef DIAGNOSTIC 95051860Sbostic if (fs->lfs_iocount == 0) 95151860Sbostic panic("lfs_callback: zero iocount\n"); 95251860Sbostic #endif 95351860Sbostic if (--fs->lfs_iocount == 0) 95452688Sbostic wakeup(&fs->lfs_iocount); 95551915Sbostic 95652688Sbostic if (bp->b_saveaddr) { 95752688Sbostic free(bp->b_un.b_addr, M_SEGMENT); 95852688Sbostic bp->b_un.b_addr = bp->b_saveaddr; 95952819Sbostic bp->b_saveaddr = NULL; 96052688Sbostic } 96151860Sbostic brelse(bp); 96251860Sbostic } 96351342Sbostic 96451215Sbostic /* 96551188Sbostic * Shellsort (diminishing increment sort) from Data Structures and 96651188Sbostic * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290; 96751188Sbostic * see also Knuth Vol. 3, page 84. The increments are selected from 96851188Sbostic * formula (8), page 95. Roughly O(N^3/2). 96951188Sbostic */ 97051188Sbostic /* 97151188Sbostic * This is our own private copy of shellsort because we want to sort 97251188Sbostic * two parallel arrays (the array of buffer pointers and the array of 97351188Sbostic * logical block numbers) simultaneously. Note that we cast the array 97451188Sbostic * of logical block numbers to a unsigned in this routine so that the 97551188Sbostic * negative block numbers (meta data blocks) sort AFTER the data blocks. 97651188Sbostic */ 97752077Sbostic void 97852077Sbostic lfs_shellsort(bp_array, lb_array, nmemb) 97952085Sbostic struct buf **bp_array; 98051215Sbostic daddr_t *lb_array; 98151188Sbostic register int nmemb; 98251188Sbostic { 98351188Sbostic static int __rsshell_increments[] = { 4, 1, 0 }; 98451188Sbostic register int incr, *incrp, t1, t2; 98552085Sbostic struct buf *bp_temp; 98651188Sbostic u_long lb_temp; 98751188Sbostic 98851188Sbostic for (incrp = __rsshell_increments; incr = *incrp++;) 98951188Sbostic for (t1 = incr; t1 < nmemb; ++t1) 99051188Sbostic for (t2 = t1 - incr; t2 >= 0;) 99151188Sbostic if (lb_array[t2] > lb_array[t2 + incr]) { 99251188Sbostic lb_temp = lb_array[t2]; 99351188Sbostic lb_array[t2] = lb_array[t2 + incr]; 99451188Sbostic lb_array[t2 + incr] = lb_temp; 99551188Sbostic bp_temp = bp_array[t2]; 99651188Sbostic bp_array[t2] = bp_array[t2 + incr]; 99751188Sbostic bp_array[t2 + incr] = bp_temp; 99851188Sbostic t2 -= incr; 99951188Sbostic } else 100051188Sbostic break; 100151188Sbostic } 1002