123394Smckusick /* 251492Sbostic * Copyright (c) 1991 Regents of the University of California. 334432Sbostic * All rights reserved. 423394Smckusick * 544536Sbostic * %sccs.include.redist.c% 634432Sbostic * 7*55937Sbostic * @(#)lfs_alloc.c 7.52 (Berkeley) 08/21/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 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; 4851155Sbostic int 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); 7252080Sbostic bp = getblk(vp, blkno, fs->lfs_bsize); 7352080Sbostic if (!bp) { 7452080Sbostic uprintf("\n%s: no inodes left\n", fs->lfs_fsmnt); 7552080Sbostic log(LOG_ERR, "uid %d on %s: out of inodes\n", 7653591Sheideman ap->a_cred->cr_uid, fs->lfs_fsmnt); 7752080Sbostic return (ENOSPC); 7852080Sbostic } 7952080Sbostic i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) * 8052080Sbostic fs->lfs_ifpb; 8152080Sbostic fs->lfs_free = i; 8252080Sbostic max = i + fs->lfs_ifpb; 8352080Sbostic for (ifp = (struct ifile *)bp->b_un.b_words; i < max; ++ifp) { 8452080Sbostic ifp->if_version = 1; 8552080Sbostic ifp->if_daddr = LFS_UNUSED_DADDR; 8652080Sbostic ifp->if_nextfree = ++i; 8752080Sbostic } 8852080Sbostic ifp--; 8952080Sbostic ifp->if_nextfree = LFS_UNUSED_INUM; 9052080Sbostic 9155455Sbostic ip->i_blocks += btodb(fs->lfs_bsize); 9255590Sbostic fs->lfs_bfree -= btodb(fs->lfs_bsize); 9352080Sbostic ip->i_size += fs->lfs_bsize; 9453320Smckusick vnode_pager_setsize(vp, (u_long)ip->i_size); 9552080Sbostic vnode_pager_uncache(vp); 96*55937Sbostic if (error = VOP_BWRITE(bp)) 97*55937Sbostic return (error); 9852080Sbostic } 9952080Sbostic 10051215Sbostic /* Create a vnode to associate with the inode. */ 10153591Sheideman if (error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp)) 10237735Smckusick return (error); 10353591Sheideman *ap->a_vpp = vp; 10454686Smargo vp->v_flag |= VDIROP; 10551560Smckusick ip = VTOI(vp); 10651560Smckusick VREF(ip->i_devvp); 10751155Sbostic 10852080Sbostic /* Zero out the direct and indirect block addresses. */ 10952080Sbostic bzero(ip->i_db, (NDADDR + NIADDR) * sizeof(daddr_t)); 11052080Sbostic 11151215Sbostic /* Set a new generation number for this inode. */ 11238255Smckusick if (++nextgennumber < (u_long)time.tv_sec) 11338255Smckusick nextgennumber = time.tv_sec; 11438255Smckusick ip->i_gen = nextgennumber; 1154359Smckusick 11651215Sbostic /* Insert into the inode hash table. */ 11751485Sbostic ufs_ihashins(ip); 1184359Smckusick 11951215Sbostic /* Set superblock modified bit and increment file count. */ 12051215Sbostic fs->lfs_fmod = 1; 12151215Sbostic ++fs->lfs_nfiles; 12251155Sbostic return (0); 1234359Smckusick } 1244359Smckusick 12551215Sbostic /* Create a new vnode/inode pair and initialize what fields we can. */ 12651347Sbostic int 12751155Sbostic lfs_vcreate(mp, ino, vpp) 12852080Sbostic struct mount *mp; 12951155Sbostic ino_t ino; 13052080Sbostic struct vnode **vpp; 1314359Smckusick { 13253528Sheideman extern int (**lfs_vnodeop_p)(); 13352080Sbostic struct inode *ip; 13452080Sbostic struct ufsmount *ump; 13551155Sbostic int error, i; 1364359Smckusick 13751215Sbostic /* Create the vnode. */ 13853528Sheideman if (error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) { 13951990Smckusick *vpp = NULL; 14051311Sbostic return (error); 14151990Smckusick } 1424359Smckusick 14351215Sbostic /* Get a pointer to the private mount structure. */ 14451155Sbostic ump = VFSTOUFS(mp); 1454651Smckusic 14651155Sbostic /* Initialize the inode. */ 14751990Smckusick MALLOC(ip, struct inode *, sizeof(struct inode), M_LFSNODE, M_WAITOK); 14851990Smckusick (*vpp)->v_data = ip; 14951560Smckusick ip->i_vnode = *vpp; 15052273Sbostic ip->i_devvp = ump->um_devvp; 151*55937Sbostic ip->i_flag = IMOD; 15251155Sbostic ip->i_dev = ump->um_dev; 15351560Smckusick ip->i_number = ip->i_din.di_inum = ino; 15451155Sbostic ip->i_lfs = ump->um_lfs; 15551155Sbostic #ifdef QUOTA 15651155Sbostic for (i = 0; i < MAXQUOTAS; i++) 15751155Sbostic ip->i_dquot[i] = NODQUOT; 15851155Sbostic #endif 15952273Sbostic ip->i_lockf = 0; 16052273Sbostic ip->i_diroff = 0; 16152273Sbostic ip->i_mode = 0; 16252273Sbostic ip->i_size = 0; 16355590Sbostic ip->i_blocks = 0; 164*55937Sbostic ++ump->um_lfs->lfs_uinodes; 16551155Sbostic return (0); 1664651Smckusic } 16751183Sbostic 16851485Sbostic /* Free an inode. */ 16951485Sbostic /* ARGUSED */ 17053572Sheideman int 17154686Smargo lfs_vfree(ap) 17254686Smargo struct vop_vfree_args /* { 17354686Smargo struct vnode *a_pvp; 17454686Smargo ino_t a_ino; 17554686Smargo int a_mode; 17654686Smargo } */ *ap; 17751215Sbostic { 17852680Sstaelin SEGUSE *sup; 17952080Sbostic struct buf *bp; 18052080Sbostic struct ifile *ifp; 18152080Sbostic struct inode *ip; 18251492Sbostic struct lfs *fs; 18352680Sstaelin daddr_t old_iaddr; 18451215Sbostic ino_t ino; 185*55937Sbostic int error; 18651215Sbostic 18755807Sbostic /* Get the inode number and file system. */ 18853591Sheideman ip = VTOI(ap->a_pvp); 18951215Sbostic fs = ip->i_lfs; 19051215Sbostic ino = ip->i_number; 191*55937Sbostic if (ip->i_flag & IMOD) { 192*55937Sbostic --fs->lfs_uinodes; 193*55937Sbostic ip->i_flag &= ~(IMOD | IACC | IUPD | ICHG); 194*55937Sbostic } 19551485Sbostic /* 19651933Sbostic * Set the ifile's inode entry to unused, increment its version number 19751933Sbostic * and link it into the free chain. 19851485Sbostic */ 19951215Sbostic LFS_IENTRY(ifp, fs, ino, bp); 20052680Sstaelin old_iaddr = ifp->if_daddr; 20151485Sbostic ifp->if_daddr = LFS_UNUSED_DADDR; 20251485Sbostic ++ifp->if_version; 20351485Sbostic ifp->if_nextfree = fs->lfs_free; 20451485Sbostic fs->lfs_free = ino; 205*55937Sbostic (void) VOP_BWRITE(bp); 20651215Sbostic 20752680Sstaelin if (old_iaddr != LFS_UNUSED_DADDR) { 20852680Sstaelin LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp); 20952680Sstaelin #ifdef DIAGNOSTIC 21052680Sstaelin if (sup->su_nbytes < sizeof(struct dinode)) 21152680Sstaelin panic("lfs_vfree: negative byte count (segment %d)\n", 21252680Sstaelin datosn(fs, old_iaddr)); 21352680Sstaelin #endif 21452680Sstaelin sup->su_nbytes -= sizeof(struct dinode); 215*55937Sbostic (void) VOP_BWRITE(bp); 21652680Sstaelin } 21752680Sstaelin 21851485Sbostic /* Set superblock modified bit and decrement file count. */ 21951485Sbostic fs->lfs_fmod = 1; 22051485Sbostic --fs->lfs_nfiles; 22153572Sheideman return (0); 22251215Sbostic } 223