lfs_subr.c revision 1.6 1 /* $NetBSD: lfs_subr.c,v 1.6 1998/03/01 02:23:25 fvdl 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.4 (Berkeley) 5/8/95
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 ufs_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, ip, lbn);
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(ufs_daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT,
117 M_WAITOK);
118 sp->seg_flags = flags;
119 sp->vp = NULL;
120 (void) lfs_initseg(fs);
121
122 /*
123 * Keep a cumulative count of the outstanding I/O operations. If the
124 * disk drive catches up with us it could go to zero before we finish,
125 * so we artificially increment it by one until we've scheduled all of
126 * the writes we intend to do.
127 */
128 s = splbio();
129 ++fs->lfs_iocount;
130 splx(s);
131 }
132 /*
133 * lfs_segunlock --
134 * Single thread the segment writer.
135 */
136 void
137 lfs_segunlock(fs)
138 struct lfs *fs;
139 {
140 struct segment *sp;
141 unsigned long sync, ckp;
142 int s;
143
144 if (fs->lfs_seglock == 1) {
145
146 sp = fs->lfs_sp;
147 sync = sp->seg_flags & SEGM_SYNC;
148 ckp = sp->seg_flags & SEGM_CKP;
149 if (sp->bpp != sp->cbpp) {
150 /* Free allocated segment summary */
151 fs->lfs_offset -= LFS_SUMMARY_SIZE / DEV_BSIZE;
152 brelvp(*sp->bpp);
153 free((*sp->bpp)->b_data, M_SEGMENT);
154 free(*sp->bpp, M_SEGMENT);
155 } else
156 printf ("unlock to 0 with no summary");
157 free(sp->bpp, M_SEGMENT);
158 free(sp, M_SEGMENT);
159
160 /*
161 * If the I/O count is non-zero, sleep until it reaches zero.
162 * At the moment, the user's process hangs around so we can
163 * sleep.
164 */
165 s = splbio();
166 --fs->lfs_iocount;
167 /*
168 * We let checkpoints happen asynchronously. That means
169 * that during recovery, we have to roll forward between
170 * the two segments described by the first and second
171 * superblocks to make sure that the checkpoint described
172 * by a superblock completed.
173 */
174 if (sync && fs->lfs_iocount)
175 (void)tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs vflush", 0);
176 splx(s);
177 if (ckp) {
178 fs->lfs_nactive = 0;
179 lfs_writesuper(fs);
180 }
181 --fs->lfs_seglock;
182 fs->lfs_lockpid = 0;
183 wakeup(&fs->lfs_seglock);
184 } else if (fs->lfs_seglock == 0) {
185 panic ("Seglock not held");
186 } else {
187 --fs->lfs_seglock;
188 }
189 }
190