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