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