xref: /csrg-svn/sys/ufs/lfs/lfs_alloc.c (revision 56028)
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.53 (Berkeley) 08/25/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 	/* Get the head of the freelist. */
51 	fs = VTOI(ap->a_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 		bp = getblk(vp, blkno, fs->lfs_bsize);
73 		i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
74 		    fs->lfs_ifpb;
75 		fs->lfs_free = i;
76 		max = i + fs->lfs_ifpb;
77 		for (ifp = (struct ifile *)bp->b_un.b_words; i < max; ++ifp) {
78 			ifp->if_version = 1;
79 			ifp->if_daddr = LFS_UNUSED_DADDR;
80 			ifp->if_nextfree = ++i;
81 		}
82 		ifp--;
83 		ifp->if_nextfree = LFS_UNUSED_INUM;
84 
85 		ip->i_blocks += btodb(fs->lfs_bsize);
86 		fs->lfs_bfree -= btodb(fs->lfs_bsize);
87 		ip->i_size += fs->lfs_bsize;
88 		vnode_pager_setsize(vp, (u_long)ip->i_size);
89 		vnode_pager_uncache(vp);
90 		if (error = VOP_BWRITE(bp))
91 			return (error);
92 	}
93 
94 	/* Create a vnode to associate with the inode. */
95 	if (error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp))
96 		return (error);
97 	*ap->a_vpp = vp;
98 	vp->v_flag |= VDIROP;
99 	ip = VTOI(vp);
100 	VREF(ip->i_devvp);
101 
102 	/* Zero out the direct and indirect block addresses. */
103 	bzero(ip->i_db, (NDADDR + NIADDR) * sizeof(daddr_t));
104 
105 	/* Set a new generation number for this inode. */
106 	if (++nextgennumber < (u_long)time.tv_sec)
107 		nextgennumber = time.tv_sec;
108 	ip->i_gen = nextgennumber;
109 
110 	/* Insert into the inode hash table. */
111 	ufs_ihashins(ip);
112 
113 	/* Set superblock modified bit and increment file count. */
114 	fs->lfs_fmod = 1;
115 	++fs->lfs_nfiles;
116 	return (0);
117 }
118 
119 /* Create a new vnode/inode pair and initialize what fields we can. */
120 int
121 lfs_vcreate(mp, ino, vpp)
122 	struct mount *mp;
123 	ino_t ino;
124 	struct vnode **vpp;
125 {
126 	extern int (**lfs_vnodeop_p)();
127 	struct inode *ip;
128 	struct ufsmount *ump;
129 	int error, i;
130 
131 	/* Create the vnode. */
132 	if (error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) {
133 		*vpp = NULL;
134 		return (error);
135 	}
136 
137 	/* Get a pointer to the private mount structure. */
138 	ump = VFSTOUFS(mp);
139 
140 	/* Initialize the inode. */
141 	MALLOC(ip, struct inode *, sizeof(struct inode), M_LFSNODE, M_WAITOK);
142 	(*vpp)->v_data = ip;
143 	ip->i_vnode = *vpp;
144 	ip->i_devvp = ump->um_devvp;
145 	ip->i_flag = IMOD;
146 	ip->i_dev = ump->um_dev;
147 	ip->i_number = ip->i_din.di_inum = ino;
148 	ip->i_lfs = ump->um_lfs;
149 #ifdef QUOTA
150 	for (i = 0; i < MAXQUOTAS; i++)
151 		ip->i_dquot[i] = NODQUOT;
152 #endif
153 	ip->i_lockf = 0;
154 	ip->i_diroff = 0;
155 	ip->i_mode = 0;
156 	ip->i_size = 0;
157 	ip->i_blocks = 0;
158 	++ump->um_lfs->lfs_uinodes;
159 	return (0);
160 }
161 
162 /* Free an inode. */
163 /* ARGUSED */
164 int
165 lfs_vfree(ap)
166 	struct vop_vfree_args /* {
167 		struct vnode *a_pvp;
168 		ino_t a_ino;
169 		int a_mode;
170 	} */ *ap;
171 {
172 	SEGUSE *sup;
173 	struct buf *bp;
174 	struct ifile *ifp;
175 	struct inode *ip;
176 	struct lfs *fs;
177 	daddr_t old_iaddr;
178 	ino_t ino;
179 	int error;
180 
181 	/* Get the inode number and file system. */
182 	ip = VTOI(ap->a_pvp);
183 	fs = ip->i_lfs;
184 	ino = ip->i_number;
185 	if (ip->i_flag & IMOD) {
186 		--fs->lfs_uinodes;
187 		ip->i_flag &= ~(IMOD | IACC | IUPD | ICHG);
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 	old_iaddr = ifp->if_daddr;
195 	ifp->if_daddr = LFS_UNUSED_DADDR;
196 	++ifp->if_version;
197 	ifp->if_nextfree = fs->lfs_free;
198 	fs->lfs_free = ino;
199 	(void) VOP_BWRITE(bp);
200 
201 	if (old_iaddr != LFS_UNUSED_DADDR) {
202 		LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
203 #ifdef DIAGNOSTIC
204 		if (sup->su_nbytes < sizeof(struct dinode))
205 			panic("lfs_vfree: negative byte count (segment %d)\n",
206 			    datosn(fs, old_iaddr));
207 #endif
208 		sup->su_nbytes -= sizeof(struct dinode);
209 		(void) VOP_BWRITE(bp);
210 	}
211 
212 	/* Set superblock modified bit and decrement file count. */
213 	fs->lfs_fmod = 1;
214 	--fs->lfs_nfiles;
215 	return (0);
216 }
217