ffs_vnops.c revision 1.99 1 1.99 ad /* $NetBSD: ffs_vnops.c,v 1.99 2008/04/29 18:18:09 ad Exp $ */
2 1.3 cgd
3 1.1 mycroft /*
4 1.1 mycroft * Copyright (c) 1982, 1986, 1989, 1993
5 1.1 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 mycroft *
7 1.1 mycroft * Redistribution and use in source and binary forms, with or without
8 1.1 mycroft * modification, are permitted provided that the following conditions
9 1.1 mycroft * are met:
10 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
11 1.1 mycroft * notice, this list of conditions and the following disclaimer.
12 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
14 1.1 mycroft * documentation and/or other materials provided with the distribution.
15 1.60 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 mycroft * may be used to endorse or promote products derived from this software
17 1.1 mycroft * without specific prior written permission.
18 1.1 mycroft *
19 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 mycroft * SUCH DAMAGE.
30 1.1 mycroft *
31 1.12 fvdl * @(#)ffs_vnops.c 8.15 (Berkeley) 5/14/95
32 1.1 mycroft */
33 1.44 lukem
34 1.44 lukem #include <sys/cdefs.h>
35 1.99 ad __KERNEL_RCSID(0, "$NetBSD: ffs_vnops.c,v 1.99 2008/04/29 18:18:09 ad Exp $");
36 1.11 mrg
37 1.1 mycroft #include <sys/param.h>
38 1.1 mycroft #include <sys/systm.h>
39 1.1 mycroft #include <sys/resourcevar.h>
40 1.1 mycroft #include <sys/kernel.h>
41 1.1 mycroft #include <sys/file.h>
42 1.1 mycroft #include <sys/stat.h>
43 1.1 mycroft #include <sys/buf.h>
44 1.50 jdolecek #include <sys/event.h>
45 1.1 mycroft #include <sys/proc.h>
46 1.1 mycroft #include <sys/mount.h>
47 1.1 mycroft #include <sys/vnode.h>
48 1.16 thorpej #include <sys/pool.h>
49 1.6 christos #include <sys/signalvar.h>
50 1.80 elad #include <sys/kauth.h>
51 1.84 hannken #include <sys/fstrans.h>
52 1.1 mycroft
53 1.8 mycroft #include <miscfs/fifofs/fifo.h>
54 1.8 mycroft #include <miscfs/genfs/genfs.h>
55 1.1 mycroft #include <miscfs/specfs/specdev.h>
56 1.1 mycroft
57 1.1 mycroft #include <ufs/ufs/inode.h>
58 1.1 mycroft #include <ufs/ufs/dir.h>
59 1.1 mycroft #include <ufs/ufs/ufs_extern.h>
60 1.2 mycroft #include <ufs/ufs/ufsmount.h>
61 1.1 mycroft
62 1.1 mycroft #include <ufs/ffs/fs.h>
63 1.1 mycroft #include <ufs/ffs/ffs_extern.h>
64 1.1 mycroft
65 1.49 chs #include <uvm/uvm.h>
66 1.49 chs
67 1.1 mycroft /* Global vfs data structures for ufs. */
68 1.74 xtraeme int (**ffs_vnodeop_p)(void *);
69 1.37 jdolecek const struct vnodeopv_entry_desc ffs_vnodeop_entries[] = {
70 1.1 mycroft { &vop_default_desc, vn_default_error },
71 1.1 mycroft { &vop_lookup_desc, ufs_lookup }, /* lookup */
72 1.1 mycroft { &vop_create_desc, ufs_create }, /* create */
73 1.5 mycroft { &vop_whiteout_desc, ufs_whiteout }, /* whiteout */
74 1.1 mycroft { &vop_mknod_desc, ufs_mknod }, /* mknod */
75 1.1 mycroft { &vop_open_desc, ufs_open }, /* open */
76 1.1 mycroft { &vop_close_desc, ufs_close }, /* close */
77 1.1 mycroft { &vop_access_desc, ufs_access }, /* access */
78 1.1 mycroft { &vop_getattr_desc, ufs_getattr }, /* getattr */
79 1.1 mycroft { &vop_setattr_desc, ufs_setattr }, /* setattr */
80 1.1 mycroft { &vop_read_desc, ffs_read }, /* read */
81 1.1 mycroft { &vop_write_desc, ffs_write }, /* write */
82 1.1 mycroft { &vop_ioctl_desc, ufs_ioctl }, /* ioctl */
83 1.19 wrstuden { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
84 1.9 mycroft { &vop_poll_desc, ufs_poll }, /* poll */
85 1.50 jdolecek { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
86 1.12 fvdl { &vop_revoke_desc, ufs_revoke }, /* revoke */
87 1.1 mycroft { &vop_mmap_desc, ufs_mmap }, /* mmap */
88 1.1 mycroft { &vop_fsync_desc, ffs_fsync }, /* fsync */
89 1.1 mycroft { &vop_seek_desc, ufs_seek }, /* seek */
90 1.1 mycroft { &vop_remove_desc, ufs_remove }, /* remove */
91 1.1 mycroft { &vop_link_desc, ufs_link }, /* link */
92 1.1 mycroft { &vop_rename_desc, ufs_rename }, /* rename */
93 1.1 mycroft { &vop_mkdir_desc, ufs_mkdir }, /* mkdir */
94 1.1 mycroft { &vop_rmdir_desc, ufs_rmdir }, /* rmdir */
95 1.1 mycroft { &vop_symlink_desc, ufs_symlink }, /* symlink */
96 1.1 mycroft { &vop_readdir_desc, ufs_readdir }, /* readdir */
97 1.1 mycroft { &vop_readlink_desc, ufs_readlink }, /* readlink */
98 1.1 mycroft { &vop_abortop_desc, ufs_abortop }, /* abortop */
99 1.1 mycroft { &vop_inactive_desc, ufs_inactive }, /* inactive */
100 1.1 mycroft { &vop_reclaim_desc, ffs_reclaim }, /* reclaim */
101 1.90 hannken { &vop_lock_desc, ffs_lock }, /* lock */
102 1.90 hannken { &vop_unlock_desc, ffs_unlock }, /* unlock */
103 1.1 mycroft { &vop_bmap_desc, ufs_bmap }, /* bmap */
104 1.1 mycroft { &vop_strategy_desc, ufs_strategy }, /* strategy */
105 1.1 mycroft { &vop_print_desc, ufs_print }, /* print */
106 1.90 hannken { &vop_islocked_desc, ffs_islocked }, /* islocked */
107 1.1 mycroft { &vop_pathconf_desc, ufs_pathconf }, /* pathconf */
108 1.1 mycroft { &vop_advlock_desc, ufs_advlock }, /* advlock */
109 1.8 mycroft { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
110 1.88 yamt { &vop_getpages_desc, genfs_getpages }, /* getpages */
111 1.75 yamt { &vop_putpages_desc, genfs_putpages }, /* putpages */
112 1.73 thorpej { &vop_openextattr_desc, ffs_openextattr }, /* openextattr */
113 1.73 thorpej { &vop_closeextattr_desc, ffs_closeextattr }, /* closeextattr */
114 1.73 thorpej { &vop_getextattr_desc, ffs_getextattr }, /* getextattr */
115 1.73 thorpej { &vop_setextattr_desc, ffs_setextattr }, /* setextattr */
116 1.73 thorpej { &vop_listextattr_desc, ffs_listextattr }, /* listextattr */
117 1.73 thorpej { &vop_deleteextattr_desc, ffs_deleteextattr }, /* deleteextattr */
118 1.35 chs { NULL, NULL }
119 1.1 mycroft };
120 1.37 jdolecek const struct vnodeopv_desc ffs_vnodeop_opv_desc =
121 1.1 mycroft { &ffs_vnodeop_p, ffs_vnodeop_entries };
122 1.1 mycroft
123 1.74 xtraeme int (**ffs_specop_p)(void *);
124 1.37 jdolecek const struct vnodeopv_entry_desc ffs_specop_entries[] = {
125 1.1 mycroft { &vop_default_desc, vn_default_error },
126 1.1 mycroft { &vop_lookup_desc, spec_lookup }, /* lookup */
127 1.1 mycroft { &vop_create_desc, spec_create }, /* create */
128 1.1 mycroft { &vop_mknod_desc, spec_mknod }, /* mknod */
129 1.1 mycroft { &vop_open_desc, spec_open }, /* open */
130 1.1 mycroft { &vop_close_desc, ufsspec_close }, /* close */
131 1.1 mycroft { &vop_access_desc, ufs_access }, /* access */
132 1.1 mycroft { &vop_getattr_desc, ufs_getattr }, /* getattr */
133 1.1 mycroft { &vop_setattr_desc, ufs_setattr }, /* setattr */
134 1.1 mycroft { &vop_read_desc, ufsspec_read }, /* read */
135 1.1 mycroft { &vop_write_desc, ufsspec_write }, /* write */
136 1.1 mycroft { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
137 1.19 wrstuden { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
138 1.9 mycroft { &vop_poll_desc, spec_poll }, /* poll */
139 1.50 jdolecek { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
140 1.12 fvdl { &vop_revoke_desc, spec_revoke }, /* revoke */
141 1.1 mycroft { &vop_mmap_desc, spec_mmap }, /* mmap */
142 1.1 mycroft { &vop_fsync_desc, ffs_fsync }, /* fsync */
143 1.1 mycroft { &vop_seek_desc, spec_seek }, /* seek */
144 1.1 mycroft { &vop_remove_desc, spec_remove }, /* remove */
145 1.1 mycroft { &vop_link_desc, spec_link }, /* link */
146 1.1 mycroft { &vop_rename_desc, spec_rename }, /* rename */
147 1.1 mycroft { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
148 1.1 mycroft { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
149 1.1 mycroft { &vop_symlink_desc, spec_symlink }, /* symlink */
150 1.1 mycroft { &vop_readdir_desc, spec_readdir }, /* readdir */
151 1.1 mycroft { &vop_readlink_desc, spec_readlink }, /* readlink */
152 1.1 mycroft { &vop_abortop_desc, spec_abortop }, /* abortop */
153 1.1 mycroft { &vop_inactive_desc, ufs_inactive }, /* inactive */
154 1.1 mycroft { &vop_reclaim_desc, ffs_reclaim }, /* reclaim */
155 1.90 hannken { &vop_lock_desc, ffs_lock }, /* lock */
156 1.90 hannken { &vop_unlock_desc, ffs_unlock }, /* unlock */
157 1.1 mycroft { &vop_bmap_desc, spec_bmap }, /* bmap */
158 1.1 mycroft { &vop_strategy_desc, spec_strategy }, /* strategy */
159 1.1 mycroft { &vop_print_desc, ufs_print }, /* print */
160 1.90 hannken { &vop_islocked_desc, ffs_islocked }, /* islocked */
161 1.1 mycroft { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
162 1.1 mycroft { &vop_advlock_desc, spec_advlock }, /* advlock */
163 1.8 mycroft { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
164 1.38 chs { &vop_getpages_desc, spec_getpages }, /* getpages */
165 1.38 chs { &vop_putpages_desc, spec_putpages }, /* putpages */
166 1.73 thorpej { &vop_openextattr_desc, ffs_openextattr }, /* openextattr */
167 1.73 thorpej { &vop_closeextattr_desc, ffs_closeextattr }, /* closeextattr */
168 1.73 thorpej { &vop_getextattr_desc, ffs_getextattr }, /* getextattr */
169 1.73 thorpej { &vop_setextattr_desc, ffs_setextattr }, /* setextattr */
170 1.73 thorpej { &vop_listextattr_desc, ffs_listextattr }, /* listextattr */
171 1.73 thorpej { &vop_deleteextattr_desc, ffs_deleteextattr }, /* deleteextattr */
172 1.35 chs { NULL, NULL }
173 1.1 mycroft };
174 1.37 jdolecek const struct vnodeopv_desc ffs_specop_opv_desc =
175 1.1 mycroft { &ffs_specop_p, ffs_specop_entries };
176 1.1 mycroft
177 1.74 xtraeme int (**ffs_fifoop_p)(void *);
178 1.37 jdolecek const struct vnodeopv_entry_desc ffs_fifoop_entries[] = {
179 1.1 mycroft { &vop_default_desc, vn_default_error },
180 1.1 mycroft { &vop_lookup_desc, fifo_lookup }, /* lookup */
181 1.1 mycroft { &vop_create_desc, fifo_create }, /* create */
182 1.1 mycroft { &vop_mknod_desc, fifo_mknod }, /* mknod */
183 1.1 mycroft { &vop_open_desc, fifo_open }, /* open */
184 1.1 mycroft { &vop_close_desc, ufsfifo_close }, /* close */
185 1.1 mycroft { &vop_access_desc, ufs_access }, /* access */
186 1.1 mycroft { &vop_getattr_desc, ufs_getattr }, /* getattr */
187 1.1 mycroft { &vop_setattr_desc, ufs_setattr }, /* setattr */
188 1.1 mycroft { &vop_read_desc, ufsfifo_read }, /* read */
189 1.1 mycroft { &vop_write_desc, ufsfifo_write }, /* write */
190 1.1 mycroft { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
191 1.19 wrstuden { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
192 1.9 mycroft { &vop_poll_desc, fifo_poll }, /* poll */
193 1.50 jdolecek { &vop_kqfilter_desc, fifo_kqfilter }, /* kqfilter */
194 1.12 fvdl { &vop_revoke_desc, fifo_revoke }, /* revoke */
195 1.1 mycroft { &vop_mmap_desc, fifo_mmap }, /* mmap */
196 1.1 mycroft { &vop_fsync_desc, ffs_fsync }, /* fsync */
197 1.1 mycroft { &vop_seek_desc, fifo_seek }, /* seek */
198 1.1 mycroft { &vop_remove_desc, fifo_remove }, /* remove */
199 1.1 mycroft { &vop_link_desc, fifo_link }, /* link */
200 1.1 mycroft { &vop_rename_desc, fifo_rename }, /* rename */
201 1.1 mycroft { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
202 1.1 mycroft { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
203 1.1 mycroft { &vop_symlink_desc, fifo_symlink }, /* symlink */
204 1.1 mycroft { &vop_readdir_desc, fifo_readdir }, /* readdir */
205 1.1 mycroft { &vop_readlink_desc, fifo_readlink }, /* readlink */
206 1.1 mycroft { &vop_abortop_desc, fifo_abortop }, /* abortop */
207 1.1 mycroft { &vop_inactive_desc, ufs_inactive }, /* inactive */
208 1.1 mycroft { &vop_reclaim_desc, ffs_reclaim }, /* reclaim */
209 1.90 hannken { &vop_lock_desc, ffs_lock }, /* lock */
210 1.90 hannken { &vop_unlock_desc, ffs_unlock }, /* unlock */
211 1.1 mycroft { &vop_bmap_desc, fifo_bmap }, /* bmap */
212 1.1 mycroft { &vop_strategy_desc, fifo_strategy }, /* strategy */
213 1.1 mycroft { &vop_print_desc, ufs_print }, /* print */
214 1.90 hannken { &vop_islocked_desc, ffs_islocked }, /* islocked */
215 1.1 mycroft { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
216 1.1 mycroft { &vop_advlock_desc, fifo_advlock }, /* advlock */
217 1.8 mycroft { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
218 1.40 sommerfe { &vop_putpages_desc, fifo_putpages }, /* putpages */
219 1.73 thorpej { &vop_openextattr_desc, ffs_openextattr }, /* openextattr */
220 1.73 thorpej { &vop_closeextattr_desc, ffs_closeextattr }, /* closeextattr */
221 1.73 thorpej { &vop_getextattr_desc, ffs_getextattr }, /* getextattr */
222 1.73 thorpej { &vop_setextattr_desc, ffs_setextattr }, /* setextattr */
223 1.73 thorpej { &vop_listextattr_desc, ffs_listextattr }, /* listextattr */
224 1.73 thorpej { &vop_deleteextattr_desc, ffs_deleteextattr }, /* deleteextattr */
225 1.35 chs { NULL, NULL }
226 1.1 mycroft };
227 1.37 jdolecek const struct vnodeopv_desc ffs_fifoop_opv_desc =
228 1.1 mycroft { &ffs_fifoop_p, ffs_fifoop_entries };
229 1.1 mycroft
230 1.1 mycroft #include <ufs/ufs/ufs_readwrite.c>
231 1.20 fvdl
232 1.33 fvdl int
233 1.70 thorpej ffs_fsync(void *v)
234 1.33 fvdl {
235 1.33 fvdl struct vop_fsync_args /* {
236 1.33 fvdl struct vnode *a_vp;
237 1.80 elad kauth_cred_t a_cred;
238 1.33 fvdl int a_flags;
239 1.46 chs off_t a_offlo;
240 1.46 chs off_t a_offhi;
241 1.77 christos struct lwp *a_l;
242 1.33 fvdl } */ *ap = v;
243 1.35 chs struct buf *bp;
244 1.94 ad int num, error, i;
245 1.33 fvdl struct indir ia[NIADDR + 1];
246 1.33 fvdl int bsize;
247 1.45 simonb daddr_t blk_high;
248 1.33 fvdl struct vnode *vp;
249 1.33 fvdl
250 1.84 hannken vp = ap->a_vp;
251 1.84 hannken
252 1.87 hannken fstrans_start(vp->v_mount, FSTRANS_LAZY);
253 1.33 fvdl /*
254 1.33 fvdl * XXX no easy way to sync a range in a file with softdep.
255 1.33 fvdl */
256 1.84 hannken if ((ap->a_offlo == 0 && ap->a_offhi == 0) || DOINGSOFTDEP(vp) ||
257 1.84 hannken (vp->v_type != VREG)) {
258 1.94 ad error = ffs_full_fsync(vp, ap->a_flags);
259 1.84 hannken goto out;
260 1.84 hannken }
261 1.33 fvdl
262 1.84 hannken bsize = vp->v_mount->mnt_stat.f_iosize;
263 1.33 fvdl blk_high = ap->a_offhi / bsize;
264 1.33 fvdl if (ap->a_offhi % bsize != 0)
265 1.33 fvdl blk_high++;
266 1.33 fvdl
267 1.33 fvdl /*
268 1.35 chs * First, flush all pages in range.
269 1.33 fvdl */
270 1.35 chs
271 1.94 ad mutex_enter(&vp->v_interlock);
272 1.64 dbj error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
273 1.64 dbj round_page(ap->a_offhi), PGO_CLEANIT |
274 1.64 dbj ((ap->a_flags & FSYNC_WAIT) ? PGO_SYNCIO : 0));
275 1.64 dbj if (error) {
276 1.84 hannken goto out;
277 1.39 chs }
278 1.33 fvdl
279 1.33 fvdl /*
280 1.35 chs * Then, flush indirect blocks.
281 1.33 fvdl */
282 1.35 chs
283 1.61 kleink if (blk_high >= NDADDR) {
284 1.33 fvdl error = ufs_getlbns(vp, blk_high, ia, &num);
285 1.94 ad if (error)
286 1.84 hannken goto out;
287 1.94 ad
288 1.94 ad mutex_enter(&bufcache_lock);
289 1.33 fvdl for (i = 0; i < num; i++) {
290 1.94 ad if ((bp = incore(vp, ia[i].in_lbn)) == NULL)
291 1.94 ad continue;
292 1.94 ad if ((bp->b_cflags & BC_BUSY) != 0 ||
293 1.94 ad (bp->b_oflags & BO_DELWRI) == 0)
294 1.94 ad continue;
295 1.94 ad bp->b_cflags |= BC_BUSY | BC_VFLUSH;
296 1.94 ad mutex_exit(&bufcache_lock);
297 1.94 ad bawrite(bp);
298 1.94 ad mutex_enter(&bufcache_lock);
299 1.33 fvdl }
300 1.94 ad mutex_exit(&bufcache_lock);
301 1.33 fvdl }
302 1.33 fvdl
303 1.33 fvdl if (ap->a_flags & FSYNC_WAIT) {
304 1.94 ad mutex_enter(&vp->v_interlock);
305 1.94 ad while (vp->v_numoutput > 0)
306 1.94 ad cv_wait(&vp->v_cv, &vp->v_interlock);
307 1.94 ad mutex_exit(&vp->v_interlock);
308 1.33 fvdl }
309 1.33 fvdl
310 1.76 yamt error = ffs_update(vp, NULL, NULL,
311 1.66 thorpej ((ap->a_flags & (FSYNC_WAIT | FSYNC_DATAONLY)) == FSYNC_WAIT)
312 1.67 wrstuden ? UPDATE_WAIT : 0);
313 1.67 wrstuden
314 1.67 wrstuden if (error == 0 && ap->a_flags & FSYNC_CACHE) {
315 1.67 wrstuden int l = 0;
316 1.67 wrstuden VOP_IOCTL(VTOI(vp)->i_devvp, DIOCCACHESYNC, &l, FWRITE,
317 1.93 pooka curlwp->l_cred);
318 1.67 wrstuden }
319 1.67 wrstuden
320 1.84 hannken out:
321 1.84 hannken fstrans_done(vp->v_mount);
322 1.67 wrstuden return error;
323 1.33 fvdl }
324 1.33 fvdl
325 1.20 fvdl /*
326 1.20 fvdl * Synch an open file.
327 1.20 fvdl */
328 1.20 fvdl /* ARGSUSED */
329 1.94 ad int
330 1.94 ad ffs_full_fsync(struct vnode *vp, int flags)
331 1.20 fvdl {
332 1.20 fvdl struct buf *bp, *nbp;
333 1.94 ad int error, passes, skipmeta, inodedeps_only, waitfor;
334 1.99 ad struct mount *mp;
335 1.99 ad
336 1.99 ad error = 0;
337 1.24 fvdl
338 1.20 fvdl if (vp->v_type == VBLK &&
339 1.20 fvdl vp->v_specmountpoint != NULL &&
340 1.20 fvdl (vp->v_specmountpoint->mnt_flag & MNT_SOFTDEP))
341 1.20 fvdl softdep_fsync_mountdev(vp);
342 1.21 perseant
343 1.94 ad mutex_enter(&vp->v_interlock);
344 1.94 ad
345 1.94 ad inodedeps_only = DOINGSOFTDEP(vp) && (flags & FSYNC_RECLAIM)
346 1.89 pooka && UVM_OBJ_IS_CLEAN(&vp->v_uobj) && LIST_EMPTY(&vp->v_dirtyblkhd);
347 1.47 fvdl
348 1.35 chs /*
349 1.35 chs * Flush all dirty data associated with a vnode.
350 1.20 fvdl */
351 1.35 chs
352 1.72 yamt if (vp->v_type == VREG || vp->v_type == VBLK) {
353 1.99 ad if ((flags & FSYNC_VFS) != 0)
354 1.99 ad mp = vp->v_specmountpoint;
355 1.99 ad else
356 1.99 ad mp = vp->v_mount;
357 1.46 chs error = VOP_PUTPAGES(vp, 0, 0, PGO_ALLPAGES | PGO_CLEANIT |
358 1.94 ad ((flags & FSYNC_WAIT) ? PGO_SYNCIO : 0) |
359 1.99 ad (fstrans_getstate(mp) == FSTRANS_SUSPENDING ?
360 1.84 hannken PGO_FREE : 0));
361 1.99 ad if (error)
362 1.39 chs return error;
363 1.94 ad } else
364 1.94 ad mutex_exit(&vp->v_interlock);
365 1.35 chs
366 1.20 fvdl passes = NIADDR + 1;
367 1.20 fvdl skipmeta = 0;
368 1.94 ad if (flags & FSYNC_WAIT)
369 1.20 fvdl skipmeta = 1;
370 1.35 chs
371 1.20 fvdl loop:
372 1.94 ad mutex_enter(&bufcache_lock);
373 1.94 ad LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
374 1.94 ad bp->b_cflags &= ~BC_SCANNED;
375 1.94 ad }
376 1.29 mycroft for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
377 1.29 mycroft nbp = LIST_NEXT(bp, b_vnbufs);
378 1.94 ad if (bp->b_cflags & (BC_BUSY | BC_SCANNED))
379 1.20 fvdl continue;
380 1.94 ad if ((bp->b_oflags & BO_DELWRI) == 0)
381 1.20 fvdl panic("ffs_fsync: not dirty");
382 1.94 ad if (skipmeta && bp->b_lblkno < 0)
383 1.20 fvdl continue;
384 1.94 ad bp->b_cflags |= BC_BUSY | BC_VFLUSH | BC_SCANNED;
385 1.94 ad mutex_exit(&bufcache_lock);
386 1.20 fvdl /*
387 1.20 fvdl * On our final pass through, do all I/O synchronously
388 1.20 fvdl * so that we can find out if our flush is failing
389 1.20 fvdl * because of write errors.
390 1.20 fvdl */
391 1.94 ad if (passes > 0 || !(flags & FSYNC_WAIT))
392 1.20 fvdl (void) bawrite(bp);
393 1.20 fvdl else if ((error = bwrite(bp)) != 0)
394 1.20 fvdl return (error);
395 1.20 fvdl /*
396 1.94 ad * Since we unlocked during the I/O, we need
397 1.20 fvdl * to start from a known point.
398 1.20 fvdl */
399 1.94 ad mutex_enter(&bufcache_lock);
400 1.29 mycroft nbp = LIST_FIRST(&vp->v_dirtyblkhd);
401 1.20 fvdl }
402 1.94 ad mutex_exit(&bufcache_lock);
403 1.61 kleink if (skipmeta) {
404 1.20 fvdl skipmeta = 0;
405 1.20 fvdl goto loop;
406 1.20 fvdl }
407 1.94 ad
408 1.94 ad if (flags & FSYNC_WAIT) {
409 1.94 ad mutex_enter(&vp->v_interlock);
410 1.20 fvdl while (vp->v_numoutput) {
411 1.94 ad cv_wait(&vp->v_cv, &vp->v_interlock);
412 1.20 fvdl }
413 1.94 ad mutex_exit(&vp->v_interlock);
414 1.20 fvdl
415 1.69 perry /*
416 1.20 fvdl * Ensure that any filesystem metadata associated
417 1.20 fvdl * with the vnode has been written.
418 1.20 fvdl */
419 1.94 ad if ((error = softdep_sync_metadata(vp)) != 0)
420 1.20 fvdl return (error);
421 1.29 mycroft
422 1.29 mycroft if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
423 1.29 mycroft /*
424 1.29 mycroft * Block devices associated with filesystems may
425 1.29 mycroft * have new I/O requests posted for them even if
426 1.29 mycroft * the vnode is locked, so no amount of trying will
427 1.29 mycroft * get them clean. Thus we give block devices a
428 1.29 mycroft * good effort, then just give up. For all other file
429 1.29 mycroft * types, go around and try again until it is clean.
430 1.29 mycroft */
431 1.29 mycroft if (passes > 0) {
432 1.29 mycroft passes--;
433 1.29 mycroft goto loop;
434 1.29 mycroft }
435 1.20 fvdl #ifdef DIAGNOSTIC
436 1.29 mycroft if (vp->v_type != VBLK)
437 1.29 mycroft vprint("ffs_fsync: dirty", vp);
438 1.20 fvdl #endif
439 1.29 mycroft }
440 1.29 mycroft }
441 1.47 fvdl
442 1.47 fvdl if (inodedeps_only)
443 1.47 fvdl waitfor = 0;
444 1.47 fvdl else
445 1.94 ad waitfor = (flags & FSYNC_WAIT) ? UPDATE_WAIT : 0;
446 1.99 ad
447 1.99 ad if (vp->v_tag == VT_UFS)
448 1.99 ad error = ffs_update(vp, NULL, NULL, waitfor);
449 1.99 ad else {
450 1.99 ad KASSERT(vp->v_type == VBLK);
451 1.99 ad KASSERT((flags & FSYNC_VFS) != 0);
452 1.99 ad }
453 1.67 wrstuden
454 1.94 ad if (error == 0 && flags & FSYNC_CACHE) {
455 1.67 wrstuden int i = 0;
456 1.99 ad if ((flags & FSYNC_VFS) == 0)
457 1.99 ad vp = VTOI(vp)->i_devvp;
458 1.99 ad VOP_IOCTL(vp, DIOCCACHESYNC, &i, FWRITE, curlwp->l_cred);
459 1.67 wrstuden }
460 1.67 wrstuden
461 1.67 wrstuden return error;
462 1.20 fvdl }
463 1.1 mycroft
464 1.1 mycroft /*
465 1.1 mycroft * Reclaim an inode so that it can be used for other purposes.
466 1.1 mycroft */
467 1.1 mycroft int
468 1.70 thorpej ffs_reclaim(void *v)
469 1.6 christos {
470 1.1 mycroft struct vop_reclaim_args /* {
471 1.1 mycroft struct vnode *a_vp;
472 1.77 christos struct lwp *a_l;
473 1.6 christos } */ *ap = v;
474 1.26 augustss struct vnode *vp = ap->a_vp;
475 1.56 fvdl struct inode *ip = VTOI(vp);
476 1.84 hannken struct mount *mp = vp->v_mount;
477 1.56 fvdl struct ufsmount *ump = ip->i_ump;
478 1.94 ad void *data;
479 1.1 mycroft int error;
480 1.1 mycroft
481 1.87 hannken fstrans_start(mp, FSTRANS_LAZY);
482 1.93 pooka if ((error = ufs_reclaim(vp)) != 0) {
483 1.84 hannken fstrans_done(mp);
484 1.1 mycroft return (error);
485 1.84 hannken }
486 1.57 fvdl if (ip->i_din.ffs1_din != NULL) {
487 1.57 fvdl if (ump->um_fstype == UFS1)
488 1.95 ad pool_cache_put(ffs_dinode1_cache, ip->i_din.ffs1_din);
489 1.57 fvdl else
490 1.95 ad pool_cache_put(ffs_dinode2_cache, ip->i_din.ffs2_din);
491 1.57 fvdl }
492 1.16 thorpej /*
493 1.94 ad * To interlock with ffs_sync().
494 1.94 ad */
495 1.94 ad genfs_node_destroy(vp);
496 1.94 ad mutex_enter(&vp->v_interlock);
497 1.94 ad data = vp->v_data;
498 1.94 ad vp->v_data = NULL;
499 1.94 ad mutex_exit(&vp->v_interlock);
500 1.94 ad
501 1.94 ad /*
502 1.16 thorpej * XXX MFS ends up here, too, to free an inode. Should we create
503 1.16 thorpej * XXX a separate pool for MFS inodes?
504 1.16 thorpej */
505 1.95 ad pool_cache_put(ffs_inode_cache, data);
506 1.84 hannken fstrans_done(mp);
507 1.1 mycroft return (0);
508 1.35 chs }
509 1.35 chs
510 1.88 yamt #if 0
511 1.39 chs int
512 1.39 chs ffs_getpages(void *v)
513 1.39 chs {
514 1.39 chs struct vop_getpages_args /* {
515 1.39 chs struct vnode *a_vp;
516 1.39 chs voff_t a_offset;
517 1.39 chs struct vm_page **a_m;
518 1.39 chs int *a_count;
519 1.39 chs int a_centeridx;
520 1.39 chs vm_prot_t a_access_type;
521 1.39 chs int a_advice;
522 1.39 chs int a_flags;
523 1.39 chs } */ *ap = v;
524 1.39 chs struct vnode *vp = ap->a_vp;
525 1.39 chs struct inode *ip = VTOI(vp);
526 1.39 chs struct fs *fs = ip->i_fs;
527 1.39 chs
528 1.39 chs /*
529 1.39 chs * don't allow a softdep write to create pages for only part of a block.
530 1.39 chs * the dependency tracking requires that all pages be in memory for
531 1.39 chs * a block involved in a dependency.
532 1.39 chs */
533 1.39 chs
534 1.39 chs if (ap->a_flags & PGO_OVERWRITE &&
535 1.39 chs (blkoff(fs, ap->a_offset) != 0 ||
536 1.39 chs blkoff(fs, *ap->a_count << PAGE_SHIFT) != 0) &&
537 1.39 chs DOINGSOFTDEP(ap->a_vp)) {
538 1.39 chs if ((ap->a_flags & PGO_LOCKED) == 0) {
539 1.94 ad mutex_exit(&vp->v_interlock);
540 1.39 chs }
541 1.39 chs return EINVAL;
542 1.39 chs }
543 1.39 chs return genfs_getpages(v);
544 1.49 chs }
545 1.88 yamt #endif
546 1.49 chs
547 1.35 chs /*
548 1.35 chs * Return the last logical file offset that should be written for this file
549 1.35 chs * if we're doing a write that ends at "size".
550 1.35 chs */
551 1.39 chs
552 1.39 chs void
553 1.83 christos ffs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
554 1.35 chs {
555 1.39 chs struct inode *ip = VTOI(vp);
556 1.35 chs struct fs *fs = ip->i_fs;
557 1.52 fvdl daddr_t olbn, nlbn;
558 1.55 perseant
559 1.56 fvdl olbn = lblkno(fs, ip->i_size);
560 1.39 chs nlbn = lblkno(fs, size);
561 1.35 chs if (nlbn < NDADDR && olbn <= nlbn) {
562 1.39 chs *eobp = fragroundup(fs, size);
563 1.35 chs } else {
564 1.39 chs *eobp = blkroundup(fs, size);
565 1.35 chs }
566 1.1 mycroft }
567 1.73 thorpej
568 1.73 thorpej int
569 1.73 thorpej ffs_openextattr(void *v)
570 1.73 thorpej {
571 1.73 thorpej struct vop_openextattr_args /* {
572 1.73 thorpej struct vnode *a_vp;
573 1.80 elad kauth_cred_t a_cred;
574 1.73 thorpej struct proc *a_p;
575 1.73 thorpej } */ *ap = v;
576 1.73 thorpej struct inode *ip = VTOI(ap->a_vp);
577 1.73 thorpej struct fs *fs = ip->i_fs;
578 1.73 thorpej
579 1.73 thorpej /* Not supported for UFS1 file systems. */
580 1.73 thorpej if (fs->fs_magic == FS_UFS1_MAGIC)
581 1.73 thorpej return (EOPNOTSUPP);
582 1.73 thorpej
583 1.73 thorpej /* XXX Not implemented for UFS2 file systems. */
584 1.73 thorpej return (EOPNOTSUPP);
585 1.73 thorpej }
586 1.73 thorpej
587 1.73 thorpej int
588 1.73 thorpej ffs_closeextattr(void *v)
589 1.73 thorpej {
590 1.73 thorpej struct vop_closeextattr_args /* {
591 1.73 thorpej struct vnode *a_vp;
592 1.73 thorpej int a_commit;
593 1.80 elad kauth_cred_t a_cred;
594 1.73 thorpej struct proc *a_p;
595 1.73 thorpej } */ *ap = v;
596 1.73 thorpej struct inode *ip = VTOI(ap->a_vp);
597 1.73 thorpej struct fs *fs = ip->i_fs;
598 1.73 thorpej
599 1.73 thorpej /* Not supported for UFS1 file systems. */
600 1.73 thorpej if (fs->fs_magic == FS_UFS1_MAGIC)
601 1.73 thorpej return (EOPNOTSUPP);
602 1.73 thorpej
603 1.73 thorpej /* XXX Not implemented for UFS2 file systems. */
604 1.73 thorpej return (EOPNOTSUPP);
605 1.73 thorpej }
606 1.73 thorpej
607 1.73 thorpej int
608 1.73 thorpej ffs_getextattr(void *v)
609 1.73 thorpej {
610 1.73 thorpej struct vop_getextattr_args /* {
611 1.73 thorpej struct vnode *a_vp;
612 1.73 thorpej int a_attrnamespace;
613 1.73 thorpej const char *a_name;
614 1.73 thorpej struct uio *a_uio;
615 1.73 thorpej size_t *a_size;
616 1.80 elad kauth_cred_t a_cred;
617 1.73 thorpej struct proc *a_p;
618 1.73 thorpej } */ *ap = v;
619 1.84 hannken struct vnode *vp = ap->a_vp;
620 1.84 hannken struct inode *ip = VTOI(vp);
621 1.73 thorpej struct fs *fs = ip->i_fs;
622 1.73 thorpej
623 1.73 thorpej if (fs->fs_magic == FS_UFS1_MAGIC) {
624 1.73 thorpej #ifdef UFS_EXTATTR
625 1.84 hannken int error;
626 1.84 hannken
627 1.87 hannken fstrans_start(vp->v_mount, FSTRANS_SHARED);
628 1.84 hannken error = ufs_getextattr(ap);
629 1.84 hannken fstrans_done(vp->v_mount);
630 1.84 hannken return error;
631 1.73 thorpej #else
632 1.73 thorpej return (EOPNOTSUPP);
633 1.73 thorpej #endif
634 1.73 thorpej }
635 1.73 thorpej
636 1.73 thorpej /* XXX Not implemented for UFS2 file systems. */
637 1.73 thorpej return (EOPNOTSUPP);
638 1.73 thorpej }
639 1.73 thorpej
640 1.73 thorpej int
641 1.73 thorpej ffs_setextattr(void *v)
642 1.73 thorpej {
643 1.73 thorpej struct vop_setextattr_args /* {
644 1.73 thorpej struct vnode *a_vp;
645 1.73 thorpej int a_attrnamespace;
646 1.73 thorpej const char *a_name;
647 1.73 thorpej struct uio *a_uio;
648 1.80 elad kauth_cred_t a_cred;
649 1.73 thorpej struct proc *a_p;
650 1.73 thorpej } */ *ap = v;
651 1.84 hannken struct vnode *vp = ap->a_vp;
652 1.84 hannken struct inode *ip = VTOI(vp);
653 1.73 thorpej struct fs *fs = ip->i_fs;
654 1.73 thorpej
655 1.73 thorpej if (fs->fs_magic == FS_UFS1_MAGIC) {
656 1.73 thorpej #ifdef UFS_EXTATTR
657 1.84 hannken int error;
658 1.84 hannken
659 1.87 hannken fstrans_start(vp->v_mount, FSTRANS_SHARED);
660 1.84 hannken error = ufs_setextattr(ap);
661 1.84 hannken fstrans_done(vp->v_mount);
662 1.84 hannken return error;
663 1.73 thorpej #else
664 1.73 thorpej return (EOPNOTSUPP);
665 1.73 thorpej #endif
666 1.73 thorpej }
667 1.73 thorpej
668 1.73 thorpej /* XXX Not implemented for UFS2 file systems. */
669 1.73 thorpej return (EOPNOTSUPP);
670 1.73 thorpej }
671 1.73 thorpej
672 1.73 thorpej int
673 1.73 thorpej ffs_listextattr(void *v)
674 1.73 thorpej {
675 1.73 thorpej struct vop_listextattr_args /* {
676 1.73 thorpej struct vnode *a_vp;
677 1.73 thorpej int a_attrnamespace;
678 1.73 thorpej struct uio *a_uio;
679 1.73 thorpej size_t *a_size;
680 1.80 elad kauth_cred_t a_cred;
681 1.73 thorpej struct proc *a_p;
682 1.73 thorpej } */ *ap = v;
683 1.73 thorpej struct inode *ip = VTOI(ap->a_vp);
684 1.73 thorpej struct fs *fs = ip->i_fs;
685 1.73 thorpej
686 1.73 thorpej /* Not supported for UFS1 file systems. */
687 1.73 thorpej if (fs->fs_magic == FS_UFS1_MAGIC)
688 1.73 thorpej return (EOPNOTSUPP);
689 1.73 thorpej
690 1.73 thorpej /* XXX Not implemented for UFS2 file systems. */
691 1.73 thorpej return (EOPNOTSUPP);
692 1.73 thorpej }
693 1.73 thorpej
694 1.73 thorpej int
695 1.73 thorpej ffs_deleteextattr(void *v)
696 1.73 thorpej {
697 1.73 thorpej struct vop_deleteextattr_args /* {
698 1.73 thorpej struct vnode *a_vp;
699 1.73 thorpej int a_attrnamespace;
700 1.80 elad kauth_cred_t a_cred;
701 1.73 thorpej struct proc *a_p;
702 1.73 thorpej } */ *ap = v;
703 1.84 hannken struct vnode *vp = ap->a_vp;
704 1.84 hannken struct inode *ip = VTOI(vp);
705 1.73 thorpej struct fs *fs = ip->i_fs;
706 1.73 thorpej
707 1.73 thorpej if (fs->fs_magic == FS_UFS1_MAGIC) {
708 1.73 thorpej #ifdef UFS_EXTATTR
709 1.84 hannken int error;
710 1.84 hannken
711 1.87 hannken fstrans_start(vp->v_mount, FSTRANS_SHARED);
712 1.84 hannken error = ufs_deleteextattr(ap);
713 1.84 hannken fstrans_done(vp->v_mount);
714 1.84 hannken return error;
715 1.73 thorpej #else
716 1.73 thorpej return (EOPNOTSUPP);
717 1.73 thorpej #endif
718 1.73 thorpej }
719 1.73 thorpej
720 1.73 thorpej /* XXX Not implemented for UFS2 file systems. */
721 1.73 thorpej return (EOPNOTSUPP);
722 1.73 thorpej }
723 1.90 hannken
724 1.90 hannken /*
725 1.90 hannken * Lock the node.
726 1.90 hannken */
727 1.90 hannken int
728 1.90 hannken ffs_lock(void *v)
729 1.90 hannken {
730 1.90 hannken struct vop_lock_args /* {
731 1.90 hannken struct vnode *a_vp;
732 1.90 hannken int a_flags;
733 1.90 hannken } */ *ap = v;
734 1.90 hannken struct vnode *vp = ap->a_vp;
735 1.90 hannken struct mount *mp = vp->v_mount;
736 1.98 ad struct vnlock *lkp;
737 1.91 hannken int flags = ap->a_flags;
738 1.91 hannken int result;
739 1.90 hannken
740 1.98 ad if ((flags & LK_INTERLOCK) != 0) {
741 1.98 ad mutex_exit(&vp->v_interlock);
742 1.98 ad flags &= ~LK_INTERLOCK;
743 1.98 ad }
744 1.98 ad
745 1.90 hannken /*
746 1.90 hannken * Fake lock during file system suspension.
747 1.90 hannken */
748 1.90 hannken if ((vp->v_type == VREG || vp->v_type == VDIR) &&
749 1.90 hannken fstrans_is_owner(mp) &&
750 1.90 hannken fstrans_getstate(mp) == FSTRANS_SUSPENDING) {
751 1.90 hannken return 0;
752 1.90 hannken }
753 1.91 hannken
754 1.91 hannken for (;;) {
755 1.91 hannken lkp = vp->v_vnlock;
756 1.98 ad result = vlockmgr(lkp, flags);
757 1.91 hannken if (lkp == vp->v_vnlock || result != 0)
758 1.91 hannken return result;
759 1.91 hannken /*
760 1.91 hannken * Apparent success, except that the vnode mutated between
761 1.91 hannken * snapshot file vnode and regular file vnode while this
762 1.91 hannken * thread slept. The lock currently held is not the right
763 1.91 hannken * lock. Release it, and try to get the new lock.
764 1.91 hannken */
765 1.98 ad (void) vlockmgr(lkp, LK_RELEASE);
766 1.91 hannken }
767 1.90 hannken }
768 1.90 hannken
769 1.90 hannken /*
770 1.90 hannken * Unlock the node.
771 1.90 hannken */
772 1.90 hannken int
773 1.90 hannken ffs_unlock(void *v)
774 1.90 hannken {
775 1.90 hannken struct vop_unlock_args /* {
776 1.90 hannken struct vnode *a_vp;
777 1.90 hannken int a_flags;
778 1.90 hannken } */ *ap = v;
779 1.90 hannken struct vnode *vp = ap->a_vp;
780 1.90 hannken struct mount *mp = vp->v_mount;
781 1.90 hannken
782 1.98 ad KASSERT(ap->a_flags == 0);
783 1.98 ad
784 1.90 hannken /*
785 1.90 hannken * Fake unlock during file system suspension.
786 1.90 hannken */
787 1.90 hannken if ((vp->v_type == VREG || vp->v_type == VDIR) &&
788 1.90 hannken fstrans_is_owner(mp) &&
789 1.90 hannken fstrans_getstate(mp) == FSTRANS_SUSPENDING) {
790 1.90 hannken return 0;
791 1.90 hannken }
792 1.98 ad return (vlockmgr(vp->v_vnlock, LK_RELEASE));
793 1.90 hannken }
794 1.90 hannken
795 1.90 hannken /*
796 1.90 hannken * Return whether or not the node is locked.
797 1.90 hannken */
798 1.90 hannken int
799 1.90 hannken ffs_islocked(void *v)
800 1.90 hannken {
801 1.90 hannken struct vop_islocked_args /* {
802 1.90 hannken struct vnode *a_vp;
803 1.90 hannken } */ *ap = v;
804 1.90 hannken struct vnode *vp = ap->a_vp;
805 1.90 hannken
806 1.98 ad return (vlockstatus(vp->v_vnlock));
807 1.90 hannken }
808