lfs_alloc.c revision 1.44 1 /* $NetBSD: lfs_alloc.c,v 1.44 2000/11/27 03:33:57 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 static int extend_ifile(struct lfs *, struct ucred *);
99 static int lfs_ialloc(struct lfs *, struct vnode *, ino_t, int, struct vnode **);
100
101 /*
102 * Allocate a particular inode with a particular version number, freeing
103 * any previous versions of this inode that may have gone before.
104 * Used by the roll-forward code.
105 *
106 * XXX this function does not have appropriate locking to be used on a live fs;
107 * XXX but something similar could probably be used for an "undelete" call.
108 */
109 int
110 lfs_rf_valloc(struct lfs *fs, ino_t ino, int version, struct proc *p,
111 struct vnode **vpp)
112 {
113 IFILE *ifp;
114 struct buf *bp;
115 struct vnode *vp;
116 struct inode *ip;
117 ino_t tino, oldnext;
118 int error;
119
120 /*
121 * First, just try a vget. If the version number is the one we want,
122 * we don't have to do anything else. If the version number is wrong,
123 * take appropriate action.
124 */
125 error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
126 if (error == 0) {
127 /* printf("lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp); */
128
129 *vpp = vp;
130 ip = VTOI(vp);
131 if (ip->i_ffs_gen == version)
132 return 0;
133 else if (ip->i_ffs_gen < version) {
134 VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, p);
135 ip->i_ffs_gen = version;
136 LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED | IN_UPDATE);
137 return 0;
138 } else {
139 /* printf("ino %d: asked for version %d but got %d\n",
140 ino, version, ip->i_ffs_gen); */
141 vput(vp);
142 *vpp = NULLVP;
143 return EEXIST;
144 }
145 }
146
147 /*
148 * The inode is not in use. Find it on the free list.
149 */
150 /* If the Ifile is too short to contain this inum, extend it */
151 while (VTOI(fs->lfs_ivnode)->i_ffs_size <=
152 dbtob(fsbtodb(fs, ino / fs->lfs_ifpb + fs->lfs_cleansz +
153 fs->lfs_segtabsz))) {
154 extend_ifile(fs, NOCRED);
155 }
156
157 LFS_IENTRY(ifp, fs, ino, bp);
158 oldnext = ifp->if_nextfree;
159 ifp->if_version = version;
160 brelse(bp);
161
162 if (ino == fs->lfs_free) {
163 fs->lfs_free = oldnext;
164 } else {
165 tino = fs->lfs_free;
166 while(1) {
167 LFS_IENTRY(ifp, fs, tino, bp);
168 if (ifp->if_nextfree == ino ||
169 ifp->if_nextfree == LFS_UNUSED_INUM)
170 break;
171 tino = ifp->if_nextfree;
172 brelse(bp);
173 }
174 if (ifp->if_nextfree == LFS_UNUSED_INUM) {
175 brelse(bp);
176 return ENOENT;
177 }
178 ifp->if_nextfree = oldnext;
179 VOP_BWRITE(bp);
180 }
181
182 error = lfs_ialloc(fs, fs->lfs_ivnode, ino, version, &vp);
183 if (error == 0) {
184 /*
185 * Make it VREG so we can put blocks on it. We will change
186 * this later if it turns out to be some other kind of file.
187 */
188 ip = VTOI(vp);
189 ip->i_ffs_mode = IFREG;
190 ip->i_ffs_nlink = 1;
191 ip->i_ffs_effnlink = 1;
192 ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
193 ip = VTOI(vp);
194
195 /* printf("lfs_rf_valloc: ino %d vp %p\n", ino, vp); */
196
197 /* The dirop-nature of this vnode is past */
198 (void)lfs_vunref(vp);
199 --lfs_dirvcount;
200 vp->v_flag &= ~VDIROP;
201 --fs->lfs_nadirop;
202 ip->i_flag &= ~IN_ADIROP;
203 }
204 *vpp = vp;
205 return error;
206 }
207
208 static int
209 extend_ifile(struct lfs *fs, struct ucred *cred)
210 {
211 struct vnode *vp;
212 struct inode *ip;
213 IFILE *ifp;
214 struct buf *bp;
215 int error;
216 ufs_daddr_t i, blkno, max;
217 ino_t oldlast;
218
219 vp = fs->lfs_ivnode;
220 (void)lfs_vref(vp);
221 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
222 ip = VTOI(vp);
223 blkno = lblkno(fs, ip->i_ffs_size);
224 if ((error = VOP_BALLOC(vp, ip->i_ffs_size, fs->lfs_bsize, cred, 0,
225 &bp)) != 0) {
226 VOP_UNLOCK(vp, 0);
227 lfs_vunref(vp);
228 return (error);
229 }
230 ip->i_ffs_size += fs->lfs_bsize;
231 uvm_vnp_setsize(vp, ip->i_ffs_size);
232 (void)uvm_vnp_uncache(vp);
233 VOP_UNLOCK(vp, 0);
234
235 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
236 fs->lfs_ifpb;
237 oldlast = fs->lfs_free;
238 fs->lfs_free = i;
239 #ifdef DIAGNOSTIC
240 if(fs->lfs_free == LFS_UNUSED_INUM)
241 panic("inode 0 allocated [2]");
242 #endif /* DIAGNOSTIC */
243 max = i + fs->lfs_ifpb;
244 /* printf("extend ifile for ino %d--%d\n", i, max); */
245 for (ifp = (struct ifile *)bp->b_data; i < max; ++ifp) {
246 ifp->if_version = 1;
247 ifp->if_daddr = LFS_UNUSED_DADDR;
248 ifp->if_nextfree = ++i;
249 }
250 ifp--;
251 ifp->if_nextfree = oldlast;
252 (void) VOP_BWRITE(bp); /* Ifile */
253 lfs_vunref(vp);
254
255 return 0;
256 }
257
258 /* Allocate a new inode. */
259 /* ARGSUSED */
260 /* VOP_BWRITE 2i times */
261 int
262 lfs_valloc(v)
263 void *v;
264 {
265 struct vop_valloc_args /* {
266 struct vnode *a_pvp;
267 int a_mode;
268 struct ucred *a_cred;
269 struct vnode **a_vpp;
270 } */ *ap = v;
271 struct lfs *fs;
272 struct buf *bp;
273 struct ifile *ifp;
274 ino_t new_ino;
275 int error;
276 int new_gen;
277
278 fs = VTOI(ap->a_pvp)->i_lfs;
279 if (fs->lfs_ronly)
280 return EROFS;
281 *ap->a_vpp = NULL;
282
283 /*
284 * Use lfs_seglock here, instead of fs->lfs_freelock, to ensure that
285 * the free list is not changed in between the time that the ifile
286 * blocks are written to disk and the time that the superblock is
287 * written to disk.
288 *
289 * XXX this sucks. We should instead encode the head of the free
290 * list into the CLEANERINFO block of the Ifile. [XXX v2]
291 */
292 lfs_seglock(fs, SEGM_PROT);
293
294 /* Get the head of the freelist. */
295 new_ino = fs->lfs_free;
296 #ifdef DIAGNOSTIC
297 if(new_ino == LFS_UNUSED_INUM) {
298 #ifdef DEBUG
299 lfs_dump_super(fs);
300 #endif /* DEBUG */
301 panic("inode 0 allocated [1]");
302 }
303 #endif /* DIAGNOSTIC */
304 #ifdef ALLOCPRINT
305 printf("lfs_valloc: allocate inode %d\n", new_ino);
306 #endif
307
308 /*
309 * Remove the inode from the free list and write the new start
310 * of the free list into the superblock.
311 */
312 LFS_IENTRY(ifp, fs, new_ino, bp);
313 if (ifp->if_daddr != LFS_UNUSED_DADDR)
314 panic("lfs_valloc: inuse inode %d on the free list", new_ino);
315 fs->lfs_free = ifp->if_nextfree;
316 new_gen = ifp->if_version; /* version was updated by vfree */
317 brelse(bp);
318
319 /* Extend IFILE so that the next lfs_valloc will succeed. */
320 if (fs->lfs_free == LFS_UNUSED_INUM) {
321 if ((error = extend_ifile(fs, ap->a_cred)) != 0) {
322 fs->lfs_free = new_ino;
323 lfs_segunlock(fs);
324 return error;
325 }
326 }
327 #ifdef DIAGNOSTIC
328 if(fs->lfs_free == LFS_UNUSED_INUM)
329 panic("inode 0 allocated [3]");
330 #endif /* DIAGNOSTIC */
331
332 lfs_segunlock(fs);
333
334 return lfs_ialloc(fs, ap->a_pvp, new_ino, new_gen, ap->a_vpp);
335 }
336
337 static int
338 lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
339 struct vnode **vpp)
340 {
341 struct inode *ip;
342 struct vnode *vp;
343 IFILE *ifp;
344 struct buf *bp;
345 int error;
346
347 error = getnewvnode(VT_LFS, pvp->v_mount, lfs_vnodeop_p, &vp);
348 /* printf("lfs_ialloc: ino %d vp %p error %d\n", new_ino, vp, error);*/
349 if (error)
350 goto errout;
351
352 lockmgr(&ufs_hashlock, LK_EXCLUSIVE, 0);
353 /* Create an inode to associate with the vnode. */
354 lfs_vcreate(pvp->v_mount, new_ino, vp);
355
356 ip = VTOI(vp);
357 /* Zero out the direct and indirect block addresses. */
358 bzero(&ip->i_din, sizeof(ip->i_din));
359 ip->i_din.ffs_din.di_inumber = new_ino;
360
361 /* Set a new generation number for this inode. */
362 if (new_gen)
363 ip->i_ffs_gen = new_gen;
364
365 /* Insert into the inode hash table. */
366 ufs_ihashins(ip);
367 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
368
369 error = ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
370 ip = VTOI(vp);
371 if (error) {
372 vput(vp);
373 goto errout;
374 }
375 /* printf("lfs_ialloc[2]: ino %d vp %p\n", new_ino, vp);*/
376
377 *vpp = vp;
378 #if 1
379 if(!(vp->v_flag & VDIROP)) {
380 (void)lfs_vref(vp);
381 ++lfs_dirvcount;
382 }
383 vp->v_flag |= VDIROP;
384
385 if(!(ip->i_flag & IN_ADIROP))
386 ++fs->lfs_nadirop;
387 ip->i_flag |= IN_ADIROP;
388 #endif
389 VREF(ip->i_devvp);
390 /* Set superblock modified bit and increment file count. */
391 fs->lfs_fmod = 1;
392 ++fs->lfs_nfiles;
393 return (0);
394
395 errout:
396 /*
397 * Put the new inum back on the free list.
398 */
399 LFS_IENTRY(ifp, fs, new_ino, bp);
400 ifp->if_daddr = LFS_UNUSED_DADDR;
401 ifp->if_nextfree = fs->lfs_free;
402 fs->lfs_free = new_ino;
403 (void) VOP_BWRITE(bp); /* Ifile */
404
405 *vpp = NULLVP;
406 return (error);
407 }
408
409 /* Create a new vnode/inode pair and initialize what fields we can. */
410 void
411 lfs_vcreate(mp, ino, vp)
412 struct mount *mp;
413 ino_t ino;
414 struct vnode *vp;
415 {
416 struct inode *ip;
417 struct ufsmount *ump;
418 #ifdef QUOTA
419 int i;
420 #endif
421
422 /* Get a pointer to the private mount structure. */
423 ump = VFSTOUFS(mp);
424
425 /* Initialize the inode. */
426 ip = pool_get(&lfs_inode_pool, PR_WAITOK);
427 vp->v_data = ip;
428 ip->i_vnode = vp;
429 ip->i_devvp = ump->um_devvp;
430 ip->i_dev = ump->um_dev;
431 ip->i_number = ip->i_din.ffs_din.di_inumber = ino;
432 ip->i_lfs = ump->um_lfs;
433 #ifdef QUOTA
434 for (i = 0; i < MAXQUOTAS; i++)
435 ip->i_dquot[i] = NODQUOT;
436 #endif
437 ip->i_lockf = 0;
438 ip->i_diroff = 0;
439 ip->i_ffs_mode = 0;
440 ip->i_ffs_size = 0;
441 ip->i_ffs_blocks = 0;
442 ip->i_lfs_effnblks = 0;
443 ip->i_flag = 0;
444 LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED);
445 }
446
447 /* Free an inode. */
448 /* ARGUSED */
449 /* VOP_BWRITE 2i times */
450 int
451 lfs_vfree(v)
452 void *v;
453 {
454 struct vop_vfree_args /* {
455 struct vnode *a_pvp;
456 ino_t a_ino;
457 int a_mode;
458 } */ *ap = v;
459 SEGUSE *sup;
460 struct buf *bp;
461 struct ifile *ifp;
462 struct inode *ip;
463 struct vnode *vp;
464 struct lfs *fs;
465 ufs_daddr_t old_iaddr;
466 ino_t ino;
467 extern int lfs_dirvcount;
468
469 /* Get the inode number and file system. */
470 vp = ap->a_pvp;
471 ip = VTOI(vp);
472 fs = ip->i_lfs;
473 ino = ip->i_number;
474
475 #if 0
476 /*
477 * Right now this is unnecessary since we take the seglock.
478 * But if the seglock is no longer necessary (e.g. we put the
479 * head of the free list into the Ifile) we will need to drain
480 * this vnode of any pending writes.
481 */
482 if (WRITEINPROG(vp))
483 tsleep(vp, (PRIBIO+1), "lfs_vfree", 0);
484 #endif
485 lfs_seglock(fs, SEGM_PROT);
486
487 if(vp->v_flag & VDIROP) {
488 --lfs_dirvcount;
489 vp->v_flag &= ~VDIROP;
490 wakeup(&lfs_dirvcount);
491 lfs_vunref(vp);
492 }
493 if (ip->i_flag & IN_ADIROP) {
494 --fs->lfs_nadirop;
495 ip->i_flag &= ~IN_ADIROP;
496 }
497
498 LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
499 ip->i_flag &= ~IN_ALLMOD;
500
501 /*
502 * Set the ifile's inode entry to unused, increment its version number
503 * and link it into the free chain.
504 */
505 LFS_IENTRY(ifp, fs, ino, bp);
506 old_iaddr = ifp->if_daddr;
507 ifp->if_daddr = LFS_UNUSED_DADDR;
508 ++ifp->if_version;
509 ifp->if_nextfree = fs->lfs_free;
510 fs->lfs_free = ino;
511 (void) VOP_BWRITE(bp); /* Ifile */
512 #ifdef DIAGNOSTIC
513 if(fs->lfs_free == LFS_UNUSED_INUM) {
514 panic("inode 0 freed");
515 }
516 #endif /* DIAGNOSTIC */
517 if (old_iaddr != LFS_UNUSED_DADDR) {
518 LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
519 #ifdef DIAGNOSTIC
520 if (sup->su_nbytes < DINODE_SIZE) {
521 printf("lfs_vfree: negative byte count"
522 " (segment %d short by %d)\n",
523 datosn(fs, old_iaddr),
524 (int)DINODE_SIZE - sup->su_nbytes);
525 panic("lfs_vfree: negative byte count");
526 sup->su_nbytes = DINODE_SIZE;
527 }
528 #endif
529 sup->su_nbytes -= DINODE_SIZE;
530 (void) VOP_BWRITE(bp); /* Ifile */
531 }
532
533 /* Set superblock modified bit and decrement file count. */
534 fs->lfs_fmod = 1;
535 --fs->lfs_nfiles;
536
537 lfs_segunlock(fs);
538 return (0);
539 }
540