151188Sbostic /* 251188Sbostic * Copyright (c) 1991 Regents of the University of California. 351188Sbostic * All rights reserved. 451188Sbostic * 551188Sbostic * %sccs.include.redist.c% 651188Sbostic * 7*52819Sbostic * @(#)lfs_segment.c 7.14 (Berkeley) 03/03/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 5752085Sbostic int 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 *)); 7352085Sbostic void lfs_writeinode __P((struct lfs *, struct segment *, struct inode *)); 7452085Sbostic void lfs_writeseg __P((struct lfs *, struct segment *)); 7552085Sbostic void lfs_writesuper __P((struct lfs *, struct segment *)); 7651188Sbostic 7751860Sbostic int lfs_allclean_wakeup; /* Cleaner wakeup address. */ 7851860Sbostic 7952328Sbostic /* 8052328Sbostic * Ifile and meta data blocks are not marked busy, so segment writes MUST be 8152328Sbostic * single threaded. Currently, there are two paths into lfs_segwrite, sync() 8252328Sbostic * and getnewbuf(). They both mark the file system busy. Lfs_vflush() 8352328Sbostic * explicitly marks the file system busy. So lfs_segwrite is safe. I think. 8452328Sbostic */ 8552328Sbostic 8651188Sbostic int 8752328Sbostic lfs_vflush(vp) 8852328Sbostic struct vnode *vp; 8952328Sbostic { 9052328Sbostic struct inode *ip; 9152328Sbostic struct lfs *fs; 9252328Sbostic struct mount *mp; 9352328Sbostic struct segment *sp; 9452328Sbostic int error, s; 9552328Sbostic 9652328Sbostic #ifdef VERBOSE 9752328Sbostic printf("lfs_vflush\n"); 9852328Sbostic #endif 9952328Sbostic mp = vp->v_mount; 10052328Sbostic fs = VFSTOUFS(mp)->um_lfs; 10152328Sbostic 10252328Sbostic /* 10352328Sbostic * XXX 10452328Sbostic * check flags? 10552328Sbostic * mp->mnt_flag & (MNT_MLOCK|MNT_RDONLY|MNT_MPBUSY) || 10652328Sbostic */ 10752328Sbostic if (vfs_busy(mp)) 10852328Sbostic return (0); 10952328Sbostic 11052328Sbostic /* 11152328Sbostic * Allocate a segment structure and enough space to hold pointers to 11252328Sbostic * the maximum possible number of buffers which can be described in a 11352328Sbostic * single summary block. 11452328Sbostic */ 11552328Sbostic sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK); 11652328Sbostic sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) / 11752328Sbostic sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK); 11852328Sbostic sp->seg_flags = SEGM_CKP; 11952328Sbostic lfs_initseg(fs, sp); 12052328Sbostic 12152328Sbostic /* 12252328Sbostic * Keep a cumulative count of the outstanding I/O operations. If the 12352328Sbostic * disk drive catches up with us it could go to zero before we finish, 12452328Sbostic * so we artificially increment it by one until we've scheduled all of 12552328Sbostic * the writes we intend to do. 12652328Sbostic */ 12752328Sbostic s = splbio(); 12852688Sbostic ++fs->lfs_iocount; 12952328Sbostic splx(s); 13052328Sbostic 13152328Sbostic if (vp->v_dirtyblkhd != NULL) 13252328Sbostic lfs_writefile(fs, sp, vp); 13352328Sbostic ip = VTOI(vp); 13452328Sbostic lfs_writeinode(fs, sp, ip); 13552328Sbostic ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG); 13652328Sbostic 13752328Sbostic lfs_writeseg(fs, sp); 13852328Sbostic 13952328Sbostic /* 14052328Sbostic * If the I/O count is non-zero, sleep until it reaches zero. At the 14152328Sbostic * moment, the user's process hangs around so we can sleep. 14252328Sbostic */ 14352328Sbostic s = splbio(); 14452328Sbostic if (--fs->lfs_iocount && (error = 14552688Sbostic tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs vflush", 0))) 14652328Sbostic return (error); 14752328Sbostic splx(s); 14852328Sbostic vfs_unbusy(mp); 14952328Sbostic 15052328Sbostic free(sp->bpp, M_SEGMENT); 15152328Sbostic free(sp, M_SEGMENT); 15252328Sbostic 15352328Sbostic return (0); 15452328Sbostic } 15552328Sbostic 15652328Sbostic int 15751215Sbostic lfs_segwrite(mp, do_ckp) 15852085Sbostic struct mount *mp; 15951860Sbostic int do_ckp; /* Do a checkpoint. */ 16051188Sbostic { 16152085Sbostic struct inode *ip; 16251499Sbostic struct lfs *fs; 16352085Sbostic struct segment *sp; 16452085Sbostic struct vnode *vp; 16552328Sbostic int error, islocked, s; 16651188Sbostic 16751860Sbostic #ifdef VERBOSE 16851860Sbostic printf("lfs_segwrite\n"); 16951860Sbostic #endif 17052328Sbostic fs = VFSTOUFS(mp)->um_lfs; 17152085Sbostic 17251860Sbostic /* 17352328Sbostic * Allocate a segment structure and enough space to hold pointers to 17452328Sbostic * the maximum possible number of buffers which can be described in a 17552328Sbostic * single summary block. 17652328Sbostic */ 17752328Sbostic sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK); 17852328Sbostic sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) / 17952328Sbostic sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK); 18052328Sbostic sp->seg_flags = do_ckp ? SEGM_CKP : 0; 18152328Sbostic lfs_initseg(fs, sp); 18252328Sbostic 18352328Sbostic /* 18452688Sbostic * Keep a cumulative count of the outstanding I/O operations. If the 18552688Sbostic * disk drive catches up with us it could go to zero before we finish, 18652688Sbostic * so we artificially increment it by one until we've scheduled all of 18752688Sbostic * the writes we intend to do. If not a checkpoint, we never do the 18852688Sbostic * final decrement, avoiding the wakeup in the callback routine. 18951860Sbostic */ 19052688Sbostic s = splbio(); 19152688Sbostic ++fs->lfs_iocount; 19252688Sbostic splx(s); 19351342Sbostic 19452328Sbostic loop: for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) { 19551188Sbostic /* 19651188Sbostic * If the vnode that we are about to sync is no longer 19751188Sbostic * associated with this mount point, start over. 19851188Sbostic */ 19951188Sbostic if (vp->v_mount != mp) 20051188Sbostic goto loop; 20151860Sbostic 20252328Sbostic islocked = VOP_ISLOCKED(vp); 20351860Sbostic 20452077Sbostic /* 20552077Sbostic * XXX 20652077Sbostic * This is wrong, I think -- we should just wait until we 20752077Sbostic * get the vnode and go on. Probably going to reschedule 20852077Sbostic * all of the writes we already scheduled... 20952077Sbostic */ 21052328Sbostic if (islocked) 21152328Sbostic VREF(vp); 21252328Sbostic else if (vget(vp)) 21352085Sbostic { 21452085Sbostic printf("lfs_segment: failed to get vnode (tell Keith)!\n"); 21551188Sbostic goto loop; 21652085Sbostic } 21752328Sbostic /* 21852328Sbostic * Write the inode/file if dirty and it's not the 21952328Sbostic * the IFILE. 22052328Sbostic */ 22152328Sbostic ip = VTOI(vp); 22252328Sbostic if ((ip->i_flag & (IMOD | IACC | IUPD | ICHG) || 22352328Sbostic vp->v_dirtyblkhd != NULL) && 22452328Sbostic ip->i_number != LFS_IFILE_INUM) { 22552328Sbostic if (vp->v_dirtyblkhd != NULL) 22652328Sbostic lfs_writefile(fs, sp, vp); 22752328Sbostic lfs_writeinode(fs, sp, ip); 22852328Sbostic ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG); 22952328Sbostic } 23052328Sbostic if (islocked) 23152328Sbostic vrele(vp); 23252328Sbostic else 23352328Sbostic vput(vp); 23451188Sbostic } 23551860Sbostic if (do_ckp) { 23652077Sbostic vp = fs->lfs_ivnode; 23752077Sbostic while (vget(vp)); 23852077Sbostic ip = VTOI(vp); 23952077Sbostic if (vp->v_dirtyblkhd != NULL) 24052077Sbostic lfs_writefile(fs, sp, vp); 24152077Sbostic lfs_writeinode(fs, sp, ip); 24252077Sbostic ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG); 24352077Sbostic vput(vp); 24451860Sbostic } 24551342Sbostic lfs_writeseg(fs, sp); 24651342Sbostic 24751215Sbostic /* 24851860Sbostic * If the I/O count is non-zero, sleep until it reaches zero. At the 24951860Sbostic * moment, the user's process hangs around so we can sleep. 25051215Sbostic */ 25152688Sbostic s = splbio(); 25252688Sbostic --fs->lfs_iocount; 25351860Sbostic if (do_ckp) { 25452688Sbostic if (fs->lfs_iocount && (error = 25552688Sbostic tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs sync", 0))) 25651915Sbostic return (error); 25751860Sbostic splx(s); 25851860Sbostic lfs_writesuper(fs, sp); 25952688Sbostic } else 26052688Sbostic splx(s); 26151215Sbostic 26251927Sbostic free(sp->bpp, M_SEGMENT); 26351927Sbostic free(sp, M_SEGMENT); 26451215Sbostic 26551860Sbostic return (0); 26651188Sbostic } 26751188Sbostic 26851860Sbostic /* 26951860Sbostic * Write the dirty blocks associated with a vnode. 27051860Sbostic */ 27152077Sbostic void 27251860Sbostic lfs_writefile(fs, sp, vp) 27351499Sbostic struct lfs *fs; 27452085Sbostic struct segment *sp; 27552085Sbostic struct vnode *vp; 27651188Sbostic { 27751860Sbostic struct buf *bp; 27852085Sbostic struct finfo *fip; 27951860Sbostic IFILE *ifp; 28051188Sbostic 28151860Sbostic #ifdef VERBOSE 28251860Sbostic printf("lfs_writefile\n"); 28351860Sbostic #endif 28452085Sbostic if (sp->seg_bytes_left < fs->lfs_bsize || 28552085Sbostic sp->sum_bytes_left < sizeof(struct finfo)) { 28652085Sbostic lfs_writeseg(fs, sp); 28752085Sbostic lfs_initseg(fs, sp); 28852085Sbostic } 28952085Sbostic sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(daddr_t); 29051215Sbostic 29152085Sbostic fip = sp->fip; 29252085Sbostic fip->fi_nblocks = 0; 29352085Sbostic fip->fi_ino = VTOI(vp)->i_number; 29452085Sbostic LFS_IENTRY(ifp, fs, fip->fi_ino, bp); 29552085Sbostic fip->fi_version = ifp->if_version; 29652085Sbostic brelse(bp); 29751188Sbostic 29852085Sbostic /* 29952085Sbostic * It may not be necessary to write the meta-data blocks at this point, 30052085Sbostic * as the roll-forward recovery code should be able to reconstruct the 30152085Sbostic * list. 30252085Sbostic */ 30352085Sbostic lfs_gather(fs, sp, vp, lfs_match_data); 30452085Sbostic lfs_gather(fs, sp, vp, lfs_match_indir); 30552085Sbostic lfs_gather(fs, sp, vp, lfs_match_dindir); 30651860Sbostic #ifdef TRIPLE 30752085Sbostic lfs_gather(fs, sp, vp, lfs_match_tindir); 30851860Sbostic #endif 30951342Sbostic 31052085Sbostic fip = sp->fip; 31151860Sbostic #ifdef META 31252085Sbostic printf("lfs_writefile: adding %d blocks\n", fip->fi_nblocks); 31351860Sbostic #endif 31452085Sbostic if (fip->fi_nblocks != 0) { 31552085Sbostic ++((SEGSUM *)(sp->segsum))->ss_nfinfo; 31652085Sbostic sp->fip = 31752085Sbostic (struct finfo *)((caddr_t)fip + sizeof(struct finfo) + 31852085Sbostic sizeof(daddr_t) * (fip->fi_nblocks - 1)); 31952682Sstaelin } else 32052682Sstaelin sp->sum_bytes_left += sizeof(struct finfo) - sizeof(daddr_t); 32151215Sbostic } 32251215Sbostic 32352077Sbostic void 32451915Sbostic lfs_writeinode(fs, sp, ip) 32551915Sbostic struct lfs *fs; 32652085Sbostic struct segment *sp; 32752085Sbostic struct inode *ip; 32851915Sbostic { 32952085Sbostic struct buf *bp, *ibp; 33052077Sbostic IFILE *ifp; 33152682Sstaelin SEGUSE *sup; 33252682Sstaelin daddr_t daddr; 33352077Sbostic ino_t ino; 33451915Sbostic int ndx; 33551915Sbostic 33651915Sbostic #ifdef VERBOSE 33751915Sbostic printf("lfs_writeinode\n"); 33851915Sbostic #endif 33951915Sbostic /* Allocate a new inode block if necessary. */ 34051915Sbostic if (sp->ibp == NULL) { 34151915Sbostic /* Allocate a new segment if necessary. */ 34251915Sbostic if (sp->seg_bytes_left < fs->lfs_bsize || 34351915Sbostic sp->sum_bytes_left < sizeof(daddr_t)) { 34451915Sbostic lfs_writeseg(fs, sp); 34551915Sbostic lfs_initseg(fs, sp); 34651915Sbostic } 34751915Sbostic 34851915Sbostic /* Get next inode block. */ 34952682Sstaelin daddr = fs->lfs_offset; 35051915Sbostic fs->lfs_offset += fsbtodb(fs, 1); 35151915Sbostic sp->ibp = *sp->cbpp++ = 35252688Sbostic lfs_newbuf(fs, daddr, fs->lfs_bsize); 35351915Sbostic 35452688Sbostic /* Set remaining space counters. */ 35551915Sbostic sp->seg_bytes_left -= fs->lfs_bsize; 35651915Sbostic sp->sum_bytes_left -= sizeof(daddr_t); 35752077Sbostic ndx = LFS_SUMMARY_SIZE / sizeof(daddr_t) - 35851915Sbostic sp->ninodes / INOPB(fs) - 1; 35952682Sstaelin ((daddr_t *)(sp->segsum))[ndx] = daddr; 36051915Sbostic } 36151915Sbostic 36252085Sbostic /* Update the inode times and copy the inode onto the inode page. */ 36352077Sbostic ITIMES(ip, &time, &time); 36451915Sbostic bp = sp->ibp; 36552085Sbostic bp->b_un.b_dino[sp->ninodes % INOPB(fs)] = ip->i_din; 36651915Sbostic 36751915Sbostic /* Increment inode count in segment summary block. */ 36851915Sbostic ++((SEGSUM *)(sp->segsum))->ss_ninos; 36951915Sbostic 37051915Sbostic /* If this page is full, set flag to allocate a new page. */ 37151915Sbostic if (++sp->ninodes % INOPB(fs) == 0) 37251915Sbostic sp->ibp = NULL; 37351915Sbostic 37451915Sbostic /* 37552077Sbostic * If updating the ifile, update the super-block. Update the disk 37652077Sbostic * address and access times for this inode in the ifile. 37751915Sbostic */ 37852077Sbostic ino = ip->i_number; 37952077Sbostic if (ino == LFS_IFILE_INUM) 38051915Sbostic fs->lfs_idaddr = bp->b_blkno; 38152077Sbostic 38252077Sbostic LFS_IENTRY(ifp, fs, ino, ibp); 38352682Sstaelin daddr = ifp->if_daddr; 38452077Sbostic ifp->if_daddr = bp->b_blkno; 38552085Sbostic LFS_UBWRITE(ibp); 38652682Sstaelin 38752682Sstaelin if (daddr != LFS_UNUSED_DADDR) { 38852682Sstaelin LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp); 38952682Sstaelin #ifdef DIAGNOSTIC 39052682Sstaelin if (sup->su_nbytes < sizeof(struct dinode)) 391*52819Sbostic /* XXX -- Change to a panic. */ 392*52819Sbostic printf("lfs: negative bytes (segment %d)\n", 39352682Sstaelin datosn(fs, daddr)); 39452682Sstaelin #endif 39552682Sstaelin sup->su_nbytes -= sizeof(struct dinode); 39652682Sstaelin LFS_UBWRITE(bp); 39752682Sstaelin } 39851915Sbostic } 39951915Sbostic 40052077Sbostic void 40151215Sbostic lfs_gather(fs, sp, vp, match) 40251499Sbostic struct lfs *fs; 40352085Sbostic struct segment *sp; 40452085Sbostic struct vnode *vp; 40552085Sbostic int (*match) __P((struct lfs *, struct buf *)); 40651215Sbostic { 40752085Sbostic struct buf **bpp, *bp, *nbp; 40852085Sbostic struct finfo *fip; 40952085Sbostic struct inode *ip; 41051215Sbostic daddr_t *lbp, *start_lbp; 41151342Sbostic u_long version; 41251342Sbostic int s; 41351215Sbostic 41451860Sbostic #ifdef VERBOSE 41551860Sbostic printf("lfs_gather\n"); 41651860Sbostic #endif 41751215Sbostic ip = VTOI(vp); 41851215Sbostic bpp = sp->cbpp; 41951215Sbostic fip = sp->fip; 42051215Sbostic start_lbp = lbp = &fip->fi_blocks[fip->fi_nblocks]; 42151215Sbostic 42251215Sbostic s = splbio(); 42351215Sbostic for (bp = vp->v_dirtyblkhd; bp; bp = nbp) { 42451215Sbostic nbp = bp->b_blockf; 42551915Sbostic /* 42651915Sbostic * XXX 42752682Sstaelin * Should sleep on any BUSY buffer if doing an fsync? 42851915Sbostic */ 42952328Sbostic if (bp->b_flags & B_BUSY || !match(fs, bp)) 43051215Sbostic continue; 43151342Sbostic #ifdef DIAGNOSTIC 43251860Sbostic if (!(bp->b_flags & B_DELWRI)) 43351915Sbostic panic("lfs_gather: bp not B_DELWRI"); 43451860Sbostic if (!(bp->b_flags & B_LOCKED)) 43551915Sbostic panic("lfs_gather: bp not B_LOCKED"); 43651342Sbostic #endif 43751860Sbostic /* 43851860Sbostic * If full, finish this segment. We may be doing I/O, so 43951860Sbostic * release and reacquire the splbio(). 44051860Sbostic */ 44151342Sbostic if (sp->sum_bytes_left < sizeof(daddr_t) || 44251215Sbostic sp->seg_bytes_left < fs->lfs_bsize) { 44351215Sbostic splx(s); 44451342Sbostic lfs_updatemeta(fs, 44551860Sbostic sp, vp, start_lbp, bpp, lbp - start_lbp); 44651215Sbostic 44751342Sbostic /* Add the current file to the segment summary. */ 44851342Sbostic ++((SEGSUM *)(sp->segsum))->ss_nfinfo; 44951215Sbostic 45051342Sbostic version = fip->fi_version; 45151860Sbostic lfs_writeseg(fs, sp); 45251915Sbostic lfs_initseg(fs, sp); 45351342Sbostic 45451215Sbostic fip = sp->fip; 45551342Sbostic fip->fi_version = version; 45651215Sbostic fip->fi_ino = ip->i_number; 45751342Sbostic start_lbp = lbp = fip->fi_blocks; 45851342Sbostic 45952682Sstaelin sp->sum_bytes_left -= 46052682Sstaelin sizeof(struct finfo) - sizeof(daddr_t); 46152682Sstaelin 46251215Sbostic bpp = sp->cbpp; 46351215Sbostic s = splbio(); 46451215Sbostic } 46552682Sstaelin 46652682Sstaelin /* Insert into the buffer list, update the FINFO block. */ 46752682Sstaelin *sp->cbpp++ = bp; 46852682Sstaelin ++fip->fi_nblocks; 46952682Sstaelin *lbp++ = bp->b_lblkno; 47052682Sstaelin 47152682Sstaelin sp->sum_bytes_left -= sizeof(daddr_t); 47252682Sstaelin sp->seg_bytes_left -= bp->b_bufsize; 47351188Sbostic } 47451215Sbostic splx(s); 47551860Sbostic lfs_updatemeta(fs, sp, vp, start_lbp, bpp, lbp - start_lbp); 47651188Sbostic } 47751188Sbostic 47851342Sbostic /* 47951342Sbostic * Update the metadata that points to the blocks listed in the FINFO 48051188Sbostic * array. 48151188Sbostic */ 48252077Sbostic void 48351860Sbostic lfs_updatemeta(fs, sp, vp, lbp, bpp, nblocks) 48451499Sbostic struct lfs *fs; 48552085Sbostic struct segment *sp; 48652085Sbostic struct vnode *vp; 48751215Sbostic daddr_t *lbp; 48852085Sbostic struct buf **bpp; 48951215Sbostic int nblocks; 49051188Sbostic { 49151915Sbostic SEGUSE *sup; 49252085Sbostic struct buf *bp; 49351860Sbostic INDIR a[NIADDR], *ap; 49452085Sbostic struct inode *ip; 49551915Sbostic daddr_t daddr, lbn, off; 49651860Sbostic int db_per_fsb, error, i, num; 49751188Sbostic 49851860Sbostic #ifdef VERBOSE 49951860Sbostic printf("lfs_updatemeta\n"); 50051860Sbostic #endif 50151342Sbostic if (nblocks == 0) 50251215Sbostic return; 50351215Sbostic 50451915Sbostic /* Sort the blocks. */ 50552077Sbostic lfs_shellsort(bpp, lbp, nblocks); 50651215Sbostic 50751915Sbostic /* 50851915Sbostic * Assign disk addresses, and update references to the logical 50951915Sbostic * block and the segment usage information. 51051915Sbostic */ 51151860Sbostic db_per_fsb = fsbtodb(fs, 1); 51251915Sbostic for (i = nblocks; i--; ++bpp) { 51351915Sbostic lbn = *lbp++; 51451915Sbostic (*bpp)->b_blkno = off = fs->lfs_offset; 51551860Sbostic fs->lfs_offset += db_per_fsb; 51651215Sbostic 51751860Sbostic if (error = lfs_bmaparray(vp, lbn, &daddr, a, &num)) 51852085Sbostic panic("lfs_updatemeta: lfs_bmaparray %d", error); 51951860Sbostic ip = VTOI(vp); 52051860Sbostic switch (num) { 52151860Sbostic case 0: 52251915Sbostic ip->i_db[lbn] = off; 52351860Sbostic break; 52451860Sbostic case 1: 52551915Sbostic ip->i_ib[a[0].in_off] = off; 52651860Sbostic break; 52751860Sbostic default: 52851860Sbostic ap = &a[num - 1]; 52951860Sbostic if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 53051860Sbostic panic("lfs_updatemeta: bread bno %d", 53151860Sbostic ap->in_lbn); 53251915Sbostic bp->b_un.b_daddr[ap->in_off] = off; 53351342Sbostic lfs_bwrite(bp); 53451188Sbostic } 53551915Sbostic 53651915Sbostic /* Update segment usage information. */ 53751915Sbostic if (daddr != UNASSIGNED) { 53851915Sbostic LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp); 53951915Sbostic #ifdef DIAGNOSTIC 54051915Sbostic if (sup->su_nbytes < fs->lfs_bsize) 541*52819Sbostic /* XXX -- Change to a panic. */ 542*52819Sbostic printf("lfs: negative bytes (segment %d)\n", 54351915Sbostic datosn(fs, daddr)); 54451915Sbostic #endif 54551915Sbostic sup->su_nbytes -= fs->lfs_bsize; 54652085Sbostic LFS_UBWRITE(bp); 54751915Sbostic } 54851188Sbostic } 54951188Sbostic } 55051188Sbostic 55151915Sbostic /* 55251915Sbostic * Start a new segment. 55351915Sbostic */ 55452077Sbostic void 55551915Sbostic lfs_initseg(fs, sp) 55651499Sbostic struct lfs *fs; 55752085Sbostic struct segment *sp; 55851188Sbostic { 55951915Sbostic SEGUSE *sup; 56051915Sbostic SEGSUM *ssp; 56151915Sbostic struct buf *bp; 56251915Sbostic daddr_t lbn, *lbnp; 56351215Sbostic 56451860Sbostic #ifdef VERBOSE 56551915Sbostic printf("lfs_initseg\n"); 56651860Sbostic #endif 56751915Sbostic /* Advance to the next segment. */ 56851927Sbostic if (!LFS_PARTIAL_FITS(fs)) { 56952682Sstaelin /* Wake up any cleaning procs waiting on this file system. */ 57052688Sbostic wakeup(&fs->lfs_nextseg); 57152688Sbostic wakeup(&lfs_allclean_wakeup); 57252682Sstaelin 57351927Sbostic lfs_newseg(fs); 57451927Sbostic fs->lfs_offset = fs->lfs_curseg; 57551915Sbostic sp->seg_number = datosn(fs, fs->lfs_curseg); 57651915Sbostic sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE; 57751915Sbostic 57851915Sbostic /* 57951927Sbostic * If the segment contains a superblock, update the offset 58051927Sbostic * and summary address to skip over it. 58151915Sbostic */ 58252077Sbostic LFS_SEGENTRY(sup, fs, sp->seg_number, bp); 58351927Sbostic if (sup->su_flags & SEGUSE_SUPERBLOCK) { 58451915Sbostic fs->lfs_offset += LFS_SBPAD / DEV_BSIZE; 58551915Sbostic sp->seg_bytes_left -= LFS_SBPAD; 58651215Sbostic } 58752085Sbostic brelse(bp); 58851915Sbostic } else { 58951915Sbostic sp->seg_number = datosn(fs, fs->lfs_curseg); 59051915Sbostic sp->seg_bytes_left = (fs->lfs_dbpseg - 59151915Sbostic (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE; 59251915Sbostic } 59351342Sbostic 59451915Sbostic sp->ibp = NULL; 59551915Sbostic sp->ninodes = 0; 59651342Sbostic 59751915Sbostic /* Get a new buffer for SEGSUM and enter it into the buffer list. */ 59851915Sbostic sp->cbpp = sp->bpp; 59952688Sbostic *sp->cbpp = lfs_newbuf(fs, fs->lfs_offset, LFS_SUMMARY_SIZE); 60051915Sbostic sp->segsum = (*sp->cbpp)->b_un.b_addr; 60151915Sbostic ++sp->cbpp; 60251915Sbostic fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE; 60351342Sbostic 60451915Sbostic /* Set point to SEGSUM, initialize it. */ 60551915Sbostic ssp = sp->segsum; 60651915Sbostic ssp->ss_next = fs->lfs_nextseg; 60751915Sbostic ssp->ss_nfinfo = ssp->ss_ninos = 0; 60851342Sbostic 60951915Sbostic /* Set pointer to first FINFO, initialize it. */ 61052085Sbostic sp->fip = (struct finfo *)(sp->segsum + sizeof(SEGSUM)); 61151915Sbostic sp->fip->fi_nblocks = 0; 61251342Sbostic 61351915Sbostic sp->seg_bytes_left -= LFS_SUMMARY_SIZE; 61451915Sbostic sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM); 61551915Sbostic } 61651342Sbostic 61751915Sbostic /* 61851915Sbostic * Return the next segment to write. 61951915Sbostic */ 62052077Sbostic void 62151915Sbostic lfs_newseg(fs) 62251915Sbostic struct lfs *fs; 62351915Sbostic { 62451927Sbostic CLEANERINFO *cip; 62551915Sbostic SEGUSE *sup; 62651915Sbostic struct buf *bp; 62751927Sbostic int curseg, isdirty, sn; 62851915Sbostic 62951915Sbostic #ifdef VERBOSE 63051915Sbostic printf("lfs_newseg\n"); 63151915Sbostic #endif 63251927Sbostic /* 63351927Sbostic * Turn off the active bit for the current segment, turn on the 63451927Sbostic * active and dirty bits for the next segment, update the cleaner 63551927Sbostic * info. Set the current segment to the next segment, get a new 63651927Sbostic * next segment. 63751927Sbostic */ 63851927Sbostic LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_curseg), bp); 63951927Sbostic sup->su_flags &= ~SEGUSE_ACTIVE; 64052085Sbostic LFS_UBWRITE(bp); 64151927Sbostic 64251927Sbostic LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp); 64351927Sbostic sup->su_flags |= SEGUSE_ACTIVE | SEGUSE_DIRTY; 64452085Sbostic LFS_UBWRITE(bp); 64551927Sbostic 64651927Sbostic LFS_CLEANERINFO(cip, fs, bp); 64751927Sbostic --cip->clean; 64851927Sbostic ++cip->dirty; 64952085Sbostic LFS_UBWRITE(bp); 65051927Sbostic 65151927Sbostic fs->lfs_lastseg = fs->lfs_curseg; 65251927Sbostic fs->lfs_curseg = fs->lfs_nextseg; 65351927Sbostic for (sn = curseg = datosn(fs, fs->lfs_curseg);;) { 65451915Sbostic sn = (sn + 1) % fs->lfs_nseg; 65551927Sbostic if (sn == curseg) 65651915Sbostic panic("lfs_nextseg: no clean segments"); 65751915Sbostic LFS_SEGENTRY(sup, fs, sn, bp); 65851915Sbostic isdirty = sup->su_flags & SEGUSE_DIRTY; 65952085Sbostic brelse(bp); 66051915Sbostic if (!isdirty) 66151915Sbostic break; 66251915Sbostic } 66351927Sbostic fs->lfs_nextseg = sntoda(fs, sn); 66451188Sbostic } 66551188Sbostic 66652077Sbostic void 66751188Sbostic lfs_writeseg(fs, sp) 66851499Sbostic struct lfs *fs; 66952085Sbostic struct segment *sp; 67051188Sbostic { 67152688Sbostic struct buf **bpp, *bp, *cbp; 67251188Sbostic SEGUSE *sup; 67352085Sbostic SEGSUM *ssp; 67451860Sbostic dev_t i_dev; 67551860Sbostic u_long *datap, *dp; 67652688Sbostic size_t size; 67752688Sbostic int ch_per_blk, i, nblocks, num, s, (*strategy)__P((struct buf *)); 67852688Sbostic char *p; 67951188Sbostic 68051860Sbostic #ifdef VERBOSE 68151860Sbostic printf("lfs_writeseg\n"); 68251860Sbostic #endif 68352085Sbostic if ((nblocks = sp->cbpp - sp->bpp) == 0) 68452085Sbostic return; 68552085Sbostic 68651188Sbostic /* 68752085Sbostic * Compute checksum across data and then across summary; the first 68852085Sbostic * block (the summary block) is skipped. Set the create time here 68952085Sbostic * so that it's guaranteed to be later than the inode mod times. 69051860Sbostic * 69151860Sbostic * XXX 69251860Sbostic * Fix this to do it inline, instead of malloc/copy. 69351188Sbostic */ 69451860Sbostic datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK); 69551915Sbostic for (bpp = sp->bpp, i = nblocks - 1; i--;) 69651915Sbostic *dp++ = (*++bpp)->b_un.b_words[0]; 69752085Sbostic ssp = (SEGSUM *)sp->segsum; 69852103Sbostic ssp->ss_create = time.tv_sec; 69952085Sbostic ssp->ss_datasum = cksum(datap, nblocks * sizeof(u_long)); 70052085Sbostic ssp->ss_sumsum = 70152085Sbostic cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum)); 70251927Sbostic free(datap, M_SEGMENT); 70351188Sbostic 70451860Sbostic i_dev = VTOI(fs->lfs_ivnode)->i_dev; 70551860Sbostic strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op->vop_strategy; 70651301Sbostic 70752688Sbostic /* 70852688Sbostic * When we simply write the blocks we lose a rotation for every block 70952688Sbostic * written. To avoid this problem, we allocate memory in chunks, copy 71052688Sbostic * the buffers into the chunk and write the chunk. 56K was chosen as 71152688Sbostic * some driver/controllers can't handle unsigned 16 bit transfers. 71252688Sbostic * When the data is copied to the chunk, turn off the the B_LOCKED bit 71352688Sbostic * and brelse the buffer (which will move them to the LRU list). Add 71452688Sbostic * the B_CALL flag to the buffer header so we can count I/O's for the 71552688Sbostic * checkpoints and so we can release the allocated memory. 71652688Sbostic * 71752688Sbostic * XXX 71852688Sbostic * This should be removed if the new virtual memory system allows us to 71952688Sbostic * easily make the buffers contiguous in kernel memory and if that's 72052688Sbostic * fast enough. 72152688Sbostic */ 72252688Sbostic #define LFS_CHUNKSIZE (56 * 1024) 72352688Sbostic ch_per_blk = LFS_CHUNKSIZE / fs->lfs_bsize; 72452688Sbostic for (bpp = sp->bpp, i = nblocks; i;) { 72552688Sbostic num = ch_per_blk; 72652688Sbostic if (num > i) 72752688Sbostic num = i; 72852688Sbostic i -= num; 72952688Sbostic size = num * fs->lfs_bsize; 73052688Sbostic 73152688Sbostic cbp = lfs_newbuf(fs, (*bpp)->b_blkno, 0); 73252688Sbostic cbp->b_dev = i_dev; 73352688Sbostic cbp->b_flags = B_ASYNC | B_BUSY | B_CALL; 73452688Sbostic cbp->b_iodone = lfs_callback; 73552688Sbostic cbp->b_saveaddr = cbp->b_un.b_addr; 73652688Sbostic cbp->b_un.b_addr = malloc(size, M_SEGMENT, M_WAITOK); 73752688Sbostic 73852688Sbostic s = splbio(); 73952688Sbostic ++fs->lfs_iocount; 74052688Sbostic for (p = cbp->b_un.b_addr; num--;) { 74152688Sbostic bp = *bpp++; 74252688Sbostic bcopy(bp->b_un.b_addr, p, bp->b_bcount); 74352688Sbostic p += bp->b_bcount; 74452688Sbostic bp->b_flags &= 74552688Sbostic ~(B_DONE | B_ERROR | B_READ | B_DELWRI | B_LOCKED); 74652688Sbostic if (!(bp->b_flags & B_NOCACHE)) { 74752688Sbostic bremfree(bp); 74852688Sbostic reassignbuf(bp, bp->b_vp); 74952688Sbostic } 75052688Sbostic brelse(bp); 75151860Sbostic } 75252688Sbostic splx(s); 75352688Sbostic cbp->b_bcount = p - cbp->b_un.b_addr; 75452688Sbostic (strategy)(cbp); 75551860Sbostic } 75652077Sbostic 75752682Sstaelin /* Update the segment usage information. */ 75852682Sstaelin LFS_SEGENTRY(sup, fs, sp->seg_number, bp); 75952682Sstaelin sup->su_nbytes += nblocks - 1 - 76052682Sstaelin (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs) << fs->lfs_bshift; 76152682Sstaelin sup->su_nbytes += ssp->ss_ninos * sizeof(struct dinode); 76252682Sstaelin sup->su_lastmod = time.tv_sec; 76352682Sstaelin LFS_UBWRITE(bp); 76451188Sbostic } 76551188Sbostic 76652077Sbostic void 76751860Sbostic lfs_writesuper(fs, sp) 76851499Sbostic struct lfs *fs; 76952085Sbostic struct segment *sp; 77051301Sbostic { 77152085Sbostic struct buf *bp; 77251860Sbostic dev_t i_dev; 77352085Sbostic int (*strategy) __P((struct buf *)); 77451301Sbostic 77551860Sbostic #ifdef VERBOSE 77651860Sbostic printf("lfs_writesuper\n"); 77751860Sbostic #endif 77851860Sbostic i_dev = VTOI(fs->lfs_ivnode)->i_dev; 77951860Sbostic strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op->vop_strategy; 78051356Sbostic 78151342Sbostic /* Checksum the superblock and copy it into a buffer. */ 78251499Sbostic fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum)); 78352688Sbostic bp = lfs_newbuf(fs, fs->lfs_sboffs[0], LFS_SBPAD); 78451860Sbostic *bp->b_un.b_lfs = *fs; 78551215Sbostic 78651356Sbostic /* Write the first superblock (wait). */ 78751860Sbostic bp->b_dev = i_dev; 78851915Sbostic bp->b_flags |= B_BUSY; 78951860Sbostic bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI); 79051342Sbostic (strategy)(bp); 79151215Sbostic biowait(bp); 79251342Sbostic 79351356Sbostic /* Write the second superblock (don't wait). */ 79451215Sbostic bp->b_blkno = bp->b_lblkno = fs->lfs_sboffs[1]; 79551915Sbostic bp->b_flags |= B_ASYNC | B_BUSY; 79651860Sbostic bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI); 79751342Sbostic (strategy)(bp); 79851215Sbostic } 79951215Sbostic 80051342Sbostic /* 80151342Sbostic * Logical block number match routines used when traversing the dirty block 80251342Sbostic * chain. 80351342Sbostic */ 80452077Sbostic int 80552077Sbostic lfs_match_data(fs, bp) 80651860Sbostic struct lfs *fs; 80752085Sbostic struct buf *bp; 80851215Sbostic { 80951342Sbostic return (bp->b_lblkno >= 0); 81051215Sbostic } 81151215Sbostic 81252077Sbostic int 81352077Sbostic lfs_match_indir(fs, bp) 81451860Sbostic struct lfs *fs; 81552085Sbostic struct buf *bp; 81651215Sbostic { 81751860Sbostic int lbn; 81851860Sbostic 81951860Sbostic lbn = bp->b_lblkno; 82051860Sbostic return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0); 82151215Sbostic } 82251215Sbostic 82352077Sbostic int 82452077Sbostic lfs_match_dindir(fs, bp) 82551860Sbostic struct lfs *fs; 82652085Sbostic struct buf *bp; 82751215Sbostic { 82851860Sbostic int lbn; 82951860Sbostic 83051860Sbostic lbn = bp->b_lblkno; 83151860Sbostic return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1); 83251215Sbostic } 83351215Sbostic 83452077Sbostic int 83552077Sbostic lfs_match_tindir(fs, bp) 83651499Sbostic struct lfs *fs; 83752085Sbostic struct buf *bp; 83851342Sbostic { 83951860Sbostic int lbn; 84051342Sbostic 84151860Sbostic lbn = bp->b_lblkno; 84251860Sbostic return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2); 84351860Sbostic } 84451342Sbostic 84551860Sbostic /* 84651860Sbostic * Allocate a new buffer header. 84751860Sbostic */ 84852085Sbostic struct buf * 84952688Sbostic lfs_newbuf(fs, daddr, size) 85051860Sbostic struct lfs *fs; 85151860Sbostic daddr_t daddr; 85251860Sbostic size_t size; 85351860Sbostic { 85452085Sbostic struct buf *bp; 85551342Sbostic 85651860Sbostic #ifdef VERBOSE 85751860Sbostic printf("lfs_newbuf\n"); 85851860Sbostic #endif 85951860Sbostic bp = getnewbuf(); 86051860Sbostic bremhash(bp); 86151860Sbostic bgetvp(fs->lfs_ivnode, bp); 86251860Sbostic bp->b_bcount = 0; 86351860Sbostic bp->b_lblkno = daddr; 86451860Sbostic bp->b_blkno = daddr; 86551860Sbostic bp->b_error = 0; 86651860Sbostic bp->b_resid = 0; 86752688Sbostic if (size) 86852688Sbostic allocbuf(bp, size); 86951860Sbostic bp->b_flags |= B_NOCACHE; 87052688Sbostic bp->b_saveaddr = NULL; 87151915Sbostic binshash(bp, &bfreelist[BQ_AGE]); 87251860Sbostic return (bp); 87351860Sbostic } 87451342Sbostic 87552077Sbostic int /* XXX should be void */ 87651860Sbostic lfs_callback(bp) 87752085Sbostic struct buf *bp; 87851860Sbostic { 87951860Sbostic struct lfs *fs; 88051342Sbostic 88151860Sbostic fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs; 88251860Sbostic #ifdef DIAGNOSTIC 88351860Sbostic if (fs->lfs_iocount == 0) 88451860Sbostic panic("lfs_callback: zero iocount\n"); 88551860Sbostic #endif 88651860Sbostic if (--fs->lfs_iocount == 0) 88752688Sbostic wakeup(&fs->lfs_iocount); 88851915Sbostic 88952688Sbostic if (bp->b_saveaddr) { 89052688Sbostic free(bp->b_un.b_addr, M_SEGMENT); 89152688Sbostic bp->b_un.b_addr = bp->b_saveaddr; 892*52819Sbostic bp->b_saveaddr = NULL; 89352688Sbostic } 89451860Sbostic brelse(bp); 89551860Sbostic } 89651342Sbostic 89751215Sbostic /* 89851188Sbostic * Shellsort (diminishing increment sort) from Data Structures and 89951188Sbostic * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290; 90051188Sbostic * see also Knuth Vol. 3, page 84. The increments are selected from 90151188Sbostic * formula (8), page 95. Roughly O(N^3/2). 90251188Sbostic */ 90351188Sbostic /* 90451188Sbostic * This is our own private copy of shellsort because we want to sort 90551188Sbostic * two parallel arrays (the array of buffer pointers and the array of 90651188Sbostic * logical block numbers) simultaneously. Note that we cast the array 90751188Sbostic * of logical block numbers to a unsigned in this routine so that the 90851188Sbostic * negative block numbers (meta data blocks) sort AFTER the data blocks. 90951188Sbostic */ 91052077Sbostic void 91152077Sbostic lfs_shellsort(bp_array, lb_array, nmemb) 91252085Sbostic struct buf **bp_array; 91351215Sbostic daddr_t *lb_array; 91451188Sbostic register int nmemb; 91551188Sbostic { 91651188Sbostic static int __rsshell_increments[] = { 4, 1, 0 }; 91751188Sbostic register int incr, *incrp, t1, t2; 91852085Sbostic struct buf *bp_temp; 91951188Sbostic u_long lb_temp; 92051188Sbostic 92151188Sbostic for (incrp = __rsshell_increments; incr = *incrp++;) 92251188Sbostic for (t1 = incr; t1 < nmemb; ++t1) 92351188Sbostic for (t2 = t1 - incr; t2 >= 0;) 92451188Sbostic if (lb_array[t2] > lb_array[t2 + incr]) { 92551188Sbostic lb_temp = lb_array[t2]; 92651188Sbostic lb_array[t2] = lb_array[t2 + incr]; 92751188Sbostic lb_array[t2 + incr] = lb_temp; 92851188Sbostic bp_temp = bp_array[t2]; 92951188Sbostic bp_array[t2] = bp_array[t2 + incr]; 93051188Sbostic bp_array[t2 + incr] = bp_temp; 93151188Sbostic t2 -= incr; 93251188Sbostic } else 93351188Sbostic break; 93451188Sbostic } 935