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