123394Smckusick /* 251492Sbostic * Copyright (c) 1991 Regents of the University of California. 334432Sbostic * All rights reserved. 423394Smckusick * 544536Sbostic * %sccs.include.redist.c% 634432Sbostic * 7*53591Sheideman * @(#)lfs_alloc.c 7.45 (Berkeley) 05/15/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 3253528Sheideman lfs_valloc (ap) 3353528Sheideman struct vop_valloc_args *ap; 3451155Sbostic { 3551492Sbostic struct lfs *fs; 3652080Sbostic struct buf *bp; 3752080Sbostic struct ifile *ifp; 3852080Sbostic struct inode *ip; 3952080Sbostic struct vnode *vp; 4052080Sbostic daddr_t blkno; 4151155Sbostic ino_t new_ino; 4252080Sbostic u_long i, max; 4351155Sbostic int error; 444359Smckusick 4551854Sbostic #ifdef VERBOSE 4651854Sbostic printf("lfs_valloc\n"); 4751854Sbostic #endif 4851215Sbostic /* Get the head of the freelist. */ 49*53591Sheideman fs = VTOI(ap->a_pvp)->i_lfs; 5051155Sbostic new_ino = fs->lfs_free; 5151485Sbostic #ifdef ALLOCPRINT 5251485Sbostic printf("lfs_ialloc: allocate inode %d\n", new_ino); 5351485Sbostic #endif 544359Smckusick 5551933Sbostic /* 5652080Sbostic * Remove the inode from the free list and write the new start 5752080Sbostic * of the free list into the superblock. 5851933Sbostic */ 5951155Sbostic LFS_IENTRY(ifp, fs, new_ino, bp); 6051155Sbostic if (ifp->if_daddr != LFS_UNUSED_DADDR) 6151215Sbostic panic("lfs_ialloc: inuse inode on the free list"); 6251183Sbostic fs->lfs_free = ifp->if_nextfree; 6352080Sbostic brelse(bp); 644426Smckusic 6552080Sbostic /* Extend IFILE so that the next lfs_valloc will succeed. */ 6652080Sbostic if (fs->lfs_free == LFS_UNUSED_INUM) { 6752080Sbostic vp = fs->lfs_ivnode; 6852080Sbostic ip = VTOI(vp); 6952080Sbostic blkno = lblkno(fs, ip->i_size); 7052080Sbostic printf("Extending ifile: blkno = %d\n", blkno); 7152080Sbostic bp = getblk(vp, blkno, fs->lfs_bsize); 7252080Sbostic if (!bp) { 7352080Sbostic uprintf("\n%s: no inodes left\n", fs->lfs_fsmnt); 7452080Sbostic log(LOG_ERR, "uid %d on %s: out of inodes\n", 75*53591Sheideman ap->a_cred->cr_uid, fs->lfs_fsmnt); 7652080Sbostic return (ENOSPC); 7752080Sbostic } 7852080Sbostic i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) * 7952080Sbostic fs->lfs_ifpb; 8052080Sbostic printf("Extending ifile: first inum = %d\n", i); 8152080Sbostic fs->lfs_free = i; 8252080Sbostic max = i + fs->lfs_ifpb; 8352080Sbostic printf("Extending ifile: max inum = %d\n", max); 8452080Sbostic for (ifp = (struct ifile *)bp->b_un.b_words; i < max; ++ifp) { 8552080Sbostic ifp->if_version = 1; 8652080Sbostic ifp->if_daddr = LFS_UNUSED_DADDR; 8752080Sbostic ifp->if_nextfree = ++i; 8852080Sbostic } 8952080Sbostic ifp--; 9052080Sbostic ifp->if_nextfree = LFS_UNUSED_INUM; 9152080Sbostic 9252080Sbostic ++ip->i_blocks; /* XXX This may not be right. */ 9352080Sbostic ip->i_size += fs->lfs_bsize; 9452080Sbostic printf("Extending ifile: blocks = %d size = %d\n", ip->i_blocks, ip->i_size); 9553320Smckusick vnode_pager_setsize(vp, (u_long)ip->i_size); 9652080Sbostic vnode_pager_uncache(vp); 9752080Sbostic LFS_UBWRITE(bp); 9852080Sbostic } 9952080Sbostic 10051215Sbostic /* Create a vnode to associate with the inode. */ 101*53591Sheideman if (error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp)) 10237735Smckusick return (error); 103*53591Sheideman *ap->a_vpp = vp; 10451560Smckusick ip = VTOI(vp); 10551560Smckusick VREF(ip->i_devvp); 10651155Sbostic 10752080Sbostic /* Zero out the direct and indirect block addresses. */ 10852080Sbostic bzero(ip->i_db, (NDADDR + NIADDR) * sizeof(daddr_t)); 10952080Sbostic 11051215Sbostic /* Set a new generation number for this inode. */ 11138255Smckusick if (++nextgennumber < (u_long)time.tv_sec) 11238255Smckusick nextgennumber = time.tv_sec; 11338255Smckusick ip->i_gen = nextgennumber; 1144359Smckusick 11551215Sbostic /* Insert into the inode hash table. */ 11651485Sbostic ufs_ihashins(ip); 1174359Smckusick 11851215Sbostic /* Set superblock modified bit and increment file count. */ 11951215Sbostic fs->lfs_fmod = 1; 12051215Sbostic ++fs->lfs_nfiles; 12151155Sbostic return (0); 1224359Smckusick } 1234359Smckusick 12451215Sbostic /* Create a new vnode/inode pair and initialize what fields we can. */ 12551347Sbostic int 12651155Sbostic lfs_vcreate(mp, ino, vpp) 12752080Sbostic struct mount *mp; 12851155Sbostic ino_t ino; 12952080Sbostic struct vnode **vpp; 1304359Smckusick { 13153528Sheideman extern int (**lfs_vnodeop_p)(); 13252080Sbostic struct inode *ip; 13352080Sbostic struct ufsmount *ump; 13451155Sbostic int error, i; 1354359Smckusick 13651854Sbostic #ifdef VERBOSE 13751485Sbostic printf("lfs_vcreate: ino %d\n", ino); 13851485Sbostic #endif 13951215Sbostic /* Create the vnode. */ 14053528Sheideman if (error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) { 14151990Smckusick *vpp = NULL; 14251311Sbostic return (error); 14351990Smckusick } 1444359Smckusick 14551215Sbostic /* Get a pointer to the private mount structure. */ 14651155Sbostic ump = VFSTOUFS(mp); 1474651Smckusic 14851155Sbostic /* Initialize the inode. */ 14951990Smckusick MALLOC(ip, struct inode *, sizeof(struct inode), M_LFSNODE, M_WAITOK); 15051990Smckusick (*vpp)->v_data = ip; 15151560Smckusick ip->i_vnode = *vpp; 15252273Sbostic ip->i_devvp = ump->um_devvp; 15351560Smckusick ip->i_flag = 0; 15451155Sbostic ip->i_dev = ump->um_dev; 15551560Smckusick ip->i_number = ip->i_din.di_inum = ino; 15651155Sbostic ip->i_lfs = ump->um_lfs; 15751155Sbostic #ifdef QUOTA 15851155Sbostic for (i = 0; i < MAXQUOTAS; i++) 15951155Sbostic ip->i_dquot[i] = NODQUOT; 16051155Sbostic #endif 16152273Sbostic ip->i_lockf = 0; 16252273Sbostic ip->i_diroff = 0; 16352273Sbostic ip->i_mode = 0; 16452273Sbostic ip->i_size = 0; 16551155Sbostic return (0); 1664651Smckusic } 16751183Sbostic 16851485Sbostic /* Free an inode. */ 16951485Sbostic /* ARGUSED */ 17053572Sheideman int 17153528Sheideman lfs_vfree (ap) 17253528Sheideman struct vop_vfree_args *ap; 17351215Sbostic { 17452680Sstaelin SEGUSE *sup; 17552080Sbostic struct buf *bp; 17652080Sbostic struct ifile *ifp; 17752080Sbostic struct inode *ip; 17851492Sbostic struct lfs *fs; 17952680Sstaelin daddr_t old_iaddr; 18051215Sbostic ino_t ino; 18151215Sbostic 182*53591Sheideman ip = VTOI(ap->a_pvp); 18351854Sbostic #ifdef VERBOSE 18451854Sbostic printf("lfs_vfree: free %d\n", ip->i_number); 18551485Sbostic #endif 18651485Sbostic /* Get the inode number and file system. */ 18751215Sbostic fs = ip->i_lfs; 18851215Sbostic ino = ip->i_number; 18951485Sbostic 19051485Sbostic /* 19151933Sbostic * Set the ifile's inode entry to unused, increment its version number 19251933Sbostic * and link it into the free chain. 19351485Sbostic */ 19451215Sbostic LFS_IENTRY(ifp, fs, ino, bp); 19552680Sstaelin old_iaddr = ifp->if_daddr; 19651485Sbostic ifp->if_daddr = LFS_UNUSED_DADDR; 19751485Sbostic ++ifp->if_version; 19851485Sbostic ifp->if_nextfree = fs->lfs_free; 19951485Sbostic fs->lfs_free = ino; 20052080Sbostic LFS_UBWRITE(bp); 20151215Sbostic 20252680Sstaelin if (old_iaddr != LFS_UNUSED_DADDR) { 20352680Sstaelin LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp); 20452680Sstaelin #ifdef DIAGNOSTIC 20552680Sstaelin if (sup->su_nbytes < sizeof(struct dinode)) 20652680Sstaelin panic("lfs_vfree: negative byte count (segment %d)\n", 20752680Sstaelin datosn(fs, old_iaddr)); 20852680Sstaelin #endif 20952680Sstaelin sup->su_nbytes -= sizeof(struct dinode); 21052680Sstaelin LFS_UBWRITE(bp); 21152680Sstaelin } 21252680Sstaelin 21351485Sbostic /* Set superblock modified bit and decrement file count. */ 21451485Sbostic fs->lfs_fmod = 1; 21551485Sbostic --fs->lfs_nfiles; 21653572Sheideman return (0); 21751215Sbostic } 21853572Sheideman 21953572Sheideman 220