123399Smckusick /* 251498Sbostic * Copyright (c) 1986, 1989, 1991 Regents of the University of California. 337736Smckusick * All rights reserved. 423399Smckusick * 544537Sbostic * %sccs.include.redist.c% 637736Smckusick * 7*53144Sstaelin * @(#)lfs_inode.c 7.59 (Berkeley) 04/08/92 823399Smckusick */ 924Sbill 1051484Sbostic #include <sys/param.h> 1151484Sbostic #include <sys/systm.h> 1251484Sbostic #include <sys/mount.h> 1351484Sbostic #include <sys/proc.h> 1451484Sbostic #include <sys/file.h> 1551484Sbostic #include <sys/buf.h> 1651484Sbostic #include <sys/vnode.h> 1751484Sbostic #include <sys/kernel.h> 1851484Sbostic #include <sys/malloc.h> 1924Sbill 2051498Sbostic #include <ufs/ufs/quota.h> 2151498Sbostic #include <ufs/ufs/inode.h> 2251498Sbostic #include <ufs/ufs/ufsmount.h> 2351498Sbostic #include <ufs/ufs/ufs_extern.h> 2447571Skarels 2551498Sbostic #include <ufs/lfs/lfs.h> 2651498Sbostic #include <ufs/lfs/lfs_extern.h> 2724Sbill 2852834Sbostic static struct dinode *lfs_ifind __P((struct lfs *, ino_t, struct dinode *)); 2952834Sbostic 3051346Sbostic int 3151155Sbostic lfs_init() 3224Sbill { 3351857Sbostic #ifdef VERBOSE 3451857Sbostic printf("lfs_init\n"); 3551857Sbostic #endif 3651484Sbostic return (ufs_init()); 3724Sbill } 3824Sbill 3924Sbill /* 4051484Sbostic * Look up an LFS dinode number to find its incore vnode. If not already 4151484Sbostic * in core, read it in from the specified device. Return the inode locked. 4251484Sbostic * Detection and handling of mount points must be done by the calling routine. 4324Sbill */ 4451346Sbostic int 4551562Smckusick lfs_vget(mntp, ino, vpp) 4651562Smckusick struct mount *mntp; 474818Swnj ino_t ino; 4851562Smckusick struct vnode **vpp; 4924Sbill { 5051498Sbostic register struct lfs *fs; 5151484Sbostic register struct inode *ip; 5251484Sbostic struct buf *bp; 5352224Sbostic struct ifile *ifp; 5451498Sbostic struct vnode *vp; 5551562Smckusick struct ufsmount *ump; 5652224Sbostic daddr_t daddr; 5751484Sbostic dev_t dev; 5851346Sbostic int error; 5924Sbill 6051857Sbostic #ifdef VERBOSE 6151857Sbostic printf("lfs_vget\n"); 6251857Sbostic #endif 6351562Smckusick ump = VFSTOUFS(mntp); 6451562Smckusick dev = ump->um_dev; 6551562Smckusick if ((*vpp = ufs_ihashget(dev, ino)) != NULL) 6651484Sbostic return (0); 6751484Sbostic 6852224Sbostic /* Translate the inode number to a disk address. */ 6952224Sbostic fs = ump->um_lfs; 7052224Sbostic if (ino == LFS_IFILE_INUM) 7152224Sbostic daddr = fs->lfs_idaddr; 7252224Sbostic else { 7352224Sbostic LFS_IENTRY(ifp, fs, ino, bp); 7452224Sbostic daddr = ifp->if_daddr; 7552224Sbostic brelse(bp); 7652224Sbostic if (daddr == LFS_UNUSED_DADDR) 7752224Sbostic return (ENOENT); 7852224Sbostic } 7952224Sbostic 8051155Sbostic /* Allocate new vnode/inode. */ 8151498Sbostic if (error = lfs_vcreate(mntp, ino, &vp)) { 8251562Smckusick *vpp = NULL; 8337736Smckusick return (error); 8437736Smckusick } 8552224Sbostic 8637736Smckusick /* 8739440Smckusick * Put it onto its hash chain and lock it so that other requests for 8839440Smckusick * this inode will block if they arrive while we are sleeping waiting 8939440Smckusick * for old data structures to be purged or for the contents of the 9039440Smckusick * disk portion of this inode to be read. 9139440Smckusick */ 9251562Smckusick ip = VTOI(vp); 9351484Sbostic ufs_ihashins(ip); 9451155Sbostic 9552224Sbostic /* 9652224Sbostic * XXX 9752224Sbostic * This may not need to be here, logically it should go down with 9852224Sbostic * the i_devvp initialization. 9952224Sbostic * Ask Kirk. 10052224Sbostic */ 10152224Sbostic ip->i_lfs = ump->um_lfs; 10252224Sbostic 10351484Sbostic /* Read in the disk contents for the inode, copy into the inode. */ 10452224Sbostic if (error = 10552224Sbostic bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp)) { 10637736Smckusick /* 10751857Sbostic * The inode does not contain anything useful, so it 10851857Sbostic * would be misleading to leave it on its hash chain. 10951857Sbostic * Iput() will return it to the free list. 11041334Smckusick */ 11141334Smckusick remque(ip); 11241334Smckusick ip->i_forw = ip; 11341334Smckusick ip->i_back = ip; 11451484Sbostic 11551484Sbostic /* Unlock and discard unneeded inode. */ 11651484Sbostic ufs_iput(ip); 11737736Smckusick brelse(bp); 11851562Smckusick *vpp = NULL; 11939440Smckusick return (error); 12037736Smckusick } 12151155Sbostic ip->i_din = *lfs_ifind(fs, ino, bp->b_un.b_dino); 12239440Smckusick brelse(bp); 12351155Sbostic 12451498Sbostic /* 12551498Sbostic * Initialize the vnode from the inode, check for aliases. In all 12651498Sbostic * cases re-init ip, the underlying vnode/inode may have changed. 12751498Sbostic */ 12851595Smckusick if (error = ufs_vinit(mntp, &lfs_specops, LFS_FIFOOPS, &vp)) { 12951484Sbostic ufs_iput(ip); 13051562Smckusick *vpp = NULL; 13151484Sbostic return (error); 13240289Smckusick } 13351562Smckusick /* 13451562Smckusick * Finish inode initialization now that aliasing has been resolved. 13551562Smckusick */ 13651562Smckusick ip->i_devvp = ump->um_devvp; 13751562Smckusick VREF(ip->i_devvp); 13851562Smckusick *vpp = vp; 13937736Smckusick return (0); 14037736Smckusick } 1417334Skre 14252834Sbostic /* Search a block for a specific dinode. */ 14352834Sbostic static struct dinode * 14452834Sbostic lfs_ifind(fs, ino, dip) 14552834Sbostic struct lfs *fs; 14652834Sbostic ino_t ino; 14752834Sbostic register struct dinode *dip; 14852834Sbostic { 14952834Sbostic register int cnt; 15052834Sbostic 15152834Sbostic #ifdef VERBOSE 15252834Sbostic printf("lfs_ifind: inode %d\n", ino); 15352834Sbostic #endif 15452834Sbostic for (cnt = INOPB(fs); cnt--; ++dip) 15552834Sbostic if (dip->di_inum == ino) 15652834Sbostic return (dip); 15752834Sbostic 15852834Sbostic panic("lfs_ifind: dinode %u not found", ino); 15952834Sbostic /* NOTREACHED */ 16052834Sbostic } 16152834Sbostic 16251346Sbostic int 16351562Smckusick lfs_update(vp, ta, tm, waitfor) 16451562Smckusick register struct vnode *vp; 16551484Sbostic struct timeval *ta, *tm; 16651484Sbostic int waitfor; 1677118Smckusick { 16851562Smckusick struct inode *ip; 16951562Smckusick 17051857Sbostic #ifdef VERBOSE 17151857Sbostic printf("lfs_update\n"); 17251857Sbostic #endif 17351562Smckusick if (vp->v_mount->mnt_flag & MNT_RDONLY) 17451562Smckusick return (0); 17551562Smckusick ip = VTOI(vp); 17651562Smckusick if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0) 17751562Smckusick return (0); 17851562Smckusick if (ip->i_flag&IACC) 17951562Smckusick ip->i_atime = ta->tv_sec; 18052018Smckusick if (ip->i_flag&IUPD) { 18151562Smckusick ip->i_mtime = tm->tv_sec; 18252018Smckusick INCRQUAD((ip)->i_modrev); 18352018Smckusick } 18451562Smckusick if (ip->i_flag&ICHG) 18551562Smckusick ip->i_ctime = time.tv_sec; 18651562Smckusick ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD); 18751562Smckusick 18852327Sbostic /* Push back the vnode and any dirty blocks it may have. */ 18952327Sbostic return (waitfor ? lfs_vflush(vp) : 0); 19024Sbill } 19124Sbill 19252222Sbostic /* Update segment usage information when removing a block. */ 19352225Sbostic #define UPDATE_SEGUSE \ 19452225Sbostic if (lastseg != -1) { \ 19552225Sbostic LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \ 19652681Sstaelin sup->su_nbytes -= num << fs->lfs_bshift; \ 19752225Sbostic LFS_UBWRITE(sup_bp); \ 19852225Sbostic blocksreleased += num; \ 19952225Sbostic } 20052222Sbostic 20152222Sbostic #define SEGDEC { \ 20252222Sbostic if (daddr != UNASSIGNED) { \ 20352222Sbostic if (lastseg != (seg = datosn(fs, daddr))) { \ 20452222Sbostic UPDATE_SEGUSE; \ 20552222Sbostic num = 1; \ 20652222Sbostic lastseg = seg; \ 20752222Sbostic } else \ 20852222Sbostic ++num; \ 20952222Sbostic } \ 21052222Sbostic } 21152222Sbostic 21224Sbill /* 21352222Sbostic * Truncate the inode ip to at most length size. Update segment usage 21452222Sbostic * table information. 21524Sbill */ 21651484Sbostic /* ARGSUSED */ 21751484Sbostic int 21852222Sbostic lfs_truncate(vp, length, flags) 21952222Sbostic struct vnode *vp; 2209165Ssam u_long length; 22139676Smckusick int flags; 22224Sbill { 22352222Sbostic register INDIR *ap; 22452222Sbostic register int i; 22552222Sbostic register daddr_t *daddrp; 22652225Sbostic struct buf *bp, *sup_bp; 22752327Sbostic struct ifile *ifp; 22852222Sbostic struct inode *ip; 22952222Sbostic struct lfs *fs; 23052222Sbostic INDIR a[NIADDR + 2], a_end[NIADDR + 2]; 23152222Sbostic SEGUSE *sup; 23252222Sbostic daddr_t daddr, lastblock, lbn, olastblock; 23352222Sbostic off_t off; 23452222Sbostic long blocksreleased; 23552222Sbostic int error, depth, lastseg, num, offset, seg, size; 2369165Ssam 23751857Sbostic #ifdef VERBOSE 23851857Sbostic printf("lfs_truncate\n"); 23951857Sbostic #endif 24052222Sbostic vnode_pager_setsize(vp, length); 24152327Sbostic 24252222Sbostic ip = VTOI(vp); 24352327Sbostic fs = ip->i_lfs; 24452327Sbostic 24552327Sbostic /* If truncating the file to 0, update the version number. */ 24652327Sbostic if (length == 0) { 24752327Sbostic LFS_IENTRY(ifp, fs, ip->i_number, bp); 24852327Sbostic ++ifp->if_version; 24952327Sbostic LFS_UBWRITE(bp); 25052327Sbostic } 25152327Sbostic 25251484Sbostic /* If length is larger than the file, just update the times. */ 25352222Sbostic if (ip->i_size <= length) { 25452222Sbostic ip->i_flag |= ICHG|IUPD; 25552222Sbostic return (lfs_update(vp, &time, &time, 1)); 25613000Ssam } 25752327Sbostic 25852222Sbostic /* 25952222Sbostic * Calculate index into inode's block list of last direct and indirect 26052222Sbostic * blocks (if any) which we want to keep. Lastblock is 0 when the 26152222Sbostic * file is truncated to 0. 26252222Sbostic */ 26352222Sbostic lastblock = lblkno(fs, length + fs->lfs_bsize - 1); 26452222Sbostic olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1; 26551484Sbostic 2661203Sbill /* 26751484Sbostic * Update the size of the file. If the file is not being truncated to 26851484Sbostic * a block boundry, the contents of the partial block following the end 26951484Sbostic * of the file must be zero'ed in case it ever become accessable again 27051484Sbostic * because of subsequent file growth. 2711203Sbill */ 27217942Smckusick offset = blkoff(fs, length); 27351484Sbostic if (offset == 0) 27452222Sbostic ip->i_size = length; 27551484Sbostic else { 27617942Smckusick lbn = lblkno(fs, length); 27741313Smckusick #ifdef QUOTA 27852222Sbostic if (error = getinoquota(ip)) 27941313Smckusick return (error); 28051183Sbostic #endif 28152222Sbostic if (error = bread(vp, lbn, fs->lfs_bsize, NOCRED, &bp)) 28237736Smckusick return (error); 28352222Sbostic ip->i_size = length; 28451857Sbostic size = blksize(fs); 28552222Sbostic (void)vnode_pager_uncache(vp); 28626272Skarels bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 28745112Smckusick allocbuf(bp, size); 28852078Sbostic LFS_UBWRITE(bp); 28917942Smckusick } 29052078Sbostic /* 29152222Sbostic * Modify sup->su_nbyte counters for each deleted block; keep track 29252222Sbostic * of number of blocks removed for ip->i_blocks. 29352222Sbostic */ 29452222Sbostic blocksreleased = 0; 29552222Sbostic num = 0; 29652222Sbostic lastseg = -1; 29752222Sbostic 29852222Sbostic for (lbn = olastblock; lbn >= lastblock;) { 29952222Sbostic lfs_bmaparray(vp, lbn, &daddr, a, &depth); 30052222Sbostic if (lbn == olastblock) 30152222Sbostic for (i = NIADDR + 2; i--;) 30252222Sbostic a_end[i] = a[i]; 30352222Sbostic switch (depth) { 30452222Sbostic case 0: /* Direct block. */ 30552222Sbostic daddr = ip->i_db[lbn]; 30652222Sbostic SEGDEC; 30752222Sbostic ip->i_db[lbn] = 0; 30852222Sbostic --lbn; 30952222Sbostic break; 31052222Sbostic #ifdef DIAGNOSTIC 31152222Sbostic case 1: /* An indirect block. */ 31252222Sbostic panic("lfs_truncate: lfs_bmaparray returned depth 1"); 31352222Sbostic /* NOTREACHED */ 31452222Sbostic #endif 31552222Sbostic default: /* Chain of indirect blocks. */ 31652222Sbostic ap = a + --depth; 31752222Sbostic if (ap->in_off > 0 && lbn != lastblock) { 31852222Sbostic lbn -= ap->in_off < lbn - lastblock ? 31952222Sbostic ap->in_off : lbn - lastblock; 32052222Sbostic break; 32152222Sbostic } 32252222Sbostic for (; depth && (ap->in_off == 0 || lbn == lastblock); 32352222Sbostic --ap, --depth) { 32452222Sbostic /* 32552222Sbostic * XXX 32652222Sbostic * The indirect block may not yet exist, so 32752222Sbostic * bread will create one just so we can free 32852222Sbostic * it. 32952222Sbostic */ 33052222Sbostic if (bread(vp, 33152222Sbostic ap->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 33252222Sbostic panic("lfs_truncate: bread bno %d", 33352222Sbostic ap->in_lbn); 33452222Sbostic daddrp = bp->b_un.b_daddr + ap->in_off; 33552222Sbostic for (i = ap->in_off; 33652222Sbostic i++ <= a_end[depth].in_off;) { 33752222Sbostic daddr = *daddrp++; 33852222Sbostic SEGDEC; 33952222Sbostic } 340*53144Sstaelin a_end[depth].in_off = NINDIR(fs) - 1; 341*53144Sstaelin if (ap->in_off == 0) 342*53144Sstaelin brelse (bp); 343*53144Sstaelin else { 34452222Sbostic bzero(bp->b_un.b_daddr + ap->in_off, 34552222Sbostic fs->lfs_bsize - 34652222Sbostic ap->in_off * sizeof(daddr_t)); 34752222Sbostic LFS_UBWRITE(bp); 348*53144Sstaelin } 34952222Sbostic } 350*53144Sstaelin if (depth == 0 && a[1].in_off == 0) { 35152222Sbostic off = a[0].in_off; 35252222Sbostic daddr = ip->i_ib[off]; 35352222Sbostic SEGDEC; 35452222Sbostic ip->i_ib[off] = 0; 35552222Sbostic } 35652681Sstaelin if (lbn == lastblock || lbn <= NDADDR) 35752222Sbostic --lbn; 35852222Sbostic else { 35952222Sbostic lbn -= NINDIR(fs); 36052222Sbostic if (lbn < lastblock) 36152222Sbostic lbn = lastblock; 36252222Sbostic } 36352222Sbostic } 36452222Sbostic } 36552225Sbostic UPDATE_SEGUSE; 36652222Sbostic ip->i_blocks -= blocksreleased; 36752222Sbostic /* 36852078Sbostic * XXX 36952222Sbostic * Currently, we don't know when we allocate an indirect block, so 37052222Sbostic * ip->i_blocks isn't getting incremented appropriately. As a result, 37152222Sbostic * when we delete any indirect blocks, we get a bad number here. 37252078Sbostic */ 37352222Sbostic if (ip->i_blocks < 0) 37452222Sbostic ip->i_blocks = 0; 37552222Sbostic ip->i_flag |= ICHG|IUPD; 37652222Sbostic (void)vinvalbuf(vp, length > 0); 37752222Sbostic error = lfs_update(vp, &time, &time, MNT_WAIT); 37851857Sbostic return (0); 37924Sbill } 380