xref: /netbsd-src/sys/ufs/lfs/lfs_subr.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: lfs_subr.c,v 1.5 1996/10/12 21:58:52 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)lfs_subr.c	8.2 (Berkeley) 9/21/93
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/namei.h>
41 #include <sys/vnode.h>
42 #include <sys/buf.h>
43 #include <sys/mount.h>
44 #include <sys/malloc.h>
45 #include <sys/proc.h>
46 
47 #include <ufs/ufs/quota.h>
48 #include <ufs/ufs/inode.h>
49 #include <ufs/lfs/lfs.h>
50 #include <ufs/lfs/lfs_extern.h>
51 
52 /*
53  * Return buffer with the contents of block "offset" from the beginning of
54  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
55  * remaining space in the directory.
56  */
57 int
58 lfs_blkatoff(v)
59 	void *v;
60 {
61 	struct vop_blkatoff_args /* {
62 		struct vnode *a_vp;
63 		off_t a_offset;
64 		char **a_res;
65 		struct buf **a_bpp;
66 	} */ *ap = v;
67 	register struct lfs *fs;
68 	struct inode *ip;
69 	struct buf *bp;
70 	daddr_t lbn;
71 	int bsize, error;
72 
73 	ip = VTOI(ap->a_vp);
74 	fs = ip->i_lfs;
75 	lbn = lblkno(fs, ap->a_offset);
76 	bsize = blksize(fs);
77 
78 	*ap->a_bpp = NULL;
79 	if ((error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) != 0) {
80 		brelse(bp);
81 		return (error);
82 	}
83 	if (ap->a_res)
84 		*ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset);
85 	*ap->a_bpp = bp;
86 	return (0);
87 }
88 
89 
90 /*
91  * lfs_seglock --
92  *	Single thread the segment writer.
93  */
94 void
95 lfs_seglock(fs, flags)
96 	struct lfs *fs;
97 	unsigned long flags;
98 {
99 	struct segment *sp;
100 	int s;
101 
102 	if (fs->lfs_seglock)
103 		if (fs->lfs_lockpid == curproc->p_pid) {
104 			++fs->lfs_seglock;
105 			fs->lfs_sp->seg_flags |= flags;
106 			return;
107 		} else while (fs->lfs_seglock)
108 			(void)tsleep(&fs->lfs_seglock, PRIBIO + 1,
109 			    "lfs seglock", 0);
110 
111 	fs->lfs_seglock = 1;
112 	fs->lfs_lockpid = curproc->p_pid;
113 
114 	sp = fs->lfs_sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK);
115 	sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) /
116 	    sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK);
117 	sp->seg_flags = flags;
118 	sp->vp = NULL;
119 	(void) lfs_initseg(fs);
120 
121 	/*
122 	 * Keep a cumulative count of the outstanding I/O operations.  If the
123 	 * disk drive catches up with us it could go to zero before we finish,
124 	 * so we artificially increment it by one until we've scheduled all of
125 	 * the writes we intend to do.
126 	 */
127 	s = splbio();
128 	++fs->lfs_iocount;
129 	splx(s);
130 }
131 /*
132  * lfs_segunlock --
133  *	Single thread the segment writer.
134  */
135 void
136 lfs_segunlock(fs)
137 	struct lfs *fs;
138 {
139 	struct segment *sp;
140 	unsigned long sync, ckp;
141 	int s;
142 
143 	if (fs->lfs_seglock == 1) {
144 
145 		sp = fs->lfs_sp;
146 		sync = sp->seg_flags & SEGM_SYNC;
147 		ckp = sp->seg_flags & SEGM_CKP;
148 		if (sp->bpp != sp->cbpp) {
149 			/* Free allocated segment summary */
150 			fs->lfs_offset -= LFS_SUMMARY_SIZE / DEV_BSIZE;
151 			brelvp(*sp->bpp);
152 			free((*sp->bpp)->b_data, M_SEGMENT);
153 			free(*sp->bpp, M_SEGMENT);
154 		} else
155 			printf ("unlock to 0 with no summary");
156 		free(sp->bpp, M_SEGMENT);
157 		free(sp, M_SEGMENT);
158 
159 		/*
160 		 * If the I/O count is non-zero, sleep until it reaches zero.
161 		 * At the moment, the user's process hangs around so we can
162 		 * sleep.
163 		 */
164 		s = splbio();
165 		--fs->lfs_iocount;
166 		/*
167 		 * We let checkpoints happen asynchronously.  That means
168 		 * that during recovery, we have to roll forward between
169 		 * the two segments described by the first and second
170 		 * superblocks to make sure that the checkpoint described
171 		 * by a superblock completed.
172 		 */
173 		if (sync && fs->lfs_iocount)
174 		    (void)tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs vflush", 0);
175 		splx(s);
176 		if (ckp) {
177 			fs->lfs_nactive = 0;
178 			lfs_writesuper(fs);
179 		}
180 		--fs->lfs_seglock;
181 		fs->lfs_lockpid = 0;
182 		wakeup(&fs->lfs_seglock);
183 	} else if (fs->lfs_seglock == 0) {
184 		panic ("Seglock not held");
185 	} else {
186 		--fs->lfs_seglock;
187 	}
188 }
189