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*52224Sbostic * @(#)lfs_inode.c 7.54 (Berkeley) 01/18/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 2851346Sbostic int 2951155Sbostic lfs_init() 3024Sbill { 3151857Sbostic #ifdef VERBOSE 3251857Sbostic printf("lfs_init\n"); 3351857Sbostic #endif 3451484Sbostic return (ufs_init()); 3524Sbill } 3624Sbill 3724Sbill /* 3851484Sbostic * Look up an LFS dinode number to find its incore vnode. If not already 3951484Sbostic * in core, read it in from the specified device. Return the inode locked. 4051484Sbostic * Detection and handling of mount points must be done by the calling routine. 4124Sbill */ 4251346Sbostic int 4351562Smckusick lfs_vget(mntp, ino, vpp) 4451562Smckusick struct mount *mntp; 454818Swnj ino_t ino; 4651562Smckusick struct vnode **vpp; 4724Sbill { 4851498Sbostic register struct lfs *fs; 4951484Sbostic register struct inode *ip; 5051484Sbostic struct buf *bp; 51*52224Sbostic struct ifile *ifp; 5251498Sbostic struct vnode *vp; 5351562Smckusick struct ufsmount *ump; 54*52224Sbostic daddr_t daddr; 5551484Sbostic dev_t dev; 5651346Sbostic int error; 5724Sbill 5851857Sbostic #ifdef VERBOSE 5951857Sbostic printf("lfs_vget\n"); 6051857Sbostic #endif 6151562Smckusick ump = VFSTOUFS(mntp); 6251562Smckusick dev = ump->um_dev; 6351562Smckusick if ((*vpp = ufs_ihashget(dev, ino)) != NULL) 6451484Sbostic return (0); 6551484Sbostic 66*52224Sbostic /* Translate the inode number to a disk address. */ 67*52224Sbostic fs = ump->um_lfs; 68*52224Sbostic if (ino == LFS_IFILE_INUM) 69*52224Sbostic daddr = fs->lfs_idaddr; 70*52224Sbostic else { 71*52224Sbostic LFS_IENTRY(ifp, fs, ino, bp); 72*52224Sbostic daddr = ifp->if_daddr; 73*52224Sbostic brelse(bp); 74*52224Sbostic if (daddr == LFS_UNUSED_DADDR) 75*52224Sbostic return (ENOENT); 76*52224Sbostic } 77*52224Sbostic 7851155Sbostic /* Allocate new vnode/inode. */ 7951498Sbostic if (error = lfs_vcreate(mntp, ino, &vp)) { 8051562Smckusick *vpp = NULL; 8137736Smckusick return (error); 8237736Smckusick } 83*52224Sbostic 8437736Smckusick /* 8539440Smckusick * Put it onto its hash chain and lock it so that other requests for 8639440Smckusick * this inode will block if they arrive while we are sleeping waiting 8739440Smckusick * for old data structures to be purged or for the contents of the 8839440Smckusick * disk portion of this inode to be read. 8939440Smckusick */ 9051562Smckusick ip = VTOI(vp); 9151484Sbostic ufs_ihashins(ip); 9251155Sbostic 93*52224Sbostic /* 94*52224Sbostic * XXX 95*52224Sbostic * This may not need to be here, logically it should go down with 96*52224Sbostic * the i_devvp initialization. 97*52224Sbostic * Ask Kirk. 98*52224Sbostic */ 99*52224Sbostic ip->i_lfs = ump->um_lfs; 100*52224Sbostic 10151484Sbostic /* Read in the disk contents for the inode, copy into the inode. */ 102*52224Sbostic if (error = 103*52224Sbostic bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp)) { 10437736Smckusick /* 10551857Sbostic * The inode does not contain anything useful, so it 10651857Sbostic * would be misleading to leave it on its hash chain. 10751857Sbostic * Iput() will return it to the free list. 10841334Smckusick */ 10941334Smckusick remque(ip); 11041334Smckusick ip->i_forw = ip; 11141334Smckusick ip->i_back = ip; 11251484Sbostic 11351484Sbostic /* Unlock and discard unneeded inode. */ 11451484Sbostic ufs_iput(ip); 11537736Smckusick brelse(bp); 11651562Smckusick *vpp = NULL; 11739440Smckusick return (error); 11837736Smckusick } 11951155Sbostic ip->i_din = *lfs_ifind(fs, ino, bp->b_un.b_dino); 12039440Smckusick brelse(bp); 12151155Sbostic 12251498Sbostic /* 12351498Sbostic * Initialize the vnode from the inode, check for aliases. In all 12451498Sbostic * cases re-init ip, the underlying vnode/inode may have changed. 12551498Sbostic */ 12651595Smckusick if (error = ufs_vinit(mntp, &lfs_specops, LFS_FIFOOPS, &vp)) { 12751484Sbostic ufs_iput(ip); 12851562Smckusick *vpp = NULL; 12951484Sbostic return (error); 13040289Smckusick } 13151562Smckusick /* 13251562Smckusick * Finish inode initialization now that aliasing has been resolved. 13351562Smckusick */ 13451562Smckusick ip->i_devvp = ump->um_devvp; 13551562Smckusick VREF(ip->i_devvp); 13651562Smckusick *vpp = vp; 13737736Smckusick return (0); 13837736Smckusick } 1397334Skre 14051346Sbostic int 14151562Smckusick lfs_update(vp, ta, tm, waitfor) 14251562Smckusick register struct vnode *vp; 14351484Sbostic struct timeval *ta, *tm; 14451484Sbostic int waitfor; 1457118Smckusick { 14651562Smckusick struct inode *ip; 14751562Smckusick 14851857Sbostic #ifdef VERBOSE 14951857Sbostic printf("lfs_update\n"); 15051857Sbostic #endif 15151562Smckusick if (vp->v_mount->mnt_flag & MNT_RDONLY) 15251562Smckusick return (0); 15351562Smckusick ip = VTOI(vp); 15451562Smckusick if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0) 15551562Smckusick return (0); 15651562Smckusick if (ip->i_flag&IACC) 15751562Smckusick ip->i_atime = ta->tv_sec; 15852018Smckusick if (ip->i_flag&IUPD) { 15951562Smckusick ip->i_mtime = tm->tv_sec; 16052018Smckusick INCRQUAD((ip)->i_modrev); 16152018Smckusick } 16251562Smckusick if (ip->i_flag&ICHG) 16351562Smckusick ip->i_ctime = time.tv_sec; 16451562Smckusick ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD); 16551562Smckusick 16638452Smckusick /* 16751484Sbostic * XXX 16851562Smckusick * I'm not real sure what to do here; once we have fsync and partial 16951562Smckusick * segments working in the LFS context, this must be fixed to be 17051562Smckusick * correct. The contents of the inode have to be pushed back to 17152078Sbostic * stable storage. 17238452Smckusick */ 17351484Sbostic return (0); 17424Sbill } 17524Sbill 17652222Sbostic /* Update segment usage information when removing a block. */ 17752222Sbostic #define UPDATE_SEGUSE { \ 17852222Sbostic LFS_SEGENTRY(sup, fs, lastseg, bp); \ 17952222Sbostic sup->su_nbytes -= fs->lfs_bsize * num; \ 18052222Sbostic LFS_UBWRITE(bp); \ 18152222Sbostic blocksreleased += num; \ 18252222Sbostic } 18352222Sbostic 18452222Sbostic #define SEGDEC { \ 18552222Sbostic if (daddr != UNASSIGNED) { \ 18652222Sbostic if (lastseg != (seg = datosn(fs, daddr))) { \ 18752222Sbostic UPDATE_SEGUSE; \ 18852222Sbostic num = 1; \ 18952222Sbostic lastseg = seg; \ 19052222Sbostic } else \ 19152222Sbostic ++num; \ 19252222Sbostic } \ 19352222Sbostic } 19452222Sbostic 19524Sbill /* 19652222Sbostic * Truncate the inode ip to at most length size. Update segment usage 19752222Sbostic * table information. 19824Sbill */ 19951484Sbostic /* ARGSUSED */ 20051484Sbostic int 20152222Sbostic lfs_truncate(vp, length, flags) 20252222Sbostic struct vnode *vp; 2039165Ssam u_long length; 20439676Smckusick int flags; 20524Sbill { 20652222Sbostic register INDIR *ap; 20752222Sbostic register int i; 20852222Sbostic register daddr_t *daddrp; 20917942Smckusick struct buf *bp; 21052222Sbostic struct inode *ip; 21152222Sbostic struct lfs *fs; 21252222Sbostic INDIR a[NIADDR + 2], a_end[NIADDR + 2]; 21352222Sbostic SEGUSE *sup; 21452222Sbostic daddr_t daddr, lastblock, lbn, olastblock; 21552222Sbostic off_t off; 21652222Sbostic long blocksreleased; 21752222Sbostic int error, depth, lastseg, num, offset, seg, size; 2189165Ssam 21951857Sbostic #ifdef VERBOSE 22051857Sbostic printf("lfs_truncate\n"); 22151857Sbostic #endif 22252222Sbostic vnode_pager_setsize(vp, length); 22352222Sbostic ip = VTOI(vp); 22451484Sbostic /* If length is larger than the file, just update the times. */ 22552222Sbostic if (ip->i_size <= length) { 22652222Sbostic ip->i_flag |= ICHG|IUPD; 22752222Sbostic return (lfs_update(vp, &time, &time, 1)); 22813000Ssam } 22952222Sbostic /* 23052222Sbostic * Calculate index into inode's block list of last direct and indirect 23152222Sbostic * blocks (if any) which we want to keep. Lastblock is 0 when the 23252222Sbostic * file is truncated to 0. 23352222Sbostic */ 23452222Sbostic fs = ip->i_lfs; 23552222Sbostic lastblock = lblkno(fs, length + fs->lfs_bsize - 1); 23652222Sbostic olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1; 23751484Sbostic 2381203Sbill /* 23951484Sbostic * Update the size of the file. If the file is not being truncated to 24051484Sbostic * a block boundry, the contents of the partial block following the end 24151484Sbostic * of the file must be zero'ed in case it ever become accessable again 24251484Sbostic * because of subsequent file growth. 2431203Sbill */ 24417942Smckusick offset = blkoff(fs, length); 24551484Sbostic if (offset == 0) 24652222Sbostic ip->i_size = length; 24751484Sbostic else { 24817942Smckusick lbn = lblkno(fs, length); 24941313Smckusick #ifdef QUOTA 25052222Sbostic if (error = getinoquota(ip)) 25141313Smckusick return (error); 25251183Sbostic #endif 25352222Sbostic if (error = bread(vp, lbn, fs->lfs_bsize, NOCRED, &bp)) 25437736Smckusick return (error); 25552222Sbostic ip->i_size = length; 25651857Sbostic size = blksize(fs); 25752222Sbostic (void)vnode_pager_uncache(vp); 25826272Skarels bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 25945112Smckusick allocbuf(bp, size); 26052078Sbostic LFS_UBWRITE(bp); 26117942Smckusick } 26252078Sbostic /* 26352222Sbostic * Modify sup->su_nbyte counters for each deleted block; keep track 26452222Sbostic * of number of blocks removed for ip->i_blocks. 26552222Sbostic */ 26652222Sbostic blocksreleased = 0; 26752222Sbostic num = 0; 26852222Sbostic lastseg = -1; 26952222Sbostic 27052222Sbostic for (lbn = olastblock; lbn >= lastblock;) { 27152222Sbostic lfs_bmaparray(vp, lbn, &daddr, a, &depth); 27252222Sbostic if (lbn == olastblock) 27352222Sbostic for (i = NIADDR + 2; i--;) 27452222Sbostic a_end[i] = a[i]; 27552222Sbostic switch (depth) { 27652222Sbostic case 0: /* Direct block. */ 27752222Sbostic daddr = ip->i_db[lbn]; 27852222Sbostic SEGDEC; 27952222Sbostic ip->i_db[lbn] = 0; 28052222Sbostic --lbn; 28152222Sbostic break; 28252222Sbostic #ifdef DIAGNOSTIC 28352222Sbostic case 1: /* An indirect block. */ 28452222Sbostic panic("lfs_truncate: lfs_bmaparray returned depth 1"); 28552222Sbostic /* NOTREACHED */ 28652222Sbostic #endif 28752222Sbostic default: /* Chain of indirect blocks. */ 28852222Sbostic ap = a + --depth; 28952222Sbostic if (ap->in_off > 0 && lbn != lastblock) { 29052222Sbostic lbn -= ap->in_off < lbn - lastblock ? 29152222Sbostic ap->in_off : lbn - lastblock; 29252222Sbostic break; 29352222Sbostic } 29452222Sbostic for (; depth && (ap->in_off == 0 || lbn == lastblock); 29552222Sbostic --ap, --depth) { 29652222Sbostic /* 29752222Sbostic * XXX 29852222Sbostic * The indirect block may not yet exist, so 29952222Sbostic * bread will create one just so we can free 30052222Sbostic * it. 30152222Sbostic */ 30252222Sbostic if (bread(vp, 30352222Sbostic ap->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 30452222Sbostic panic("lfs_truncate: bread bno %d", 30552222Sbostic ap->in_lbn); 30652222Sbostic daddrp = bp->b_un.b_daddr + ap->in_off; 30752222Sbostic for (i = ap->in_off; 30852222Sbostic i++ <= a_end[depth].in_off;) { 30952222Sbostic daddr = *daddrp++; 31052222Sbostic SEGDEC; 31152222Sbostic } 31252222Sbostic a_end[depth].in_off=NINDIR(fs)-1; 31352222Sbostic if (ap->in_off > 0 && lbn == lastblock) { 31452222Sbostic bzero(bp->b_un.b_daddr + ap->in_off, 31552222Sbostic fs->lfs_bsize - 31652222Sbostic ap->in_off * sizeof(daddr_t)); 31752222Sbostic LFS_UBWRITE(bp); 31852222Sbostic } else 31952222Sbostic brelse (bp); 32052222Sbostic } 32152222Sbostic if (a[1].in_off == 0) { 32252222Sbostic off = a[0].in_off; 32352222Sbostic daddr = ip->i_ib[off]; 32452222Sbostic SEGDEC; 32552222Sbostic ip->i_ib[off] = 0; 32652222Sbostic } 32752222Sbostic if (lbn == lastblock) 32852222Sbostic --lbn; 32952222Sbostic else { 33052222Sbostic lbn -= NINDIR(fs); 33152222Sbostic if (lbn < lastblock) 33252222Sbostic lbn = lastblock; 33352222Sbostic } 33452222Sbostic } 33552222Sbostic } 33652222Sbostic if (lastseg != -1) 33752222Sbostic UPDATE_SEGUSE; 33852222Sbostic 33952222Sbostic ip->i_blocks -= blocksreleased; 34052222Sbostic /* 34152078Sbostic * XXX 34252222Sbostic * Currently, we don't know when we allocate an indirect block, so 34352222Sbostic * ip->i_blocks isn't getting incremented appropriately. As a result, 34452222Sbostic * when we delete any indirect blocks, we get a bad number here. 34552078Sbostic */ 34652222Sbostic if (ip->i_blocks < 0) 34752222Sbostic ip->i_blocks = 0; 34852222Sbostic ip->i_flag |= ICHG|IUPD; 34952222Sbostic (void)vinvalbuf(vp, length > 0); 35052222Sbostic error = lfs_update(vp, &time, &time, MNT_WAIT); 35151857Sbostic return (0); 35224Sbill } 353