1*0a6a1f1dSLionel Sambuc /* $NetBSD: lfs_vnops.c,v 1.293 2015/09/21 01:24:23 dholland Exp $ */
2d65f6f70SBen Gras
3d65f6f70SBen Gras /*-
4d65f6f70SBen Gras * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
5d65f6f70SBen Gras * All rights reserved.
6d65f6f70SBen Gras *
7d65f6f70SBen Gras * This code is derived from software contributed to The NetBSD Foundation
8d65f6f70SBen Gras * by Konrad E. Schroder <perseant@hhhh.org>.
9d65f6f70SBen Gras *
10d65f6f70SBen Gras * Redistribution and use in source and binary forms, with or without
11d65f6f70SBen Gras * modification, are permitted provided that the following conditions
12d65f6f70SBen Gras * are met:
13d65f6f70SBen Gras * 1. Redistributions of source code must retain the above copyright
14d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer.
15d65f6f70SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
16d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer in the
17d65f6f70SBen Gras * documentation and/or other materials provided with the distribution.
18d65f6f70SBen Gras *
19d65f6f70SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20d65f6f70SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21d65f6f70SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22d65f6f70SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23d65f6f70SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24d65f6f70SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25d65f6f70SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26d65f6f70SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27d65f6f70SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28d65f6f70SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29d65f6f70SBen Gras * POSSIBILITY OF SUCH DAMAGE.
30d65f6f70SBen Gras */
31d65f6f70SBen Gras /*
32d65f6f70SBen Gras * Copyright (c) 1986, 1989, 1991, 1993, 1995
33d65f6f70SBen Gras * The Regents of the University of California. All rights reserved.
34d65f6f70SBen Gras *
35d65f6f70SBen Gras * Redistribution and use in source and binary forms, with or without
36d65f6f70SBen Gras * modification, are permitted provided that the following conditions
37d65f6f70SBen Gras * are met:
38d65f6f70SBen Gras * 1. Redistributions of source code must retain the above copyright
39d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer.
40d65f6f70SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
41d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer in the
42d65f6f70SBen Gras * documentation and/or other materials provided with the distribution.
43d65f6f70SBen Gras * 3. Neither the name of the University nor the names of its contributors
44d65f6f70SBen Gras * may be used to endorse or promote products derived from this software
45d65f6f70SBen Gras * without specific prior written permission.
46d65f6f70SBen Gras *
47d65f6f70SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48d65f6f70SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49d65f6f70SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50d65f6f70SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51d65f6f70SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52d65f6f70SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53d65f6f70SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54d65f6f70SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55d65f6f70SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56d65f6f70SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57d65f6f70SBen Gras * SUCH DAMAGE.
58d65f6f70SBen Gras *
59d65f6f70SBen Gras * @(#)lfs_vnops.c 8.13 (Berkeley) 6/10/95
60d65f6f70SBen Gras */
61d65f6f70SBen Gras
62*0a6a1f1dSLionel Sambuc /* from NetBSD: ufs_vnops.c,v 1.213 2013/06/08 05:47:02 kardel Exp */
63*0a6a1f1dSLionel Sambuc /*-
64*0a6a1f1dSLionel Sambuc * Copyright (c) 2008 The NetBSD Foundation, Inc.
65*0a6a1f1dSLionel Sambuc * All rights reserved.
66*0a6a1f1dSLionel Sambuc *
67*0a6a1f1dSLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
68*0a6a1f1dSLionel Sambuc * by Wasabi Systems, Inc.
69*0a6a1f1dSLionel Sambuc *
70*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
71*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
72*0a6a1f1dSLionel Sambuc * are met:
73*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
74*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
75*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
76*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
77*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution.
78*0a6a1f1dSLionel Sambuc *
79*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
80*0a6a1f1dSLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
81*0a6a1f1dSLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
82*0a6a1f1dSLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
83*0a6a1f1dSLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
84*0a6a1f1dSLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
85*0a6a1f1dSLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
86*0a6a1f1dSLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
87*0a6a1f1dSLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
88*0a6a1f1dSLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
89*0a6a1f1dSLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
90*0a6a1f1dSLionel Sambuc */
91*0a6a1f1dSLionel Sambuc /*
92*0a6a1f1dSLionel Sambuc * Copyright (c) 1982, 1986, 1989, 1993, 1995
93*0a6a1f1dSLionel Sambuc * The Regents of the University of California. All rights reserved.
94*0a6a1f1dSLionel Sambuc * (c) UNIX System Laboratories, Inc.
95*0a6a1f1dSLionel Sambuc * All or some portions of this file are derived from material licensed
96*0a6a1f1dSLionel Sambuc * to the University of California by American Telephone and Telegraph
97*0a6a1f1dSLionel Sambuc * Co. or Unix System Laboratories, Inc. and are reproduced herein with
98*0a6a1f1dSLionel Sambuc * the permission of UNIX System Laboratories, Inc.
99*0a6a1f1dSLionel Sambuc *
100*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
101*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
102*0a6a1f1dSLionel Sambuc * are met:
103*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
104*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
105*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
106*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
107*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution.
108*0a6a1f1dSLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
109*0a6a1f1dSLionel Sambuc * may be used to endorse or promote products derived from this software
110*0a6a1f1dSLionel Sambuc * without specific prior written permission.
111*0a6a1f1dSLionel Sambuc *
112*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
113*0a6a1f1dSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
114*0a6a1f1dSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
115*0a6a1f1dSLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
116*0a6a1f1dSLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
117*0a6a1f1dSLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
118*0a6a1f1dSLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
119*0a6a1f1dSLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
120*0a6a1f1dSLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
121*0a6a1f1dSLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
122*0a6a1f1dSLionel Sambuc * SUCH DAMAGE.
123*0a6a1f1dSLionel Sambuc *
124*0a6a1f1dSLionel Sambuc * @(#)ufs_vnops.c 8.28 (Berkeley) 7/31/95
125*0a6a1f1dSLionel Sambuc */
126*0a6a1f1dSLionel Sambuc
127d65f6f70SBen Gras #include <sys/cdefs.h>
128*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: lfs_vnops.c,v 1.293 2015/09/21 01:24:23 dholland Exp $");
129d65f6f70SBen Gras
130d65f6f70SBen Gras #ifdef _KERNEL_OPT
131d65f6f70SBen Gras #include "opt_compat_netbsd.h"
132d65f6f70SBen Gras #include "opt_uvm_page_trkown.h"
133d65f6f70SBen Gras #endif
134d65f6f70SBen Gras
135d65f6f70SBen Gras #include <sys/param.h>
136d65f6f70SBen Gras #include <sys/systm.h>
137d65f6f70SBen Gras #include <sys/namei.h>
138d65f6f70SBen Gras #include <sys/resourcevar.h>
139d65f6f70SBen Gras #include <sys/kernel.h>
140d65f6f70SBen Gras #include <sys/file.h>
141d65f6f70SBen Gras #include <sys/stat.h>
142d65f6f70SBen Gras #include <sys/buf.h>
143d65f6f70SBen Gras #include <sys/proc.h>
144d65f6f70SBen Gras #include <sys/mount.h>
145d65f6f70SBen Gras #include <sys/vnode.h>
146d65f6f70SBen Gras #include <sys/pool.h>
147d65f6f70SBen Gras #include <sys/signalvar.h>
148d65f6f70SBen Gras #include <sys/kauth.h>
149d65f6f70SBen Gras #include <sys/syslog.h>
150d65f6f70SBen Gras #include <sys/fstrans.h>
151d65f6f70SBen Gras
152d65f6f70SBen Gras #include <miscfs/fifofs/fifo.h>
153d65f6f70SBen Gras #include <miscfs/genfs/genfs.h>
154d65f6f70SBen Gras #include <miscfs/specfs/specdev.h>
155d65f6f70SBen Gras
15684d9c625SLionel Sambuc #include <ufs/lfs/ulfs_inode.h>
15784d9c625SLionel Sambuc #include <ufs/lfs/ulfsmount.h>
15884d9c625SLionel Sambuc #include <ufs/lfs/ulfs_bswap.h>
15984d9c625SLionel Sambuc #include <ufs/lfs/ulfs_extern.h>
160d65f6f70SBen Gras
161d65f6f70SBen Gras #include <uvm/uvm.h>
162d65f6f70SBen Gras #include <uvm/uvm_pmap.h>
163d65f6f70SBen Gras #include <uvm/uvm_stat.h>
164d65f6f70SBen Gras #include <uvm/uvm_pager.h>
165d65f6f70SBen Gras
166d65f6f70SBen Gras #include <ufs/lfs/lfs.h>
167*0a6a1f1dSLionel Sambuc #include <ufs/lfs/lfs_accessors.h>
16884d9c625SLionel Sambuc #include <ufs/lfs/lfs_kernel.h>
169d65f6f70SBen Gras #include <ufs/lfs/lfs_extern.h>
170d65f6f70SBen Gras
171d65f6f70SBen Gras extern pid_t lfs_writer_daemon;
172d65f6f70SBen Gras int lfs_ignore_lazy_sync = 1;
173d65f6f70SBen Gras
17484d9c625SLionel Sambuc static int lfs_openextattr(void *v);
17584d9c625SLionel Sambuc static int lfs_closeextattr(void *v);
17684d9c625SLionel Sambuc static int lfs_getextattr(void *v);
17784d9c625SLionel Sambuc static int lfs_setextattr(void *v);
17884d9c625SLionel Sambuc static int lfs_listextattr(void *v);
17984d9c625SLionel Sambuc static int lfs_deleteextattr(void *v);
18084d9c625SLionel Sambuc
181d65f6f70SBen Gras /* Global vfs data structures for lfs. */
182d65f6f70SBen Gras int (**lfs_vnodeop_p)(void *);
183d65f6f70SBen Gras const struct vnodeopv_entry_desc lfs_vnodeop_entries[] = {
184d65f6f70SBen Gras { &vop_default_desc, vn_default_error },
18584d9c625SLionel Sambuc { &vop_lookup_desc, ulfs_lookup }, /* lookup */
186d65f6f70SBen Gras { &vop_create_desc, lfs_create }, /* create */
18784d9c625SLionel Sambuc { &vop_whiteout_desc, ulfs_whiteout }, /* whiteout */
188d65f6f70SBen Gras { &vop_mknod_desc, lfs_mknod }, /* mknod */
18984d9c625SLionel Sambuc { &vop_open_desc, ulfs_open }, /* open */
190d65f6f70SBen Gras { &vop_close_desc, lfs_close }, /* close */
19184d9c625SLionel Sambuc { &vop_access_desc, ulfs_access }, /* access */
192d65f6f70SBen Gras { &vop_getattr_desc, lfs_getattr }, /* getattr */
193d65f6f70SBen Gras { &vop_setattr_desc, lfs_setattr }, /* setattr */
194d65f6f70SBen Gras { &vop_read_desc, lfs_read }, /* read */
195d65f6f70SBen Gras { &vop_write_desc, lfs_write }, /* write */
196*0a6a1f1dSLionel Sambuc { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
197*0a6a1f1dSLionel Sambuc { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
19884d9c625SLionel Sambuc { &vop_ioctl_desc, ulfs_ioctl }, /* ioctl */
199d65f6f70SBen Gras { &vop_fcntl_desc, lfs_fcntl }, /* fcntl */
20084d9c625SLionel Sambuc { &vop_poll_desc, ulfs_poll }, /* poll */
201d65f6f70SBen Gras { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
20284d9c625SLionel Sambuc { &vop_revoke_desc, ulfs_revoke }, /* revoke */
203d65f6f70SBen Gras { &vop_mmap_desc, lfs_mmap }, /* mmap */
204d65f6f70SBen Gras { &vop_fsync_desc, lfs_fsync }, /* fsync */
20584d9c625SLionel Sambuc { &vop_seek_desc, ulfs_seek }, /* seek */
206d65f6f70SBen Gras { &vop_remove_desc, lfs_remove }, /* remove */
207d65f6f70SBen Gras { &vop_link_desc, lfs_link }, /* link */
208d65f6f70SBen Gras { &vop_rename_desc, lfs_rename }, /* rename */
209d65f6f70SBen Gras { &vop_mkdir_desc, lfs_mkdir }, /* mkdir */
210d65f6f70SBen Gras { &vop_rmdir_desc, lfs_rmdir }, /* rmdir */
211d65f6f70SBen Gras { &vop_symlink_desc, lfs_symlink }, /* symlink */
21284d9c625SLionel Sambuc { &vop_readdir_desc, ulfs_readdir }, /* readdir */
21384d9c625SLionel Sambuc { &vop_readlink_desc, ulfs_readlink }, /* readlink */
21484d9c625SLionel Sambuc { &vop_abortop_desc, ulfs_abortop }, /* abortop */
215d65f6f70SBen Gras { &vop_inactive_desc, lfs_inactive }, /* inactive */
216d65f6f70SBen Gras { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
21784d9c625SLionel Sambuc { &vop_lock_desc, ulfs_lock }, /* lock */
21884d9c625SLionel Sambuc { &vop_unlock_desc, ulfs_unlock }, /* unlock */
21984d9c625SLionel Sambuc { &vop_bmap_desc, ulfs_bmap }, /* bmap */
220d65f6f70SBen Gras { &vop_strategy_desc, lfs_strategy }, /* strategy */
22184d9c625SLionel Sambuc { &vop_print_desc, ulfs_print }, /* print */
22284d9c625SLionel Sambuc { &vop_islocked_desc, ulfs_islocked }, /* islocked */
22384d9c625SLionel Sambuc { &vop_pathconf_desc, ulfs_pathconf }, /* pathconf */
22484d9c625SLionel Sambuc { &vop_advlock_desc, ulfs_advlock }, /* advlock */
225d65f6f70SBen Gras { &vop_bwrite_desc, lfs_bwrite }, /* bwrite */
226d65f6f70SBen Gras { &vop_getpages_desc, lfs_getpages }, /* getpages */
227d65f6f70SBen Gras { &vop_putpages_desc, lfs_putpages }, /* putpages */
22884d9c625SLionel Sambuc { &vop_openextattr_desc, lfs_openextattr }, /* openextattr */
22984d9c625SLionel Sambuc { &vop_closeextattr_desc, lfs_closeextattr }, /* closeextattr */
23084d9c625SLionel Sambuc { &vop_getextattr_desc, lfs_getextattr }, /* getextattr */
23184d9c625SLionel Sambuc { &vop_setextattr_desc, lfs_setextattr }, /* setextattr */
23284d9c625SLionel Sambuc { &vop_listextattr_desc, lfs_listextattr }, /* listextattr */
23384d9c625SLionel Sambuc { &vop_deleteextattr_desc, lfs_deleteextattr }, /* deleteextattr */
234d65f6f70SBen Gras { NULL, NULL }
235d65f6f70SBen Gras };
236d65f6f70SBen Gras const struct vnodeopv_desc lfs_vnodeop_opv_desc =
237d65f6f70SBen Gras { &lfs_vnodeop_p, lfs_vnodeop_entries };
238d65f6f70SBen Gras
239d65f6f70SBen Gras int (**lfs_specop_p)(void *);
240d65f6f70SBen Gras const struct vnodeopv_entry_desc lfs_specop_entries[] = {
241d65f6f70SBen Gras { &vop_default_desc, vn_default_error },
242d65f6f70SBen Gras { &vop_lookup_desc, spec_lookup }, /* lookup */
243d65f6f70SBen Gras { &vop_create_desc, spec_create }, /* create */
244d65f6f70SBen Gras { &vop_mknod_desc, spec_mknod }, /* mknod */
245d65f6f70SBen Gras { &vop_open_desc, spec_open }, /* open */
246d65f6f70SBen Gras { &vop_close_desc, lfsspec_close }, /* close */
24784d9c625SLionel Sambuc { &vop_access_desc, ulfs_access }, /* access */
248d65f6f70SBen Gras { &vop_getattr_desc, lfs_getattr }, /* getattr */
249d65f6f70SBen Gras { &vop_setattr_desc, lfs_setattr }, /* setattr */
25084d9c625SLionel Sambuc { &vop_read_desc, ulfsspec_read }, /* read */
25184d9c625SLionel Sambuc { &vop_write_desc, ulfsspec_write }, /* write */
252*0a6a1f1dSLionel Sambuc { &vop_fallocate_desc, spec_fallocate }, /* fallocate */
253*0a6a1f1dSLionel Sambuc { &vop_fdiscard_desc, spec_fdiscard }, /* fdiscard */
254d65f6f70SBen Gras { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
25584d9c625SLionel Sambuc { &vop_fcntl_desc, ulfs_fcntl }, /* fcntl */
256d65f6f70SBen Gras { &vop_poll_desc, spec_poll }, /* poll */
257d65f6f70SBen Gras { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
258d65f6f70SBen Gras { &vop_revoke_desc, spec_revoke }, /* revoke */
259d65f6f70SBen Gras { &vop_mmap_desc, spec_mmap }, /* mmap */
260d65f6f70SBen Gras { &vop_fsync_desc, spec_fsync }, /* fsync */
261d65f6f70SBen Gras { &vop_seek_desc, spec_seek }, /* seek */
262d65f6f70SBen Gras { &vop_remove_desc, spec_remove }, /* remove */
263d65f6f70SBen Gras { &vop_link_desc, spec_link }, /* link */
264d65f6f70SBen Gras { &vop_rename_desc, spec_rename }, /* rename */
265d65f6f70SBen Gras { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
266d65f6f70SBen Gras { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
267d65f6f70SBen Gras { &vop_symlink_desc, spec_symlink }, /* symlink */
268d65f6f70SBen Gras { &vop_readdir_desc, spec_readdir }, /* readdir */
269d65f6f70SBen Gras { &vop_readlink_desc, spec_readlink }, /* readlink */
270d65f6f70SBen Gras { &vop_abortop_desc, spec_abortop }, /* abortop */
271d65f6f70SBen Gras { &vop_inactive_desc, lfs_inactive }, /* inactive */
272d65f6f70SBen Gras { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
27384d9c625SLionel Sambuc { &vop_lock_desc, ulfs_lock }, /* lock */
27484d9c625SLionel Sambuc { &vop_unlock_desc, ulfs_unlock }, /* unlock */
275d65f6f70SBen Gras { &vop_bmap_desc, spec_bmap }, /* bmap */
276d65f6f70SBen Gras { &vop_strategy_desc, spec_strategy }, /* strategy */
27784d9c625SLionel Sambuc { &vop_print_desc, ulfs_print }, /* print */
27884d9c625SLionel Sambuc { &vop_islocked_desc, ulfs_islocked }, /* islocked */
279d65f6f70SBen Gras { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
280d65f6f70SBen Gras { &vop_advlock_desc, spec_advlock }, /* advlock */
281d65f6f70SBen Gras { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
282d65f6f70SBen Gras { &vop_getpages_desc, spec_getpages }, /* getpages */
283d65f6f70SBen Gras { &vop_putpages_desc, spec_putpages }, /* putpages */
28484d9c625SLionel Sambuc { &vop_openextattr_desc, lfs_openextattr }, /* openextattr */
28584d9c625SLionel Sambuc { &vop_closeextattr_desc, lfs_closeextattr }, /* closeextattr */
28684d9c625SLionel Sambuc { &vop_getextattr_desc, lfs_getextattr }, /* getextattr */
28784d9c625SLionel Sambuc { &vop_setextattr_desc, lfs_setextattr }, /* setextattr */
28884d9c625SLionel Sambuc { &vop_listextattr_desc, lfs_listextattr }, /* listextattr */
28984d9c625SLionel Sambuc { &vop_deleteextattr_desc, lfs_deleteextattr }, /* deleteextattr */
290d65f6f70SBen Gras { NULL, NULL }
291d65f6f70SBen Gras };
292d65f6f70SBen Gras const struct vnodeopv_desc lfs_specop_opv_desc =
293d65f6f70SBen Gras { &lfs_specop_p, lfs_specop_entries };
294d65f6f70SBen Gras
295d65f6f70SBen Gras int (**lfs_fifoop_p)(void *);
296d65f6f70SBen Gras const struct vnodeopv_entry_desc lfs_fifoop_entries[] = {
297d65f6f70SBen Gras { &vop_default_desc, vn_default_error },
298d65f6f70SBen Gras { &vop_lookup_desc, vn_fifo_bypass }, /* lookup */
299d65f6f70SBen Gras { &vop_create_desc, vn_fifo_bypass }, /* create */
300d65f6f70SBen Gras { &vop_mknod_desc, vn_fifo_bypass }, /* mknod */
301d65f6f70SBen Gras { &vop_open_desc, vn_fifo_bypass }, /* open */
302d65f6f70SBen Gras { &vop_close_desc, lfsfifo_close }, /* close */
30384d9c625SLionel Sambuc { &vop_access_desc, ulfs_access }, /* access */
304d65f6f70SBen Gras { &vop_getattr_desc, lfs_getattr }, /* getattr */
305d65f6f70SBen Gras { &vop_setattr_desc, lfs_setattr }, /* setattr */
30684d9c625SLionel Sambuc { &vop_read_desc, ulfsfifo_read }, /* read */
30784d9c625SLionel Sambuc { &vop_write_desc, ulfsfifo_write }, /* write */
308*0a6a1f1dSLionel Sambuc { &vop_fallocate_desc, vn_fifo_bypass }, /* fallocate */
309*0a6a1f1dSLionel Sambuc { &vop_fdiscard_desc, vn_fifo_bypass }, /* fdiscard */
310d65f6f70SBen Gras { &vop_ioctl_desc, vn_fifo_bypass }, /* ioctl */
31184d9c625SLionel Sambuc { &vop_fcntl_desc, ulfs_fcntl }, /* fcntl */
312d65f6f70SBen Gras { &vop_poll_desc, vn_fifo_bypass }, /* poll */
313d65f6f70SBen Gras { &vop_kqfilter_desc, vn_fifo_bypass }, /* kqfilter */
314d65f6f70SBen Gras { &vop_revoke_desc, vn_fifo_bypass }, /* revoke */
315d65f6f70SBen Gras { &vop_mmap_desc, vn_fifo_bypass }, /* mmap */
316d65f6f70SBen Gras { &vop_fsync_desc, vn_fifo_bypass }, /* fsync */
317d65f6f70SBen Gras { &vop_seek_desc, vn_fifo_bypass }, /* seek */
318d65f6f70SBen Gras { &vop_remove_desc, vn_fifo_bypass }, /* remove */
319d65f6f70SBen Gras { &vop_link_desc, vn_fifo_bypass }, /* link */
320d65f6f70SBen Gras { &vop_rename_desc, vn_fifo_bypass }, /* rename */
321d65f6f70SBen Gras { &vop_mkdir_desc, vn_fifo_bypass }, /* mkdir */
322d65f6f70SBen Gras { &vop_rmdir_desc, vn_fifo_bypass }, /* rmdir */
323d65f6f70SBen Gras { &vop_symlink_desc, vn_fifo_bypass }, /* symlink */
324d65f6f70SBen Gras { &vop_readdir_desc, vn_fifo_bypass }, /* readdir */
325d65f6f70SBen Gras { &vop_readlink_desc, vn_fifo_bypass }, /* readlink */
326d65f6f70SBen Gras { &vop_abortop_desc, vn_fifo_bypass }, /* abortop */
327d65f6f70SBen Gras { &vop_inactive_desc, lfs_inactive }, /* inactive */
328d65f6f70SBen Gras { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
32984d9c625SLionel Sambuc { &vop_lock_desc, ulfs_lock }, /* lock */
33084d9c625SLionel Sambuc { &vop_unlock_desc, ulfs_unlock }, /* unlock */
331d65f6f70SBen Gras { &vop_bmap_desc, vn_fifo_bypass }, /* bmap */
332d65f6f70SBen Gras { &vop_strategy_desc, vn_fifo_bypass }, /* strategy */
33384d9c625SLionel Sambuc { &vop_print_desc, ulfs_print }, /* print */
33484d9c625SLionel Sambuc { &vop_islocked_desc, ulfs_islocked }, /* islocked */
335d65f6f70SBen Gras { &vop_pathconf_desc, vn_fifo_bypass }, /* pathconf */
336d65f6f70SBen Gras { &vop_advlock_desc, vn_fifo_bypass }, /* advlock */
337d65f6f70SBen Gras { &vop_bwrite_desc, lfs_bwrite }, /* bwrite */
338d65f6f70SBen Gras { &vop_putpages_desc, vn_fifo_bypass }, /* putpages */
33984d9c625SLionel Sambuc { &vop_openextattr_desc, lfs_openextattr }, /* openextattr */
34084d9c625SLionel Sambuc { &vop_closeextattr_desc, lfs_closeextattr }, /* closeextattr */
34184d9c625SLionel Sambuc { &vop_getextattr_desc, lfs_getextattr }, /* getextattr */
34284d9c625SLionel Sambuc { &vop_setextattr_desc, lfs_setextattr }, /* setextattr */
34384d9c625SLionel Sambuc { &vop_listextattr_desc, lfs_listextattr }, /* listextattr */
34484d9c625SLionel Sambuc { &vop_deleteextattr_desc, lfs_deleteextattr }, /* deleteextattr */
345d65f6f70SBen Gras { NULL, NULL }
346d65f6f70SBen Gras };
347d65f6f70SBen Gras const struct vnodeopv_desc lfs_fifoop_opv_desc =
348d65f6f70SBen Gras { &lfs_fifoop_p, lfs_fifoop_entries };
349d65f6f70SBen Gras
350d65f6f70SBen Gras #define LFS_READWRITE
35184d9c625SLionel Sambuc #include <ufs/lfs/ulfs_readwrite.c>
352d65f6f70SBen Gras #undef LFS_READWRITE
353d65f6f70SBen Gras
354d65f6f70SBen Gras /*
355d65f6f70SBen Gras * Synch an open file.
356d65f6f70SBen Gras */
357d65f6f70SBen Gras /* ARGSUSED */
358d65f6f70SBen Gras int
lfs_fsync(void * v)359d65f6f70SBen Gras lfs_fsync(void *v)
360d65f6f70SBen Gras {
361d65f6f70SBen Gras struct vop_fsync_args /* {
362d65f6f70SBen Gras struct vnode *a_vp;
363d65f6f70SBen Gras kauth_cred_t a_cred;
364d65f6f70SBen Gras int a_flags;
365d65f6f70SBen Gras off_t offlo;
366d65f6f70SBen Gras off_t offhi;
367d65f6f70SBen Gras } */ *ap = v;
368d65f6f70SBen Gras struct vnode *vp = ap->a_vp;
369d65f6f70SBen Gras int error, wait;
370d65f6f70SBen Gras struct inode *ip = VTOI(vp);
371d65f6f70SBen Gras struct lfs *fs = ip->i_lfs;
372d65f6f70SBen Gras
373d65f6f70SBen Gras /* If we're mounted read-only, don't try to sync. */
374d65f6f70SBen Gras if (fs->lfs_ronly)
375d65f6f70SBen Gras return 0;
376d65f6f70SBen Gras
377d65f6f70SBen Gras /* If a removed vnode is being cleaned, no need to sync here. */
378d65f6f70SBen Gras if ((ap->a_flags & FSYNC_RECLAIM) != 0 && ip->i_mode == 0)
379d65f6f70SBen Gras return 0;
380d65f6f70SBen Gras
381d65f6f70SBen Gras /*
382d65f6f70SBen Gras * Trickle sync simply adds this vnode to the pager list, as if
383d65f6f70SBen Gras * the pagedaemon had requested a pageout.
384d65f6f70SBen Gras */
385d65f6f70SBen Gras if (ap->a_flags & FSYNC_LAZY) {
386d65f6f70SBen Gras if (lfs_ignore_lazy_sync == 0) {
387d65f6f70SBen Gras mutex_enter(&lfs_lock);
388d65f6f70SBen Gras if (!(ip->i_flags & IN_PAGING)) {
389d65f6f70SBen Gras ip->i_flags |= IN_PAGING;
390d65f6f70SBen Gras TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip,
391d65f6f70SBen Gras i_lfs_pchain);
392d65f6f70SBen Gras }
393d65f6f70SBen Gras wakeup(&lfs_writer_daemon);
394d65f6f70SBen Gras mutex_exit(&lfs_lock);
395d65f6f70SBen Gras }
396d65f6f70SBen Gras return 0;
397d65f6f70SBen Gras }
398d65f6f70SBen Gras
399d65f6f70SBen Gras /*
400d65f6f70SBen Gras * If a vnode is bring cleaned, flush it out before we try to
401d65f6f70SBen Gras * reuse it. This prevents the cleaner from writing files twice
402d65f6f70SBen Gras * in the same partial segment, causing an accounting underflow.
403d65f6f70SBen Gras */
404d65f6f70SBen Gras if (ap->a_flags & FSYNC_RECLAIM && ip->i_flags & IN_CLEANING) {
405d65f6f70SBen Gras lfs_vflush(vp);
406d65f6f70SBen Gras }
407d65f6f70SBen Gras
408d65f6f70SBen Gras wait = (ap->a_flags & FSYNC_WAIT);
409d65f6f70SBen Gras do {
410d65f6f70SBen Gras mutex_enter(vp->v_interlock);
411d65f6f70SBen Gras error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
412d65f6f70SBen Gras round_page(ap->a_offhi),
413d65f6f70SBen Gras PGO_CLEANIT | (wait ? PGO_SYNCIO : 0));
414d65f6f70SBen Gras if (error == EAGAIN) {
415d65f6f70SBen Gras mutex_enter(&lfs_lock);
416*0a6a1f1dSLionel Sambuc mtsleep(&fs->lfs_availsleep, PCATCH | PUSER,
417*0a6a1f1dSLionel Sambuc "lfs_fsync", hz / 100 + 1, &lfs_lock);
418d65f6f70SBen Gras mutex_exit(&lfs_lock);
419d65f6f70SBen Gras }
420d65f6f70SBen Gras } while (error == EAGAIN);
421d65f6f70SBen Gras if (error)
422d65f6f70SBen Gras return error;
423d65f6f70SBen Gras
424d65f6f70SBen Gras if ((ap->a_flags & FSYNC_DATAONLY) == 0)
425d65f6f70SBen Gras error = lfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
426d65f6f70SBen Gras
427d65f6f70SBen Gras if (error == 0 && ap->a_flags & FSYNC_CACHE) {
428d65f6f70SBen Gras int l = 0;
429d65f6f70SBen Gras error = VOP_IOCTL(ip->i_devvp, DIOCCACHESYNC, &l, FWRITE,
430d65f6f70SBen Gras curlwp->l_cred);
431d65f6f70SBen Gras }
432d65f6f70SBen Gras if (wait && !VPISEMPTY(vp))
433d65f6f70SBen Gras LFS_SET_UINO(ip, IN_MODIFIED);
434d65f6f70SBen Gras
435d65f6f70SBen Gras return error;
436d65f6f70SBen Gras }
437d65f6f70SBen Gras
438d65f6f70SBen Gras /*
43984d9c625SLionel Sambuc * Take IN_ADIROP off, then call ulfs_inactive.
440d65f6f70SBen Gras */
441d65f6f70SBen Gras int
lfs_inactive(void * v)442d65f6f70SBen Gras lfs_inactive(void *v)
443d65f6f70SBen Gras {
444d65f6f70SBen Gras struct vop_inactive_args /* {
445d65f6f70SBen Gras struct vnode *a_vp;
446d65f6f70SBen Gras } */ *ap = v;
447d65f6f70SBen Gras
448d65f6f70SBen Gras lfs_unmark_vnode(ap->a_vp);
449d65f6f70SBen Gras
450d65f6f70SBen Gras /*
451d65f6f70SBen Gras * The Ifile is only ever inactivated on unmount.
452d65f6f70SBen Gras * Streamline this process by not giving it more dirty blocks.
453d65f6f70SBen Gras */
454d65f6f70SBen Gras if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM) {
455d65f6f70SBen Gras mutex_enter(&lfs_lock);
456d65f6f70SBen Gras LFS_CLR_UINO(VTOI(ap->a_vp), IN_ALLMOD);
457d65f6f70SBen Gras mutex_exit(&lfs_lock);
458d65f6f70SBen Gras VOP_UNLOCK(ap->a_vp);
459d65f6f70SBen Gras return 0;
460d65f6f70SBen Gras }
461d65f6f70SBen Gras
46284d9c625SLionel Sambuc #ifdef DEBUG
46384d9c625SLionel Sambuc /*
46484d9c625SLionel Sambuc * This might happen on unmount.
46584d9c625SLionel Sambuc * XXX If it happens at any other time, it should be a panic.
46684d9c625SLionel Sambuc */
46784d9c625SLionel Sambuc if (ap->a_vp->v_uflag & VU_DIROP) {
46884d9c625SLionel Sambuc struct inode *ip = VTOI(ap->a_vp);
46984d9c625SLionel Sambuc printf("lfs_inactive: inactivating VU_DIROP? ino = %d\n", (int)ip->i_number);
47084d9c625SLionel Sambuc }
47184d9c625SLionel Sambuc #endif /* DIAGNOSTIC */
47284d9c625SLionel Sambuc
47384d9c625SLionel Sambuc return ulfs_inactive(v);
474d65f6f70SBen Gras }
475d65f6f70SBen Gras
47684d9c625SLionel Sambuc int
lfs_set_dirop(struct vnode * dvp,struct vnode * vp)477d65f6f70SBen Gras lfs_set_dirop(struct vnode *dvp, struct vnode *vp)
478d65f6f70SBen Gras {
479d65f6f70SBen Gras struct lfs *fs;
480d65f6f70SBen Gras int error;
481d65f6f70SBen Gras
482d65f6f70SBen Gras KASSERT(VOP_ISLOCKED(dvp));
483d65f6f70SBen Gras KASSERT(vp == NULL || VOP_ISLOCKED(vp));
484d65f6f70SBen Gras
485d65f6f70SBen Gras fs = VTOI(dvp)->i_lfs;
486d65f6f70SBen Gras
487d65f6f70SBen Gras ASSERT_NO_SEGLOCK(fs);
488d65f6f70SBen Gras /*
489d65f6f70SBen Gras * LFS_NRESERVE calculates direct and indirect blocks as well
490d65f6f70SBen Gras * as an inode block; an overestimate in most cases.
491d65f6f70SBen Gras */
492d65f6f70SBen Gras if ((error = lfs_reserve(fs, dvp, vp, LFS_NRESERVE(fs))) != 0)
493d65f6f70SBen Gras return (error);
494d65f6f70SBen Gras
495d65f6f70SBen Gras restart:
496d65f6f70SBen Gras mutex_enter(&lfs_lock);
497d65f6f70SBen Gras if (fs->lfs_dirops == 0) {
498d65f6f70SBen Gras mutex_exit(&lfs_lock);
499d65f6f70SBen Gras lfs_check(dvp, LFS_UNUSED_LBN, 0);
500d65f6f70SBen Gras mutex_enter(&lfs_lock);
501d65f6f70SBen Gras }
502d65f6f70SBen Gras while (fs->lfs_writer) {
503d65f6f70SBen Gras error = mtsleep(&fs->lfs_dirops, (PRIBIO + 1) | PCATCH,
504d65f6f70SBen Gras "lfs_sdirop", 0, &lfs_lock);
505d65f6f70SBen Gras if (error == EINTR) {
506d65f6f70SBen Gras mutex_exit(&lfs_lock);
507d65f6f70SBen Gras goto unreserve;
508d65f6f70SBen Gras }
509d65f6f70SBen Gras }
510d65f6f70SBen Gras if (lfs_dirvcount > LFS_MAX_DIROP && fs->lfs_dirops == 0) {
511d65f6f70SBen Gras wakeup(&lfs_writer_daemon);
512d65f6f70SBen Gras mutex_exit(&lfs_lock);
513d65f6f70SBen Gras preempt();
514d65f6f70SBen Gras goto restart;
515d65f6f70SBen Gras }
516d65f6f70SBen Gras
517d65f6f70SBen Gras if (lfs_dirvcount > LFS_MAX_DIROP) {
518d65f6f70SBen Gras DLOG((DLOG_DIROP, "lfs_set_dirop: sleeping with dirops=%d, "
519d65f6f70SBen Gras "dirvcount=%d\n", fs->lfs_dirops, lfs_dirvcount));
520d65f6f70SBen Gras if ((error = mtsleep(&lfs_dirvcount,
521d65f6f70SBen Gras PCATCH | PUSER | PNORELOCK, "lfs_maxdirop", 0,
522d65f6f70SBen Gras &lfs_lock)) != 0) {
523d65f6f70SBen Gras goto unreserve;
524d65f6f70SBen Gras }
525d65f6f70SBen Gras goto restart;
526d65f6f70SBen Gras }
527d65f6f70SBen Gras
528d65f6f70SBen Gras ++fs->lfs_dirops;
52984d9c625SLionel Sambuc /* fs->lfs_doifile = 1; */ /* XXX why? --ks */
530d65f6f70SBen Gras mutex_exit(&lfs_lock);
531d65f6f70SBen Gras
532d65f6f70SBen Gras /* Hold a reference so SET_ENDOP will be happy */
533d65f6f70SBen Gras vref(dvp);
534d65f6f70SBen Gras if (vp) {
535d65f6f70SBen Gras vref(vp);
536d65f6f70SBen Gras MARK_VNODE(vp);
537d65f6f70SBen Gras }
538d65f6f70SBen Gras
539d65f6f70SBen Gras MARK_VNODE(dvp);
540d65f6f70SBen Gras return 0;
541d65f6f70SBen Gras
542d65f6f70SBen Gras unreserve:
543d65f6f70SBen Gras lfs_reserve(fs, dvp, vp, -LFS_NRESERVE(fs));
544d65f6f70SBen Gras return error;
545d65f6f70SBen Gras }
546d65f6f70SBen Gras
547d65f6f70SBen Gras /*
548*0a6a1f1dSLionel Sambuc * Opposite of lfs_set_dirop... mostly. For now at least must call
549*0a6a1f1dSLionel Sambuc * UNMARK_VNODE(dvp) explicitly first. (XXX: clean that up)
550d65f6f70SBen Gras */
551*0a6a1f1dSLionel Sambuc void
lfs_unset_dirop(struct lfs * fs,struct vnode * dvp,const char * str)552*0a6a1f1dSLionel Sambuc lfs_unset_dirop(struct lfs *fs, struct vnode *dvp, const char *str)
553d65f6f70SBen Gras {
554*0a6a1f1dSLionel Sambuc mutex_enter(&lfs_lock);
555*0a6a1f1dSLionel Sambuc --fs->lfs_dirops;
556*0a6a1f1dSLionel Sambuc if (!fs->lfs_dirops) {
557*0a6a1f1dSLionel Sambuc if (fs->lfs_nadirop) {
558*0a6a1f1dSLionel Sambuc panic("lfs_unset_dirop: %s: no dirops but "
559*0a6a1f1dSLionel Sambuc " nadirop=%d", str,
560*0a6a1f1dSLionel Sambuc fs->lfs_nadirop);
561d65f6f70SBen Gras }
562*0a6a1f1dSLionel Sambuc wakeup(&fs->lfs_writer);
563*0a6a1f1dSLionel Sambuc mutex_exit(&lfs_lock);
564*0a6a1f1dSLionel Sambuc lfs_check(dvp, LFS_UNUSED_LBN, 0);
565*0a6a1f1dSLionel Sambuc } else {
566*0a6a1f1dSLionel Sambuc mutex_exit(&lfs_lock);
567d65f6f70SBen Gras }
568*0a6a1f1dSLionel Sambuc lfs_reserve(fs, dvp, NULL, -LFS_NRESERVE(fs));
569d65f6f70SBen Gras }
570d65f6f70SBen Gras
571d65f6f70SBen Gras void
lfs_mark_vnode(struct vnode * vp)572d65f6f70SBen Gras lfs_mark_vnode(struct vnode *vp)
573d65f6f70SBen Gras {
574d65f6f70SBen Gras struct inode *ip = VTOI(vp);
575d65f6f70SBen Gras struct lfs *fs = ip->i_lfs;
576d65f6f70SBen Gras
577d65f6f70SBen Gras mutex_enter(&lfs_lock);
578d65f6f70SBen Gras if (!(ip->i_flag & IN_ADIROP)) {
579d65f6f70SBen Gras if (!(vp->v_uflag & VU_DIROP)) {
58084d9c625SLionel Sambuc mutex_exit(&lfs_lock);
581*0a6a1f1dSLionel Sambuc vref(vp);
58284d9c625SLionel Sambuc mutex_enter(&lfs_lock);
583d65f6f70SBen Gras ++lfs_dirvcount;
584d65f6f70SBen Gras ++fs->lfs_dirvcount;
585d65f6f70SBen Gras TAILQ_INSERT_TAIL(&fs->lfs_dchainhd, ip, i_lfs_dchain);
586d65f6f70SBen Gras vp->v_uflag |= VU_DIROP;
587d65f6f70SBen Gras }
588d65f6f70SBen Gras ++fs->lfs_nadirop;
58984d9c625SLionel Sambuc ip->i_flag &= ~IN_CDIROP;
590d65f6f70SBen Gras ip->i_flag |= IN_ADIROP;
591d65f6f70SBen Gras } else
592d65f6f70SBen Gras KASSERT(vp->v_uflag & VU_DIROP);
593d65f6f70SBen Gras mutex_exit(&lfs_lock);
594d65f6f70SBen Gras }
595d65f6f70SBen Gras
596d65f6f70SBen Gras void
lfs_unmark_vnode(struct vnode * vp)597d65f6f70SBen Gras lfs_unmark_vnode(struct vnode *vp)
598d65f6f70SBen Gras {
599d65f6f70SBen Gras struct inode *ip = VTOI(vp);
600d65f6f70SBen Gras
60184d9c625SLionel Sambuc mutex_enter(&lfs_lock);
602d65f6f70SBen Gras if (ip && (ip->i_flag & IN_ADIROP)) {
603d65f6f70SBen Gras KASSERT(vp->v_uflag & VU_DIROP);
604d65f6f70SBen Gras --ip->i_lfs->lfs_nadirop;
605d65f6f70SBen Gras ip->i_flag &= ~IN_ADIROP;
606d65f6f70SBen Gras }
60784d9c625SLionel Sambuc mutex_exit(&lfs_lock);
608d65f6f70SBen Gras }
609d65f6f70SBen Gras
610d65f6f70SBen Gras int
lfs_symlink(void * v)611d65f6f70SBen Gras lfs_symlink(void *v)
612d65f6f70SBen Gras {
613*0a6a1f1dSLionel Sambuc struct vop_symlink_v3_args /* {
614d65f6f70SBen Gras struct vnode *a_dvp;
615d65f6f70SBen Gras struct vnode **a_vpp;
616d65f6f70SBen Gras struct componentname *a_cnp;
617d65f6f70SBen Gras struct vattr *a_vap;
618d65f6f70SBen Gras char *a_target;
619d65f6f70SBen Gras } */ *ap = v;
620*0a6a1f1dSLionel Sambuc struct lfs *fs;
621*0a6a1f1dSLionel Sambuc struct vnode *dvp, **vpp;
622*0a6a1f1dSLionel Sambuc struct inode *ip;
623*0a6a1f1dSLionel Sambuc struct ulfs_lookup_results *ulr;
624*0a6a1f1dSLionel Sambuc ssize_t len; /* XXX should be size_t */
625d65f6f70SBen Gras int error;
626d65f6f70SBen Gras
627*0a6a1f1dSLionel Sambuc dvp = ap->a_dvp;
628*0a6a1f1dSLionel Sambuc vpp = ap->a_vpp;
629*0a6a1f1dSLionel Sambuc
630*0a6a1f1dSLionel Sambuc KASSERT(vpp != NULL);
631*0a6a1f1dSLionel Sambuc KASSERT(*vpp == NULL);
632*0a6a1f1dSLionel Sambuc KASSERT(ap->a_vap->va_type == VLNK);
633*0a6a1f1dSLionel Sambuc
634*0a6a1f1dSLionel Sambuc /* XXX should handle this material another way */
635*0a6a1f1dSLionel Sambuc ulr = &VTOI(ap->a_dvp)->i_crap;
636*0a6a1f1dSLionel Sambuc ULFS_CHECK_CRAPCOUNTER(VTOI(ap->a_dvp));
637*0a6a1f1dSLionel Sambuc
638*0a6a1f1dSLionel Sambuc fs = VFSTOULFS(dvp->v_mount)->um_lfs;
639*0a6a1f1dSLionel Sambuc ASSERT_NO_SEGLOCK(fs);
640*0a6a1f1dSLionel Sambuc if (fs->lfs_ronly) {
641*0a6a1f1dSLionel Sambuc return EROFS;
642d65f6f70SBen Gras }
643*0a6a1f1dSLionel Sambuc
644*0a6a1f1dSLionel Sambuc error = lfs_set_dirop(dvp, NULL);
645*0a6a1f1dSLionel Sambuc if (error)
646*0a6a1f1dSLionel Sambuc return error;
647*0a6a1f1dSLionel Sambuc
648*0a6a1f1dSLionel Sambuc fstrans_start(dvp->v_mount, FSTRANS_SHARED);
649*0a6a1f1dSLionel Sambuc error = ulfs_makeinode(ap->a_vap, dvp, ulr, vpp, ap->a_cnp);
650*0a6a1f1dSLionel Sambuc if (error) {
651*0a6a1f1dSLionel Sambuc goto out;
652*0a6a1f1dSLionel Sambuc }
653*0a6a1f1dSLionel Sambuc
654*0a6a1f1dSLionel Sambuc VN_KNOTE(ap->a_dvp, NOTE_WRITE);
655*0a6a1f1dSLionel Sambuc ip = VTOI(*vpp);
656*0a6a1f1dSLionel Sambuc
657*0a6a1f1dSLionel Sambuc len = strlen(ap->a_target);
658*0a6a1f1dSLionel Sambuc if (len < ip->i_lfs->um_maxsymlinklen) {
659*0a6a1f1dSLionel Sambuc memcpy((char *)SHORTLINK(ip), ap->a_target, len);
660*0a6a1f1dSLionel Sambuc ip->i_size = len;
661*0a6a1f1dSLionel Sambuc DIP_ASSIGN(ip, size, len);
662*0a6a1f1dSLionel Sambuc uvm_vnp_setsize(*vpp, ip->i_size);
663*0a6a1f1dSLionel Sambuc ip->i_flag |= IN_CHANGE | IN_UPDATE;
664*0a6a1f1dSLionel Sambuc if ((*vpp)->v_mount->mnt_flag & MNT_RELATIME)
665*0a6a1f1dSLionel Sambuc ip->i_flag |= IN_ACCESS;
666*0a6a1f1dSLionel Sambuc } else {
667*0a6a1f1dSLionel Sambuc error = ulfs_bufio(UIO_WRITE, *vpp, ap->a_target, len, (off_t)0,
668*0a6a1f1dSLionel Sambuc IO_NODELOCKED | IO_JOURNALLOCKED, ap->a_cnp->cn_cred, NULL,
669*0a6a1f1dSLionel Sambuc NULL);
670*0a6a1f1dSLionel Sambuc }
671*0a6a1f1dSLionel Sambuc
672*0a6a1f1dSLionel Sambuc VOP_UNLOCK(*vpp);
673*0a6a1f1dSLionel Sambuc if (error)
674*0a6a1f1dSLionel Sambuc vrele(*vpp);
675*0a6a1f1dSLionel Sambuc
676*0a6a1f1dSLionel Sambuc out:
677*0a6a1f1dSLionel Sambuc fstrans_done(dvp->v_mount);
678*0a6a1f1dSLionel Sambuc
679*0a6a1f1dSLionel Sambuc UNMARK_VNODE(dvp);
680*0a6a1f1dSLionel Sambuc /* XXX: is it even possible for the symlink to get MARK'd? */
681*0a6a1f1dSLionel Sambuc UNMARK_VNODE(*vpp);
682*0a6a1f1dSLionel Sambuc if (error) {
683*0a6a1f1dSLionel Sambuc *vpp = NULL;
684*0a6a1f1dSLionel Sambuc }
685*0a6a1f1dSLionel Sambuc lfs_unset_dirop(fs, dvp, "symlink");
686*0a6a1f1dSLionel Sambuc
687*0a6a1f1dSLionel Sambuc vrele(dvp);
688d65f6f70SBen Gras return (error);
689d65f6f70SBen Gras }
690d65f6f70SBen Gras
691d65f6f70SBen Gras int
lfs_mknod(void * v)692d65f6f70SBen Gras lfs_mknod(void *v)
693d65f6f70SBen Gras {
694*0a6a1f1dSLionel Sambuc struct vop_mknod_v3_args /* {
695d65f6f70SBen Gras struct vnode *a_dvp;
696d65f6f70SBen Gras struct vnode **a_vpp;
697d65f6f70SBen Gras struct componentname *a_cnp;
698d65f6f70SBen Gras struct vattr *a_vap;
699d65f6f70SBen Gras } */ *ap = v;
700*0a6a1f1dSLionel Sambuc struct lfs *fs;
701*0a6a1f1dSLionel Sambuc struct vnode *dvp, **vpp;
70284d9c625SLionel Sambuc struct vattr *vap;
703d65f6f70SBen Gras struct inode *ip;
704d65f6f70SBen Gras int error;
705d65f6f70SBen Gras ino_t ino;
70684d9c625SLionel Sambuc struct ulfs_lookup_results *ulr;
70784d9c625SLionel Sambuc
708*0a6a1f1dSLionel Sambuc dvp = ap->a_dvp;
70984d9c625SLionel Sambuc vpp = ap->a_vpp;
710*0a6a1f1dSLionel Sambuc vap = ap->a_vap;
711*0a6a1f1dSLionel Sambuc
712*0a6a1f1dSLionel Sambuc KASSERT(vpp != NULL);
713*0a6a1f1dSLionel Sambuc KASSERT(*vpp == NULL);
714d65f6f70SBen Gras
715d65f6f70SBen Gras /* XXX should handle this material another way */
716*0a6a1f1dSLionel Sambuc ulr = &VTOI(dvp)->i_crap;
717*0a6a1f1dSLionel Sambuc ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
718d65f6f70SBen Gras
719*0a6a1f1dSLionel Sambuc fs = VFSTOULFS(dvp->v_mount)->um_lfs;
720*0a6a1f1dSLionel Sambuc ASSERT_NO_SEGLOCK(fs);
721*0a6a1f1dSLionel Sambuc if (fs->lfs_ronly) {
722*0a6a1f1dSLionel Sambuc return EROFS;
723d65f6f70SBen Gras }
72484d9c625SLionel Sambuc
725*0a6a1f1dSLionel Sambuc error = lfs_set_dirop(dvp, NULL);
726*0a6a1f1dSLionel Sambuc if (error)
727*0a6a1f1dSLionel Sambuc return error;
728*0a6a1f1dSLionel Sambuc
72984d9c625SLionel Sambuc fstrans_start(ap->a_dvp->v_mount, FSTRANS_SHARED);
730*0a6a1f1dSLionel Sambuc error = ulfs_makeinode(vap, dvp, ulr, vpp, ap->a_cnp);
731d65f6f70SBen Gras
732d65f6f70SBen Gras /* Either way we're done with the dirop at this point */
733*0a6a1f1dSLionel Sambuc UNMARK_VNODE(dvp);
734*0a6a1f1dSLionel Sambuc UNMARK_VNODE(*vpp);
735*0a6a1f1dSLionel Sambuc lfs_unset_dirop(fs, dvp, "mknod");
736*0a6a1f1dSLionel Sambuc /*
737*0a6a1f1dSLionel Sambuc * XXX this is where this used to be (though inside some evil
738*0a6a1f1dSLionel Sambuc * macros) but it clearly should be moved further down.
739*0a6a1f1dSLionel Sambuc * - dholland 20140515
740*0a6a1f1dSLionel Sambuc */
741*0a6a1f1dSLionel Sambuc vrele(dvp);
742d65f6f70SBen Gras
74384d9c625SLionel Sambuc if (error) {
74484d9c625SLionel Sambuc fstrans_done(ap->a_dvp->v_mount);
74584d9c625SLionel Sambuc *vpp = NULL;
746d65f6f70SBen Gras return (error);
74784d9c625SLionel Sambuc }
748d65f6f70SBen Gras
749*0a6a1f1dSLionel Sambuc VN_KNOTE(dvp, NOTE_WRITE);
750d65f6f70SBen Gras ip = VTOI(*vpp);
751d65f6f70SBen Gras ino = ip->i_number;
752d65f6f70SBen Gras ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
753d65f6f70SBen Gras
754d65f6f70SBen Gras /*
755d65f6f70SBen Gras * Call fsync to write the vnode so that we don't have to deal with
756*0a6a1f1dSLionel Sambuc * flushing it when it's marked VU_DIROP or reclaiming.
757d65f6f70SBen Gras *
758d65f6f70SBen Gras * XXX KS - If we can't flush we also can't call vgone(), so must
759d65f6f70SBen Gras * return. But, that leaves this vnode in limbo, also not good.
760d65f6f70SBen Gras * Can this ever happen (barring hardware failure)?
761d65f6f70SBen Gras */
762d65f6f70SBen Gras if ((error = VOP_FSYNC(*vpp, NOCRED, FSYNC_WAIT, 0, 0)) != 0) {
763d65f6f70SBen Gras panic("lfs_mknod: couldn't fsync (ino %llu)",
764d65f6f70SBen Gras (unsigned long long)ino);
765d65f6f70SBen Gras /* return (error); */
766d65f6f70SBen Gras }
767d65f6f70SBen Gras
76884d9c625SLionel Sambuc fstrans_done(ap->a_dvp->v_mount);
769*0a6a1f1dSLionel Sambuc KASSERT(error == 0);
770*0a6a1f1dSLionel Sambuc VOP_UNLOCK(*vpp);
771d65f6f70SBen Gras return (0);
772d65f6f70SBen Gras }
773d65f6f70SBen Gras
774*0a6a1f1dSLionel Sambuc /*
775*0a6a1f1dSLionel Sambuc * Create a regular file
776*0a6a1f1dSLionel Sambuc */
777d65f6f70SBen Gras int
lfs_create(void * v)778d65f6f70SBen Gras lfs_create(void *v)
779d65f6f70SBen Gras {
780*0a6a1f1dSLionel Sambuc struct vop_create_v3_args /* {
781d65f6f70SBen Gras struct vnode *a_dvp;
782d65f6f70SBen Gras struct vnode **a_vpp;
783d65f6f70SBen Gras struct componentname *a_cnp;
784d65f6f70SBen Gras struct vattr *a_vap;
785d65f6f70SBen Gras } */ *ap = v;
786*0a6a1f1dSLionel Sambuc struct lfs *fs;
787*0a6a1f1dSLionel Sambuc struct vnode *dvp, **vpp;
788*0a6a1f1dSLionel Sambuc struct vattr *vap;
789*0a6a1f1dSLionel Sambuc struct ulfs_lookup_results *ulr;
790d65f6f70SBen Gras int error;
791d65f6f70SBen Gras
792*0a6a1f1dSLionel Sambuc dvp = ap->a_dvp;
793*0a6a1f1dSLionel Sambuc vpp = ap->a_vpp;
794*0a6a1f1dSLionel Sambuc vap = ap->a_vap;
795*0a6a1f1dSLionel Sambuc
796*0a6a1f1dSLionel Sambuc KASSERT(vpp != NULL);
797*0a6a1f1dSLionel Sambuc KASSERT(*vpp == NULL);
798*0a6a1f1dSLionel Sambuc
799*0a6a1f1dSLionel Sambuc /* XXX should handle this material another way */
800*0a6a1f1dSLionel Sambuc ulr = &VTOI(dvp)->i_crap;
801*0a6a1f1dSLionel Sambuc ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
802*0a6a1f1dSLionel Sambuc
803*0a6a1f1dSLionel Sambuc fs = VFSTOULFS(dvp->v_mount)->um_lfs;
804*0a6a1f1dSLionel Sambuc ASSERT_NO_SEGLOCK(fs);
805*0a6a1f1dSLionel Sambuc if (fs->lfs_ronly) {
806*0a6a1f1dSLionel Sambuc return EROFS;
807d65f6f70SBen Gras }
808*0a6a1f1dSLionel Sambuc
809*0a6a1f1dSLionel Sambuc error = lfs_set_dirop(dvp, NULL);
810*0a6a1f1dSLionel Sambuc if (error)
811*0a6a1f1dSLionel Sambuc return error;
812*0a6a1f1dSLionel Sambuc
813*0a6a1f1dSLionel Sambuc fstrans_start(dvp->v_mount, FSTRANS_SHARED);
814*0a6a1f1dSLionel Sambuc error = ulfs_makeinode(vap, dvp, ulr, vpp, ap->a_cnp);
815*0a6a1f1dSLionel Sambuc if (error) {
816*0a6a1f1dSLionel Sambuc fstrans_done(dvp->v_mount);
817*0a6a1f1dSLionel Sambuc goto out;
818*0a6a1f1dSLionel Sambuc }
819*0a6a1f1dSLionel Sambuc fstrans_done(dvp->v_mount);
820*0a6a1f1dSLionel Sambuc VN_KNOTE(dvp, NOTE_WRITE);
821*0a6a1f1dSLionel Sambuc VOP_UNLOCK(*vpp);
822*0a6a1f1dSLionel Sambuc
823*0a6a1f1dSLionel Sambuc out:
824*0a6a1f1dSLionel Sambuc
825*0a6a1f1dSLionel Sambuc UNMARK_VNODE(dvp);
826*0a6a1f1dSLionel Sambuc UNMARK_VNODE(*vpp);
827*0a6a1f1dSLionel Sambuc if (error) {
828*0a6a1f1dSLionel Sambuc *vpp = NULL;
829*0a6a1f1dSLionel Sambuc }
830*0a6a1f1dSLionel Sambuc lfs_unset_dirop(fs, dvp, "create");
831*0a6a1f1dSLionel Sambuc
832*0a6a1f1dSLionel Sambuc vrele(dvp);
833d65f6f70SBen Gras return (error);
834d65f6f70SBen Gras }
835d65f6f70SBen Gras
836d65f6f70SBen Gras int
lfs_mkdir(void * v)837d65f6f70SBen Gras lfs_mkdir(void *v)
838d65f6f70SBen Gras {
839*0a6a1f1dSLionel Sambuc struct vop_mkdir_v3_args /* {
840d65f6f70SBen Gras struct vnode *a_dvp;
841d65f6f70SBen Gras struct vnode **a_vpp;
842d65f6f70SBen Gras struct componentname *a_cnp;
843d65f6f70SBen Gras struct vattr *a_vap;
844d65f6f70SBen Gras } */ *ap = v;
845*0a6a1f1dSLionel Sambuc struct lfs *fs;
846*0a6a1f1dSLionel Sambuc struct vnode *dvp, *tvp, **vpp;
847*0a6a1f1dSLionel Sambuc struct inode *dp, *ip;
848*0a6a1f1dSLionel Sambuc struct componentname *cnp;
849*0a6a1f1dSLionel Sambuc struct vattr *vap;
850*0a6a1f1dSLionel Sambuc struct ulfs_lookup_results *ulr;
851*0a6a1f1dSLionel Sambuc struct buf *bp;
852*0a6a1f1dSLionel Sambuc LFS_DIRHEADER *dirp;
853*0a6a1f1dSLionel Sambuc int dirblksiz;
854d65f6f70SBen Gras int error;
855d65f6f70SBen Gras
856*0a6a1f1dSLionel Sambuc dvp = ap->a_dvp;
857*0a6a1f1dSLionel Sambuc tvp = NULL;
858*0a6a1f1dSLionel Sambuc vpp = ap->a_vpp;
859*0a6a1f1dSLionel Sambuc cnp = ap->a_cnp;
860*0a6a1f1dSLionel Sambuc vap = ap->a_vap;
861*0a6a1f1dSLionel Sambuc
862*0a6a1f1dSLionel Sambuc dp = VTOI(dvp);
863*0a6a1f1dSLionel Sambuc ip = NULL;
864*0a6a1f1dSLionel Sambuc
865*0a6a1f1dSLionel Sambuc KASSERT(vap->va_type == VDIR);
866*0a6a1f1dSLionel Sambuc KASSERT(vpp != NULL);
867*0a6a1f1dSLionel Sambuc KASSERT(*vpp == NULL);
868*0a6a1f1dSLionel Sambuc
869*0a6a1f1dSLionel Sambuc /* XXX should handle this material another way */
870*0a6a1f1dSLionel Sambuc ulr = &dp->i_crap;
871*0a6a1f1dSLionel Sambuc ULFS_CHECK_CRAPCOUNTER(dp);
872*0a6a1f1dSLionel Sambuc
873*0a6a1f1dSLionel Sambuc fs = VFSTOULFS(dvp->v_mount)->um_lfs;
874*0a6a1f1dSLionel Sambuc ASSERT_NO_SEGLOCK(fs);
875*0a6a1f1dSLionel Sambuc if (fs->lfs_ronly) {
876*0a6a1f1dSLionel Sambuc return EROFS;
877d65f6f70SBen Gras }
878*0a6a1f1dSLionel Sambuc dirblksiz = fs->um_dirblksiz;
879*0a6a1f1dSLionel Sambuc /* XXX dholland 20150911 I believe this to be true, but... */
880*0a6a1f1dSLionel Sambuc //KASSERT(dirblksiz == LFS_DIRBLKSIZ);
881*0a6a1f1dSLionel Sambuc
882*0a6a1f1dSLionel Sambuc error = lfs_set_dirop(dvp, NULL);
883*0a6a1f1dSLionel Sambuc if (error)
884*0a6a1f1dSLionel Sambuc return error;
885*0a6a1f1dSLionel Sambuc
886*0a6a1f1dSLionel Sambuc fstrans_start(dvp->v_mount, FSTRANS_SHARED);
887*0a6a1f1dSLionel Sambuc
888*0a6a1f1dSLionel Sambuc if ((nlink_t)dp->i_nlink >= LINK_MAX) {
889*0a6a1f1dSLionel Sambuc error = EMLINK;
890*0a6a1f1dSLionel Sambuc goto out;
891*0a6a1f1dSLionel Sambuc }
892*0a6a1f1dSLionel Sambuc
893*0a6a1f1dSLionel Sambuc /*
894*0a6a1f1dSLionel Sambuc * Must simulate part of ulfs_makeinode here to acquire the inode,
895*0a6a1f1dSLionel Sambuc * but not have it entered in the parent directory. The entry is
896*0a6a1f1dSLionel Sambuc * made later after writing "." and ".." entries.
897*0a6a1f1dSLionel Sambuc */
898*0a6a1f1dSLionel Sambuc error = vcache_new(dvp->v_mount, dvp, vap, cnp->cn_cred, ap->a_vpp);
899*0a6a1f1dSLionel Sambuc if (error)
900*0a6a1f1dSLionel Sambuc goto out;
901*0a6a1f1dSLionel Sambuc
902*0a6a1f1dSLionel Sambuc error = vn_lock(*ap->a_vpp, LK_EXCLUSIVE);
903*0a6a1f1dSLionel Sambuc if (error) {
904*0a6a1f1dSLionel Sambuc vrele(*ap->a_vpp);
905*0a6a1f1dSLionel Sambuc *ap->a_vpp = NULL;
906*0a6a1f1dSLionel Sambuc goto out;
907*0a6a1f1dSLionel Sambuc }
908*0a6a1f1dSLionel Sambuc
909*0a6a1f1dSLionel Sambuc tvp = *ap->a_vpp;
910*0a6a1f1dSLionel Sambuc lfs_mark_vnode(tvp);
911*0a6a1f1dSLionel Sambuc ip = VTOI(tvp);
912*0a6a1f1dSLionel Sambuc ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
913*0a6a1f1dSLionel Sambuc ip->i_nlink = 2;
914*0a6a1f1dSLionel Sambuc DIP_ASSIGN(ip, nlink, 2);
915*0a6a1f1dSLionel Sambuc if (cnp->cn_flags & ISWHITEOUT) {
916*0a6a1f1dSLionel Sambuc ip->i_flags |= UF_OPAQUE;
917*0a6a1f1dSLionel Sambuc DIP_ASSIGN(ip, flags, ip->i_flags);
918*0a6a1f1dSLionel Sambuc }
919*0a6a1f1dSLionel Sambuc
920*0a6a1f1dSLionel Sambuc /*
921*0a6a1f1dSLionel Sambuc * Bump link count in parent directory to reflect work done below.
922*0a6a1f1dSLionel Sambuc */
923*0a6a1f1dSLionel Sambuc dp->i_nlink++;
924*0a6a1f1dSLionel Sambuc DIP_ASSIGN(dp, nlink, dp->i_nlink);
925*0a6a1f1dSLionel Sambuc dp->i_flag |= IN_CHANGE;
926*0a6a1f1dSLionel Sambuc if ((error = lfs_update(dvp, NULL, NULL, UPDATE_DIROP)) != 0)
927*0a6a1f1dSLionel Sambuc goto bad;
928*0a6a1f1dSLionel Sambuc
929*0a6a1f1dSLionel Sambuc /*
930*0a6a1f1dSLionel Sambuc * Initialize directory with "." and "..". This used to use a
931*0a6a1f1dSLionel Sambuc * static template but that adds moving parts for very little
932*0a6a1f1dSLionel Sambuc * benefit.
933*0a6a1f1dSLionel Sambuc */
934*0a6a1f1dSLionel Sambuc if ((error = lfs_balloc(tvp, (off_t)0, dirblksiz, cnp->cn_cred,
935*0a6a1f1dSLionel Sambuc B_CLRBUF, &bp)) != 0)
936*0a6a1f1dSLionel Sambuc goto bad;
937*0a6a1f1dSLionel Sambuc ip->i_size = dirblksiz;
938*0a6a1f1dSLionel Sambuc DIP_ASSIGN(ip, size, dirblksiz);
939*0a6a1f1dSLionel Sambuc ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
940*0a6a1f1dSLionel Sambuc uvm_vnp_setsize(tvp, ip->i_size);
941*0a6a1f1dSLionel Sambuc dirp = bp->b_data;
942*0a6a1f1dSLionel Sambuc
943*0a6a1f1dSLionel Sambuc /* . */
944*0a6a1f1dSLionel Sambuc lfs_dir_setino(fs, dirp, ip->i_number);
945*0a6a1f1dSLionel Sambuc lfs_dir_setreclen(fs, dirp, LFS_DIRECTSIZ(fs, 1));
946*0a6a1f1dSLionel Sambuc lfs_dir_settype(fs, dirp, LFS_DT_DIR);
947*0a6a1f1dSLionel Sambuc lfs_dir_setnamlen(fs, dirp, 1);
948*0a6a1f1dSLionel Sambuc lfs_copydirname(fs, lfs_dir_nameptr(fs, dirp), ".", 1,
949*0a6a1f1dSLionel Sambuc LFS_DIRECTSIZ(fs, 1));
950*0a6a1f1dSLionel Sambuc dirp = LFS_NEXTDIR(fs, dirp);
951*0a6a1f1dSLionel Sambuc /* .. */
952*0a6a1f1dSLionel Sambuc lfs_dir_setino(fs, dirp, dp->i_number);
953*0a6a1f1dSLionel Sambuc lfs_dir_setreclen(fs, dirp, dirblksiz - LFS_DIRECTSIZ(fs, 1));
954*0a6a1f1dSLionel Sambuc lfs_dir_settype(fs, dirp, LFS_DT_DIR);
955*0a6a1f1dSLionel Sambuc lfs_dir_setnamlen(fs, dirp, 2);
956*0a6a1f1dSLionel Sambuc lfs_copydirname(fs, lfs_dir_nameptr(fs, dirp), "..", 2,
957*0a6a1f1dSLionel Sambuc dirblksiz - LFS_DIRECTSIZ(fs, 1));
958*0a6a1f1dSLionel Sambuc
959*0a6a1f1dSLionel Sambuc /*
960*0a6a1f1dSLionel Sambuc * Directory set up; now install its entry in the parent directory.
961*0a6a1f1dSLionel Sambuc */
962*0a6a1f1dSLionel Sambuc if ((error = VOP_BWRITE(bp->b_vp, bp)) != 0)
963*0a6a1f1dSLionel Sambuc goto bad;
964*0a6a1f1dSLionel Sambuc if ((error = lfs_update(tvp, NULL, NULL, UPDATE_DIROP)) != 0) {
965*0a6a1f1dSLionel Sambuc goto bad;
966*0a6a1f1dSLionel Sambuc }
967*0a6a1f1dSLionel Sambuc error = ulfs_direnter(dvp, ulr, tvp,
968*0a6a1f1dSLionel Sambuc cnp, ip->i_number, LFS_IFTODT(ip->i_mode), bp);
969*0a6a1f1dSLionel Sambuc bad:
970*0a6a1f1dSLionel Sambuc if (error == 0) {
971*0a6a1f1dSLionel Sambuc VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
972*0a6a1f1dSLionel Sambuc VOP_UNLOCK(tvp);
973*0a6a1f1dSLionel Sambuc } else {
974*0a6a1f1dSLionel Sambuc dp->i_nlink--;
975*0a6a1f1dSLionel Sambuc DIP_ASSIGN(dp, nlink, dp->i_nlink);
976*0a6a1f1dSLionel Sambuc dp->i_flag |= IN_CHANGE;
977*0a6a1f1dSLionel Sambuc /*
978*0a6a1f1dSLionel Sambuc * No need to do an explicit lfs_truncate here, vrele will
979*0a6a1f1dSLionel Sambuc * do this for us because we set the link count to 0.
980*0a6a1f1dSLionel Sambuc */
981*0a6a1f1dSLionel Sambuc ip->i_nlink = 0;
982*0a6a1f1dSLionel Sambuc DIP_ASSIGN(ip, nlink, 0);
983*0a6a1f1dSLionel Sambuc ip->i_flag |= IN_CHANGE;
984*0a6a1f1dSLionel Sambuc /* If IN_ADIROP, account for it */
985*0a6a1f1dSLionel Sambuc lfs_unmark_vnode(tvp);
986*0a6a1f1dSLionel Sambuc vput(tvp);
987*0a6a1f1dSLionel Sambuc }
988*0a6a1f1dSLionel Sambuc
989*0a6a1f1dSLionel Sambuc out:
990*0a6a1f1dSLionel Sambuc fstrans_done(dvp->v_mount);
991*0a6a1f1dSLionel Sambuc
992*0a6a1f1dSLionel Sambuc UNMARK_VNODE(dvp);
993*0a6a1f1dSLionel Sambuc UNMARK_VNODE(*vpp);
994*0a6a1f1dSLionel Sambuc if (error) {
995*0a6a1f1dSLionel Sambuc *vpp = NULL;
996*0a6a1f1dSLionel Sambuc }
997*0a6a1f1dSLionel Sambuc lfs_unset_dirop(fs, dvp, "mkdir");
998*0a6a1f1dSLionel Sambuc
999*0a6a1f1dSLionel Sambuc vrele(dvp);
1000d65f6f70SBen Gras return (error);
1001d65f6f70SBen Gras }
1002d65f6f70SBen Gras
1003d65f6f70SBen Gras int
lfs_remove(void * v)1004d65f6f70SBen Gras lfs_remove(void *v)
1005d65f6f70SBen Gras {
1006d65f6f70SBen Gras struct vop_remove_args /* {
1007d65f6f70SBen Gras struct vnode *a_dvp;
1008d65f6f70SBen Gras struct vnode *a_vp;
1009d65f6f70SBen Gras struct componentname *a_cnp;
1010d65f6f70SBen Gras } */ *ap = v;
1011d65f6f70SBen Gras struct vnode *dvp, *vp;
1012d65f6f70SBen Gras struct inode *ip;
1013d65f6f70SBen Gras int error;
1014d65f6f70SBen Gras
1015d65f6f70SBen Gras dvp = ap->a_dvp;
1016d65f6f70SBen Gras vp = ap->a_vp;
1017d65f6f70SBen Gras ip = VTOI(vp);
1018*0a6a1f1dSLionel Sambuc if ((error = lfs_set_dirop(dvp, vp)) != 0) {
1019d65f6f70SBen Gras if (dvp == vp)
1020d65f6f70SBen Gras vrele(vp);
1021d65f6f70SBen Gras else
1022d65f6f70SBen Gras vput(vp);
1023d65f6f70SBen Gras vput(dvp);
1024d65f6f70SBen Gras return error;
1025d65f6f70SBen Gras }
102684d9c625SLionel Sambuc error = ulfs_remove(ap);
1027d65f6f70SBen Gras if (ip->i_nlink == 0)
1028d65f6f70SBen Gras lfs_orphan(ip->i_lfs, ip->i_number);
1029*0a6a1f1dSLionel Sambuc
1030*0a6a1f1dSLionel Sambuc UNMARK_VNODE(dvp);
1031*0a6a1f1dSLionel Sambuc if (ap->a_vp) {
1032*0a6a1f1dSLionel Sambuc UNMARK_VNODE(ap->a_vp);
1033*0a6a1f1dSLionel Sambuc }
1034*0a6a1f1dSLionel Sambuc lfs_unset_dirop(ip->i_lfs, dvp, "remove");
1035*0a6a1f1dSLionel Sambuc vrele(dvp);
1036*0a6a1f1dSLionel Sambuc if (ap->a_vp) {
1037*0a6a1f1dSLionel Sambuc vrele(ap->a_vp);
1038*0a6a1f1dSLionel Sambuc }
1039*0a6a1f1dSLionel Sambuc
1040d65f6f70SBen Gras return (error);
1041d65f6f70SBen Gras }
1042d65f6f70SBen Gras
1043d65f6f70SBen Gras int
lfs_rmdir(void * v)1044d65f6f70SBen Gras lfs_rmdir(void *v)
1045d65f6f70SBen Gras {
1046d65f6f70SBen Gras struct vop_rmdir_args /* {
1047d65f6f70SBen Gras struct vnodeop_desc *a_desc;
1048d65f6f70SBen Gras struct vnode *a_dvp;
1049d65f6f70SBen Gras struct vnode *a_vp;
1050d65f6f70SBen Gras struct componentname *a_cnp;
1051d65f6f70SBen Gras } */ *ap = v;
1052d65f6f70SBen Gras struct vnode *vp;
1053d65f6f70SBen Gras struct inode *ip;
1054d65f6f70SBen Gras int error;
1055d65f6f70SBen Gras
1056d65f6f70SBen Gras vp = ap->a_vp;
1057d65f6f70SBen Gras ip = VTOI(vp);
1058*0a6a1f1dSLionel Sambuc if ((error = lfs_set_dirop(ap->a_dvp, ap->a_vp)) != 0) {
1059d65f6f70SBen Gras if (ap->a_dvp == vp)
1060d65f6f70SBen Gras vrele(ap->a_dvp);
1061d65f6f70SBen Gras else
1062d65f6f70SBen Gras vput(ap->a_dvp);
1063d65f6f70SBen Gras vput(vp);
1064d65f6f70SBen Gras return error;
1065d65f6f70SBen Gras }
106684d9c625SLionel Sambuc error = ulfs_rmdir(ap);
1067d65f6f70SBen Gras if (ip->i_nlink == 0)
1068d65f6f70SBen Gras lfs_orphan(ip->i_lfs, ip->i_number);
1069*0a6a1f1dSLionel Sambuc
1070*0a6a1f1dSLionel Sambuc UNMARK_VNODE(ap->a_dvp);
1071*0a6a1f1dSLionel Sambuc if (ap->a_vp) {
1072*0a6a1f1dSLionel Sambuc UNMARK_VNODE(ap->a_vp);
1073*0a6a1f1dSLionel Sambuc }
1074*0a6a1f1dSLionel Sambuc lfs_unset_dirop(ip->i_lfs, ap->a_dvp, "rmdir");
1075*0a6a1f1dSLionel Sambuc vrele(ap->a_dvp);
1076*0a6a1f1dSLionel Sambuc if (ap->a_vp) {
1077*0a6a1f1dSLionel Sambuc vrele(ap->a_vp);
1078*0a6a1f1dSLionel Sambuc }
1079*0a6a1f1dSLionel Sambuc
1080d65f6f70SBen Gras return (error);
1081d65f6f70SBen Gras }
1082d65f6f70SBen Gras
1083d65f6f70SBen Gras int
lfs_link(void * v)1084d65f6f70SBen Gras lfs_link(void *v)
1085d65f6f70SBen Gras {
1086*0a6a1f1dSLionel Sambuc struct vop_link_v2_args /* {
1087d65f6f70SBen Gras struct vnode *a_dvp;
1088d65f6f70SBen Gras struct vnode *a_vp;
1089d65f6f70SBen Gras struct componentname *a_cnp;
1090d65f6f70SBen Gras } */ *ap = v;
1091*0a6a1f1dSLionel Sambuc struct lfs *fs;
1092*0a6a1f1dSLionel Sambuc struct vnode *dvp;
1093d65f6f70SBen Gras int error;
1094d65f6f70SBen Gras
1095*0a6a1f1dSLionel Sambuc dvp = ap->a_dvp;
1096*0a6a1f1dSLionel Sambuc
1097*0a6a1f1dSLionel Sambuc fs = VFSTOULFS(dvp->v_mount)->um_lfs;
1098*0a6a1f1dSLionel Sambuc ASSERT_NO_SEGLOCK(fs);
1099*0a6a1f1dSLionel Sambuc if (fs->lfs_ronly) {
1100*0a6a1f1dSLionel Sambuc return EROFS;
1101*0a6a1f1dSLionel Sambuc }
1102*0a6a1f1dSLionel Sambuc
1103*0a6a1f1dSLionel Sambuc error = lfs_set_dirop(dvp, NULL);
1104*0a6a1f1dSLionel Sambuc if (error) {
1105d65f6f70SBen Gras return error;
1106d65f6f70SBen Gras }
1107*0a6a1f1dSLionel Sambuc
110884d9c625SLionel Sambuc error = ulfs_link(ap);
1109*0a6a1f1dSLionel Sambuc
1110*0a6a1f1dSLionel Sambuc UNMARK_VNODE(dvp);
1111*0a6a1f1dSLionel Sambuc lfs_unset_dirop(fs, dvp, "link");
1112*0a6a1f1dSLionel Sambuc vrele(dvp);
1113*0a6a1f1dSLionel Sambuc
1114d65f6f70SBen Gras return (error);
1115d65f6f70SBen Gras }
1116d65f6f70SBen Gras
1117d65f6f70SBen Gras /* XXX hack to avoid calling ITIMES in getattr */
1118d65f6f70SBen Gras int
lfs_getattr(void * v)1119d65f6f70SBen Gras lfs_getattr(void *v)
1120d65f6f70SBen Gras {
1121d65f6f70SBen Gras struct vop_getattr_args /* {
1122d65f6f70SBen Gras struct vnode *a_vp;
1123d65f6f70SBen Gras struct vattr *a_vap;
1124d65f6f70SBen Gras kauth_cred_t a_cred;
1125d65f6f70SBen Gras } */ *ap = v;
1126d65f6f70SBen Gras struct vnode *vp = ap->a_vp;
1127d65f6f70SBen Gras struct inode *ip = VTOI(vp);
1128d65f6f70SBen Gras struct vattr *vap = ap->a_vap;
1129d65f6f70SBen Gras struct lfs *fs = ip->i_lfs;
113084d9c625SLionel Sambuc
113184d9c625SLionel Sambuc fstrans_start(vp->v_mount, FSTRANS_SHARED);
1132d65f6f70SBen Gras /*
1133d65f6f70SBen Gras * Copy from inode table
1134d65f6f70SBen Gras */
1135d65f6f70SBen Gras vap->va_fsid = ip->i_dev;
1136d65f6f70SBen Gras vap->va_fileid = ip->i_number;
113784d9c625SLionel Sambuc vap->va_mode = ip->i_mode & ~LFS_IFMT;
1138d65f6f70SBen Gras vap->va_nlink = ip->i_nlink;
1139d65f6f70SBen Gras vap->va_uid = ip->i_uid;
1140d65f6f70SBen Gras vap->va_gid = ip->i_gid;
1141*0a6a1f1dSLionel Sambuc switch (vp->v_type) {
1142*0a6a1f1dSLionel Sambuc case VBLK:
1143*0a6a1f1dSLionel Sambuc case VCHR:
1144*0a6a1f1dSLionel Sambuc vap->va_rdev = (dev_t)lfs_dino_getrdev(fs, ip->i_din);
1145*0a6a1f1dSLionel Sambuc break;
1146*0a6a1f1dSLionel Sambuc default:
1147*0a6a1f1dSLionel Sambuc vap->va_rdev = NODEV;
1148*0a6a1f1dSLionel Sambuc break;
1149*0a6a1f1dSLionel Sambuc }
1150d65f6f70SBen Gras vap->va_size = vp->v_size;
1151*0a6a1f1dSLionel Sambuc vap->va_atime.tv_sec = lfs_dino_getatime(fs, ip->i_din);
1152*0a6a1f1dSLionel Sambuc vap->va_atime.tv_nsec = lfs_dino_getatimensec(fs, ip->i_din);
1153*0a6a1f1dSLionel Sambuc vap->va_mtime.tv_sec = lfs_dino_getmtime(fs, ip->i_din);
1154*0a6a1f1dSLionel Sambuc vap->va_mtime.tv_nsec = lfs_dino_getmtimensec(fs, ip->i_din);
1155*0a6a1f1dSLionel Sambuc vap->va_ctime.tv_sec = lfs_dino_getctime(fs, ip->i_din);
1156*0a6a1f1dSLionel Sambuc vap->va_ctime.tv_nsec = lfs_dino_getctimensec(fs, ip->i_din);
1157d65f6f70SBen Gras vap->va_flags = ip->i_flags;
1158d65f6f70SBen Gras vap->va_gen = ip->i_gen;
1159d65f6f70SBen Gras /* this doesn't belong here */
1160d65f6f70SBen Gras if (vp->v_type == VBLK)
1161d65f6f70SBen Gras vap->va_blocksize = BLKDEV_IOSIZE;
1162d65f6f70SBen Gras else if (vp->v_type == VCHR)
1163d65f6f70SBen Gras vap->va_blocksize = MAXBSIZE;
1164d65f6f70SBen Gras else
1165d65f6f70SBen Gras vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
1166*0a6a1f1dSLionel Sambuc vap->va_bytes = lfs_fsbtob(fs, ip->i_lfs_effnblks);
1167d65f6f70SBen Gras vap->va_type = vp->v_type;
1168d65f6f70SBen Gras vap->va_filerev = ip->i_modrev;
116984d9c625SLionel Sambuc fstrans_done(vp->v_mount);
1170d65f6f70SBen Gras return (0);
1171d65f6f70SBen Gras }
1172d65f6f70SBen Gras
1173d65f6f70SBen Gras /*
1174d65f6f70SBen Gras * Check to make sure the inode blocks won't choke the buffer
117584d9c625SLionel Sambuc * cache, then call ulfs_setattr as usual.
1176d65f6f70SBen Gras */
1177d65f6f70SBen Gras int
lfs_setattr(void * v)1178d65f6f70SBen Gras lfs_setattr(void *v)
1179d65f6f70SBen Gras {
1180d65f6f70SBen Gras struct vop_setattr_args /* {
1181d65f6f70SBen Gras struct vnode *a_vp;
1182d65f6f70SBen Gras struct vattr *a_vap;
1183d65f6f70SBen Gras kauth_cred_t a_cred;
1184d65f6f70SBen Gras } */ *ap = v;
1185d65f6f70SBen Gras struct vnode *vp = ap->a_vp;
1186d65f6f70SBen Gras
1187d65f6f70SBen Gras lfs_check(vp, LFS_UNUSED_LBN, 0);
118884d9c625SLionel Sambuc return ulfs_setattr(v);
1189d65f6f70SBen Gras }
1190d65f6f70SBen Gras
1191d65f6f70SBen Gras /*
1192d65f6f70SBen Gras * Release the block we hold on lfs_newseg wrapping. Called on file close,
1193d65f6f70SBen Gras * or explicitly from LFCNWRAPGO. Called with the interlock held.
1194d65f6f70SBen Gras */
1195d65f6f70SBen Gras static int
lfs_wrapgo(struct lfs * fs,struct inode * ip,int waitfor)1196d65f6f70SBen Gras lfs_wrapgo(struct lfs *fs, struct inode *ip, int waitfor)
1197d65f6f70SBen Gras {
1198d65f6f70SBen Gras if (fs->lfs_stoplwp != curlwp)
1199d65f6f70SBen Gras return EBUSY;
1200d65f6f70SBen Gras
1201d65f6f70SBen Gras fs->lfs_stoplwp = NULL;
1202d65f6f70SBen Gras cv_signal(&fs->lfs_stopcv);
1203d65f6f70SBen Gras
1204d65f6f70SBen Gras KASSERT(fs->lfs_nowrap > 0);
1205d65f6f70SBen Gras if (fs->lfs_nowrap <= 0) {
1206d65f6f70SBen Gras return 0;
1207d65f6f70SBen Gras }
1208d65f6f70SBen Gras
1209d65f6f70SBen Gras if (--fs->lfs_nowrap == 0) {
1210*0a6a1f1dSLionel Sambuc log(LOG_NOTICE, "%s: re-enabled log wrap\n",
1211*0a6a1f1dSLionel Sambuc lfs_sb_getfsmnt(fs));
1212d65f6f70SBen Gras wakeup(&fs->lfs_wrappass);
1213d65f6f70SBen Gras lfs_wakeup_cleaner(fs);
1214d65f6f70SBen Gras }
1215d65f6f70SBen Gras if (waitfor) {
1216*0a6a1f1dSLionel Sambuc mtsleep(&fs->lfs_nextsegsleep, PCATCH | PUSER, "segment",
1217d65f6f70SBen Gras 0, &lfs_lock);
1218d65f6f70SBen Gras }
1219d65f6f70SBen Gras
1220d65f6f70SBen Gras return 0;
1221d65f6f70SBen Gras }
1222d65f6f70SBen Gras
1223d65f6f70SBen Gras /*
122484d9c625SLionel Sambuc * Close called.
122584d9c625SLionel Sambuc *
122684d9c625SLionel Sambuc * Update the times on the inode.
1227d65f6f70SBen Gras */
1228d65f6f70SBen Gras /* ARGSUSED */
1229d65f6f70SBen Gras int
lfs_close(void * v)1230d65f6f70SBen Gras lfs_close(void *v)
1231d65f6f70SBen Gras {
1232d65f6f70SBen Gras struct vop_close_args /* {
1233d65f6f70SBen Gras struct vnode *a_vp;
1234d65f6f70SBen Gras int a_fflag;
1235d65f6f70SBen Gras kauth_cred_t a_cred;
1236d65f6f70SBen Gras } */ *ap = v;
1237d65f6f70SBen Gras struct vnode *vp = ap->a_vp;
1238d65f6f70SBen Gras struct inode *ip = VTOI(vp);
1239d65f6f70SBen Gras struct lfs *fs = ip->i_lfs;
1240d65f6f70SBen Gras
124184d9c625SLionel Sambuc if ((ip->i_number == ULFS_ROOTINO || ip->i_number == LFS_IFILE_INUM) &&
1242d65f6f70SBen Gras fs->lfs_stoplwp == curlwp) {
1243d65f6f70SBen Gras mutex_enter(&lfs_lock);
1244d65f6f70SBen Gras log(LOG_NOTICE, "lfs_close: releasing log wrap control\n");
1245d65f6f70SBen Gras lfs_wrapgo(fs, ip, 0);
1246d65f6f70SBen Gras mutex_exit(&lfs_lock);
1247d65f6f70SBen Gras }
1248d65f6f70SBen Gras
1249d65f6f70SBen Gras if (vp == ip->i_lfs->lfs_ivnode &&
1250d65f6f70SBen Gras vp->v_mount->mnt_iflag & IMNT_UNMOUNT)
1251d65f6f70SBen Gras return 0;
1252d65f6f70SBen Gras
125384d9c625SLionel Sambuc fstrans_start(vp->v_mount, FSTRANS_SHARED);
1254d65f6f70SBen Gras if (vp->v_usecount > 1 && vp != ip->i_lfs->lfs_ivnode) {
1255d65f6f70SBen Gras LFS_ITIMES(ip, NULL, NULL, NULL);
1256d65f6f70SBen Gras }
125784d9c625SLionel Sambuc fstrans_done(vp->v_mount);
1258d65f6f70SBen Gras return (0);
1259d65f6f70SBen Gras }
1260d65f6f70SBen Gras
1261d65f6f70SBen Gras /*
1262d65f6f70SBen Gras * Close wrapper for special devices.
1263d65f6f70SBen Gras *
1264d65f6f70SBen Gras * Update the times on the inode then do device close.
1265d65f6f70SBen Gras */
1266d65f6f70SBen Gras int
lfsspec_close(void * v)1267d65f6f70SBen Gras lfsspec_close(void *v)
1268d65f6f70SBen Gras {
1269d65f6f70SBen Gras struct vop_close_args /* {
1270d65f6f70SBen Gras struct vnode *a_vp;
1271d65f6f70SBen Gras int a_fflag;
1272d65f6f70SBen Gras kauth_cred_t a_cred;
1273d65f6f70SBen Gras } */ *ap = v;
1274d65f6f70SBen Gras struct vnode *vp;
1275d65f6f70SBen Gras struct inode *ip;
1276d65f6f70SBen Gras
1277d65f6f70SBen Gras vp = ap->a_vp;
1278d65f6f70SBen Gras ip = VTOI(vp);
1279d65f6f70SBen Gras if (vp->v_usecount > 1) {
1280d65f6f70SBen Gras LFS_ITIMES(ip, NULL, NULL, NULL);
1281d65f6f70SBen Gras }
1282d65f6f70SBen Gras return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
1283d65f6f70SBen Gras }
1284d65f6f70SBen Gras
1285d65f6f70SBen Gras /*
1286d65f6f70SBen Gras * Close wrapper for fifo's.
1287d65f6f70SBen Gras *
1288d65f6f70SBen Gras * Update the times on the inode then do device close.
1289d65f6f70SBen Gras */
1290d65f6f70SBen Gras int
lfsfifo_close(void * v)1291d65f6f70SBen Gras lfsfifo_close(void *v)
1292d65f6f70SBen Gras {
1293d65f6f70SBen Gras struct vop_close_args /* {
1294d65f6f70SBen Gras struct vnode *a_vp;
1295d65f6f70SBen Gras int a_fflag;
1296d65f6f70SBen Gras kauth_cred_ a_cred;
1297d65f6f70SBen Gras } */ *ap = v;
1298d65f6f70SBen Gras struct vnode *vp;
1299d65f6f70SBen Gras struct inode *ip;
1300d65f6f70SBen Gras
1301d65f6f70SBen Gras vp = ap->a_vp;
1302d65f6f70SBen Gras ip = VTOI(vp);
1303d65f6f70SBen Gras if (ap->a_vp->v_usecount > 1) {
1304d65f6f70SBen Gras LFS_ITIMES(ip, NULL, NULL, NULL);
1305d65f6f70SBen Gras }
1306d65f6f70SBen Gras return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
1307d65f6f70SBen Gras }
1308d65f6f70SBen Gras
1309d65f6f70SBen Gras /*
1310d65f6f70SBen Gras * Reclaim an inode so that it can be used for other purposes.
1311d65f6f70SBen Gras */
1312d65f6f70SBen Gras
1313d65f6f70SBen Gras int
lfs_reclaim(void * v)1314d65f6f70SBen Gras lfs_reclaim(void *v)
1315d65f6f70SBen Gras {
1316d65f6f70SBen Gras struct vop_reclaim_args /* {
1317d65f6f70SBen Gras struct vnode *a_vp;
1318d65f6f70SBen Gras } */ *ap = v;
1319d65f6f70SBen Gras struct vnode *vp = ap->a_vp;
1320d65f6f70SBen Gras struct inode *ip = VTOI(vp);
1321d65f6f70SBen Gras struct lfs *fs = ip->i_lfs;
1322d65f6f70SBen Gras int error;
1323d65f6f70SBen Gras
1324d65f6f70SBen Gras /*
1325d65f6f70SBen Gras * The inode must be freed and updated before being removed
1326d65f6f70SBen Gras * from its hash chain. Other threads trying to gain a hold
1327*0a6a1f1dSLionel Sambuc * or lock on the inode will be stalled.
1328d65f6f70SBen Gras */
1329d65f6f70SBen Gras if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1330d65f6f70SBen Gras lfs_vfree(vp, ip->i_number, ip->i_omode);
1331d65f6f70SBen Gras
1332d65f6f70SBen Gras mutex_enter(&lfs_lock);
1333d65f6f70SBen Gras LFS_CLR_UINO(ip, IN_ALLMOD);
1334d65f6f70SBen Gras mutex_exit(&lfs_lock);
133584d9c625SLionel Sambuc if ((error = ulfs_reclaim(vp)))
1336d65f6f70SBen Gras return (error);
1337d65f6f70SBen Gras
1338d65f6f70SBen Gras /*
1339d65f6f70SBen Gras * Take us off the paging and/or dirop queues if we were on them.
1340d65f6f70SBen Gras * We shouldn't be on them.
1341d65f6f70SBen Gras */
1342d65f6f70SBen Gras mutex_enter(&lfs_lock);
1343d65f6f70SBen Gras if (ip->i_flags & IN_PAGING) {
1344d65f6f70SBen Gras log(LOG_WARNING, "%s: reclaimed vnode is IN_PAGING\n",
1345*0a6a1f1dSLionel Sambuc lfs_sb_getfsmnt(fs));
1346d65f6f70SBen Gras ip->i_flags &= ~IN_PAGING;
1347d65f6f70SBen Gras TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
1348d65f6f70SBen Gras }
1349d65f6f70SBen Gras if (vp->v_uflag & VU_DIROP) {
1350d65f6f70SBen Gras panic("reclaimed vnode is VU_DIROP");
1351d65f6f70SBen Gras vp->v_uflag &= ~VU_DIROP;
1352d65f6f70SBen Gras TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
1353d65f6f70SBen Gras }
1354d65f6f70SBen Gras mutex_exit(&lfs_lock);
1355d65f6f70SBen Gras
1356*0a6a1f1dSLionel Sambuc pool_put(&lfs_dinode_pool, ip->i_din);
1357d65f6f70SBen Gras lfs_deregister_all(vp);
1358d65f6f70SBen Gras pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
1359d65f6f70SBen Gras ip->inode_ext.lfs = NULL;
1360d65f6f70SBen Gras genfs_node_destroy(vp);
1361d65f6f70SBen Gras pool_put(&lfs_inode_pool, vp->v_data);
1362d65f6f70SBen Gras vp->v_data = NULL;
1363d65f6f70SBen Gras return (0);
1364d65f6f70SBen Gras }
1365d65f6f70SBen Gras
1366d65f6f70SBen Gras /*
1367d65f6f70SBen Gras * Read a block from a storage device.
136884d9c625SLionel Sambuc *
136984d9c625SLionel Sambuc * Calculate the logical to physical mapping if not done already,
137084d9c625SLionel Sambuc * then call the device strategy routine.
137184d9c625SLionel Sambuc *
1372d65f6f70SBen Gras * In order to avoid reading blocks that are in the process of being
1373d65f6f70SBen Gras * written by the cleaner---and hence are not mutexed by the normal
1374d65f6f70SBen Gras * buffer cache / page cache mechanisms---check for collisions before
1375d65f6f70SBen Gras * reading.
1376d65f6f70SBen Gras *
137784d9c625SLionel Sambuc * We inline ulfs_strategy to make sure that the VOP_BMAP occurs *before*
1378d65f6f70SBen Gras * the active cleaner test.
1379d65f6f70SBen Gras *
1380d65f6f70SBen Gras * XXX This code assumes that lfs_markv makes synchronous checkpoints.
1381d65f6f70SBen Gras */
1382d65f6f70SBen Gras int
lfs_strategy(void * v)1383d65f6f70SBen Gras lfs_strategy(void *v)
1384d65f6f70SBen Gras {
1385d65f6f70SBen Gras struct vop_strategy_args /* {
1386d65f6f70SBen Gras struct vnode *a_vp;
1387d65f6f70SBen Gras struct buf *a_bp;
1388d65f6f70SBen Gras } */ *ap = v;
1389d65f6f70SBen Gras struct buf *bp;
1390d65f6f70SBen Gras struct lfs *fs;
1391d65f6f70SBen Gras struct vnode *vp;
1392d65f6f70SBen Gras struct inode *ip;
1393d65f6f70SBen Gras daddr_t tbn;
139484d9c625SLionel Sambuc #define MAXLOOP 25
139584d9c625SLionel Sambuc int i, sn, error, slept, loopcount;
1396d65f6f70SBen Gras
1397d65f6f70SBen Gras bp = ap->a_bp;
1398d65f6f70SBen Gras vp = ap->a_vp;
1399d65f6f70SBen Gras ip = VTOI(vp);
1400d65f6f70SBen Gras fs = ip->i_lfs;
1401d65f6f70SBen Gras
1402d65f6f70SBen Gras /* lfs uses its strategy routine only for read */
1403d65f6f70SBen Gras KASSERT(bp->b_flags & B_READ);
1404d65f6f70SBen Gras
1405d65f6f70SBen Gras if (vp->v_type == VBLK || vp->v_type == VCHR)
1406d65f6f70SBen Gras panic("lfs_strategy: spec");
1407d65f6f70SBen Gras KASSERT(bp->b_bcount != 0);
1408d65f6f70SBen Gras if (bp->b_blkno == bp->b_lblkno) {
1409d65f6f70SBen Gras error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1410d65f6f70SBen Gras NULL);
1411d65f6f70SBen Gras if (error) {
1412d65f6f70SBen Gras bp->b_error = error;
1413d65f6f70SBen Gras bp->b_resid = bp->b_bcount;
1414d65f6f70SBen Gras biodone(bp);
1415d65f6f70SBen Gras return (error);
1416d65f6f70SBen Gras }
1417d65f6f70SBen Gras if ((long)bp->b_blkno == -1) /* no valid data */
1418d65f6f70SBen Gras clrbuf(bp);
1419d65f6f70SBen Gras }
1420d65f6f70SBen Gras if ((long)bp->b_blkno < 0) { /* block is not on disk */
1421d65f6f70SBen Gras bp->b_resid = bp->b_bcount;
1422d65f6f70SBen Gras biodone(bp);
1423d65f6f70SBen Gras return (0);
1424d65f6f70SBen Gras }
1425d65f6f70SBen Gras
1426d65f6f70SBen Gras slept = 1;
142784d9c625SLionel Sambuc loopcount = 0;
1428d65f6f70SBen Gras mutex_enter(&lfs_lock);
1429d65f6f70SBen Gras while (slept && fs->lfs_seglock) {
1430d65f6f70SBen Gras mutex_exit(&lfs_lock);
1431d65f6f70SBen Gras /*
1432d65f6f70SBen Gras * Look through list of intervals.
1433d65f6f70SBen Gras * There will only be intervals to look through
1434d65f6f70SBen Gras * if the cleaner holds the seglock.
1435d65f6f70SBen Gras * Since the cleaner is synchronous, we can trust
1436d65f6f70SBen Gras * the list of intervals to be current.
1437d65f6f70SBen Gras */
143884d9c625SLionel Sambuc tbn = LFS_DBTOFSB(fs, bp->b_blkno);
143984d9c625SLionel Sambuc sn = lfs_dtosn(fs, tbn);
1440d65f6f70SBen Gras slept = 0;
1441d65f6f70SBen Gras for (i = 0; i < fs->lfs_cleanind; i++) {
144284d9c625SLionel Sambuc if (sn == lfs_dtosn(fs, fs->lfs_cleanint[i]) &&
1443d65f6f70SBen Gras tbn >= fs->lfs_cleanint[i]) {
1444d65f6f70SBen Gras DLOG((DLOG_CLEAN,
1445d65f6f70SBen Gras "lfs_strategy: ino %d lbn %" PRId64
1446*0a6a1f1dSLionel Sambuc " ind %d sn %d fsb %" PRIx64
1447d65f6f70SBen Gras " given sn %d fsb %" PRIx64 "\n",
1448d65f6f70SBen Gras ip->i_number, bp->b_lblkno, i,
144984d9c625SLionel Sambuc lfs_dtosn(fs, fs->lfs_cleanint[i]),
1450d65f6f70SBen Gras fs->lfs_cleanint[i], sn, tbn));
1451d65f6f70SBen Gras DLOG((DLOG_CLEAN,
1452d65f6f70SBen Gras "lfs_strategy: sleeping on ino %d lbn %"
1453d65f6f70SBen Gras PRId64 "\n", ip->i_number, bp->b_lblkno));
1454d65f6f70SBen Gras mutex_enter(&lfs_lock);
1455d65f6f70SBen Gras if (LFS_SEGLOCK_HELD(fs) && fs->lfs_iocount) {
145684d9c625SLionel Sambuc /*
145784d9c625SLionel Sambuc * Cleaner can't wait for itself.
145884d9c625SLionel Sambuc * Instead, wait for the blocks
145984d9c625SLionel Sambuc * to be written to disk.
146084d9c625SLionel Sambuc * XXX we need pribio in the test
146184d9c625SLionel Sambuc * XXX here.
146284d9c625SLionel Sambuc */
1463d65f6f70SBen Gras mtsleep(&fs->lfs_iocount,
1464d65f6f70SBen Gras (PRIBIO + 1) | PNORELOCK,
146584d9c625SLionel Sambuc "clean2", hz/10 + 1,
1466d65f6f70SBen Gras &lfs_lock);
1467d65f6f70SBen Gras slept = 1;
146884d9c625SLionel Sambuc ++loopcount;
1469d65f6f70SBen Gras break;
1470d65f6f70SBen Gras } else if (fs->lfs_seglock) {
1471d65f6f70SBen Gras mtsleep(&fs->lfs_seglock,
1472d65f6f70SBen Gras (PRIBIO + 1) | PNORELOCK,
1473d65f6f70SBen Gras "clean1", 0,
1474d65f6f70SBen Gras &lfs_lock);
1475d65f6f70SBen Gras slept = 1;
1476d65f6f70SBen Gras break;
1477d65f6f70SBen Gras }
1478d65f6f70SBen Gras mutex_exit(&lfs_lock);
1479d65f6f70SBen Gras }
1480d65f6f70SBen Gras }
1481d65f6f70SBen Gras mutex_enter(&lfs_lock);
148284d9c625SLionel Sambuc if (loopcount > MAXLOOP) {
148384d9c625SLionel Sambuc printf("lfs_strategy: breaking out of clean2 loop\n");
148484d9c625SLionel Sambuc break;
148584d9c625SLionel Sambuc }
1486d65f6f70SBen Gras }
1487d65f6f70SBen Gras mutex_exit(&lfs_lock);
1488d65f6f70SBen Gras
1489d65f6f70SBen Gras vp = ip->i_devvp;
149084d9c625SLionel Sambuc return VOP_STRATEGY(vp, bp);
1491d65f6f70SBen Gras }
1492d65f6f70SBen Gras
1493d65f6f70SBen Gras /*
1494d65f6f70SBen Gras * Inline lfs_segwrite/lfs_writevnodes, but just for dirops.
1495d65f6f70SBen Gras * Technically this is a checkpoint (the on-disk state is valid)
1496d65f6f70SBen Gras * even though we are leaving out all the file data.
1497d65f6f70SBen Gras */
149884d9c625SLionel Sambuc int
lfs_flush_dirops(struct lfs * fs)149984d9c625SLionel Sambuc lfs_flush_dirops(struct lfs *fs)
150084d9c625SLionel Sambuc {
150184d9c625SLionel Sambuc struct inode *ip, *nip;
150284d9c625SLionel Sambuc struct vnode *vp;
1503*0a6a1f1dSLionel Sambuc extern int lfs_dostats; /* XXX this does not belong here */
150484d9c625SLionel Sambuc struct segment *sp;
1505*0a6a1f1dSLionel Sambuc SEGSUM *ssp;
150684d9c625SLionel Sambuc int flags = 0;
150784d9c625SLionel Sambuc int error = 0;
150884d9c625SLionel Sambuc
150984d9c625SLionel Sambuc ASSERT_MAYBE_SEGLOCK(fs);
151084d9c625SLionel Sambuc KASSERT(fs->lfs_nadirop == 0);
151184d9c625SLionel Sambuc
151284d9c625SLionel Sambuc if (fs->lfs_ronly)
151384d9c625SLionel Sambuc return EROFS;
151484d9c625SLionel Sambuc
151584d9c625SLionel Sambuc mutex_enter(&lfs_lock);
151684d9c625SLionel Sambuc if (TAILQ_FIRST(&fs->lfs_dchainhd) == NULL) {
151784d9c625SLionel Sambuc mutex_exit(&lfs_lock);
151884d9c625SLionel Sambuc return 0;
151984d9c625SLionel Sambuc } else
152084d9c625SLionel Sambuc mutex_exit(&lfs_lock);
152184d9c625SLionel Sambuc
152284d9c625SLionel Sambuc if (lfs_dostats)
152384d9c625SLionel Sambuc ++lfs_stats.flush_invoked;
152484d9c625SLionel Sambuc
1525d65f6f70SBen Gras lfs_imtime(fs);
152684d9c625SLionel Sambuc lfs_seglock(fs, flags);
1527d65f6f70SBen Gras sp = fs->lfs_sp;
1528d65f6f70SBen Gras
1529d65f6f70SBen Gras /*
1530d65f6f70SBen Gras * lfs_writevnodes, optimized to get dirops out of the way.
1531d65f6f70SBen Gras * Only write dirops, and don't flush files' pages, only
1532d65f6f70SBen Gras * blocks from the directories.
1533d65f6f70SBen Gras *
1534d65f6f70SBen Gras * We don't need to vref these files because they are
1535d65f6f70SBen Gras * dirops and so hold an extra reference until the
1536d65f6f70SBen Gras * segunlock clears them of that status.
1537d65f6f70SBen Gras *
1538d65f6f70SBen Gras * We don't need to check for IN_ADIROP because we know that
1539d65f6f70SBen Gras * no dirops are active.
1540d65f6f70SBen Gras *
1541d65f6f70SBen Gras */
1542d65f6f70SBen Gras mutex_enter(&lfs_lock);
1543d65f6f70SBen Gras for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
1544d65f6f70SBen Gras nip = TAILQ_NEXT(ip, i_lfs_dchain);
1545d65f6f70SBen Gras mutex_exit(&lfs_lock);
1546d65f6f70SBen Gras vp = ITOV(ip);
1547*0a6a1f1dSLionel Sambuc mutex_enter(vp->v_interlock);
1548d65f6f70SBen Gras
1549d65f6f70SBen Gras KASSERT((ip->i_flag & IN_ADIROP) == 0);
155084d9c625SLionel Sambuc KASSERT(vp->v_uflag & VU_DIROP);
1551*0a6a1f1dSLionel Sambuc KASSERT(vdead_check(vp, VDEAD_NOWAIT) == 0);
1552d65f6f70SBen Gras
1553d65f6f70SBen Gras /*
1554d65f6f70SBen Gras * All writes to directories come from dirops; all
1555d65f6f70SBen Gras * writes to files' direct blocks go through the page
1556d65f6f70SBen Gras * cache, which we're not touching. Reads to files
1557d65f6f70SBen Gras * and/or directories will not be affected by writing
1558d65f6f70SBen Gras * directory blocks inodes and file inodes. So we don't
155984d9c625SLionel Sambuc * really need to lock.
1560d65f6f70SBen Gras */
1561*0a6a1f1dSLionel Sambuc if (vdead_check(vp, VDEAD_NOWAIT) != 0) {
1562*0a6a1f1dSLionel Sambuc mutex_exit(vp->v_interlock);
1563d65f6f70SBen Gras mutex_enter(&lfs_lock);
1564d65f6f70SBen Gras continue;
1565d65f6f70SBen Gras }
1566*0a6a1f1dSLionel Sambuc mutex_exit(vp->v_interlock);
1567d65f6f70SBen Gras /* XXX see below
1568d65f6f70SBen Gras * waslocked = VOP_ISLOCKED(vp);
1569d65f6f70SBen Gras */
1570d65f6f70SBen Gras if (vp->v_type != VREG &&
1571d65f6f70SBen Gras ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp))) {
157284d9c625SLionel Sambuc error = lfs_writefile(fs, sp, vp);
1573d65f6f70SBen Gras if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
1574d65f6f70SBen Gras !(ip->i_flag & IN_ALLMOD)) {
1575d65f6f70SBen Gras mutex_enter(&lfs_lock);
1576d65f6f70SBen Gras LFS_SET_UINO(ip, IN_MODIFIED);
1577d65f6f70SBen Gras mutex_exit(&lfs_lock);
1578d65f6f70SBen Gras }
157984d9c625SLionel Sambuc if (error && (sp->seg_flags & SEGM_SINGLE)) {
158084d9c625SLionel Sambuc mutex_enter(&lfs_lock);
158184d9c625SLionel Sambuc error = EAGAIN;
158284d9c625SLionel Sambuc break;
158384d9c625SLionel Sambuc }
1584d65f6f70SBen Gras }
1585d65f6f70SBen Gras KDASSERT(ip->i_number != LFS_IFILE_INUM);
158684d9c625SLionel Sambuc error = lfs_writeinode(fs, sp, ip);
1587d65f6f70SBen Gras mutex_enter(&lfs_lock);
158884d9c625SLionel Sambuc if (error && (sp->seg_flags & SEGM_SINGLE)) {
158984d9c625SLionel Sambuc error = EAGAIN;
159084d9c625SLionel Sambuc break;
159184d9c625SLionel Sambuc }
159284d9c625SLionel Sambuc
1593d65f6f70SBen Gras /*
159484d9c625SLionel Sambuc * We might need to update these inodes again,
159584d9c625SLionel Sambuc * for example, if they have data blocks to write.
159684d9c625SLionel Sambuc * Make sure that after this flush, they are still
159784d9c625SLionel Sambuc * marked IN_MODIFIED so that we don't forget to
159884d9c625SLionel Sambuc * write them.
1599d65f6f70SBen Gras */
160084d9c625SLionel Sambuc /* XXX only for non-directories? --KS */
160184d9c625SLionel Sambuc LFS_SET_UINO(ip, IN_MODIFIED);
1602d65f6f70SBen Gras }
1603d65f6f70SBen Gras mutex_exit(&lfs_lock);
1604d65f6f70SBen Gras /* We've written all the dirops there are */
1605*0a6a1f1dSLionel Sambuc ssp = (SEGSUM *)sp->segsum;
1606*0a6a1f1dSLionel Sambuc lfs_ss_setflags(fs, ssp, lfs_ss_getflags(fs, ssp) & ~(SS_CONT));
1607d65f6f70SBen Gras lfs_finalize_fs_seguse(fs);
1608d65f6f70SBen Gras (void) lfs_writeseg(fs, sp);
1609d65f6f70SBen Gras lfs_segunlock(fs);
161084d9c625SLionel Sambuc
161184d9c625SLionel Sambuc return error;
1612d65f6f70SBen Gras }
1613d65f6f70SBen Gras
1614d65f6f70SBen Gras /*
1615d65f6f70SBen Gras * Flush all vnodes for which the pagedaemon has requested pageouts.
1616d65f6f70SBen Gras * Skip over any files that are marked VU_DIROP (since lfs_flush_dirop()
1617d65f6f70SBen Gras * has just run, this would be an error). If we have to skip a vnode
1618d65f6f70SBen Gras * for any reason, just skip it; if we have to wait for the cleaner,
1619d65f6f70SBen Gras * abort. The writer daemon will call us again later.
1620d65f6f70SBen Gras */
162184d9c625SLionel Sambuc int
lfs_flush_pchain(struct lfs * fs)1622d65f6f70SBen Gras lfs_flush_pchain(struct lfs *fs)
1623d65f6f70SBen Gras {
1624d65f6f70SBen Gras struct inode *ip, *nip;
1625d65f6f70SBen Gras struct vnode *vp;
1626d65f6f70SBen Gras extern int lfs_dostats;
1627d65f6f70SBen Gras struct segment *sp;
162884d9c625SLionel Sambuc int error, error2;
1629d65f6f70SBen Gras
1630d65f6f70SBen Gras ASSERT_NO_SEGLOCK(fs);
1631d65f6f70SBen Gras
1632d65f6f70SBen Gras if (fs->lfs_ronly)
163384d9c625SLionel Sambuc return EROFS;
1634d65f6f70SBen Gras
1635d65f6f70SBen Gras mutex_enter(&lfs_lock);
1636d65f6f70SBen Gras if (TAILQ_FIRST(&fs->lfs_pchainhd) == NULL) {
1637d65f6f70SBen Gras mutex_exit(&lfs_lock);
163884d9c625SLionel Sambuc return 0;
1639d65f6f70SBen Gras } else
1640d65f6f70SBen Gras mutex_exit(&lfs_lock);
1641d65f6f70SBen Gras
1642d65f6f70SBen Gras /* Get dirops out of the way */
164384d9c625SLionel Sambuc if ((error = lfs_flush_dirops(fs)) != 0)
164484d9c625SLionel Sambuc return error;
1645d65f6f70SBen Gras
1646d65f6f70SBen Gras if (lfs_dostats)
1647d65f6f70SBen Gras ++lfs_stats.flush_invoked;
1648d65f6f70SBen Gras
1649d65f6f70SBen Gras /*
1650d65f6f70SBen Gras * Inline lfs_segwrite/lfs_writevnodes, but just for pageouts.
1651d65f6f70SBen Gras */
1652d65f6f70SBen Gras lfs_imtime(fs);
1653d65f6f70SBen Gras lfs_seglock(fs, 0);
1654d65f6f70SBen Gras sp = fs->lfs_sp;
1655d65f6f70SBen Gras
1656d65f6f70SBen Gras /*
1657d65f6f70SBen Gras * lfs_writevnodes, optimized to clear pageout requests.
1658d65f6f70SBen Gras * Only write non-dirop files that are in the pageout queue.
1659d65f6f70SBen Gras * We're very conservative about what we write; we want to be
1660d65f6f70SBen Gras * fast and async.
1661d65f6f70SBen Gras */
1662d65f6f70SBen Gras mutex_enter(&lfs_lock);
1663d65f6f70SBen Gras top:
1664d65f6f70SBen Gras for (ip = TAILQ_FIRST(&fs->lfs_pchainhd); ip != NULL; ip = nip) {
1665*0a6a1f1dSLionel Sambuc struct mount *mp = ITOV(ip)->v_mount;
1666*0a6a1f1dSLionel Sambuc ino_t ino = ip->i_number;
1667*0a6a1f1dSLionel Sambuc
1668d65f6f70SBen Gras nip = TAILQ_NEXT(ip, i_lfs_pchain);
1669d65f6f70SBen Gras
1670d65f6f70SBen Gras if (!(ip->i_flags & IN_PAGING))
1671d65f6f70SBen Gras goto top;
1672d65f6f70SBen Gras
1673d65f6f70SBen Gras mutex_exit(&lfs_lock);
1674*0a6a1f1dSLionel Sambuc if (vcache_get(mp, &ino, sizeof(ino), &vp) != 0) {
1675*0a6a1f1dSLionel Sambuc mutex_enter(&lfs_lock);
1676*0a6a1f1dSLionel Sambuc continue;
1677*0a6a1f1dSLionel Sambuc };
1678*0a6a1f1dSLionel Sambuc if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1679*0a6a1f1dSLionel Sambuc vrele(vp);
1680d65f6f70SBen Gras mutex_enter(&lfs_lock);
1681d65f6f70SBen Gras continue;
1682d65f6f70SBen Gras }
1683*0a6a1f1dSLionel Sambuc ip = VTOI(vp);
1684*0a6a1f1dSLionel Sambuc mutex_enter(&lfs_lock);
1685*0a6a1f1dSLionel Sambuc if ((vp->v_uflag & VU_DIROP) != 0 || vp->v_type != VREG ||
1686*0a6a1f1dSLionel Sambuc !(ip->i_flags & IN_PAGING)) {
1687*0a6a1f1dSLionel Sambuc mutex_exit(&lfs_lock);
1688*0a6a1f1dSLionel Sambuc vput(vp);
1689*0a6a1f1dSLionel Sambuc mutex_enter(&lfs_lock);
1690*0a6a1f1dSLionel Sambuc goto top;
1691*0a6a1f1dSLionel Sambuc }
1692*0a6a1f1dSLionel Sambuc mutex_exit(&lfs_lock);
1693d65f6f70SBen Gras
1694d65f6f70SBen Gras error = lfs_writefile(fs, sp, vp);
1695d65f6f70SBen Gras if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
1696d65f6f70SBen Gras !(ip->i_flag & IN_ALLMOD)) {
1697d65f6f70SBen Gras mutex_enter(&lfs_lock);
1698d65f6f70SBen Gras LFS_SET_UINO(ip, IN_MODIFIED);
1699d65f6f70SBen Gras mutex_exit(&lfs_lock);
1700d65f6f70SBen Gras }
1701d65f6f70SBen Gras KDASSERT(ip->i_number != LFS_IFILE_INUM);
170284d9c625SLionel Sambuc error2 = lfs_writeinode(fs, sp, ip);
1703d65f6f70SBen Gras
1704d65f6f70SBen Gras VOP_UNLOCK(vp);
1705*0a6a1f1dSLionel Sambuc vrele(vp);
1706d65f6f70SBen Gras
170784d9c625SLionel Sambuc if (error == EAGAIN || error2 == EAGAIN) {
1708d65f6f70SBen Gras lfs_writeseg(fs, sp);
1709d65f6f70SBen Gras mutex_enter(&lfs_lock);
1710d65f6f70SBen Gras break;
1711d65f6f70SBen Gras }
1712d65f6f70SBen Gras mutex_enter(&lfs_lock);
1713d65f6f70SBen Gras }
1714d65f6f70SBen Gras mutex_exit(&lfs_lock);
1715d65f6f70SBen Gras (void) lfs_writeseg(fs, sp);
1716d65f6f70SBen Gras lfs_segunlock(fs);
171784d9c625SLionel Sambuc
171884d9c625SLionel Sambuc return 0;
1719d65f6f70SBen Gras }
1720d65f6f70SBen Gras
1721d65f6f70SBen Gras /*
1722*0a6a1f1dSLionel Sambuc * Conversion for compat.
1723*0a6a1f1dSLionel Sambuc */
1724*0a6a1f1dSLionel Sambuc static void
block_info_from_70(BLOCK_INFO * bi,const BLOCK_INFO_70 * bi70)1725*0a6a1f1dSLionel Sambuc block_info_from_70(BLOCK_INFO *bi, const BLOCK_INFO_70 *bi70)
1726*0a6a1f1dSLionel Sambuc {
1727*0a6a1f1dSLionel Sambuc bi->bi_inode = bi70->bi_inode;
1728*0a6a1f1dSLionel Sambuc bi->bi_lbn = bi70->bi_lbn;
1729*0a6a1f1dSLionel Sambuc bi->bi_daddr = bi70->bi_daddr;
1730*0a6a1f1dSLionel Sambuc bi->bi_segcreate = bi70->bi_segcreate;
1731*0a6a1f1dSLionel Sambuc bi->bi_version = bi70->bi_version;
1732*0a6a1f1dSLionel Sambuc bi->bi_bp = bi70->bi_bp;
1733*0a6a1f1dSLionel Sambuc bi->bi_size = bi70->bi_size;
1734*0a6a1f1dSLionel Sambuc }
1735*0a6a1f1dSLionel Sambuc
1736*0a6a1f1dSLionel Sambuc static void
block_info_to_70(BLOCK_INFO_70 * bi70,const BLOCK_INFO * bi)1737*0a6a1f1dSLionel Sambuc block_info_to_70(BLOCK_INFO_70 *bi70, const BLOCK_INFO *bi)
1738*0a6a1f1dSLionel Sambuc {
1739*0a6a1f1dSLionel Sambuc bi70->bi_inode = bi->bi_inode;
1740*0a6a1f1dSLionel Sambuc bi70->bi_lbn = bi->bi_lbn;
1741*0a6a1f1dSLionel Sambuc bi70->bi_daddr = bi->bi_daddr;
1742*0a6a1f1dSLionel Sambuc bi70->bi_segcreate = bi->bi_segcreate;
1743*0a6a1f1dSLionel Sambuc bi70->bi_version = bi->bi_version;
1744*0a6a1f1dSLionel Sambuc bi70->bi_bp = bi->bi_bp;
1745*0a6a1f1dSLionel Sambuc bi70->bi_size = bi->bi_size;
1746*0a6a1f1dSLionel Sambuc }
1747*0a6a1f1dSLionel Sambuc
1748*0a6a1f1dSLionel Sambuc /*
1749d65f6f70SBen Gras * Provide a fcntl interface to sys_lfs_{segwait,bmapv,markv}.
1750d65f6f70SBen Gras */
1751d65f6f70SBen Gras int
lfs_fcntl(void * v)1752d65f6f70SBen Gras lfs_fcntl(void *v)
1753d65f6f70SBen Gras {
1754d65f6f70SBen Gras struct vop_fcntl_args /* {
1755d65f6f70SBen Gras struct vnode *a_vp;
1756d65f6f70SBen Gras u_int a_command;
1757d65f6f70SBen Gras void * a_data;
1758d65f6f70SBen Gras int a_fflag;
1759d65f6f70SBen Gras kauth_cred_t a_cred;
1760d65f6f70SBen Gras } */ *ap = v;
1761d65f6f70SBen Gras struct timeval tv;
1762d65f6f70SBen Gras struct timeval *tvp;
1763d65f6f70SBen Gras BLOCK_INFO *blkiov;
1764*0a6a1f1dSLionel Sambuc BLOCK_INFO_70 *blkiov70;
1765d65f6f70SBen Gras CLEANERINFO *cip;
1766d65f6f70SBen Gras SEGUSE *sup;
1767*0a6a1f1dSLionel Sambuc int blkcnt, i, error;
1768d65f6f70SBen Gras size_t fh_size;
1769d65f6f70SBen Gras struct lfs_fcntl_markv blkvp;
1770*0a6a1f1dSLionel Sambuc struct lfs_fcntl_markv_70 blkvp70;
1771d65f6f70SBen Gras struct lwp *l;
1772d65f6f70SBen Gras fsid_t *fsidp;
1773d65f6f70SBen Gras struct lfs *fs;
1774d65f6f70SBen Gras struct buf *bp;
1775d65f6f70SBen Gras fhandle_t *fhp;
1776d65f6f70SBen Gras daddr_t off;
177784d9c625SLionel Sambuc int oclean;
1778d65f6f70SBen Gras
1779d65f6f70SBen Gras /* Only respect LFS fcntls on fs root or Ifile */
178084d9c625SLionel Sambuc if (VTOI(ap->a_vp)->i_number != ULFS_ROOTINO &&
1781d65f6f70SBen Gras VTOI(ap->a_vp)->i_number != LFS_IFILE_INUM) {
178284d9c625SLionel Sambuc return ulfs_fcntl(v);
1783d65f6f70SBen Gras }
1784d65f6f70SBen Gras
1785d65f6f70SBen Gras /* Avoid locking a draining lock */
1786d65f6f70SBen Gras if (ap->a_vp->v_mount->mnt_iflag & IMNT_UNMOUNT) {
1787d65f6f70SBen Gras return ESHUTDOWN;
1788d65f6f70SBen Gras }
1789d65f6f70SBen Gras
1790d65f6f70SBen Gras /* LFS control and monitoring fcntls are available only to root */
1791d65f6f70SBen Gras l = curlwp;
1792d65f6f70SBen Gras if (((ap->a_command & 0xff00) >> 8) == 'L' &&
179384d9c625SLionel Sambuc (error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
179484d9c625SLionel Sambuc KAUTH_REQ_SYSTEM_LFS_FCNTL, NULL, NULL, NULL)) != 0)
1795d65f6f70SBen Gras return (error);
1796d65f6f70SBen Gras
1797d65f6f70SBen Gras fs = VTOI(ap->a_vp)->i_lfs;
1798d65f6f70SBen Gras fsidp = &ap->a_vp->v_mount->mnt_stat.f_fsidx;
1799d65f6f70SBen Gras
1800d65f6f70SBen Gras error = 0;
1801d65f6f70SBen Gras switch ((int)ap->a_command) {
1802d65f6f70SBen Gras case LFCNSEGWAITALL_COMPAT_50:
1803d65f6f70SBen Gras case LFCNSEGWAITALL_COMPAT:
1804d65f6f70SBen Gras fsidp = NULL;
1805*0a6a1f1dSLionel Sambuc /* FALLTHROUGH */
1806d65f6f70SBen Gras case LFCNSEGWAIT_COMPAT_50:
1807d65f6f70SBen Gras case LFCNSEGWAIT_COMPAT:
1808d65f6f70SBen Gras {
1809d65f6f70SBen Gras struct timeval50 *tvp50
1810d65f6f70SBen Gras = (struct timeval50 *)ap->a_data;
1811d65f6f70SBen Gras timeval50_to_timeval(tvp50, &tv);
1812d65f6f70SBen Gras tvp = &tv;
1813d65f6f70SBen Gras }
1814d65f6f70SBen Gras goto segwait_common;
1815d65f6f70SBen Gras case LFCNSEGWAITALL:
1816d65f6f70SBen Gras fsidp = NULL;
1817*0a6a1f1dSLionel Sambuc /* FALLTHROUGH */
1818d65f6f70SBen Gras case LFCNSEGWAIT:
1819d65f6f70SBen Gras tvp = (struct timeval *)ap->a_data;
1820d65f6f70SBen Gras segwait_common:
1821d65f6f70SBen Gras mutex_enter(&lfs_lock);
1822d65f6f70SBen Gras ++fs->lfs_sleepers;
1823d65f6f70SBen Gras mutex_exit(&lfs_lock);
1824d65f6f70SBen Gras
1825d65f6f70SBen Gras error = lfs_segwait(fsidp, tvp);
1826d65f6f70SBen Gras
1827d65f6f70SBen Gras mutex_enter(&lfs_lock);
1828d65f6f70SBen Gras if (--fs->lfs_sleepers == 0)
1829d65f6f70SBen Gras wakeup(&fs->lfs_sleepers);
1830d65f6f70SBen Gras mutex_exit(&lfs_lock);
1831d65f6f70SBen Gras return error;
1832d65f6f70SBen Gras
1833*0a6a1f1dSLionel Sambuc case LFCNBMAPV_COMPAT_70:
1834*0a6a1f1dSLionel Sambuc case LFCNMARKV_COMPAT_70:
1835*0a6a1f1dSLionel Sambuc blkvp70 = *(struct lfs_fcntl_markv_70 *)ap->a_data;
1836*0a6a1f1dSLionel Sambuc
1837*0a6a1f1dSLionel Sambuc blkcnt = blkvp70.blkcnt;
1838*0a6a1f1dSLionel Sambuc if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
1839*0a6a1f1dSLionel Sambuc return (EINVAL);
1840*0a6a1f1dSLionel Sambuc blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
1841*0a6a1f1dSLionel Sambuc blkiov70 = lfs_malloc(fs, sizeof(BLOCK_INFO_70), LFS_NB_BLKIOV);
1842*0a6a1f1dSLionel Sambuc for (i = 0; i < blkcnt; i++) {
1843*0a6a1f1dSLionel Sambuc error = copyin(&blkvp70.blkiov[i], blkiov70,
1844*0a6a1f1dSLionel Sambuc sizeof(*blkiov70));
1845*0a6a1f1dSLionel Sambuc if (error) {
1846*0a6a1f1dSLionel Sambuc lfs_free(fs, blkiov70, LFS_NB_BLKIOV);
1847*0a6a1f1dSLionel Sambuc lfs_free(fs, blkiov, LFS_NB_BLKIOV);
1848*0a6a1f1dSLionel Sambuc return error;
1849*0a6a1f1dSLionel Sambuc }
1850*0a6a1f1dSLionel Sambuc block_info_from_70(&blkiov[i], blkiov70);
1851*0a6a1f1dSLionel Sambuc }
1852*0a6a1f1dSLionel Sambuc
1853*0a6a1f1dSLionel Sambuc mutex_enter(&lfs_lock);
1854*0a6a1f1dSLionel Sambuc ++fs->lfs_sleepers;
1855*0a6a1f1dSLionel Sambuc mutex_exit(&lfs_lock);
1856*0a6a1f1dSLionel Sambuc if (ap->a_command == LFCNBMAPV)
1857*0a6a1f1dSLionel Sambuc error = lfs_bmapv(l, fsidp, blkiov, blkcnt);
1858*0a6a1f1dSLionel Sambuc else /* LFCNMARKV */
1859*0a6a1f1dSLionel Sambuc error = lfs_markv(l, fsidp, blkiov, blkcnt);
1860*0a6a1f1dSLionel Sambuc if (error == 0) {
1861*0a6a1f1dSLionel Sambuc for (i = 0; i < blkcnt; i++) {
1862*0a6a1f1dSLionel Sambuc block_info_to_70(blkiov70, &blkiov[i]);
1863*0a6a1f1dSLionel Sambuc error = copyout(blkiov70, &blkvp70.blkiov[i],
1864*0a6a1f1dSLionel Sambuc sizeof(*blkiov70));
1865*0a6a1f1dSLionel Sambuc if (error) {
1866*0a6a1f1dSLionel Sambuc break;
1867*0a6a1f1dSLionel Sambuc }
1868*0a6a1f1dSLionel Sambuc }
1869*0a6a1f1dSLionel Sambuc }
1870*0a6a1f1dSLionel Sambuc mutex_enter(&lfs_lock);
1871*0a6a1f1dSLionel Sambuc if (--fs->lfs_sleepers == 0)
1872*0a6a1f1dSLionel Sambuc wakeup(&fs->lfs_sleepers);
1873*0a6a1f1dSLionel Sambuc mutex_exit(&lfs_lock);
1874*0a6a1f1dSLionel Sambuc lfs_free(fs, blkiov, LFS_NB_BLKIOV);
1875*0a6a1f1dSLionel Sambuc return error;
1876*0a6a1f1dSLionel Sambuc
1877d65f6f70SBen Gras case LFCNBMAPV:
1878d65f6f70SBen Gras case LFCNMARKV:
1879d65f6f70SBen Gras blkvp = *(struct lfs_fcntl_markv *)ap->a_data;
1880d65f6f70SBen Gras
1881d65f6f70SBen Gras blkcnt = blkvp.blkcnt;
1882d65f6f70SBen Gras if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
1883d65f6f70SBen Gras return (EINVAL);
1884d65f6f70SBen Gras blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
1885d65f6f70SBen Gras if ((error = copyin(blkvp.blkiov, blkiov,
1886d65f6f70SBen Gras blkcnt * sizeof(BLOCK_INFO))) != 0) {
1887d65f6f70SBen Gras lfs_free(fs, blkiov, LFS_NB_BLKIOV);
1888d65f6f70SBen Gras return error;
1889d65f6f70SBen Gras }
1890d65f6f70SBen Gras
1891d65f6f70SBen Gras mutex_enter(&lfs_lock);
1892d65f6f70SBen Gras ++fs->lfs_sleepers;
1893d65f6f70SBen Gras mutex_exit(&lfs_lock);
1894d65f6f70SBen Gras if (ap->a_command == LFCNBMAPV)
1895*0a6a1f1dSLionel Sambuc error = lfs_bmapv(l, fsidp, blkiov, blkcnt);
1896d65f6f70SBen Gras else /* LFCNMARKV */
1897*0a6a1f1dSLionel Sambuc error = lfs_markv(l, fsidp, blkiov, blkcnt);
1898d65f6f70SBen Gras if (error == 0)
1899d65f6f70SBen Gras error = copyout(blkiov, blkvp.blkiov,
1900d65f6f70SBen Gras blkcnt * sizeof(BLOCK_INFO));
1901d65f6f70SBen Gras mutex_enter(&lfs_lock);
1902d65f6f70SBen Gras if (--fs->lfs_sleepers == 0)
1903d65f6f70SBen Gras wakeup(&fs->lfs_sleepers);
1904d65f6f70SBen Gras mutex_exit(&lfs_lock);
1905d65f6f70SBen Gras lfs_free(fs, blkiov, LFS_NB_BLKIOV);
1906d65f6f70SBen Gras return error;
1907d65f6f70SBen Gras
1908d65f6f70SBen Gras case LFCNRECLAIM:
1909d65f6f70SBen Gras /*
1910d65f6f70SBen Gras * Flush dirops and write Ifile, allowing empty segments
1911d65f6f70SBen Gras * to be immediately reclaimed.
1912d65f6f70SBen Gras */
1913d65f6f70SBen Gras lfs_writer_enter(fs, "pndirop");
1914*0a6a1f1dSLionel Sambuc off = lfs_sb_getoffset(fs);
1915d65f6f70SBen Gras lfs_seglock(fs, SEGM_FORCE_CKP | SEGM_CKP);
1916d65f6f70SBen Gras lfs_flush_dirops(fs);
1917d65f6f70SBen Gras LFS_CLEANERINFO(cip, fs, bp);
1918*0a6a1f1dSLionel Sambuc oclean = lfs_ci_getclean(fs, cip);
1919d65f6f70SBen Gras LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1920d65f6f70SBen Gras lfs_segwrite(ap->a_vp->v_mount, SEGM_FORCE_CKP);
1921d65f6f70SBen Gras fs->lfs_sp->seg_flags |= SEGM_PROT;
1922d65f6f70SBen Gras lfs_segunlock(fs);
1923d65f6f70SBen Gras lfs_writer_leave(fs);
1924d65f6f70SBen Gras
1925d65f6f70SBen Gras #ifdef DEBUG
1926d65f6f70SBen Gras LFS_CLEANERINFO(cip, fs, bp);
1927d65f6f70SBen Gras DLOG((DLOG_CLEAN, "lfs_fcntl: reclaim wrote %" PRId64
1928d65f6f70SBen Gras " blocks, cleaned %" PRId32 " segments (activesb %d)\n",
1929*0a6a1f1dSLionel Sambuc lfs_sb_getoffset(fs) - off,
1930*0a6a1f1dSLionel Sambuc lfs_ci_getclean(fs, cip) - oclean,
1931d65f6f70SBen Gras fs->lfs_activesb));
1932d65f6f70SBen Gras LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
193384d9c625SLionel Sambuc #else
193484d9c625SLionel Sambuc __USE(oclean);
193584d9c625SLionel Sambuc __USE(off);
1936d65f6f70SBen Gras #endif
1937d65f6f70SBen Gras
1938d65f6f70SBen Gras return 0;
1939d65f6f70SBen Gras
1940d65f6f70SBen Gras case LFCNIFILEFH_COMPAT:
1941d65f6f70SBen Gras /* Return the filehandle of the Ifile */
1942d65f6f70SBen Gras if ((error = kauth_authorize_system(l->l_cred,
1943d65f6f70SBen Gras KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)) != 0)
1944d65f6f70SBen Gras return (error);
1945d65f6f70SBen Gras fhp = (struct fhandle *)ap->a_data;
1946d65f6f70SBen Gras fhp->fh_fsid = *fsidp;
1947d65f6f70SBen Gras fh_size = 16; /* former VFS_MAXFIDSIZ */
1948d65f6f70SBen Gras return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
1949d65f6f70SBen Gras
1950d65f6f70SBen Gras case LFCNIFILEFH_COMPAT2:
1951d65f6f70SBen Gras case LFCNIFILEFH:
1952d65f6f70SBen Gras /* Return the filehandle of the Ifile */
1953d65f6f70SBen Gras fhp = (struct fhandle *)ap->a_data;
1954d65f6f70SBen Gras fhp->fh_fsid = *fsidp;
1955d65f6f70SBen Gras fh_size = sizeof(struct lfs_fhandle) -
1956d65f6f70SBen Gras offsetof(fhandle_t, fh_fid);
1957d65f6f70SBen Gras return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
1958d65f6f70SBen Gras
1959d65f6f70SBen Gras case LFCNREWIND:
1960d65f6f70SBen Gras /* Move lfs_offset to the lowest-numbered segment */
1961d65f6f70SBen Gras return lfs_rewind(fs, *(int *)ap->a_data);
1962d65f6f70SBen Gras
1963d65f6f70SBen Gras case LFCNINVAL:
1964d65f6f70SBen Gras /* Mark a segment SEGUSE_INVAL */
1965d65f6f70SBen Gras LFS_SEGENTRY(sup, fs, *(int *)ap->a_data, bp);
1966d65f6f70SBen Gras if (sup->su_nbytes > 0) {
1967d65f6f70SBen Gras brelse(bp, 0);
1968d65f6f70SBen Gras lfs_unset_inval_all(fs);
1969d65f6f70SBen Gras return EBUSY;
1970d65f6f70SBen Gras }
1971d65f6f70SBen Gras sup->su_flags |= SEGUSE_INVAL;
1972d65f6f70SBen Gras VOP_BWRITE(bp->b_vp, bp);
1973d65f6f70SBen Gras return 0;
1974d65f6f70SBen Gras
1975d65f6f70SBen Gras case LFCNRESIZE:
1976d65f6f70SBen Gras /* Resize the filesystem */
1977d65f6f70SBen Gras return lfs_resize_fs(fs, *(int *)ap->a_data);
1978d65f6f70SBen Gras
1979d65f6f70SBen Gras case LFCNWRAPSTOP:
1980d65f6f70SBen Gras case LFCNWRAPSTOP_COMPAT:
1981d65f6f70SBen Gras /*
1982d65f6f70SBen Gras * Hold lfs_newseg at segment 0; if requested, sleep until
1983d65f6f70SBen Gras * the filesystem wraps around. To support external agents
1984d65f6f70SBen Gras * (dump, fsck-based regression test) that need to look at
1985d65f6f70SBen Gras * a snapshot of the filesystem, without necessarily
1986d65f6f70SBen Gras * requiring that all fs activity stops.
1987d65f6f70SBen Gras */
1988d65f6f70SBen Gras if (fs->lfs_stoplwp == curlwp)
1989d65f6f70SBen Gras return EALREADY;
1990d65f6f70SBen Gras
1991d65f6f70SBen Gras mutex_enter(&lfs_lock);
1992d65f6f70SBen Gras while (fs->lfs_stoplwp != NULL)
1993d65f6f70SBen Gras cv_wait(&fs->lfs_stopcv, &lfs_lock);
1994d65f6f70SBen Gras fs->lfs_stoplwp = curlwp;
1995d65f6f70SBen Gras if (fs->lfs_nowrap == 0)
1996*0a6a1f1dSLionel Sambuc log(LOG_NOTICE, "%s: disabled log wrap\n",
1997*0a6a1f1dSLionel Sambuc lfs_sb_getfsmnt(fs));
1998d65f6f70SBen Gras ++fs->lfs_nowrap;
1999d65f6f70SBen Gras if (*(int *)ap->a_data == 1
2000d65f6f70SBen Gras || ap->a_command == LFCNWRAPSTOP_COMPAT) {
2001d65f6f70SBen Gras log(LOG_NOTICE, "LFCNSTOPWRAP waiting for log wrap\n");
2002d65f6f70SBen Gras error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
2003d65f6f70SBen Gras "segwrap", 0, &lfs_lock);
2004d65f6f70SBen Gras log(LOG_NOTICE, "LFCNSTOPWRAP done waiting\n");
2005d65f6f70SBen Gras if (error) {
2006d65f6f70SBen Gras lfs_wrapgo(fs, VTOI(ap->a_vp), 0);
2007d65f6f70SBen Gras }
2008d65f6f70SBen Gras }
2009d65f6f70SBen Gras mutex_exit(&lfs_lock);
2010d65f6f70SBen Gras return 0;
2011d65f6f70SBen Gras
2012d65f6f70SBen Gras case LFCNWRAPGO:
2013d65f6f70SBen Gras case LFCNWRAPGO_COMPAT:
2014d65f6f70SBen Gras /*
2015d65f6f70SBen Gras * Having done its work, the agent wakes up the writer.
2016d65f6f70SBen Gras * If the argument is 1, it sleeps until a new segment
2017d65f6f70SBen Gras * is selected.
2018d65f6f70SBen Gras */
2019d65f6f70SBen Gras mutex_enter(&lfs_lock);
2020d65f6f70SBen Gras error = lfs_wrapgo(fs, VTOI(ap->a_vp),
2021d65f6f70SBen Gras ap->a_command == LFCNWRAPGO_COMPAT ? 1 :
2022d65f6f70SBen Gras *((int *)ap->a_data));
2023d65f6f70SBen Gras mutex_exit(&lfs_lock);
2024d65f6f70SBen Gras return error;
2025d65f6f70SBen Gras
2026d65f6f70SBen Gras case LFCNWRAPPASS:
2027d65f6f70SBen Gras if ((VTOI(ap->a_vp)->i_lfs_iflags & LFSI_WRAPWAIT))
2028d65f6f70SBen Gras return EALREADY;
2029d65f6f70SBen Gras mutex_enter(&lfs_lock);
2030d65f6f70SBen Gras if (fs->lfs_stoplwp != curlwp) {
2031d65f6f70SBen Gras mutex_exit(&lfs_lock);
2032d65f6f70SBen Gras return EALREADY;
2033d65f6f70SBen Gras }
2034d65f6f70SBen Gras if (fs->lfs_nowrap == 0) {
2035d65f6f70SBen Gras mutex_exit(&lfs_lock);
2036d65f6f70SBen Gras return EBUSY;
2037d65f6f70SBen Gras }
2038d65f6f70SBen Gras fs->lfs_wrappass = 1;
2039d65f6f70SBen Gras wakeup(&fs->lfs_wrappass);
2040d65f6f70SBen Gras /* Wait for the log to wrap, if asked */
2041d65f6f70SBen Gras if (*(int *)ap->a_data) {
2042*0a6a1f1dSLionel Sambuc vref(ap->a_vp);
2043d65f6f70SBen Gras VTOI(ap->a_vp)->i_lfs_iflags |= LFSI_WRAPWAIT;
2044d65f6f70SBen Gras log(LOG_NOTICE, "LFCNPASS waiting for log wrap\n");
2045d65f6f70SBen Gras error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
2046d65f6f70SBen Gras "segwrap", 0, &lfs_lock);
2047d65f6f70SBen Gras log(LOG_NOTICE, "LFCNPASS done waiting\n");
2048d65f6f70SBen Gras VTOI(ap->a_vp)->i_lfs_iflags &= ~LFSI_WRAPWAIT;
2049*0a6a1f1dSLionel Sambuc vrele(ap->a_vp);
2050d65f6f70SBen Gras }
2051d65f6f70SBen Gras mutex_exit(&lfs_lock);
2052d65f6f70SBen Gras return error;
2053d65f6f70SBen Gras
2054d65f6f70SBen Gras case LFCNWRAPSTATUS:
2055d65f6f70SBen Gras mutex_enter(&lfs_lock);
2056d65f6f70SBen Gras *(int *)ap->a_data = fs->lfs_wrapstatus;
2057d65f6f70SBen Gras mutex_exit(&lfs_lock);
2058d65f6f70SBen Gras return 0;
2059d65f6f70SBen Gras
2060d65f6f70SBen Gras default:
206184d9c625SLionel Sambuc return ulfs_fcntl(v);
2062d65f6f70SBen Gras }
2063d65f6f70SBen Gras return 0;
2064d65f6f70SBen Gras }
2065d65f6f70SBen Gras
2066d65f6f70SBen Gras /*
2067d65f6f70SBen Gras * Return the last logical file offset that should be written for this file
2068d65f6f70SBen Gras * if we're doing a write that ends at "size". If writing, we need to know
2069d65f6f70SBen Gras * about sizes on disk, i.e. fragments if there are any; if reading, we need
2070d65f6f70SBen Gras * to know about entire blocks.
2071d65f6f70SBen Gras */
2072d65f6f70SBen Gras void
lfs_gop_size(struct vnode * vp,off_t size,off_t * eobp,int flags)2073d65f6f70SBen Gras lfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
2074d65f6f70SBen Gras {
2075d65f6f70SBen Gras struct inode *ip = VTOI(vp);
2076d65f6f70SBen Gras struct lfs *fs = ip->i_lfs;
2077d65f6f70SBen Gras daddr_t olbn, nlbn;
2078d65f6f70SBen Gras
207984d9c625SLionel Sambuc olbn = lfs_lblkno(fs, ip->i_size);
208084d9c625SLionel Sambuc nlbn = lfs_lblkno(fs, size);
208184d9c625SLionel Sambuc if (!(flags & GOP_SIZE_MEM) && nlbn < ULFS_NDADDR && olbn <= nlbn) {
208284d9c625SLionel Sambuc *eobp = lfs_fragroundup(fs, size);
2083d65f6f70SBen Gras } else {
208484d9c625SLionel Sambuc *eobp = lfs_blkroundup(fs, size);
2085d65f6f70SBen Gras }
2086d65f6f70SBen Gras }
2087d65f6f70SBen Gras
2088d65f6f70SBen Gras #ifdef DEBUG
2089d65f6f70SBen Gras void lfs_dump_vop(void *);
2090d65f6f70SBen Gras
2091d65f6f70SBen Gras void
lfs_dump_vop(void * v)2092d65f6f70SBen Gras lfs_dump_vop(void *v)
2093d65f6f70SBen Gras {
2094d65f6f70SBen Gras struct vop_putpages_args /* {
2095d65f6f70SBen Gras struct vnode *a_vp;
2096d65f6f70SBen Gras voff_t a_offlo;
2097d65f6f70SBen Gras voff_t a_offhi;
2098d65f6f70SBen Gras int a_flags;
2099d65f6f70SBen Gras } */ *ap = v;
2100d65f6f70SBen Gras
2101*0a6a1f1dSLionel Sambuc struct inode *ip = VTOI(ap->a_vp);
2102*0a6a1f1dSLionel Sambuc struct lfs *fs = ip->i_lfs;
2103*0a6a1f1dSLionel Sambuc
2104d65f6f70SBen Gras #ifdef DDB
2105d65f6f70SBen Gras vfs_vnode_print(ap->a_vp, 0, printf);
2106d65f6f70SBen Gras #endif
2107*0a6a1f1dSLionel Sambuc lfs_dump_dinode(fs, ip->i_din);
2108d65f6f70SBen Gras }
2109d65f6f70SBen Gras #endif
2110d65f6f70SBen Gras
2111d65f6f70SBen Gras int
lfs_mmap(void * v)2112d65f6f70SBen Gras lfs_mmap(void *v)
2113d65f6f70SBen Gras {
2114d65f6f70SBen Gras struct vop_mmap_args /* {
2115d65f6f70SBen Gras const struct vnodeop_desc *a_desc;
2116d65f6f70SBen Gras struct vnode *a_vp;
2117d65f6f70SBen Gras vm_prot_t a_prot;
2118d65f6f70SBen Gras kauth_cred_t a_cred;
2119d65f6f70SBen Gras } */ *ap = v;
2120d65f6f70SBen Gras
2121d65f6f70SBen Gras if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM)
2122d65f6f70SBen Gras return EOPNOTSUPP;
212384d9c625SLionel Sambuc return ulfs_mmap(v);
212484d9c625SLionel Sambuc }
212584d9c625SLionel Sambuc
212684d9c625SLionel Sambuc static int
lfs_openextattr(void * v)212784d9c625SLionel Sambuc lfs_openextattr(void *v)
212884d9c625SLionel Sambuc {
212984d9c625SLionel Sambuc struct vop_openextattr_args /* {
213084d9c625SLionel Sambuc struct vnode *a_vp;
213184d9c625SLionel Sambuc kauth_cred_t a_cred;
213284d9c625SLionel Sambuc struct proc *a_p;
213384d9c625SLionel Sambuc } */ *ap = v;
213484d9c625SLionel Sambuc struct inode *ip = VTOI(ap->a_vp);
213584d9c625SLionel Sambuc struct ulfsmount *ump = ip->i_ump;
213684d9c625SLionel Sambuc //struct lfs *fs = ip->i_lfs;
213784d9c625SLionel Sambuc
213884d9c625SLionel Sambuc /* Not supported for ULFS1 file systems. */
213984d9c625SLionel Sambuc if (ump->um_fstype == ULFS1)
214084d9c625SLionel Sambuc return (EOPNOTSUPP);
214184d9c625SLionel Sambuc
214284d9c625SLionel Sambuc /* XXX Not implemented for ULFS2 file systems. */
214384d9c625SLionel Sambuc return (EOPNOTSUPP);
214484d9c625SLionel Sambuc }
214584d9c625SLionel Sambuc
214684d9c625SLionel Sambuc static int
lfs_closeextattr(void * v)214784d9c625SLionel Sambuc lfs_closeextattr(void *v)
214884d9c625SLionel Sambuc {
214984d9c625SLionel Sambuc struct vop_closeextattr_args /* {
215084d9c625SLionel Sambuc struct vnode *a_vp;
215184d9c625SLionel Sambuc int a_commit;
215284d9c625SLionel Sambuc kauth_cred_t a_cred;
215384d9c625SLionel Sambuc struct proc *a_p;
215484d9c625SLionel Sambuc } */ *ap = v;
215584d9c625SLionel Sambuc struct inode *ip = VTOI(ap->a_vp);
215684d9c625SLionel Sambuc struct ulfsmount *ump = ip->i_ump;
215784d9c625SLionel Sambuc //struct lfs *fs = ip->i_lfs;
215884d9c625SLionel Sambuc
215984d9c625SLionel Sambuc /* Not supported for ULFS1 file systems. */
216084d9c625SLionel Sambuc if (ump->um_fstype == ULFS1)
216184d9c625SLionel Sambuc return (EOPNOTSUPP);
216284d9c625SLionel Sambuc
216384d9c625SLionel Sambuc /* XXX Not implemented for ULFS2 file systems. */
216484d9c625SLionel Sambuc return (EOPNOTSUPP);
216584d9c625SLionel Sambuc }
216684d9c625SLionel Sambuc
216784d9c625SLionel Sambuc static int
lfs_getextattr(void * v)216884d9c625SLionel Sambuc lfs_getextattr(void *v)
216984d9c625SLionel Sambuc {
217084d9c625SLionel Sambuc struct vop_getextattr_args /* {
217184d9c625SLionel Sambuc struct vnode *a_vp;
217284d9c625SLionel Sambuc int a_attrnamespace;
217384d9c625SLionel Sambuc const char *a_name;
217484d9c625SLionel Sambuc struct uio *a_uio;
217584d9c625SLionel Sambuc size_t *a_size;
217684d9c625SLionel Sambuc kauth_cred_t a_cred;
217784d9c625SLionel Sambuc struct proc *a_p;
217884d9c625SLionel Sambuc } */ *ap = v;
217984d9c625SLionel Sambuc struct vnode *vp = ap->a_vp;
218084d9c625SLionel Sambuc struct inode *ip = VTOI(vp);
218184d9c625SLionel Sambuc struct ulfsmount *ump = ip->i_ump;
218284d9c625SLionel Sambuc //struct lfs *fs = ip->i_lfs;
218384d9c625SLionel Sambuc int error;
218484d9c625SLionel Sambuc
218584d9c625SLionel Sambuc if (ump->um_fstype == ULFS1) {
218684d9c625SLionel Sambuc #ifdef LFS_EXTATTR
218784d9c625SLionel Sambuc fstrans_start(vp->v_mount, FSTRANS_SHARED);
218884d9c625SLionel Sambuc error = ulfs_getextattr(ap);
218984d9c625SLionel Sambuc fstrans_done(vp->v_mount);
219084d9c625SLionel Sambuc #else
219184d9c625SLionel Sambuc error = EOPNOTSUPP;
219284d9c625SLionel Sambuc #endif
219384d9c625SLionel Sambuc return error;
219484d9c625SLionel Sambuc }
219584d9c625SLionel Sambuc
219684d9c625SLionel Sambuc /* XXX Not implemented for ULFS2 file systems. */
219784d9c625SLionel Sambuc return (EOPNOTSUPP);
219884d9c625SLionel Sambuc }
219984d9c625SLionel Sambuc
220084d9c625SLionel Sambuc static int
lfs_setextattr(void * v)220184d9c625SLionel Sambuc lfs_setextattr(void *v)
220284d9c625SLionel Sambuc {
220384d9c625SLionel Sambuc struct vop_setextattr_args /* {
220484d9c625SLionel Sambuc struct vnode *a_vp;
220584d9c625SLionel Sambuc int a_attrnamespace;
220684d9c625SLionel Sambuc const char *a_name;
220784d9c625SLionel Sambuc struct uio *a_uio;
220884d9c625SLionel Sambuc kauth_cred_t a_cred;
220984d9c625SLionel Sambuc struct proc *a_p;
221084d9c625SLionel Sambuc } */ *ap = v;
221184d9c625SLionel Sambuc struct vnode *vp = ap->a_vp;
221284d9c625SLionel Sambuc struct inode *ip = VTOI(vp);
221384d9c625SLionel Sambuc struct ulfsmount *ump = ip->i_ump;
221484d9c625SLionel Sambuc //struct lfs *fs = ip->i_lfs;
221584d9c625SLionel Sambuc int error;
221684d9c625SLionel Sambuc
221784d9c625SLionel Sambuc if (ump->um_fstype == ULFS1) {
221884d9c625SLionel Sambuc #ifdef LFS_EXTATTR
221984d9c625SLionel Sambuc fstrans_start(vp->v_mount, FSTRANS_SHARED);
222084d9c625SLionel Sambuc error = ulfs_setextattr(ap);
222184d9c625SLionel Sambuc fstrans_done(vp->v_mount);
222284d9c625SLionel Sambuc #else
222384d9c625SLionel Sambuc error = EOPNOTSUPP;
222484d9c625SLionel Sambuc #endif
222584d9c625SLionel Sambuc return error;
222684d9c625SLionel Sambuc }
222784d9c625SLionel Sambuc
222884d9c625SLionel Sambuc /* XXX Not implemented for ULFS2 file systems. */
222984d9c625SLionel Sambuc return (EOPNOTSUPP);
223084d9c625SLionel Sambuc }
223184d9c625SLionel Sambuc
223284d9c625SLionel Sambuc static int
lfs_listextattr(void * v)223384d9c625SLionel Sambuc lfs_listextattr(void *v)
223484d9c625SLionel Sambuc {
223584d9c625SLionel Sambuc struct vop_listextattr_args /* {
223684d9c625SLionel Sambuc struct vnode *a_vp;
223784d9c625SLionel Sambuc int a_attrnamespace;
223884d9c625SLionel Sambuc struct uio *a_uio;
223984d9c625SLionel Sambuc size_t *a_size;
224084d9c625SLionel Sambuc kauth_cred_t a_cred;
224184d9c625SLionel Sambuc struct proc *a_p;
224284d9c625SLionel Sambuc } */ *ap = v;
224384d9c625SLionel Sambuc struct vnode *vp = ap->a_vp;
224484d9c625SLionel Sambuc struct inode *ip = VTOI(vp);
224584d9c625SLionel Sambuc struct ulfsmount *ump = ip->i_ump;
224684d9c625SLionel Sambuc //struct lfs *fs = ip->i_lfs;
224784d9c625SLionel Sambuc int error;
224884d9c625SLionel Sambuc
224984d9c625SLionel Sambuc if (ump->um_fstype == ULFS1) {
225084d9c625SLionel Sambuc #ifdef LFS_EXTATTR
225184d9c625SLionel Sambuc fstrans_start(vp->v_mount, FSTRANS_SHARED);
225284d9c625SLionel Sambuc error = ulfs_listextattr(ap);
225384d9c625SLionel Sambuc fstrans_done(vp->v_mount);
225484d9c625SLionel Sambuc #else
225584d9c625SLionel Sambuc error = EOPNOTSUPP;
225684d9c625SLionel Sambuc #endif
225784d9c625SLionel Sambuc return error;
225884d9c625SLionel Sambuc }
225984d9c625SLionel Sambuc
226084d9c625SLionel Sambuc /* XXX Not implemented for ULFS2 file systems. */
226184d9c625SLionel Sambuc return (EOPNOTSUPP);
226284d9c625SLionel Sambuc }
226384d9c625SLionel Sambuc
226484d9c625SLionel Sambuc static int
lfs_deleteextattr(void * v)226584d9c625SLionel Sambuc lfs_deleteextattr(void *v)
226684d9c625SLionel Sambuc {
226784d9c625SLionel Sambuc struct vop_deleteextattr_args /* {
226884d9c625SLionel Sambuc struct vnode *a_vp;
226984d9c625SLionel Sambuc int a_attrnamespace;
227084d9c625SLionel Sambuc kauth_cred_t a_cred;
227184d9c625SLionel Sambuc struct proc *a_p;
227284d9c625SLionel Sambuc } */ *ap = v;
227384d9c625SLionel Sambuc struct vnode *vp = ap->a_vp;
227484d9c625SLionel Sambuc struct inode *ip = VTOI(vp);
227584d9c625SLionel Sambuc struct ulfsmount *ump = ip->i_ump;
227684d9c625SLionel Sambuc //struct fs *fs = ip->i_lfs;
227784d9c625SLionel Sambuc int error;
227884d9c625SLionel Sambuc
227984d9c625SLionel Sambuc if (ump->um_fstype == ULFS1) {
228084d9c625SLionel Sambuc #ifdef LFS_EXTATTR
228184d9c625SLionel Sambuc fstrans_start(vp->v_mount, FSTRANS_SHARED);
228284d9c625SLionel Sambuc error = ulfs_deleteextattr(ap);
228384d9c625SLionel Sambuc fstrans_done(vp->v_mount);
228484d9c625SLionel Sambuc #else
228584d9c625SLionel Sambuc error = EOPNOTSUPP;
228684d9c625SLionel Sambuc #endif
228784d9c625SLionel Sambuc return error;
228884d9c625SLionel Sambuc }
228984d9c625SLionel Sambuc
229084d9c625SLionel Sambuc /* XXX Not implemented for ULFS2 file systems. */
229184d9c625SLionel Sambuc return (EOPNOTSUPP);
2292d65f6f70SBen Gras }
2293