lfs_vnops.c revision 1.93 1 /* $NetBSD: lfs_vnops.c,v 1.93 2003/03/04 19:19:43 perseant Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*
39 * Copyright (c) 1986, 1989, 1991, 1993, 1995
40 * The Regents of the University of California. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)lfs_vnops.c 8.13 (Berkeley) 6/10/95
71 */
72
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: lfs_vnops.c,v 1.93 2003/03/04 19:19:43 perseant Exp $");
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/namei.h>
79 #include <sys/resourcevar.h>
80 #include <sys/kernel.h>
81 #include <sys/file.h>
82 #include <sys/stat.h>
83 #include <sys/buf.h>
84 #include <sys/proc.h>
85 #include <sys/mount.h>
86 #include <sys/vnode.h>
87 #include <sys/malloc.h>
88 #include <sys/pool.h>
89 #include <sys/signalvar.h>
90
91 #include <miscfs/fifofs/fifo.h>
92 #include <miscfs/genfs/genfs.h>
93 #include <miscfs/specfs/specdev.h>
94
95 #include <ufs/ufs/inode.h>
96 #include <ufs/ufs/dir.h>
97 #include <ufs/ufs/ufsmount.h>
98 #include <ufs/ufs/ufs_extern.h>
99
100 #include <uvm/uvm.h>
101 #ifdef LFS_UBC
102 # include <uvm/uvm_pmap.h>
103 # include <uvm/uvm_stat.h>
104 # include <uvm/uvm_pager.h>
105 #endif
106
107 #include <ufs/lfs/lfs.h>
108 #include <ufs/lfs/lfs_extern.h>
109
110 extern pid_t lfs_writer_daemon;
111 extern int lfs_subsys_pages;
112
113 /* Global vfs data structures for lfs. */
114 int (**lfs_vnodeop_p)(void *);
115 const struct vnodeopv_entry_desc lfs_vnodeop_entries[] = {
116 { &vop_default_desc, vn_default_error },
117 { &vop_lookup_desc, ufs_lookup }, /* lookup */
118 { &vop_create_desc, lfs_create }, /* create */
119 { &vop_whiteout_desc, ufs_whiteout }, /* whiteout */
120 { &vop_mknod_desc, lfs_mknod }, /* mknod */
121 { &vop_open_desc, ufs_open }, /* open */
122 { &vop_close_desc, lfs_close }, /* close */
123 { &vop_access_desc, ufs_access }, /* access */
124 { &vop_getattr_desc, lfs_getattr }, /* getattr */
125 { &vop_setattr_desc, lfs_setattr }, /* setattr */
126 { &vop_read_desc, lfs_read }, /* read */
127 { &vop_write_desc, lfs_write }, /* write */
128 { &vop_lease_desc, ufs_lease_check }, /* lease */
129 { &vop_ioctl_desc, ufs_ioctl }, /* ioctl */
130 { &vop_fcntl_desc, lfs_fcntl }, /* fcntl */
131 { &vop_poll_desc, ufs_poll }, /* poll */
132 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
133 { &vop_revoke_desc, ufs_revoke }, /* revoke */
134 #ifdef LFS_UBC
135 { &vop_mmap_desc, lfs_mmap }, /* mmap */
136 #else
137 { &vop_mmap_desc, ufs_mmap }, /* mmap */
138 #endif
139 { &vop_fsync_desc, lfs_fsync }, /* fsync */
140 { &vop_seek_desc, ufs_seek }, /* seek */
141 { &vop_remove_desc, lfs_remove }, /* remove */
142 { &vop_link_desc, lfs_link }, /* link */
143 { &vop_rename_desc, lfs_rename }, /* rename */
144 { &vop_mkdir_desc, lfs_mkdir }, /* mkdir */
145 { &vop_rmdir_desc, lfs_rmdir }, /* rmdir */
146 { &vop_symlink_desc, lfs_symlink }, /* symlink */
147 { &vop_readdir_desc, ufs_readdir }, /* readdir */
148 { &vop_readlink_desc, ufs_readlink }, /* readlink */
149 { &vop_abortop_desc, ufs_abortop }, /* abortop */
150 { &vop_inactive_desc, lfs_inactive }, /* inactive */
151 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
152 { &vop_lock_desc, ufs_lock }, /* lock */
153 { &vop_unlock_desc, ufs_unlock }, /* unlock */
154 { &vop_bmap_desc, ufs_bmap }, /* bmap */
155 { &vop_strategy_desc, ufs_strategy }, /* strategy */
156 { &vop_print_desc, ufs_print }, /* print */
157 { &vop_islocked_desc, ufs_islocked }, /* islocked */
158 { &vop_pathconf_desc, ufs_pathconf }, /* pathconf */
159 { &vop_advlock_desc, ufs_advlock }, /* advlock */
160 { &vop_blkatoff_desc, lfs_blkatoff }, /* blkatoff */
161 { &vop_valloc_desc, lfs_valloc }, /* valloc */
162 { &vop_balloc_desc, lfs_balloc }, /* balloc */
163 { &vop_vfree_desc, lfs_vfree }, /* vfree */
164 { &vop_truncate_desc, lfs_truncate }, /* truncate */
165 { &vop_update_desc, lfs_update }, /* update */
166 { &vop_bwrite_desc, lfs_bwrite }, /* bwrite */
167 #ifdef LFS_UBC
168 { &vop_getpages_desc, genfs_getpages }, /* getpages */
169 #else
170 { &vop_getpages_desc, lfs_getpages }, /* getpages */
171 #endif
172 { &vop_putpages_desc, lfs_putpages }, /* putpages */
173 { NULL, NULL }
174 };
175 const struct vnodeopv_desc lfs_vnodeop_opv_desc =
176 { &lfs_vnodeop_p, lfs_vnodeop_entries };
177
178 int (**lfs_specop_p)(void *);
179 const struct vnodeopv_entry_desc lfs_specop_entries[] = {
180 { &vop_default_desc, vn_default_error },
181 { &vop_lookup_desc, spec_lookup }, /* lookup */
182 { &vop_create_desc, spec_create }, /* create */
183 { &vop_mknod_desc, spec_mknod }, /* mknod */
184 { &vop_open_desc, spec_open }, /* open */
185 { &vop_close_desc, lfsspec_close }, /* close */
186 { &vop_access_desc, ufs_access }, /* access */
187 { &vop_getattr_desc, lfs_getattr }, /* getattr */
188 { &vop_setattr_desc, lfs_setattr }, /* setattr */
189 { &vop_read_desc, ufsspec_read }, /* read */
190 { &vop_write_desc, ufsspec_write }, /* write */
191 { &vop_lease_desc, spec_lease_check }, /* lease */
192 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
193 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
194 { &vop_poll_desc, spec_poll }, /* poll */
195 { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
196 { &vop_revoke_desc, spec_revoke }, /* revoke */
197 { &vop_mmap_desc, spec_mmap }, /* mmap */
198 { &vop_fsync_desc, spec_fsync }, /* fsync */
199 { &vop_seek_desc, spec_seek }, /* seek */
200 { &vop_remove_desc, spec_remove }, /* remove */
201 { &vop_link_desc, spec_link }, /* link */
202 { &vop_rename_desc, spec_rename }, /* rename */
203 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
204 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
205 { &vop_symlink_desc, spec_symlink }, /* symlink */
206 { &vop_readdir_desc, spec_readdir }, /* readdir */
207 { &vop_readlink_desc, spec_readlink }, /* readlink */
208 { &vop_abortop_desc, spec_abortop }, /* abortop */
209 { &vop_inactive_desc, lfs_inactive }, /* inactive */
210 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
211 { &vop_lock_desc, ufs_lock }, /* lock */
212 { &vop_unlock_desc, ufs_unlock }, /* unlock */
213 { &vop_bmap_desc, spec_bmap }, /* bmap */
214 { &vop_strategy_desc, spec_strategy }, /* strategy */
215 { &vop_print_desc, ufs_print }, /* print */
216 { &vop_islocked_desc, ufs_islocked }, /* islocked */
217 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
218 { &vop_advlock_desc, spec_advlock }, /* advlock */
219 { &vop_blkatoff_desc, spec_blkatoff }, /* blkatoff */
220 { &vop_valloc_desc, spec_valloc }, /* valloc */
221 { &vop_vfree_desc, lfs_vfree }, /* vfree */
222 { &vop_truncate_desc, spec_truncate }, /* truncate */
223 { &vop_update_desc, lfs_update }, /* update */
224 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
225 { &vop_getpages_desc, spec_getpages }, /* getpages */
226 { &vop_putpages_desc, spec_putpages }, /* putpages */
227 { NULL, NULL }
228 };
229 const struct vnodeopv_desc lfs_specop_opv_desc =
230 { &lfs_specop_p, lfs_specop_entries };
231
232 int (**lfs_fifoop_p)(void *);
233 const struct vnodeopv_entry_desc lfs_fifoop_entries[] = {
234 { &vop_default_desc, vn_default_error },
235 { &vop_lookup_desc, fifo_lookup }, /* lookup */
236 { &vop_create_desc, fifo_create }, /* create */
237 { &vop_mknod_desc, fifo_mknod }, /* mknod */
238 { &vop_open_desc, fifo_open }, /* open */
239 { &vop_close_desc, lfsfifo_close }, /* close */
240 { &vop_access_desc, ufs_access }, /* access */
241 { &vop_getattr_desc, lfs_getattr }, /* getattr */
242 { &vop_setattr_desc, lfs_setattr }, /* setattr */
243 { &vop_read_desc, ufsfifo_read }, /* read */
244 { &vop_write_desc, ufsfifo_write }, /* write */
245 { &vop_lease_desc, fifo_lease_check }, /* lease */
246 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
247 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
248 { &vop_poll_desc, fifo_poll }, /* poll */
249 { &vop_kqfilter_desc, fifo_kqfilter }, /* kqfilter */
250 { &vop_revoke_desc, fifo_revoke }, /* revoke */
251 { &vop_mmap_desc, fifo_mmap }, /* mmap */
252 { &vop_fsync_desc, fifo_fsync }, /* fsync */
253 { &vop_seek_desc, fifo_seek }, /* seek */
254 { &vop_remove_desc, fifo_remove }, /* remove */
255 { &vop_link_desc, fifo_link }, /* link */
256 { &vop_rename_desc, fifo_rename }, /* rename */
257 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
258 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
259 { &vop_symlink_desc, fifo_symlink }, /* symlink */
260 { &vop_readdir_desc, fifo_readdir }, /* readdir */
261 { &vop_readlink_desc, fifo_readlink }, /* readlink */
262 { &vop_abortop_desc, fifo_abortop }, /* abortop */
263 { &vop_inactive_desc, lfs_inactive }, /* inactive */
264 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
265 { &vop_lock_desc, ufs_lock }, /* lock */
266 { &vop_unlock_desc, ufs_unlock }, /* unlock */
267 { &vop_bmap_desc, fifo_bmap }, /* bmap */
268 { &vop_strategy_desc, fifo_strategy }, /* strategy */
269 { &vop_print_desc, ufs_print }, /* print */
270 { &vop_islocked_desc, ufs_islocked }, /* islocked */
271 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
272 { &vop_advlock_desc, fifo_advlock }, /* advlock */
273 { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */
274 { &vop_valloc_desc, fifo_valloc }, /* valloc */
275 { &vop_vfree_desc, lfs_vfree }, /* vfree */
276 { &vop_truncate_desc, fifo_truncate }, /* truncate */
277 { &vop_update_desc, lfs_update }, /* update */
278 { &vop_bwrite_desc, lfs_bwrite }, /* bwrite */
279 { &vop_putpages_desc, fifo_putpages }, /* putpages */
280 { NULL, NULL }
281 };
282 const struct vnodeopv_desc lfs_fifoop_opv_desc =
283 { &lfs_fifoop_p, lfs_fifoop_entries };
284
285 /*
286 * A function version of LFS_ITIMES, for the UFS functions which call ITIMES
287 */
288 void
289 lfs_itimes(struct inode *ip, struct timespec *acc, struct timespec *mod, struct timespec *cre)
290 {
291 LFS_ITIMES(ip, acc, mod, cre);
292 }
293
294 #define LFS_READWRITE
295 #include <ufs/ufs/ufs_readwrite.c>
296 #undef LFS_READWRITE
297
298 /*
299 * Synch an open file.
300 */
301 /* ARGSUSED */
302 int
303 lfs_fsync(void *v)
304 {
305 struct vop_fsync_args /* {
306 struct vnode *a_vp;
307 struct ucred *a_cred;
308 int a_flags;
309 off_t offlo;
310 off_t offhi;
311 struct proc *a_p;
312 } */ *ap = v;
313 struct vnode *vp = ap->a_vp;
314 int error, wait;
315
316 /*
317 * Trickle sync checks for need to do a checkpoint after possible
318 * activity from the pagedaemon.
319 */
320 if (ap->a_flags & FSYNC_LAZY) {
321 wakeup(&lfs_writer_daemon);
322 return 0;
323 }
324
325 wait = (ap->a_flags & FSYNC_WAIT);
326 do {
327 #ifdef DEBUG
328 struct buf *bp;
329 #endif
330
331 simple_lock(&vp->v_interlock);
332 error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
333 round_page(ap->a_offhi),
334 PGO_CLEANIT | (wait ? PGO_SYNCIO : 0));
335 if (error)
336 return error;
337 error = VOP_UPDATE(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
338 if (wait && error == 0 && !VPISEMPTY(vp)) {
339 #ifdef DEBUG
340 printf("lfs_fsync: reflushing ino %d\n",
341 VTOI(vp)->i_number);
342 printf("vflags %x iflags %x npages %d\n",
343 vp->v_flag, VTOI(vp)->i_flag,
344 vp->v_uobj.uo_npages);
345 LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs)
346 printf("%" PRId64 " (%lx)", bp->b_lblkno,
347 bp->b_flags);
348 printf("\n");
349 #endif
350 VTOI(vp)->i_flag |= IN_MODIFIED;
351 }
352 } while (wait && error == 0 && !VPISEMPTY(vp));
353
354 return error;
355 }
356
357 /*
358 * Take IN_ADIROP off, then call ufs_inactive.
359 */
360 int
361 lfs_inactive(void *v)
362 {
363 struct vop_inactive_args /* {
364 struct vnode *a_vp;
365 struct proc *a_p;
366 } */ *ap = v;
367
368 KASSERT(VTOI(ap->a_vp)->i_ffs_nlink == VTOI(ap->a_vp)->i_ffs_effnlink);
369
370 lfs_unmark_vnode(ap->a_vp);
371
372 return ufs_inactive(v);
373 }
374
375 /*
376 * These macros are used to bracket UFS directory ops, so that we can
377 * identify all the pages touched during directory ops which need to
378 * be ordered and flushed atomically, so that they may be recovered.
379 */
380 /*
381 * XXX KS - Because we have to mark nodes VDIROP in order to prevent
382 * the cache from reclaiming them while a dirop is in progress, we must
383 * also manage the number of nodes so marked (otherwise we can run out).
384 * We do this by setting lfs_dirvcount to the number of marked vnodes; it
385 * is decremented during segment write, when VDIROP is taken off.
386 */
387 #define SET_DIROP(vp) SET_DIROP2((vp), NULL)
388 #define SET_DIROP2(vp, vp2) lfs_set_dirop((vp), (vp2))
389 static int lfs_set_dirop(struct vnode *, struct vnode *);
390 extern int lfs_dirvcount;
391 extern int lfs_do_flush;
392
393 #define NRESERVE(fs) (btofsb(fs, (NIADDR + 3 + (2 * NIADDR + 3)) << fs->lfs_bshift))
394
395 static int
396 lfs_set_dirop(struct vnode *vp, struct vnode *vp2)
397 {
398 struct lfs *fs;
399 int error;
400
401 KASSERT(VOP_ISLOCKED(vp));
402 KASSERT(vp2 == NULL || VOP_ISLOCKED(vp2));
403
404 fs = VTOI(vp)->i_lfs;
405 /*
406 * We might need one directory block plus supporting indirect blocks,
407 * plus an inode block and ifile page for the new vnode.
408 */
409 if ((error = lfs_reserve(fs, vp, vp2, NRESERVE(fs))) != 0)
410 return (error);
411
412 if (fs->lfs_dirops == 0)
413 lfs_check(vp, LFS_UNUSED_LBN, 0);
414 while (fs->lfs_writer || lfs_dirvcount > LFS_MAX_DIROP) {
415 if (fs->lfs_writer)
416 tsleep(&fs->lfs_dirops, PRIBIO + 1, "lfs_sdirop", 0);
417 if (lfs_dirvcount > LFS_MAX_DIROP && fs->lfs_dirops == 0) {
418 wakeup(&lfs_writer_daemon);
419 preempt(NULL);
420 }
421
422 if (lfs_dirvcount > LFS_MAX_DIROP) {
423 #ifdef DEBUG_LFS
424 printf("lfs_set_dirop: sleeping with dirops=%d, "
425 "dirvcount=%d\n", fs->lfs_dirops,
426 lfs_dirvcount);
427 #endif
428 if ((error = tsleep(&lfs_dirvcount, PCATCH|PUSER,
429 "lfs_maxdirop", 0)) != 0) {
430 goto unreserve;
431 }
432 }
433 }
434 ++fs->lfs_dirops;
435 fs->lfs_doifile = 1;
436
437 /* Hold a reference so SET_ENDOP will be happy */
438 vref(vp);
439 if (vp2)
440 vref(vp2);
441
442 return 0;
443
444 unreserve:
445 lfs_reserve(fs, vp, vp2, -NRESERVE(fs));
446 return error;
447 }
448
449 #define SET_ENDOP(fs, vp, str) SET_ENDOP2((fs), (vp), NULL, (str))
450 #define SET_ENDOP2(fs, vp, vp2, str) { \
451 --(fs)->lfs_dirops; \
452 if (!(fs)->lfs_dirops) { \
453 if ((fs)->lfs_nadirop) { \
454 panic("SET_ENDOP: %s: no dirops but nadirop=%d", \
455 (str), (fs)->lfs_nadirop); \
456 } \
457 wakeup(&(fs)->lfs_writer); \
458 lfs_check((vp),LFS_UNUSED_LBN,0); \
459 } \
460 lfs_reserve((fs), vp, vp2, -NRESERVE(fs)); /* XXX */ \
461 vrele(vp); \
462 if (vp2) \
463 vrele(vp2); \
464 }
465
466 #define MARK_VNODE(dvp) do { \
467 struct inode *_ip = VTOI(dvp); \
468 struct lfs *_fs = _ip->i_lfs; \
469 \
470 if (!((dvp)->v_flag & VDIROP)) { \
471 (void)lfs_vref(dvp); \
472 ++lfs_dirvcount; \
473 TAILQ_INSERT_TAIL(&_fs->lfs_dchainhd, _ip, i_lfs_dchain); \
474 } \
475 (dvp)->v_flag |= VDIROP; \
476 if (!(_ip->i_flag & IN_ADIROP)) { \
477 ++_fs->lfs_nadirop; \
478 } \
479 _ip->i_flag |= IN_ADIROP; \
480 } while (0)
481
482 #define UNMARK_VNODE(vp) lfs_unmark_vnode(vp)
483
484 void lfs_unmark_vnode(struct vnode *vp)
485 {
486 struct inode *ip;
487
488 ip = VTOI(vp);
489
490 if (ip->i_flag & IN_ADIROP)
491 --ip->i_lfs->lfs_nadirop;
492 ip->i_flag &= ~IN_ADIROP;
493 }
494
495 int
496 lfs_symlink(void *v)
497 {
498 struct vop_symlink_args /* {
499 struct vnode *a_dvp;
500 struct vnode **a_vpp;
501 struct componentname *a_cnp;
502 struct vattr *a_vap;
503 char *a_target;
504 } */ *ap = v;
505 int error;
506
507 if ((error = SET_DIROP(ap->a_dvp)) != 0) {
508 vput(ap->a_dvp);
509 return error;
510 }
511 MARK_VNODE(ap->a_dvp);
512 error = ufs_symlink(ap);
513 UNMARK_VNODE(ap->a_dvp);
514 if (*(ap->a_vpp))
515 UNMARK_VNODE(*(ap->a_vpp));
516 SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"symlink");
517 return (error);
518 }
519
520 int
521 lfs_mknod(void *v)
522 {
523 struct vop_mknod_args /* {
524 struct vnode *a_dvp;
525 struct vnode **a_vpp;
526 struct componentname *a_cnp;
527 struct vattr *a_vap;
528 } */ *ap = v;
529 struct vattr *vap = ap->a_vap;
530 struct vnode **vpp = ap->a_vpp;
531 struct inode *ip;
532 int error;
533 struct mount *mp;
534 ino_t ino;
535
536 if ((error = SET_DIROP(ap->a_dvp)) != 0) {
537 vput(ap->a_dvp);
538 return error;
539 }
540 MARK_VNODE(ap->a_dvp);
541 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
542 ap->a_dvp, vpp, ap->a_cnp);
543 UNMARK_VNODE(ap->a_dvp);
544 if (*(ap->a_vpp))
545 UNMARK_VNODE(*(ap->a_vpp));
546
547 /* Either way we're done with the dirop at this point */
548 SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"mknod");
549
550 if (error)
551 return (error);
552
553 ip = VTOI(*vpp);
554 mp = (*vpp)->v_mount;
555 ino = ip->i_number;
556 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
557 if (vap->va_rdev != VNOVAL) {
558 /*
559 * Want to be able to use this to make badblock
560 * inodes, so don't truncate the dev number.
561 */
562 #if 0
563 ip->i_ffs_rdev = ufs_rw32(vap->va_rdev,
564 UFS_MPNEEDSWAP((*vpp)->v_mount));
565 #else
566 ip->i_ffs_rdev = vap->va_rdev;
567 #endif
568 }
569 /*
570 * Call fsync to write the vnode so that we don't have to deal with
571 * flushing it when it's marked VDIROP|VXLOCK.
572 *
573 * XXX KS - If we can't flush we also can't call vgone(), so must
574 * return. But, that leaves this vnode in limbo, also not good.
575 * Can this ever happen (barring hardware failure)?
576 */
577 if ((error = VOP_FSYNC(*vpp, NOCRED, FSYNC_WAIT, 0, 0,
578 curproc)) != 0) {
579 printf("Couldn't fsync in mknod (ino %d)---what do I do?\n",
580 VTOI(*vpp)->i_number);
581 return (error);
582 }
583 /*
584 * Remove vnode so that it will be reloaded by VFS_VGET and
585 * checked to see if it is an alias of an existing entry in
586 * the inode cache.
587 */
588 /* Used to be vput, but that causes us to call VOP_INACTIVE twice. */
589 VOP_UNLOCK(*vpp, 0);
590 lfs_vunref(*vpp);
591 (*vpp)->v_type = VNON;
592 vgone(*vpp);
593 error = VFS_VGET(mp, ino, vpp);
594 if (error != 0) {
595 *vpp = NULL;
596 return (error);
597 }
598 return (0);
599 }
600
601 int
602 lfs_create(void *v)
603 {
604 struct vop_create_args /* {
605 struct vnode *a_dvp;
606 struct vnode **a_vpp;
607 struct componentname *a_cnp;
608 struct vattr *a_vap;
609 } */ *ap = v;
610 int error;
611
612 if ((error = SET_DIROP(ap->a_dvp)) != 0) {
613 vput(ap->a_dvp);
614 return error;
615 }
616 MARK_VNODE(ap->a_dvp);
617 error = ufs_create(ap);
618 UNMARK_VNODE(ap->a_dvp);
619 if (*(ap->a_vpp))
620 UNMARK_VNODE(*(ap->a_vpp));
621 SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"create");
622 return (error);
623 }
624
625 int
626 lfs_mkdir(void *v)
627 {
628 struct vop_mkdir_args /* {
629 struct vnode *a_dvp;
630 struct vnode **a_vpp;
631 struct componentname *a_cnp;
632 struct vattr *a_vap;
633 } */ *ap = v;
634 int error;
635
636 if ((error = SET_DIROP(ap->a_dvp)) != 0) {
637 vput(ap->a_dvp);
638 return error;
639 }
640 MARK_VNODE(ap->a_dvp);
641 error = ufs_mkdir(ap);
642 UNMARK_VNODE(ap->a_dvp);
643 if (*(ap->a_vpp))
644 UNMARK_VNODE(*(ap->a_vpp));
645 SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"mkdir");
646 return (error);
647 }
648
649 int
650 lfs_remove(void *v)
651 {
652 struct vop_remove_args /* {
653 struct vnode *a_dvp;
654 struct vnode *a_vp;
655 struct componentname *a_cnp;
656 } */ *ap = v;
657 struct vnode *dvp, *vp;
658 int error;
659
660 dvp = ap->a_dvp;
661 vp = ap->a_vp;
662 if ((error = SET_DIROP2(dvp, vp)) != 0) {
663 if (dvp == vp)
664 vrele(vp);
665 else
666 vput(vp);
667 vput(dvp);
668 return error;
669 }
670 MARK_VNODE(dvp);
671 MARK_VNODE(vp);
672 error = ufs_remove(ap);
673 UNMARK_VNODE(dvp);
674 UNMARK_VNODE(vp);
675
676 SET_ENDOP2(VTOI(dvp)->i_lfs, dvp, vp, "remove");
677 return (error);
678 }
679
680 int
681 lfs_rmdir(void *v)
682 {
683 struct vop_rmdir_args /* {
684 struct vnodeop_desc *a_desc;
685 struct vnode *a_dvp;
686 struct vnode *a_vp;
687 struct componentname *a_cnp;
688 } */ *ap = v;
689 struct vnode *vp;
690 int error;
691
692 vp = ap->a_vp;
693 if ((error = SET_DIROP2(ap->a_dvp, ap->a_vp)) != 0) {
694 vrele(ap->a_dvp);
695 if (ap->a_vp != ap->a_dvp)
696 VOP_UNLOCK(ap->a_dvp, 0);
697 vput(vp);
698 return error;
699 }
700 MARK_VNODE(ap->a_dvp);
701 MARK_VNODE(vp);
702 error = ufs_rmdir(ap);
703 UNMARK_VNODE(ap->a_dvp);
704 UNMARK_VNODE(vp);
705
706 SET_ENDOP2(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, vp, "rmdir");
707 return (error);
708 }
709
710 int
711 lfs_link(void *v)
712 {
713 struct vop_link_args /* {
714 struct vnode *a_dvp;
715 struct vnode *a_vp;
716 struct componentname *a_cnp;
717 } */ *ap = v;
718 int error;
719
720 if ((error = SET_DIROP(ap->a_dvp)) != 0) {
721 vput(ap->a_dvp);
722 return error;
723 }
724 MARK_VNODE(ap->a_dvp);
725 error = ufs_link(ap);
726 UNMARK_VNODE(ap->a_dvp);
727 SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"link");
728 return (error);
729 }
730
731 int
732 lfs_rename(void *v)
733 {
734 struct vop_rename_args /* {
735 struct vnode *a_fdvp;
736 struct vnode *a_fvp;
737 struct componentname *a_fcnp;
738 struct vnode *a_tdvp;
739 struct vnode *a_tvp;
740 struct componentname *a_tcnp;
741 } */ *ap = v;
742 struct vnode *tvp, *fvp, *tdvp, *fdvp;
743 struct componentname *tcnp, *fcnp;
744 int error;
745 struct lfs *fs;
746
747 fs = VTOI(ap->a_fdvp)->i_lfs;
748 tvp = ap->a_tvp;
749 tdvp = ap->a_tdvp;
750 tcnp = ap->a_tcnp;
751 fvp = ap->a_fvp;
752 fdvp = ap->a_fdvp;
753 fcnp = ap->a_fcnp;
754
755 /*
756 * Check for cross-device rename.
757 * If it is, we don't want to set dirops, just error out.
758 * (In particular note that MARK_VNODE(tdvp) will DTWT on
759 * a cross-device rename.)
760 *
761 * Copied from ufs_rename.
762 */
763 if ((fvp->v_mount != tdvp->v_mount) ||
764 (tvp && (fvp->v_mount != tvp->v_mount))) {
765 error = EXDEV;
766 goto errout;
767 }
768
769 /*
770 * Check to make sure we're not renaming a vnode onto itself
771 * (deleting a hard link by renaming one name onto another);
772 * if we are we can't recursively call VOP_REMOVE since that
773 * would leave us with an unaccounted-for number of live dirops.
774 *
775 * Inline the relevant section of ufs_rename here, *before*
776 * calling SET_DIROP2.
777 */
778 if (tvp && ((VTOI(tvp)->i_ffs_flags & (IMMUTABLE | APPEND)) ||
779 (VTOI(tdvp)->i_ffs_flags & APPEND))) {
780 error = EPERM;
781 goto errout;
782 }
783 if (fvp == tvp) {
784 if (fvp->v_type == VDIR) {
785 error = EINVAL;
786 goto errout;
787 }
788
789 /* Release destination completely. */
790 VOP_ABORTOP(tdvp, tcnp);
791 vput(tdvp);
792 vput(tvp);
793
794 /* Delete source. */
795 vrele(fvp);
796 fcnp->cn_flags &= ~(MODMASK | SAVESTART);
797 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
798 fcnp->cn_nameiop = DELETE;
799 if ((error = relookup(fdvp, &fvp, fcnp))){
800 /* relookup blew away fdvp */
801 return (error);
802 }
803 return (VOP_REMOVE(fdvp, fvp, fcnp));
804 }
805
806 if ((error = SET_DIROP2(tdvp, tvp)) != 0)
807 goto errout;
808 MARK_VNODE(fdvp);
809 MARK_VNODE(tdvp);
810 MARK_VNODE(fvp);
811 if (tvp) {
812 MARK_VNODE(tvp);
813 }
814
815 error = ufs_rename(ap);
816 UNMARK_VNODE(fdvp);
817 UNMARK_VNODE(tdvp);
818 UNMARK_VNODE(fvp);
819 if (tvp) {
820 UNMARK_VNODE(tvp);
821 }
822 SET_ENDOP2(fs, tdvp, tvp, "rename");
823 return (error);
824
825 errout:
826 VOP_ABORTOP(tdvp, ap->a_tcnp); /* XXX, why not in NFS? */
827 if (tdvp == tvp)
828 vrele(tdvp);
829 else
830 vput(tdvp);
831 if (tvp)
832 vput(tvp);
833 VOP_ABORTOP(fdvp, ap->a_fcnp); /* XXX, why not in NFS? */
834 vrele(fdvp);
835 vrele(fvp);
836 return (error);
837 }
838
839 /* XXX hack to avoid calling ITIMES in getattr */
840 int
841 lfs_getattr(void *v)
842 {
843 struct vop_getattr_args /* {
844 struct vnode *a_vp;
845 struct vattr *a_vap;
846 struct ucred *a_cred;
847 struct proc *a_p;
848 } */ *ap = v;
849 struct vnode *vp = ap->a_vp;
850 struct inode *ip = VTOI(vp);
851 struct vattr *vap = ap->a_vap;
852 struct lfs *fs = ip->i_lfs;
853 /*
854 * Copy from inode table
855 */
856 vap->va_fsid = ip->i_dev;
857 vap->va_fileid = ip->i_number;
858 vap->va_mode = ip->i_ffs_mode & ~IFMT;
859 vap->va_nlink = ip->i_ffs_nlink;
860 vap->va_uid = ip->i_ffs_uid;
861 vap->va_gid = ip->i_ffs_gid;
862 vap->va_rdev = (dev_t)ip->i_ffs_rdev;
863 vap->va_size = vp->v_size;
864 vap->va_atime.tv_sec = ip->i_ffs_atime;
865 vap->va_atime.tv_nsec = ip->i_ffs_atimensec;
866 vap->va_mtime.tv_sec = ip->i_ffs_mtime;
867 vap->va_mtime.tv_nsec = ip->i_ffs_mtimensec;
868 vap->va_ctime.tv_sec = ip->i_ffs_ctime;
869 vap->va_ctime.tv_nsec = ip->i_ffs_ctimensec;
870 vap->va_flags = ip->i_ffs_flags;
871 vap->va_gen = ip->i_ffs_gen;
872 /* this doesn't belong here */
873 if (vp->v_type == VBLK)
874 vap->va_blocksize = BLKDEV_IOSIZE;
875 else if (vp->v_type == VCHR)
876 vap->va_blocksize = MAXBSIZE;
877 else
878 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
879 vap->va_bytes = fsbtob(fs, (u_quad_t)ip->i_lfs_effnblks);
880 vap->va_type = vp->v_type;
881 vap->va_filerev = ip->i_modrev;
882 return (0);
883 }
884
885 /*
886 * Check to make sure the inode blocks won't choke the buffer
887 * cache, then call ufs_setattr as usual.
888 */
889 int
890 lfs_setattr(void *v)
891 {
892 struct vop_getattr_args /* {
893 struct vnode *a_vp;
894 struct vattr *a_vap;
895 struct ucred *a_cred;
896 struct proc *a_p;
897 } */ *ap = v;
898 struct vnode *vp = ap->a_vp;
899
900 lfs_check(vp, LFS_UNUSED_LBN, 0);
901 return ufs_setattr(v);
902 }
903
904 /*
905 * Close called
906 *
907 * XXX -- we were using ufs_close, but since it updates the
908 * times on the inode, we might need to bump the uinodes
909 * count.
910 */
911 /* ARGSUSED */
912 int
913 lfs_close(void *v)
914 {
915 struct vop_close_args /* {
916 struct vnode *a_vp;
917 int a_fflag;
918 struct ucred *a_cred;
919 struct proc *a_p;
920 } */ *ap = v;
921 struct vnode *vp = ap->a_vp;
922 struct inode *ip = VTOI(vp);
923 struct timespec ts;
924
925 if (vp->v_usecount > 1) {
926 TIMEVAL_TO_TIMESPEC(&time, &ts);
927 LFS_ITIMES(ip, &ts, &ts, &ts);
928 }
929 return (0);
930 }
931
932 /*
933 * Close wrapper for special devices.
934 *
935 * Update the times on the inode then do device close.
936 */
937 int
938 lfsspec_close(void *v)
939 {
940 struct vop_close_args /* {
941 struct vnode *a_vp;
942 int a_fflag;
943 struct ucred *a_cred;
944 struct proc *a_p;
945 } */ *ap = v;
946 struct vnode *vp;
947 struct inode *ip;
948 struct timespec ts;
949
950 vp = ap->a_vp;
951 ip = VTOI(vp);
952 if (vp->v_usecount > 1) {
953 TIMEVAL_TO_TIMESPEC(&time, &ts);
954 LFS_ITIMES(ip, &ts, &ts, &ts);
955 }
956 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
957 }
958
959 /*
960 * Close wrapper for fifo's.
961 *
962 * Update the times on the inode then do device close.
963 */
964 int
965 lfsfifo_close(void *v)
966 {
967 struct vop_close_args /* {
968 struct vnode *a_vp;
969 int a_fflag;
970 struct ucred *a_cred;
971 struct proc *a_p;
972 } */ *ap = v;
973 struct vnode *vp;
974 struct inode *ip;
975 struct timespec ts;
976
977 vp = ap->a_vp;
978 ip = VTOI(vp);
979 if (ap->a_vp->v_usecount > 1) {
980 TIMEVAL_TO_TIMESPEC(&time, &ts);
981 LFS_ITIMES(ip, &ts, &ts, &ts);
982 }
983 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
984 }
985
986 /*
987 * Reclaim an inode so that it can be used for other purposes.
988 */
989 int lfs_no_inactive = 0;
990
991 int
992 lfs_reclaim(void *v)
993 {
994 struct vop_reclaim_args /* {
995 struct vnode *a_vp;
996 struct proc *a_p;
997 } */ *ap = v;
998 struct vnode *vp = ap->a_vp;
999 struct inode *ip = VTOI(vp);
1000 int error;
1001
1002 KASSERT(ip->i_ffs_nlink == ip->i_ffs_effnlink);
1003
1004 LFS_CLR_UINO(ip, IN_ALLMOD);
1005 if ((error = ufs_reclaim(vp, ap->a_p)))
1006 return (error);
1007 pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
1008 ip->inode_ext.lfs = NULL;
1009 pool_put(&lfs_inode_pool, vp->v_data);
1010 vp->v_data = NULL;
1011 return (0);
1012 }
1013
1014 static void
1015 lfs_flush_dirops(struct lfs *fs)
1016 {
1017 struct inode *ip, *nip;
1018 struct vnode *vp;
1019 extern int lfs_dostats;
1020 struct segment *sp;
1021 int needunlock;
1022
1023 if (fs->lfs_ronly)
1024 return;
1025
1026 if (TAILQ_FIRST(&fs->lfs_dchainhd) == NULL)
1027 return;
1028
1029 /* XXX simplelock fs->lfs_dirops */
1030 while (fs->lfs_dirops > 0) {
1031 ++fs->lfs_diropwait;
1032 tsleep(&fs->lfs_writer, PRIBIO+1, "pndirop", 0);
1033 --fs->lfs_diropwait;
1034 }
1035 /* disallow dirops during flush */
1036 fs->lfs_writer++;
1037
1038 if (lfs_dostats)
1039 ++lfs_stats.flush_invoked;
1040
1041 /*
1042 * Inline lfs_segwrite/lfs_writevnodes, but just for dirops.
1043 * Technically this is a checkpoint (the on-disk state is valid)
1044 * even though we are leaving out all the file data.
1045 */
1046 lfs_imtime(fs);
1047 lfs_seglock(fs, SEGM_CKP);
1048 sp = fs->lfs_sp;
1049
1050 /*
1051 * lfs_writevnodes, optimized to get dirops out of the way.
1052 * Only write dirops, and don't flush files' pages, only
1053 * blocks from the directories.
1054 *
1055 * We don't need to vref these files because they are
1056 * dirops and so hold an extra reference until the
1057 * segunlock clears them of that status.
1058 *
1059 * We don't need to check for IN_ADIROP because we know that
1060 * no dirops are active.
1061 *
1062 */
1063 for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
1064 nip = TAILQ_NEXT(ip, i_lfs_dchain);
1065 vp = ITOV(ip);
1066
1067 /*
1068 * All writes to directories come from dirops; all
1069 * writes to files' direct blocks go through the page
1070 * cache, which we're not touching. Reads to files
1071 * and/or directories will not be affected by writing
1072 * directory blocks inodes and file inodes. So we don't
1073 * really need to lock. If we don't lock, though,
1074 * make sure that we don't clear IN_MODIFIED
1075 * unnecessarily.
1076 */
1077 if (vp->v_flag & VXLOCK)
1078 continue;
1079 if (vn_lock(vp, LK_EXCLUSIVE | LK_CANRECURSE |
1080 LK_NOWAIT) == 0) {
1081 needunlock = 1;
1082 } else {
1083 printf("lfs_flush_dirops: flushing locked ino %d\n",
1084 VTOI(vp)->i_number);
1085 needunlock = 0;
1086 }
1087 if (vp->v_type != VREG &&
1088 ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp))) {
1089 lfs_writefile(fs, sp, vp);
1090 if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
1091 !(ip->i_flag & IN_ALLMOD)) {
1092 LFS_SET_UINO(ip, IN_MODIFIED);
1093 }
1094 }
1095 (void) lfs_writeinode(fs, sp, ip);
1096 if (needunlock)
1097 VOP_UNLOCK(vp, 0);
1098 else
1099 LFS_SET_UINO(ip, IN_MODIFIED);
1100 }
1101 /* We've written all the dirops there are */
1102 ((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
1103 (void) lfs_writeseg(fs, sp);
1104 lfs_segunlock(fs);
1105
1106 if (--fs->lfs_writer == 0)
1107 wakeup(&fs->lfs_dirops);
1108 }
1109
1110 /*
1111 * Provide a fcntl interface to sys_lfs_{segwait,bmapv,markv}.
1112 */
1113 int
1114 lfs_fcntl(void *v)
1115 {
1116 struct vop_fcntl_args /* {
1117 struct vnode *a_vp;
1118 u_long a_command;
1119 caddr_t a_data;
1120 int a_fflag;
1121 struct ucred *a_cred;
1122 struct proc *a_p;
1123 } */ *ap = v;
1124 struct timeval *tvp;
1125 BLOCK_INFO *blkiov;
1126 CLEANERINFO *cip;
1127 int blkcnt, error, oclean;
1128 struct lfs_fcntl_markv blkvp;
1129 fsid_t *fsidp;
1130 struct lfs *fs;
1131 struct buf *bp;
1132 daddr_t off;
1133
1134 /* Only respect LFS fcntls on fs root or Ifile */
1135 if (VTOI(ap->a_vp)->i_number != ROOTINO &&
1136 VTOI(ap->a_vp)->i_number != LFS_IFILE_INUM) {
1137 return ufs_fcntl(v);
1138 }
1139
1140 fsidp = &ap->a_vp->v_mount->mnt_stat.f_fsid;
1141
1142 switch(ap->a_command) {
1143 case LFCNSEGWAITALL:
1144 fsidp = NULL;
1145 /* FALLSTHROUGH */
1146 case LFCNSEGWAIT:
1147 tvp = (struct timeval *)ap->a_data;
1148 VOP_UNLOCK(ap->a_vp, 0);
1149 error = lfs_segwait(fsidp, tvp);
1150 VOP_LOCK(ap->a_vp, LK_EXCLUSIVE);
1151 return error;
1152
1153 case LFCNBMAPV:
1154 case LFCNMARKV:
1155 if ((error = suser(ap->a_p->p_ucred, &ap->a_p->p_acflag)) != 0)
1156 return (error);
1157 blkvp = *(struct lfs_fcntl_markv *)ap->a_data;
1158
1159 blkcnt = blkvp.blkcnt;
1160 if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
1161 return (EINVAL);
1162 blkiov = malloc(blkcnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
1163 if ((error = copyin(blkvp.blkiov, blkiov,
1164 blkcnt * sizeof(BLOCK_INFO))) != 0) {
1165 free(blkiov, M_SEGMENT);
1166 return error;
1167 }
1168
1169 VOP_UNLOCK(ap->a_vp, 0);
1170 if (ap->a_command == LFCNBMAPV)
1171 error = lfs_bmapv(ap->a_p, fsidp, blkiov, blkcnt);
1172 else /* LFCNMARKV */
1173 error = lfs_markv(ap->a_p, fsidp, blkiov, blkcnt);
1174 if (error == 0)
1175 error = copyout(blkiov, blkvp.blkiov,
1176 blkcnt * sizeof(BLOCK_INFO));
1177 VOP_LOCK(ap->a_vp, LK_EXCLUSIVE);
1178 free(blkiov, M_SEGMENT);
1179 return error;
1180
1181 case LFCNRECLAIM:
1182 /*
1183 * Flush dirops and write Ifile, allowing empty segments
1184 * to be immediately reclaimed.
1185 */
1186 fs = VTOI(ap->a_vp)->i_lfs;
1187 off = fs->lfs_offset;
1188 lfs_seglock(fs, SEGM_FORCE_CKP | SEGM_CKP);
1189 lfs_flush_dirops(fs);
1190 LFS_CLEANERINFO(cip, fs, bp);
1191 oclean = cip->clean;
1192 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1193 lfs_segwrite(ap->a_vp->v_mount, SEGM_FORCE_CKP);
1194 lfs_segunlock(fs);
1195
1196 #ifdef DEBUG_LFS
1197 LFS_CLEANERINFO(cip, fs, bp);
1198 oclean = cip->clean;
1199 printf("lfs_fcntl: reclaim wrote %" PRId64 " blocks, cleaned "
1200 "%" PRId32 " segments (activesb %d)\n",
1201 fs->lfs_offset - off, cip->clean - oclean,
1202 fs->lfs_activesb);
1203 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
1204 #endif
1205
1206 return 0;
1207
1208 default:
1209 return ufs_fcntl(v);
1210 }
1211 return 0;
1212 }
1213
1214 #ifndef LFS_UBC
1215 int
1216 lfs_getpages(void *v)
1217 {
1218 struct vop_getpages_args /* {
1219 struct vnode *a_vp;
1220 voff_t a_offset;
1221 struct vm_page **a_m;
1222 int *a_count;
1223 int a_centeridx;
1224 vm_prot_t a_access_type;
1225 int a_advice;
1226 int a_flags;
1227 } */ *ap = v;
1228
1229 if ((ap->a_access_type & VM_PROT_WRITE) != 0) {
1230 LFS_SET_UINO(VTOI(ap->a_vp), IN_MODIFIED);
1231 }
1232 return genfs_compat_getpages(v);
1233 }
1234
1235 int
1236 lfs_putpages(void *v)
1237 {
1238 int error;
1239
1240 error = genfs_putpages(v);
1241 return error;
1242 }
1243
1244 #else /* LFS_UBC */
1245
1246 /*
1247 * Make sure that for all pages in every block in the given range,
1248 * either all are dirty or all are clean. If any of the pages
1249 * we've seen so far are dirty, put the vnode on the paging chain,
1250 * and mark it IN_PAGING.
1251 */
1252 static int
1253 check_dirty(struct lfs *fs, struct vnode *vp,
1254 off_t startoffset, off_t endoffset, off_t blkeof,
1255 int flags)
1256 {
1257 int by_list;
1258 struct vm_page *curpg, *pgs[MAXBSIZE / PAGE_SIZE], *pg;
1259 struct lwp *l = curlwp ? curlwp : &lwp0;
1260 off_t soff;
1261 voff_t off;
1262 int i, dirty, tdirty, nonexistent, any_dirty;
1263 int pages_per_block = fs->lfs_bsize >> PAGE_SHIFT;
1264
1265 top:
1266 by_list = (vp->v_uobj.uo_npages <=
1267 ((endoffset - startoffset) >> PAGE_SHIFT) *
1268 UVM_PAGE_HASH_PENALTY);
1269 any_dirty = 0;
1270
1271 if (by_list) {
1272 curpg = TAILQ_FIRST(&vp->v_uobj.memq);
1273 PHOLD(l);
1274 } else {
1275 soff = startoffset;
1276 }
1277 while (by_list || soff < MIN(blkeof, endoffset)) {
1278 if (by_list) {
1279 if (pages_per_block > 1) {
1280 while (curpg && (curpg->offset & fs->lfs_bmask))
1281 curpg = TAILQ_NEXT(curpg, listq);
1282 }
1283 if (curpg == NULL)
1284 break;
1285 soff = curpg->offset;
1286 }
1287
1288 /*
1289 * Mark all pages in extended range busy; find out if any
1290 * of them are dirty.
1291 */
1292 nonexistent = dirty = 0;
1293 for (i = 0; i == 0 || i < pages_per_block; i++) {
1294 if (by_list && pages_per_block <= 1) {
1295 pgs[i] = pg = curpg;
1296 } else {
1297 off = soff + (i << PAGE_SHIFT);
1298 pgs[i] = pg = uvm_pagelookup(&vp->v_uobj, off);
1299 if (pg == NULL) {
1300 ++nonexistent;
1301 continue;
1302 }
1303 }
1304 KASSERT(pg != NULL);
1305 while (pg->flags & PG_BUSY) {
1306 pg->flags |= PG_WANTED;
1307 UVM_UNLOCK_AND_WAIT(pg, &vp->v_interlock, 0,
1308 "lfsput", 0);
1309 simple_lock(&vp->v_interlock);
1310 if (by_list)
1311 goto top;
1312 }
1313 pg->flags |= PG_BUSY;
1314 UVM_PAGE_OWN(pg, "lfs_putpages");
1315
1316 pmap_page_protect(pg, VM_PROT_NONE);
1317 tdirty = (pmap_clear_modify(pg) ||
1318 (pg->flags & PG_CLEAN) == 0);
1319 dirty += tdirty;
1320 }
1321 if (pages_per_block > 0 && nonexistent >= pages_per_block) {
1322 if (by_list) {
1323 curpg = TAILQ_NEXT(curpg, listq);
1324 } else {
1325 soff += fs->lfs_bsize;
1326 }
1327 continue;
1328 }
1329
1330 any_dirty += dirty;
1331 KASSERT(nonexistent == 0);
1332
1333 /*
1334 * If any are dirty make all dirty; unbusy them,
1335 * but if we were asked to clean, wire them so that
1336 * the pagedaemon doesn't bother us about them while
1337 * they're on their way to disk.
1338 */
1339 for (i = 0; i == 0 || i < pages_per_block; i++) {
1340 pg = pgs[i];
1341 KASSERT(!((pg->flags & PG_CLEAN) && (pg->flags & PG_DELWRI)));
1342 if (dirty) {
1343 pg->flags &= ~PG_CLEAN;
1344 if (flags & PGO_FREE) {
1345 /* XXXUBC need better way to update */
1346 lfs_subsys_pages += MIN(1, pages_per_block);
1347 /*
1348 * wire the page so that
1349 * pdaemon don't see it again.
1350 */
1351 uvm_lock_pageq();
1352 uvm_pagewire(pg);
1353 uvm_unlock_pageq();
1354
1355 /* Suspended write flag */
1356 pg->flags |= PG_DELWRI;
1357 }
1358 }
1359 if (pg->flags & PG_WANTED)
1360 wakeup(pg);
1361 pg->flags &= ~(PG_WANTED|PG_BUSY);
1362 UVM_PAGE_OWN(pg, NULL);
1363 }
1364
1365 if (by_list) {
1366 curpg = TAILQ_NEXT(curpg, listq);
1367 } else {
1368 soff += MAX(PAGE_SIZE, fs->lfs_bsize);
1369 }
1370 }
1371 if (by_list) {
1372 PRELE(l);
1373 }
1374
1375 /*
1376 * If any pages were dirty, mark this inode as "pageout requested",
1377 * and put it on the paging queue.
1378 * XXXUBC locking (check locking on dchainhd too)
1379 */
1380 #ifdef notyet
1381 if (any_dirty) {
1382 if (!(ip->i_flags & IN_PAGING)) {
1383 ip->i_flags |= IN_PAGING;
1384 TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip, i_lfs_pchain);
1385 }
1386 }
1387 #endif
1388 return any_dirty;
1389 }
1390
1391 /*
1392 * lfs_putpages functions like genfs_putpages except that
1393 *
1394 * (1) It needs to bounds-check the incoming requests to ensure that
1395 * they are block-aligned; if they are not, expand the range and
1396 * do the right thing in case, e.g., the requested range is clean
1397 * but the expanded range is dirty.
1398 * (2) It needs to explicitly send blocks to be written when it is done.
1399 * VOP_PUTPAGES is not ever called with the seglock held, so
1400 * we simply take the seglock and let lfs_segunlock wait for us.
1401 * XXX Actually we can be called with the seglock held, if we have
1402 * XXX to flush a vnode while lfs_markv is in operation. As of this
1403 * XXX writing we panic in this case.
1404 *
1405 * Assumptions:
1406 *
1407 * (1) The caller does not hold any pages in this vnode busy. If it does,
1408 * there is a danger that when we expand the page range and busy the
1409 * pages we will deadlock.
1410 * (2) We are called with vp->v_interlock held; we must return with it
1411 * released.
1412 * (3) We don't absolutely have to free pages right away, provided that
1413 * the request does not have PGO_SYNCIO. When the pagedaemon gives
1414 * us a request with PGO_FREE, we take the pages out of the paging
1415 * queue and wake up the writer, which will handle freeing them for us.
1416 *
1417 * We ensure that for any filesystem block, all pages for that
1418 * block are either resident or not, even if those pages are higher
1419 * than EOF; that means that we will be getting requests to free
1420 * "unused" pages above EOF all the time, and should ignore them.
1421 */
1422
1423 int
1424 lfs_putpages(void *v)
1425 {
1426 int error;
1427 struct vop_putpages_args /* {
1428 struct vnode *a_vp;
1429 voff_t a_offlo;
1430 voff_t a_offhi;
1431 int a_flags;
1432 } */ *ap = v;
1433 struct vnode *vp;
1434 struct inode *ip;
1435 struct lfs *fs;
1436 struct segment *sp;
1437 off_t origoffset, startoffset, endoffset, origendoffset, blkeof;
1438 off_t max_endoffset;
1439 int pages_per_block;
1440 int s, sync, dirty, pagedaemon;
1441 UVMHIST_FUNC("lfs_putpages"); UVMHIST_CALLED(ubchist);
1442
1443 vp = ap->a_vp;
1444 ip = VTOI(vp);
1445 fs = ip->i_lfs;
1446 sync = (ap->a_flags & PGO_SYNCIO);
1447 pagedaemon = (curproc == uvm.pagedaemon_proc);
1448
1449 /* Putpages does nothing for metadata. */
1450 if (vp == fs->lfs_ivnode || vp->v_type != VREG) {
1451 simple_unlock(&vp->v_interlock);
1452 return 0;
1453 }
1454
1455 /*
1456 * If there are no pages, don't do anything.
1457 */
1458 if (vp->v_uobj.uo_npages == 0) {
1459 s = splbio();
1460 if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL &&
1461 (vp->v_flag & VONWORKLST)) {
1462 vp->v_flag &= ~VONWORKLST;
1463 LIST_REMOVE(vp, v_synclist);
1464 }
1465 splx(s);
1466 simple_unlock(&vp->v_interlock);
1467 return 0;
1468 }
1469
1470 blkeof = blkroundup(fs, ip->i_ffs_size);
1471
1472 /*
1473 * Ignore requests to free pages past EOF but in the same block
1474 * as EOF, unless the request is synchronous. (XXX why sync?)
1475 * XXXUBC Make these pages look "active" so the pagedaemon won't
1476 * XXXUBC bother us with them again.
1477 */
1478 if (!sync && ap->a_offlo >= ip->i_ffs_size && ap->a_offlo < blkeof) {
1479 origoffset = ap->a_offlo;
1480 ap->a_offlo = blkeof;
1481 if (ap->a_offhi > 0 && ap->a_offhi <= ap->a_offlo) {
1482 simple_unlock(&vp->v_interlock);
1483 return 0;
1484 }
1485 }
1486
1487 /*
1488 * Extend page range to start and end at block boundaries.
1489 * (For the purposes of VOP_PUTPAGES, fragments don't exist.)
1490 */
1491 pages_per_block = fs->lfs_bsize >> PAGE_SHIFT;
1492 origoffset = ap->a_offlo;
1493 origendoffset = ap->a_offhi;
1494 startoffset = origoffset & ~(fs->lfs_bmask);
1495 max_endoffset = (trunc_page(LLONG_MAX) >> fs->lfs_bshift)
1496 << fs->lfs_bshift;
1497
1498 if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
1499 endoffset = max_endoffset;
1500 origendoffset = endoffset;
1501 } else {
1502 origendoffset = round_page(ap->a_offhi);
1503 endoffset = round_page(blkroundup(fs, origendoffset));
1504 }
1505
1506 KASSERT(startoffset > 0 || endoffset >= startoffset);
1507 if (startoffset == endoffset) {
1508 /* Nothing to do, why were we called? */
1509 simple_unlock(&vp->v_interlock);
1510 #ifdef DEBUG
1511 printf("lfs_putpages: startoffset = endoffset = %" PRId64 "\n",
1512 startoffset);
1513 #endif
1514 return 0;
1515 }
1516
1517 ap->a_offlo = startoffset;
1518 ap->a_offhi = endoffset;
1519
1520 if (!(ap->a_flags & PGO_CLEANIT))
1521 return genfs_putpages(v);
1522
1523 /*
1524 * Make sure that all pages in any given block are dirty, or
1525 * none of them are. Find out if any of the pages we've been
1526 * asked about are dirty. If none are dirty, send them on
1527 * through genfs_putpages(), albeit with adjusted offsets.
1528 * XXXUBC I am assuming here that they can't be dirtied in
1529 * XXXUBC the meantime, but I bet that's wrong.
1530 */
1531 dirty = check_dirty(fs, vp, startoffset, endoffset, blkeof, ap->a_flags);
1532 if (!dirty)
1533 return genfs_putpages(v);
1534
1535 /*
1536 * Dirty and asked to clean.
1537 *
1538 * Pagedaemon can't actually write LFS pages; wake up
1539 * the writer to take care of that. The writer will
1540 * notice the pager inode queue and act on that.
1541 */
1542 if (pagedaemon) {
1543 ++fs->lfs_pdflush;
1544 wakeup(&lfs_writer_daemon);
1545 simple_unlock(&vp->v_interlock);
1546 return EWOULDBLOCK;
1547 }
1548
1549 /*
1550 * If this is a file created in a recent dirop, we can't flush its
1551 * inode until the dirop is complete. Drain dirops, then flush the
1552 * filesystem (taking care of any other pending dirops while we're
1553 * at it).
1554 */
1555 if ((ap->a_flags & (PGO_CLEANIT|PGO_LOCKED)) == PGO_CLEANIT &&
1556 (vp->v_flag & VDIROP)) {
1557 int locked;
1558
1559 /* printf("putpages to clean VDIROP, flushing\n"); */
1560 while (fs->lfs_dirops > 0) {
1561 ++fs->lfs_diropwait;
1562 tsleep(&fs->lfs_writer, PRIBIO+1, "ppdirop", 0);
1563 --fs->lfs_diropwait;
1564 }
1565 ++fs->lfs_writer;
1566 locked = VOP_ISLOCKED(vp) && /* XXX */
1567 vp->v_lock.lk_lockholder == curproc->p_pid;
1568 if (locked)
1569 VOP_UNLOCK(vp, 0);
1570 simple_unlock(&vp->v_interlock);
1571
1572 lfs_flush_fs(fs, sync ? SEGM_SYNC : 0);
1573
1574 simple_lock(&vp->v_interlock);
1575 if (locked)
1576 VOP_LOCK(vp, LK_EXCLUSIVE);
1577 if (--fs->lfs_writer == 0)
1578 wakeup(&fs->lfs_dirops);
1579
1580 /* XXX the flush should have taken care of this one too! */
1581 }
1582
1583
1584 /*
1585 * This is it. We are going to write some pages. From here on
1586 * down it's all just mechanics.
1587 *
1588 * If there are more than one page per block, we don't want to get
1589 * caught locking them backwards; so set PGO_BUSYFAIL to avoid
1590 * deadlocks. Also, don't let genfs_putpages wait;
1591 * lfs_segunlock will wait for us, if need be.
1592 */
1593 ap->a_flags &= ~PGO_SYNCIO;
1594 if (pages_per_block > 1)
1595 ap->a_flags |= PGO_BUSYFAIL;
1596
1597 /*
1598 * If we've already got the seglock, flush the node and return.
1599 * The FIP has already been set up for us by lfs_writefile,
1600 * and FIP cleanup and lfs_updatemeta will also be done there,
1601 * unless genfs_putpages returns EDEADLK; then we must flush
1602 * what we have, and correct FIP and segment header accounting.
1603 */
1604 if (ap->a_flags & PGO_LOCKED) {
1605 sp = fs->lfs_sp;
1606 sp->vp = vp;
1607
1608 while ((error = genfs_putpages(v)) == EDEADLK) {
1609 #ifdef DEBUG_LFS
1610 printf("lfs_putpages: genfs_putpages returned EDEADLK"
1611 " ino %d off %x (seg %d)\n",
1612 ip->i_number, fs->lfs_offset,
1613 dtosn(fs, fs->lfs_offset));
1614 #endif
1615 /* If nothing to write, short-circuit */
1616 if (sp->cbpp - sp->bpp == 1) {
1617 preempt(NULL);
1618 simple_lock(&vp->v_interlock);
1619 continue;
1620 }
1621 /* Write gathered pages */
1622 lfs_updatemeta(sp);
1623 (void) lfs_writeseg(fs, sp);
1624
1625 /* Reinitialize brand new FIP and add us to it */
1626 sp->vp = vp;
1627 sp->fip->fi_version = ip->i_ffs_gen;
1628 sp->fip->fi_ino = ip->i_number;
1629 /* Add us to the new segment summary. */
1630 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
1631 sp->sum_bytes_left -=
1632 sizeof(struct finfo) - sizeof(int32_t);
1633
1634 /* Give the write a chance to complete */
1635 preempt(NULL);
1636 simple_lock(&vp->v_interlock);
1637 }
1638 return error;
1639 }
1640
1641 simple_unlock(&vp->v_interlock);
1642 /*
1643 * Take the seglock, because we are going to be writing pages.
1644 */
1645 if ((error = lfs_seglock(fs, SEGM_PROT | (sync ? SEGM_SYNC : 0))) != 0)
1646 return error;
1647
1648 /*
1649 * VOP_PUTPAGES should not be called while holding the seglock.
1650 * XXXUBC fix lfs_markv, or do this properly.
1651 */
1652 /* KASSERT(fs->lfs_seglock == 1); */
1653
1654 /*
1655 * We assume we're being called with sp->fip pointing at blank space.
1656 * Account for a new FIP in the segment header, and set sp->vp.
1657 * (This should duplicate the setup at the top of lfs_writefile().)
1658 */
1659 sp = fs->lfs_sp;
1660 if (sp->seg_bytes_left < fs->lfs_bsize ||
1661 sp->sum_bytes_left < sizeof(struct finfo))
1662 (void) lfs_writeseg(fs, fs->lfs_sp);
1663
1664 sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(int32_t);
1665 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
1666 sp->vp = vp;
1667
1668 if (vp->v_flag & VDIROP)
1669 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
1670
1671 sp->fip->fi_nblocks = 0;
1672 sp->fip->fi_ino = ip->i_number;
1673 sp->fip->fi_version = ip->i_ffs_gen;
1674
1675 /*
1676 * Loop through genfs_putpages until all pages are gathered.
1677 * genfs_putpages() drops the interlock, so reacquire it if necessary.
1678 */
1679 simple_lock(&vp->v_interlock);
1680 while ((error = genfs_putpages(v)) == EDEADLK) {
1681 #ifdef DEBUG_LFS
1682 printf("lfs_putpages: genfs_putpages returned EDEADLK [2]"
1683 " ino %d off %x (seg %d)\n",
1684 ip->i_number, fs->lfs_offset,
1685 dtosn(fs, fs->lfs_offset));
1686 #endif
1687 /* If nothing to write, short-circuit */
1688 if (sp->cbpp - sp->bpp == 1) {
1689 preempt(NULL);
1690 simple_lock(&vp->v_interlock);
1691 continue;
1692 }
1693 /* Write gathered pages */
1694 lfs_updatemeta(sp);
1695 (void) lfs_writeseg(fs, sp);
1696
1697 /*
1698 * Reinitialize brand new FIP and add us to it.
1699 * (This should duplicate the fixup in lfs_gatherpages().)
1700 */
1701 sp->vp = vp;
1702 sp->fip->fi_version = ip->i_ffs_gen;
1703 sp->fip->fi_ino = ip->i_number;
1704 /* Add us to the new segment summary. */
1705 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
1706 sp->sum_bytes_left -=
1707 sizeof(struct finfo) - sizeof(int32_t);
1708
1709 /* Give the write a chance to complete */
1710 preempt(NULL);
1711 simple_lock(&vp->v_interlock);
1712 }
1713
1714 /*
1715 * Blocks are now gathered into a segment waiting to be written.
1716 * All that's left to do is update metadata, and write them.
1717 */
1718 lfs_updatemeta(fs->lfs_sp);
1719 fs->lfs_sp->vp = NULL;
1720 /*
1721 * Clean up FIP, since we're done writing this file.
1722 * This should duplicate cleanup at the end of lfs_writefile().
1723 */
1724 if (sp->fip->fi_nblocks != 0) {
1725 sp->fip = (FINFO*)((caddr_t)sp->fip + sizeof(struct finfo) +
1726 sizeof(int32_t) * (sp->fip->fi_nblocks - 1));
1727 sp->start_lbp = &sp->fip->fi_blocks[0];
1728 } else {
1729 sp->sum_bytes_left += sizeof(FINFO) - sizeof(int32_t);
1730 --((SEGSUM *)(sp->segsum))->ss_nfinfo;
1731 }
1732 lfs_writeseg(fs, fs->lfs_sp);
1733
1734 /*
1735 * XXX - with the malloc/copy writeseg, the pages are freed by now
1736 * even if we don't wait (e.g. if we hold a nested lock). This
1737 * will not be true if we stop using malloc/copy.
1738 */
1739 KASSERT(fs->lfs_sp->seg_flags & SEGM_PROT);
1740 lfs_segunlock(fs);
1741
1742 /*
1743 * Wait for v_numoutput to drop to zero. The seglock should
1744 * take care of this, but there is a slight possibility that
1745 * aiodoned might not have got around to our buffers yet.
1746 */
1747 if (sync) {
1748 int s;
1749
1750 s = splbio();
1751 simple_lock(&global_v_numoutput_slock);
1752 while(vp->v_numoutput > 0) {
1753 #ifdef DEBUG
1754 printf("ino %d sleeping on num %d\n",
1755 ip->i_number, vp->v_numoutput);
1756 #endif
1757 vp->v_flag |= VBWAIT;
1758 ltsleep(&vp->v_numoutput, PRIBIO + 1, "lfs_vn", 0,
1759 &global_v_numoutput_slock);
1760 }
1761 simple_unlock(&global_v_numoutput_slock);
1762 splx(s);
1763 }
1764 return error;
1765 }
1766
1767 /*
1768 * Find out whether the vnode has any blocks or pages waiting to be written.
1769 * We used to just check LIST_EMPTY(&vp->v_dirtyblkhd), but there is not
1770 * presently as simple a mechanism for the page cache.
1771 */
1772 int
1773 lfs_checkifempty(struct vnode *vp)
1774 {
1775 struct vm_page *pg;
1776 struct buf *bp;
1777 int r, s;
1778
1779 if (vp->v_type != VREG || VTOI(vp)->i_number == LFS_IFILE_INUM)
1780 return LIST_EMPTY(&vp->v_dirtyblkhd);
1781
1782 /*
1783 * For vnodes with pages it is a little more complex.
1784 * Pages that have been written (i.e. are "clean" for our purposes)
1785 * might be in seemingly dirty buffers, so we have to troll
1786 * looking for indirect block buffers as well as pages.
1787 */
1788 simple_lock(&vp->v_interlock);
1789 s = splbio();
1790 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp;
1791 bp = LIST_NEXT(bp, b_vnbufs)) {
1792 if (bp->b_lblkno < 0) {
1793 splx(s);
1794 simple_unlock(&vp->v_interlock);
1795 return 0;
1796 }
1797 }
1798 splx(s);
1799
1800 /*
1801 * Run through the page list to find dirty pages.
1802 * Right now I just walk the memq.
1803 */
1804 pg = TAILQ_FIRST(&vp->v_uobj.memq);
1805 r = 1;
1806 while(pg) {
1807 if ((pg->flags & PG_CLEAN) == 0 || pmap_is_modified(pg)) {
1808 r = 0;
1809 break;
1810 }
1811 pg = TAILQ_NEXT(pg, listq);
1812 }
1813 #if 0
1814 if (r != !(vp->v_flag & VONWORKLST)) {
1815 printf("nope, VONWORKLST isn't good enough!\n");
1816 }
1817 #endif
1818 simple_unlock(&vp->v_interlock);
1819 return r;
1820 }
1821
1822 /*
1823 * Return the last logical file offset that should be written for this file
1824 * if we're doing a write that ends at "size". If writing, we need to know
1825 * about sizes on disk, i.e. fragments if there are any; if reading, we need
1826 * to know about entire blocks.
1827 */
1828 void
1829 lfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
1830 {
1831 struct inode *ip = VTOI(vp);
1832 struct lfs *fs = ip->i_lfs;
1833 daddr_t olbn, nlbn;
1834
1835 KASSERT(flags & (GOP_SIZE_READ | GOP_SIZE_WRITE));
1836 KASSERT((flags & (GOP_SIZE_READ | GOP_SIZE_WRITE))
1837 != (GOP_SIZE_READ | GOP_SIZE_WRITE));
1838
1839 olbn = lblkno(fs, ip->i_ffs_size);
1840 nlbn = lblkno(fs, size);
1841 if ((flags & GOP_SIZE_WRITE) && nlbn < NDADDR && olbn <= nlbn) {
1842 *eobp = fragroundup(fs, size);
1843 } else {
1844 *eobp = blkroundup(fs, size);
1845 }
1846 }
1847
1848 #ifdef DEBUG
1849 void lfs_dump_vop(void *);
1850
1851 void
1852 lfs_dump_vop(void *v)
1853 {
1854 struct vop_putpages_args /* {
1855 struct vnode *a_vp;
1856 voff_t a_offlo;
1857 voff_t a_offhi;
1858 int a_flags;
1859 } */ *ap = v;
1860
1861 vfs_vnode_print(ap->a_vp, 0, printf);
1862 lfs_dump_dinode(&VTOI(ap->a_vp)->i_din.ffs_din);
1863 }
1864 #endif
1865
1866 int
1867 lfs_mmap(void *v)
1868 {
1869 struct vop_mmap_args /* {
1870 const struct vnodeop_desc *a_desc;
1871 struct vnode *a_vp;
1872 int a_fflags;
1873 struct ucred *a_cred;
1874 struct proc *a_p;
1875 } */ *ap = v;
1876
1877 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM)
1878 return EOPNOTSUPP;
1879 return ufs_mmap(v);
1880 }
1881 #endif /* LFS_UBC */
1882