lfs_vnops.c revision 1.232 1 /* $NetBSD: lfs_vnops.c,v 1.232 2010/12/18 00:01:46 eeh 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 /*
32 * Copyright (c) 1986, 1989, 1991, 1993, 1995
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * @(#)lfs_vnops.c 8.13 (Berkeley) 6/10/95
60 */
61
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: lfs_vnops.c,v 1.232 2010/12/18 00:01:46 eeh Exp $");
64
65 #ifdef _KERNEL_OPT
66 #include "opt_compat_netbsd.h"
67 #endif
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/namei.h>
72 #include <sys/resourcevar.h>
73 #include <sys/kernel.h>
74 #include <sys/file.h>
75 #include <sys/stat.h>
76 #include <sys/buf.h>
77 #include <sys/proc.h>
78 #include <sys/mount.h>
79 #include <sys/vnode.h>
80 #include <sys/pool.h>
81 #include <sys/signalvar.h>
82 #include <sys/kauth.h>
83 #include <sys/syslog.h>
84 #include <sys/fstrans.h>
85
86 #include <miscfs/fifofs/fifo.h>
87 #include <miscfs/genfs/genfs.h>
88 #include <miscfs/specfs/specdev.h>
89
90 #include <ufs/ufs/inode.h>
91 #include <ufs/ufs/dir.h>
92 #include <ufs/ufs/ufsmount.h>
93 #include <ufs/ufs/ufs_extern.h>
94
95 #include <uvm/uvm.h>
96 #include <uvm/uvm_pmap.h>
97 #include <uvm/uvm_stat.h>
98 #include <uvm/uvm_pager.h>
99
100 #include <ufs/lfs/lfs.h>
101 #include <ufs/lfs/lfs_extern.h>
102
103 extern pid_t lfs_writer_daemon;
104 int lfs_ignore_lazy_sync = 1;
105
106 /* Global vfs data structures for lfs. */
107 int (**lfs_vnodeop_p)(void *);
108 const struct vnodeopv_entry_desc lfs_vnodeop_entries[] = {
109 { &vop_default_desc, vn_default_error },
110 { &vop_lookup_desc, ufs_lookup }, /* lookup */
111 { &vop_create_desc, lfs_create }, /* create */
112 { &vop_whiteout_desc, ufs_whiteout }, /* whiteout */
113 { &vop_mknod_desc, lfs_mknod }, /* mknod */
114 { &vop_open_desc, ufs_open }, /* open */
115 { &vop_close_desc, lfs_close }, /* close */
116 { &vop_access_desc, ufs_access }, /* access */
117 { &vop_getattr_desc, lfs_getattr }, /* getattr */
118 { &vop_setattr_desc, lfs_setattr }, /* setattr */
119 { &vop_read_desc, lfs_read }, /* read */
120 { &vop_write_desc, lfs_write }, /* write */
121 { &vop_ioctl_desc, ufs_ioctl }, /* ioctl */
122 { &vop_fcntl_desc, lfs_fcntl }, /* fcntl */
123 { &vop_poll_desc, ufs_poll }, /* poll */
124 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
125 { &vop_revoke_desc, ufs_revoke }, /* revoke */
126 { &vop_mmap_desc, lfs_mmap }, /* mmap */
127 { &vop_fsync_desc, lfs_fsync }, /* fsync */
128 { &vop_seek_desc, ufs_seek }, /* seek */
129 { &vop_remove_desc, lfs_remove }, /* remove */
130 { &vop_link_desc, lfs_link }, /* link */
131 { &vop_rename_desc, lfs_rename }, /* rename */
132 { &vop_mkdir_desc, lfs_mkdir }, /* mkdir */
133 { &vop_rmdir_desc, lfs_rmdir }, /* rmdir */
134 { &vop_symlink_desc, lfs_symlink }, /* symlink */
135 { &vop_readdir_desc, ufs_readdir }, /* readdir */
136 { &vop_readlink_desc, ufs_readlink }, /* readlink */
137 { &vop_abortop_desc, ufs_abortop }, /* abortop */
138 { &vop_inactive_desc, lfs_inactive }, /* inactive */
139 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
140 { &vop_lock_desc, ufs_lock }, /* lock */
141 { &vop_unlock_desc, ufs_unlock }, /* unlock */
142 { &vop_bmap_desc, ufs_bmap }, /* bmap */
143 { &vop_strategy_desc, lfs_strategy }, /* strategy */
144 { &vop_print_desc, ufs_print }, /* print */
145 { &vop_islocked_desc, ufs_islocked }, /* islocked */
146 { &vop_pathconf_desc, ufs_pathconf }, /* pathconf */
147 { &vop_advlock_desc, ufs_advlock }, /* advlock */
148 { &vop_bwrite_desc, lfs_bwrite }, /* bwrite */
149 { &vop_getpages_desc, lfs_getpages }, /* getpages */
150 { &vop_putpages_desc, lfs_putpages }, /* putpages */
151 { NULL, NULL }
152 };
153 const struct vnodeopv_desc lfs_vnodeop_opv_desc =
154 { &lfs_vnodeop_p, lfs_vnodeop_entries };
155
156 int (**lfs_specop_p)(void *);
157 const struct vnodeopv_entry_desc lfs_specop_entries[] = {
158 { &vop_default_desc, vn_default_error },
159 { &vop_lookup_desc, spec_lookup }, /* lookup */
160 { &vop_create_desc, spec_create }, /* create */
161 { &vop_mknod_desc, spec_mknod }, /* mknod */
162 { &vop_open_desc, spec_open }, /* open */
163 { &vop_close_desc, lfsspec_close }, /* close */
164 { &vop_access_desc, ufs_access }, /* access */
165 { &vop_getattr_desc, lfs_getattr }, /* getattr */
166 { &vop_setattr_desc, lfs_setattr }, /* setattr */
167 { &vop_read_desc, ufsspec_read }, /* read */
168 { &vop_write_desc, ufsspec_write }, /* write */
169 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
170 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
171 { &vop_poll_desc, spec_poll }, /* poll */
172 { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
173 { &vop_revoke_desc, spec_revoke }, /* revoke */
174 { &vop_mmap_desc, spec_mmap }, /* mmap */
175 { &vop_fsync_desc, spec_fsync }, /* fsync */
176 { &vop_seek_desc, spec_seek }, /* seek */
177 { &vop_remove_desc, spec_remove }, /* remove */
178 { &vop_link_desc, spec_link }, /* link */
179 { &vop_rename_desc, spec_rename }, /* rename */
180 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
181 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
182 { &vop_symlink_desc, spec_symlink }, /* symlink */
183 { &vop_readdir_desc, spec_readdir }, /* readdir */
184 { &vop_readlink_desc, spec_readlink }, /* readlink */
185 { &vop_abortop_desc, spec_abortop }, /* abortop */
186 { &vop_inactive_desc, lfs_inactive }, /* inactive */
187 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
188 { &vop_lock_desc, ufs_lock }, /* lock */
189 { &vop_unlock_desc, ufs_unlock }, /* unlock */
190 { &vop_bmap_desc, spec_bmap }, /* bmap */
191 { &vop_strategy_desc, spec_strategy }, /* strategy */
192 { &vop_print_desc, ufs_print }, /* print */
193 { &vop_islocked_desc, ufs_islocked }, /* islocked */
194 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
195 { &vop_advlock_desc, spec_advlock }, /* advlock */
196 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
197 { &vop_getpages_desc, spec_getpages }, /* getpages */
198 { &vop_putpages_desc, spec_putpages }, /* putpages */
199 { NULL, NULL }
200 };
201 const struct vnodeopv_desc lfs_specop_opv_desc =
202 { &lfs_specop_p, lfs_specop_entries };
203
204 int (**lfs_fifoop_p)(void *);
205 const struct vnodeopv_entry_desc lfs_fifoop_entries[] = {
206 { &vop_default_desc, vn_default_error },
207 { &vop_lookup_desc, vn_fifo_bypass }, /* lookup */
208 { &vop_create_desc, vn_fifo_bypass }, /* create */
209 { &vop_mknod_desc, vn_fifo_bypass }, /* mknod */
210 { &vop_open_desc, vn_fifo_bypass }, /* open */
211 { &vop_close_desc, lfsfifo_close }, /* close */
212 { &vop_access_desc, ufs_access }, /* access */
213 { &vop_getattr_desc, lfs_getattr }, /* getattr */
214 { &vop_setattr_desc, lfs_setattr }, /* setattr */
215 { &vop_read_desc, ufsfifo_read }, /* read */
216 { &vop_write_desc, ufsfifo_write }, /* write */
217 { &vop_ioctl_desc, vn_fifo_bypass }, /* ioctl */
218 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
219 { &vop_poll_desc, vn_fifo_bypass }, /* poll */
220 { &vop_kqfilter_desc, vn_fifo_bypass }, /* kqfilter */
221 { &vop_revoke_desc, vn_fifo_bypass }, /* revoke */
222 { &vop_mmap_desc, vn_fifo_bypass }, /* mmap */
223 { &vop_fsync_desc, vn_fifo_bypass }, /* fsync */
224 { &vop_seek_desc, vn_fifo_bypass }, /* seek */
225 { &vop_remove_desc, vn_fifo_bypass }, /* remove */
226 { &vop_link_desc, vn_fifo_bypass }, /* link */
227 { &vop_rename_desc, vn_fifo_bypass }, /* rename */
228 { &vop_mkdir_desc, vn_fifo_bypass }, /* mkdir */
229 { &vop_rmdir_desc, vn_fifo_bypass }, /* rmdir */
230 { &vop_symlink_desc, vn_fifo_bypass }, /* symlink */
231 { &vop_readdir_desc, vn_fifo_bypass }, /* readdir */
232 { &vop_readlink_desc, vn_fifo_bypass }, /* readlink */
233 { &vop_abortop_desc, vn_fifo_bypass }, /* abortop */
234 { &vop_inactive_desc, lfs_inactive }, /* inactive */
235 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
236 { &vop_lock_desc, ufs_lock }, /* lock */
237 { &vop_unlock_desc, ufs_unlock }, /* unlock */
238 { &vop_bmap_desc, vn_fifo_bypass }, /* bmap */
239 { &vop_strategy_desc, vn_fifo_bypass }, /* strategy */
240 { &vop_print_desc, ufs_print }, /* print */
241 { &vop_islocked_desc, ufs_islocked }, /* islocked */
242 { &vop_pathconf_desc, vn_fifo_bypass }, /* pathconf */
243 { &vop_advlock_desc, vn_fifo_bypass }, /* advlock */
244 { &vop_bwrite_desc, lfs_bwrite }, /* bwrite */
245 { &vop_putpages_desc, vn_fifo_bypass }, /* putpages */
246 { NULL, NULL }
247 };
248 const struct vnodeopv_desc lfs_fifoop_opv_desc =
249 { &lfs_fifoop_p, lfs_fifoop_entries };
250
251 static int check_dirty(struct lfs *, struct vnode *, off_t, off_t, off_t, int, int, struct vm_page **);
252
253 #define LFS_READWRITE
254 #include <ufs/ufs/ufs_readwrite.c>
255 #undef LFS_READWRITE
256
257 /*
258 * Synch an open file.
259 */
260 /* ARGSUSED */
261 int
262 lfs_fsync(void *v)
263 {
264 struct vop_fsync_args /* {
265 struct vnode *a_vp;
266 kauth_cred_t a_cred;
267 int a_flags;
268 off_t offlo;
269 off_t offhi;
270 } */ *ap = v;
271 struct vnode *vp = ap->a_vp;
272 int error, wait;
273 struct inode *ip = VTOI(vp);
274 struct lfs *fs = ip->i_lfs;
275
276 /* If we're mounted read-only, don't try to sync. */
277 if (fs->lfs_ronly)
278 return 0;
279
280 /* If a removed vnode is being cleaned, no need to sync here. */
281 if ((ap->a_flags & FSYNC_RECLAIM) != 0 && ip->i_mode == 0)
282 return 0;
283
284 /*
285 * Trickle sync simply adds this vnode to the pager list, as if
286 * the pagedaemon had requested a pageout.
287 */
288 if (ap->a_flags & FSYNC_LAZY) {
289 if (lfs_ignore_lazy_sync == 0) {
290 mutex_enter(&lfs_lock);
291 if (!(ip->i_flags & IN_PAGING)) {
292 ip->i_flags |= IN_PAGING;
293 TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip,
294 i_lfs_pchain);
295 }
296 wakeup(&lfs_writer_daemon);
297 mutex_exit(&lfs_lock);
298 }
299 return 0;
300 }
301
302 /*
303 * If a vnode is bring cleaned, flush it out before we try to
304 * reuse it. This prevents the cleaner from writing files twice
305 * in the same partial segment, causing an accounting underflow.
306 */
307 if (ap->a_flags & FSYNC_RECLAIM && ip->i_flags & IN_CLEANING) {
308 lfs_vflush(vp);
309 }
310
311 wait = (ap->a_flags & FSYNC_WAIT);
312 do {
313 mutex_enter(&vp->v_interlock);
314 error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
315 round_page(ap->a_offhi),
316 PGO_CLEANIT | (wait ? PGO_SYNCIO : 0));
317 if (error == EAGAIN) {
318 mutex_enter(&lfs_lock);
319 mtsleep(&fs->lfs_avail, PCATCH | PUSER, "lfs_fsync",
320 hz / 100 + 1, &lfs_lock);
321 mutex_exit(&lfs_lock);
322 }
323 } while (error == EAGAIN);
324 if (error)
325 return error;
326
327 if ((ap->a_flags & FSYNC_DATAONLY) == 0)
328 error = lfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
329
330 if (error == 0 && ap->a_flags & FSYNC_CACHE) {
331 int l = 0;
332 error = VOP_IOCTL(ip->i_devvp, DIOCCACHESYNC, &l, FWRITE,
333 curlwp->l_cred);
334 }
335 if (wait && !VPISEMPTY(vp))
336 LFS_SET_UINO(ip, IN_MODIFIED);
337
338 return error;
339 }
340
341 /*
342 * Take IN_ADIROP off, then call ufs_inactive.
343 */
344 int
345 lfs_inactive(void *v)
346 {
347 struct vop_inactive_args /* {
348 struct vnode *a_vp;
349 } */ *ap = v;
350
351 lfs_unmark_vnode(ap->a_vp);
352
353 /*
354 * The Ifile is only ever inactivated on unmount.
355 * Streamline this process by not giving it more dirty blocks.
356 */
357 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM) {
358 mutex_enter(&lfs_lock);
359 LFS_CLR_UINO(VTOI(ap->a_vp), IN_ALLMOD);
360 mutex_exit(&lfs_lock);
361 VOP_UNLOCK(ap->a_vp);
362 return 0;
363 }
364
365 return ufs_inactive(v);
366 }
367
368 /*
369 * These macros are used to bracket UFS directory ops, so that we can
370 * identify all the pages touched during directory ops which need to
371 * be ordered and flushed atomically, so that they may be recovered.
372 *
373 * Because we have to mark nodes VU_DIROP in order to prevent
374 * the cache from reclaiming them while a dirop is in progress, we must
375 * also manage the number of nodes so marked (otherwise we can run out).
376 * We do this by setting lfs_dirvcount to the number of marked vnodes; it
377 * is decremented during segment write, when VU_DIROP is taken off.
378 */
379 #define MARK_VNODE(vp) lfs_mark_vnode(vp)
380 #define UNMARK_VNODE(vp) lfs_unmark_vnode(vp)
381 #define SET_DIROP_CREATE(dvp, vpp) lfs_set_dirop_create((dvp), (vpp))
382 #define SET_DIROP_REMOVE(dvp, vp) lfs_set_dirop((dvp), (vp))
383 static int lfs_set_dirop_create(struct vnode *, struct vnode **);
384 static int lfs_set_dirop(struct vnode *, struct vnode *);
385
386 static int
387 lfs_set_dirop(struct vnode *dvp, struct vnode *vp)
388 {
389 struct lfs *fs;
390 int error;
391
392 KASSERT(VOP_ISLOCKED(dvp));
393 KASSERT(vp == NULL || VOP_ISLOCKED(vp));
394
395 fs = VTOI(dvp)->i_lfs;
396
397 ASSERT_NO_SEGLOCK(fs);
398 /*
399 * LFS_NRESERVE calculates direct and indirect blocks as well
400 * as an inode block; an overestimate in most cases.
401 */
402 if ((error = lfs_reserve(fs, dvp, vp, LFS_NRESERVE(fs))) != 0)
403 return (error);
404
405 restart:
406 mutex_enter(&lfs_lock);
407 if (fs->lfs_dirops == 0) {
408 mutex_exit(&lfs_lock);
409 lfs_check(dvp, LFS_UNUSED_LBN, 0);
410 mutex_enter(&lfs_lock);
411 }
412 while (fs->lfs_writer) {
413 error = mtsleep(&fs->lfs_dirops, (PRIBIO + 1) | PCATCH,
414 "lfs_sdirop", 0, &lfs_lock);
415 if (error == EINTR) {
416 mutex_exit(&lfs_lock);
417 goto unreserve;
418 }
419 }
420 if (lfs_dirvcount > LFS_MAX_DIROP && fs->lfs_dirops == 0) {
421 wakeup(&lfs_writer_daemon);
422 mutex_exit(&lfs_lock);
423 preempt();
424 goto restart;
425 }
426
427 if (lfs_dirvcount > LFS_MAX_DIROP) {
428 mutex_exit(&lfs_lock);
429 DLOG((DLOG_DIROP, "lfs_set_dirop: sleeping with dirops=%d, "
430 "dirvcount=%d\n", fs->lfs_dirops, lfs_dirvcount));
431 if ((error = mtsleep(&lfs_dirvcount,
432 PCATCH | PUSER | PNORELOCK, "lfs_maxdirop", 0,
433 &lfs_lock)) != 0) {
434 goto unreserve;
435 }
436 goto restart;
437 }
438
439 ++fs->lfs_dirops;
440 fs->lfs_doifile = 1;
441 mutex_exit(&lfs_lock);
442
443 /* Hold a reference so SET_ENDOP will be happy */
444 vref(dvp);
445 if (vp) {
446 vref(vp);
447 MARK_VNODE(vp);
448 }
449
450 MARK_VNODE(dvp);
451 return 0;
452
453 unreserve:
454 lfs_reserve(fs, dvp, vp, -LFS_NRESERVE(fs));
455 return error;
456 }
457
458 /*
459 * Get a new vnode *before* adjusting the dirop count, to avoid a deadlock
460 * in getnewvnode(), if we have a stacked filesystem mounted on top
461 * of us.
462 *
463 * NB: this means we have to clear the new vnodes on error. Fortunately
464 * SET_ENDOP is there to do that for us.
465 */
466 static int
467 lfs_set_dirop_create(struct vnode *dvp, struct vnode **vpp)
468 {
469 int error;
470 struct lfs *fs;
471
472 fs = VFSTOUFS(dvp->v_mount)->um_lfs;
473 ASSERT_NO_SEGLOCK(fs);
474 if (fs->lfs_ronly)
475 return EROFS;
476 if (vpp && (error = getnewvnode(VT_LFS, dvp->v_mount, lfs_vnodeop_p, vpp))) {
477 DLOG((DLOG_ALLOC, "lfs_set_dirop_create: dvp %p error %d\n",
478 dvp, error));
479 return error;
480 }
481 if ((error = lfs_set_dirop(dvp, NULL)) != 0) {
482 if (vpp) {
483 ungetnewvnode(*vpp);
484 *vpp = NULL;
485 }
486 return error;
487 }
488 return 0;
489 }
490
491 #define SET_ENDOP_BASE(fs, dvp, str) \
492 do { \
493 mutex_enter(&lfs_lock); \
494 --(fs)->lfs_dirops; \
495 if (!(fs)->lfs_dirops) { \
496 if ((fs)->lfs_nadirop) { \
497 panic("SET_ENDOP: %s: no dirops but " \
498 " nadirop=%d", (str), \
499 (fs)->lfs_nadirop); \
500 } \
501 wakeup(&(fs)->lfs_writer); \
502 mutex_exit(&lfs_lock); \
503 lfs_check((dvp), LFS_UNUSED_LBN, 0); \
504 } else \
505 mutex_exit(&lfs_lock); \
506 } while(0)
507 #define SET_ENDOP_CREATE(fs, dvp, nvpp, str) \
508 do { \
509 UNMARK_VNODE(dvp); \
510 if (nvpp && *nvpp) \
511 UNMARK_VNODE(*nvpp); \
512 /* Check for error return to stem vnode leakage */ \
513 if (nvpp && *nvpp && !((*nvpp)->v_uflag & VU_DIROP)) \
514 ungetnewvnode(*(nvpp)); \
515 SET_ENDOP_BASE((fs), (dvp), (str)); \
516 lfs_reserve((fs), (dvp), NULL, -LFS_NRESERVE(fs)); \
517 vrele(dvp); \
518 } while(0)
519 #define SET_ENDOP_CREATE_AP(ap, str) \
520 SET_ENDOP_CREATE(VTOI((ap)->a_dvp)->i_lfs, (ap)->a_dvp, \
521 (ap)->a_vpp, (str))
522 #define SET_ENDOP_REMOVE(fs, dvp, ovp, str) \
523 do { \
524 UNMARK_VNODE(dvp); \
525 if (ovp) \
526 UNMARK_VNODE(ovp); \
527 SET_ENDOP_BASE((fs), (dvp), (str)); \
528 lfs_reserve((fs), (dvp), (ovp), -LFS_NRESERVE(fs)); \
529 vrele(dvp); \
530 if (ovp) \
531 vrele(ovp); \
532 } while(0)
533
534 void
535 lfs_mark_vnode(struct vnode *vp)
536 {
537 struct inode *ip = VTOI(vp);
538 struct lfs *fs = ip->i_lfs;
539
540 mutex_enter(&lfs_lock);
541 if (!(ip->i_flag & IN_ADIROP)) {
542 if (!(vp->v_uflag & VU_DIROP)) {
543 mutex_enter(&vp->v_interlock);
544 (void)lfs_vref(vp);
545 ++lfs_dirvcount;
546 ++fs->lfs_dirvcount;
547 TAILQ_INSERT_TAIL(&fs->lfs_dchainhd, ip, i_lfs_dchain);
548 vp->v_uflag |= VU_DIROP;
549 }
550 ++fs->lfs_nadirop;
551 ip->i_flag |= IN_ADIROP;
552 } else
553 KASSERT(vp->v_uflag & VU_DIROP);
554 mutex_exit(&lfs_lock);
555 }
556
557 void
558 lfs_unmark_vnode(struct vnode *vp)
559 {
560 struct inode *ip = VTOI(vp);
561
562 if (ip && (ip->i_flag & IN_ADIROP)) {
563 KASSERT(vp->v_uflag & VU_DIROP);
564 mutex_enter(&lfs_lock);
565 --ip->i_lfs->lfs_nadirop;
566 mutex_exit(&lfs_lock);
567 ip->i_flag &= ~IN_ADIROP;
568 }
569 }
570
571 int
572 lfs_symlink(void *v)
573 {
574 struct vop_symlink_args /* {
575 struct vnode *a_dvp;
576 struct vnode **a_vpp;
577 struct componentname *a_cnp;
578 struct vattr *a_vap;
579 char *a_target;
580 } */ *ap = v;
581 int error;
582
583 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
584 vput(ap->a_dvp);
585 return error;
586 }
587 error = ufs_symlink(ap);
588 SET_ENDOP_CREATE_AP(ap, "symlink");
589 return (error);
590 }
591
592 int
593 lfs_mknod(void *v)
594 {
595 struct vop_mknod_args /* {
596 struct vnode *a_dvp;
597 struct vnode **a_vpp;
598 struct componentname *a_cnp;
599 struct vattr *a_vap;
600 } */ *ap = v;
601 struct vattr *vap = ap->a_vap;
602 struct vnode **vpp = ap->a_vpp;
603 struct inode *ip;
604 int error;
605 struct mount *mp;
606 ino_t ino;
607
608 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
609 vput(ap->a_dvp);
610 return error;
611 }
612 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
613 ap->a_dvp, vpp, ap->a_cnp);
614
615 /* Either way we're done with the dirop at this point */
616 SET_ENDOP_CREATE_AP(ap, "mknod");
617
618 if (error)
619 return (error);
620
621 ip = VTOI(*vpp);
622 mp = (*vpp)->v_mount;
623 ino = ip->i_number;
624 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
625 if (vap->va_rdev != VNOVAL) {
626 /*
627 * Want to be able to use this to make badblock
628 * inodes, so don't truncate the dev number.
629 */
630 #if 0
631 ip->i_ffs1_rdev = ufs_rw32(vap->va_rdev,
632 UFS_MPNEEDSWAP((*vpp)->v_mount));
633 #else
634 ip->i_ffs1_rdev = vap->va_rdev;
635 #endif
636 }
637
638 /*
639 * Call fsync to write the vnode so that we don't have to deal with
640 * flushing it when it's marked VU_DIROP|VI_XLOCK.
641 *
642 * XXX KS - If we can't flush we also can't call vgone(), so must
643 * return. But, that leaves this vnode in limbo, also not good.
644 * Can this ever happen (barring hardware failure)?
645 */
646 if ((error = VOP_FSYNC(*vpp, NOCRED, FSYNC_WAIT, 0, 0)) != 0) {
647 panic("lfs_mknod: couldn't fsync (ino %llu)",
648 (unsigned long long)ino);
649 /* return (error); */
650 }
651 /*
652 * Remove vnode so that it will be reloaded by VFS_VGET and
653 * checked to see if it is an alias of an existing entry in
654 * the inode cache.
655 */
656 /* Used to be vput, but that causes us to call VOP_INACTIVE twice. */
657
658 VOP_UNLOCK(*vpp);
659 (*vpp)->v_type = VNON;
660 vgone(*vpp);
661 error = VFS_VGET(mp, ino, vpp);
662
663 if (error != 0) {
664 *vpp = NULL;
665 return (error);
666 }
667 return (0);
668 }
669
670 int
671 lfs_create(void *v)
672 {
673 struct vop_create_args /* {
674 struct vnode *a_dvp;
675 struct vnode **a_vpp;
676 struct componentname *a_cnp;
677 struct vattr *a_vap;
678 } */ *ap = v;
679 int error;
680
681 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
682 vput(ap->a_dvp);
683 return error;
684 }
685 error = ufs_create(ap);
686 SET_ENDOP_CREATE_AP(ap, "create");
687 return (error);
688 }
689
690 int
691 lfs_mkdir(void *v)
692 {
693 struct vop_mkdir_args /* {
694 struct vnode *a_dvp;
695 struct vnode **a_vpp;
696 struct componentname *a_cnp;
697 struct vattr *a_vap;
698 } */ *ap = v;
699 int error;
700
701 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
702 vput(ap->a_dvp);
703 return error;
704 }
705 error = ufs_mkdir(ap);
706 SET_ENDOP_CREATE_AP(ap, "mkdir");
707 return (error);
708 }
709
710 int
711 lfs_remove(void *v)
712 {
713 struct vop_remove_args /* {
714 struct vnode *a_dvp;
715 struct vnode *a_vp;
716 struct componentname *a_cnp;
717 } */ *ap = v;
718 struct vnode *dvp, *vp;
719 struct inode *ip;
720 int error;
721
722 dvp = ap->a_dvp;
723 vp = ap->a_vp;
724 ip = VTOI(vp);
725 if ((error = SET_DIROP_REMOVE(dvp, vp)) != 0) {
726 if (dvp == vp)
727 vrele(vp);
728 else
729 vput(vp);
730 vput(dvp);
731 return error;
732 }
733 error = ufs_remove(ap);
734 if (ip->i_nlink == 0)
735 lfs_orphan(ip->i_lfs, ip->i_number);
736 SET_ENDOP_REMOVE(ip->i_lfs, dvp, ap->a_vp, "remove");
737 return (error);
738 }
739
740 int
741 lfs_rmdir(void *v)
742 {
743 struct vop_rmdir_args /* {
744 struct vnodeop_desc *a_desc;
745 struct vnode *a_dvp;
746 struct vnode *a_vp;
747 struct componentname *a_cnp;
748 } */ *ap = v;
749 struct vnode *vp;
750 struct inode *ip;
751 int error;
752
753 vp = ap->a_vp;
754 ip = VTOI(vp);
755 if ((error = SET_DIROP_REMOVE(ap->a_dvp, ap->a_vp)) != 0) {
756 if (ap->a_dvp == vp)
757 vrele(ap->a_dvp);
758 else
759 vput(ap->a_dvp);
760 vput(vp);
761 return error;
762 }
763 error = ufs_rmdir(ap);
764 if (ip->i_nlink == 0)
765 lfs_orphan(ip->i_lfs, ip->i_number);
766 SET_ENDOP_REMOVE(ip->i_lfs, ap->a_dvp, ap->a_vp, "rmdir");
767 return (error);
768 }
769
770 int
771 lfs_link(void *v)
772 {
773 struct vop_link_args /* {
774 struct vnode *a_dvp;
775 struct vnode *a_vp;
776 struct componentname *a_cnp;
777 } */ *ap = v;
778 int error;
779 struct vnode **vpp = NULL;
780
781 if ((error = SET_DIROP_CREATE(ap->a_dvp, vpp)) != 0) {
782 vput(ap->a_dvp);
783 return error;
784 }
785 error = ufs_link(ap);
786 SET_ENDOP_CREATE(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, vpp, "link");
787 return (error);
788 }
789
790 int
791 lfs_rename(void *v)
792 {
793 struct vop_rename_args /* {
794 struct vnode *a_fdvp;
795 struct vnode *a_fvp;
796 struct componentname *a_fcnp;
797 struct vnode *a_tdvp;
798 struct vnode *a_tvp;
799 struct componentname *a_tcnp;
800 } */ *ap = v;
801 struct vnode *tvp, *fvp, *tdvp, *fdvp;
802 struct componentname *tcnp, *fcnp;
803 int error;
804 struct lfs *fs;
805
806 fs = VTOI(ap->a_fdvp)->i_lfs;
807 tvp = ap->a_tvp;
808 tdvp = ap->a_tdvp;
809 tcnp = ap->a_tcnp;
810 fvp = ap->a_fvp;
811 fdvp = ap->a_fdvp;
812 fcnp = ap->a_fcnp;
813
814 /*
815 * Check for cross-device rename.
816 * If it is, we don't want to set dirops, just error out.
817 * (In particular note that MARK_VNODE(tdvp) will DTWT on
818 * a cross-device rename.)
819 *
820 * Copied from ufs_rename.
821 */
822 if ((fvp->v_mount != tdvp->v_mount) ||
823 (tvp && (fvp->v_mount != tvp->v_mount))) {
824 error = EXDEV;
825 goto errout;
826 }
827
828 /*
829 * Check to make sure we're not renaming a vnode onto itself
830 * (deleting a hard link by renaming one name onto another);
831 * if we are we can't recursively call VOP_REMOVE since that
832 * would leave us with an unaccounted-for number of live dirops.
833 *
834 * Inline the relevant section of ufs_rename here, *before*
835 * calling SET_DIROP_REMOVE.
836 */
837 if (tvp && ((VTOI(tvp)->i_flags & (IMMUTABLE | APPEND)) ||
838 (VTOI(tdvp)->i_flags & APPEND))) {
839 error = EPERM;
840 goto errout;
841 }
842 if (fvp == tvp) {
843 if (fvp->v_type == VDIR) {
844 error = EINVAL;
845 goto errout;
846 }
847
848 /* Release destination completely. */
849 VOP_ABORTOP(tdvp, tcnp);
850 vput(tdvp);
851 vput(tvp);
852
853 /* Delete source. */
854 vrele(fvp);
855 fcnp->cn_flags &= ~(MODMASK | SAVESTART);
856 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
857 fcnp->cn_nameiop = DELETE;
858 vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
859 if ((error = relookup(fdvp, &fvp, fcnp))) {
860 vput(fdvp);
861 return (error);
862 }
863 return (VOP_REMOVE(fdvp, fvp, fcnp));
864 }
865
866 if ((error = SET_DIROP_REMOVE(tdvp, tvp)) != 0)
867 goto errout;
868 MARK_VNODE(fdvp);
869 MARK_VNODE(fvp);
870
871 error = ufs_rename(ap);
872 UNMARK_VNODE(fdvp);
873 UNMARK_VNODE(fvp);
874 SET_ENDOP_REMOVE(fs, tdvp, tvp, "rename");
875 return (error);
876
877 errout:
878 VOP_ABORTOP(tdvp, ap->a_tcnp); /* XXX, why not in NFS? */
879 if (tdvp == tvp)
880 vrele(tdvp);
881 else
882 vput(tdvp);
883 if (tvp)
884 vput(tvp);
885 VOP_ABORTOP(fdvp, ap->a_fcnp); /* XXX, why not in NFS? */
886 vrele(fdvp);
887 vrele(fvp);
888 return (error);
889 }
890
891 /* XXX hack to avoid calling ITIMES in getattr */
892 int
893 lfs_getattr(void *v)
894 {
895 struct vop_getattr_args /* {
896 struct vnode *a_vp;
897 struct vattr *a_vap;
898 kauth_cred_t a_cred;
899 } */ *ap = v;
900 struct vnode *vp = ap->a_vp;
901 struct inode *ip = VTOI(vp);
902 struct vattr *vap = ap->a_vap;
903 struct lfs *fs = ip->i_lfs;
904 /*
905 * Copy from inode table
906 */
907 vap->va_fsid = ip->i_dev;
908 vap->va_fileid = ip->i_number;
909 vap->va_mode = ip->i_mode & ~IFMT;
910 vap->va_nlink = ip->i_nlink;
911 vap->va_uid = ip->i_uid;
912 vap->va_gid = ip->i_gid;
913 vap->va_rdev = (dev_t)ip->i_ffs1_rdev;
914 vap->va_size = vp->v_size;
915 vap->va_atime.tv_sec = ip->i_ffs1_atime;
916 vap->va_atime.tv_nsec = ip->i_ffs1_atimensec;
917 vap->va_mtime.tv_sec = ip->i_ffs1_mtime;
918 vap->va_mtime.tv_nsec = ip->i_ffs1_mtimensec;
919 vap->va_ctime.tv_sec = ip->i_ffs1_ctime;
920 vap->va_ctime.tv_nsec = ip->i_ffs1_ctimensec;
921 vap->va_flags = ip->i_flags;
922 vap->va_gen = ip->i_gen;
923 /* this doesn't belong here */
924 if (vp->v_type == VBLK)
925 vap->va_blocksize = BLKDEV_IOSIZE;
926 else if (vp->v_type == VCHR)
927 vap->va_blocksize = MAXBSIZE;
928 else
929 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
930 vap->va_bytes = fsbtob(fs, (u_quad_t)ip->i_lfs_effnblks);
931 vap->va_type = vp->v_type;
932 vap->va_filerev = ip->i_modrev;
933 return (0);
934 }
935
936 /*
937 * Check to make sure the inode blocks won't choke the buffer
938 * cache, then call ufs_setattr as usual.
939 */
940 int
941 lfs_setattr(void *v)
942 {
943 struct vop_setattr_args /* {
944 struct vnode *a_vp;
945 struct vattr *a_vap;
946 kauth_cred_t a_cred;
947 } */ *ap = v;
948 struct vnode *vp = ap->a_vp;
949
950 lfs_check(vp, LFS_UNUSED_LBN, 0);
951 return ufs_setattr(v);
952 }
953
954 /*
955 * Release the block we hold on lfs_newseg wrapping. Called on file close,
956 * or explicitly from LFCNWRAPGO. Called with the interlock held.
957 */
958 static int
959 lfs_wrapgo(struct lfs *fs, struct inode *ip, int waitfor)
960 {
961 if (fs->lfs_stoplwp != curlwp)
962 return EBUSY;
963
964 fs->lfs_stoplwp = NULL;
965 cv_signal(&fs->lfs_stopcv);
966
967 KASSERT(fs->lfs_nowrap > 0);
968 if (fs->lfs_nowrap <= 0) {
969 return 0;
970 }
971
972 if (--fs->lfs_nowrap == 0) {
973 log(LOG_NOTICE, "%s: re-enabled log wrap\n", fs->lfs_fsmnt);
974 wakeup(&fs->lfs_wrappass);
975 lfs_wakeup_cleaner(fs);
976 }
977 if (waitfor) {
978 mtsleep(&fs->lfs_nextseg, PCATCH | PUSER, "segment",
979 0, &lfs_lock);
980 }
981
982 return 0;
983 }
984
985 /*
986 * Close called
987 */
988 /* ARGSUSED */
989 int
990 lfs_close(void *v)
991 {
992 struct vop_close_args /* {
993 struct vnode *a_vp;
994 int a_fflag;
995 kauth_cred_t a_cred;
996 } */ *ap = v;
997 struct vnode *vp = ap->a_vp;
998 struct inode *ip = VTOI(vp);
999 struct lfs *fs = ip->i_lfs;
1000
1001 if ((ip->i_number == ROOTINO || ip->i_number == LFS_IFILE_INUM) &&
1002 fs->lfs_stoplwp == curlwp) {
1003 mutex_enter(&lfs_lock);
1004 log(LOG_NOTICE, "lfs_close: releasing log wrap control\n");
1005 lfs_wrapgo(fs, ip, 0);
1006 mutex_exit(&lfs_lock);
1007 }
1008
1009 if (vp == ip->i_lfs->lfs_ivnode &&
1010 vp->v_mount->mnt_iflag & IMNT_UNMOUNT)
1011 return 0;
1012
1013 if (vp->v_usecount > 1 && vp != ip->i_lfs->lfs_ivnode) {
1014 LFS_ITIMES(ip, NULL, NULL, NULL);
1015 }
1016 return (0);
1017 }
1018
1019 /*
1020 * Close wrapper for special devices.
1021 *
1022 * Update the times on the inode then do device close.
1023 */
1024 int
1025 lfsspec_close(void *v)
1026 {
1027 struct vop_close_args /* {
1028 struct vnode *a_vp;
1029 int a_fflag;
1030 kauth_cred_t a_cred;
1031 } */ *ap = v;
1032 struct vnode *vp;
1033 struct inode *ip;
1034
1035 vp = ap->a_vp;
1036 ip = VTOI(vp);
1037 if (vp->v_usecount > 1) {
1038 LFS_ITIMES(ip, NULL, NULL, NULL);
1039 }
1040 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
1041 }
1042
1043 /*
1044 * Close wrapper for fifo's.
1045 *
1046 * Update the times on the inode then do device close.
1047 */
1048 int
1049 lfsfifo_close(void *v)
1050 {
1051 struct vop_close_args /* {
1052 struct vnode *a_vp;
1053 int a_fflag;
1054 kauth_cred_ a_cred;
1055 } */ *ap = v;
1056 struct vnode *vp;
1057 struct inode *ip;
1058
1059 vp = ap->a_vp;
1060 ip = VTOI(vp);
1061 if (ap->a_vp->v_usecount > 1) {
1062 LFS_ITIMES(ip, NULL, NULL, NULL);
1063 }
1064 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
1065 }
1066
1067 /*
1068 * Reclaim an inode so that it can be used for other purposes.
1069 */
1070
1071 int
1072 lfs_reclaim(void *v)
1073 {
1074 struct vop_reclaim_args /* {
1075 struct vnode *a_vp;
1076 } */ *ap = v;
1077 struct vnode *vp = ap->a_vp;
1078 struct inode *ip = VTOI(vp);
1079 struct lfs *fs = ip->i_lfs;
1080 int error;
1081
1082 /*
1083 * The inode must be freed and updated before being removed
1084 * from its hash chain. Other threads trying to gain a hold
1085 * on the inode will be stalled because it is locked (VI_XLOCK).
1086 */
1087 if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1088 lfs_vfree(vp, ip->i_number, ip->i_omode);
1089
1090 mutex_enter(&lfs_lock);
1091 LFS_CLR_UINO(ip, IN_ALLMOD);
1092 mutex_exit(&lfs_lock);
1093 if ((error = ufs_reclaim(vp)))
1094 return (error);
1095
1096 /*
1097 * Take us off the paging and/or dirop queues if we were on them.
1098 * We shouldn't be on them.
1099 */
1100 mutex_enter(&lfs_lock);
1101 if (ip->i_flags & IN_PAGING) {
1102 log(LOG_WARNING, "%s: reclaimed vnode is IN_PAGING\n",
1103 fs->lfs_fsmnt);
1104 ip->i_flags &= ~IN_PAGING;
1105 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
1106 }
1107 if (vp->v_uflag & VU_DIROP) {
1108 panic("reclaimed vnode is VU_DIROP");
1109 vp->v_uflag &= ~VU_DIROP;
1110 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
1111 }
1112 mutex_exit(&lfs_lock);
1113
1114 pool_put(&lfs_dinode_pool, ip->i_din.ffs1_din);
1115 lfs_deregister_all(vp);
1116 pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
1117 ip->inode_ext.lfs = NULL;
1118 genfs_node_destroy(vp);
1119 pool_put(&lfs_inode_pool, vp->v_data);
1120 vp->v_data = NULL;
1121 return (0);
1122 }
1123
1124 /*
1125 * Read a block from a storage device.
1126 * In order to avoid reading blocks that are in the process of being
1127 * written by the cleaner---and hence are not mutexed by the normal
1128 * buffer cache / page cache mechanisms---check for collisions before
1129 * reading.
1130 *
1131 * We inline ufs_strategy to make sure that the VOP_BMAP occurs *before*
1132 * the active cleaner test.
1133 *
1134 * XXX This code assumes that lfs_markv makes synchronous checkpoints.
1135 */
1136 int
1137 lfs_strategy(void *v)
1138 {
1139 struct vop_strategy_args /* {
1140 struct vnode *a_vp;
1141 struct buf *a_bp;
1142 } */ *ap = v;
1143 struct buf *bp;
1144 struct lfs *fs;
1145 struct vnode *vp;
1146 struct inode *ip;
1147 daddr_t tbn;
1148 int i, sn, error, slept;
1149
1150 bp = ap->a_bp;
1151 vp = ap->a_vp;
1152 ip = VTOI(vp);
1153 fs = ip->i_lfs;
1154
1155 /* lfs uses its strategy routine only for read */
1156 KASSERT(bp->b_flags & B_READ);
1157
1158 if (vp->v_type == VBLK || vp->v_type == VCHR)
1159 panic("lfs_strategy: spec");
1160 KASSERT(bp->b_bcount != 0);
1161 if (bp->b_blkno == bp->b_lblkno) {
1162 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1163 NULL);
1164 if (error) {
1165 bp->b_error = error;
1166 bp->b_resid = bp->b_bcount;
1167 biodone(bp);
1168 return (error);
1169 }
1170 if ((long)bp->b_blkno == -1) /* no valid data */
1171 clrbuf(bp);
1172 }
1173 if ((long)bp->b_blkno < 0) { /* block is not on disk */
1174 bp->b_resid = bp->b_bcount;
1175 biodone(bp);
1176 return (0);
1177 }
1178
1179 slept = 1;
1180 mutex_enter(&lfs_lock);
1181 while (slept && fs->lfs_seglock) {
1182 mutex_exit(&lfs_lock);
1183 /*
1184 * Look through list of intervals.
1185 * There will only be intervals to look through
1186 * if the cleaner holds the seglock.
1187 * Since the cleaner is synchronous, we can trust
1188 * the list of intervals to be current.
1189 */
1190 tbn = dbtofsb(fs, bp->b_blkno);
1191 sn = dtosn(fs, tbn);
1192 slept = 0;
1193 for (i = 0; i < fs->lfs_cleanind; i++) {
1194 if (sn == dtosn(fs, fs->lfs_cleanint[i]) &&
1195 tbn >= fs->lfs_cleanint[i]) {
1196 DLOG((DLOG_CLEAN,
1197 "lfs_strategy: ino %d lbn %" PRId64
1198 " ind %d sn %d fsb %" PRIx32
1199 " given sn %d fsb %" PRIx64 "\n",
1200 ip->i_number, bp->b_lblkno, i,
1201 dtosn(fs, fs->lfs_cleanint[i]),
1202 fs->lfs_cleanint[i], sn, tbn));
1203 DLOG((DLOG_CLEAN,
1204 "lfs_strategy: sleeping on ino %d lbn %"
1205 PRId64 "\n", ip->i_number, bp->b_lblkno));
1206 mutex_enter(&lfs_lock);
1207 if (LFS_SEGLOCK_HELD(fs) && fs->lfs_iocount) {
1208 /* Cleaner can't wait for itself */
1209 mtsleep(&fs->lfs_iocount,
1210 (PRIBIO + 1) | PNORELOCK,
1211 "clean2", 0,
1212 &lfs_lock);
1213 slept = 1;
1214 break;
1215 } else if (fs->lfs_seglock) {
1216 mtsleep(&fs->lfs_seglock,
1217 (PRIBIO + 1) | PNORELOCK,
1218 "clean1", 0,
1219 &lfs_lock);
1220 slept = 1;
1221 break;
1222 }
1223 mutex_exit(&lfs_lock);
1224 }
1225 }
1226 mutex_enter(&lfs_lock);
1227 }
1228 mutex_exit(&lfs_lock);
1229
1230 vp = ip->i_devvp;
1231 VOP_STRATEGY(vp, bp);
1232 return (0);
1233 }
1234
1235 void
1236 lfs_flush_dirops(struct lfs *fs)
1237 {
1238 struct inode *ip, *nip;
1239 struct vnode *vp;
1240 extern int lfs_dostats;
1241 struct segment *sp;
1242
1243 ASSERT_MAYBE_SEGLOCK(fs);
1244 KASSERT(fs->lfs_nadirop == 0);
1245
1246 if (fs->lfs_ronly)
1247 return;
1248
1249 mutex_enter(&lfs_lock);
1250 if (TAILQ_FIRST(&fs->lfs_dchainhd) == NULL) {
1251 mutex_exit(&lfs_lock);
1252 return;
1253 } else
1254 mutex_exit(&lfs_lock);
1255
1256 if (lfs_dostats)
1257 ++lfs_stats.flush_invoked;
1258
1259 /*
1260 * Inline lfs_segwrite/lfs_writevnodes, but just for dirops.
1261 * Technically this is a checkpoint (the on-disk state is valid)
1262 * even though we are leaving out all the file data.
1263 */
1264 lfs_imtime(fs);
1265 lfs_seglock(fs, SEGM_CKP);
1266 sp = fs->lfs_sp;
1267
1268 /*
1269 * lfs_writevnodes, optimized to get dirops out of the way.
1270 * Only write dirops, and don't flush files' pages, only
1271 * blocks from the directories.
1272 *
1273 * We don't need to vref these files because they are
1274 * dirops and so hold an extra reference until the
1275 * segunlock clears them of that status.
1276 *
1277 * We don't need to check for IN_ADIROP because we know that
1278 * no dirops are active.
1279 *
1280 */
1281 mutex_enter(&lfs_lock);
1282 for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
1283 nip = TAILQ_NEXT(ip, i_lfs_dchain);
1284 mutex_exit(&lfs_lock);
1285 vp = ITOV(ip);
1286
1287 KASSERT((ip->i_flag & IN_ADIROP) == 0);
1288
1289 /*
1290 * All writes to directories come from dirops; all
1291 * writes to files' direct blocks go through the page
1292 * cache, which we're not touching. Reads to files
1293 * and/or directories will not be affected by writing
1294 * directory blocks inodes and file inodes. So we don't
1295 * really need to lock. If we don't lock, though,
1296 * make sure that we don't clear IN_MODIFIED
1297 * unnecessarily.
1298 */
1299 if (vp->v_iflag & VI_XLOCK) {
1300 mutex_enter(&lfs_lock);
1301 continue;
1302 }
1303 /* XXX see below
1304 * waslocked = VOP_ISLOCKED(vp);
1305 */
1306 if (vp->v_type != VREG &&
1307 ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp))) {
1308 lfs_writefile(fs, sp, vp);
1309 if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
1310 !(ip->i_flag & IN_ALLMOD)) {
1311 mutex_enter(&lfs_lock);
1312 LFS_SET_UINO(ip, IN_MODIFIED);
1313 mutex_exit(&lfs_lock);
1314 }
1315 }
1316 KDASSERT(ip->i_number != LFS_IFILE_INUM);
1317 (void) lfs_writeinode(fs, sp, ip);
1318 mutex_enter(&lfs_lock);
1319 /*
1320 * XXX
1321 * LK_EXCLOTHER is dead -- what is intended here?
1322 * if (waslocked == LK_EXCLOTHER)
1323 * LFS_SET_UINO(ip, IN_MODIFIED);
1324 */
1325 }
1326 mutex_exit(&lfs_lock);
1327 /* We've written all the dirops there are */
1328 ((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
1329 lfs_finalize_fs_seguse(fs);
1330 (void) lfs_writeseg(fs, sp);
1331 lfs_segunlock(fs);
1332 }
1333
1334 /*
1335 * Flush all vnodes for which the pagedaemon has requested pageouts.
1336 * Skip over any files that are marked VU_DIROP (since lfs_flush_dirop()
1337 * has just run, this would be an error). If we have to skip a vnode
1338 * for any reason, just skip it; if we have to wait for the cleaner,
1339 * abort. The writer daemon will call us again later.
1340 */
1341 void
1342 lfs_flush_pchain(struct lfs *fs)
1343 {
1344 struct inode *ip, *nip;
1345 struct vnode *vp;
1346 extern int lfs_dostats;
1347 struct segment *sp;
1348 int error;
1349
1350 ASSERT_NO_SEGLOCK(fs);
1351
1352 if (fs->lfs_ronly)
1353 return;
1354
1355 mutex_enter(&lfs_lock);
1356 if (TAILQ_FIRST(&fs->lfs_pchainhd) == NULL) {
1357 mutex_exit(&lfs_lock);
1358 return;
1359 } else
1360 mutex_exit(&lfs_lock);
1361
1362 /* Get dirops out of the way */
1363 lfs_flush_dirops(fs);
1364
1365 if (lfs_dostats)
1366 ++lfs_stats.flush_invoked;
1367
1368 /*
1369 * Inline lfs_segwrite/lfs_writevnodes, but just for pageouts.
1370 */
1371 lfs_imtime(fs);
1372 lfs_seglock(fs, 0);
1373 sp = fs->lfs_sp;
1374
1375 /*
1376 * lfs_writevnodes, optimized to clear pageout requests.
1377 * Only write non-dirop files that are in the pageout queue.
1378 * We're very conservative about what we write; we want to be
1379 * fast and async.
1380 */
1381 mutex_enter(&lfs_lock);
1382 top:
1383 for (ip = TAILQ_FIRST(&fs->lfs_pchainhd); ip != NULL; ip = nip) {
1384 nip = TAILQ_NEXT(ip, i_lfs_pchain);
1385 vp = ITOV(ip);
1386
1387 if (!(ip->i_flags & IN_PAGING))
1388 goto top;
1389
1390 mutex_enter(&vp->v_interlock);
1391 if ((vp->v_iflag & VI_XLOCK) || (vp->v_uflag & VU_DIROP) != 0) {
1392 mutex_exit(&vp->v_interlock);
1393 continue;
1394 }
1395 if (vp->v_type != VREG) {
1396 mutex_exit(&vp->v_interlock);
1397 continue;
1398 }
1399 if (lfs_vref(vp))
1400 continue;
1401 mutex_exit(&lfs_lock);
1402
1403 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_RETRY) != 0) {
1404 lfs_vunref(vp);
1405 mutex_enter(&lfs_lock);
1406 continue;
1407 }
1408
1409 error = lfs_writefile(fs, sp, vp);
1410 if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
1411 !(ip->i_flag & IN_ALLMOD)) {
1412 mutex_enter(&lfs_lock);
1413 LFS_SET_UINO(ip, IN_MODIFIED);
1414 mutex_exit(&lfs_lock);
1415 }
1416 KDASSERT(ip->i_number != LFS_IFILE_INUM);
1417 (void) lfs_writeinode(fs, sp, ip);
1418
1419 VOP_UNLOCK(vp);
1420 lfs_vunref(vp);
1421
1422 if (error == EAGAIN) {
1423 lfs_writeseg(fs, sp);
1424 mutex_enter(&lfs_lock);
1425 break;
1426 }
1427 mutex_enter(&lfs_lock);
1428 }
1429 mutex_exit(&lfs_lock);
1430 (void) lfs_writeseg(fs, sp);
1431 lfs_segunlock(fs);
1432 }
1433
1434 /*
1435 * Provide a fcntl interface to sys_lfs_{segwait,bmapv,markv}.
1436 */
1437 int
1438 lfs_fcntl(void *v)
1439 {
1440 struct vop_fcntl_args /* {
1441 struct vnode *a_vp;
1442 u_int a_command;
1443 void * a_data;
1444 int a_fflag;
1445 kauth_cred_t a_cred;
1446 } */ *ap = v;
1447 struct timeval tv;
1448 struct timeval *tvp;
1449 BLOCK_INFO *blkiov;
1450 CLEANERINFO *cip;
1451 SEGUSE *sup;
1452 int blkcnt, error, oclean;
1453 size_t fh_size;
1454 struct lfs_fcntl_markv blkvp;
1455 struct lwp *l;
1456 fsid_t *fsidp;
1457 struct lfs *fs;
1458 struct buf *bp;
1459 fhandle_t *fhp;
1460 daddr_t off;
1461
1462 /* Only respect LFS fcntls on fs root or Ifile */
1463 if (VTOI(ap->a_vp)->i_number != ROOTINO &&
1464 VTOI(ap->a_vp)->i_number != LFS_IFILE_INUM) {
1465 return ufs_fcntl(v);
1466 }
1467
1468 /* Avoid locking a draining lock */
1469 if (ap->a_vp->v_mount->mnt_iflag & IMNT_UNMOUNT) {
1470 return ESHUTDOWN;
1471 }
1472
1473 /* LFS control and monitoring fcntls are available only to root */
1474 l = curlwp;
1475 if (((ap->a_command & 0xff00) >> 8) == 'L' &&
1476 (error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
1477 NULL)) != 0)
1478 return (error);
1479
1480 fs = VTOI(ap->a_vp)->i_lfs;
1481 fsidp = &ap->a_vp->v_mount->mnt_stat.f_fsidx;
1482
1483 error = 0;
1484 switch ((int)ap->a_command) {
1485 case LFCNSEGWAITALL_COMPAT_50:
1486 case LFCNSEGWAITALL_COMPAT:
1487 fsidp = NULL;
1488 /* FALLSTHROUGH */
1489 case LFCNSEGWAIT_COMPAT_50:
1490 case LFCNSEGWAIT_COMPAT:
1491 {
1492 struct timeval50 *tvp50
1493 = (struct timeval50 *)ap->a_data;
1494 timeval50_to_timeval(tvp50, &tv);
1495 tvp = &tv;
1496 }
1497 goto segwait_common;
1498 case LFCNSEGWAITALL:
1499 fsidp = NULL;
1500 /* FALLSTHROUGH */
1501 case LFCNSEGWAIT:
1502 tvp = (struct timeval *)ap->a_data;
1503 segwait_common:
1504 mutex_enter(&lfs_lock);
1505 ++fs->lfs_sleepers;
1506 mutex_exit(&lfs_lock);
1507
1508 error = lfs_segwait(fsidp, tvp);
1509
1510 mutex_enter(&lfs_lock);
1511 if (--fs->lfs_sleepers == 0)
1512 wakeup(&fs->lfs_sleepers);
1513 mutex_exit(&lfs_lock);
1514 return error;
1515
1516 case LFCNBMAPV:
1517 case LFCNMARKV:
1518 blkvp = *(struct lfs_fcntl_markv *)ap->a_data;
1519
1520 blkcnt = blkvp.blkcnt;
1521 if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
1522 return (EINVAL);
1523 blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
1524 if ((error = copyin(blkvp.blkiov, blkiov,
1525 blkcnt * sizeof(BLOCK_INFO))) != 0) {
1526 lfs_free(fs, blkiov, LFS_NB_BLKIOV);
1527 return error;
1528 }
1529
1530 mutex_enter(&lfs_lock);
1531 ++fs->lfs_sleepers;
1532 mutex_exit(&lfs_lock);
1533 if (ap->a_command == LFCNBMAPV)
1534 error = lfs_bmapv(l->l_proc, fsidp, blkiov, blkcnt);
1535 else /* LFCNMARKV */
1536 error = lfs_markv(l->l_proc, fsidp, blkiov, blkcnt);
1537 if (error == 0)
1538 error = copyout(blkiov, blkvp.blkiov,
1539 blkcnt * sizeof(BLOCK_INFO));
1540 mutex_enter(&lfs_lock);
1541 if (--fs->lfs_sleepers == 0)
1542 wakeup(&fs->lfs_sleepers);
1543 mutex_exit(&lfs_lock);
1544 lfs_free(fs, blkiov, LFS_NB_BLKIOV);
1545 return error;
1546
1547 case LFCNRECLAIM:
1548 /*
1549 * Flush dirops and write Ifile, allowing empty segments
1550 * to be immediately reclaimed.
1551 */
1552 lfs_writer_enter(fs, "pndirop");
1553 off = fs->lfs_offset;
1554 lfs_seglock(fs, SEGM_FORCE_CKP | SEGM_CKP);
1555 lfs_flush_dirops(fs);
1556 LFS_CLEANERINFO(cip, fs, bp);
1557 oclean = cip->clean;
1558 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1559 lfs_segwrite(ap->a_vp->v_mount, SEGM_FORCE_CKP);
1560 fs->lfs_sp->seg_flags |= SEGM_PROT;
1561 lfs_segunlock(fs);
1562 lfs_writer_leave(fs);
1563
1564 #ifdef DEBUG
1565 LFS_CLEANERINFO(cip, fs, bp);
1566 DLOG((DLOG_CLEAN, "lfs_fcntl: reclaim wrote %" PRId64
1567 " blocks, cleaned %" PRId32 " segments (activesb %d)\n",
1568 fs->lfs_offset - off, cip->clean - oclean,
1569 fs->lfs_activesb));
1570 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
1571 #endif
1572
1573 return 0;
1574
1575 case LFCNIFILEFH_COMPAT:
1576 /* Return the filehandle of the Ifile */
1577 if ((error = kauth_authorize_system(l->l_cred,
1578 KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)) != 0)
1579 return (error);
1580 fhp = (struct fhandle *)ap->a_data;
1581 fhp->fh_fsid = *fsidp;
1582 fh_size = 16; /* former VFS_MAXFIDSIZ */
1583 return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
1584
1585 case LFCNIFILEFH_COMPAT2:
1586 case LFCNIFILEFH:
1587 /* Return the filehandle of the Ifile */
1588 fhp = (struct fhandle *)ap->a_data;
1589 fhp->fh_fsid = *fsidp;
1590 fh_size = sizeof(struct lfs_fhandle) -
1591 offsetof(fhandle_t, fh_fid);
1592 return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
1593
1594 case LFCNREWIND:
1595 /* Move lfs_offset to the lowest-numbered segment */
1596 return lfs_rewind(fs, *(int *)ap->a_data);
1597
1598 case LFCNINVAL:
1599 /* Mark a segment SEGUSE_INVAL */
1600 LFS_SEGENTRY(sup, fs, *(int *)ap->a_data, bp);
1601 if (sup->su_nbytes > 0) {
1602 brelse(bp, 0);
1603 lfs_unset_inval_all(fs);
1604 return EBUSY;
1605 }
1606 sup->su_flags |= SEGUSE_INVAL;
1607 VOP_BWRITE(bp);
1608 return 0;
1609
1610 case LFCNRESIZE:
1611 /* Resize the filesystem */
1612 return lfs_resize_fs(fs, *(int *)ap->a_data);
1613
1614 case LFCNWRAPSTOP:
1615 case LFCNWRAPSTOP_COMPAT:
1616 /*
1617 * Hold lfs_newseg at segment 0; if requested, sleep until
1618 * the filesystem wraps around. To support external agents
1619 * (dump, fsck-based regression test) that need to look at
1620 * a snapshot of the filesystem, without necessarily
1621 * requiring that all fs activity stops.
1622 */
1623 if (fs->lfs_stoplwp == curlwp)
1624 return EALREADY;
1625
1626 mutex_enter(&lfs_lock);
1627 while (fs->lfs_stoplwp != NULL)
1628 cv_wait(&fs->lfs_stopcv, &lfs_lock);
1629 fs->lfs_stoplwp = curlwp;
1630 if (fs->lfs_nowrap == 0)
1631 log(LOG_NOTICE, "%s: disabled log wrap\n", fs->lfs_fsmnt);
1632 ++fs->lfs_nowrap;
1633 if (*(int *)ap->a_data == 1
1634 || ap->a_command == LFCNWRAPSTOP_COMPAT) {
1635 log(LOG_NOTICE, "LFCNSTOPWRAP waiting for log wrap\n");
1636 error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
1637 "segwrap", 0, &lfs_lock);
1638 log(LOG_NOTICE, "LFCNSTOPWRAP done waiting\n");
1639 if (error) {
1640 lfs_wrapgo(fs, VTOI(ap->a_vp), 0);
1641 }
1642 }
1643 mutex_exit(&lfs_lock);
1644 return 0;
1645
1646 case LFCNWRAPGO:
1647 case LFCNWRAPGO_COMPAT:
1648 /*
1649 * Having done its work, the agent wakes up the writer.
1650 * If the argument is 1, it sleeps until a new segment
1651 * is selected.
1652 */
1653 mutex_enter(&lfs_lock);
1654 error = lfs_wrapgo(fs, VTOI(ap->a_vp),
1655 ap->a_command == LFCNWRAPGO_COMPAT ? 1 :
1656 *((int *)ap->a_data));
1657 mutex_exit(&lfs_lock);
1658 return error;
1659
1660 case LFCNWRAPPASS:
1661 if ((VTOI(ap->a_vp)->i_lfs_iflags & LFSI_WRAPWAIT))
1662 return EALREADY;
1663 mutex_enter(&lfs_lock);
1664 if (fs->lfs_stoplwp != curlwp) {
1665 mutex_exit(&lfs_lock);
1666 return EALREADY;
1667 }
1668 if (fs->lfs_nowrap == 0) {
1669 mutex_exit(&lfs_lock);
1670 return EBUSY;
1671 }
1672 fs->lfs_wrappass = 1;
1673 wakeup(&fs->lfs_wrappass);
1674 /* Wait for the log to wrap, if asked */
1675 if (*(int *)ap->a_data) {
1676 mutex_enter(&ap->a_vp->v_interlock);
1677 lfs_vref(ap->a_vp);
1678 VTOI(ap->a_vp)->i_lfs_iflags |= LFSI_WRAPWAIT;
1679 log(LOG_NOTICE, "LFCNPASS waiting for log wrap\n");
1680 error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
1681 "segwrap", 0, &lfs_lock);
1682 log(LOG_NOTICE, "LFCNPASS done waiting\n");
1683 VTOI(ap->a_vp)->i_lfs_iflags &= ~LFSI_WRAPWAIT;
1684 lfs_vunref(ap->a_vp);
1685 }
1686 mutex_exit(&lfs_lock);
1687 return error;
1688
1689 case LFCNWRAPSTATUS:
1690 mutex_enter(&lfs_lock);
1691 *(int *)ap->a_data = fs->lfs_wrapstatus;
1692 mutex_exit(&lfs_lock);
1693 return 0;
1694
1695 default:
1696 return ufs_fcntl(v);
1697 }
1698 return 0;
1699 }
1700
1701 int
1702 lfs_getpages(void *v)
1703 {
1704 struct vop_getpages_args /* {
1705 struct vnode *a_vp;
1706 voff_t a_offset;
1707 struct vm_page **a_m;
1708 int *a_count;
1709 int a_centeridx;
1710 vm_prot_t a_access_type;
1711 int a_advice;
1712 int a_flags;
1713 } */ *ap = v;
1714
1715 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM &&
1716 (ap->a_access_type & VM_PROT_WRITE) != 0) {
1717 return EPERM;
1718 }
1719 if ((ap->a_access_type & VM_PROT_WRITE) != 0) {
1720 mutex_enter(&lfs_lock);
1721 LFS_SET_UINO(VTOI(ap->a_vp), IN_MODIFIED);
1722 mutex_exit(&lfs_lock);
1723 }
1724
1725 /*
1726 * we're relying on the fact that genfs_getpages() always read in
1727 * entire filesystem blocks.
1728 */
1729 return genfs_getpages(v);
1730 }
1731
1732 /*
1733 * Wait for a page to become unbusy, possibly printing diagnostic messages
1734 * as well.
1735 *
1736 * Called with vp->v_interlock held; return with it held.
1737 */
1738 static void
1739 wait_for_page(struct vnode *vp, struct vm_page *pg, const char *label)
1740 {
1741 if ((pg->flags & PG_BUSY) == 0)
1742 return; /* Nothing to wait for! */
1743
1744 #if defined(DEBUG) && defined(UVM_PAGE_TRKOWN)
1745 static struct vm_page *lastpg;
1746
1747 if (label != NULL && pg != lastpg) {
1748 if (pg->owner_tag) {
1749 printf("lfs_putpages[%d.%d]: %s: page %p owner %d.%d [%s]\n",
1750 curproc->p_pid, curlwp->l_lid, label,
1751 pg, pg->owner, pg->lowner, pg->owner_tag);
1752 } else {
1753 printf("lfs_putpages[%d.%d]: %s: page %p unowned?!\n",
1754 curproc->p_pid, curlwp->l_lid, label, pg);
1755 }
1756 }
1757 lastpg = pg;
1758 #endif
1759
1760 pg->flags |= PG_WANTED;
1761 UVM_UNLOCK_AND_WAIT(pg, &vp->v_interlock, 0, "lfsput", 0);
1762 mutex_enter(&vp->v_interlock);
1763 }
1764
1765 /*
1766 * This routine is called by lfs_putpages() when it can't complete the
1767 * write because a page is busy. This means that either (1) someone,
1768 * possibly the pagedaemon, is looking at this page, and will give it up
1769 * presently; or (2) we ourselves are holding the page busy in the
1770 * process of being written (either gathered or actually on its way to
1771 * disk). We don't need to give up the segment lock, but we might need
1772 * to call lfs_writeseg() to expedite the page's journey to disk.
1773 *
1774 * Called with vp->v_interlock held; return with it held.
1775 */
1776 /* #define BUSYWAIT */
1777 static void
1778 write_and_wait(struct lfs *fs, struct vnode *vp, struct vm_page *pg,
1779 int seglocked, const char *label)
1780 {
1781 #ifndef BUSYWAIT
1782 struct inode *ip = VTOI(vp);
1783 struct segment *sp = fs->lfs_sp;
1784 int count = 0;
1785
1786 if (pg == NULL)
1787 return;
1788
1789 while (pg->flags & PG_BUSY &&
1790 pg->uobject == &vp->v_uobj) {
1791 mutex_exit(&vp->v_interlock);
1792 if (sp->cbpp - sp->bpp > 1) {
1793 /* Write gathered pages */
1794 lfs_updatemeta(sp);
1795 lfs_release_finfo(fs);
1796 (void) lfs_writeseg(fs, sp);
1797
1798 /*
1799 * Reinitialize FIP
1800 */
1801 KASSERT(sp->vp == vp);
1802 lfs_acquire_finfo(fs, ip->i_number,
1803 ip->i_gen);
1804 }
1805 ++count;
1806 mutex_enter(&vp->v_interlock);
1807 wait_for_page(vp, pg, label);
1808 }
1809 if (label != NULL && count > 1)
1810 printf("lfs_putpages[%d]: %s: %sn = %d\n", curproc->p_pid,
1811 label, (count > 0 ? "looping, " : ""), count);
1812 #else
1813 preempt(1);
1814 #endif
1815 }
1816
1817 /*
1818 * Make sure that for all pages in every block in the given range,
1819 * either all are dirty or all are clean. If any of the pages
1820 * we've seen so far are dirty, put the vnode on the paging chain,
1821 * and mark it IN_PAGING.
1822 *
1823 * If checkfirst != 0, don't check all the pages but return at the
1824 * first dirty page.
1825 */
1826 static int
1827 check_dirty(struct lfs *fs, struct vnode *vp,
1828 off_t startoffset, off_t endoffset, off_t blkeof,
1829 int flags, int checkfirst, struct vm_page **pgp)
1830 {
1831 int by_list;
1832 struct vm_page *curpg = NULL; /* XXX: gcc */
1833 struct vm_page *pgs[MAXBSIZE / PAGE_SIZE], *pg;
1834 off_t soff = 0; /* XXX: gcc */
1835 voff_t off;
1836 int i;
1837 int nonexistent;
1838 int any_dirty; /* number of dirty pages */
1839 int dirty; /* number of dirty pages in a block */
1840 int tdirty;
1841 int pages_per_block = fs->lfs_bsize >> PAGE_SHIFT;
1842 int pagedaemon = (curlwp == uvm.pagedaemon_lwp);
1843
1844 ASSERT_MAYBE_SEGLOCK(fs);
1845 top:
1846 by_list = (vp->v_uobj.uo_npages <=
1847 ((endoffset - startoffset) >> PAGE_SHIFT) *
1848 UVM_PAGE_TREE_PENALTY);
1849 any_dirty = 0;
1850
1851 if (by_list) {
1852 curpg = TAILQ_FIRST(&vp->v_uobj.memq);
1853 } else {
1854 soff = startoffset;
1855 }
1856 while (by_list || soff < MIN(blkeof, endoffset)) {
1857 if (by_list) {
1858 /*
1859 * Find the first page in a block. Skip
1860 * blocks outside our area of interest or beyond
1861 * the end of file.
1862 */
1863 KASSERT((curpg->flags & PG_MARKER) == 0);
1864 if (pages_per_block > 1) {
1865 while (curpg &&
1866 ((curpg->offset & fs->lfs_bmask) ||
1867 curpg->offset >= vp->v_size ||
1868 curpg->offset >= endoffset)) {
1869 curpg = TAILQ_NEXT(curpg, listq.queue);
1870 KASSERT(curpg == NULL ||
1871 (curpg->flags & PG_MARKER) == 0);
1872 }
1873 }
1874 if (curpg == NULL)
1875 break;
1876 soff = curpg->offset;
1877 }
1878
1879 /*
1880 * Mark all pages in extended range busy; find out if any
1881 * of them are dirty.
1882 */
1883 nonexistent = dirty = 0;
1884 for (i = 0; i == 0 || i < pages_per_block; i++) {
1885 if (by_list && pages_per_block <= 1) {
1886 pgs[i] = pg = curpg;
1887 } else {
1888 off = soff + (i << PAGE_SHIFT);
1889 pgs[i] = pg = uvm_pagelookup(&vp->v_uobj, off);
1890 if (pg == NULL) {
1891 ++nonexistent;
1892 continue;
1893 }
1894 }
1895 KASSERT(pg != NULL);
1896
1897 /*
1898 * If we're holding the segment lock, we can deadlock
1899 * against a process that has our page and is waiting
1900 * for the cleaner, while the cleaner waits for the
1901 * segment lock. Just bail in that case.
1902 */
1903 if ((pg->flags & PG_BUSY) &&
1904 (pagedaemon || LFS_SEGLOCK_HELD(fs))) {
1905 if (i > 0)
1906 uvm_page_unbusy(pgs, i);
1907 DLOG((DLOG_PAGE, "lfs_putpages: avoiding 3-way or pagedaemon deadlock\n"));
1908 if (pgp)
1909 *pgp = pg;
1910 return -1;
1911 }
1912
1913 while (pg->flags & PG_BUSY) {
1914 wait_for_page(vp, pg, NULL);
1915 if (i > 0)
1916 uvm_page_unbusy(pgs, i);
1917 goto top;
1918 }
1919 pg->flags |= PG_BUSY;
1920 UVM_PAGE_OWN(pg, "lfs_putpages");
1921
1922 pmap_page_protect(pg, VM_PROT_NONE);
1923 tdirty = (pmap_clear_modify(pg) ||
1924 (pg->flags & PG_CLEAN) == 0);
1925 dirty += tdirty;
1926 }
1927 if (pages_per_block > 0 && nonexistent >= pages_per_block) {
1928 if (by_list) {
1929 curpg = TAILQ_NEXT(curpg, listq.queue);
1930 } else {
1931 soff += fs->lfs_bsize;
1932 }
1933 continue;
1934 }
1935
1936 any_dirty += dirty;
1937 KASSERT(nonexistent == 0);
1938
1939 /*
1940 * If any are dirty make all dirty; unbusy them,
1941 * but if we were asked to clean, wire them so that
1942 * the pagedaemon doesn't bother us about them while
1943 * they're on their way to disk.
1944 */
1945 for (i = 0; i == 0 || i < pages_per_block; i++) {
1946 pg = pgs[i];
1947 KASSERT(!((pg->flags & PG_CLEAN) && (pg->flags & PG_DELWRI)));
1948 if (dirty) {
1949 pg->flags &= ~PG_CLEAN;
1950 if (flags & PGO_FREE) {
1951 /*
1952 * Wire the page so that
1953 * pdaemon doesn't see it again.
1954 */
1955 mutex_enter(&uvm_pageqlock);
1956 uvm_pagewire(pg);
1957 mutex_exit(&uvm_pageqlock);
1958
1959 /* Suspended write flag */
1960 pg->flags |= PG_DELWRI;
1961 }
1962 }
1963 if (pg->flags & PG_WANTED)
1964 wakeup(pg);
1965 pg->flags &= ~(PG_WANTED|PG_BUSY);
1966 UVM_PAGE_OWN(pg, NULL);
1967 }
1968
1969 if (checkfirst && any_dirty)
1970 break;
1971
1972 if (by_list) {
1973 curpg = TAILQ_NEXT(curpg, listq.queue);
1974 } else {
1975 soff += MAX(PAGE_SIZE, fs->lfs_bsize);
1976 }
1977 }
1978
1979 return any_dirty;
1980 }
1981
1982 /*
1983 * lfs_putpages functions like genfs_putpages except that
1984 *
1985 * (1) It needs to bounds-check the incoming requests to ensure that
1986 * they are block-aligned; if they are not, expand the range and
1987 * do the right thing in case, e.g., the requested range is clean
1988 * but the expanded range is dirty.
1989 *
1990 * (2) It needs to explicitly send blocks to be written when it is done.
1991 * If VOP_PUTPAGES is called without the seglock held, we simply take
1992 * the seglock and let lfs_segunlock wait for us.
1993 * XXX There might be a bad situation if we have to flush a vnode while
1994 * XXX lfs_markv is in operation. As of this writing we panic in this
1995 * XXX case.
1996 *
1997 * Assumptions:
1998 *
1999 * (1) The caller does not hold any pages in this vnode busy. If it does,
2000 * there is a danger that when we expand the page range and busy the
2001 * pages we will deadlock.
2002 *
2003 * (2) We are called with vp->v_interlock held; we must return with it
2004 * released.
2005 *
2006 * (3) We don't absolutely have to free pages right away, provided that
2007 * the request does not have PGO_SYNCIO. When the pagedaemon gives
2008 * us a request with PGO_FREE, we take the pages out of the paging
2009 * queue and wake up the writer, which will handle freeing them for us.
2010 *
2011 * We ensure that for any filesystem block, all pages for that
2012 * block are either resident or not, even if those pages are higher
2013 * than EOF; that means that we will be getting requests to free
2014 * "unused" pages above EOF all the time, and should ignore them.
2015 *
2016 * (4) If we are called with PGO_LOCKED, the finfo array we are to write
2017 * into has been set up for us by lfs_writefile. If not, we will
2018 * have to handle allocating and/or freeing an finfo entry.
2019 *
2020 * XXX note that we're (ab)using PGO_LOCKED as "seglock held".
2021 */
2022
2023 /* How many times to loop before we should start to worry */
2024 #define TOOMANY 4
2025
2026 int
2027 lfs_putpages(void *v)
2028 {
2029 int error;
2030 struct vop_putpages_args /* {
2031 struct vnode *a_vp;
2032 voff_t a_offlo;
2033 voff_t a_offhi;
2034 int a_flags;
2035 } */ *ap = v;
2036 struct vnode *vp;
2037 struct inode *ip;
2038 struct lfs *fs;
2039 struct segment *sp;
2040 off_t origoffset, startoffset, endoffset, origendoffset, blkeof;
2041 off_t off, max_endoffset;
2042 bool seglocked, sync, pagedaemon;
2043 struct vm_page *pg, *busypg;
2044 UVMHIST_FUNC("lfs_putpages"); UVMHIST_CALLED(ubchist);
2045 #ifdef DEBUG
2046 int debug_n_again, debug_n_dirtyclean;
2047 #endif
2048
2049 vp = ap->a_vp;
2050 ip = VTOI(vp);
2051 fs = ip->i_lfs;
2052 sync = (ap->a_flags & PGO_SYNCIO) != 0;
2053 pagedaemon = (curlwp == uvm.pagedaemon_lwp);
2054
2055 /* Putpages does nothing for metadata. */
2056 if (vp == fs->lfs_ivnode || vp->v_type != VREG) {
2057 mutex_exit(&vp->v_interlock);
2058 return 0;
2059 }
2060
2061 /*
2062 * If there are no pages, don't do anything.
2063 */
2064 if (vp->v_uobj.uo_npages == 0) {
2065 if (TAILQ_EMPTY(&vp->v_uobj.memq) &&
2066 (vp->v_iflag & VI_ONWORKLST) &&
2067 LIST_FIRST(&vp->v_dirtyblkhd) == NULL) {
2068 vp->v_iflag &= ~VI_WRMAPDIRTY;
2069 vn_syncer_remove_from_worklist(vp);
2070 }
2071 mutex_exit(&vp->v_interlock);
2072
2073 /* Remove us from paging queue, if we were on it */
2074 mutex_enter(&lfs_lock);
2075 if (ip->i_flags & IN_PAGING) {
2076 ip->i_flags &= ~IN_PAGING;
2077 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
2078 }
2079 mutex_exit(&lfs_lock);
2080 return 0;
2081 }
2082
2083 blkeof = blkroundup(fs, ip->i_size);
2084
2085 /*
2086 * Ignore requests to free pages past EOF but in the same block
2087 * as EOF, unless the request is synchronous. (If the request is
2088 * sync, it comes from lfs_truncate.)
2089 * XXXUBC Make these pages look "active" so the pagedaemon won't
2090 * XXXUBC bother us with them again.
2091 */
2092 if (!sync && ap->a_offlo >= ip->i_size && ap->a_offlo < blkeof) {
2093 origoffset = ap->a_offlo;
2094 for (off = origoffset; off < blkeof; off += fs->lfs_bsize) {
2095 pg = uvm_pagelookup(&vp->v_uobj, off);
2096 KASSERT(pg != NULL);
2097 while (pg->flags & PG_BUSY) {
2098 pg->flags |= PG_WANTED;
2099 UVM_UNLOCK_AND_WAIT(pg, &vp->v_interlock, 0,
2100 "lfsput2", 0);
2101 mutex_enter(&vp->v_interlock);
2102 }
2103 mutex_enter(&uvm_pageqlock);
2104 uvm_pageactivate(pg);
2105 mutex_exit(&uvm_pageqlock);
2106 }
2107 ap->a_offlo = blkeof;
2108 if (ap->a_offhi > 0 && ap->a_offhi <= ap->a_offlo) {
2109 mutex_exit(&vp->v_interlock);
2110 return 0;
2111 }
2112 }
2113
2114 /*
2115 * Extend page range to start and end at block boundaries.
2116 * (For the purposes of VOP_PUTPAGES, fragments don't exist.)
2117 */
2118 origoffset = ap->a_offlo;
2119 origendoffset = ap->a_offhi;
2120 startoffset = origoffset & ~(fs->lfs_bmask);
2121 max_endoffset = (trunc_page(LLONG_MAX) >> fs->lfs_bshift)
2122 << fs->lfs_bshift;
2123
2124 if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
2125 endoffset = max_endoffset;
2126 origendoffset = endoffset;
2127 } else {
2128 origendoffset = round_page(ap->a_offhi);
2129 endoffset = round_page(blkroundup(fs, origendoffset));
2130 }
2131
2132 KASSERT(startoffset > 0 || endoffset >= startoffset);
2133 if (startoffset == endoffset) {
2134 /* Nothing to do, why were we called? */
2135 mutex_exit(&vp->v_interlock);
2136 DLOG((DLOG_PAGE, "lfs_putpages: startoffset = endoffset = %"
2137 PRId64 "\n", startoffset));
2138 return 0;
2139 }
2140
2141 ap->a_offlo = startoffset;
2142 ap->a_offhi = endoffset;
2143
2144 /*
2145 * If not cleaning, just send the pages through genfs_putpages
2146 * to be returned to the pool.
2147 */
2148 if (!(ap->a_flags & PGO_CLEANIT))
2149 return genfs_putpages(v);
2150
2151 /* Set PGO_BUSYFAIL to avoid deadlocks */
2152 ap->a_flags |= PGO_BUSYFAIL;
2153
2154 /*
2155 * Likewise, if we are asked to clean but the pages are not
2156 * dirty, we can just free them using genfs_putpages.
2157 */
2158 #ifdef DEBUG
2159 debug_n_dirtyclean = 0;
2160 #endif
2161 do {
2162 int r;
2163
2164 /* Count the number of dirty pages */
2165 r = check_dirty(fs, vp, startoffset, endoffset, blkeof,
2166 ap->a_flags, 1, NULL);
2167 if (r < 0) {
2168 /* Pages are busy with another process */
2169 mutex_exit(&vp->v_interlock);
2170 return EDEADLK;
2171 }
2172 if (r > 0) /* Some pages are dirty */
2173 break;
2174
2175 /*
2176 * Sometimes pages are dirtied between the time that
2177 * we check and the time we try to clean them.
2178 * Instruct lfs_gop_write to return EDEADLK in this case
2179 * so we can write them properly.
2180 */
2181 ip->i_lfs_iflags |= LFSI_NO_GOP_WRITE;
2182 r = genfs_do_putpages(vp, startoffset, endoffset,
2183 ap->a_flags & ~PGO_SYNCIO, &busypg);
2184 ip->i_lfs_iflags &= ~LFSI_NO_GOP_WRITE;
2185 if (r != EDEADLK)
2186 return r;
2187
2188 /* One of the pages was busy. Start over. */
2189 mutex_enter(&vp->v_interlock);
2190 wait_for_page(vp, busypg, "dirtyclean");
2191 #ifdef DEBUG
2192 ++debug_n_dirtyclean;
2193 #endif
2194 } while(1);
2195
2196 #ifdef DEBUG
2197 if (debug_n_dirtyclean > TOOMANY)
2198 printf("lfs_putpages: dirtyclean: looping, n = %d\n",
2199 debug_n_dirtyclean);
2200 #endif
2201
2202 /*
2203 * Dirty and asked to clean.
2204 *
2205 * Pagedaemon can't actually write LFS pages; wake up
2206 * the writer to take care of that. The writer will
2207 * notice the pager inode queue and act on that.
2208 *
2209 * XXX We must drop the vp->interlock before taking the lfs_lock or we
2210 * get a nasty deadlock with lfs_flush_pchain().
2211 */
2212 if (pagedaemon) {
2213 mutex_exit(&vp->v_interlock);
2214 mutex_enter(&lfs_lock);
2215 if (!(ip->i_flags & IN_PAGING)) {
2216 ip->i_flags |= IN_PAGING;
2217 TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip, i_lfs_pchain);
2218 }
2219 wakeup(&lfs_writer_daemon);
2220 mutex_exit(&lfs_lock);
2221 preempt();
2222 return EWOULDBLOCK;
2223 }
2224
2225 /*
2226 * If this is a file created in a recent dirop, we can't flush its
2227 * inode until the dirop is complete. Drain dirops, then flush the
2228 * filesystem (taking care of any other pending dirops while we're
2229 * at it).
2230 */
2231 if ((ap->a_flags & (PGO_CLEANIT|PGO_LOCKED)) == PGO_CLEANIT &&
2232 (vp->v_uflag & VU_DIROP)) {
2233 int locked;
2234
2235 DLOG((DLOG_PAGE, "lfs_putpages: flushing VU_DIROP\n"));
2236 /* XXX VOP_ISLOCKED() may not be used for lock decisions. */
2237 locked = (VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
2238 mutex_exit(&vp->v_interlock);
2239 lfs_writer_enter(fs, "ppdirop");
2240 if (locked)
2241 VOP_UNLOCK(vp); /* XXX why? */
2242
2243 mutex_enter(&lfs_lock);
2244 lfs_flush_fs(fs, sync ? SEGM_SYNC : 0);
2245 mutex_exit(&lfs_lock);
2246
2247 if (locked)
2248 VOP_LOCK(vp, LK_EXCLUSIVE);
2249 mutex_enter(&vp->v_interlock);
2250 lfs_writer_leave(fs);
2251
2252 /* XXX the flush should have taken care of this one too! */
2253 }
2254
2255 /*
2256 * This is it. We are going to write some pages. From here on
2257 * down it's all just mechanics.
2258 *
2259 * Don't let genfs_putpages wait; lfs_segunlock will wait for us.
2260 */
2261 ap->a_flags &= ~PGO_SYNCIO;
2262
2263 /*
2264 * If we've already got the seglock, flush the node and return.
2265 * The FIP has already been set up for us by lfs_writefile,
2266 * and FIP cleanup and lfs_updatemeta will also be done there,
2267 * unless genfs_putpages returns EDEADLK; then we must flush
2268 * what we have, and correct FIP and segment header accounting.
2269 */
2270 get_seglock:
2271 /*
2272 * If we are not called with the segment locked, lock it.
2273 * Account for a new FIP in the segment header, and set sp->vp.
2274 * (This should duplicate the setup at the top of lfs_writefile().)
2275 */
2276 seglocked = (ap->a_flags & PGO_LOCKED) != 0;
2277 if (!seglocked) {
2278 mutex_exit(&vp->v_interlock);
2279 error = lfs_seglock(fs, SEGM_PROT | (sync ? SEGM_SYNC : 0));
2280 if (error != 0)
2281 return error;
2282 mutex_enter(&vp->v_interlock);
2283 lfs_acquire_finfo(fs, ip->i_number, ip->i_gen);
2284 }
2285 sp = fs->lfs_sp;
2286 KASSERT(sp->vp == NULL);
2287 sp->vp = vp;
2288
2289 /*
2290 * Ensure that the partial segment is marked SS_DIROP if this
2291 * vnode is a DIROP.
2292 */
2293 if (!seglocked && vp->v_uflag & VU_DIROP)
2294 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
2295
2296 /*
2297 * Loop over genfs_putpages until all pages are gathered.
2298 * genfs_putpages() drops the interlock, so reacquire it if necessary.
2299 * Whenever we lose the interlock we have to rerun check_dirty, as
2300 * well, since more pages might have been dirtied in our absence.
2301 */
2302 #ifdef DEBUG
2303 debug_n_again = 0;
2304 #endif
2305 do {
2306 busypg = NULL;
2307 if (check_dirty(fs, vp, startoffset, endoffset, blkeof,
2308 ap->a_flags, 0, &busypg) < 0) {
2309 mutex_exit(&vp->v_interlock);
2310
2311 mutex_enter(&vp->v_interlock);
2312 write_and_wait(fs, vp, busypg, seglocked, NULL);
2313 if (!seglocked) {
2314 mutex_exit(&vp->v_interlock);
2315 lfs_release_finfo(fs);
2316 lfs_segunlock(fs);
2317 mutex_enter(&vp->v_interlock);
2318 }
2319 sp->vp = NULL;
2320 goto get_seglock;
2321 }
2322
2323 busypg = NULL;
2324 error = genfs_do_putpages(vp, startoffset, endoffset,
2325 ap->a_flags, &busypg);
2326
2327 if (error == EDEADLK || error == EAGAIN) {
2328 DLOG((DLOG_PAGE, "lfs_putpages: genfs_putpages returned"
2329 " %d ino %d off %x (seg %d)\n", error,
2330 ip->i_number, fs->lfs_offset,
2331 dtosn(fs, fs->lfs_offset)));
2332
2333 mutex_enter(&vp->v_interlock);
2334 write_and_wait(fs, vp, busypg, seglocked, "again");
2335 }
2336 #ifdef DEBUG
2337 ++debug_n_again;
2338 #endif
2339 } while (error == EDEADLK);
2340 #ifdef DEBUG
2341 if (debug_n_again > TOOMANY)
2342 printf("lfs_putpages: again: looping, n = %d\n", debug_n_again);
2343 #endif
2344
2345 KASSERT(sp != NULL && sp->vp == vp);
2346 if (!seglocked) {
2347 sp->vp = NULL;
2348
2349 /* Write indirect blocks as well */
2350 lfs_gather(fs, fs->lfs_sp, vp, lfs_match_indir);
2351 lfs_gather(fs, fs->lfs_sp, vp, lfs_match_dindir);
2352 lfs_gather(fs, fs->lfs_sp, vp, lfs_match_tindir);
2353
2354 KASSERT(sp->vp == NULL);
2355 sp->vp = vp;
2356 }
2357
2358 /*
2359 * Blocks are now gathered into a segment waiting to be written.
2360 * All that's left to do is update metadata, and write them.
2361 */
2362 lfs_updatemeta(sp);
2363 KASSERT(sp->vp == vp);
2364 sp->vp = NULL;
2365
2366 /*
2367 * If we were called from lfs_writefile, we don't need to clean up
2368 * the FIP or unlock the segment lock. We're done.
2369 */
2370 if (seglocked)
2371 return error;
2372
2373 /* Clean up FIP and send it to disk. */
2374 lfs_release_finfo(fs);
2375 lfs_writeseg(fs, fs->lfs_sp);
2376
2377 /*
2378 * Remove us from paging queue if we wrote all our pages.
2379 */
2380 if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
2381 mutex_enter(&lfs_lock);
2382 if (ip->i_flags & IN_PAGING) {
2383 ip->i_flags &= ~IN_PAGING;
2384 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
2385 }
2386 mutex_exit(&lfs_lock);
2387 }
2388
2389 /*
2390 * XXX - with the malloc/copy writeseg, the pages are freed by now
2391 * even if we don't wait (e.g. if we hold a nested lock). This
2392 * will not be true if we stop using malloc/copy.
2393 */
2394 KASSERT(fs->lfs_sp->seg_flags & SEGM_PROT);
2395 lfs_segunlock(fs);
2396
2397 /*
2398 * Wait for v_numoutput to drop to zero. The seglock should
2399 * take care of this, but there is a slight possibility that
2400 * aiodoned might not have got around to our buffers yet.
2401 */
2402 if (sync) {
2403 mutex_enter(&vp->v_interlock);
2404 while (vp->v_numoutput > 0) {
2405 DLOG((DLOG_PAGE, "lfs_putpages: ino %d sleeping on"
2406 " num %d\n", ip->i_number, vp->v_numoutput));
2407 cv_wait(&vp->v_cv, &vp->v_interlock);
2408 }
2409 mutex_exit(&vp->v_interlock);
2410 }
2411 return error;
2412 }
2413
2414 /*
2415 * Return the last logical file offset that should be written for this file
2416 * if we're doing a write that ends at "size". If writing, we need to know
2417 * about sizes on disk, i.e. fragments if there are any; if reading, we need
2418 * to know about entire blocks.
2419 */
2420 void
2421 lfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
2422 {
2423 struct inode *ip = VTOI(vp);
2424 struct lfs *fs = ip->i_lfs;
2425 daddr_t olbn, nlbn;
2426
2427 olbn = lblkno(fs, ip->i_size);
2428 nlbn = lblkno(fs, size);
2429 if (!(flags & GOP_SIZE_MEM) && nlbn < NDADDR && olbn <= nlbn) {
2430 *eobp = fragroundup(fs, size);
2431 } else {
2432 *eobp = blkroundup(fs, size);
2433 }
2434 }
2435
2436 #ifdef DEBUG
2437 void lfs_dump_vop(void *);
2438
2439 void
2440 lfs_dump_vop(void *v)
2441 {
2442 struct vop_putpages_args /* {
2443 struct vnode *a_vp;
2444 voff_t a_offlo;
2445 voff_t a_offhi;
2446 int a_flags;
2447 } */ *ap = v;
2448
2449 #ifdef DDB
2450 vfs_vnode_print(ap->a_vp, 0, printf);
2451 #endif
2452 lfs_dump_dinode(VTOI(ap->a_vp)->i_din.ffs1_din);
2453 }
2454 #endif
2455
2456 int
2457 lfs_mmap(void *v)
2458 {
2459 struct vop_mmap_args /* {
2460 const struct vnodeop_desc *a_desc;
2461 struct vnode *a_vp;
2462 vm_prot_t a_prot;
2463 kauth_cred_t a_cred;
2464 } */ *ap = v;
2465
2466 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM)
2467 return EOPNOTSUPP;
2468 return ufs_mmap(v);
2469 }
2470