123394Smckusick /* 263375Sbostic * Copyright (c) 1991, 1993 363375Sbostic * The Regents of the University of California. All rights reserved. 423394Smckusick * 544536Sbostic * %sccs.include.redist.c% 634432Sbostic * 7*64609Sbostic * @(#)lfs_alloc.c 8.3 (Berkeley) 09/23/93 823394Smckusick */ 94359Smckusick 1051485Sbostic #include <sys/param.h> 1151485Sbostic #include <sys/kernel.h> 1251485Sbostic #include <sys/buf.h> 1351485Sbostic #include <sys/vnode.h> 1451485Sbostic #include <sys/syslog.h> 1551485Sbostic #include <sys/mount.h> 1651990Smckusick #include <sys/malloc.h> 174359Smckusick 1853320Smckusick #include <vm/vm.h> 1953320Smckusick 2051492Sbostic #include <ufs/ufs/quota.h> 2151492Sbostic #include <ufs/ufs/inode.h> 2251492Sbostic #include <ufs/ufs/ufsmount.h> 2347571Skarels 2451492Sbostic #include <ufs/lfs/lfs.h> 2551492Sbostic #include <ufs/lfs/lfs_extern.h> 2651485Sbostic 2751492Sbostic extern u_long nextgennumber; 2851492Sbostic 2951485Sbostic /* Allocate a new inode. */ 3051485Sbostic /* ARGSUSED */ 3151485Sbostic int 3254686Smargo lfs_valloc(ap) 3354686Smargo struct vop_valloc_args /* { 3454686Smargo struct vnode *a_pvp; 3554686Smargo int a_mode; 3654686Smargo struct ucred *a_cred; 3754686Smargo struct vnode **a_vpp; 3854686Smargo } */ *ap; 3951155Sbostic { 4051492Sbostic struct lfs *fs; 4152080Sbostic struct buf *bp; 4252080Sbostic struct ifile *ifp; 4352080Sbostic struct inode *ip; 4452080Sbostic struct vnode *vp; 4552080Sbostic daddr_t blkno; 4651155Sbostic ino_t new_ino; 4752080Sbostic u_long i, max; 4856157Smargo int bb, error; 494359Smckusick 5051215Sbostic /* Get the head of the freelist. */ 5153591Sheideman fs = VTOI(ap->a_pvp)->i_lfs; 5251155Sbostic new_ino = fs->lfs_free; 5351485Sbostic #ifdef ALLOCPRINT 5451485Sbostic printf("lfs_ialloc: allocate inode %d\n", new_ino); 5551485Sbostic #endif 564359Smckusick 5751933Sbostic /* 5852080Sbostic * Remove the inode from the free list and write the new start 5952080Sbostic * of the free list into the superblock. 6051933Sbostic */ 6151155Sbostic LFS_IENTRY(ifp, fs, new_ino, bp); 6251155Sbostic if (ifp->if_daddr != LFS_UNUSED_DADDR) 6351215Sbostic panic("lfs_ialloc: inuse inode on the free list"); 6451183Sbostic fs->lfs_free = ifp->if_nextfree; 6552080Sbostic brelse(bp); 664426Smckusic 6752080Sbostic /* Extend IFILE so that the next lfs_valloc will succeed. */ 6852080Sbostic if (fs->lfs_free == LFS_UNUSED_INUM) { 6952080Sbostic vp = fs->lfs_ivnode; 7052080Sbostic ip = VTOI(vp); 7152080Sbostic blkno = lblkno(fs, ip->i_size); 7257069Smargo lfs_balloc(vp, fs->lfs_bsize, blkno, &bp); 7357069Smargo ip->i_size += fs->lfs_bsize; 7457069Smargo vnode_pager_setsize(vp, (u_long)ip->i_size); 7557069Smargo vnode_pager_uncache(vp); 7657069Smargo 7752080Sbostic i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) * 7852080Sbostic fs->lfs_ifpb; 7952080Sbostic fs->lfs_free = i; 8052080Sbostic max = i + fs->lfs_ifpb; 8164524Sbostic for (ifp = (struct ifile *)bp->b_data; i < max; ++ifp) { 8252080Sbostic ifp->if_version = 1; 8352080Sbostic ifp->if_daddr = LFS_UNUSED_DADDR; 8452080Sbostic ifp->if_nextfree = ++i; 8552080Sbostic } 8652080Sbostic ifp--; 8752080Sbostic ifp->if_nextfree = LFS_UNUSED_INUM; 8855937Sbostic if (error = VOP_BWRITE(bp)) 8955937Sbostic return (error); 9052080Sbostic } 9152080Sbostic 9251215Sbostic /* Create a vnode to associate with the inode. */ 9353591Sheideman if (error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp)) 9437735Smckusick return (error); 9556866Smargo 9657069Smargo 9751560Smckusick ip = VTOI(vp); 9852080Sbostic /* Zero out the direct and indirect block addresses. */ 9956866Smargo bzero(&ip->i_din, sizeof(struct dinode)); 10056866Smargo ip->i_din.di_inumber = new_ino; 10152080Sbostic 10251215Sbostic /* Set a new generation number for this inode. */ 10338255Smckusick if (++nextgennumber < (u_long)time.tv_sec) 10438255Smckusick nextgennumber = time.tv_sec; 10538255Smckusick ip->i_gen = nextgennumber; 1064359Smckusick 10751215Sbostic /* Insert into the inode hash table. */ 10851485Sbostic ufs_ihashins(ip); 1094359Smckusick 11056866Smargo if (error = ufs_vinit(vp->v_mount, lfs_specop_p, LFS_FIFOOPS, &vp)) { 11156866Smargo vput(vp); 11256866Smargo *ap->a_vpp = NULL; 11356866Smargo return (error); 11456866Smargo } 11556866Smargo 11656866Smargo *ap->a_vpp = vp; 11756866Smargo vp->v_flag |= VDIROP; 11856866Smargo VREF(ip->i_devvp); 11956866Smargo 12051215Sbostic /* Set superblock modified bit and increment file count. */ 12151215Sbostic fs->lfs_fmod = 1; 12251215Sbostic ++fs->lfs_nfiles; 12351155Sbostic return (0); 1244359Smckusick } 1254359Smckusick 12651215Sbostic /* Create a new vnode/inode pair and initialize what fields we can. */ 12751347Sbostic int 12851155Sbostic lfs_vcreate(mp, ino, vpp) 12952080Sbostic struct mount *mp; 13051155Sbostic ino_t ino; 13152080Sbostic struct vnode **vpp; 1324359Smckusick { 13353528Sheideman extern int (**lfs_vnodeop_p)(); 13452080Sbostic struct inode *ip; 13552080Sbostic struct ufsmount *ump; 13651155Sbostic int error, i; 1374359Smckusick 13851215Sbostic /* Create the vnode. */ 13953528Sheideman if (error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) { 14051990Smckusick *vpp = NULL; 14151311Sbostic return (error); 14251990Smckusick } 1434359Smckusick 14451215Sbostic /* Get a pointer to the private mount structure. */ 14551155Sbostic ump = VFSTOUFS(mp); 1464651Smckusic 14751155Sbostic /* Initialize the inode. */ 14851990Smckusick MALLOC(ip, struct inode *, sizeof(struct inode), M_LFSNODE, M_WAITOK); 14951990Smckusick (*vpp)->v_data = ip; 15051560Smckusick ip->i_vnode = *vpp; 15152273Sbostic ip->i_devvp = ump->um_devvp; 152*64609Sbostic ip->i_flag = IN_MODIFIED; 15351155Sbostic ip->i_dev = ump->um_dev; 15456866Smargo ip->i_number = ip->i_din.di_inumber = ino; 15557069Smargo ip->i_din.di_spare[0] = 0xdeadbeef; 15657069Smargo ip->i_din.di_spare[1] = 0xdeadbeef; 15751155Sbostic ip->i_lfs = ump->um_lfs; 15851155Sbostic #ifdef QUOTA 15951155Sbostic for (i = 0; i < MAXQUOTAS; i++) 16051155Sbostic ip->i_dquot[i] = NODQUOT; 16151155Sbostic #endif 16252273Sbostic ip->i_lockf = 0; 16352273Sbostic ip->i_diroff = 0; 16452273Sbostic ip->i_mode = 0; 16552273Sbostic ip->i_size = 0; 16655590Sbostic ip->i_blocks = 0; 16755937Sbostic ++ump->um_lfs->lfs_uinodes; 16851155Sbostic return (0); 1694651Smckusic } 17051183Sbostic 17151485Sbostic /* Free an inode. */ 17251485Sbostic /* ARGUSED */ 17353572Sheideman int 17454686Smargo lfs_vfree(ap) 17554686Smargo struct vop_vfree_args /* { 17654686Smargo struct vnode *a_pvp; 17754686Smargo ino_t a_ino; 17854686Smargo int a_mode; 17954686Smargo } */ *ap; 18051215Sbostic { 18152680Sstaelin SEGUSE *sup; 18252080Sbostic struct buf *bp; 18352080Sbostic struct ifile *ifp; 18452080Sbostic struct inode *ip; 18551492Sbostic struct lfs *fs; 18652680Sstaelin daddr_t old_iaddr; 18751215Sbostic ino_t ino; 18855937Sbostic int error; 18951215Sbostic 19055807Sbostic /* Get the inode number and file system. */ 19153591Sheideman ip = VTOI(ap->a_pvp); 19251215Sbostic fs = ip->i_lfs; 19351215Sbostic ino = ip->i_number; 194*64609Sbostic if (ip->i_flag & IN_MODIFIED) { 19555937Sbostic --fs->lfs_uinodes; 196*64609Sbostic ip->i_flag &= 197*64609Sbostic ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE); 19855937Sbostic } 19951485Sbostic /* 20051933Sbostic * Set the ifile's inode entry to unused, increment its version number 20151933Sbostic * and link it into the free chain. 20251485Sbostic */ 20351215Sbostic LFS_IENTRY(ifp, fs, ino, bp); 20452680Sstaelin old_iaddr = ifp->if_daddr; 20551485Sbostic ifp->if_daddr = LFS_UNUSED_DADDR; 20651485Sbostic ++ifp->if_version; 20751485Sbostic ifp->if_nextfree = fs->lfs_free; 20851485Sbostic fs->lfs_free = ino; 20955937Sbostic (void) VOP_BWRITE(bp); 21051215Sbostic 21152680Sstaelin if (old_iaddr != LFS_UNUSED_DADDR) { 21252680Sstaelin LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp); 21352680Sstaelin #ifdef DIAGNOSTIC 21452680Sstaelin if (sup->su_nbytes < sizeof(struct dinode)) 21552680Sstaelin panic("lfs_vfree: negative byte count (segment %d)\n", 21652680Sstaelin datosn(fs, old_iaddr)); 21752680Sstaelin #endif 21852680Sstaelin sup->su_nbytes -= sizeof(struct dinode); 21955937Sbostic (void) VOP_BWRITE(bp); 22052680Sstaelin } 22152680Sstaelin 22251485Sbostic /* Set superblock modified bit and decrement file count. */ 22351485Sbostic fs->lfs_fmod = 1; 22451485Sbostic --fs->lfs_nfiles; 22553572Sheideman return (0); 22651215Sbostic } 227