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.35 (Berkeley) 11/05/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 17 #include <ufs/ufs/quota.h> 18 #include <ufs/ufs/inode.h> 19 #include <ufs/ufs/ufsmount.h> 20 21 #include <ufs/lfs/lfs.h> 22 #include <ufs/lfs/lfs_extern.h> 23 24 extern u_long nextgennumber; 25 26 /* Allocate a new inode. */ 27 /* ARGSUSED */ 28 int 29 lfs_valloc(pvp, notused, cred, vpp) 30 VNODE *pvp, **vpp; 31 int notused; 32 UCRED *cred; 33 { 34 struct lfs *fs; 35 BUF *bp; 36 IFILE *ifp; 37 INODE *ip; 38 VNODE *vp; 39 ino_t new_ino; 40 int error; 41 42 /* Get the head of the freelist. */ 43 fs = VTOI(pvp)->i_lfs; 44 new_ino = fs->lfs_free; 45 if (new_ino == LFS_UNUSED_INUM) { 46 /* 47 * XXX 48 * Currently, no more inodes are allocated if the ifile fills 49 * up. The ifile should be extended instead. 50 */ 51 uprintf("\n%s: no inodes left\n", fs->lfs_fsmnt); 52 log(LOG_ERR, "uid %d on %s: out of inodes\n", 53 cred->cr_uid, fs->lfs_fsmnt); 54 return (ENOSPC); 55 } 56 #ifdef ALLOCPRINT 57 printf("lfs_ialloc: allocate inode %d\n", new_ino); 58 #endif 59 60 /* Read the appropriate block from the ifile. */ 61 LFS_IENTRY(ifp, fs, new_ino, bp); 62 63 if (ifp->if_daddr != LFS_UNUSED_DADDR) 64 panic("lfs_ialloc: inuse inode on the free list"); 65 66 /* Remove from the free list, set the access time, write it back. */ 67 fs->lfs_free = ifp->if_nextfree; 68 ifp->if_st_atime = time.tv_sec; 69 lfs_bwrite(bp); 70 71 /* Create a vnode to associate with the inode. */ 72 if (error = lfs_vcreate(pvp->v_mount, new_ino, &vp)) 73 return (error); 74 *vpp = vp; 75 ip = VTOI(vp); 76 VREF(ip->i_devvp); 77 78 /* Set a new generation number for this inode. */ 79 if (++nextgennumber < (u_long)time.tv_sec) 80 nextgennumber = time.tv_sec; 81 ip->i_gen = nextgennumber; 82 83 /* Insert into the inode hash table. */ 84 ufs_ihashins(ip); 85 86 /* Set superblock modified bit and increment file count. */ 87 fs->lfs_fmod = 1; 88 ++fs->lfs_nfiles; 89 return (0); 90 } 91 92 /* Create a new vnode/inode pair and initialize what fields we can. */ 93 int 94 lfs_vcreate(mp, ino, vpp) 95 MOUNT *mp; 96 ino_t ino; 97 VNODE **vpp; 98 { 99 extern struct vnodeops lfs_vnodeops; 100 INODE *ip; 101 UFSMOUNT *ump; 102 int error, i; 103 104 #ifdef ALLOCPRINT 105 printf("lfs_vcreate: ino %d\n", ino); 106 #endif 107 /* Create the vnode. */ 108 if (error = getnewvnode(VT_LFS, mp, &lfs_vnodeops, vpp)) 109 return (error); 110 111 /* Get a pointer to the private mount structure. */ 112 ump = VFSTOUFS(mp); 113 114 /* Initialize the inode. */ 115 ip = VTOI(*vpp); 116 ip->i_vnode = *vpp; 117 ip->i_flag = 0; 118 ip->i_mode = 0; 119 ip->i_diroff = 0; 120 ip->i_lockf = 0; 121 ip->i_dev = ump->um_dev; 122 ip->i_number = ip->i_din.di_inum = ino; 123 ip->i_lfs = ump->um_lfs; 124 ip->i_devvp = ump->um_devvp; 125 #ifdef QUOTA 126 for (i = 0; i < MAXQUOTAS; i++) 127 ip->i_dquot[i] = NODQUOT; 128 #endif 129 return (0); 130 } 131 132 /* Free an inode. */ 133 /* ARGUSED */ 134 void 135 lfs_vfree(vp, notused1, notused2) 136 VNODE *vp; 137 ino_t notused1; 138 int notused2; 139 { 140 BUF *bp; 141 IFILE *ifp; 142 INODE *ip; 143 struct lfs *fs; 144 ino_t ino; 145 146 ip = VTOI(vp); 147 #ifdef ALLOCPRINT 148 printf("lfs_ifree: free %d\n", ip->i_number); 149 #endif 150 /* Get the inode number and file system. */ 151 fs = ip->i_lfs; 152 ino = ip->i_number; 153 154 /* 155 * Read the appropriate block from the ifile. Set the inode entry to 156 * unused, increment its version number and link it into the free chain. 157 */ 158 LFS_IENTRY(ifp, fs, ino, bp); 159 ifp->if_daddr = LFS_UNUSED_DADDR; 160 ++ifp->if_version; 161 ifp->if_nextfree = fs->lfs_free; 162 fs->lfs_free = ino; 163 164 lfs_bwrite(bp); 165 166 /* Set superblock modified bit and decrement file count. */ 167 fs->lfs_fmod = 1; 168 --fs->lfs_nfiles; 169 } 170