lfs_alloc.c revision 1.43 1 /* $NetBSD: lfs_alloc.c,v 1.43 2000/09/09 04:49:54 perseant Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*
39 * Copyright (c) 1991, 1993
40 * The Regents of the University of California. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)lfs_alloc.c 8.4 (Berkeley) 1/4/94
71 */
72
73 #if defined(_KERNEL) && !defined(_LKM)
74 #include "opt_quota.h"
75 #endif
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/buf.h>
81 #include <sys/vnode.h>
82 #include <sys/syslog.h>
83 #include <sys/mount.h>
84 #include <sys/malloc.h>
85 #include <sys/pool.h>
86
87 #include <ufs/ufs/quota.h>
88 #include <ufs/ufs/inode.h>
89 #include <ufs/ufs/ufsmount.h>
90 #include <ufs/ufs/ufs_extern.h>
91
92 #include <ufs/lfs/lfs.h>
93 #include <ufs/lfs/lfs_extern.h>
94
95 extern int lfs_dirvcount;
96 extern struct lock ufs_hashlock;
97
98 /* Allocate a new inode. */
99 /* ARGSUSED */
100 /* VOP_BWRITE 2i times */
101 int
102 lfs_valloc(v)
103 void *v;
104 {
105 struct vop_valloc_args /* {
106 struct vnode *a_pvp;
107 int a_mode;
108 struct ucred *a_cred;
109 struct vnode **a_vpp;
110 } */ *ap = v;
111 struct lfs *fs;
112 struct buf *bp;
113 struct ifile *ifp;
114 struct inode *ip;
115 struct vnode *vp;
116 ufs_daddr_t blkno;
117 ino_t new_ino;
118 u_long i, max;
119 int error;
120 int new_gen;
121 extern int lfs_dirvcount;
122
123 fs = VTOI(ap->a_pvp)->i_lfs;
124 if (fs->lfs_ronly)
125 return EROFS;
126 *ap->a_vpp = NULL;
127
128 /*
129 * Use lfs_seglock here, instead of fs->lfs_freelock, to ensure that
130 * the free list is not changed in between the time that the ifile
131 * blocks are written to disk and the time that the superblock is
132 * written to disk.
133 *
134 * XXX this sucks. We should instead encode the head of the free
135 * list into the CLEANERINFO block of the Ifile.
136 */
137 lfs_seglock(fs, SEGM_PROT);
138
139 /* Get the head of the freelist. */
140 new_ino = fs->lfs_free;
141 #ifdef DIAGNOSTIC
142 if(new_ino == LFS_UNUSED_INUM) {
143 #ifdef DEBUG
144 lfs_dump_super(fs);
145 #endif /* DEBUG */
146 panic("inode 0 allocated [1]");
147 }
148 #endif /* DIAGNOSTIC */
149 #ifdef ALLOCPRINT
150 printf("lfs_ialloc: allocate inode %d\n", new_ino);
151 #endif
152
153 /*
154 * Remove the inode from the free list and write the new start
155 * of the free list into the superblock.
156 */
157 LFS_IENTRY(ifp, fs, new_ino, bp);
158 if (ifp->if_daddr != LFS_UNUSED_DADDR)
159 panic("lfs_ialloc: inuse inode %d on the free list", new_ino);
160 fs->lfs_free = ifp->if_nextfree;
161 new_gen = ifp->if_version; /* version was updated by vfree */
162 #ifdef LFS_DEBUG_NEXTFREE
163 ifp->if_nextfree = 0;
164 (void) VOP_BWRITE(bp); /* Ifile */
165 #else
166 brelse(bp);
167 #endif
168
169 /* Extend IFILE so that the next lfs_valloc will succeed. */
170 if (fs->lfs_free == LFS_UNUSED_INUM) {
171 vp = fs->lfs_ivnode;
172 (void)lfs_vref(vp);
173 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
174 ip = VTOI(vp);
175 blkno = lblkno(fs, ip->i_ffs_size);
176 if ((error = VOP_BALLOC(vp, ip->i_ffs_size, fs->lfs_bsize,
177 ap->a_cred, 0, &bp)) != 0) {
178 VOP_UNLOCK(vp, 0);
179 lfs_segunlock(fs);
180 fs->lfs_free = new_ino;
181 return (error);
182 }
183 ip->i_ffs_size += fs->lfs_bsize;
184 uvm_vnp_setsize(vp, ip->i_ffs_size);
185 (void)uvm_vnp_uncache(vp);
186 VOP_UNLOCK(vp, 0);
187
188 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
189 fs->lfs_ifpb;
190 fs->lfs_free = i;
191 #ifdef DIAGNOSTIC
192 if(fs->lfs_free == LFS_UNUSED_INUM)
193 panic("inode 0 allocated [2]");
194 #endif /* DIAGNOSTIC */
195 max = i + fs->lfs_ifpb;
196 for (ifp = (struct ifile *)bp->b_data; i < max; ++ifp) {
197 ifp->if_version = 1;
198 ifp->if_daddr = LFS_UNUSED_DADDR;
199 ifp->if_nextfree = ++i;
200 }
201 ifp--;
202 ifp->if_nextfree = LFS_UNUSED_INUM;
203 (void) VOP_BWRITE(bp); /* Ifile */
204 lfs_vunref(vp);
205 }
206 #ifdef DIAGNOSTIC
207 if(fs->lfs_free == LFS_UNUSED_INUM)
208 panic("inode 0 allocated [3]");
209 #endif /* DIAGNOSTIC */
210
211 lfs_segunlock(fs);
212
213 if ((error = getnewvnode(VT_LFS, ap->a_pvp->v_mount,
214 lfs_vnodeop_p, &vp)) != 0)
215 goto errout;
216
217 lockmgr(&ufs_hashlock, LK_EXCLUSIVE, 0);
218 /* Create an inode to associate with the vnode. */
219 lfs_vcreate(ap->a_pvp->v_mount, new_ino, vp);
220
221 ip = VTOI(vp);
222 /* Zero out the direct and indirect block addresses. */
223 bzero(&ip->i_din, sizeof(ip->i_din));
224 ip->i_din.ffs_din.di_inumber = new_ino;
225
226 /* Set a new generation number for this inode. */
227 ip->i_ffs_gen = new_gen;
228
229 /* Insert into the inode hash table. */
230 ufs_ihashins(ip);
231 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
232
233 error = ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
234 if (error) {
235 vput(vp);
236 goto errout;
237 }
238
239 *ap->a_vpp = vp;
240 #if 1
241 if(!(vp->v_flag & VDIROP)) {
242 (void)lfs_vref(vp);
243 ++lfs_dirvcount;
244 }
245 vp->v_flag |= VDIROP;
246
247 if(!(ip->i_flag & IN_ADIROP))
248 ++fs->lfs_nadirop;
249 ip->i_flag |= IN_ADIROP;
250 #endif
251 VREF(ip->i_devvp);
252 /* Set superblock modified bit and increment file count. */
253 fs->lfs_fmod = 1;
254 ++fs->lfs_nfiles;
255 return (0);
256
257 errout:
258 /*
259 * Put the new inum back on the free list.
260 */
261 LFS_IENTRY(ifp, fs, new_ino, bp);
262 ifp->if_daddr = LFS_UNUSED_DADDR;
263 ifp->if_nextfree = fs->lfs_free;
264 fs->lfs_free = new_ino;
265 (void) VOP_BWRITE(bp); /* Ifile */
266
267 return (error);
268 }
269
270 /* Create a new vnode/inode pair and initialize what fields we can. */
271 void
272 lfs_vcreate(mp, ino, vp)
273 struct mount *mp;
274 ino_t ino;
275 struct vnode *vp;
276 {
277 struct inode *ip;
278 struct ufsmount *ump;
279 #ifdef QUOTA
280 int i;
281 #endif
282
283 /* Get a pointer to the private mount structure. */
284 ump = VFSTOUFS(mp);
285
286 /* Initialize the inode. */
287 ip = pool_get(&lfs_inode_pool, PR_WAITOK);
288 vp->v_data = ip;
289 ip->i_vnode = vp;
290 ip->i_devvp = ump->um_devvp;
291 ip->i_dev = ump->um_dev;
292 ip->i_number = ip->i_din.ffs_din.di_inumber = ino;
293 ip->i_lfs = ump->um_lfs;
294 #ifdef QUOTA
295 for (i = 0; i < MAXQUOTAS; i++)
296 ip->i_dquot[i] = NODQUOT;
297 #endif
298 ip->i_lockf = 0;
299 ip->i_diroff = 0;
300 ip->i_ffs_mode = 0;
301 ip->i_ffs_size = 0;
302 ip->i_ffs_blocks = 0;
303 ip->i_lfs_effnblks = 0;
304 ip->i_flag = 0;
305 LFS_SET_UINO(ip, IN_MODIFIED);
306 }
307
308 /* Free an inode. */
309 /* ARGUSED */
310 /* VOP_BWRITE 2i times */
311 int
312 lfs_vfree(v)
313 void *v;
314 {
315 struct vop_vfree_args /* {
316 struct vnode *a_pvp;
317 ino_t a_ino;
318 int a_mode;
319 } */ *ap = v;
320 SEGUSE *sup;
321 struct buf *bp;
322 struct ifile *ifp;
323 struct inode *ip;
324 struct vnode *vp;
325 struct lfs *fs;
326 ufs_daddr_t old_iaddr;
327 ino_t ino;
328 extern int lfs_dirvcount;
329
330 /* Get the inode number and file system. */
331 vp = ap->a_pvp;
332 ip = VTOI(vp);
333 fs = ip->i_lfs;
334 ino = ip->i_number;
335
336 #if 0
337 /*
338 * Right now this is unnecessary since we take the seglock.
339 * But if the seglock is no longer necessary (e.g. we put the
340 * head of the free list into the Ifile) we will need to drain
341 * this vnode of any pending writes.
342 */
343 if (WRITEINPROG(vp))
344 tsleep(vp, (PRIBIO+1), "lfs_vfree", 0);
345 #endif
346 lfs_seglock(fs, SEGM_PROT);
347
348 if(vp->v_flag & VDIROP) {
349 --lfs_dirvcount;
350 vp->v_flag &= ~VDIROP;
351 wakeup(&lfs_dirvcount);
352 lfs_vunref(vp);
353 }
354 if (ip->i_flag & IN_ADIROP) {
355 --fs->lfs_nadirop;
356 ip->i_flag &= ~IN_ADIROP;
357 }
358
359 LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
360 ip->i_flag &= ~IN_ALLMOD;
361
362 /*
363 * Set the ifile's inode entry to unused, increment its version number
364 * and link it into the free chain.
365 */
366 LFS_IENTRY(ifp, fs, ino, bp);
367 old_iaddr = ifp->if_daddr;
368 ifp->if_daddr = LFS_UNUSED_DADDR;
369 ++ifp->if_version;
370 ifp->if_nextfree = fs->lfs_free;
371 fs->lfs_free = ino;
372 (void) VOP_BWRITE(bp); /* Ifile */
373 #ifdef DIAGNOSTIC
374 if(fs->lfs_free == LFS_UNUSED_INUM) {
375 panic("inode 0 freed");
376 }
377 #endif /* DIAGNOSTIC */
378 if (old_iaddr != LFS_UNUSED_DADDR) {
379 LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
380 #ifdef DIAGNOSTIC
381 if (sup->su_nbytes < DINODE_SIZE) {
382 printf("lfs_vfree: negative byte count"
383 " (segment %d short by %d)\n",
384 datosn(fs, old_iaddr),
385 (int)DINODE_SIZE - sup->su_nbytes);
386 panic("lfs_vfree: negative byte count");
387 sup->su_nbytes = DINODE_SIZE;
388 }
389 #endif
390 sup->su_nbytes -= DINODE_SIZE;
391 (void) VOP_BWRITE(bp); /* Ifile */
392 }
393
394 /* Set superblock modified bit and decrement file count. */
395 fs->lfs_fmod = 1;
396 --fs->lfs_nfiles;
397
398 lfs_segunlock(fs);
399 return (0);
400 }
401