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