xref: /original-bsd/sys/ufs/lfs/lfs_alloc.c (revision 2932bec8)
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.42 (Berkeley) 05/04/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(pvp, notused, cred, vpp)
33 	struct vnode *pvp, **vpp;
34 	int notused;
35 	struct ucred *cred;
36 {
37 	struct lfs *fs;
38 	struct buf *bp;
39 	struct ifile *ifp;
40 	struct inode *ip;
41 	struct vnode *vp;
42 	daddr_t blkno;
43 	ino_t new_ino;
44 	u_long i, max;
45 	int error;
46 
47 #ifdef VERBOSE
48 	printf("lfs_valloc\n");
49 #endif
50 	/* Get the head of the freelist. */
51 	fs = VTOI(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 		vp = fs->lfs_ivnode;
70 		ip = VTOI(vp);
71 		blkno = lblkno(fs, ip->i_size);
72 printf("Extending ifile: blkno = %d\n", blkno);
73 		bp = getblk(vp, blkno, fs->lfs_bsize);
74 		if (!bp) {
75 			uprintf("\n%s: no inodes left\n", fs->lfs_fsmnt);
76 			log(LOG_ERR, "uid %d on %s: out of inodes\n",
77 			    cred->cr_uid, fs->lfs_fsmnt);
78 			return (ENOSPC);
79 		}
80 		i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
81 		    fs->lfs_ifpb;
82 printf("Extending ifile: first inum = %d\n", i);
83 		fs->lfs_free = i;
84 		max = i + fs->lfs_ifpb;
85 printf("Extending ifile: max inum = %d\n", max);
86 		for (ifp = (struct ifile *)bp->b_un.b_words; i < max; ++ifp) {
87 			ifp->if_version = 1;
88 			ifp->if_daddr = LFS_UNUSED_DADDR;
89 			ifp->if_nextfree = ++i;
90 		}
91 		ifp--;
92 		ifp->if_nextfree = LFS_UNUSED_INUM;
93 
94 		++ip->i_blocks;			/* XXX This may not be right. */
95 		ip->i_size += fs->lfs_bsize;
96 printf("Extending ifile: blocks = %d size = %d\n", ip->i_blocks, ip->i_size);
97 		vnode_pager_setsize(vp, (u_long)ip->i_size);
98 		vnode_pager_uncache(vp);
99 		LFS_UBWRITE(bp);
100 	}
101 
102 	/* Create a vnode to associate with the inode. */
103 	if (error = lfs_vcreate(pvp->v_mount, new_ino, &vp))
104 		return (error);
105 	*vpp = vp;
106 	ip = VTOI(vp);
107 	VREF(ip->i_devvp);
108 
109 	/* Zero out the direct and indirect block addresses. */
110 	bzero(ip->i_db, (NDADDR + NIADDR) * sizeof(daddr_t));
111 
112 	/* Set a new generation number for this inode. */
113 	if (++nextgennumber < (u_long)time.tv_sec)
114 		nextgennumber = time.tv_sec;
115 	ip->i_gen = nextgennumber;
116 
117 	/* Insert into the inode hash table. */
118 	ufs_ihashins(ip);
119 
120 	/* Set superblock modified bit and increment file count. */
121 	fs->lfs_fmod = 1;
122 	++fs->lfs_nfiles;
123 	return (0);
124 }
125 
126 /* Create a new vnode/inode pair and initialize what fields we can. */
127 int
128 lfs_vcreate(mp, ino, vpp)
129 	struct mount *mp;
130 	ino_t ino;
131 	struct vnode **vpp;
132 {
133 	extern struct vnodeops lfs_vnodeops;
134 	struct inode *ip;
135 	struct ufsmount *ump;
136 	int error, i;
137 
138 #ifdef VERBOSE
139 	printf("lfs_vcreate: ino %d\n", ino);
140 #endif
141 	/* Create the vnode. */
142 	if (error = getnewvnode(VT_LFS, mp, &lfs_vnodeops, vpp)) {
143 		*vpp = NULL;
144 		return (error);
145 	}
146 
147 	/* Get a pointer to the private mount structure. */
148 	ump = VFSTOUFS(mp);
149 
150 	/* Initialize the inode. */
151 	MALLOC(ip, struct inode *, sizeof(struct inode), M_LFSNODE, M_WAITOK);
152 	(*vpp)->v_data = ip;
153 	ip->i_vnode = *vpp;
154 	ip->i_devvp = ump->um_devvp;
155 	ip->i_flag = 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 #ifdef QUOTA
160 	for (i = 0; i < MAXQUOTAS; i++)
161 		ip->i_dquot[i] = NODQUOT;
162 #endif
163 	ip->i_lockf = 0;
164 	ip->i_diroff = 0;
165 	ip->i_mode = 0;
166 	ip->i_size = 0;
167 	return (0);
168 }
169 
170 /* Free an inode. */
171 /* ARGUSED */
172 void
173 lfs_vfree(vp, notused1, notused2)
174 	struct vnode *vp;
175 	ino_t notused1;
176 	int notused2;
177 {
178 	SEGUSE *sup;
179 	struct buf *bp;
180 	struct ifile *ifp;
181 	struct inode *ip;
182 	struct lfs *fs;
183 	daddr_t old_iaddr;
184 	ino_t ino;
185 
186 	ip = VTOI(vp);
187 #ifdef VERBOSE
188 	printf("lfs_vfree: free %d\n", ip->i_number);
189 #endif
190 	/* Get the inode number and file system. */
191 	fs = ip->i_lfs;
192 	ino = ip->i_number;
193 
194 	/*
195 	 * Set the ifile's inode entry to unused, increment its version number
196 	 * and link it into the free chain.
197 	 */
198 	LFS_IENTRY(ifp, fs, ino, bp);
199 	old_iaddr = ifp->if_daddr;
200 	ifp->if_daddr = LFS_UNUSED_DADDR;
201 	++ifp->if_version;
202 	ifp->if_nextfree = fs->lfs_free;
203 	fs->lfs_free = ino;
204 	LFS_UBWRITE(bp);
205 
206 	if (old_iaddr != LFS_UNUSED_DADDR) {
207 		LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
208 #ifdef DIAGNOSTIC
209 		if (sup->su_nbytes < sizeof(struct dinode))
210 			panic("lfs_vfree: negative byte count (segment %d)\n",
211 			    datosn(fs, old_iaddr));
212 #endif
213 		sup->su_nbytes -= sizeof(struct dinode);
214 		LFS_UBWRITE(bp);
215 	}
216 
217 	/* Set superblock modified bit and decrement file count. */
218 	fs->lfs_fmod = 1;
219 	--fs->lfs_nfiles;
220 }
221