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.44 (Berkeley) 05/15/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 *ap; 34 #define pvp (ap->a_pvp) 35 #define notused (ap->a_mode) 36 #define cred (ap->a_cred) 37 #define vpp (ap->a_vpp) 38 { 39 struct lfs *fs; 40 struct buf *bp; 41 struct ifile *ifp; 42 struct inode *ip; 43 struct vnode *vp; 44 daddr_t blkno; 45 ino_t new_ino; 46 u_long i, max; 47 int error; 48 49 #ifdef VERBOSE 50 printf("lfs_valloc\n"); 51 #endif 52 /* Get the head of the freelist. */ 53 fs = VTOI(pvp)->i_lfs; 54 new_ino = fs->lfs_free; 55 #ifdef ALLOCPRINT 56 printf("lfs_ialloc: allocate inode %d\n", new_ino); 57 #endif 58 59 /* 60 * Remove the inode from the free list and write the new start 61 * of the free list into the superblock. 62 */ 63 LFS_IENTRY(ifp, fs, new_ino, bp); 64 if (ifp->if_daddr != LFS_UNUSED_DADDR) 65 panic("lfs_ialloc: inuse inode on the free list"); 66 fs->lfs_free = ifp->if_nextfree; 67 brelse(bp); 68 69 /* Extend IFILE so that the next lfs_valloc will succeed. */ 70 if (fs->lfs_free == LFS_UNUSED_INUM) { 71 vp = fs->lfs_ivnode; 72 ip = VTOI(vp); 73 blkno = lblkno(fs, ip->i_size); 74 printf("Extending ifile: blkno = %d\n", blkno); 75 bp = getblk(vp, blkno, fs->lfs_bsize); 76 if (!bp) { 77 uprintf("\n%s: no inodes left\n", fs->lfs_fsmnt); 78 log(LOG_ERR, "uid %d on %s: out of inodes\n", 79 cred->cr_uid, fs->lfs_fsmnt); 80 return (ENOSPC); 81 } 82 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) * 83 fs->lfs_ifpb; 84 printf("Extending ifile: first inum = %d\n", i); 85 fs->lfs_free = i; 86 max = i + fs->lfs_ifpb; 87 printf("Extending ifile: max inum = %d\n", max); 88 for (ifp = (struct ifile *)bp->b_un.b_words; i < max; ++ifp) { 89 ifp->if_version = 1; 90 ifp->if_daddr = LFS_UNUSED_DADDR; 91 ifp->if_nextfree = ++i; 92 } 93 ifp--; 94 ifp->if_nextfree = LFS_UNUSED_INUM; 95 96 ++ip->i_blocks; /* XXX This may not be right. */ 97 ip->i_size += fs->lfs_bsize; 98 printf("Extending ifile: blocks = %d size = %d\n", ip->i_blocks, ip->i_size); 99 vnode_pager_setsize(vp, (u_long)ip->i_size); 100 vnode_pager_uncache(vp); 101 LFS_UBWRITE(bp); 102 } 103 104 /* Create a vnode to associate with the inode. */ 105 if (error = lfs_vcreate(pvp->v_mount, new_ino, &vp)) 106 return (error); 107 *vpp = vp; 108 ip = VTOI(vp); 109 VREF(ip->i_devvp); 110 111 /* Zero out the direct and indirect block addresses. */ 112 bzero(ip->i_db, (NDADDR + NIADDR) * sizeof(daddr_t)); 113 114 /* Set a new generation number for this inode. */ 115 if (++nextgennumber < (u_long)time.tv_sec) 116 nextgennumber = time.tv_sec; 117 ip->i_gen = nextgennumber; 118 119 /* Insert into the inode hash table. */ 120 ufs_ihashins(ip); 121 122 /* Set superblock modified bit and increment file count. */ 123 fs->lfs_fmod = 1; 124 ++fs->lfs_nfiles; 125 return (0); 126 } 127 #undef pvp 128 #undef notused 129 #undef cred 130 #undef vpp 131 132 /* Create a new vnode/inode pair and initialize what fields we can. */ 133 int 134 lfs_vcreate(mp, ino, vpp) 135 struct mount *mp; 136 ino_t ino; 137 struct vnode **vpp; 138 { 139 extern int (**lfs_vnodeop_p)(); 140 struct inode *ip; 141 struct ufsmount *ump; 142 int error, i; 143 144 #ifdef VERBOSE 145 printf("lfs_vcreate: ino %d\n", ino); 146 #endif 147 /* Create the vnode. */ 148 if (error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) { 149 *vpp = NULL; 150 return (error); 151 } 152 153 /* Get a pointer to the private mount structure. */ 154 ump = VFSTOUFS(mp); 155 156 /* Initialize the inode. */ 157 MALLOC(ip, struct inode *, sizeof(struct inode), M_LFSNODE, M_WAITOK); 158 (*vpp)->v_data = ip; 159 ip->i_vnode = *vpp; 160 ip->i_devvp = ump->um_devvp; 161 ip->i_flag = 0; 162 ip->i_dev = ump->um_dev; 163 ip->i_number = ip->i_din.di_inum = ino; 164 ip->i_lfs = ump->um_lfs; 165 #ifdef QUOTA 166 for (i = 0; i < MAXQUOTAS; i++) 167 ip->i_dquot[i] = NODQUOT; 168 #endif 169 ip->i_lockf = 0; 170 ip->i_diroff = 0; 171 ip->i_mode = 0; 172 ip->i_size = 0; 173 return (0); 174 } 175 176 /* Free an inode. */ 177 /* ARGUSED */ 178 int 179 lfs_vfree (ap) 180 struct vop_vfree_args *ap; 181 #define vp (ap->a_pvp) 182 #define notused1 (ap->a_ino) 183 #define notused2 (ap->a_mode) 184 { 185 SEGUSE *sup; 186 struct buf *bp; 187 struct ifile *ifp; 188 struct inode *ip; 189 struct lfs *fs; 190 daddr_t old_iaddr; 191 ino_t ino; 192 193 ip = VTOI(vp); 194 #ifdef VERBOSE 195 printf("lfs_vfree: free %d\n", ip->i_number); 196 #endif 197 /* Get the inode number and file system. */ 198 fs = ip->i_lfs; 199 ino = ip->i_number; 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 LFS_UBWRITE(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 LFS_UBWRITE(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 #undef vp 230 #undef notused1 231 #undef notused2 232 233 234