123394Smckusick /* 251492Sbostic * Copyright (c) 1991 Regents of the University of California. 334432Sbostic * All rights reserved. 423394Smckusick * 544536Sbostic * %sccs.include.redist.c% 634432Sbostic * 7*53320Smckusick * @(#)lfs_alloc.c 7.42 (Berkeley) 05/04/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 18*53320Smckusick #include <vm/vm.h> 19*53320Smckusick 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 3251560Smckusick lfs_valloc(pvp, notused, cred, vpp) 3352080Sbostic struct vnode *pvp, **vpp; 3451485Sbostic int notused; 3552080Sbostic struct ucred *cred; 3651155Sbostic { 3751492Sbostic struct lfs *fs; 3852080Sbostic struct buf *bp; 3952080Sbostic struct ifile *ifp; 4052080Sbostic struct inode *ip; 4152080Sbostic struct vnode *vp; 4252080Sbostic daddr_t blkno; 4351155Sbostic ino_t new_ino; 4452080Sbostic u_long i, max; 4551155Sbostic int error; 464359Smckusick 4751854Sbostic #ifdef VERBOSE 4851854Sbostic printf("lfs_valloc\n"); 4951854Sbostic #endif 5051215Sbostic /* Get the head of the freelist. */ 5151560Smckusick fs = VTOI(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 printf("Extending ifile: blkno = %d\n", blkno); 7352080Sbostic bp = getblk(vp, blkno, fs->lfs_bsize); 7452080Sbostic if (!bp) { 7552080Sbostic uprintf("\n%s: no inodes left\n", fs->lfs_fsmnt); 7652080Sbostic log(LOG_ERR, "uid %d on %s: out of inodes\n", 7752080Sbostic cred->cr_uid, fs->lfs_fsmnt); 7852080Sbostic return (ENOSPC); 7952080Sbostic } 8052080Sbostic i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) * 8152080Sbostic fs->lfs_ifpb; 8252080Sbostic printf("Extending ifile: first inum = %d\n", i); 8352080Sbostic fs->lfs_free = i; 8452080Sbostic max = i + fs->lfs_ifpb; 8552080Sbostic printf("Extending ifile: max inum = %d\n", max); 8652080Sbostic for (ifp = (struct ifile *)bp->b_un.b_words; i < max; ++ifp) { 8752080Sbostic ifp->if_version = 1; 8852080Sbostic ifp->if_daddr = LFS_UNUSED_DADDR; 8952080Sbostic ifp->if_nextfree = ++i; 9052080Sbostic } 9152080Sbostic ifp--; 9252080Sbostic ifp->if_nextfree = LFS_UNUSED_INUM; 9352080Sbostic 9452080Sbostic ++ip->i_blocks; /* XXX This may not be right. */ 9552080Sbostic ip->i_size += fs->lfs_bsize; 9652080Sbostic printf("Extending ifile: blocks = %d size = %d\n", ip->i_blocks, ip->i_size); 97*53320Smckusick vnode_pager_setsize(vp, (u_long)ip->i_size); 9852080Sbostic vnode_pager_uncache(vp); 9952080Sbostic LFS_UBWRITE(bp); 10052080Sbostic } 10152080Sbostic 10251215Sbostic /* Create a vnode to associate with the inode. */ 10351560Smckusick if (error = lfs_vcreate(pvp->v_mount, new_ino, &vp)) 10437735Smckusick return (error); 10551560Smckusick *vpp = vp; 10651560Smckusick ip = VTOI(vp); 10751560Smckusick VREF(ip->i_devvp); 10851155Sbostic 10952080Sbostic /* Zero out the direct and indirect block addresses. */ 11052080Sbostic bzero(ip->i_db, (NDADDR + NIADDR) * sizeof(daddr_t)); 11152080Sbostic 11251215Sbostic /* Set a new generation number for this inode. */ 11338255Smckusick if (++nextgennumber < (u_long)time.tv_sec) 11438255Smckusick nextgennumber = time.tv_sec; 11538255Smckusick ip->i_gen = nextgennumber; 1164359Smckusick 11751215Sbostic /* Insert into the inode hash table. */ 11851485Sbostic ufs_ihashins(ip); 1194359Smckusick 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 { 13351560Smckusick extern struct vnodeops lfs_vnodeops; 13452080Sbostic struct inode *ip; 13552080Sbostic struct ufsmount *ump; 13651155Sbostic int error, i; 1374359Smckusick 13851854Sbostic #ifdef VERBOSE 13951485Sbostic printf("lfs_vcreate: ino %d\n", ino); 14051485Sbostic #endif 14151215Sbostic /* Create the vnode. */ 14251990Smckusick if (error = getnewvnode(VT_LFS, mp, &lfs_vnodeops, vpp)) { 14351990Smckusick *vpp = NULL; 14451311Sbostic return (error); 14551990Smckusick } 1464359Smckusick 14751215Sbostic /* Get a pointer to the private mount structure. */ 14851155Sbostic ump = VFSTOUFS(mp); 1494651Smckusic 15051155Sbostic /* Initialize the inode. */ 15151990Smckusick MALLOC(ip, struct inode *, sizeof(struct inode), M_LFSNODE, M_WAITOK); 15251990Smckusick (*vpp)->v_data = ip; 15351560Smckusick ip->i_vnode = *vpp; 15452273Sbostic ip->i_devvp = ump->um_devvp; 15551560Smckusick ip->i_flag = 0; 15651155Sbostic ip->i_dev = ump->um_dev; 15751560Smckusick ip->i_number = ip->i_din.di_inum = ino; 15851155Sbostic ip->i_lfs = ump->um_lfs; 15951155Sbostic #ifdef QUOTA 16051155Sbostic for (i = 0; i < MAXQUOTAS; i++) 16151155Sbostic ip->i_dquot[i] = NODQUOT; 16251155Sbostic #endif 16352273Sbostic ip->i_lockf = 0; 16452273Sbostic ip->i_diroff = 0; 16552273Sbostic ip->i_mode = 0; 16652273Sbostic ip->i_size = 0; 16751155Sbostic return (0); 1684651Smckusic } 16951183Sbostic 17051485Sbostic /* Free an inode. */ 17151485Sbostic /* ARGUSED */ 17251215Sbostic void 17351560Smckusick lfs_vfree(vp, notused1, notused2) 17452080Sbostic struct vnode *vp; 17551485Sbostic ino_t notused1; 17651485Sbostic int notused2; 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; 18551215Sbostic 18651560Smckusick ip = VTOI(vp); 18751854Sbostic #ifdef VERBOSE 18851854Sbostic printf("lfs_vfree: free %d\n", ip->i_number); 18951485Sbostic #endif 19051485Sbostic /* Get the inode number and file system. */ 19151215Sbostic fs = ip->i_lfs; 19251215Sbostic ino = ip->i_number; 19351485Sbostic 19451485Sbostic /* 19551933Sbostic * Set the ifile's inode entry to unused, increment its version number 19651933Sbostic * and link it into the free chain. 19751485Sbostic */ 19851215Sbostic LFS_IENTRY(ifp, fs, ino, bp); 19952680Sstaelin old_iaddr = ifp->if_daddr; 20051485Sbostic ifp->if_daddr = LFS_UNUSED_DADDR; 20151485Sbostic ++ifp->if_version; 20251485Sbostic ifp->if_nextfree = fs->lfs_free; 20351485Sbostic fs->lfs_free = ino; 20452080Sbostic LFS_UBWRITE(bp); 20551215Sbostic 20652680Sstaelin if (old_iaddr != LFS_UNUSED_DADDR) { 20752680Sstaelin LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp); 20852680Sstaelin #ifdef DIAGNOSTIC 20952680Sstaelin if (sup->su_nbytes < sizeof(struct dinode)) 21052680Sstaelin panic("lfs_vfree: negative byte count (segment %d)\n", 21152680Sstaelin datosn(fs, old_iaddr)); 21252680Sstaelin #endif 21352680Sstaelin sup->su_nbytes -= sizeof(struct dinode); 21452680Sstaelin LFS_UBWRITE(bp); 21552680Sstaelin } 21652680Sstaelin 21751485Sbostic /* Set superblock modified bit and decrement file count. */ 21851485Sbostic fs->lfs_fmod = 1; 21951485Sbostic --fs->lfs_nfiles; 22051215Sbostic } 221