1 /* 2 * Copyright (c) 1991 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)lfs_alloc.c 7.55 (Berkeley) 11/17/92 8 */ 9 10 #include <sys/param.h> 11 #include <sys/kernel.h> 12 #include <sys/buf.h> 13 #include <sys/vnode.h> 14 #include <sys/syslog.h> 15 #include <sys/mount.h> 16 #include <sys/malloc.h> 17 18 #include <vm/vm.h> 19 20 #include <ufs/ufs/quota.h> 21 #include <ufs/ufs/inode.h> 22 #include <ufs/ufs/ufsmount.h> 23 24 #include <ufs/lfs/lfs.h> 25 #include <ufs/lfs/lfs_extern.h> 26 27 extern u_long nextgennumber; 28 29 /* Allocate a new inode. */ 30 /* ARGSUSED */ 31 int 32 lfs_valloc(ap) 33 struct vop_valloc_args /* { 34 struct vnode *a_pvp; 35 int a_mode; 36 struct ucred *a_cred; 37 struct vnode **a_vpp; 38 } */ *ap; 39 { 40 struct lfs *fs; 41 struct buf *bp; 42 struct ifile *ifp; 43 struct inode *ip; 44 struct vnode *vp; 45 daddr_t blkno; 46 ino_t new_ino; 47 u_long i, max; 48 int bb, error; 49 50 /* Get the head of the freelist. */ 51 fs = VTOI(ap->a_pvp)->i_lfs; 52 new_ino = fs->lfs_free; 53 #ifdef ALLOCPRINT 54 printf("lfs_ialloc: allocate inode %d\n", new_ino); 55 #endif 56 57 /* 58 * Remove the inode from the free list and write the new start 59 * of the free list into the superblock. 60 */ 61 LFS_IENTRY(ifp, fs, new_ino, bp); 62 if (ifp->if_daddr != LFS_UNUSED_DADDR) 63 panic("lfs_ialloc: inuse inode on the free list"); 64 fs->lfs_free = ifp->if_nextfree; 65 brelse(bp); 66 67 /* Extend IFILE so that the next lfs_valloc will succeed. */ 68 if (fs->lfs_free == LFS_UNUSED_INUM) { 69 bb = fsbtodb(fs, 1); 70 if (!ISSPACE(fs, bb, ap->a_cred)) 71 return(ENOSPC); 72 vp = fs->lfs_ivnode; 73 ip = VTOI(vp); 74 blkno = lblkno(fs, ip->i_size); 75 bp = getblk(vp, blkno, fs->lfs_bsize); 76 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) * 77 fs->lfs_ifpb; 78 fs->lfs_free = i; 79 max = i + fs->lfs_ifpb; 80 for (ifp = (struct ifile *)bp->b_un.b_words; i < max; ++ifp) { 81 ifp->if_version = 1; 82 ifp->if_daddr = LFS_UNUSED_DADDR; 83 ifp->if_nextfree = ++i; 84 } 85 ifp--; 86 ifp->if_nextfree = LFS_UNUSED_INUM; 87 88 ip->i_blocks += btodb(fs->lfs_bsize); 89 fs->lfs_bfree -= btodb(fs->lfs_bsize); 90 ip->i_size += fs->lfs_bsize; 91 vnode_pager_setsize(vp, (u_long)ip->i_size); 92 vnode_pager_uncache(vp); 93 if (error = VOP_BWRITE(bp)) 94 return (error); 95 } 96 97 /* Create a vnode to associate with the inode. */ 98 if (error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp)) 99 return (error); 100 101 ip = VTOI(vp); 102 103 /* Zero out the direct and indirect block addresses. */ 104 bzero(&ip->i_din, sizeof(struct dinode)); 105 ip->i_din.di_inumber = new_ino; 106 107 /* Set a new generation number for this inode. */ 108 if (++nextgennumber < (u_long)time.tv_sec) 109 nextgennumber = time.tv_sec; 110 ip->i_gen = nextgennumber; 111 112 /* Insert into the inode hash table. */ 113 ufs_ihashins(ip); 114 115 if (error = ufs_vinit(vp->v_mount, lfs_specop_p, LFS_FIFOOPS, &vp)) { 116 vput(vp); 117 *ap->a_vpp = NULL; 118 return (error); 119 } 120 121 *ap->a_vpp = vp; 122 vp->v_flag |= VDIROP; 123 VREF(ip->i_devvp); 124 125 /* Set superblock modified bit and increment file count. */ 126 fs->lfs_fmod = 1; 127 ++fs->lfs_nfiles; 128 return (0); 129 } 130 131 /* Create a new vnode/inode pair and initialize what fields we can. */ 132 int 133 lfs_vcreate(mp, ino, vpp) 134 struct mount *mp; 135 ino_t ino; 136 struct vnode **vpp; 137 { 138 extern int (**lfs_vnodeop_p)(); 139 struct inode *ip; 140 struct ufsmount *ump; 141 int error, i; 142 143 /* Create the vnode. */ 144 if (error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) { 145 *vpp = NULL; 146 return (error); 147 } 148 149 /* Get a pointer to the private mount structure. */ 150 ump = VFSTOUFS(mp); 151 152 /* Initialize the inode. */ 153 MALLOC(ip, struct inode *, sizeof(struct inode), M_LFSNODE, M_WAITOK); 154 (*vpp)->v_data = ip; 155 ip->i_vnode = *vpp; 156 ip->i_devvp = ump->um_devvp; 157 ip->i_flag = IMOD; 158 ip->i_dev = ump->um_dev; 159 ip->i_number = ip->i_din.di_inumber = ino; 160 ip->i_lfs = ump->um_lfs; 161 #ifdef QUOTA 162 for (i = 0; i < MAXQUOTAS; i++) 163 ip->i_dquot[i] = NODQUOT; 164 #endif 165 ip->i_lockf = 0; 166 ip->i_diroff = 0; 167 ip->i_mode = 0; 168 ip->i_size = 0; 169 ip->i_blocks = 0; 170 ++ump->um_lfs->lfs_uinodes; 171 return (0); 172 } 173 174 /* Free an inode. */ 175 /* ARGUSED */ 176 int 177 lfs_vfree(ap) 178 struct vop_vfree_args /* { 179 struct vnode *a_pvp; 180 ino_t a_ino; 181 int a_mode; 182 } */ *ap; 183 { 184 SEGUSE *sup; 185 struct buf *bp; 186 struct ifile *ifp; 187 struct inode *ip; 188 struct lfs *fs; 189 daddr_t old_iaddr; 190 ino_t ino; 191 int error; 192 193 /* Get the inode number and file system. */ 194 ip = VTOI(ap->a_pvp); 195 fs = ip->i_lfs; 196 ino = ip->i_number; 197 if (ip->i_flag & IMOD) { 198 --fs->lfs_uinodes; 199 ip->i_flag &= ~(IMOD | IACC | IUPD | ICHG); 200 } 201 /* 202 * Set the ifile's inode entry to unused, increment its version number 203 * and link it into the free chain. 204 */ 205 LFS_IENTRY(ifp, fs, ino, bp); 206 old_iaddr = ifp->if_daddr; 207 ifp->if_daddr = LFS_UNUSED_DADDR; 208 ++ifp->if_version; 209 ifp->if_nextfree = fs->lfs_free; 210 fs->lfs_free = ino; 211 (void) VOP_BWRITE(bp); 212 213 if (old_iaddr != LFS_UNUSED_DADDR) { 214 LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp); 215 #ifdef DIAGNOSTIC 216 if (sup->su_nbytes < sizeof(struct dinode)) 217 panic("lfs_vfree: negative byte count (segment %d)\n", 218 datosn(fs, old_iaddr)); 219 #endif 220 sup->su_nbytes -= sizeof(struct dinode); 221 (void) VOP_BWRITE(bp); 222 } 223 224 /* Set superblock modified bit and decrement file count. */ 225 fs->lfs_fmod = 1; 226 --fs->lfs_nfiles; 227 return (0); 228 } 229