123394Smckusick /* 251492Sbostic * Copyright (c) 1991 Regents of the University of California. 334432Sbostic * All rights reserved. 423394Smckusick * 544536Sbostic * %sccs.include.redist.c% 634432Sbostic * 7*52680Sstaelin * @(#)lfs_alloc.c 7.41 (Berkeley) 02/27/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 1851492Sbostic #include <ufs/ufs/quota.h> 1951492Sbostic #include <ufs/ufs/inode.h> 2051492Sbostic #include <ufs/ufs/ufsmount.h> 2147571Skarels 2251492Sbostic #include <ufs/lfs/lfs.h> 2351492Sbostic #include <ufs/lfs/lfs_extern.h> 2451485Sbostic 2551492Sbostic extern u_long nextgennumber; 2651492Sbostic 2751485Sbostic /* Allocate a new inode. */ 2851485Sbostic /* ARGSUSED */ 2951485Sbostic int 3051560Smckusick lfs_valloc(pvp, notused, cred, vpp) 3152080Sbostic struct vnode *pvp, **vpp; 3251485Sbostic int notused; 3352080Sbostic struct ucred *cred; 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. */ 4951560Smckusick fs = VTOI(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", 7552080Sbostic 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); 9552080Sbostic vnode_pager_setsize(vp, ip->i_size); 9652080Sbostic vnode_pager_uncache(vp); 9752080Sbostic LFS_UBWRITE(bp); 9852080Sbostic } 9952080Sbostic 10051215Sbostic /* Create a vnode to associate with the inode. */ 10151560Smckusick if (error = lfs_vcreate(pvp->v_mount, new_ino, &vp)) 10237735Smckusick return (error); 10351560Smckusick *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 { 13151560Smckusick extern struct vnodeops lfs_vnodeops; 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. */ 14051990Smckusick if (error = getnewvnode(VT_LFS, mp, &lfs_vnodeops, 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 */ 17051215Sbostic void 17151560Smckusick lfs_vfree(vp, notused1, notused2) 17252080Sbostic struct vnode *vp; 17351485Sbostic ino_t notused1; 17451485Sbostic int notused2; 17551215Sbostic { 176*52680Sstaelin SEGUSE *sup; 17752080Sbostic struct buf *bp; 17852080Sbostic struct ifile *ifp; 17952080Sbostic struct inode *ip; 18051492Sbostic struct lfs *fs; 181*52680Sstaelin daddr_t old_iaddr; 18251215Sbostic ino_t ino; 18351215Sbostic 18451560Smckusick ip = VTOI(vp); 18551854Sbostic #ifdef VERBOSE 18651854Sbostic printf("lfs_vfree: free %d\n", ip->i_number); 18751485Sbostic #endif 18851485Sbostic /* Get the inode number and file system. */ 18951215Sbostic fs = ip->i_lfs; 19051215Sbostic ino = ip->i_number; 19151485Sbostic 19251485Sbostic /* 19351933Sbostic * Set the ifile's inode entry to unused, increment its version number 19451933Sbostic * and link it into the free chain. 19551485Sbostic */ 19651215Sbostic LFS_IENTRY(ifp, fs, ino, bp); 197*52680Sstaelin old_iaddr = ifp->if_daddr; 19851485Sbostic ifp->if_daddr = LFS_UNUSED_DADDR; 19951485Sbostic ++ifp->if_version; 20051485Sbostic ifp->if_nextfree = fs->lfs_free; 20151485Sbostic fs->lfs_free = ino; 20252080Sbostic LFS_UBWRITE(bp); 20351215Sbostic 204*52680Sstaelin if (old_iaddr != LFS_UNUSED_DADDR) { 205*52680Sstaelin LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp); 206*52680Sstaelin #ifdef DIAGNOSTIC 207*52680Sstaelin if (sup->su_nbytes < sizeof(struct dinode)) 208*52680Sstaelin panic("lfs_vfree: negative byte count (segment %d)\n", 209*52680Sstaelin datosn(fs, old_iaddr)); 210*52680Sstaelin #endif 211*52680Sstaelin sup->su_nbytes -= sizeof(struct dinode); 212*52680Sstaelin LFS_UBWRITE(bp); 213*52680Sstaelin } 214*52680Sstaelin 21551485Sbostic /* Set superblock modified bit and decrement file count. */ 21651485Sbostic fs->lfs_fmod = 1; 21751485Sbostic --fs->lfs_nfiles; 21851215Sbostic } 219