lfs_subr.c revision 1.2
1/*	$NetBSD: lfs_subr.c,v 1.2 1994/06/29 06:47:00 cgd 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/namei.h>
40#include <sys/vnode.h>
41#include <sys/buf.h>
42#include <sys/mount.h>
43#include <sys/malloc.h>
44#include <sys/proc.h>
45
46#include <ufs/ufs/quota.h>
47#include <ufs/ufs/inode.h>
48#include <ufs/lfs/lfs.h>
49#include <ufs/lfs/lfs_extern.h>
50
51/*
52 * Return buffer with the contents of block "offset" from the beginning of
53 * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
54 * remaining space in the directory.
55 */
56int
57lfs_blkatoff(ap)
58	struct vop_blkatoff_args /* {
59		struct vnode *a_vp;
60		off_t a_offset;
61		char **a_res;
62		struct buf **a_bpp;
63	} */ *ap;
64{
65	register struct lfs *fs;
66	struct inode *ip;
67	struct buf *bp;
68	daddr_t lbn;
69	int bsize, error;
70
71	ip = VTOI(ap->a_vp);
72	fs = ip->i_lfs;
73	lbn = lblkno(fs, ap->a_offset);
74	bsize = blksize(fs);
75
76	*ap->a_bpp = NULL;
77	if (error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) {
78		brelse(bp);
79		return (error);
80	}
81	if (ap->a_res)
82		*ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset);
83	*ap->a_bpp = bp;
84	return (0);
85}
86
87
88/*
89 * lfs_seglock --
90 *	Single thread the segment writer.
91 */
92void
93lfs_seglock(fs, flags)
94	struct lfs *fs;
95	unsigned long flags;
96{
97	struct segment *sp;
98	int s;
99
100	if (fs->lfs_seglock)
101		if (fs->lfs_lockpid == curproc->p_pid) {
102			++fs->lfs_seglock;
103			fs->lfs_sp->seg_flags |= flags;
104			return;
105		} else while (fs->lfs_seglock)
106			(void)tsleep(&fs->lfs_seglock, PRIBIO + 1,
107			    "lfs seglock", 0);
108
109	fs->lfs_seglock = 1;
110	fs->lfs_lockpid = curproc->p_pid;
111
112	sp = fs->lfs_sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK);
113	sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) /
114	    sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK);
115	sp->seg_flags = flags;
116	sp->vp = NULL;
117	(void) lfs_initseg(fs);
118
119	/*
120	 * Keep a cumulative count of the outstanding I/O operations.  If the
121	 * disk drive catches up with us it could go to zero before we finish,
122	 * so we artificially increment it by one until we've scheduled all of
123	 * the writes we intend to do.
124	 */
125	s = splbio();
126	++fs->lfs_iocount;
127	splx(s);
128}
129/*
130 * lfs_segunlock --
131 *	Single thread the segment writer.
132 */
133void
134lfs_segunlock(fs)
135	struct lfs *fs;
136{
137	struct segment *sp;
138	unsigned long sync, ckp;
139	int s;
140
141	if (fs->lfs_seglock == 1) {
142
143		sp = fs->lfs_sp;
144		sync = sp->seg_flags & SEGM_SYNC;
145		ckp = sp->seg_flags & SEGM_CKP;
146		if (sp->bpp != sp->cbpp) {
147			/* Free allocated segment summary */
148			fs->lfs_offset -= LFS_SUMMARY_SIZE / DEV_BSIZE;
149			brelvp(*sp->bpp);
150			free((*sp->bpp)->b_data, M_SEGMENT);
151			free(*sp->bpp, M_SEGMENT);
152		} else
153			printf ("unlock to 0 with no summary");
154		free(sp->bpp, M_SEGMENT);
155		free(sp, M_SEGMENT);
156
157		/*
158		 * If the I/O count is non-zero, sleep until it reaches zero.
159		 * At the moment, the user's process hangs around so we can
160		 * sleep.
161		 */
162		s = splbio();
163		--fs->lfs_iocount;
164		/*
165		 * We let checkpoints happen asynchronously.  That means
166		 * that during recovery, we have to roll forward between
167		 * the two segments described by the first and second
168		 * superblocks to make sure that the checkpoint described
169		 * by a superblock completed.
170		 */
171		if (sync && fs->lfs_iocount)
172		    (void)tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs vflush", 0);
173		splx(s);
174		if (ckp) {
175			fs->lfs_nactive = 0;
176			lfs_writesuper(fs);
177		}
178		--fs->lfs_seglock;
179		fs->lfs_lockpid = 0;
180		wakeup(&fs->lfs_seglock);
181	} else if (fs->lfs_seglock == 0) {
182		panic ("Seglock not held");
183	} else {
184		--fs->lfs_seglock;
185	}
186}
187