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