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