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