lfs_vnops.c revision 1.190 1 /* $NetBSD: lfs_vnops.c,v 1.190 2006/09/28 23:08:23 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.190 2006/09/28 23:08:23 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 error = ltsleep(&fs->lfs_dirops, (PRIBIO + 1) | PCATCH,
401 "lfs_sdirop", 0, &fs->lfs_interlock);
402 if (error == EINTR) {
403 simple_unlock(&fs->lfs_interlock);
404 goto unreserve;
405 }
406 }
407 simple_lock(&lfs_subsys_lock);
408 if (lfs_dirvcount > LFS_MAX_DIROP && fs->lfs_dirops == 0) {
409 wakeup(&lfs_writer_daemon);
410 simple_unlock(&lfs_subsys_lock);
411 simple_unlock(&fs->lfs_interlock);
412 preempt(1);
413 goto restart;
414 }
415
416 if (lfs_dirvcount > LFS_MAX_DIROP) {
417 simple_unlock(&fs->lfs_interlock);
418 DLOG((DLOG_DIROP, "lfs_set_dirop: sleeping with dirops=%d, "
419 "dirvcount=%d\n", fs->lfs_dirops, lfs_dirvcount));
420 if ((error = ltsleep(&lfs_dirvcount,
421 PCATCH | PUSER | PNORELOCK, "lfs_maxdirop", 0,
422 &lfs_subsys_lock)) != 0) {
423 goto unreserve;
424 }
425 goto restart;
426 }
427 simple_unlock(&lfs_subsys_lock);
428
429 ++fs->lfs_dirops;
430 fs->lfs_doifile = 1;
431 simple_unlock(&fs->lfs_interlock);
432
433 /* Hold a reference so SET_ENDOP will be happy */
434 vref(dvp);
435 if (vp) {
436 vref(vp);
437 MARK_VNODE(vp);
438 }
439
440 MARK_VNODE(dvp);
441 return 0;
442
443 unreserve:
444 lfs_reserve(fs, dvp, vp, -LFS_NRESERVE(fs));
445 return error;
446 }
447
448 /*
449 * Get a new vnode *before* adjusting the dirop count, to avoid a deadlock
450 * in getnewvnode(), if we have a stacked filesystem mounted on top
451 * of us.
452 *
453 * NB: this means we have to clear the new vnodes on error. Fortunately
454 * SET_ENDOP is there to do that for us.
455 */
456 static int
457 lfs_set_dirop_create(struct vnode *dvp, struct vnode **vpp)
458 {
459 int error;
460 struct lfs *fs;
461
462 fs = VFSTOUFS(dvp->v_mount)->um_lfs;
463 ASSERT_NO_SEGLOCK(fs);
464 if (fs->lfs_ronly)
465 return EROFS;
466 if (vpp && (error = getnewvnode(VT_LFS, dvp->v_mount, lfs_vnodeop_p, vpp))) {
467 DLOG((DLOG_ALLOC, "lfs_set_dirop_create: dvp %p error %d\n",
468 dvp, error));
469 return error;
470 }
471 if ((error = lfs_set_dirop(dvp, NULL)) != 0) {
472 if (vpp) {
473 ungetnewvnode(*vpp);
474 *vpp = NULL;
475 }
476 return error;
477 }
478 return 0;
479 }
480
481 #define SET_ENDOP_BASE(fs, dvp, str) \
482 do { \
483 simple_lock(&(fs)->lfs_interlock); \
484 --(fs)->lfs_dirops; \
485 if (!(fs)->lfs_dirops) { \
486 if ((fs)->lfs_nadirop) { \
487 panic("SET_ENDOP: %s: no dirops but " \
488 " nadirop=%d", (str), \
489 (fs)->lfs_nadirop); \
490 } \
491 wakeup(&(fs)->lfs_writer); \
492 simple_unlock(&(fs)->lfs_interlock); \
493 lfs_check((dvp), LFS_UNUSED_LBN, 0); \
494 } else \
495 simple_unlock(&(fs)->lfs_interlock); \
496 } while(0)
497 #define SET_ENDOP_CREATE(fs, dvp, nvpp, str) \
498 do { \
499 UNMARK_VNODE(dvp); \
500 if (nvpp && *nvpp) \
501 UNMARK_VNODE(*nvpp); \
502 /* Check for error return to stem vnode leakage */ \
503 if (nvpp && *nvpp && !((*nvpp)->v_flag & VDIROP)) \
504 ungetnewvnode(*(nvpp)); \
505 SET_ENDOP_BASE((fs), (dvp), (str)); \
506 lfs_reserve((fs), (dvp), NULL, -LFS_NRESERVE(fs)); \
507 vrele(dvp); \
508 } while(0)
509 #define SET_ENDOP_CREATE_AP(ap, str) \
510 SET_ENDOP_CREATE(VTOI((ap)->a_dvp)->i_lfs, (ap)->a_dvp, \
511 (ap)->a_vpp, (str))
512 #define SET_ENDOP_REMOVE(fs, dvp, ovp, str) \
513 do { \
514 UNMARK_VNODE(dvp); \
515 if (ovp) \
516 UNMARK_VNODE(ovp); \
517 SET_ENDOP_BASE((fs), (dvp), (str)); \
518 lfs_reserve((fs), (dvp), (ovp), -LFS_NRESERVE(fs)); \
519 vrele(dvp); \
520 if (ovp) \
521 vrele(ovp); \
522 } while(0)
523
524 void
525 lfs_mark_vnode(struct vnode *vp)
526 {
527 struct inode *ip = VTOI(vp);
528 struct lfs *fs = ip->i_lfs;
529
530 simple_lock(&fs->lfs_interlock);
531 if (!(ip->i_flag & IN_ADIROP)) {
532 if (!(vp->v_flag & VDIROP)) {
533 (void)lfs_vref(vp);
534 simple_lock(&lfs_subsys_lock);
535 ++lfs_dirvcount;
536 ++fs->lfs_dirvcount;
537 simple_unlock(&lfs_subsys_lock);
538 TAILQ_INSERT_TAIL(&fs->lfs_dchainhd, ip, i_lfs_dchain);
539 vp->v_flag |= VDIROP;
540 }
541 ++fs->lfs_nadirop;
542 ip->i_flag |= IN_ADIROP;
543 } else
544 KASSERT(vp->v_flag & VDIROP);
545 simple_unlock(&fs->lfs_interlock);
546 }
547
548 void
549 lfs_unmark_vnode(struct vnode *vp)
550 {
551 struct inode *ip = VTOI(vp);
552
553 if (ip && (ip->i_flag & IN_ADIROP)) {
554 KASSERT(vp->v_flag & VDIROP);
555 simple_lock(&ip->i_lfs->lfs_interlock);
556 --ip->i_lfs->lfs_nadirop;
557 simple_unlock(&ip->i_lfs->lfs_interlock);
558 ip->i_flag &= ~IN_ADIROP;
559 }
560 }
561
562 int
563 lfs_symlink(void *v)
564 {
565 struct vop_symlink_args /* {
566 struct vnode *a_dvp;
567 struct vnode **a_vpp;
568 struct componentname *a_cnp;
569 struct vattr *a_vap;
570 char *a_target;
571 } */ *ap = v;
572 int error;
573
574 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
575 vput(ap->a_dvp);
576 return error;
577 }
578 error = ufs_symlink(ap);
579 SET_ENDOP_CREATE_AP(ap, "symlink");
580 return (error);
581 }
582
583 int
584 lfs_mknod(void *v)
585 {
586 struct vop_mknod_args /* {
587 struct vnode *a_dvp;
588 struct vnode **a_vpp;
589 struct componentname *a_cnp;
590 struct vattr *a_vap;
591 } */ *ap = v;
592 struct vattr *vap = ap->a_vap;
593 struct vnode **vpp = ap->a_vpp;
594 struct inode *ip;
595 int error;
596 struct mount *mp;
597 ino_t ino;
598
599 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
600 vput(ap->a_dvp);
601 return error;
602 }
603 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
604 ap->a_dvp, vpp, ap->a_cnp);
605
606 /* Either way we're done with the dirop at this point */
607 SET_ENDOP_CREATE_AP(ap, "mknod");
608
609 if (error)
610 return (error);
611
612 ip = VTOI(*vpp);
613 mp = (*vpp)->v_mount;
614 ino = ip->i_number;
615 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
616 if (vap->va_rdev != VNOVAL) {
617 /*
618 * Want to be able to use this to make badblock
619 * inodes, so don't truncate the dev number.
620 */
621 #if 0
622 ip->i_ffs1_rdev = ufs_rw32(vap->va_rdev,
623 UFS_MPNEEDSWAP((*vpp)->v_mount));
624 #else
625 ip->i_ffs1_rdev = vap->va_rdev;
626 #endif
627 }
628
629 /*
630 * Call fsync to write the vnode so that we don't have to deal with
631 * flushing it when it's marked VDIROP|VXLOCK.
632 *
633 * XXX KS - If we can't flush we also can't call vgone(), so must
634 * return. But, that leaves this vnode in limbo, also not good.
635 * Can this ever happen (barring hardware failure)?
636 */
637 if ((error = VOP_FSYNC(*vpp, NOCRED, FSYNC_WAIT, 0, 0,
638 curlwp)) != 0) {
639 panic("lfs_mknod: couldn't fsync (ino %llu)",
640 (unsigned long long)ino);
641 /* return (error); */
642 }
643 /*
644 * Remove vnode so that it will be reloaded by VFS_VGET and
645 * checked to see if it is an alias of an existing entry in
646 * the inode cache.
647 */
648 /* Used to be vput, but that causes us to call VOP_INACTIVE twice. */
649
650 VOP_UNLOCK(*vpp, 0);
651 lfs_vunref(*vpp);
652 (*vpp)->v_type = VNON;
653 vgone(*vpp);
654 error = VFS_VGET(mp, ino, vpp);
655
656 if (error != 0) {
657 *vpp = NULL;
658 return (error);
659 }
660 return (0);
661 }
662
663 int
664 lfs_create(void *v)
665 {
666 struct vop_create_args /* {
667 struct vnode *a_dvp;
668 struct vnode **a_vpp;
669 struct componentname *a_cnp;
670 struct vattr *a_vap;
671 } */ *ap = v;
672 int error;
673
674 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
675 vput(ap->a_dvp);
676 return error;
677 }
678 error = ufs_create(ap);
679 SET_ENDOP_CREATE_AP(ap, "create");
680 return (error);
681 }
682
683 int
684 lfs_mkdir(void *v)
685 {
686 struct vop_mkdir_args /* {
687 struct vnode *a_dvp;
688 struct vnode **a_vpp;
689 struct componentname *a_cnp;
690 struct vattr *a_vap;
691 } */ *ap = v;
692 int error;
693
694 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
695 vput(ap->a_dvp);
696 return error;
697 }
698 error = ufs_mkdir(ap);
699 SET_ENDOP_CREATE_AP(ap, "mkdir");
700 return (error);
701 }
702
703 int
704 lfs_remove(void *v)
705 {
706 struct vop_remove_args /* {
707 struct vnode *a_dvp;
708 struct vnode *a_vp;
709 struct componentname *a_cnp;
710 } */ *ap = v;
711 struct vnode *dvp, *vp;
712 struct inode *ip;
713 int error;
714
715 dvp = ap->a_dvp;
716 vp = ap->a_vp;
717 ip = VTOI(vp);
718 if ((error = SET_DIROP_REMOVE(dvp, vp)) != 0) {
719 if (dvp == vp)
720 vrele(vp);
721 else
722 vput(vp);
723 vput(dvp);
724 return error;
725 }
726 error = ufs_remove(ap);
727 if (ip->i_nlink == 0)
728 lfs_orphan(ip->i_lfs, ip->i_number);
729 SET_ENDOP_REMOVE(ip->i_lfs, dvp, ap->a_vp, "remove");
730 return (error);
731 }
732
733 int
734 lfs_rmdir(void *v)
735 {
736 struct vop_rmdir_args /* {
737 struct vnodeop_desc *a_desc;
738 struct vnode *a_dvp;
739 struct vnode *a_vp;
740 struct componentname *a_cnp;
741 } */ *ap = v;
742 struct vnode *vp;
743 struct inode *ip;
744 int error;
745
746 vp = ap->a_vp;
747 ip = VTOI(vp);
748 if ((error = SET_DIROP_REMOVE(ap->a_dvp, ap->a_vp)) != 0) {
749 vrele(ap->a_dvp);
750 if (ap->a_vp != ap->a_dvp)
751 VOP_UNLOCK(ap->a_dvp, 0);
752 vput(vp);
753 return error;
754 }
755 error = ufs_rmdir(ap);
756 if (ip->i_nlink == 0)
757 lfs_orphan(ip->i_lfs, ip->i_number);
758 SET_ENDOP_REMOVE(ip->i_lfs, ap->a_dvp, ap->a_vp, "rmdir");
759 return (error);
760 }
761
762 int
763 lfs_link(void *v)
764 {
765 struct vop_link_args /* {
766 struct vnode *a_dvp;
767 struct vnode *a_vp;
768 struct componentname *a_cnp;
769 } */ *ap = v;
770 int error;
771 struct vnode **vpp = NULL;
772
773 if ((error = SET_DIROP_CREATE(ap->a_dvp, vpp)) != 0) {
774 vput(ap->a_dvp);
775 return error;
776 }
777 error = ufs_link(ap);
778 SET_ENDOP_CREATE(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, vpp, "link");
779 return (error);
780 }
781
782 int
783 lfs_rename(void *v)
784 {
785 struct vop_rename_args /* {
786 struct vnode *a_fdvp;
787 struct vnode *a_fvp;
788 struct componentname *a_fcnp;
789 struct vnode *a_tdvp;
790 struct vnode *a_tvp;
791 struct componentname *a_tcnp;
792 } */ *ap = v;
793 struct vnode *tvp, *fvp, *tdvp, *fdvp;
794 struct componentname *tcnp, *fcnp;
795 int error;
796 struct lfs *fs;
797
798 fs = VTOI(ap->a_fdvp)->i_lfs;
799 tvp = ap->a_tvp;
800 tdvp = ap->a_tdvp;
801 tcnp = ap->a_tcnp;
802 fvp = ap->a_fvp;
803 fdvp = ap->a_fdvp;
804 fcnp = ap->a_fcnp;
805
806 /*
807 * Check for cross-device rename.
808 * If it is, we don't want to set dirops, just error out.
809 * (In particular note that MARK_VNODE(tdvp) will DTWT on
810 * a cross-device rename.)
811 *
812 * Copied from ufs_rename.
813 */
814 if ((fvp->v_mount != tdvp->v_mount) ||
815 (tvp && (fvp->v_mount != tvp->v_mount))) {
816 error = EXDEV;
817 goto errout;
818 }
819
820 /*
821 * Check to make sure we're not renaming a vnode onto itself
822 * (deleting a hard link by renaming one name onto another);
823 * if we are we can't recursively call VOP_REMOVE since that
824 * would leave us with an unaccounted-for number of live dirops.
825 *
826 * Inline the relevant section of ufs_rename here, *before*
827 * calling SET_DIROP_REMOVE.
828 */
829 if (tvp && ((VTOI(tvp)->i_flags & (IMMUTABLE | APPEND)) ||
830 (VTOI(tdvp)->i_flags & APPEND))) {
831 error = EPERM;
832 goto errout;
833 }
834 if (fvp == tvp) {
835 if (fvp->v_type == VDIR) {
836 error = EINVAL;
837 goto errout;
838 }
839
840 /* Release destination completely. */
841 VOP_ABORTOP(tdvp, tcnp);
842 vput(tdvp);
843 vput(tvp);
844
845 /* Delete source. */
846 vrele(fvp);
847 fcnp->cn_flags &= ~(MODMASK | SAVESTART);
848 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
849 fcnp->cn_nameiop = DELETE;
850 if ((error = relookup(fdvp, &fvp, fcnp))){
851 /* relookup blew away fdvp */
852 return (error);
853 }
854 return (VOP_REMOVE(fdvp, fvp, fcnp));
855 }
856
857 if ((error = SET_DIROP_REMOVE(tdvp, tvp)) != 0)
858 goto errout;
859 MARK_VNODE(fdvp);
860 MARK_VNODE(fvp);
861
862 error = ufs_rename(ap);
863 UNMARK_VNODE(fdvp);
864 UNMARK_VNODE(fvp);
865 SET_ENDOP_REMOVE(fs, tdvp, tvp, "rename");
866 return (error);
867
868 errout:
869 VOP_ABORTOP(tdvp, ap->a_tcnp); /* XXX, why not in NFS? */
870 if (tdvp == tvp)
871 vrele(tdvp);
872 else
873 vput(tdvp);
874 if (tvp)
875 vput(tvp);
876 VOP_ABORTOP(fdvp, ap->a_fcnp); /* XXX, why not in NFS? */
877 vrele(fdvp);
878 vrele(fvp);
879 return (error);
880 }
881
882 /* XXX hack to avoid calling ITIMES in getattr */
883 int
884 lfs_getattr(void *v)
885 {
886 struct vop_getattr_args /* {
887 struct vnode *a_vp;
888 struct vattr *a_vap;
889 kauth_cred_t a_cred;
890 struct lwp *a_l;
891 } */ *ap = v;
892 struct vnode *vp = ap->a_vp;
893 struct inode *ip = VTOI(vp);
894 struct vattr *vap = ap->a_vap;
895 struct lfs *fs = ip->i_lfs;
896 /*
897 * Copy from inode table
898 */
899 vap->va_fsid = ip->i_dev;
900 vap->va_fileid = ip->i_number;
901 vap->va_mode = ip->i_mode & ~IFMT;
902 vap->va_nlink = ip->i_nlink;
903 vap->va_uid = ip->i_uid;
904 vap->va_gid = ip->i_gid;
905 vap->va_rdev = (dev_t)ip->i_ffs1_rdev;
906 vap->va_size = vp->v_size;
907 vap->va_atime.tv_sec = ip->i_ffs1_atime;
908 vap->va_atime.tv_nsec = ip->i_ffs1_atimensec;
909 vap->va_mtime.tv_sec = ip->i_ffs1_mtime;
910 vap->va_mtime.tv_nsec = ip->i_ffs1_mtimensec;
911 vap->va_ctime.tv_sec = ip->i_ffs1_ctime;
912 vap->va_ctime.tv_nsec = ip->i_ffs1_ctimensec;
913 vap->va_flags = ip->i_flags;
914 vap->va_gen = ip->i_gen;
915 /* this doesn't belong here */
916 if (vp->v_type == VBLK)
917 vap->va_blocksize = BLKDEV_IOSIZE;
918 else if (vp->v_type == VCHR)
919 vap->va_blocksize = MAXBSIZE;
920 else
921 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
922 vap->va_bytes = fsbtob(fs, (u_quad_t)ip->i_lfs_effnblks);
923 vap->va_type = vp->v_type;
924 vap->va_filerev = ip->i_modrev;
925 return (0);
926 }
927
928 /*
929 * Check to make sure the inode blocks won't choke the buffer
930 * cache, then call ufs_setattr as usual.
931 */
932 int
933 lfs_setattr(void *v)
934 {
935 struct vop_setattr_args /* {
936 struct vnode *a_vp;
937 struct vattr *a_vap;
938 kauth_cred_t a_cred;
939 struct lwp *a_l;
940 } */ *ap = v;
941 struct vnode *vp = ap->a_vp;
942
943 lfs_check(vp, LFS_UNUSED_LBN, 0);
944 return ufs_setattr(v);
945 }
946
947 /*
948 * Release the block we hold on lfs_newseg wrapping. Called on file close,
949 * or explicitly from LFCNWRAPGO. Called with the interlock held.
950 */
951 static int
952 lfs_wrapgo(struct lfs *fs, struct inode *ip, int waitfor)
953 {
954 if (lockstatus(&fs->lfs_stoplock) != LK_EXCLUSIVE)
955 return EBUSY;
956
957 lockmgr(&fs->lfs_stoplock, LK_RELEASE, &fs->lfs_interlock);
958
959 KASSERT(fs->lfs_nowrap > 0);
960 if (fs->lfs_nowrap <= 0) {
961 return 0;
962 }
963
964 if (--fs->lfs_nowrap == 0) {
965 log(LOG_NOTICE, "%s: re-enabled log wrap\n", fs->lfs_fsmnt);
966 wakeup(&fs->lfs_wrappass);
967 lfs_wakeup_cleaner(fs);
968 }
969 if (waitfor) {
970 ltsleep(&fs->lfs_nextseg, PCATCH | PUSER,
971 "segment", 0, &fs->lfs_interlock);
972 }
973
974 return 0;
975 }
976
977 /*
978 * Close called
979 */
980 /* ARGSUSED */
981 int
982 lfs_close(void *v)
983 {
984 struct vop_close_args /* {
985 struct vnode *a_vp;
986 int a_fflag;
987 kauth_cred_t a_cred;
988 struct lwp *a_l;
989 } */ *ap = v;
990 struct vnode *vp = ap->a_vp;
991 struct inode *ip = VTOI(vp);
992 struct lfs *fs = ip->i_lfs;
993
994 if ((ip->i_number == ROOTINO || ip->i_number == LFS_IFILE_INUM) &&
995 lockstatus(&fs->lfs_stoplock) == LK_EXCLUSIVE) {
996 simple_lock(&fs->lfs_interlock);
997 log(LOG_NOTICE, "lfs_close: releasing log wrap control\n");
998 lfs_wrapgo(fs, ip, 0);
999 simple_unlock(&fs->lfs_interlock);
1000 }
1001
1002 if (vp == ip->i_lfs->lfs_ivnode &&
1003 vp->v_mount->mnt_iflag & IMNT_UNMOUNT)
1004 return 0;
1005
1006 if (vp->v_usecount > 1 && vp != ip->i_lfs->lfs_ivnode) {
1007 LFS_ITIMES(ip, NULL, NULL, NULL);
1008 }
1009 return (0);
1010 }
1011
1012 /*
1013 * Close wrapper for special devices.
1014 *
1015 * Update the times on the inode then do device close.
1016 */
1017 int
1018 lfsspec_close(void *v)
1019 {
1020 struct vop_close_args /* {
1021 struct vnode *a_vp;
1022 int a_fflag;
1023 kauth_cred_t a_cred;
1024 struct lwp *a_l;
1025 } */ *ap = v;
1026 struct vnode *vp;
1027 struct inode *ip;
1028
1029 vp = ap->a_vp;
1030 ip = VTOI(vp);
1031 if (vp->v_usecount > 1) {
1032 LFS_ITIMES(ip, NULL, NULL, NULL);
1033 }
1034 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
1035 }
1036
1037 /*
1038 * Close wrapper for fifo's.
1039 *
1040 * Update the times on the inode then do device close.
1041 */
1042 int
1043 lfsfifo_close(void *v)
1044 {
1045 struct vop_close_args /* {
1046 struct vnode *a_vp;
1047 int a_fflag;
1048 kauth_cred_ a_cred;
1049 struct lwp *a_l;
1050 } */ *ap = v;
1051 struct vnode *vp;
1052 struct inode *ip;
1053
1054 vp = ap->a_vp;
1055 ip = VTOI(vp);
1056 if (ap->a_vp->v_usecount > 1) {
1057 LFS_ITIMES(ip, NULL, NULL, NULL);
1058 }
1059 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
1060 }
1061
1062 /*
1063 * Reclaim an inode so that it can be used for other purposes.
1064 */
1065
1066 int
1067 lfs_reclaim(void *v)
1068 {
1069 struct vop_reclaim_args /* {
1070 struct vnode *a_vp;
1071 struct lwp *a_l;
1072 } */ *ap = v;
1073 struct vnode *vp = ap->a_vp;
1074 struct inode *ip = VTOI(vp);
1075 int error;
1076
1077 KASSERT(ip->i_nlink == ip->i_ffs_effnlink);
1078
1079 LFS_CLR_UINO(ip, IN_ALLMOD);
1080 if ((error = ufs_reclaim(vp, ap->a_l)))
1081 return (error);
1082 pool_put(&lfs_dinode_pool, ip->i_din.ffs1_din);
1083 lfs_deregister_all(vp);
1084 pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
1085 ip->inode_ext.lfs = NULL;
1086 pool_put(&lfs_inode_pool, vp->v_data);
1087 vp->v_data = NULL;
1088 return (0);
1089 }
1090
1091 /*
1092 * Read a block from a storage device.
1093 * In order to avoid reading blocks that are in the process of being
1094 * written by the cleaner---and hence are not mutexed by the normal
1095 * buffer cache / page cache mechanisms---check for collisions before
1096 * reading.
1097 *
1098 * We inline ufs_strategy to make sure that the VOP_BMAP occurs *before*
1099 * the active cleaner test.
1100 *
1101 * XXX This code assumes that lfs_markv makes synchronous checkpoints.
1102 */
1103 int
1104 lfs_strategy(void *v)
1105 {
1106 struct vop_strategy_args /* {
1107 struct vnode *a_vp;
1108 struct buf *a_bp;
1109 } */ *ap = v;
1110 struct buf *bp;
1111 struct lfs *fs;
1112 struct vnode *vp;
1113 struct inode *ip;
1114 daddr_t tbn;
1115 int i, sn, error, slept;
1116
1117 bp = ap->a_bp;
1118 vp = ap->a_vp;
1119 ip = VTOI(vp);
1120 fs = ip->i_lfs;
1121
1122 /* lfs uses its strategy routine only for read */
1123 KASSERT(bp->b_flags & B_READ);
1124
1125 if (vp->v_type == VBLK || vp->v_type == VCHR)
1126 panic("lfs_strategy: spec");
1127 KASSERT(bp->b_bcount != 0);
1128 if (bp->b_blkno == bp->b_lblkno) {
1129 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1130 NULL);
1131 if (error) {
1132 bp->b_error = error;
1133 bp->b_flags |= B_ERROR;
1134 biodone(bp);
1135 return (error);
1136 }
1137 if ((long)bp->b_blkno == -1) /* no valid data */
1138 clrbuf(bp);
1139 }
1140 if ((long)bp->b_blkno < 0) { /* block is not on disk */
1141 biodone(bp);
1142 return (0);
1143 }
1144
1145 slept = 1;
1146 simple_lock(&fs->lfs_interlock);
1147 while (slept && fs->lfs_seglock) {
1148 simple_unlock(&fs->lfs_interlock);
1149 /*
1150 * Look through list of intervals.
1151 * There will only be intervals to look through
1152 * if the cleaner holds the seglock.
1153 * Since the cleaner is synchronous, we can trust
1154 * the list of intervals to be current.
1155 */
1156 tbn = dbtofsb(fs, bp->b_blkno);
1157 sn = dtosn(fs, tbn);
1158 slept = 0;
1159 for (i = 0; i < fs->lfs_cleanind; i++) {
1160 if (sn == dtosn(fs, fs->lfs_cleanint[i]) &&
1161 tbn >= fs->lfs_cleanint[i]) {
1162 DLOG((DLOG_CLEAN,
1163 "lfs_strategy: ino %d lbn %" PRId64
1164 " ind %d sn %d fsb %" PRIx32
1165 " given sn %d fsb %" PRIx64 "\n",
1166 ip->i_number, bp->b_lblkno, i,
1167 dtosn(fs, fs->lfs_cleanint[i]),
1168 fs->lfs_cleanint[i], sn, tbn));
1169 DLOG((DLOG_CLEAN,
1170 "lfs_strategy: sleeping on ino %d lbn %"
1171 PRId64 "\n", ip->i_number, bp->b_lblkno));
1172 simple_lock(&fs->lfs_interlock);
1173 if (LFS_SEGLOCK_HELD(fs) && fs->lfs_iocount) {
1174 /* Cleaner can't wait for itself */
1175 ltsleep(&fs->lfs_iocount,
1176 (PRIBIO + 1) | PNORELOCK,
1177 "clean2", 0,
1178 &fs->lfs_interlock);
1179 slept = 1;
1180 break;
1181 } else if (fs->lfs_seglock) {
1182 ltsleep(&fs->lfs_seglock,
1183 (PRIBIO + 1) | PNORELOCK,
1184 "clean1", 0,
1185 &fs->lfs_interlock);
1186 slept = 1;
1187 break;
1188 }
1189 simple_unlock(&fs->lfs_interlock);
1190 }
1191 }
1192 simple_lock(&fs->lfs_interlock);
1193 }
1194 simple_unlock(&fs->lfs_interlock);
1195
1196 vp = ip->i_devvp;
1197 VOP_STRATEGY(vp, bp);
1198 return (0);
1199 }
1200
1201 void
1202 lfs_flush_dirops(struct lfs *fs)
1203 {
1204 struct inode *ip, *nip;
1205 struct vnode *vp;
1206 extern int lfs_dostats;
1207 struct segment *sp;
1208 int waslocked;
1209
1210 ASSERT_MAYBE_SEGLOCK(fs);
1211 KASSERT(fs->lfs_nadirop == 0);
1212
1213 if (fs->lfs_ronly)
1214 return;
1215
1216 simple_lock(&fs->lfs_interlock);
1217 if (TAILQ_FIRST(&fs->lfs_dchainhd) == NULL) {
1218 simple_unlock(&fs->lfs_interlock);
1219 return;
1220 } else
1221 simple_unlock(&fs->lfs_interlock);
1222
1223 if (lfs_dostats)
1224 ++lfs_stats.flush_invoked;
1225
1226 /*
1227 * Inline lfs_segwrite/lfs_writevnodes, but just for dirops.
1228 * Technically this is a checkpoint (the on-disk state is valid)
1229 * even though we are leaving out all the file data.
1230 */
1231 lfs_imtime(fs);
1232 lfs_seglock(fs, SEGM_CKP);
1233 sp = fs->lfs_sp;
1234
1235 /*
1236 * lfs_writevnodes, optimized to get dirops out of the way.
1237 * Only write dirops, and don't flush files' pages, only
1238 * blocks from the directories.
1239 *
1240 * We don't need to vref these files because they are
1241 * dirops and so hold an extra reference until the
1242 * segunlock clears them of that status.
1243 *
1244 * We don't need to check for IN_ADIROP because we know that
1245 * no dirops are active.
1246 *
1247 */
1248 simple_lock(&fs->lfs_interlock);
1249 for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
1250 nip = TAILQ_NEXT(ip, i_lfs_dchain);
1251 simple_unlock(&fs->lfs_interlock);
1252 vp = ITOV(ip);
1253
1254 KASSERT((ip->i_flag & IN_ADIROP) == 0);
1255
1256 /*
1257 * All writes to directories come from dirops; all
1258 * writes to files' direct blocks go through the page
1259 * cache, which we're not touching. Reads to files
1260 * and/or directories will not be affected by writing
1261 * directory blocks inodes and file inodes. So we don't
1262 * really need to lock. If we don't lock, though,
1263 * make sure that we don't clear IN_MODIFIED
1264 * unnecessarily.
1265 */
1266 if (vp->v_flag & (VXLOCK | VFREEING)) {
1267 simple_lock(&fs->lfs_interlock);
1268 continue;
1269 }
1270 waslocked = VOP_ISLOCKED(vp);
1271 if (vp->v_type != VREG &&
1272 ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp))) {
1273 lfs_writefile(fs, sp, vp);
1274 if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
1275 !(ip->i_flag & IN_ALLMOD)) {
1276 LFS_SET_UINO(ip, IN_MODIFIED);
1277 }
1278 }
1279 KDASSERT(ip->i_number != LFS_IFILE_INUM);
1280 (void) lfs_writeinode(fs, sp, ip);
1281 if (waslocked == LK_EXCLOTHER)
1282 LFS_SET_UINO(ip, IN_MODIFIED);
1283 simple_lock(&fs->lfs_interlock);
1284 }
1285 simple_unlock(&fs->lfs_interlock);
1286 /* We've written all the dirops there are */
1287 ((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
1288 lfs_finalize_fs_seguse(fs);
1289 (void) lfs_writeseg(fs, sp);
1290 lfs_segunlock(fs);
1291 }
1292
1293 /*
1294 * Flush all vnodes for which the pagedaemon has requested pageouts.
1295 * Skip over any files that are marked VDIROP (since lfs_flush_dirop()
1296 * has just run, this would be an error). If we have to skip a vnode
1297 * for any reason, just skip it; if we have to wait for the cleaner,
1298 * abort. The writer daemon will call us again later.
1299 */
1300 void
1301 lfs_flush_pchain(struct lfs *fs)
1302 {
1303 struct inode *ip, *nip;
1304 struct vnode *vp;
1305 extern int lfs_dostats;
1306 struct segment *sp;
1307 int error;
1308
1309 ASSERT_NO_SEGLOCK(fs);
1310
1311 if (fs->lfs_ronly)
1312 return;
1313
1314 simple_lock(&fs->lfs_interlock);
1315 if (TAILQ_FIRST(&fs->lfs_pchainhd) == NULL) {
1316 simple_unlock(&fs->lfs_interlock);
1317 return;
1318 } else
1319 simple_unlock(&fs->lfs_interlock);
1320
1321 /* Get dirops out of the way */
1322 lfs_flush_dirops(fs);
1323
1324 if (lfs_dostats)
1325 ++lfs_stats.flush_invoked;
1326
1327 /*
1328 * Inline lfs_segwrite/lfs_writevnodes, but just for pageouts.
1329 */
1330 lfs_imtime(fs);
1331 lfs_seglock(fs, 0);
1332 sp = fs->lfs_sp;
1333
1334 /*
1335 * lfs_writevnodes, optimized to clear pageout requests.
1336 * Only write non-dirop files that are in the pageout queue.
1337 * We're very conservative about what we write; we want to be
1338 * fast and async.
1339 */
1340 simple_lock(&fs->lfs_interlock);
1341 top:
1342 for (ip = TAILQ_FIRST(&fs->lfs_pchainhd); ip != NULL; ip = nip) {
1343 nip = TAILQ_NEXT(ip, i_lfs_pchain);
1344 vp = ITOV(ip);
1345
1346 if (!(ip->i_flags & IN_PAGING))
1347 goto top;
1348
1349 if (vp->v_flag & (VXLOCK|VDIROP))
1350 continue;
1351 if (vp->v_type != VREG)
1352 continue;
1353 if (lfs_vref(vp))
1354 continue;
1355 simple_unlock(&fs->lfs_interlock);
1356
1357 if (VOP_ISLOCKED(vp)) {
1358 lfs_vunref(vp);
1359 simple_lock(&fs->lfs_interlock);
1360 continue;
1361 }
1362
1363 error = lfs_writefile(fs, sp, vp);
1364 if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
1365 !(ip->i_flag & IN_ALLMOD)) {
1366 LFS_SET_UINO(ip, IN_MODIFIED);
1367 }
1368 KDASSERT(ip->i_number != LFS_IFILE_INUM);
1369 (void) lfs_writeinode(fs, sp, ip);
1370
1371 lfs_vunref(vp);
1372
1373 if (error == EAGAIN) {
1374 lfs_writeseg(fs, sp);
1375 simple_lock(&fs->lfs_interlock);
1376 break;
1377 }
1378 simple_lock(&fs->lfs_interlock);
1379 }
1380 simple_unlock(&fs->lfs_interlock);
1381 (void) lfs_writeseg(fs, sp);
1382 lfs_segunlock(fs);
1383 }
1384
1385 /*
1386 * Provide a fcntl interface to sys_lfs_{segwait,bmapv,markv}.
1387 */
1388 int
1389 lfs_fcntl(void *v)
1390 {
1391 struct vop_fcntl_args /* {
1392 struct vnode *a_vp;
1393 u_long a_command;
1394 caddr_t a_data;
1395 int a_fflag;
1396 kauth_cred_t a_cred;
1397 struct lwp *a_l;
1398 } */ *ap = v;
1399 struct timeval *tvp;
1400 BLOCK_INFO *blkiov;
1401 CLEANERINFO *cip;
1402 SEGUSE *sup;
1403 int blkcnt, error, oclean;
1404 size_t fh_size;
1405 struct lfs_fcntl_markv blkvp;
1406 struct lwp *l;
1407 fsid_t *fsidp;
1408 struct lfs *fs;
1409 struct buf *bp;
1410 fhandle_t *fhp;
1411 daddr_t off;
1412
1413 /* Only respect LFS fcntls on fs root or Ifile */
1414 if (VTOI(ap->a_vp)->i_number != ROOTINO &&
1415 VTOI(ap->a_vp)->i_number != LFS_IFILE_INUM) {
1416 return ufs_fcntl(v);
1417 }
1418
1419 /* Avoid locking a draining lock */
1420 if (ap->a_vp->v_mount->mnt_iflag & IMNT_UNMOUNT) {
1421 return ESHUTDOWN;
1422 }
1423
1424 /* LFS control and monitoring fcntls are available only to root */
1425 l = ap->a_l;
1426 if (((ap->a_command & 0xff00) >> 8) == 'L' &&
1427 (error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
1428 &l->l_acflag)) != 0)
1429 return (error);
1430
1431 fs = VTOI(ap->a_vp)->i_lfs;
1432 fsidp = &ap->a_vp->v_mount->mnt_stat.f_fsidx;
1433
1434 error = 0;
1435 switch (ap->a_command) {
1436 case LFCNSEGWAITALL:
1437 case LFCNSEGWAITALL_COMPAT:
1438 fsidp = NULL;
1439 /* FALLSTHROUGH */
1440 case LFCNSEGWAIT:
1441 case LFCNSEGWAIT_COMPAT:
1442 tvp = (struct timeval *)ap->a_data;
1443 simple_lock(&fs->lfs_interlock);
1444 ++fs->lfs_sleepers;
1445 simple_unlock(&fs->lfs_interlock);
1446
1447 error = lfs_segwait(fsidp, tvp);
1448
1449 simple_lock(&fs->lfs_interlock);
1450 if (--fs->lfs_sleepers == 0)
1451 wakeup(&fs->lfs_sleepers);
1452 simple_unlock(&fs->lfs_interlock);
1453 return error;
1454
1455 case LFCNBMAPV:
1456 case LFCNMARKV:
1457 blkvp = *(struct lfs_fcntl_markv *)ap->a_data;
1458
1459 blkcnt = blkvp.blkcnt;
1460 if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
1461 return (EINVAL);
1462 blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
1463 if ((error = copyin(blkvp.blkiov, blkiov,
1464 blkcnt * sizeof(BLOCK_INFO))) != 0) {
1465 lfs_free(fs, blkiov, LFS_NB_BLKIOV);
1466 return error;
1467 }
1468
1469 simple_lock(&fs->lfs_interlock);
1470 ++fs->lfs_sleepers;
1471 simple_unlock(&fs->lfs_interlock);
1472 if (ap->a_command == LFCNBMAPV)
1473 error = lfs_bmapv(l->l_proc, fsidp, blkiov, blkcnt);
1474 else /* LFCNMARKV */
1475 error = lfs_markv(l->l_proc, fsidp, blkiov, blkcnt);
1476 if (error == 0)
1477 error = copyout(blkiov, blkvp.blkiov,
1478 blkcnt * sizeof(BLOCK_INFO));
1479 simple_lock(&fs->lfs_interlock);
1480 if (--fs->lfs_sleepers == 0)
1481 wakeup(&fs->lfs_sleepers);
1482 simple_unlock(&fs->lfs_interlock);
1483 lfs_free(fs, blkiov, LFS_NB_BLKIOV);
1484 return error;
1485
1486 case LFCNRECLAIM:
1487 /*
1488 * Flush dirops and write Ifile, allowing empty segments
1489 * to be immediately reclaimed.
1490 */
1491 lfs_writer_enter(fs, "pndirop");
1492 off = fs->lfs_offset;
1493 lfs_seglock(fs, SEGM_FORCE_CKP | SEGM_CKP);
1494 lfs_flush_dirops(fs);
1495 LFS_CLEANERINFO(cip, fs, bp);
1496 oclean = cip->clean;
1497 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1498 lfs_segwrite(ap->a_vp->v_mount, SEGM_FORCE_CKP);
1499 fs->lfs_sp->seg_flags |= SEGM_PROT;
1500 lfs_segunlock(fs);
1501 lfs_writer_leave(fs);
1502
1503 #ifdef DEBUG
1504 LFS_CLEANERINFO(cip, fs, bp);
1505 DLOG((DLOG_CLEAN, "lfs_fcntl: reclaim wrote %" PRId64
1506 " blocks, cleaned %" PRId32 " segments (activesb %d)\n",
1507 fs->lfs_offset - off, cip->clean - oclean,
1508 fs->lfs_activesb));
1509 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
1510 #endif
1511
1512 return 0;
1513
1514 #ifdef COMPAT_30
1515 case LFCNIFILEFH_COMPAT:
1516 /* Return the filehandle of the Ifile */
1517 if ((error = kauth_authorize_generic(l->l_cred,
1518 KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
1519 return (error);
1520 fhp = (struct fhandle *)ap->a_data;
1521 fhp->fh_fsid = *fsidp;
1522 fh_size = 16; /* former VFS_MAXFIDSIZ */
1523 return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
1524 #endif
1525
1526 case LFCNIFILEFH_COMPAT2:
1527 case LFCNIFILEFH:
1528 /* Return the filehandle of the Ifile */
1529 fhp = (struct fhandle *)ap->a_data;
1530 fhp->fh_fsid = *fsidp;
1531 fh_size = sizeof(struct lfs_fhandle) -
1532 offsetof(fhandle_t, fh_fid);
1533 return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
1534
1535 case LFCNREWIND:
1536 /* Move lfs_offset to the lowest-numbered segment */
1537 return lfs_rewind(fs, *(int *)ap->a_data);
1538
1539 case LFCNINVAL:
1540 /* Mark a segment SEGUSE_INVAL */
1541 LFS_SEGENTRY(sup, fs, *(int *)ap->a_data, bp);
1542 if (sup->su_nbytes > 0) {
1543 brelse(bp);
1544 lfs_unset_inval_all(fs);
1545 return EBUSY;
1546 }
1547 sup->su_flags |= SEGUSE_INVAL;
1548 VOP_BWRITE(bp);
1549 return 0;
1550
1551 case LFCNRESIZE:
1552 /* Resize the filesystem */
1553 return lfs_resize_fs(fs, *(int *)ap->a_data);
1554
1555 case LFCNWRAPSTOP:
1556 case LFCNWRAPSTOP_COMPAT:
1557 /*
1558 * Hold lfs_newseg at segment 0; if requested, sleep until
1559 * the filesystem wraps around. To support external agents
1560 * (dump, fsck-based regression test) that need to look at
1561 * a snapshot of the filesystem, without necessarily
1562 * requiring that all fs activity stops.
1563 */
1564 if (lockstatus(&fs->lfs_stoplock))
1565 return EALREADY;
1566
1567 simple_lock(&fs->lfs_interlock);
1568 lockmgr(&fs->lfs_stoplock, LK_EXCLUSIVE, &fs->lfs_interlock);
1569 if (fs->lfs_nowrap == 0)
1570 log(LOG_NOTICE, "%s: disabled log wrap\n", fs->lfs_fsmnt);
1571 ++fs->lfs_nowrap;
1572 if (*(int *)ap->a_data == 1 ||
1573 ap->a_command == LFCNWRAPSTOP_COMPAT) {
1574 log(LOG_NOTICE, "LFCNSTOPWRAP waiting for log wrap\n");
1575 error = ltsleep(&fs->lfs_nowrap, PCATCH | PUSER,
1576 "segwrap", 0, &fs->lfs_interlock);
1577 log(LOG_NOTICE, "LFCNSTOPWRAP done waiting\n");
1578 if (error) {
1579 lfs_wrapgo(fs, VTOI(ap->a_vp), 0);
1580 }
1581 }
1582 simple_unlock(&fs->lfs_interlock);
1583 return 0;
1584
1585 case LFCNWRAPGO:
1586 case LFCNWRAPGO_COMPAT:
1587 /*
1588 * Having done its work, the agent wakes up the writer.
1589 * If the argument is 1, it sleeps until a new segment
1590 * is selected.
1591 */
1592 simple_lock(&fs->lfs_interlock);
1593 error = lfs_wrapgo(fs, VTOI(ap->a_vp),
1594 (ap->a_command == LFCNWRAPGO_COMPAT ? 1 :
1595 *((int *)ap->a_data)));
1596 simple_unlock(&fs->lfs_interlock);
1597 return error;
1598
1599 case LFCNWRAPPASS:
1600 if (lockstatus(&fs->lfs_stoplock) != LK_EXCLUSIVE)
1601 return EALREADY;
1602 if ((VTOI(ap->a_vp)->i_lfs_iflags & LFSI_WRAPWAIT))
1603 return EALREADY;
1604 simple_lock(&fs->lfs_interlock);
1605 if (fs->lfs_nowrap == 0) {
1606 simple_unlock(&fs->lfs_interlock);
1607 return EBUSY;
1608 }
1609 fs->lfs_wrappass = 1;
1610 wakeup(&fs->lfs_wrappass);
1611 /* Wait for the log to wrap, if asked */
1612 if (*(int *)ap->a_data) {
1613 lfs_vref(ap->a_vp);
1614 VTOI(ap->a_vp)->i_lfs_iflags |= LFSI_WRAPWAIT;
1615 log(LOG_NOTICE, "LFCNPASS waiting for log wrap\n");
1616 error = ltsleep(&fs->lfs_nowrap, PCATCH | PUSER,
1617 "segwrap", 0, &fs->lfs_interlock);
1618 log(LOG_NOTICE, "LFCNPASS done waiting\n");
1619 VTOI(ap->a_vp)->i_lfs_iflags &= ~LFSI_WRAPWAIT;
1620 lfs_vunref(ap->a_vp);
1621 }
1622 simple_unlock(&fs->lfs_interlock);
1623 return error;
1624
1625 case LFCNWRAPSTATUS:
1626 simple_lock(&fs->lfs_interlock);
1627 *(int *)ap->a_data = fs->lfs_wrapstatus;
1628 simple_unlock(&fs->lfs_interlock);
1629 return 0;
1630
1631 default:
1632 return ufs_fcntl(v);
1633 }
1634 return 0;
1635 }
1636
1637 int
1638 lfs_getpages(void *v)
1639 {
1640 struct vop_getpages_args /* {
1641 struct vnode *a_vp;
1642 voff_t a_offset;
1643 struct vm_page **a_m;
1644 int *a_count;
1645 int a_centeridx;
1646 vm_prot_t a_access_type;
1647 int a_advice;
1648 int a_flags;
1649 } */ *ap = v;
1650
1651 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM &&
1652 (ap->a_access_type & VM_PROT_WRITE) != 0) {
1653 return EPERM;
1654 }
1655 if ((ap->a_access_type & VM_PROT_WRITE) != 0) {
1656 LFS_SET_UINO(VTOI(ap->a_vp), IN_MODIFIED);
1657 }
1658
1659 /*
1660 * we're relying on the fact that genfs_getpages() always read in
1661 * entire filesystem blocks.
1662 */
1663 return genfs_getpages(v);
1664 }
1665
1666 /*
1667 * Make sure that for all pages in every block in the given range,
1668 * either all are dirty or all are clean. If any of the pages
1669 * we've seen so far are dirty, put the vnode on the paging chain,
1670 * and mark it IN_PAGING.
1671 *
1672 * If checkfirst != 0, don't check all the pages but return at the
1673 * first dirty page.
1674 */
1675 static int
1676 check_dirty(struct lfs *fs, struct vnode *vp,
1677 off_t startoffset, off_t endoffset, off_t blkeof,
1678 int flags, int checkfirst)
1679 {
1680 int by_list;
1681 struct vm_page *curpg = NULL; /* XXX: gcc */
1682 struct vm_page *pgs[MAXBSIZE / PAGE_SIZE], *pg;
1683 off_t soff = 0; /* XXX: gcc */
1684 voff_t off;
1685 int i;
1686 int nonexistent;
1687 int any_dirty; /* number of dirty pages */
1688 int dirty; /* number of dirty pages in a block */
1689 int tdirty;
1690 int pages_per_block = fs->lfs_bsize >> PAGE_SHIFT;
1691 int pagedaemon = (curproc == uvm.pagedaemon_proc);
1692
1693 ASSERT_MAYBE_SEGLOCK(fs);
1694 top:
1695 by_list = (vp->v_uobj.uo_npages <=
1696 ((endoffset - startoffset) >> PAGE_SHIFT) *
1697 UVM_PAGE_HASH_PENALTY);
1698 any_dirty = 0;
1699
1700 if (by_list) {
1701 curpg = TAILQ_FIRST(&vp->v_uobj.memq);
1702 } else {
1703 soff = startoffset;
1704 }
1705 while (by_list || soff < MIN(blkeof, endoffset)) {
1706 if (by_list) {
1707 /*
1708 * Find the first page in a block. Skip
1709 * blocks outside our area of interest or beyond
1710 * the end of file.
1711 */
1712 if (pages_per_block > 1) {
1713 while (curpg &&
1714 ((curpg->offset & fs->lfs_bmask) ||
1715 curpg->offset >= vp->v_size ||
1716 curpg->offset >= endoffset))
1717 curpg = TAILQ_NEXT(curpg, listq);
1718 }
1719 if (curpg == NULL)
1720 break;
1721 soff = curpg->offset;
1722 }
1723
1724 /*
1725 * Mark all pages in extended range busy; find out if any
1726 * of them are dirty.
1727 */
1728 nonexistent = dirty = 0;
1729 for (i = 0; i == 0 || i < pages_per_block; i++) {
1730 if (by_list && pages_per_block <= 1) {
1731 pgs[i] = pg = curpg;
1732 } else {
1733 off = soff + (i << PAGE_SHIFT);
1734 pgs[i] = pg = uvm_pagelookup(&vp->v_uobj, off);
1735 if (pg == NULL) {
1736 ++nonexistent;
1737 continue;
1738 }
1739 }
1740 KASSERT(pg != NULL);
1741
1742 /*
1743 * If we're holding the segment lock, we can deadlock
1744 * against a process that has our page and is waiting
1745 * for the cleaner, while the cleaner waits for the
1746 * segment lock. Just bail in that case.
1747 */
1748 if ((pg->flags & PG_BUSY) &&
1749 (pagedaemon || LFS_SEGLOCK_HELD(fs))) {
1750 if (by_list && i > 0)
1751 uvm_page_unbusy(pgs, i);
1752 DLOG((DLOG_PAGE, "lfs_putpages: avoiding 3-way or pagedaemon deadlock\n"));
1753 return -1;
1754 }
1755
1756 while (pg->flags & PG_BUSY) {
1757 pg->flags |= PG_WANTED;
1758 UVM_UNLOCK_AND_WAIT(pg, &vp->v_interlock, 0,
1759 "lfsput", 0);
1760 simple_lock(&vp->v_interlock);
1761 if (by_list) {
1762 if (i > 0)
1763 uvm_page_unbusy(pgs, i);
1764 goto top;
1765 }
1766 }
1767 pg->flags |= PG_BUSY;
1768 UVM_PAGE_OWN(pg, "lfs_putpages");
1769
1770 pmap_page_protect(pg, VM_PROT_NONE);
1771 tdirty = (pmap_clear_modify(pg) ||
1772 (pg->flags & PG_CLEAN) == 0);
1773 dirty += tdirty;
1774 }
1775 if (pages_per_block > 0 && nonexistent >= pages_per_block) {
1776 if (by_list) {
1777 curpg = TAILQ_NEXT(curpg, listq);
1778 } else {
1779 soff += fs->lfs_bsize;
1780 }
1781 continue;
1782 }
1783
1784 any_dirty += dirty;
1785 KASSERT(nonexistent == 0);
1786
1787 /*
1788 * If any are dirty make all dirty; unbusy them,
1789 * but if we were asked to clean, wire them so that
1790 * the pagedaemon doesn't bother us about them while
1791 * they're on their way to disk.
1792 */
1793 for (i = 0; i == 0 || i < pages_per_block; i++) {
1794 pg = pgs[i];
1795 KASSERT(!((pg->flags & PG_CLEAN) && (pg->flags & PG_DELWRI)));
1796 if (dirty) {
1797 pg->flags &= ~PG_CLEAN;
1798 if (flags & PGO_FREE) {
1799 /*
1800 * Wire the page so that
1801 * pdaemon doesn't see it again.
1802 */
1803 uvm_lock_pageq();
1804 uvm_pagewire(pg);
1805 uvm_unlock_pageq();
1806
1807 /* Suspended write flag */
1808 pg->flags |= PG_DELWRI;
1809 }
1810 }
1811 if (pg->flags & PG_WANTED)
1812 wakeup(pg);
1813 pg->flags &= ~(PG_WANTED|PG_BUSY);
1814 UVM_PAGE_OWN(pg, NULL);
1815 }
1816
1817 if (checkfirst && any_dirty)
1818 break;
1819
1820 if (by_list) {
1821 curpg = TAILQ_NEXT(curpg, listq);
1822 } else {
1823 soff += MAX(PAGE_SIZE, fs->lfs_bsize);
1824 }
1825 }
1826
1827 return any_dirty;
1828 }
1829
1830 /*
1831 * lfs_putpages functions like genfs_putpages except that
1832 *
1833 * (1) It needs to bounds-check the incoming requests to ensure that
1834 * they are block-aligned; if they are not, expand the range and
1835 * do the right thing in case, e.g., the requested range is clean
1836 * but the expanded range is dirty.
1837 *
1838 * (2) It needs to explicitly send blocks to be written when it is done.
1839 * VOP_PUTPAGES is not ever called with the seglock held, so
1840 * we simply take the seglock and let lfs_segunlock wait for us.
1841 * XXX Actually we can be called with the seglock held, if we have
1842 * XXX to flush a vnode while lfs_markv is in operation. As of this
1843 * XXX writing we panic in this case.
1844 *
1845 * Assumptions:
1846 *
1847 * (1) The caller does not hold any pages in this vnode busy. If it does,
1848 * there is a danger that when we expand the page range and busy the
1849 * pages we will deadlock.
1850 *
1851 * (2) We are called with vp->v_interlock held; we must return with it
1852 * released.
1853 *
1854 * (3) We don't absolutely have to free pages right away, provided that
1855 * the request does not have PGO_SYNCIO. When the pagedaemon gives
1856 * us a request with PGO_FREE, we take the pages out of the paging
1857 * queue and wake up the writer, which will handle freeing them for us.
1858 *
1859 * We ensure that for any filesystem block, all pages for that
1860 * block are either resident or not, even if those pages are higher
1861 * than EOF; that means that we will be getting requests to free
1862 * "unused" pages above EOF all the time, and should ignore them.
1863 *
1864 * (4) If we are called with PGO_LOCKED, the finfo array we are to write
1865 * into has been set up for us by lfs_writefile. If not, we will
1866 * have to handle allocating and/or freeing an finfo entry.
1867 *
1868 * XXX note that we're (ab)using PGO_LOCKED as "seglock held".
1869 */
1870
1871 int
1872 lfs_putpages(void *v)
1873 {
1874 int error;
1875 struct vop_putpages_args /* {
1876 struct vnode *a_vp;
1877 voff_t a_offlo;
1878 voff_t a_offhi;
1879 int a_flags;
1880 } */ *ap = v;
1881 struct vnode *vp;
1882 struct inode *ip;
1883 struct lfs *fs;
1884 struct segment *sp;
1885 off_t origoffset, startoffset, endoffset, origendoffset, blkeof;
1886 off_t off, max_endoffset;
1887 int s;
1888 boolean_t seglocked, sync, pagedaemon;
1889 struct vm_page *pg;
1890 UVMHIST_FUNC("lfs_putpages"); UVMHIST_CALLED(ubchist);
1891
1892 vp = ap->a_vp;
1893 ip = VTOI(vp);
1894 fs = ip->i_lfs;
1895 sync = (ap->a_flags & PGO_SYNCIO) != 0;
1896 pagedaemon = (curproc == uvm.pagedaemon_proc);
1897
1898 /* Putpages does nothing for metadata. */
1899 if (vp == fs->lfs_ivnode || vp->v_type != VREG) {
1900 simple_unlock(&vp->v_interlock);
1901 return 0;
1902 }
1903
1904 /*
1905 * If there are no pages, don't do anything.
1906 */
1907 if (vp->v_uobj.uo_npages == 0) {
1908 s = splbio();
1909 if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL &&
1910 (vp->v_flag & VONWORKLST)) {
1911 vp->v_flag &= ~VONWORKLST;
1912 LIST_REMOVE(vp, v_synclist);
1913 }
1914 splx(s);
1915 simple_unlock(&vp->v_interlock);
1916
1917 /* Remove us from paging queue, if we were on it */
1918 simple_lock(&fs->lfs_interlock);
1919 if (ip->i_flags & IN_PAGING) {
1920 ip->i_flags &= ~IN_PAGING;
1921 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
1922 }
1923 simple_unlock(&fs->lfs_interlock);
1924 return 0;
1925 }
1926
1927 blkeof = blkroundup(fs, ip->i_size);
1928
1929 /*
1930 * Ignore requests to free pages past EOF but in the same block
1931 * as EOF, unless the request is synchronous. (If the request is
1932 * sync, it comes from lfs_truncate.)
1933 * XXXUBC Make these pages look "active" so the pagedaemon won't
1934 * XXXUBC bother us with them again.
1935 */
1936 if (!sync && ap->a_offlo >= ip->i_size && ap->a_offlo < blkeof) {
1937 origoffset = ap->a_offlo;
1938 for (off = origoffset; off < blkeof; off += fs->lfs_bsize) {
1939 pg = uvm_pagelookup(&vp->v_uobj, off);
1940 KASSERT(pg != NULL);
1941 while (pg->flags & PG_BUSY) {
1942 pg->flags |= PG_WANTED;
1943 UVM_UNLOCK_AND_WAIT(pg, &vp->v_interlock, 0,
1944 "lfsput2", 0);
1945 simple_lock(&vp->v_interlock);
1946 }
1947 uvm_lock_pageq();
1948 uvm_pageactivate(pg);
1949 uvm_unlock_pageq();
1950 }
1951 ap->a_offlo = blkeof;
1952 if (ap->a_offhi > 0 && ap->a_offhi <= ap->a_offlo) {
1953 simple_unlock(&vp->v_interlock);
1954 return 0;
1955 }
1956 }
1957
1958 /*
1959 * Extend page range to start and end at block boundaries.
1960 * (For the purposes of VOP_PUTPAGES, fragments don't exist.)
1961 */
1962 origoffset = ap->a_offlo;
1963 origendoffset = ap->a_offhi;
1964 startoffset = origoffset & ~(fs->lfs_bmask);
1965 max_endoffset = (trunc_page(LLONG_MAX) >> fs->lfs_bshift)
1966 << fs->lfs_bshift;
1967
1968 if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
1969 endoffset = max_endoffset;
1970 origendoffset = endoffset;
1971 } else {
1972 origendoffset = round_page(ap->a_offhi);
1973 endoffset = round_page(blkroundup(fs, origendoffset));
1974 }
1975
1976 KASSERT(startoffset > 0 || endoffset >= startoffset);
1977 if (startoffset == endoffset) {
1978 /* Nothing to do, why were we called? */
1979 simple_unlock(&vp->v_interlock);
1980 DLOG((DLOG_PAGE, "lfs_putpages: startoffset = endoffset = %"
1981 PRId64 "\n", startoffset));
1982 return 0;
1983 }
1984
1985 ap->a_offlo = startoffset;
1986 ap->a_offhi = endoffset;
1987
1988 if (!(ap->a_flags & PGO_CLEANIT))
1989 return genfs_putpages(v);
1990
1991 /*
1992 * If there are more than one page per block, we don't want
1993 * to get caught locking them backwards; so set PGO_BUSYFAIL
1994 * to avoid deadlocks.
1995 */
1996 ap->a_flags |= PGO_BUSYFAIL;
1997
1998 do {
1999 int r;
2000
2001 /* If no pages are dirty, we can just use genfs_putpages. */
2002 r = check_dirty(fs, vp, startoffset, endoffset, blkeof,
2003 ap->a_flags, 1);
2004 if (r < 0) {
2005 simple_unlock(&vp->v_interlock);
2006 return EDEADLK;
2007 }
2008 if (r > 0)
2009 break;
2010
2011 /*
2012 * Sometimes pages are dirtied between the time that
2013 * we check and the time we try to clean them.
2014 * Instruct lfs_gop_write to return EDEADLK in this case
2015 * so we can write them properly.
2016 */
2017 ip->i_lfs_iflags |= LFSI_NO_GOP_WRITE;
2018 r = genfs_putpages(v);
2019 ip->i_lfs_iflags &= ~LFSI_NO_GOP_WRITE;
2020 if (r != EDEADLK)
2021 return r;
2022
2023 /* Start over. */
2024 preempt(1);
2025 simple_lock(&vp->v_interlock);
2026 } while(1);
2027
2028 /*
2029 * Dirty and asked to clean.
2030 *
2031 * Pagedaemon can't actually write LFS pages; wake up
2032 * the writer to take care of that. The writer will
2033 * notice the pager inode queue and act on that.
2034 */
2035 if (pagedaemon) {
2036 simple_lock(&fs->lfs_interlock);
2037 if (!(ip->i_flags & IN_PAGING)) {
2038 ip->i_flags |= IN_PAGING;
2039 TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip, i_lfs_pchain);
2040 }
2041 simple_lock(&lfs_subsys_lock);
2042 wakeup(&lfs_writer_daemon);
2043 simple_unlock(&lfs_subsys_lock);
2044 simple_unlock(&fs->lfs_interlock);
2045 simple_unlock(&vp->v_interlock);
2046 preempt(1);
2047 return EWOULDBLOCK;
2048 }
2049
2050 /*
2051 * If this is a file created in a recent dirop, we can't flush its
2052 * inode until the dirop is complete. Drain dirops, then flush the
2053 * filesystem (taking care of any other pending dirops while we're
2054 * at it).
2055 */
2056 if ((ap->a_flags & (PGO_CLEANIT|PGO_LOCKED)) == PGO_CLEANIT &&
2057 (vp->v_flag & VDIROP)) {
2058 int locked;
2059
2060 DLOG((DLOG_PAGE, "lfs_putpages: flushing VDIROP\n"));
2061 locked = (VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
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