lfs_alloc.c revision 1.83 1 /* $NetBSD: lfs_alloc.c,v 1.83 2005/05/29 21:25:24 christos 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. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)lfs_alloc.c 8.4 (Berkeley) 1/4/94
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.83 2005/05/29 21:25:24 christos Exp $");
71
72 #if defined(_KERNEL_OPT)
73 #include "opt_quota.h"
74 #endif
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/buf.h>
80 #include <sys/lock.h>
81 #include <sys/vnode.h>
82 #include <sys/syslog.h>
83 #include <sys/mount.h>
84 #include <sys/pool.h>
85 #include <sys/proc.h>
86 #include <sys/tree.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 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,
100 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 * Called with the Ifile inode locked.
111 */
112 int
113 lfs_rf_valloc(struct lfs *fs, ino_t ino, int vers, struct proc *p,
114 struct vnode **vpp)
115 {
116 IFILE *ifp;
117 struct buf *bp, *cbp;
118 struct vnode *vp;
119 struct inode *ip;
120 ino_t tino, oldnext;
121 int error;
122 CLEANERINFO *cip;
123
124 ASSERT_SEGLOCK(fs); /* XXX it doesn't, really */
125
126 /*
127 * First, just try a vget. If the version number is the one we want,
128 * we don't have to do anything else. If the version number is wrong,
129 * take appropriate action.
130 */
131 error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
132 if (error == 0) {
133 DLOG((DLOG_RF, "lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp));
134
135 *vpp = vp;
136 ip = VTOI(vp);
137 if (ip->i_gen == vers)
138 return 0;
139 else if (ip->i_gen < vers) {
140 VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, p);
141 ip->i_gen = ip->i_ffs1_gen = vers;
142 LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
143 return 0;
144 } else {
145 DLOG((DLOG_RF, "ino %d: sought version %d, got %d\n",
146 ino, vers, ip->i_ffs1_gen));
147 vput(vp);
148 *vpp = NULLVP;
149 return EEXIST;
150 }
151 }
152
153 /*
154 * The inode is not in use. Find it on the free list.
155 */
156 /* If the Ifile is too short to contain this inum, extend it */
157 while (VTOI(fs->lfs_ivnode)->i_size <= (ino /
158 fs->lfs_ifpb + fs->lfs_cleansz + fs->lfs_segtabsz)
159 << fs->lfs_bshift) {
160 extend_ifile(fs, NOCRED);
161 }
162
163 LFS_IENTRY(ifp, fs, ino, bp);
164 oldnext = ifp->if_nextfree;
165 ifp->if_version = vers;
166 brelse(bp);
167
168 LFS_GET_HEADFREE(fs, cip, cbp, &ino);
169 if (ino) {
170 LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
171 } else {
172 tino = ino;
173 while (1) {
174 LFS_IENTRY(ifp, fs, tino, bp);
175 if (ifp->if_nextfree == ino ||
176 ifp->if_nextfree == LFS_UNUSED_INUM)
177 break;
178 tino = ifp->if_nextfree;
179 brelse(bp);
180 }
181 if (ifp->if_nextfree == LFS_UNUSED_INUM) {
182 brelse(bp);
183 return ENOENT;
184 }
185 ifp->if_nextfree = oldnext;
186 LFS_BWRITE_LOG(bp);
187 }
188
189 error = lfs_ialloc(fs, fs->lfs_ivnode, ino, vers, &vp);
190 if (error == 0) {
191 /*
192 * Make it VREG so we can put blocks on it. We will change
193 * this later if it turns out to be some other kind of file.
194 */
195 ip = VTOI(vp);
196 ip->i_mode = ip->i_ffs1_mode = IFREG;
197 ip->i_nlink = ip->i_ffs1_nlink = 1;
198 ip->i_ffs_effnlink = 1;
199 ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
200 ip = VTOI(vp);
201
202 DLOG((DLOG_RF, "lfs_rf_valloc: ino %d vp %p\n", ino, vp));
203
204 /* The dirop-nature of this vnode is past */
205 lfs_unmark_vnode(vp);
206 (void)lfs_vunref(vp);
207 vp->v_flag &= ~VDIROP;
208 simple_lock(&fs->lfs_interlock);
209 simple_lock(&lfs_subsys_lock);
210 --lfs_dirvcount;
211 simple_unlock(&lfs_subsys_lock);
212 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
213 simple_unlock(&fs->lfs_interlock);
214 }
215 *vpp = vp;
216 return error;
217 }
218
219 /*
220 * Add a new block to the Ifile, to accommodate future file creations.
221 * Called with the segment lock held.
222 */
223 static int
224 extend_ifile(struct lfs *fs, struct ucred *cred)
225 {
226 struct vnode *vp;
227 struct inode *ip;
228 IFILE *ifp;
229 IFILE_V1 *ifp_v1;
230 struct buf *bp, *cbp;
231 int error;
232 daddr_t i, blkno, xmax;
233 ino_t oldlast;
234 CLEANERINFO *cip;
235
236 ASSERT_SEGLOCK(fs);
237
238 vp = fs->lfs_ivnode;
239 ip = VTOI(vp);
240 blkno = lblkno(fs, ip->i_size);
241 if ((error = VOP_BALLOC(vp, ip->i_size, fs->lfs_bsize, cred, 0,
242 &bp)) != 0) {
243 return (error);
244 }
245 ip->i_size += fs->lfs_bsize;
246 ip->i_ffs1_size = ip->i_size;
247 uvm_vnp_setsize(vp, ip->i_size);
248
249 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
250 fs->lfs_ifpb;
251 LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
252 LFS_PUT_HEADFREE(fs, cip, cbp, i);
253 #ifdef DIAGNOSTIC
254 if (fs->lfs_freehd == LFS_UNUSED_INUM)
255 panic("inode 0 allocated [2]");
256 #endif /* DIAGNOSTIC */
257 xmax = i + fs->lfs_ifpb;
258
259 if (fs->lfs_version == 1) {
260 for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) {
261 ifp_v1->if_version = 1;
262 ifp_v1->if_daddr = LFS_UNUSED_DADDR;
263 ifp_v1->if_nextfree = ++i;
264 }
265 ifp_v1--;
266 ifp_v1->if_nextfree = oldlast;
267 } else {
268 for (ifp = (IFILE *)bp->b_data; i < xmax; ++ifp) {
269 ifp->if_version = 1;
270 ifp->if_daddr = LFS_UNUSED_DADDR;
271 ifp->if_nextfree = ++i;
272 }
273 ifp--;
274 ifp->if_nextfree = oldlast;
275 }
276 LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1);
277
278 (void) LFS_BWRITE_LOG(bp); /* Ifile */
279
280 return 0;
281 }
282
283 /* Allocate a new inode. */
284 /* ARGSUSED */
285 /* VOP_BWRITE 2i times */
286 int
287 lfs_valloc(void *v)
288 {
289 struct vop_valloc_args /* {
290 struct vnode *a_pvp;
291 int a_mode;
292 struct ucred *a_cred;
293 struct vnode **a_vpp;
294 } */ *ap = v;
295 struct lfs *fs;
296 struct buf *bp, *cbp;
297 struct ifile *ifp;
298 ino_t new_ino;
299 int error;
300 int new_gen;
301 CLEANERINFO *cip;
302
303 fs = VTOI(ap->a_pvp)->i_lfs;
304 if (fs->lfs_ronly)
305 return EROFS;
306
307 ASSERT_NO_SEGLOCK(fs);
308
309 lfs_seglock(fs, SEGM_PROT);
310 vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
311
312 /* Get the head of the freelist. */
313 LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
314
315 #ifdef DIAGNOSTIC
316 if (new_ino == LFS_UNUSED_INUM) {
317 #ifdef DEBUG
318 lfs_dump_super(fs);
319 #endif /* DEBUG */
320 panic("inode 0 allocated [1]");
321 }
322 #endif /* DIAGNOSTIC */
323 DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %d\n", new_ino));
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 DLOG((DLOG_ALLOC, "lfs_valloc: headfree %d -> %d\n", new_ino,
334 ifp->if_nextfree));
335
336 new_gen = ifp->if_version; /* version was updated by vfree */
337 brelse(bp);
338
339 /* Extend IFILE so that the next lfs_valloc will succeed. */
340 if (fs->lfs_freehd == LFS_UNUSED_INUM) {
341 if ((error = extend_ifile(fs, ap->a_cred)) != 0) {
342 LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
343 VOP_UNLOCK(fs->lfs_ivnode, 0);
344 lfs_segunlock(fs);
345 return error;
346 }
347 }
348 #ifdef DIAGNOSTIC
349 if (fs->lfs_freehd == LFS_UNUSED_INUM)
350 panic("inode 0 allocated [3]");
351 #endif /* DIAGNOSTIC */
352
353 /* Set superblock modified bit and increment file count. */
354 simple_lock(&fs->lfs_interlock);
355 fs->lfs_fmod = 1;
356 simple_unlock(&fs->lfs_interlock);
357 ++fs->lfs_nfiles;
358
359 VOP_UNLOCK(fs->lfs_ivnode, 0);
360 lfs_segunlock(fs);
361
362 return lfs_ialloc(fs, ap->a_pvp, new_ino, new_gen, ap->a_vpp);
363 }
364
365 /*
366 * Finish allocating a new inode, given an inode and generation number.
367 */
368 static int
369 lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
370 struct vnode **vpp)
371 {
372 struct inode *ip;
373 struct vnode *vp;
374
375 ASSERT_NO_SEGLOCK(fs);
376
377 vp = *vpp;
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);
384 /* on-disk structure has been zeroed out by lfs_vcreate */
385 ip->i_din.ffs1_din->di_inumber = new_ino;
386
387 /* Note no blocks yet */
388 ip->i_lfs_hiblk = -1;
389
390 /* Set a new generation number for this inode. */
391 if (new_gen) {
392 ip->i_gen = new_gen;
393 ip->i_ffs1_gen = new_gen;
394 }
395
396 /* Insert into the inode hash table. */
397 ufs_ihashins(ip);
398 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
399
400 ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, vpp);
401 vp = *vpp;
402 ip = VTOI(vp);
403
404 memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
405
406 uvm_vnp_setsize(vp, 0);
407 lfs_mark_vnode(vp);
408 genfs_node_init(vp, &lfs_genfsops);
409 VREF(ip->i_devvp);
410 return (0);
411 }
412
413 /* Create a new vnode/inode pair and initialize what fields we can. */
414 void
415 lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp)
416 {
417 struct inode *ip;
418 struct ufs1_dinode *dp;
419 struct ufsmount *ump;
420 #ifdef QUOTA
421 int i;
422 #endif
423
424 /* Get a pointer to the private mount structure. */
425 ump = VFSTOUFS(mp);
426
427 ASSERT_NO_SEGLOCK(ump->um_lfs);
428
429 /* Initialize the inode. */
430 ip = pool_get(&lfs_inode_pool, PR_WAITOK);
431 memset(ip, 0, sizeof(*ip));
432 dp = pool_get(&lfs_dinode_pool, PR_WAITOK);
433 memset(dp, 0, sizeof(*dp));
434 ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK);
435 memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs));
436 vp->v_data = ip;
437 ip->i_din.ffs1_din = dp;
438 ip->i_ump = ump;
439 ip->i_vnode = vp;
440 ip->i_devvp = ump->um_devvp;
441 ip->i_dev = ump->um_dev;
442 ip->i_number = dp->di_inumber = ino;
443 ip->i_lfs = ump->um_lfs;
444 ip->i_lfs_effnblks = 0;
445 SPLAY_INIT(&ip->i_lfs_lbtree);
446 ip->i_lfs_nbtree = 0;
447 #ifdef QUOTA
448 for (i = 0; i < MAXQUOTAS; i++)
449 ip->i_dquot[i] = NODQUOT;
450 #endif
451 #ifdef DEBUG
452 if (ino == LFS_IFILE_INUM)
453 vp->v_vnlock->lk_wmesg = "inlock";
454 #endif
455 }
456
457 /* Free an inode. */
458 /* ARGUSED */
459 /* VOP_BWRITE 2i times */
460 int
461 lfs_vfree(void *v)
462 {
463 struct vop_vfree_args /* {
464 struct vnode *a_pvp;
465 ino_t a_ino;
466 int a_mode;
467 } */ *ap = v;
468 SEGUSE *sup;
469 CLEANERINFO *cip;
470 struct buf *cbp, *bp;
471 struct ifile *ifp;
472 struct inode *ip;
473 struct vnode *vp;
474 struct lfs *fs;
475 daddr_t old_iaddr;
476 ino_t ino, otail;
477 int s;
478
479 /* Get the inode number and file system. */
480 vp = ap->a_pvp;
481 ip = VTOI(vp);
482 fs = ip->i_lfs;
483 ino = ip->i_number;
484
485 ASSERT_NO_SEGLOCK(fs);
486
487 /* Drain of pending writes */
488 simple_lock(&vp->v_interlock);
489 s = splbio();
490 if (fs->lfs_version > 1 && WRITEINPROG(vp))
491 ltsleep(vp, (PRIBIO+1), "lfs_vfree", 0, &vp->v_interlock);
492 splx(s);
493 simple_unlock(&vp->v_interlock);
494
495 lfs_seglock(fs, SEGM_PROT);
496 vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
497
498 lfs_unmark_vnode(vp);
499 if (vp->v_flag & VDIROP) {
500 vp->v_flag &= ~VDIROP;
501 simple_lock(&fs->lfs_interlock);
502 simple_lock(&lfs_subsys_lock);
503 --lfs_dirvcount;
504 simple_unlock(&lfs_subsys_lock);
505 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
506 simple_unlock(&fs->lfs_interlock);
507 wakeup(&lfs_dirvcount);
508 lfs_vunref(vp);
509 }
510
511 LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
512 ip->i_flag &= ~IN_ALLMOD;
513
514 /*
515 * Set the ifile's inode entry to unused, increment its version number
516 * and link it onto the free chain.
517 */
518 LFS_IENTRY(ifp, fs, ino, bp);
519 old_iaddr = ifp->if_daddr;
520 ifp->if_daddr = LFS_UNUSED_DADDR;
521 ++ifp->if_version;
522 if (fs->lfs_version == 1) {
523 LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
524 LFS_PUT_HEADFREE(fs, cip, cbp, ino);
525 (void) LFS_BWRITE_LOG(bp); /* Ifile */
526 } else {
527 ifp->if_nextfree = LFS_UNUSED_INUM;
528 /*
529 * XXX Writing the freed node here means that it might not
530 * XXX make it into the free list in the event of a crash
531 * XXX (the ifile could be written before the rest of this
532 * XXX completes).
533 */
534 (void) LFS_BWRITE_LOG(bp); /* Ifile */
535 LFS_GET_TAILFREE(fs, cip, cbp, &otail);
536 LFS_IENTRY(ifp, fs, otail, bp);
537 ifp->if_nextfree = ino;
538 LFS_BWRITE_LOG(bp);
539 LFS_PUT_TAILFREE(fs, cip, cbp, ino);
540 DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %d -> %d\n", otail,
541 ino));
542 }
543 #ifdef DIAGNOSTIC
544 if (ino == LFS_UNUSED_INUM) {
545 panic("inode 0 freed");
546 }
547 #endif /* DIAGNOSTIC */
548 if (old_iaddr != LFS_UNUSED_DADDR) {
549 LFS_SEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp);
550 #ifdef DIAGNOSTIC
551 if (sup->su_nbytes < sizeof (struct ufs1_dinode)) {
552 printf("lfs_vfree: negative byte count"
553 " (segment %" PRIu32 " short by %d)\n",
554 dtosn(fs, old_iaddr),
555 (int)sizeof (struct ufs1_dinode) -
556 sup->su_nbytes);
557 panic("lfs_vfree: negative byte count");
558 sup->su_nbytes = sizeof (struct ufs1_dinode);
559 }
560 #endif
561 sup->su_nbytes -= sizeof (struct ufs1_dinode);
562 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp); /* Ifile */
563 }
564
565 /* Set superblock modified bit and decrement file count. */
566 simple_lock(&fs->lfs_interlock);
567 fs->lfs_fmod = 1;
568 simple_unlock(&fs->lfs_interlock);
569 --fs->lfs_nfiles;
570
571 VOP_UNLOCK(fs->lfs_ivnode, 0);
572 lfs_segunlock(fs);
573
574 return (0);
575 }
576