lfs_alloc.c revision 1.78 1 /* $NetBSD: lfs_alloc.c,v 1.78 2005/04/01 21:59:46 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. 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.78 2005/04/01 21:59:46 perseant 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
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 struct lock ufs_hashlock;
96
97 static int extend_ifile(struct lfs *, struct ucred *);
98 static int lfs_ialloc(struct lfs *, struct vnode *, ino_t, int,
99 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 * Called with the Ifile inode locked.
110 */
111 int
112 lfs_rf_valloc(struct lfs *fs, ino_t ino, int version, struct proc *p,
113 struct vnode **vpp)
114 {
115 IFILE *ifp;
116 struct buf *bp, *cbp;
117 struct vnode *vp;
118 struct inode *ip;
119 ino_t tino, oldnext;
120 int error;
121 CLEANERINFO *cip;
122
123 ASSERT_SEGLOCK(fs); /* XXX it doesn't, really */
124
125 /*
126 * First, just try a vget. If the version number is the one we want,
127 * we don't have to do anything else. If the version number is wrong,
128 * take appropriate action.
129 */
130 error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
131 if (error == 0) {
132 DLOG((DLOG_RF, "lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp));
133
134 *vpp = vp;
135 ip = VTOI(vp);
136 if (ip->i_gen == version)
137 return 0;
138 else if (ip->i_gen < version) {
139 VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, p);
140 ip->i_gen = ip->i_ffs1_gen = version;
141 LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
142 return 0;
143 } else {
144 DLOG((DLOG_RF, "ino %d: sought version %d, got %d\n",
145 ino, version, ip->i_ffs1_gen));
146 vput(vp);
147 *vpp = NULLVP;
148 return EEXIST;
149 }
150 }
151
152 /*
153 * The inode is not in use. Find it on the free list.
154 */
155 /* If the Ifile is too short to contain this inum, extend it */
156 while (VTOI(fs->lfs_ivnode)->i_size <= (ino /
157 fs->lfs_ifpb + fs->lfs_cleansz + fs->lfs_segtabsz)
158 << fs->lfs_bshift) {
159 extend_ifile(fs, NOCRED);
160 }
161
162 LFS_IENTRY(ifp, fs, ino, bp);
163 oldnext = ifp->if_nextfree;
164 ifp->if_version = version;
165 brelse(bp);
166
167 LFS_GET_HEADFREE(fs, cip, cbp, &ino);
168 if (ino) {
169 LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
170 } else {
171 tino = ino;
172 while (1) {
173 LFS_IENTRY(ifp, fs, tino, bp);
174 if (ifp->if_nextfree == ino ||
175 ifp->if_nextfree == LFS_UNUSED_INUM)
176 break;
177 tino = ifp->if_nextfree;
178 brelse(bp);
179 }
180 if (ifp->if_nextfree == LFS_UNUSED_INUM) {
181 brelse(bp);
182 return ENOENT;
183 }
184 ifp->if_nextfree = oldnext;
185 LFS_BWRITE_LOG(bp);
186 }
187
188 error = lfs_ialloc(fs, fs->lfs_ivnode, ino, version, &vp);
189 if (error == 0) {
190 /*
191 * Make it VREG so we can put blocks on it. We will change
192 * this later if it turns out to be some other kind of file.
193 */
194 ip = VTOI(vp);
195 ip->i_mode = ip->i_ffs1_mode = IFREG;
196 ip->i_nlink = ip->i_ffs1_nlink = 1;
197 ip->i_ffs_effnlink = 1;
198 ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
199 ip = VTOI(vp);
200
201 DLOG((DLOG_RF, "lfs_rf_valloc: ino %d vp %p\n", ino, vp));
202
203 /* The dirop-nature of this vnode is past */
204 lfs_unmark_vnode(vp);
205 (void)lfs_vunref(vp);
206 vp->v_flag &= ~VDIROP;
207 simple_lock(&fs->lfs_interlock);
208 simple_lock(&lfs_subsys_lock);
209 --lfs_dirvcount;
210 simple_unlock(&lfs_subsys_lock);
211 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
212 simple_unlock(&fs->lfs_interlock);
213 }
214 *vpp = vp;
215 return error;
216 }
217
218 /*
219 * Add a new block to the Ifile, to accommodate future file creations.
220 * Called with the segment lock held.
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 ASSERT_SEGLOCK(fs);
236
237 vp = fs->lfs_ivnode;
238 ip = VTOI(vp);
239 blkno = lblkno(fs, ip->i_size);
240 if ((error = VOP_BALLOC(vp, ip->i_size, fs->lfs_bsize, cred, 0,
241 &bp)) != 0) {
242 return (error);
243 }
244 ip->i_size += fs->lfs_bsize;
245 ip->i_ffs1_size = ip->i_size;
246 uvm_vnp_setsize(vp, ip->i_size);
247
248 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
249 fs->lfs_ifpb;
250 LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
251 LFS_PUT_HEADFREE(fs, cip, cbp, i);
252 #ifdef DIAGNOSTIC
253 if (fs->lfs_freehd == LFS_UNUSED_INUM)
254 panic("inode 0 allocated [2]");
255 #endif /* DIAGNOSTIC */
256 max = i + fs->lfs_ifpb;
257
258 if (fs->lfs_version == 1) {
259 for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < max; ++ifp_v1) {
260 ifp_v1->if_version = 1;
261 ifp_v1->if_daddr = LFS_UNUSED_DADDR;
262 ifp_v1->if_nextfree = ++i;
263 }
264 ifp_v1--;
265 ifp_v1->if_nextfree = oldlast;
266 } else {
267 for (ifp = (IFILE *)bp->b_data; i < max; ++ifp) {
268 ifp->if_version = 1;
269 ifp->if_daddr = LFS_UNUSED_DADDR;
270 ifp->if_nextfree = ++i;
271 }
272 ifp--;
273 ifp->if_nextfree = oldlast;
274 }
275 LFS_PUT_TAILFREE(fs, cip, cbp, max - 1);
276
277 (void) LFS_BWRITE_LOG(bp); /* Ifile */
278
279 return 0;
280 }
281
282 /* Allocate a new inode. */
283 /* ARGSUSED */
284 /* VOP_BWRITE 2i times */
285 int
286 lfs_valloc(void *v)
287 {
288 struct vop_valloc_args /* {
289 struct vnode *a_pvp;
290 int a_mode;
291 struct ucred *a_cred;
292 struct vnode **a_vpp;
293 } */ *ap = v;
294 struct lfs *fs;
295 struct buf *bp, *cbp;
296 struct ifile *ifp;
297 ino_t new_ino;
298 int error;
299 int new_gen;
300 CLEANERINFO *cip;
301
302 fs = VTOI(ap->a_pvp)->i_lfs;
303 if (fs->lfs_ronly)
304 return EROFS;
305
306 ASSERT_NO_SEGLOCK(fs);
307
308 lfs_seglock(fs, SEGM_PROT);
309 vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
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 DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %d\n", new_ino));
323
324 /*
325 * Remove the inode from the free list and write the new start
326 * of the free list into the superblock.
327 */
328 LFS_IENTRY(ifp, fs, new_ino, bp);
329 if (ifp->if_daddr != LFS_UNUSED_DADDR)
330 panic("lfs_valloc: inuse inode %d on the free list", new_ino);
331 LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
332 DLOG((DLOG_ALLOC, "lfs_valloc: headfree %d -> %d\n", new_ino,
333 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_freehd == LFS_UNUSED_INUM) {
340 if ((error = extend_ifile(fs, ap->a_cred)) != 0) {
341 LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
342 VOP_UNLOCK(fs->lfs_ivnode, 0);
343 lfs_segunlock(fs);
344 return error;
345 }
346 }
347 #ifdef DIAGNOSTIC
348 if (fs->lfs_freehd == LFS_UNUSED_INUM)
349 panic("inode 0 allocated [3]");
350 #endif /* DIAGNOSTIC */
351
352 /* Set superblock modified bit and increment file count. */
353 simple_lock(&fs->lfs_interlock);
354 fs->lfs_fmod = 1;
355 simple_unlock(&fs->lfs_interlock);
356 ++fs->lfs_nfiles;
357
358 VOP_UNLOCK(fs->lfs_ivnode, 0);
359 lfs_segunlock(fs);
360
361 return lfs_ialloc(fs, ap->a_pvp, new_ino, new_gen, ap->a_vpp);
362 }
363
364 /*
365 * Finish allocating a new inode, given an inode and generation number.
366 */
367 static int
368 lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
369 struct vnode **vpp)
370 {
371 struct inode *ip;
372 struct vnode *vp;
373
374 ASSERT_NO_SEGLOCK(fs);
375
376 vp = *vpp;
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);
383 /* on-disk structure has been zeroed out by lfs_vcreate */
384 ip->i_din.ffs1_din->di_inumber = new_ino;
385
386 /* Set a new generation number for this inode. */
387 if (new_gen) {
388 ip->i_gen = new_gen;
389 ip->i_ffs1_gen = new_gen;
390 }
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, vpp);
397 vp = *vpp;
398 ip = VTOI(vp);
399
400 memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
401
402 uvm_vnp_setsize(vp, 0);
403 lfs_mark_vnode(vp);
404 genfs_node_init(vp, &lfs_genfsops);
405 VREF(ip->i_devvp);
406 return (0);
407 }
408
409 /* Create a new vnode/inode pair and initialize what fields we can. */
410 void
411 lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp)
412 {
413 struct inode *ip;
414 struct ufs1_dinode *dp;
415 struct ufsmount *ump;
416 int i;
417
418 /* Get a pointer to the private mount structure. */
419 ump = VFSTOUFS(mp);
420
421 ASSERT_NO_SEGLOCK(ump->um_lfs);
422
423 /* Initialize the inode. */
424 ip = pool_get(&lfs_inode_pool, PR_WAITOK);
425 memset(ip, 0, sizeof(*ip));
426 dp = pool_get(&lfs_dinode_pool, PR_WAITOK);
427 memset(dp, 0, sizeof(*dp));
428 ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK);
429 memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs));
430 for (i = 0; i < LFS_BLIST_HASH_WIDTH; i++)
431 LIST_INIT(&(ip->i_lfs_blist[i]));
432 vp->v_data = ip;
433 ip->i_din.ffs1_din = dp;
434 ip->i_ump = ump;
435 ip->i_vnode = vp;
436 ip->i_devvp = ump->um_devvp;
437 ip->i_dev = ump->um_dev;
438 ip->i_number = dp->di_inumber = ino;
439 ip->i_lfs = ump->um_lfs;
440 ip->i_lfs_effnblks = 0;
441 #ifdef QUOTA
442 for (i = 0; i < MAXQUOTAS; i++)
443 ip->i_dquot[i] = NODQUOT;
444 #endif
445 #ifdef DEBUG
446 if (ino == LFS_IFILE_INUM)
447 vp->v_vnlock->lk_wmesg = "inlock";
448 #endif
449 }
450
451 /* Free an inode. */
452 /* ARGUSED */
453 /* VOP_BWRITE 2i times */
454 int
455 lfs_vfree(void *v)
456 {
457 struct vop_vfree_args /* {
458 struct vnode *a_pvp;
459 ino_t a_ino;
460 int a_mode;
461 } */ *ap = v;
462 SEGUSE *sup;
463 CLEANERINFO *cip;
464 struct buf *cbp, *bp;
465 struct ifile *ifp;
466 struct inode *ip;
467 struct vnode *vp;
468 struct lfs *fs;
469 daddr_t old_iaddr;
470 ino_t ino, otail;
471 int s;
472
473 /* Get the inode number and file system. */
474 vp = ap->a_pvp;
475 ip = VTOI(vp);
476 fs = ip->i_lfs;
477 ino = ip->i_number;
478
479 ASSERT_NO_SEGLOCK(fs);
480
481 /* Drain of pending writes */
482 simple_lock(&vp->v_interlock);
483 s = splbio();
484 if (fs->lfs_version > 1 && WRITEINPROG(vp))
485 ltsleep(vp, (PRIBIO+1), "lfs_vfree", 0, &vp->v_interlock);
486 splx(s);
487 simple_unlock(&vp->v_interlock);
488
489 lfs_seglock(fs, SEGM_PROT);
490 vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
491
492 lfs_unmark_vnode(vp);
493 if (vp->v_flag & VDIROP) {
494 vp->v_flag &= ~VDIROP;
495 simple_lock(&fs->lfs_interlock);
496 simple_lock(&lfs_subsys_lock);
497 --lfs_dirvcount;
498 simple_unlock(&lfs_subsys_lock);
499 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
500 simple_unlock(&fs->lfs_interlock);
501 wakeup(&lfs_dirvcount);
502 lfs_vunref(vp);
503 }
504
505 LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
506 ip->i_flag &= ~IN_ALLMOD;
507
508 /*
509 * Set the ifile's inode entry to unused, increment its version number
510 * and link it onto the free chain.
511 */
512 LFS_IENTRY(ifp, fs, ino, bp);
513 old_iaddr = ifp->if_daddr;
514 ifp->if_daddr = LFS_UNUSED_DADDR;
515 ++ifp->if_version;
516 if (fs->lfs_version == 1) {
517 LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
518 LFS_PUT_HEADFREE(fs, cip, cbp, ino);
519 (void) LFS_BWRITE_LOG(bp); /* Ifile */
520 } else {
521 ifp->if_nextfree = LFS_UNUSED_INUM;
522 /*
523 * XXX Writing the freed node here means that it might not
524 * XXX make it into the free list in the event of a crash
525 * XXX (the ifile could be written before the rest of this
526 * XXX completes).
527 */
528 (void) LFS_BWRITE_LOG(bp); /* Ifile */
529 LFS_GET_TAILFREE(fs, cip, cbp, &otail);
530 LFS_IENTRY(ifp, fs, otail, bp);
531 ifp->if_nextfree = ino;
532 LFS_BWRITE_LOG(bp);
533 LFS_PUT_TAILFREE(fs, cip, cbp, ino);
534 DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %d -> %d\n", otail,
535 ino));
536 }
537 #ifdef DIAGNOSTIC
538 if (ino == LFS_UNUSED_INUM) {
539 panic("inode 0 freed");
540 }
541 #endif /* DIAGNOSTIC */
542 if (old_iaddr != LFS_UNUSED_DADDR) {
543 LFS_SEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp);
544 #ifdef DIAGNOSTIC
545 if (sup->su_nbytes < sizeof (struct ufs1_dinode)) {
546 printf("lfs_vfree: negative byte count"
547 " (segment %" PRIu32 " short by %d)\n",
548 dtosn(fs, old_iaddr),
549 (int)sizeof (struct ufs1_dinode) -
550 sup->su_nbytes);
551 panic("lfs_vfree: negative byte count");
552 sup->su_nbytes = sizeof (struct ufs1_dinode);
553 }
554 #endif
555 sup->su_nbytes -= sizeof (struct ufs1_dinode);
556 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp); /* Ifile */
557 }
558
559 /* Set superblock modified bit and decrement file count. */
560 simple_lock(&fs->lfs_interlock);
561 fs->lfs_fmod = 1;
562 simple_unlock(&fs->lfs_interlock);
563 --fs->lfs_nfiles;
564
565 VOP_UNLOCK(fs->lfs_ivnode, 0);
566 lfs_segunlock(fs);
567
568 return (0);
569 }
570