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