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