xref: /original-bsd/sys/ufs/lfs/lfs_alloc.c (revision 6093a5ae)
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.49 (Berkeley) 07/23/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 error;
49 
50 #ifdef VERBOSE
51 	printf("lfs_valloc\n");
52 #endif
53 	/* Get the head of the freelist. */
54 	fs = VTOI(ap->a_pvp)->i_lfs;
55 	new_ino = fs->lfs_free;
56 #ifdef ALLOCPRINT
57 	printf("lfs_ialloc: allocate inode %d\n", new_ino);
58 #endif
59 
60 	/*
61 	 * Remove the inode from the free list and write the new start
62 	 * of the free list into the superblock.
63 	 */
64 	LFS_IENTRY(ifp, fs, new_ino, bp);
65 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
66 		panic("lfs_ialloc: inuse inode on the free list");
67 	fs->lfs_free = ifp->if_nextfree;
68 	brelse(bp);
69 
70 	/* Extend IFILE so that the next lfs_valloc will succeed. */
71 	if (fs->lfs_free == LFS_UNUSED_INUM) {
72 		vp = fs->lfs_ivnode;
73 		ip = VTOI(vp);
74 		blkno = lblkno(fs, ip->i_size);
75 printf("Extending ifile: blkno = %d\n", blkno);
76 		bp = getblk(vp, blkno, fs->lfs_bsize);
77 		if (!bp) {
78 			uprintf("\n%s: no inodes left\n", fs->lfs_fsmnt);
79 			log(LOG_ERR, "uid %d on %s: out of inodes\n",
80 			    ap->a_cred->cr_uid, fs->lfs_fsmnt);
81 			return (ENOSPC);
82 		}
83 		i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
84 		    fs->lfs_ifpb;
85 printf("Extending ifile: first inum = %d\n", i);
86 		fs->lfs_free = i;
87 		max = i + fs->lfs_ifpb;
88 printf("Extending ifile: max inum = %d\n", max);
89 		for (ifp = (struct ifile *)bp->b_un.b_words; i < max; ++ifp) {
90 			ifp->if_version = 1;
91 			ifp->if_daddr = LFS_UNUSED_DADDR;
92 			ifp->if_nextfree = ++i;
93 		}
94 		ifp--;
95 		ifp->if_nextfree = LFS_UNUSED_INUM;
96 
97 		ip->i_blocks += btodb(fs->lfs_bsize);
98 		fs->lfs_bfree -= btodb(fs->lfs_bsize);
99 		ip->i_size += fs->lfs_bsize;
100 printf("Extending ifile: blocks = %d size = %d\n", ip->i_blocks, ip->i_size);
101 		vnode_pager_setsize(vp, (u_long)ip->i_size);
102 		vnode_pager_uncache(vp);
103 		LFS_UBWRITE(bp);
104 	}
105 
106 	/* Create a vnode to associate with the inode. */
107 	if (error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp))
108 		return (error);
109 	*ap->a_vpp = vp;
110 	vp->v_flag |= VDIROP;
111 	ip = VTOI(vp);
112 	VREF(ip->i_devvp);
113 
114 	/* Zero out the direct and indirect block addresses. */
115 	bzero(ip->i_db, (NDADDR + NIADDR) * sizeof(daddr_t));
116 
117 	/* Set a new generation number for this inode. */
118 	if (++nextgennumber < (u_long)time.tv_sec)
119 		nextgennumber = time.tv_sec;
120 	ip->i_gen = nextgennumber;
121 
122 	/* Insert into the inode hash table. */
123 	ufs_ihashins(ip);
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 #ifdef VERBOSE
144 	printf("lfs_vcreate: ino %d\n", ino);
145 #endif
146 	/* Create the vnode. */
147 	if (error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) {
148 		*vpp = NULL;
149 		return (error);
150 	}
151 
152 	/* Get a pointer to the private mount structure. */
153 	ump = VFSTOUFS(mp);
154 
155 	/* Initialize the inode. */
156 	MALLOC(ip, struct inode *, sizeof(struct inode), M_LFSNODE, M_WAITOK);
157 	(*vpp)->v_data = ip;
158 	ip->i_vnode = *vpp;
159 	ip->i_devvp = ump->um_devvp;
160 	ip->i_flag = 0;
161 	ip->i_dev = ump->um_dev;
162 	ip->i_number = ip->i_din.di_inum = ino;
163 	ip->i_lfs = ump->um_lfs;
164 #ifdef QUOTA
165 	for (i = 0; i < MAXQUOTAS; i++)
166 		ip->i_dquot[i] = NODQUOT;
167 #endif
168 	ip->i_lockf = 0;
169 	ip->i_diroff = 0;
170 	ip->i_mode = 0;
171 	ip->i_size = 0;
172 	ip->i_blocks = 0;
173 	return (0);
174 }
175 
176 /* Free an inode. */
177 /* ARGUSED */
178 int
179 lfs_vfree(ap)
180 	struct vop_vfree_args /* {
181 		struct vnode *a_pvp;
182 		ino_t a_ino;
183 		int a_mode;
184 	} */ *ap;
185 {
186 	SEGUSE *sup;
187 	struct buf *bp;
188 	struct ifile *ifp;
189 	struct inode *ip;
190 	struct lfs *fs;
191 	daddr_t old_iaddr;
192 	ino_t ino;
193 
194 	ip = VTOI(ap->a_pvp);
195 #ifdef VERBOSE
196 	printf("lfs_vfree: free %d\n", ip->i_number);
197 #endif
198 	/* Get the inode number and file system. */
199 	fs = ip->i_lfs;
200 	ino = ip->i_number;
201 
202 	/*
203 	 * Set the ifile's inode entry to unused, increment its version number
204 	 * and link it into the free chain.
205 	 */
206 	LFS_IENTRY(ifp, fs, ino, bp);
207 	old_iaddr = ifp->if_daddr;
208 	ifp->if_daddr = LFS_UNUSED_DADDR;
209 	++ifp->if_version;
210 	ifp->if_nextfree = fs->lfs_free;
211 	fs->lfs_free = ino;
212 	LFS_UBWRITE(bp);
213 
214 	if (old_iaddr != LFS_UNUSED_DADDR) {
215 		LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
216 #ifdef DIAGNOSTIC
217 		if (sup->su_nbytes < sizeof(struct dinode))
218 			panic("lfs_vfree: negative byte count (segment %d)\n",
219 			    datosn(fs, old_iaddr));
220 #endif
221 		sup->su_nbytes -= sizeof(struct dinode);
222 		LFS_UBWRITE(bp);
223 	}
224 
225 	/* Set superblock modified bit and decrement file count. */
226 	fs->lfs_fmod = 1;
227 	--fs->lfs_nfiles;
228 	return (0);
229 }
230