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