lfs_alloc.c revision 1.34.2.4 1 /* $NetBSD: lfs_alloc.c,v 1.34.2.4 2000/09/14 18:50:17 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 <vm/vm.h>
88
89 #include <ufs/ufs/quota.h>
90 #include <ufs/ufs/inode.h>
91 #include <ufs/ufs/ufsmount.h>
92 #include <ufs/ufs/ufs_extern.h>
93
94 #include <ufs/lfs/lfs.h>
95 #include <ufs/lfs/lfs_extern.h>
96
97 extern int lfs_dirvcount;
98 extern struct lock ufs_hashlock;
99
100 /* Allocate a new inode. */
101 /* ARGSUSED */
102 /* VOP_BWRITE 2i times */
103 int
104 lfs_valloc(v)
105 void *v;
106 {
107 struct vop_valloc_args /* {
108 struct vnode *a_pvp;
109 int a_mode;
110 struct ucred *a_cred;
111 struct vnode **a_vpp;
112 } */ *ap = v;
113 struct lfs *fs;
114 struct buf *bp;
115 struct ifile *ifp;
116 struct inode *ip;
117 struct vnode *vp;
118 ufs_daddr_t blkno;
119 ino_t new_ino;
120 u_long i, max;
121 int error;
122 int new_gen;
123 extern int lfs_dirvcount;
124
125 fs = VTOI(ap->a_pvp)->i_lfs;
126 if (fs->lfs_ronly)
127 return EROFS;
128 *ap->a_vpp = NULL;
129
130 /*
131 * Use lfs_seglock here, instead of fs->lfs_freelock, to ensure that
132 * the free list is not changed in between the time that the ifile
133 * blocks are written to disk and the time that the superblock is
134 * written to disk.
135 *
136 * XXX this sucks. We should instead encode the head of the free
137 * list into the CLEANERINFO block of the Ifile.
138 */
139 lfs_seglock(fs, SEGM_PROT);
140
141 /* Get the head of the freelist. */
142 new_ino = fs->lfs_free;
143 #ifdef DIAGNOSTIC
144 if(new_ino == LFS_UNUSED_INUM) {
145 #ifdef DEBUG
146 lfs_dump_super(fs);
147 #endif /* DEBUG */
148 panic("inode 0 allocated [1]");
149 }
150 #endif /* DIAGNOSTIC */
151 #ifdef ALLOCPRINT
152 printf("lfs_ialloc: allocate inode %d\n", new_ino);
153 #endif
154
155 /*
156 * Remove the inode from the free list and write the new start
157 * of the free list into the superblock.
158 */
159 LFS_IENTRY(ifp, fs, new_ino, bp);
160 if (ifp->if_daddr != LFS_UNUSED_DADDR)
161 panic("lfs_ialloc: inuse inode %d on the free list", new_ino);
162 fs->lfs_free = ifp->if_nextfree;
163 new_gen = ifp->if_version; /* version was updated by vfree */
164 #ifdef LFS_DEBUG_NEXTFREE
165 ifp->if_nextfree = 0;
166 (void) VOP_BWRITE(bp); /* Ifile */
167 #else
168 brelse(bp);
169 #endif
170
171 /* Extend IFILE so that the next lfs_valloc will succeed. */
172 if (fs->lfs_free == LFS_UNUSED_INUM) {
173 vp = fs->lfs_ivnode;
174 (void)lfs_vref(vp);
175 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
176 ip = VTOI(vp);
177 blkno = lblkno(fs, ip->i_ffs_size);
178 if ((error = VOP_BALLOC(vp, ip->i_ffs_size, fs->lfs_bsize,
179 ap->a_cred, 0, &bp)) != 0) {
180 VOP_UNLOCK(vp, 0);
181 lfs_segunlock(fs);
182 fs->lfs_free = new_ino;
183 return (error);
184 }
185 ip->i_ffs_size += fs->lfs_bsize;
186 uvm_vnp_setsize(vp, ip->i_ffs_size);
187 (void)uvm_vnp_uncache(vp);
188 VOP_UNLOCK(vp, 0);
189
190 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
191 fs->lfs_ifpb;
192 fs->lfs_free = i;
193 #ifdef DIAGNOSTIC
194 if(fs->lfs_free == LFS_UNUSED_INUM)
195 panic("inode 0 allocated [2]");
196 #endif /* DIAGNOSTIC */
197 max = i + fs->lfs_ifpb;
198 for (ifp = (struct ifile *)bp->b_data; i < max; ++ifp) {
199 ifp->if_version = 1;
200 ifp->if_daddr = LFS_UNUSED_DADDR;
201 ifp->if_nextfree = ++i;
202 }
203 ifp--;
204 ifp->if_nextfree = LFS_UNUSED_INUM;
205 (void) VOP_BWRITE(bp); /* Ifile */
206 lfs_vunref(vp);
207 }
208 #ifdef DIAGNOSTIC
209 if(fs->lfs_free == LFS_UNUSED_INUM)
210 panic("inode 0 allocated [3]");
211 #endif /* DIAGNOSTIC */
212
213 lfs_segunlock(fs);
214
215 if ((error = getnewvnode(VT_LFS, ap->a_pvp->v_mount,
216 lfs_vnodeop_p, &vp)) != 0)
217 goto errout;
218
219 lockmgr(&ufs_hashlock, LK_EXCLUSIVE, 0);
220 /* Create an inode to associate with the vnode. */
221 lfs_vcreate(ap->a_pvp->v_mount, new_ino, vp);
222
223 ip = VTOI(vp);
224 /* Zero out the direct and indirect block addresses. */
225 bzero(&ip->i_din, sizeof(ip->i_din));
226 ip->i_din.ffs_din.di_inumber = new_ino;
227
228 /* Set a new generation number for this inode. */
229 ip->i_ffs_gen = new_gen;
230
231 /* Insert into the inode hash table. */
232 ufs_ihashins(ip);
233 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
234
235 error = ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
236 if (error) {
237 vput(vp);
238 goto errout;
239 }
240
241 *ap->a_vpp = vp;
242 #if 1
243 if(!(vp->v_flag & VDIROP)) {
244 (void)lfs_vref(vp);
245 ++lfs_dirvcount;
246 }
247 vp->v_flag |= VDIROP;
248
249 if(!(ip->i_flag & IN_ADIROP))
250 ++fs->lfs_nadirop;
251 ip->i_flag |= IN_ADIROP;
252 #endif
253 VREF(ip->i_devvp);
254 /* Set superblock modified bit and increment file count. */
255 fs->lfs_fmod = 1;
256 ++fs->lfs_nfiles;
257 return (0);
258
259 errout:
260 /*
261 * Put the new inum back on the free list.
262 */
263 LFS_IENTRY(ifp, fs, new_ino, bp);
264 ifp->if_daddr = LFS_UNUSED_DADDR;
265 ifp->if_nextfree = fs->lfs_free;
266 fs->lfs_free = new_ino;
267 (void) VOP_BWRITE(bp); /* Ifile */
268
269 return (error);
270 }
271
272 /* Create a new vnode/inode pair and initialize what fields we can. */
273 void
274 lfs_vcreate(mp, ino, vp)
275 struct mount *mp;
276 ino_t ino;
277 struct vnode *vp;
278 {
279 struct inode *ip;
280 struct ufsmount *ump;
281 #ifdef QUOTA
282 int i;
283 #endif
284
285 /* Get a pointer to the private mount structure. */
286 ump = VFSTOUFS(mp);
287
288 /* Initialize the inode. */
289 ip = pool_get(&lfs_inode_pool, PR_WAITOK);
290 vp->v_data = ip;
291 ip->i_vnode = vp;
292 ip->i_devvp = ump->um_devvp;
293 ip->i_dev = ump->um_dev;
294 ip->i_number = ip->i_din.ffs_din.di_inumber = ino;
295 ip->i_lfs = ump->um_lfs;
296 #ifdef QUOTA
297 for (i = 0; i < MAXQUOTAS; i++)
298 ip->i_dquot[i] = NODQUOT;
299 #endif
300 ip->i_lockf = 0;
301 ip->i_diroff = 0;
302 ip->i_ffs_mode = 0;
303 ip->i_ffs_size = 0;
304 ip->i_ffs_blocks = 0;
305 ip->i_lfs_effnblks = 0;
306 ip->i_flag = 0;
307 LFS_SET_UINO(ip, IN_MODIFIED);
308 }
309
310 /* Free an inode. */
311 /* ARGUSED */
312 /* VOP_BWRITE 2i times */
313 int
314 lfs_vfree(v)
315 void *v;
316 {
317 struct vop_vfree_args /* {
318 struct vnode *a_pvp;
319 ino_t a_ino;
320 int a_mode;
321 } */ *ap = v;
322 SEGUSE *sup;
323 struct buf *bp;
324 struct ifile *ifp;
325 struct inode *ip;
326 struct vnode *vp;
327 struct lfs *fs;
328 ufs_daddr_t old_iaddr;
329 ino_t ino;
330 extern int lfs_dirvcount;
331
332 /* Get the inode number and file system. */
333 vp = ap->a_pvp;
334 ip = VTOI(vp);
335 fs = ip->i_lfs;
336 ino = ip->i_number;
337
338 #if 0
339 /*
340 * Right now this is unnecessary since we take the seglock.
341 * But if the seglock is no longer necessary (e.g. we put the
342 * head of the free list into the Ifile) we will need to drain
343 * this vnode of any pending writes.
344 */
345 if (WRITEINPROG(vp))
346 tsleep(vp, (PRIBIO+1), "lfs_vfree", 0);
347 #endif
348 lfs_seglock(fs, SEGM_PROT);
349
350 if(vp->v_flag & VDIROP) {
351 --lfs_dirvcount;
352 vp->v_flag &= ~VDIROP;
353 wakeup(&lfs_dirvcount);
354 lfs_vunref(vp);
355 }
356 if (ip->i_flag & IN_ADIROP) {
357 --fs->lfs_nadirop;
358 ip->i_flag &= ~IN_ADIROP;
359 }
360
361 LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
362 ip->i_flag &= ~IN_ALLMOD;
363
364 /*
365 * Set the ifile's inode entry to unused, increment its version number
366 * and link it into the free chain.
367 */
368 LFS_IENTRY(ifp, fs, ino, bp);
369 old_iaddr = ifp->if_daddr;
370 ifp->if_daddr = LFS_UNUSED_DADDR;
371 ++ifp->if_version;
372 ifp->if_nextfree = fs->lfs_free;
373 fs->lfs_free = ino;
374 (void) VOP_BWRITE(bp); /* Ifile */
375 #ifdef DIAGNOSTIC
376 if(fs->lfs_free == LFS_UNUSED_INUM) {
377 panic("inode 0 freed");
378 }
379 #endif /* DIAGNOSTIC */
380 if (old_iaddr != LFS_UNUSED_DADDR) {
381 LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
382 #ifdef DIAGNOSTIC
383 if (sup->su_nbytes < DINODE_SIZE) {
384 printf("lfs_vfree: negative byte count"
385 " (segment %d short by %d)\n",
386 datosn(fs, old_iaddr),
387 (int)DINODE_SIZE - sup->su_nbytes);
388 panic("lfs_vfree: negative byte count");
389 sup->su_nbytes = DINODE_SIZE;
390 }
391 #endif
392 sup->su_nbytes -= DINODE_SIZE;
393 (void) VOP_BWRITE(bp); /* Ifile */
394 }
395
396 /* Set superblock modified bit and decrement file count. */
397 fs->lfs_fmod = 1;
398 --fs->lfs_nfiles;
399
400 lfs_segunlock(fs);
401 return (0);
402 }
403