151188Sbostic /* 251188Sbostic * Copyright (c) 1991 Regents of the University of California. 351188Sbostic * All rights reserved. 451188Sbostic * 551188Sbostic * %sccs.include.redist.c% 651188Sbostic * 7*52682Sstaelin * @(#)lfs_segment.c 7.12 (Berkeley) 02/27/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 * 6752085Sbostic lfs_newbuf __P((struct lfs *, struct segment *, 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(); 12852328Sbostic fs->lfs_iocount = 1; 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 = 14552412Smckusick tsleep((caddr_t)&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 /* 18451860Sbostic * If doing a checkpoint, we keep a cumulative count of the outstanding 18551860Sbostic * I/O operations. If the disk drive catches up with us it could go to 18651860Sbostic * zero before we finish, so we artificially increment it by one until 18751860Sbostic * we've scheduled all of the writes we intend to do. 18851860Sbostic */ 18951860Sbostic if (do_ckp) { 19051860Sbostic s = splbio(); 19151860Sbostic fs->lfs_iocount = 1; 19251860Sbostic splx(s); 19351860Sbostic } 19451342Sbostic 19552328Sbostic loop: for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) { 19651188Sbostic /* 19751188Sbostic * If the vnode that we are about to sync is no longer 19851188Sbostic * associated with this mount point, start over. 19951188Sbostic */ 20051188Sbostic if (vp->v_mount != mp) 20151188Sbostic goto loop; 20251860Sbostic 20352328Sbostic islocked = VOP_ISLOCKED(vp); 20451860Sbostic 20552077Sbostic /* 20652077Sbostic * XXX 20752077Sbostic * This is wrong, I think -- we should just wait until we 20852077Sbostic * get the vnode and go on. Probably going to reschedule 20952077Sbostic * all of the writes we already scheduled... 21052077Sbostic */ 21152328Sbostic if (islocked) 21252328Sbostic VREF(vp); 21352328Sbostic else if (vget(vp)) 21452085Sbostic { 21552085Sbostic printf("lfs_segment: failed to get vnode (tell Keith)!\n"); 21651188Sbostic goto loop; 21752085Sbostic } 21852328Sbostic /* 21952328Sbostic * Write the inode/file if dirty and it's not the 22052328Sbostic * the IFILE. 22152328Sbostic */ 22252328Sbostic ip = VTOI(vp); 22352328Sbostic if ((ip->i_flag & (IMOD | IACC | IUPD | ICHG) || 22452328Sbostic vp->v_dirtyblkhd != NULL) && 22552328Sbostic ip->i_number != LFS_IFILE_INUM) { 22652328Sbostic if (vp->v_dirtyblkhd != NULL) 22752328Sbostic lfs_writefile(fs, sp, vp); 22852328Sbostic lfs_writeinode(fs, sp, ip); 22952328Sbostic ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG); 23052328Sbostic } 23152328Sbostic if (islocked) 23252328Sbostic vrele(vp); 23352328Sbostic else 23452328Sbostic vput(vp); 23551188Sbostic } 23651860Sbostic if (do_ckp) { 23752077Sbostic vp = fs->lfs_ivnode; 23852077Sbostic while (vget(vp)); 23952077Sbostic ip = VTOI(vp); 24052077Sbostic if (vp->v_dirtyblkhd != NULL) 24152077Sbostic lfs_writefile(fs, sp, vp); 24252077Sbostic lfs_writeinode(fs, sp, ip); 24352077Sbostic ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG); 24452077Sbostic vput(vp); 24551860Sbostic } 24651342Sbostic lfs_writeseg(fs, sp); 24751342Sbostic 24851215Sbostic /* 24951860Sbostic * If the I/O count is non-zero, sleep until it reaches zero. At the 25051860Sbostic * moment, the user's process hangs around so we can sleep. 25151215Sbostic */ 25251860Sbostic if (do_ckp) { 25351860Sbostic s = splbio(); 25452412Smckusick if (--fs->lfs_iocount && 25552412Smckusick (error = tsleep((caddr_t)&fs->lfs_iocount, PRIBIO + 1, 25652412Smckusick "lfs sync", 0))) 25751915Sbostic return (error); 25851860Sbostic splx(s); 25951860Sbostic lfs_writesuper(fs, sp); 26051860Sbostic } 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)); 319*52682Sstaelin } else 320*52682Sstaelin 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; 331*52682Sstaelin SEGUSE *sup; 332*52682Sstaelin 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. */ 349*52682Sstaelin daddr = fs->lfs_offset; 35051915Sbostic fs->lfs_offset += fsbtodb(fs, 1); 35151915Sbostic sp->ibp = *sp->cbpp++ = 352*52682Sstaelin lfs_newbuf(fs, sp, daddr, fs->lfs_bsize); 35351915Sbostic 35451915Sbostic /* Set remaining space counter. */ 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; 359*52682Sstaelin ((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); 383*52682Sstaelin daddr = ifp->if_daddr; 38452077Sbostic ifp->if_daddr = bp->b_blkno; 38552085Sbostic LFS_UBWRITE(ibp); 386*52682Sstaelin 387*52682Sstaelin if (daddr != LFS_UNUSED_DADDR) { 388*52682Sstaelin LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp); 389*52682Sstaelin #ifdef DIAGNOSTIC 390*52682Sstaelin if (sup->su_nbytes < sizeof(struct dinode)) 391*52682Sstaelin panic("lfs: negative bytes (segment %d)\n", 392*52682Sstaelin datosn(fs, daddr)); 393*52682Sstaelin #endif 394*52682Sstaelin sup->su_nbytes -= sizeof(struct dinode); 395*52682Sstaelin LFS_UBWRITE(bp); 396*52682Sstaelin } 39751915Sbostic } 39851915Sbostic 39952077Sbostic void 40051215Sbostic lfs_gather(fs, sp, vp, match) 40151499Sbostic struct lfs *fs; 40252085Sbostic struct segment *sp; 40352085Sbostic struct vnode *vp; 40452085Sbostic int (*match) __P((struct lfs *, struct buf *)); 40551215Sbostic { 40652085Sbostic struct buf **bpp, *bp, *nbp; 40752085Sbostic struct finfo *fip; 40852085Sbostic struct inode *ip; 40951215Sbostic daddr_t *lbp, *start_lbp; 41051342Sbostic u_long version; 41151342Sbostic int s; 41251215Sbostic 41351860Sbostic #ifdef VERBOSE 41451860Sbostic printf("lfs_gather\n"); 41551860Sbostic #endif 41651215Sbostic ip = VTOI(vp); 41751215Sbostic bpp = sp->cbpp; 41851215Sbostic fip = sp->fip; 41951215Sbostic start_lbp = lbp = &fip->fi_blocks[fip->fi_nblocks]; 42051215Sbostic 42151215Sbostic s = splbio(); 42251215Sbostic for (bp = vp->v_dirtyblkhd; bp; bp = nbp) { 42351215Sbostic nbp = bp->b_blockf; 42451915Sbostic /* 42551915Sbostic * XXX 426*52682Sstaelin * Should sleep on any BUSY buffer if doing an fsync? 42751915Sbostic */ 42852328Sbostic if (bp->b_flags & B_BUSY || !match(fs, bp)) 42951215Sbostic continue; 43051342Sbostic #ifdef DIAGNOSTIC 43151860Sbostic if (!(bp->b_flags & B_DELWRI)) 43251915Sbostic panic("lfs_gather: bp not B_DELWRI"); 43351860Sbostic if (!(bp->b_flags & B_LOCKED)) 43451915Sbostic panic("lfs_gather: bp not B_LOCKED"); 43551342Sbostic #endif 43651860Sbostic /* 43751860Sbostic * If full, finish this segment. We may be doing I/O, so 43851860Sbostic * release and reacquire the splbio(). 43951860Sbostic */ 44051342Sbostic if (sp->sum_bytes_left < sizeof(daddr_t) || 44151215Sbostic sp->seg_bytes_left < fs->lfs_bsize) { 44251215Sbostic splx(s); 44351342Sbostic lfs_updatemeta(fs, 44451860Sbostic sp, vp, start_lbp, bpp, lbp - start_lbp); 44551215Sbostic 44651342Sbostic /* Add the current file to the segment summary. */ 44751342Sbostic ++((SEGSUM *)(sp->segsum))->ss_nfinfo; 44851215Sbostic 44951342Sbostic version = fip->fi_version; 45051860Sbostic lfs_writeseg(fs, sp); 45151915Sbostic lfs_initseg(fs, sp); 45251342Sbostic 45351215Sbostic fip = sp->fip; 45451342Sbostic fip->fi_version = version; 45551215Sbostic fip->fi_ino = ip->i_number; 45651342Sbostic start_lbp = lbp = fip->fi_blocks; 45751342Sbostic 458*52682Sstaelin sp->sum_bytes_left -= 459*52682Sstaelin sizeof(struct finfo) - sizeof(daddr_t); 460*52682Sstaelin 46151215Sbostic bpp = sp->cbpp; 46251215Sbostic s = splbio(); 46351215Sbostic } 464*52682Sstaelin 465*52682Sstaelin /* Insert into the buffer list, update the FINFO block. */ 466*52682Sstaelin *sp->cbpp++ = bp; 467*52682Sstaelin ++fip->fi_nblocks; 468*52682Sstaelin *lbp++ = bp->b_lblkno; 469*52682Sstaelin 470*52682Sstaelin sp->sum_bytes_left -= sizeof(daddr_t); 471*52682Sstaelin sp->seg_bytes_left -= bp->b_bufsize; 47251188Sbostic } 47351215Sbostic splx(s); 47451860Sbostic lfs_updatemeta(fs, sp, vp, start_lbp, bpp, lbp - start_lbp); 47551188Sbostic } 47651188Sbostic 47751342Sbostic /* 47851342Sbostic * Update the metadata that points to the blocks listed in the FINFO 47951188Sbostic * array. 48051188Sbostic */ 48152077Sbostic void 48251860Sbostic lfs_updatemeta(fs, sp, vp, lbp, bpp, nblocks) 48351499Sbostic struct lfs *fs; 48452085Sbostic struct segment *sp; 48552085Sbostic struct vnode *vp; 48651215Sbostic daddr_t *lbp; 48752085Sbostic struct buf **bpp; 48851215Sbostic int nblocks; 48951188Sbostic { 49051915Sbostic SEGUSE *sup; 49152085Sbostic struct buf *bp; 49251860Sbostic INDIR a[NIADDR], *ap; 49352085Sbostic struct inode *ip; 49451915Sbostic daddr_t daddr, lbn, off; 49551860Sbostic int db_per_fsb, error, i, num; 49651188Sbostic 49751860Sbostic #ifdef VERBOSE 49851860Sbostic printf("lfs_updatemeta\n"); 49951860Sbostic #endif 50051342Sbostic if (nblocks == 0) 50151215Sbostic return; 50251215Sbostic 50351915Sbostic /* Sort the blocks. */ 50452077Sbostic lfs_shellsort(bpp, lbp, nblocks); 50551215Sbostic 50651915Sbostic /* 50751915Sbostic * Assign disk addresses, and update references to the logical 50851915Sbostic * block and the segment usage information. 50951915Sbostic */ 51051860Sbostic db_per_fsb = fsbtodb(fs, 1); 51151915Sbostic for (i = nblocks; i--; ++bpp) { 51251915Sbostic lbn = *lbp++; 51351915Sbostic (*bpp)->b_blkno = off = fs->lfs_offset; 51451860Sbostic fs->lfs_offset += db_per_fsb; 51551215Sbostic 51651860Sbostic if (error = lfs_bmaparray(vp, lbn, &daddr, a, &num)) 51752085Sbostic panic("lfs_updatemeta: lfs_bmaparray %d", error); 51851860Sbostic ip = VTOI(vp); 51951860Sbostic switch (num) { 52051860Sbostic case 0: 52151915Sbostic ip->i_db[lbn] = off; 52251860Sbostic break; 52351860Sbostic case 1: 52451915Sbostic ip->i_ib[a[0].in_off] = off; 52551860Sbostic break; 52651860Sbostic default: 52751860Sbostic ap = &a[num - 1]; 52851860Sbostic if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 52951860Sbostic panic("lfs_updatemeta: bread bno %d", 53051860Sbostic ap->in_lbn); 53151915Sbostic bp->b_un.b_daddr[ap->in_off] = off; 53251342Sbostic lfs_bwrite(bp); 53351188Sbostic } 53451915Sbostic 53551915Sbostic /* Update segment usage information. */ 53651915Sbostic if (daddr != UNASSIGNED) { 53751915Sbostic LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp); 53851915Sbostic #ifdef DIAGNOSTIC 53951915Sbostic if (sup->su_nbytes < fs->lfs_bsize) 54051915Sbostic panic("lfs: negative bytes (segment %d)\n", 54151915Sbostic datosn(fs, daddr)); 54251915Sbostic #endif 54351915Sbostic sup->su_nbytes -= fs->lfs_bsize; 54452085Sbostic LFS_UBWRITE(bp); 54551915Sbostic } 54651188Sbostic } 54751188Sbostic } 54851188Sbostic 54951915Sbostic /* 55051915Sbostic * Start a new segment. 55151915Sbostic */ 55252077Sbostic void 55351915Sbostic lfs_initseg(fs, sp) 55451499Sbostic struct lfs *fs; 55552085Sbostic struct segment *sp; 55651188Sbostic { 55751915Sbostic SEGUSE *sup; 55851915Sbostic SEGSUM *ssp; 55951915Sbostic struct buf *bp; 56051915Sbostic daddr_t lbn, *lbnp; 56151215Sbostic 56251860Sbostic #ifdef VERBOSE 56351915Sbostic printf("lfs_initseg\n"); 56451860Sbostic #endif 56551915Sbostic /* Advance to the next segment. */ 56651927Sbostic if (!LFS_PARTIAL_FITS(fs)) { 567*52682Sstaelin /* Wake up any cleaning procs waiting on this file system. */ 568*52682Sstaelin wakeup((caddr_t)&fs->lfs_nextseg); 569*52682Sstaelin wakeup((caddr_t)&lfs_allclean_wakeup); 570*52682Sstaelin 57151927Sbostic lfs_newseg(fs); 57251927Sbostic fs->lfs_offset = fs->lfs_curseg; 57351915Sbostic sp->seg_number = datosn(fs, fs->lfs_curseg); 57451915Sbostic sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE; 57551915Sbostic 57651915Sbostic /* 57751927Sbostic * If the segment contains a superblock, update the offset 57851927Sbostic * and summary address to skip over it. 57951915Sbostic */ 58052077Sbostic LFS_SEGENTRY(sup, fs, sp->seg_number, bp); 58151927Sbostic if (sup->su_flags & SEGUSE_SUPERBLOCK) { 58251915Sbostic fs->lfs_offset += LFS_SBPAD / DEV_BSIZE; 58351915Sbostic sp->seg_bytes_left -= LFS_SBPAD; 58451215Sbostic } 58552085Sbostic brelse(bp); 58651915Sbostic } else { 58751915Sbostic sp->seg_number = datosn(fs, fs->lfs_curseg); 58851915Sbostic sp->seg_bytes_left = (fs->lfs_dbpseg - 58951915Sbostic (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE; 59051915Sbostic } 59151342Sbostic 59251915Sbostic sp->ibp = NULL; 59351915Sbostic sp->ninodes = 0; 59451342Sbostic 59551915Sbostic /* Get a new buffer for SEGSUM and enter it into the buffer list. */ 59651915Sbostic sp->cbpp = sp->bpp; 59751915Sbostic *sp->cbpp = lfs_newbuf(fs, sp, fs->lfs_offset, LFS_SUMMARY_SIZE); 59851915Sbostic sp->segsum = (*sp->cbpp)->b_un.b_addr; 59951915Sbostic ++sp->cbpp; 60051915Sbostic fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE; 60151342Sbostic 60251915Sbostic /* Set point to SEGSUM, initialize it. */ 60351915Sbostic ssp = sp->segsum; 60451915Sbostic ssp->ss_next = fs->lfs_nextseg; 60551915Sbostic ssp->ss_nfinfo = ssp->ss_ninos = 0; 60651342Sbostic 60751915Sbostic /* Set pointer to first FINFO, initialize it. */ 60852085Sbostic sp->fip = (struct finfo *)(sp->segsum + sizeof(SEGSUM)); 60951915Sbostic sp->fip->fi_nblocks = 0; 61051342Sbostic 61151915Sbostic sp->seg_bytes_left -= LFS_SUMMARY_SIZE; 61251915Sbostic sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM); 61351915Sbostic } 61451342Sbostic 61551915Sbostic /* 61651915Sbostic * Return the next segment to write. 61751915Sbostic */ 61852077Sbostic void 61951915Sbostic lfs_newseg(fs) 62051915Sbostic struct lfs *fs; 62151915Sbostic { 62251927Sbostic CLEANERINFO *cip; 62351915Sbostic SEGUSE *sup; 62451915Sbostic struct buf *bp; 62551927Sbostic int curseg, isdirty, sn; 62651915Sbostic 62751915Sbostic #ifdef VERBOSE 62851915Sbostic printf("lfs_newseg\n"); 62951915Sbostic #endif 63051927Sbostic /* 63151927Sbostic * Turn off the active bit for the current segment, turn on the 63251927Sbostic * active and dirty bits for the next segment, update the cleaner 63351927Sbostic * info. Set the current segment to the next segment, get a new 63451927Sbostic * next segment. 63551927Sbostic */ 63651927Sbostic LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_curseg), bp); 63751927Sbostic sup->su_flags &= ~SEGUSE_ACTIVE; 63852085Sbostic LFS_UBWRITE(bp); 63951927Sbostic 64051927Sbostic LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp); 64151927Sbostic sup->su_flags |= SEGUSE_ACTIVE | SEGUSE_DIRTY; 64252085Sbostic LFS_UBWRITE(bp); 64351927Sbostic 64451927Sbostic LFS_CLEANERINFO(cip, fs, bp); 64551927Sbostic --cip->clean; 64651927Sbostic ++cip->dirty; 64752085Sbostic LFS_UBWRITE(bp); 64851927Sbostic 64951927Sbostic fs->lfs_lastseg = fs->lfs_curseg; 65051927Sbostic fs->lfs_curseg = fs->lfs_nextseg; 65151927Sbostic for (sn = curseg = datosn(fs, fs->lfs_curseg);;) { 65251915Sbostic sn = (sn + 1) % fs->lfs_nseg; 65351927Sbostic if (sn == curseg) 65451915Sbostic panic("lfs_nextseg: no clean segments"); 65551915Sbostic LFS_SEGENTRY(sup, fs, sn, bp); 65651915Sbostic isdirty = sup->su_flags & SEGUSE_DIRTY; 65752085Sbostic brelse(bp); 65851915Sbostic if (!isdirty) 65951915Sbostic break; 66051915Sbostic } 66151927Sbostic fs->lfs_nextseg = sntoda(fs, sn); 66251188Sbostic } 66351188Sbostic 66452077Sbostic void 66551188Sbostic lfs_writeseg(fs, sp) 66651499Sbostic struct lfs *fs; 66752085Sbostic struct segment *sp; 66851188Sbostic { 66952085Sbostic struct buf **bpp, *bp; 67051188Sbostic SEGUSE *sup; 67152085Sbostic SEGSUM *ssp; 67251860Sbostic dev_t i_dev; 67351860Sbostic u_long *datap, *dp; 67451342Sbostic void *pmeta; 67552085Sbostic int flags, i, nblocks, s, (*strategy)__P((struct buf *)); 67651188Sbostic 67751860Sbostic #ifdef VERBOSE 67851860Sbostic printf("lfs_writeseg\n"); 67951860Sbostic #endif 68052085Sbostic if ((nblocks = sp->cbpp - sp->bpp) == 0) 68152085Sbostic return; 68252085Sbostic 68351188Sbostic /* 68452085Sbostic * Compute checksum across data and then across summary; the first 68552085Sbostic * block (the summary block) is skipped. Set the create time here 68652085Sbostic * so that it's guaranteed to be later than the inode mod times. 68751860Sbostic * 68851860Sbostic * XXX 68951860Sbostic * Fix this to do it inline, instead of malloc/copy. 69051188Sbostic */ 69151860Sbostic datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK); 69251915Sbostic for (bpp = sp->bpp, i = nblocks - 1; i--;) 69351915Sbostic *dp++ = (*++bpp)->b_un.b_words[0]; 69452085Sbostic ssp = (SEGSUM *)sp->segsum; 69552103Sbostic ssp->ss_create = time.tv_sec; 69652085Sbostic ssp->ss_datasum = cksum(datap, nblocks * sizeof(u_long)); 69752085Sbostic ssp->ss_sumsum = 69852085Sbostic cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum)); 69951927Sbostic free(datap, M_SEGMENT); 70051188Sbostic 70151188Sbostic /* 70251860Sbostic * When we gathered the blocks for I/O we did not mark them busy or 70351860Sbostic * remove them from the freelist. As we do this, turn off the B_LOCKED 70451860Sbostic * bit so the future brelse will put them on the LRU list, and add the 70551860Sbostic * B_CALL flags if we're doing a checkpoint so we can count I/O's. LFS 70651860Sbostic * requires that the super blocks (on checkpoint) be written after all 70751860Sbostic * the segment data. 70851188Sbostic */ 70951860Sbostic i_dev = VTOI(fs->lfs_ivnode)->i_dev; 71051860Sbostic strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op->vop_strategy; 71151301Sbostic 71251301Sbostic s = splbio(); 71351860Sbostic if (sp->seg_flags & SEGM_CKP) { 71451860Sbostic fs->lfs_iocount += nblocks; 71551915Sbostic flags = B_ASYNC | B_BUSY | B_CALL; 71651860Sbostic } else 71751915Sbostic flags = B_ASYNC | B_BUSY; 71851860Sbostic for (bpp = sp->bpp, i = nblocks; i--;) { 71951860Sbostic bp = *bpp++; 72051860Sbostic bp->b_flags |= flags; 72151915Sbostic bp->b_flags &= 72251915Sbostic ~(B_DONE | B_ERROR | B_READ | B_DELWRI | B_LOCKED); 72351860Sbostic bp->b_dev = i_dev; 72451860Sbostic bp->b_iodone = lfs_callback; 72551860Sbostic if (!(bp->b_flags & B_NOCACHE)) { 72651860Sbostic bremfree(bp); 72751860Sbostic reassignbuf(bp, bp->b_vp); 72851860Sbostic } 72951860Sbostic } 73051301Sbostic splx(s); 73152077Sbostic 73251860Sbostic for (bpp = sp->bpp, i = nblocks; i--;) 73351860Sbostic (strategy)(*bpp++); 734*52682Sstaelin 735*52682Sstaelin /* Update the segment usage information. */ 736*52682Sstaelin LFS_SEGENTRY(sup, fs, sp->seg_number, bp); 737*52682Sstaelin sup->su_nbytes += nblocks - 1 - 738*52682Sstaelin (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs) << fs->lfs_bshift; 739*52682Sstaelin sup->su_nbytes += ssp->ss_ninos * sizeof(struct dinode); 740*52682Sstaelin sup->su_lastmod = time.tv_sec; 741*52682Sstaelin LFS_UBWRITE(bp); 74251188Sbostic } 74351188Sbostic 74452077Sbostic void 74551860Sbostic lfs_writesuper(fs, sp) 74651499Sbostic struct lfs *fs; 74752085Sbostic struct segment *sp; 74851301Sbostic { 74952085Sbostic struct buf *bp; 75051860Sbostic dev_t i_dev; 75152085Sbostic int (*strategy) __P((struct buf *)); 75251301Sbostic 75351860Sbostic #ifdef VERBOSE 75451860Sbostic printf("lfs_writesuper\n"); 75551860Sbostic #endif 75651860Sbostic i_dev = VTOI(fs->lfs_ivnode)->i_dev; 75751860Sbostic strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op->vop_strategy; 75851356Sbostic 75951342Sbostic /* Checksum the superblock and copy it into a buffer. */ 76051499Sbostic fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum)); 76151860Sbostic bp = lfs_newbuf(fs, sp, fs->lfs_sboffs[0], LFS_SBPAD); 76251860Sbostic *bp->b_un.b_lfs = *fs; 76351215Sbostic 76451356Sbostic /* Write the first superblock (wait). */ 76551860Sbostic bp->b_dev = i_dev; 76651915Sbostic bp->b_flags |= B_BUSY; 76751860Sbostic bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI); 76851342Sbostic (strategy)(bp); 76951215Sbostic biowait(bp); 77051342Sbostic 77151356Sbostic /* Write the second superblock (don't wait). */ 77251215Sbostic bp->b_blkno = bp->b_lblkno = fs->lfs_sboffs[1]; 77351915Sbostic bp->b_flags |= B_ASYNC | B_BUSY; 77451860Sbostic bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI); 77551342Sbostic (strategy)(bp); 77651215Sbostic } 77751215Sbostic 77851342Sbostic /* 77951342Sbostic * Logical block number match routines used when traversing the dirty block 78051342Sbostic * chain. 78151342Sbostic */ 78252077Sbostic int 78352077Sbostic lfs_match_data(fs, bp) 78451860Sbostic struct lfs *fs; 78552085Sbostic struct buf *bp; 78651215Sbostic { 78751342Sbostic return (bp->b_lblkno >= 0); 78851215Sbostic } 78951215Sbostic 79052077Sbostic int 79152077Sbostic lfs_match_indir(fs, bp) 79251860Sbostic struct lfs *fs; 79352085Sbostic struct buf *bp; 79451215Sbostic { 79551860Sbostic int lbn; 79651860Sbostic 79751860Sbostic lbn = bp->b_lblkno; 79851860Sbostic return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0); 79951215Sbostic } 80051215Sbostic 80152077Sbostic int 80252077Sbostic lfs_match_dindir(fs, bp) 80351860Sbostic struct lfs *fs; 80452085Sbostic struct buf *bp; 80551215Sbostic { 80651860Sbostic int lbn; 80751860Sbostic 80851860Sbostic lbn = bp->b_lblkno; 80951860Sbostic return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1); 81051215Sbostic } 81151215Sbostic 81252077Sbostic int 81352077Sbostic lfs_match_tindir(fs, bp) 81451499Sbostic struct lfs *fs; 81552085Sbostic struct buf *bp; 81651342Sbostic { 81751860Sbostic int lbn; 81851342Sbostic 81951860Sbostic lbn = bp->b_lblkno; 82051860Sbostic return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2); 82151860Sbostic } 82251342Sbostic 82351860Sbostic /* 82451860Sbostic * Allocate a new buffer header. 82551860Sbostic */ 82652085Sbostic struct buf * 82751860Sbostic lfs_newbuf(fs, sp, daddr, size) 82851860Sbostic struct lfs *fs; 82952085Sbostic struct segment *sp; 83051860Sbostic daddr_t daddr; 83151860Sbostic size_t size; 83251860Sbostic { 83352085Sbostic struct buf *bp; 83451342Sbostic 83551860Sbostic #ifdef VERBOSE 83651860Sbostic printf("lfs_newbuf\n"); 83751860Sbostic #endif 83851860Sbostic bp = getnewbuf(); 83951860Sbostic bremhash(bp); 84051860Sbostic bgetvp(fs->lfs_ivnode, bp); 84151860Sbostic bp->b_bcount = 0; 84251860Sbostic bp->b_lblkno = daddr; 84351860Sbostic bp->b_blkno = daddr; 84451860Sbostic bp->b_error = 0; 84551860Sbostic bp->b_resid = 0; 84651860Sbostic allocbuf(bp, size); 84751860Sbostic bp->b_flags |= B_NOCACHE; 84851915Sbostic binshash(bp, &bfreelist[BQ_AGE]); 84951860Sbostic return (bp); 85051860Sbostic } 85151342Sbostic 85252077Sbostic int /* XXX should be void */ 85351860Sbostic lfs_callback(bp) 85452085Sbostic struct buf *bp; 85551860Sbostic { 85651860Sbostic struct lfs *fs; 85751342Sbostic 85851860Sbostic fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs; 85951860Sbostic #ifdef DIAGNOSTIC 86051860Sbostic if (fs->lfs_iocount == 0) 86151860Sbostic panic("lfs_callback: zero iocount\n"); 86251860Sbostic #endif 86351860Sbostic if (--fs->lfs_iocount == 0) 86452412Smckusick wakeup((caddr_t)&fs->lfs_iocount); 86551915Sbostic 86651860Sbostic brelse(bp); 86751860Sbostic } 86851342Sbostic 86951215Sbostic /* 87051188Sbostic * Shellsort (diminishing increment sort) from Data Structures and 87151188Sbostic * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290; 87251188Sbostic * see also Knuth Vol. 3, page 84. The increments are selected from 87351188Sbostic * formula (8), page 95. Roughly O(N^3/2). 87451188Sbostic */ 87551188Sbostic /* 87651188Sbostic * This is our own private copy of shellsort because we want to sort 87751188Sbostic * two parallel arrays (the array of buffer pointers and the array of 87851188Sbostic * logical block numbers) simultaneously. Note that we cast the array 87951188Sbostic * of logical block numbers to a unsigned in this routine so that the 88051188Sbostic * negative block numbers (meta data blocks) sort AFTER the data blocks. 88151188Sbostic */ 88252077Sbostic void 88352077Sbostic lfs_shellsort(bp_array, lb_array, nmemb) 88452085Sbostic struct buf **bp_array; 88551215Sbostic daddr_t *lb_array; 88651188Sbostic register int nmemb; 88751188Sbostic { 88851188Sbostic static int __rsshell_increments[] = { 4, 1, 0 }; 88951188Sbostic register int incr, *incrp, t1, t2; 89052085Sbostic struct buf *bp_temp; 89151188Sbostic u_long lb_temp; 89251188Sbostic 89351188Sbostic for (incrp = __rsshell_increments; incr = *incrp++;) 89451188Sbostic for (t1 = incr; t1 < nmemb; ++t1) 89551188Sbostic for (t2 = t1 - incr; t2 >= 0;) 89651188Sbostic if (lb_array[t2] > lb_array[t2 + incr]) { 89751188Sbostic lb_temp = lb_array[t2]; 89851188Sbostic lb_array[t2] = lb_array[t2 + incr]; 89951188Sbostic lb_array[t2 + incr] = lb_temp; 90051188Sbostic bp_temp = bp_array[t2]; 90151188Sbostic bp_array[t2] = bp_array[t2 + incr]; 90251188Sbostic bp_array[t2 + incr] = bp_temp; 90351188Sbostic t2 -= incr; 90451188Sbostic } else 90551188Sbostic break; 90651188Sbostic } 907