xref: /original-bsd/sys/ufs/lfs/lfs_alloc.c (revision 0ac4996f)
1 /*
2  * Copyright (c) 1991, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)lfs_alloc.c	8.7 (Berkeley) 05/14/95
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 #include <ufs/ufs/ufs_extern.h>
24 
25 #include <ufs/lfs/lfs.h>
26 #include <ufs/lfs/lfs_extern.h>
27 
28 extern u_long nextgennumber;
29 
30 /* Allocate a new inode. */
31 /* ARGSUSED */
32 int
33 lfs_valloc(ap)
34 	struct vop_valloc_args /* {
35 		struct vnode *a_pvp;
36 		int a_mode;
37 		struct ucred *a_cred;
38 		struct vnode **a_vpp;
39 	} */ *ap;
40 {
41 	struct lfs *fs;
42 	struct buf *bp;
43 	struct ifile *ifp;
44 	struct inode *ip;
45 	struct vnode *vp;
46 	ufs_daddr_t blkno;
47 	ino_t new_ino;
48 	u_long i, max;
49 	int error;
50 
51 	/* Get the head of the freelist. */
52 	fs = VTOI(ap->a_pvp)->i_lfs;
53 	new_ino = fs->lfs_free;
54 #ifdef ALLOCPRINT
55 	printf("lfs_ialloc: allocate inode %d\n", new_ino);
56 #endif
57 
58 	/*
59 	 * Remove the inode from the free list and write the new start
60 	 * of the free list into the superblock.
61 	 */
62 	LFS_IENTRY(ifp, fs, new_ino, bp);
63 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
64 		panic("lfs_ialloc: inuse inode on the free list");
65 	fs->lfs_free = ifp->if_nextfree;
66 	brelse(bp);
67 
68 	/* Extend IFILE so that the next lfs_valloc will succeed. */
69 	if (fs->lfs_free == LFS_UNUSED_INUM) {
70 		vp = fs->lfs_ivnode;
71 		ip = VTOI(vp);
72 		blkno = lblkno(fs, ip->i_size);
73 		lfs_balloc(vp, 0, fs->lfs_bsize, blkno, &bp);
74 		ip->i_size += fs->lfs_bsize;
75 		vnode_pager_setsize(vp, (u_long)ip->i_size);
76 		vnode_pager_uncache(vp);
77 
78 		i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
79 		    fs->lfs_ifpb;
80 		fs->lfs_free = i;
81 		max = i + fs->lfs_ifpb;
82 		for (ifp = (struct ifile *)bp->b_data; i < max; ++ifp) {
83 			ifp->if_version = 1;
84 			ifp->if_daddr = LFS_UNUSED_DADDR;
85 			ifp->if_nextfree = ++i;
86 		}
87 		ifp--;
88 		ifp->if_nextfree = LFS_UNUSED_INUM;
89 		if (error = VOP_BWRITE(bp))
90 			return (error);
91 	}
92 
93 	/* Create a vnode to associate with the inode. */
94 	if (error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp))
95 		return (error);
96 
97 
98 	ip = VTOI(vp);
99 	/* Zero out the direct and indirect block addresses. */
100 	bzero(&ip->i_din, sizeof(struct dinode));
101 	ip->i_din.di_inumber = new_ino;
102 
103 	/* Set a new generation number for this inode. */
104 	if (++nextgennumber < (u_long)time.tv_sec)
105 		nextgennumber = time.tv_sec;
106 	ip->i_gen = nextgennumber;
107 
108 	/* Insert into the inode hash table. */
109 	ufs_ihashins(ip);
110 
111 	if (error = ufs_vinit(vp->v_mount, lfs_specop_p, LFS_FIFOOPS, &vp)) {
112 		vput(vp);
113 		*ap->a_vpp = NULL;
114 		return (error);
115 	}
116 
117 	*ap->a_vpp = vp;
118 	vp->v_flag |= VDIROP;
119 	VREF(ip->i_devvp);
120 
121 	/* Set superblock modified bit and increment file count. */
122 	fs->lfs_fmod = 1;
123 	++fs->lfs_nfiles;
124 	return (0);
125 }
126 
127 /* Create a new vnode/inode pair and initialize what fields we can. */
128 int
129 lfs_vcreate(mp, ino, vpp)
130 	struct mount *mp;
131 	ino_t ino;
132 	struct vnode **vpp;
133 {
134 	extern int (**lfs_vnodeop_p)();
135 	struct inode *ip;
136 	struct ufsmount *ump;
137 	int error, i;
138 
139 	/* Create the vnode. */
140 	if (error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, 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 	lockinit(&ip->i_lock, PINOD, "lfsinode", 0, 0);
151 	(*vpp)->v_data = ip;
152 	ip->i_vnode = *vpp;
153 	ip->i_devvp = ump->um_devvp;
154 	ip->i_flag = IN_MODIFIED;
155 	ip->i_dev = ump->um_dev;
156 	ip->i_number = ip->i_din.di_inumber = ino;
157 	ip->i_lfs = ump->um_lfs;
158 #ifdef QUOTA
159 	for (i = 0; i < MAXQUOTAS; i++)
160 		ip->i_dquot[i] = NODQUOT;
161 #endif
162 	ip->i_lockf = 0;
163 	ip->i_diroff = 0;
164 	ip->i_mode = 0;
165 	ip->i_size = 0;
166 	ip->i_blocks = 0;
167 	++ump->um_lfs->lfs_uinodes;
168 	return (0);
169 }
170 
171 /* Free an inode. */
172 /* ARGUSED */
173 int
174 lfs_vfree(ap)
175 	struct vop_vfree_args /* {
176 		struct vnode *a_pvp;
177 		ino_t a_ino;
178 		int a_mode;
179 	} */ *ap;
180 {
181 	SEGUSE *sup;
182 	struct buf *bp;
183 	struct ifile *ifp;
184 	struct inode *ip;
185 	struct lfs *fs;
186 	ufs_daddr_t old_iaddr;
187 	ino_t ino;
188 
189 	/* Get the inode number and file system. */
190 	ip = VTOI(ap->a_pvp);
191 	fs = ip->i_lfs;
192 	ino = ip->i_number;
193 	if (ip->i_flag & IN_MODIFIED) {
194 		--fs->lfs_uinodes;
195 		ip->i_flag &=
196 		    ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
197 	}
198 	/*
199 	 * Set the ifile's inode entry to unused, increment its version number
200 	 * and link it into the free chain.
201 	 */
202 	LFS_IENTRY(ifp, fs, ino, bp);
203 	old_iaddr = ifp->if_daddr;
204 	ifp->if_daddr = LFS_UNUSED_DADDR;
205 	++ifp->if_version;
206 	ifp->if_nextfree = fs->lfs_free;
207 	fs->lfs_free = ino;
208 	(void) VOP_BWRITE(bp);
209 
210 	if (old_iaddr != LFS_UNUSED_DADDR) {
211 		LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
212 #ifdef DIAGNOSTIC
213 		if (sup->su_nbytes < sizeof(struct dinode))
214 			panic("lfs_vfree: negative byte count (segment %d)\n",
215 			    datosn(fs, old_iaddr));
216 #endif
217 		sup->su_nbytes -= sizeof(struct dinode);
218 		(void) VOP_BWRITE(bp);
219 	}
220 
221 	/* Set superblock modified bit and decrement file count. */
222 	fs->lfs_fmod = 1;
223 	--fs->lfs_nfiles;
224 	return (0);
225 }
226