Deleted Added
sdiff udiff text old ( 34924 ) new ( 34961 )
full compact
1/*
2 * Copyright (c) 1982, 1986, 1989, 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 * @(#)ffs_vnops.c 8.15 (Berkeley) 5/14/95
34 * $Id: ffs_vnops.c,v 1.47 1998/03/28 10:33:26 bde Exp $
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/resourcevar.h>
40#include <sys/signalvar.h>
41#include <sys/kernel.h>
42#include <sys/stat.h>
43#include <sys/buf.h>
44#include <sys/proc.h>
45#include <sys/mount.h>
46#include <sys/sysctl.h>
47#include <sys/vnode.h>
48
49#include <machine/limits.h>
50
51#include <vm/vm.h>
52#include <vm/vm_prot.h>
53#include <vm/vm_page.h>
54#include <vm/vm_object.h>
55#include <vm/vm_extern.h>
56
57#include <ufs/ufs/quota.h>
58#include <ufs/ufs/inode.h>
59#include <ufs/ufs/ufsmount.h>
60#include <ufs/ufs/ufs_extern.h>
61
62#include <ufs/ffs/fs.h>
63#include <ufs/ffs/ffs_extern.h>
64
65static int ffs_fsync __P((struct vop_fsync_args *));
66static int ffs_getpages __P((struct vop_getpages_args *));
67static int ffs_putpages __P((struct vop_putpages_args *));
68static int ffs_read __P((struct vop_read_args *));
69static int ffs_write __P((struct vop_write_args *));
70
71/* Global vfs data structures for ufs. */
72vop_t **ffs_vnodeop_p;
73static struct vnodeopv_entry_desc ffs_vnodeop_entries[] = {
74 { &vop_default_desc, (vop_t *) ufs_vnoperate },
75 { &vop_fsync_desc, (vop_t *) ffs_fsync },
76 { &vop_getpages_desc, (vop_t *) ffs_getpages },
77 { &vop_putpages_desc, (vop_t *) ffs_putpages },
78 { &vop_read_desc, (vop_t *) ffs_read },
79 { &vop_balloc_desc, (vop_t *) ffs_balloc },
80 { &vop_reallocblks_desc, (vop_t *) ffs_reallocblks },
81 { &vop_write_desc, (vop_t *) ffs_write },
82 { NULL, NULL }
83};
84static struct vnodeopv_desc ffs_vnodeop_opv_desc =
85 { &ffs_vnodeop_p, ffs_vnodeop_entries };
86
87vop_t **ffs_specop_p;
88static struct vnodeopv_entry_desc ffs_specop_entries[] = {
89 { &vop_default_desc, (vop_t *) ufs_vnoperatespec },
90 { &vop_fsync_desc, (vop_t *) ffs_fsync },
91 { NULL, NULL }
92};
93static struct vnodeopv_desc ffs_specop_opv_desc =
94 { &ffs_specop_p, ffs_specop_entries };
95
96vop_t **ffs_fifoop_p;
97static struct vnodeopv_entry_desc ffs_fifoop_entries[] = {
98 { &vop_default_desc, (vop_t *) ufs_vnoperatefifo },
99 { &vop_fsync_desc, (vop_t *) ffs_fsync },
100 { NULL, NULL }
101};
102static struct vnodeopv_desc ffs_fifoop_opv_desc =
103 { &ffs_fifoop_p, ffs_fifoop_entries };
104
105VNODEOP_SET(ffs_vnodeop_opv_desc);
106VNODEOP_SET(ffs_specop_opv_desc);
107VNODEOP_SET(ffs_fifoop_opv_desc);
108
109SYSCTL_NODE(_vfs, MOUNT_UFS, ffs, CTLFLAG_RW, 0, "FFS filesystem");
110
111#include <ufs/ufs/ufs_readwrite.c>
112
113/*
114 * Synch an open file.
115 */
116/* ARGSUSED */
117static int
118ffs_fsync(ap)
119 struct vop_fsync_args /* {
120 struct vnode *a_vp;
121 struct ucred *a_cred;
122 int a_waitfor;
123 struct proc *a_p;
124 } */ *ap;
125{
126 struct vnode *vp = ap->a_vp;
127 struct buf *bp;
128 struct timeval tv;
129 struct buf *nbp;
130 int s, error, passes, skipmeta;
131 daddr_t lbn;
132
133
134 if (vp->v_type == VBLK) {
135 lbn = INT_MAX;
136 } else {
137 struct inode *ip;
138 ip = VTOI(vp);
139 lbn = lblkno(ip->i_fs, (ip->i_size + ip->i_fs->fs_bsize - 1));
140 }
141
142 /*
143 * Flush all dirty buffers associated with a vnode.
144 */
145 passes = NIADDR;
146 skipmeta = 0;
147 if (ap->a_waitfor == MNT_WAIT)
148 skipmeta = 1;
149loop:
150 s = splbio();
151loop2:
152 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
153 nbp = bp->b_vnbufs.le_next;
154 /*
155 * First time through on a synchronous call,
156 * or if it's already scheduled, skip to the next
157 * buffer
158 */
159 if ((bp->b_flags & B_BUSY) ||
160 ((skipmeta == 1) && (bp->b_lblkno < 0)))
161 continue;
162 if ((bp->b_flags & B_DELWRI) == 0)
163 panic("ffs_fsync: not dirty");
164 /*
165 * If data is outstanding to another vnode, or we were
166 * asked to wait for everything, or it's not a file or BDEV,
167 * start the IO on this buffer immediatly.
168 */
169 if (((bp->b_vp != vp) || (ap->a_waitfor == MNT_WAIT)) ||
170 ((vp->v_type != VREG) && (vp->v_type != VBLK))) {
171
172 /*
173 * Wait for I/O associated with indirect blocks to
174 * complete, since there is no way to quickly wait
175 * for them below.
176 */
177 if ((bp->b_vp == vp) || (ap->a_waitfor != MNT_WAIT)) {
178 if (bp->b_flags & B_CLUSTEROK) {
179 (void) vfs_bio_awrite(bp);
180 splx(s);
181 } else {
182 bremfree(bp);
183 bp->b_flags |= B_BUSY;
184 splx(s);
185 (void) bawrite(bp);
186 }
187 } else {
188 bremfree(bp);
189 bp->b_flags |= B_BUSY;
190 splx(s);
191 (void) bwrite(bp);
192 }
193 } else if ((vp->v_type == VREG) && (bp->b_lblkno >= lbn)) {
194 /*
195 * If the buffer is for data that has been truncated
196 * off the file, then throw it away.
197 */
198 bremfree(bp);
199 bp->b_flags |= B_BUSY | B_INVAL | B_NOCACHE;
200 brelse(bp);
201 splx(s);
202 } else {
203 vfs_bio_awrite(bp);
204 splx(s);
205 }
206 goto loop;
207 }
208 /*
209 * If we were asked to do this synchronously, then go back for
210 * another pass, this time doing the metadata.
211 */
212 if (skipmeta) {
213 skipmeta = 0;
214 goto loop2; /* stay within the splbio() */
215 }
216 splx(s);
217
218 if (ap->a_waitfor == MNT_WAIT) {
219
220 s = splbio();
221 if (!DOINGSOFTDEP(vp)) {
222 while (vp->v_numoutput) {
223 vp->v_flag |= VBWAIT;
224 (void) tsleep((caddr_t)&vp->v_numoutput, PRIBIO + 4, "ffsfsn", 0);
225 }
226 } else {
227 /*
228 * Ensure that any filesystem metatdata associated
229 * with the vnode has been written.
230 */
231 if ((error = softdep_sync_metadata(ap)) != 0) {
232 splx(s);
233 return (error);
234 }
235 }
236
237 if (vp->v_dirtyblkhd.lh_first) {
238 /*
239 * Block devices associated with filesystems may
240 * have new I/O requests posted for them even if
241 * the vnode is locked, so no amount of trying will
242 * get them clean. Thus we give block devices a
243 * good effort, then just give up. For all other file
244 * types, go around and try again until it is clean.
245 */
246 if (passes > 0) {
247 passes -= 1;
248 goto loop2;
249 }
250#ifdef DIAGNOSTIC
251 if (vp->v_type != VBLK)
252 vprint("ffs_fsync: dirty", vp);
253#endif
254 }
255 }
256 getmicrotime(&tv);
257 error = UFS_UPDATE(ap->a_vp, &tv, &tv, (ap->a_waitfor == MNT_WAIT));
258 if (error)
259 return (error);
260 if (DOINGSOFTDEP(vp) && ap->a_waitfor == MNT_WAIT)
261 error = softdep_fsync(vp);
262 return (error);
263}