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