123394Smckusick /* 251492Sbostic * Copyright (c) 1991 Regents of the University of California. 334432Sbostic * All rights reserved. 423394Smckusick * 544536Sbostic * %sccs.include.redist.c% 634432Sbostic * 7*53528Sheideman * @(#)lfs_alloc.c 7.43 (Berkeley) 05/14/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*53528Sheideman lfs_valloc (ap) 33*53528Sheideman struct vop_valloc_args *ap; 34*53528Sheideman #define pvp (ap->a_pvp) 35*53528Sheideman #define notused (ap->a_mode) 36*53528Sheideman #define cred (ap->a_cred) 37*53528Sheideman #define vpp (ap->a_vpp) 3851155Sbostic { 3951492Sbostic struct lfs *fs; 4052080Sbostic struct buf *bp; 4152080Sbostic struct ifile *ifp; 4252080Sbostic struct inode *ip; 4352080Sbostic struct vnode *vp; 4452080Sbostic daddr_t blkno; 4551155Sbostic ino_t new_ino; 4652080Sbostic u_long i, max; 4751155Sbostic int error; 484359Smckusick 4951854Sbostic #ifdef VERBOSE 5051854Sbostic printf("lfs_valloc\n"); 5151854Sbostic #endif 5251215Sbostic /* Get the head of the freelist. */ 5351560Smckusick fs = VTOI(pvp)->i_lfs; 5451155Sbostic new_ino = fs->lfs_free; 5551485Sbostic #ifdef ALLOCPRINT 5651485Sbostic printf("lfs_ialloc: allocate inode %d\n", new_ino); 5751485Sbostic #endif 584359Smckusick 5951933Sbostic /* 6052080Sbostic * Remove the inode from the free list and write the new start 6152080Sbostic * of the free list into the superblock. 6251933Sbostic */ 6351155Sbostic LFS_IENTRY(ifp, fs, new_ino, bp); 6451155Sbostic if (ifp->if_daddr != LFS_UNUSED_DADDR) 6551215Sbostic panic("lfs_ialloc: inuse inode on the free list"); 6651183Sbostic fs->lfs_free = ifp->if_nextfree; 6752080Sbostic brelse(bp); 684426Smckusic 6952080Sbostic /* Extend IFILE so that the next lfs_valloc will succeed. */ 7052080Sbostic if (fs->lfs_free == LFS_UNUSED_INUM) { 7152080Sbostic vp = fs->lfs_ivnode; 7252080Sbostic ip = VTOI(vp); 7352080Sbostic blkno = lblkno(fs, ip->i_size); 7452080Sbostic printf("Extending ifile: blkno = %d\n", blkno); 7552080Sbostic bp = getblk(vp, blkno, fs->lfs_bsize); 7652080Sbostic if (!bp) { 7752080Sbostic uprintf("\n%s: no inodes left\n", fs->lfs_fsmnt); 7852080Sbostic log(LOG_ERR, "uid %d on %s: out of inodes\n", 7952080Sbostic cred->cr_uid, fs->lfs_fsmnt); 8052080Sbostic return (ENOSPC); 8152080Sbostic } 8252080Sbostic i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) * 8352080Sbostic fs->lfs_ifpb; 8452080Sbostic printf("Extending ifile: first inum = %d\n", i); 8552080Sbostic fs->lfs_free = i; 8652080Sbostic max = i + fs->lfs_ifpb; 8752080Sbostic printf("Extending ifile: max inum = %d\n", max); 8852080Sbostic for (ifp = (struct ifile *)bp->b_un.b_words; i < max; ++ifp) { 8952080Sbostic ifp->if_version = 1; 9052080Sbostic ifp->if_daddr = LFS_UNUSED_DADDR; 9152080Sbostic ifp->if_nextfree = ++i; 9252080Sbostic } 9352080Sbostic ifp--; 9452080Sbostic ifp->if_nextfree = LFS_UNUSED_INUM; 9552080Sbostic 9652080Sbostic ++ip->i_blocks; /* XXX This may not be right. */ 9752080Sbostic ip->i_size += fs->lfs_bsize; 9852080Sbostic printf("Extending ifile: blocks = %d size = %d\n", ip->i_blocks, ip->i_size); 9953320Smckusick vnode_pager_setsize(vp, (u_long)ip->i_size); 10052080Sbostic vnode_pager_uncache(vp); 10152080Sbostic LFS_UBWRITE(bp); 10252080Sbostic } 10352080Sbostic 10451215Sbostic /* Create a vnode to associate with the inode. */ 10551560Smckusick if (error = lfs_vcreate(pvp->v_mount, new_ino, &vp)) 10637735Smckusick return (error); 10751560Smckusick *vpp = vp; 10851560Smckusick ip = VTOI(vp); 10951560Smckusick VREF(ip->i_devvp); 11051155Sbostic 11152080Sbostic /* Zero out the direct and indirect block addresses. */ 11252080Sbostic bzero(ip->i_db, (NDADDR + NIADDR) * sizeof(daddr_t)); 11352080Sbostic 11451215Sbostic /* Set a new generation number for this inode. */ 11538255Smckusick if (++nextgennumber < (u_long)time.tv_sec) 11638255Smckusick nextgennumber = time.tv_sec; 11738255Smckusick ip->i_gen = nextgennumber; 1184359Smckusick 11951215Sbostic /* Insert into the inode hash table. */ 12051485Sbostic ufs_ihashins(ip); 1214359Smckusick 12251215Sbostic /* Set superblock modified bit and increment file count. */ 12351215Sbostic fs->lfs_fmod = 1; 12451215Sbostic ++fs->lfs_nfiles; 12551155Sbostic return (0); 1264359Smckusick } 127*53528Sheideman #undef pvp 128*53528Sheideman #undef notused 129*53528Sheideman #undef cred 130*53528Sheideman #undef vpp 1314359Smckusick 13251215Sbostic /* Create a new vnode/inode pair and initialize what fields we can. */ 13351347Sbostic int 13451155Sbostic lfs_vcreate(mp, ino, vpp) 13552080Sbostic struct mount *mp; 13651155Sbostic ino_t ino; 13752080Sbostic struct vnode **vpp; 1384359Smckusick { 139*53528Sheideman extern int (**lfs_vnodeop_p)(); 14052080Sbostic struct inode *ip; 14152080Sbostic struct ufsmount *ump; 14251155Sbostic int error, i; 1434359Smckusick 14451854Sbostic #ifdef VERBOSE 14551485Sbostic printf("lfs_vcreate: ino %d\n", ino); 14651485Sbostic #endif 14751215Sbostic /* Create the vnode. */ 148*53528Sheideman if (error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) { 14951990Smckusick *vpp = NULL; 15051311Sbostic return (error); 15151990Smckusick } 1524359Smckusick 15351215Sbostic /* Get a pointer to the private mount structure. */ 15451155Sbostic ump = VFSTOUFS(mp); 1554651Smckusic 15651155Sbostic /* Initialize the inode. */ 15751990Smckusick MALLOC(ip, struct inode *, sizeof(struct inode), M_LFSNODE, M_WAITOK); 15851990Smckusick (*vpp)->v_data = ip; 15951560Smckusick ip->i_vnode = *vpp; 16052273Sbostic ip->i_devvp = ump->um_devvp; 16151560Smckusick ip->i_flag = 0; 16251155Sbostic ip->i_dev = ump->um_dev; 16351560Smckusick ip->i_number = ip->i_din.di_inum = ino; 16451155Sbostic ip->i_lfs = ump->um_lfs; 16551155Sbostic #ifdef QUOTA 16651155Sbostic for (i = 0; i < MAXQUOTAS; i++) 16751155Sbostic ip->i_dquot[i] = NODQUOT; 16851155Sbostic #endif 16952273Sbostic ip->i_lockf = 0; 17052273Sbostic ip->i_diroff = 0; 17152273Sbostic ip->i_mode = 0; 17252273Sbostic ip->i_size = 0; 17351155Sbostic return (0); 1744651Smckusic } 17551183Sbostic 17651485Sbostic /* Free an inode. */ 17751485Sbostic /* ARGUSED */ 17851215Sbostic void 179*53528Sheideman lfs_vfree (ap) 180*53528Sheideman struct vop_vfree_args *ap; 181*53528Sheideman #define vp (ap->a_pvp) 182*53528Sheideman #define notused1 (ap->a_ino) 183*53528Sheideman #define notused2 (ap->a_mode) 18451215Sbostic { 18552680Sstaelin SEGUSE *sup; 18652080Sbostic struct buf *bp; 18752080Sbostic struct ifile *ifp; 18852080Sbostic struct inode *ip; 18951492Sbostic struct lfs *fs; 19052680Sstaelin daddr_t old_iaddr; 19151215Sbostic ino_t ino; 19251215Sbostic 19351560Smckusick ip = VTOI(vp); 19451854Sbostic #ifdef VERBOSE 19551854Sbostic printf("lfs_vfree: free %d\n", ip->i_number); 19651485Sbostic #endif 19751485Sbostic /* Get the inode number and file system. */ 19851215Sbostic fs = ip->i_lfs; 19951215Sbostic ino = ip->i_number; 20051485Sbostic 20151485Sbostic /* 20251933Sbostic * Set the ifile's inode entry to unused, increment its version number 20351933Sbostic * and link it into the free chain. 20451485Sbostic */ 20551215Sbostic LFS_IENTRY(ifp, fs, ino, bp); 20652680Sstaelin old_iaddr = ifp->if_daddr; 20751485Sbostic ifp->if_daddr = LFS_UNUSED_DADDR; 20851485Sbostic ++ifp->if_version; 20951485Sbostic ifp->if_nextfree = fs->lfs_free; 21051485Sbostic fs->lfs_free = ino; 21152080Sbostic LFS_UBWRITE(bp); 21251215Sbostic 21352680Sstaelin if (old_iaddr != LFS_UNUSED_DADDR) { 21452680Sstaelin LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp); 21552680Sstaelin #ifdef DIAGNOSTIC 21652680Sstaelin if (sup->su_nbytes < sizeof(struct dinode)) 21752680Sstaelin panic("lfs_vfree: negative byte count (segment %d)\n", 21852680Sstaelin datosn(fs, old_iaddr)); 21952680Sstaelin #endif 22052680Sstaelin sup->su_nbytes -= sizeof(struct dinode); 22152680Sstaelin LFS_UBWRITE(bp); 22252680Sstaelin } 22352680Sstaelin 22451485Sbostic /* Set superblock modified bit and decrement file count. */ 22551485Sbostic fs->lfs_fmod = 1; 22651485Sbostic --fs->lfs_nfiles; 22751215Sbostic } 228*53528Sheideman #undef vp 229*53528Sheideman #undef notused1 230*53528Sheideman #undef notused2 231