123394Smckusick /* 251492Sbostic * Copyright (c) 1991 Regents of the University of California. 334432Sbostic * All rights reserved. 423394Smckusick * 544536Sbostic * %sccs.include.redist.c% 634432Sbostic * 7*54686Smargo * @(#)lfs_alloc.c 7.47 (Berkeley) 07/05/92 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 32*54686Smargo lfs_valloc(ap) 33*54686Smargo struct vop_valloc_args /* { 34*54686Smargo struct vnode *a_pvp; 35*54686Smargo int a_mode; 36*54686Smargo struct ucred *a_cred; 37*54686Smargo struct vnode **a_vpp; 38*54686Smargo } */ *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; 4851155Sbostic int error; 494359Smckusick 5051854Sbostic #ifdef VERBOSE 5151854Sbostic printf("lfs_valloc\n"); 5251854Sbostic #endif 5351215Sbostic /* Get the head of the freelist. */ 5453591Sheideman fs = VTOI(ap->a_pvp)->i_lfs; 5551155Sbostic new_ino = fs->lfs_free; 5651485Sbostic #ifdef ALLOCPRINT 5751485Sbostic printf("lfs_ialloc: allocate inode %d\n", new_ino); 5851485Sbostic #endif 594359Smckusick 6051933Sbostic /* 6152080Sbostic * Remove the inode from the free list and write the new start 6252080Sbostic * of the free list into the superblock. 6351933Sbostic */ 6451155Sbostic LFS_IENTRY(ifp, fs, new_ino, bp); 6551155Sbostic if (ifp->if_daddr != LFS_UNUSED_DADDR) 6651215Sbostic panic("lfs_ialloc: inuse inode on the free list"); 6751183Sbostic fs->lfs_free = ifp->if_nextfree; 6852080Sbostic brelse(bp); 694426Smckusic 7052080Sbostic /* Extend IFILE so that the next lfs_valloc will succeed. */ 7152080Sbostic if (fs->lfs_free == LFS_UNUSED_INUM) { 7252080Sbostic vp = fs->lfs_ivnode; 7352080Sbostic ip = VTOI(vp); 7452080Sbostic blkno = lblkno(fs, ip->i_size); 7552080Sbostic printf("Extending ifile: blkno = %d\n", blkno); 7652080Sbostic bp = getblk(vp, blkno, fs->lfs_bsize); 7752080Sbostic if (!bp) { 7852080Sbostic uprintf("\n%s: no inodes left\n", fs->lfs_fsmnt); 7952080Sbostic log(LOG_ERR, "uid %d on %s: out of inodes\n", 8053591Sheideman ap->a_cred->cr_uid, fs->lfs_fsmnt); 8152080Sbostic return (ENOSPC); 8252080Sbostic } 8352080Sbostic i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) * 8452080Sbostic fs->lfs_ifpb; 8552080Sbostic printf("Extending ifile: first inum = %d\n", i); 8652080Sbostic fs->lfs_free = i; 8752080Sbostic max = i + fs->lfs_ifpb; 8852080Sbostic printf("Extending ifile: max inum = %d\n", max); 8952080Sbostic for (ifp = (struct ifile *)bp->b_un.b_words; i < max; ++ifp) { 9052080Sbostic ifp->if_version = 1; 9152080Sbostic ifp->if_daddr = LFS_UNUSED_DADDR; 9252080Sbostic ifp->if_nextfree = ++i; 9352080Sbostic } 9452080Sbostic ifp--; 9552080Sbostic ifp->if_nextfree = LFS_UNUSED_INUM; 9652080Sbostic 9752080Sbostic ++ip->i_blocks; /* XXX This may not be right. */ 9852080Sbostic ip->i_size += fs->lfs_bsize; 9952080Sbostic printf("Extending ifile: blocks = %d size = %d\n", ip->i_blocks, ip->i_size); 10053320Smckusick vnode_pager_setsize(vp, (u_long)ip->i_size); 10152080Sbostic vnode_pager_uncache(vp); 10252080Sbostic LFS_UBWRITE(bp); 10352080Sbostic } 10452080Sbostic 10551215Sbostic /* Create a vnode to associate with the inode. */ 10653591Sheideman if (error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp)) 10737735Smckusick return (error); 10853591Sheideman *ap->a_vpp = vp; 109*54686Smargo vp->v_flag |= VDIROP; 11051560Smckusick ip = VTOI(vp); 11151560Smckusick VREF(ip->i_devvp); 11251155Sbostic 11352080Sbostic /* Zero out the direct and indirect block addresses. */ 11452080Sbostic bzero(ip->i_db, (NDADDR + NIADDR) * sizeof(daddr_t)); 11552080Sbostic 11651215Sbostic /* Set a new generation number for this inode. */ 11738255Smckusick if (++nextgennumber < (u_long)time.tv_sec) 11838255Smckusick nextgennumber = time.tv_sec; 11938255Smckusick ip->i_gen = nextgennumber; 1204359Smckusick 12151215Sbostic /* Insert into the inode hash table. */ 12251485Sbostic ufs_ihashins(ip); 1234359Smckusick 12451215Sbostic /* Set superblock modified bit and increment file count. */ 12551215Sbostic fs->lfs_fmod = 1; 12651215Sbostic ++fs->lfs_nfiles; 12751155Sbostic return (0); 1284359Smckusick } 1294359Smckusick 13051215Sbostic /* Create a new vnode/inode pair and initialize what fields we can. */ 13151347Sbostic int 13251155Sbostic lfs_vcreate(mp, ino, vpp) 13352080Sbostic struct mount *mp; 13451155Sbostic ino_t ino; 13552080Sbostic struct vnode **vpp; 1364359Smckusick { 13753528Sheideman extern int (**lfs_vnodeop_p)(); 13852080Sbostic struct inode *ip; 13952080Sbostic struct ufsmount *ump; 14051155Sbostic int error, i; 1414359Smckusick 14251854Sbostic #ifdef VERBOSE 14351485Sbostic printf("lfs_vcreate: ino %d\n", ino); 14451485Sbostic #endif 14551215Sbostic /* Create the vnode. */ 14653528Sheideman if (error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) { 14751990Smckusick *vpp = NULL; 14851311Sbostic return (error); 14951990Smckusick } 1504359Smckusick 15151215Sbostic /* Get a pointer to the private mount structure. */ 15251155Sbostic ump = VFSTOUFS(mp); 1534651Smckusic 15451155Sbostic /* Initialize the inode. */ 15551990Smckusick MALLOC(ip, struct inode *, sizeof(struct inode), M_LFSNODE, M_WAITOK); 15651990Smckusick (*vpp)->v_data = ip; 15751560Smckusick ip->i_vnode = *vpp; 15852273Sbostic ip->i_devvp = ump->um_devvp; 15951560Smckusick ip->i_flag = 0; 16051155Sbostic ip->i_dev = ump->um_dev; 16151560Smckusick ip->i_number = ip->i_din.di_inum = ino; 16251155Sbostic ip->i_lfs = ump->um_lfs; 16351155Sbostic #ifdef QUOTA 16451155Sbostic for (i = 0; i < MAXQUOTAS; i++) 16551155Sbostic ip->i_dquot[i] = NODQUOT; 16651155Sbostic #endif 16752273Sbostic ip->i_lockf = 0; 16852273Sbostic ip->i_diroff = 0; 16952273Sbostic ip->i_mode = 0; 17052273Sbostic ip->i_size = 0; 17151155Sbostic return (0); 1724651Smckusic } 17351183Sbostic 17451485Sbostic /* Free an inode. */ 17551485Sbostic /* ARGUSED */ 17653572Sheideman int 177*54686Smargo lfs_vfree(ap) 178*54686Smargo struct vop_vfree_args /* { 179*54686Smargo struct vnode *a_pvp; 180*54686Smargo ino_t a_ino; 181*54686Smargo int a_mode; 182*54686Smargo } */ *ap; 18351215Sbostic { 18452680Sstaelin SEGUSE *sup; 18552080Sbostic struct buf *bp; 18652080Sbostic struct ifile *ifp; 18752080Sbostic struct inode *ip; 18851492Sbostic struct lfs *fs; 18952680Sstaelin daddr_t old_iaddr; 19051215Sbostic ino_t ino; 19151215Sbostic 19253591Sheideman ip = VTOI(ap->a_pvp); 19351854Sbostic #ifdef VERBOSE 19451854Sbostic printf("lfs_vfree: free %d\n", ip->i_number); 19551485Sbostic #endif 19651485Sbostic /* Get the inode number and file system. */ 19751215Sbostic fs = ip->i_lfs; 19851215Sbostic ino = ip->i_number; 19951485Sbostic 20051485Sbostic /* 20151933Sbostic * Set the ifile's inode entry to unused, increment its version number 20251933Sbostic * and link it into the free chain. 20351485Sbostic */ 20451215Sbostic LFS_IENTRY(ifp, fs, ino, bp); 20552680Sstaelin old_iaddr = ifp->if_daddr; 20651485Sbostic ifp->if_daddr = LFS_UNUSED_DADDR; 20751485Sbostic ++ifp->if_version; 20851485Sbostic ifp->if_nextfree = fs->lfs_free; 20951485Sbostic fs->lfs_free = ino; 21052080Sbostic LFS_UBWRITE(bp); 21151215Sbostic 21252680Sstaelin if (old_iaddr != LFS_UNUSED_DADDR) { 21352680Sstaelin LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp); 21452680Sstaelin #ifdef DIAGNOSTIC 21552680Sstaelin if (sup->su_nbytes < sizeof(struct dinode)) 21652680Sstaelin panic("lfs_vfree: negative byte count (segment %d)\n", 21752680Sstaelin datosn(fs, old_iaddr)); 21852680Sstaelin #endif 21952680Sstaelin sup->su_nbytes -= sizeof(struct dinode); 22052680Sstaelin LFS_UBWRITE(bp); 22152680Sstaelin } 22252680Sstaelin 22351485Sbostic /* Set superblock modified bit and decrement file count. */ 22451485Sbostic fs->lfs_fmod = 1; 22551485Sbostic --fs->lfs_nfiles; 22653572Sheideman return (0); 22751215Sbostic } 228