lfs_subr.c revision 1.19 1 /* $NetBSD: lfs_subr.c,v 1.19 2001/10/26 05:56:10 lukem 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_subr.c 8.4 (Berkeley) 5/8/95
71 */
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/namei.h>
76 #include <sys/vnode.h>
77 #include <sys/buf.h>
78 #include <sys/mount.h>
79 #include <sys/malloc.h>
80 #include <sys/proc.h>
81
82 #include <ufs/ufs/inode.h>
83 #include <ufs/lfs/lfs.h>
84 #include <ufs/lfs/lfs_extern.h>
85
86 /*
87 * Return buffer with the contents of block "offset" from the beginning of
88 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
89 * remaining space in the directory.
90 */
91 int
92 lfs_blkatoff(void *v)
93 {
94 struct vop_blkatoff_args /* {
95 struct vnode *a_vp;
96 off_t a_offset;
97 char **a_res;
98 struct buf **a_bpp;
99 } */ *ap = v;
100 struct lfs *fs;
101 struct inode *ip;
102 struct buf *bp;
103 ufs_daddr_t lbn;
104 int bsize, error;
105
106 ip = VTOI(ap->a_vp);
107 fs = ip->i_lfs;
108 lbn = lblkno(fs, ap->a_offset);
109 bsize = blksize(fs, ip, lbn);
110
111 *ap->a_bpp = NULL;
112 if ((error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) != 0) {
113 brelse(bp);
114 return (error);
115 }
116 if (ap->a_res)
117 *ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset);
118 *ap->a_bpp = bp;
119 return (0);
120 }
121
122
123 /*
124 * lfs_seglock --
125 * Single thread the segment writer.
126 */
127 void
128 lfs_seglock(struct lfs *fs, unsigned long flags)
129 {
130 struct segment *sp;
131 int s;
132
133 if (fs->lfs_seglock) {
134 if (fs->lfs_lockpid == curproc->p_pid) {
135 ++fs->lfs_seglock;
136 fs->lfs_sp->seg_flags |= flags;
137 return;
138 } else while (fs->lfs_seglock)
139 (void)tsleep(&fs->lfs_seglock, PRIBIO + 1,
140 "lfs seglock", 0);
141 }
142
143 fs->lfs_seglock = 1;
144 fs->lfs_lockpid = curproc->p_pid;
145
146 sp = fs->lfs_sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK);
147 sp->bpp = malloc(((fs->lfs_sumsize - SEGSUM_SIZE(fs)) /
148 sizeof(ufs_daddr_t) + 1) * sizeof(struct buf *),
149 M_SEGMENT, M_WAITOK);
150 sp->seg_flags = flags;
151 sp->vp = NULL;
152 (void) lfs_initseg(fs);
153
154 /*
155 * Keep a cumulative count of the outstanding I/O operations. If the
156 * disk drive catches up with us it could go to zero before we finish,
157 * so we artificially increment it by one until we've scheduled all of
158 * the writes we intend to do.
159 */
160 s = splbio();
161 ++fs->lfs_iocount;
162 splx(s);
163 }
164
165 /*
166 * lfs_segunlock --
167 * Single thread the segment writer.
168 */
169 void
170 lfs_segunlock(struct lfs *fs)
171 {
172 struct segment *sp;
173 unsigned long sync, ckp;
174 int s;
175 struct vnode *vp;
176 struct mount *mp;
177 extern int lfs_dirvcount;
178
179 sp = fs->lfs_sp;
180
181 if (fs->lfs_seglock == 1 && !(sp->seg_flags & SEGM_PROT)) {
182
183 mp = fs->lfs_ivnode->v_mount;
184 /*
185 * Go through and unmark all DIROP vnodes, possibly
186 * calling VOP_INACTIVE (through vrele). This is
187 * delayed until now in order not to accidentally
188 * write a DIROP node through lfs_flush.
189 */
190 #ifndef LFS_NO_BACKVP_HACK
191 /* BEGIN HACK */
192 #define VN_OFFSET (((caddr_t)&vp->v_mntvnodes.le_next) - (caddr_t)vp)
193 #define BACK_VP(VP) ((struct vnode *)(((caddr_t)VP->v_mntvnodes.le_prev) - VN_OFFSET))
194 #define BEG_OF_VLIST ((struct vnode *)(((caddr_t)&mp->mnt_vnodelist.lh_first) - VN_OFFSET))
195
196 /* Find last vnode. */
197 loop: for (vp = mp->mnt_vnodelist.lh_first;
198 vp && vp->v_mntvnodes.le_next != NULL;
199 vp = vp->v_mntvnodes.le_next);
200 for (; vp && vp != BEG_OF_VLIST; vp = BACK_VP(vp)) {
201 #else
202 loop:
203 for (vp = mp->mnt_vnodelist.lh_first;
204 vp != NULL;
205 vp = vp->v_mntvnodes.le_next) {
206 #endif
207 if (vp->v_mount != mp)
208 goto loop;
209 if (vp->v_type == VNON)
210 continue;
211 if (lfs_vref(vp))
212 continue;
213 if (VOP_ISLOCKED(vp) &&
214 vp->v_lock.lk_lockholder != curproc->p_pid) {
215 lfs_vunref(vp);
216 continue;
217 }
218 if ((vp->v_flag & VDIROP) &&
219 !(VTOI(vp)->i_flag & IN_ADIROP)) {
220 --lfs_dirvcount;
221 vp->v_flag &= ~VDIROP;
222 wakeup(&lfs_dirvcount);
223 fs->lfs_unlockvp = vp;
224 lfs_vunref(vp);
225 vrele(vp);
226 fs->lfs_unlockvp = NULL;
227 } else {
228 lfs_vunref(vp);
229 }
230 }
231 }
232
233 if (fs->lfs_seglock == 1) {
234 sync = sp->seg_flags & SEGM_SYNC;
235 ckp = sp->seg_flags & SEGM_CKP;
236 if (sp->bpp != sp->cbpp) {
237 /* Free allocated segment summary */
238 fs->lfs_offset -= btofsb(fs, fs->lfs_sumsize);
239 lfs_freebuf(*sp->bpp);
240 } else
241 printf ("unlock to 0 with no summary");
242
243 free(sp->bpp, M_SEGMENT);
244 sp->bpp = NULL;
245 free(sp, M_SEGMENT);
246 fs->lfs_sp = NULL;
247
248 /*
249 * If the I/O count is non-zero, sleep until it reaches zero.
250 * At the moment, the user's process hangs around so we can
251 * sleep.
252 */
253 s = splbio();
254 --fs->lfs_iocount;
255 /*
256 * We let checkpoints happen asynchronously. That means
257 * that during recovery, we have to roll forward between
258 * the two segments described by the first and second
259 * superblocks to make sure that the checkpoint described
260 * by a superblock completed.
261 */
262 while (sync && fs->lfs_iocount)
263 (void)tsleep(&fs->lfs_iocount, PRIBIO + 1,
264 "lfs vflush", 0);
265 splx(s);
266 if (ckp) {
267 fs->lfs_nactive = 0;
268 /* If we *know* everything's on disk, write both sbs */
269 if(sync)
270 lfs_writesuper(fs,fs->lfs_sboffs[fs->lfs_activesb]);
271 fs->lfs_activesb = 1 - fs->lfs_activesb;
272 lfs_writesuper(fs,fs->lfs_sboffs[fs->lfs_activesb]);
273 }
274 --fs->lfs_seglock;
275 fs->lfs_lockpid = 0;
276 wakeup(&fs->lfs_seglock);
277 } else if (fs->lfs_seglock == 0) {
278 panic ("Seglock not held");
279 } else {
280 --fs->lfs_seglock;
281 }
282 }
283