Thu Oct 15 06:25:34 2015 UTC ()
Enable mounting lfs64 volumes.


(dholland)
diff -r1.347 -r1.348 src/sys/ufs/lfs/lfs_vfsops.c

cvs diff -r1.347 -r1.348 src/sys/ufs/lfs/lfs_vfsops.c (switch to unified diff)

--- src/sys/ufs/lfs/lfs_vfsops.c 2015/10/15 06:15:48 1.347
+++ src/sys/ufs/lfs/lfs_vfsops.c 2015/10/15 06:25:34 1.348
@@ -1,2256 +1,2325 @@ @@ -1,2256 +1,2325 @@
1/* $NetBSD: lfs_vfsops.c,v 1.347 2015/10/15 06:15:48 dholland Exp $ */ 1/* $NetBSD: lfs_vfsops.c,v 1.348 2015/10/15 06:25:34 dholland Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003, 2007, 2007 4 * Copyright (c) 1999, 2000, 2001, 2002, 2003, 2007, 2007
5 * The NetBSD Foundation, Inc. 5 * The NetBSD Foundation, Inc.
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
8 * This code is derived from software contributed to The NetBSD Foundation 8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Konrad E. Schroder <perseant@hhhh.org>. 9 * by Konrad E. Schroder <perseant@hhhh.org>.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer. 15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright 16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the 17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution. 18 * documentation and/or other materials provided with the distribution.
19 * 19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE. 30 * POSSIBILITY OF SUCH DAMAGE.
31 */ 31 */
32/*- 32/*-
33 * Copyright (c) 1989, 1991, 1993, 1994 33 * Copyright (c) 1989, 1991, 1993, 1994
34 * The Regents of the University of California. All rights reserved. 34 * The Regents of the University of California. All rights reserved.
35 * 35 *
36 * Redistribution and use in source and binary forms, with or without 36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions 37 * modification, are permitted provided that the following conditions
38 * are met: 38 * are met:
39 * 1. Redistributions of source code must retain the above copyright 39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer. 40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright 41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the 42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution. 43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors 44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software 45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission. 46 * without specific prior written permission.
47 * 47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE. 58 * SUCH DAMAGE.
59 * 59 *
60 * @(#)lfs_vfsops.c 8.20 (Berkeley) 6/10/95 60 * @(#)lfs_vfsops.c 8.20 (Berkeley) 6/10/95
61 */ 61 */
62 62
63#include <sys/cdefs.h> 63#include <sys/cdefs.h>
64__KERNEL_RCSID(0, "$NetBSD: lfs_vfsops.c,v 1.347 2015/10/15 06:15:48 dholland Exp $"); 64__KERNEL_RCSID(0, "$NetBSD: lfs_vfsops.c,v 1.348 2015/10/15 06:25:34 dholland Exp $");
65 65
66#if defined(_KERNEL_OPT) 66#if defined(_KERNEL_OPT)
67#include "opt_lfs.h" 67#include "opt_lfs.h"
68#include "opt_quota.h" 68#include "opt_quota.h"
69#endif 69#endif
70 70
71#include <sys/param.h> 71#include <sys/param.h>
72#include <sys/systm.h> 72#include <sys/systm.h>
73#include <sys/namei.h> 73#include <sys/namei.h>
74#include <sys/proc.h> 74#include <sys/proc.h>
75#include <sys/kernel.h> 75#include <sys/kernel.h>
76#include <sys/vnode.h> 76#include <sys/vnode.h>
77#include <sys/mount.h> 77#include <sys/mount.h>
78#include <sys/kthread.h> 78#include <sys/kthread.h>
79#include <sys/buf.h> 79#include <sys/buf.h>
80#include <sys/device.h> 80#include <sys/device.h>
81#include <sys/mbuf.h> 81#include <sys/mbuf.h>
82#include <sys/file.h> 82#include <sys/file.h>
83#include <sys/disklabel.h> 83#include <sys/disklabel.h>
84#include <sys/ioctl.h> 84#include <sys/ioctl.h>
85#include <sys/errno.h> 85#include <sys/errno.h>
86#include <sys/malloc.h> 86#include <sys/malloc.h>
87#include <sys/pool.h> 87#include <sys/pool.h>
88#include <sys/socket.h> 88#include <sys/socket.h>
89#include <sys/syslog.h> 89#include <sys/syslog.h>
90#include <uvm/uvm_extern.h> 90#include <uvm/uvm_extern.h>
91#include <sys/sysctl.h> 91#include <sys/sysctl.h>
92#include <sys/conf.h> 92#include <sys/conf.h>
93#include <sys/kauth.h> 93#include <sys/kauth.h>
94#include <sys/module.h> 94#include <sys/module.h>
95#include <sys/syscallvar.h> 95#include <sys/syscallvar.h>
96#include <sys/syscall.h> 96#include <sys/syscall.h>
97#include <sys/syscallargs.h> 97#include <sys/syscallargs.h>
98 98
99#include <miscfs/specfs/specdev.h> 99#include <miscfs/specfs/specdev.h>
100 100
101#include <ufs/lfs/ulfs_quotacommon.h> 101#include <ufs/lfs/ulfs_quotacommon.h>
102#include <ufs/lfs/ulfs_inode.h> 102#include <ufs/lfs/ulfs_inode.h>
103#include <ufs/lfs/ulfsmount.h> 103#include <ufs/lfs/ulfsmount.h>
104#include <ufs/lfs/ulfs_bswap.h> 104#include <ufs/lfs/ulfs_bswap.h>
105#include <ufs/lfs/ulfs_extern.h> 105#include <ufs/lfs/ulfs_extern.h>
106 106
107#include <uvm/uvm.h> 107#include <uvm/uvm.h>
108#include <uvm/uvm_stat.h> 108#include <uvm/uvm_stat.h>
109#include <uvm/uvm_pager.h> 109#include <uvm/uvm_pager.h>
110#include <uvm/uvm_pdaemon.h> 110#include <uvm/uvm_pdaemon.h>
111 111
112#include <ufs/lfs/lfs.h> 112#include <ufs/lfs/lfs.h>
113#include <ufs/lfs/lfs_accessors.h> 113#include <ufs/lfs/lfs_accessors.h>
114#include <ufs/lfs/lfs_kernel.h> 114#include <ufs/lfs/lfs_kernel.h>
115#include <ufs/lfs/lfs_extern.h> 115#include <ufs/lfs/lfs_extern.h>
116 116
117#include <miscfs/genfs/genfs.h> 117#include <miscfs/genfs/genfs.h>
118#include <miscfs/genfs/genfs_node.h> 118#include <miscfs/genfs/genfs_node.h>
119 119
120MODULE(MODULE_CLASS_VFS, lfs, NULL); 120MODULE(MODULE_CLASS_VFS, lfs, NULL);
121 121
122static int lfs_gop_write(struct vnode *, struct vm_page **, int, int); 122static int lfs_gop_write(struct vnode *, struct vm_page **, int, int);
123static int lfs_mountfs(struct vnode *, struct mount *, struct lwp *); 123static int lfs_mountfs(struct vnode *, struct mount *, struct lwp *);
124 124
125static struct sysctllog *lfs_sysctl_log; 125static struct sysctllog *lfs_sysctl_log;
126 126
127extern const struct vnodeopv_desc lfs_vnodeop_opv_desc; 127extern const struct vnodeopv_desc lfs_vnodeop_opv_desc;
128extern const struct vnodeopv_desc lfs_specop_opv_desc; 128extern const struct vnodeopv_desc lfs_specop_opv_desc;
129extern const struct vnodeopv_desc lfs_fifoop_opv_desc; 129extern const struct vnodeopv_desc lfs_fifoop_opv_desc;
130 130
131pid_t lfs_writer_daemon = 0; 131pid_t lfs_writer_daemon = 0;
132lwpid_t lfs_writer_lid = 0; 132lwpid_t lfs_writer_lid = 0;
133int lfs_do_flush = 0; 133int lfs_do_flush = 0;
134#ifdef LFS_KERNEL_RFW 134#ifdef LFS_KERNEL_RFW
135int lfs_do_rfw = 0; 135int lfs_do_rfw = 0;
136#endif 136#endif
137 137
138const struct vnodeopv_desc * const lfs_vnodeopv_descs[] = { 138const struct vnodeopv_desc * const lfs_vnodeopv_descs[] = {
139 &lfs_vnodeop_opv_desc, 139 &lfs_vnodeop_opv_desc,
140 &lfs_specop_opv_desc, 140 &lfs_specop_opv_desc,
141 &lfs_fifoop_opv_desc, 141 &lfs_fifoop_opv_desc,
142 NULL, 142 NULL,
143}; 143};
144 144
145struct vfsops lfs_vfsops = { 145struct vfsops lfs_vfsops = {
146 .vfs_name = MOUNT_LFS, 146 .vfs_name = MOUNT_LFS,
147 .vfs_min_mount_data = sizeof (struct ulfs_args), 147 .vfs_min_mount_data = sizeof (struct ulfs_args),
148 .vfs_mount = lfs_mount, 148 .vfs_mount = lfs_mount,
149 .vfs_start = ulfs_start, 149 .vfs_start = ulfs_start,
150 .vfs_unmount = lfs_unmount, 150 .vfs_unmount = lfs_unmount,
151 .vfs_root = ulfs_root, 151 .vfs_root = ulfs_root,
152 .vfs_quotactl = ulfs_quotactl, 152 .vfs_quotactl = ulfs_quotactl,
153 .vfs_statvfs = lfs_statvfs, 153 .vfs_statvfs = lfs_statvfs,
154 .vfs_sync = lfs_sync, 154 .vfs_sync = lfs_sync,
155 .vfs_vget = lfs_vget, 155 .vfs_vget = lfs_vget,
156 .vfs_loadvnode = lfs_loadvnode, 156 .vfs_loadvnode = lfs_loadvnode,
157 .vfs_newvnode = lfs_newvnode, 157 .vfs_newvnode = lfs_newvnode,
158 .vfs_fhtovp = lfs_fhtovp, 158 .vfs_fhtovp = lfs_fhtovp,
159 .vfs_vptofh = lfs_vptofh, 159 .vfs_vptofh = lfs_vptofh,
160 .vfs_init = lfs_init, 160 .vfs_init = lfs_init,
161 .vfs_reinit = lfs_reinit, 161 .vfs_reinit = lfs_reinit,
162 .vfs_done = lfs_done, 162 .vfs_done = lfs_done,
163 .vfs_mountroot = lfs_mountroot, 163 .vfs_mountroot = lfs_mountroot,
164 .vfs_snapshot = (void *)eopnotsupp, 164 .vfs_snapshot = (void *)eopnotsupp,
165 .vfs_extattrctl = lfs_extattrctl, 165 .vfs_extattrctl = lfs_extattrctl,
166 .vfs_suspendctl = (void *)eopnotsupp, 166 .vfs_suspendctl = (void *)eopnotsupp,
167 .vfs_renamelock_enter = genfs_renamelock_enter, 167 .vfs_renamelock_enter = genfs_renamelock_enter,
168 .vfs_renamelock_exit = genfs_renamelock_exit, 168 .vfs_renamelock_exit = genfs_renamelock_exit,
169 .vfs_fsync = (void *)eopnotsupp, 169 .vfs_fsync = (void *)eopnotsupp,
170 .vfs_opv_descs = lfs_vnodeopv_descs 170 .vfs_opv_descs = lfs_vnodeopv_descs
171}; 171};
172 172
173const struct genfs_ops lfs_genfsops = { 173const struct genfs_ops lfs_genfsops = {
174 .gop_size = lfs_gop_size, 174 .gop_size = lfs_gop_size,
175 .gop_alloc = ulfs_gop_alloc, 175 .gop_alloc = ulfs_gop_alloc,
176 .gop_write = lfs_gop_write, 176 .gop_write = lfs_gop_write,
177 .gop_markupdate = ulfs_gop_markupdate, 177 .gop_markupdate = ulfs_gop_markupdate,
178}; 178};
179 179
180struct shortlong { 180struct shortlong {
181 const char *sname; 181 const char *sname;
182 const char *lname; 182 const char *lname;
183}; 183};
184 184
185static int 185static int
186sysctl_lfs_dostats(SYSCTLFN_ARGS) 186sysctl_lfs_dostats(SYSCTLFN_ARGS)
187{ 187{
188 extern struct lfs_stats lfs_stats; 188 extern struct lfs_stats lfs_stats;
189 extern int lfs_dostats; 189 extern int lfs_dostats;
190 int error; 190 int error;
191 191
192 error = sysctl_lookup(SYSCTLFN_CALL(rnode)); 192 error = sysctl_lookup(SYSCTLFN_CALL(rnode));
193 if (error || newp == NULL) 193 if (error || newp == NULL)
194 return (error); 194 return (error);
195 195
196 if (lfs_dostats == 0) 196 if (lfs_dostats == 0)
197 memset(&lfs_stats, 0, sizeof(lfs_stats)); 197 memset(&lfs_stats, 0, sizeof(lfs_stats));
198 198
199 return (0); 199 return (0);
200} 200}
201 201
202static void 202static void
203lfs_sysctl_setup(struct sysctllog **clog) 203lfs_sysctl_setup(struct sysctllog **clog)
204{ 204{
205 int i; 205 int i;
206 extern int lfs_writeindir, lfs_dostats, lfs_clean_vnhead, 206 extern int lfs_writeindir, lfs_dostats, lfs_clean_vnhead,
207 lfs_fs_pagetrip, lfs_ignore_lazy_sync; 207 lfs_fs_pagetrip, lfs_ignore_lazy_sync;
208#ifdef DEBUG 208#ifdef DEBUG
209 extern int lfs_debug_log_subsys[DLOG_MAX]; 209 extern int lfs_debug_log_subsys[DLOG_MAX];
210 struct shortlong dlog_names[DLOG_MAX] = { /* Must match lfs.h ! */ 210 struct shortlong dlog_names[DLOG_MAX] = { /* Must match lfs.h ! */
211 { "rollforward", "Debug roll-forward code" }, 211 { "rollforward", "Debug roll-forward code" },
212 { "alloc", "Debug inode allocation and free list" }, 212 { "alloc", "Debug inode allocation and free list" },
213 { "avail", "Debug space-available-now accounting" }, 213 { "avail", "Debug space-available-now accounting" },
214 { "flush", "Debug flush triggers" }, 214 { "flush", "Debug flush triggers" },
215 { "lockedlist", "Debug locked list accounting" }, 215 { "lockedlist", "Debug locked list accounting" },
216 { "vnode_verbose", "Verbose per-vnode-written debugging" }, 216 { "vnode_verbose", "Verbose per-vnode-written debugging" },
217 { "vnode", "Debug vnode use during segment write" }, 217 { "vnode", "Debug vnode use during segment write" },
218 { "segment", "Debug segment writing" }, 218 { "segment", "Debug segment writing" },
219 { "seguse", "Debug segment used-bytes accounting" }, 219 { "seguse", "Debug segment used-bytes accounting" },
220 { "cleaner", "Debug cleaning routines" }, 220 { "cleaner", "Debug cleaning routines" },
221 { "mount", "Debug mount/unmount routines" }, 221 { "mount", "Debug mount/unmount routines" },
222 { "pagecache", "Debug UBC interactions" }, 222 { "pagecache", "Debug UBC interactions" },
223 { "dirop", "Debug directory-operation accounting" }, 223 { "dirop", "Debug directory-operation accounting" },
224 { "malloc", "Debug private malloc accounting" }, 224 { "malloc", "Debug private malloc accounting" },
225 }; 225 };
226#endif /* DEBUG */ 226#endif /* DEBUG */
227 struct shortlong stat_names[] = { /* Must match lfs.h! */ 227 struct shortlong stat_names[] = { /* Must match lfs.h! */
228 { "segsused", "Number of new segments allocated" }, 228 { "segsused", "Number of new segments allocated" },
229 { "psegwrites", "Number of partial-segment writes" }, 229 { "psegwrites", "Number of partial-segment writes" },
230 { "psyncwrites", "Number of synchronous partial-segment" 230 { "psyncwrites", "Number of synchronous partial-segment"
231 " writes" }, 231 " writes" },
232 { "pcleanwrites", "Number of partial-segment writes by the" 232 { "pcleanwrites", "Number of partial-segment writes by the"
233 " cleaner" }, 233 " cleaner" },
234 { "blocktot", "Number of blocks written" }, 234 { "blocktot", "Number of blocks written" },
235 { "cleanblocks", "Number of blocks written by the cleaner" }, 235 { "cleanblocks", "Number of blocks written by the cleaner" },
236 { "ncheckpoints", "Number of checkpoints made" }, 236 { "ncheckpoints", "Number of checkpoints made" },
237 { "nwrites", "Number of whole writes" }, 237 { "nwrites", "Number of whole writes" },
238 { "nsync_writes", "Number of synchronous writes" }, 238 { "nsync_writes", "Number of synchronous writes" },
239 { "wait_exceeded", "Number of times writer waited for" 239 { "wait_exceeded", "Number of times writer waited for"
240 " cleaner" }, 240 " cleaner" },
241 { "write_exceeded", "Number of times writer invoked flush" }, 241 { "write_exceeded", "Number of times writer invoked flush" },
242 { "flush_invoked", "Number of times flush was invoked" }, 242 { "flush_invoked", "Number of times flush was invoked" },
243 { "vflush_invoked", "Number of time vflush was called" }, 243 { "vflush_invoked", "Number of time vflush was called" },
244 { "clean_inlocked", "Number of vnodes skipped for being dead" }, 244 { "clean_inlocked", "Number of vnodes skipped for being dead" },
245 { "clean_vnlocked", "Number of vnodes skipped for vget failure" }, 245 { "clean_vnlocked", "Number of vnodes skipped for vget failure" },
246 { "segs_reclaimed", "Number of segments reclaimed" }, 246 { "segs_reclaimed", "Number of segments reclaimed" },
247 }; 247 };
248 248
249 sysctl_createv(clog, 0, NULL, NULL, 249 sysctl_createv(clog, 0, NULL, NULL,
250 CTLFLAG_PERMANENT, 250 CTLFLAG_PERMANENT,
251 CTLTYPE_NODE, "lfs", 251 CTLTYPE_NODE, "lfs",
252 SYSCTL_DESCR("Log-structured file system"), 252 SYSCTL_DESCR("Log-structured file system"),
253 NULL, 0, NULL, 0, 253 NULL, 0, NULL, 0,
254 CTL_VFS, 5, CTL_EOL); 254 CTL_VFS, 5, CTL_EOL);
255 /* 255 /*
256 * XXX the "5" above could be dynamic, thereby eliminating one 256 * XXX the "5" above could be dynamic, thereby eliminating one
257 * more instance of the "number to vfs" mapping problem, but 257 * more instance of the "number to vfs" mapping problem, but
258 * "5" is the order as taken from sys/mount.h 258 * "5" is the order as taken from sys/mount.h
259 */ 259 */
260 260
261 sysctl_createv(clog, 0, NULL, NULL, 261 sysctl_createv(clog, 0, NULL, NULL,
262 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 262 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
263 CTLTYPE_INT, "flushindir", NULL, 263 CTLTYPE_INT, "flushindir", NULL,
264 NULL, 0, &lfs_writeindir, 0, 264 NULL, 0, &lfs_writeindir, 0,
265 CTL_VFS, 5, LFS_WRITEINDIR, CTL_EOL); 265 CTL_VFS, 5, LFS_WRITEINDIR, CTL_EOL);
266 sysctl_createv(clog, 0, NULL, NULL, 266 sysctl_createv(clog, 0, NULL, NULL,
267 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 267 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
268 CTLTYPE_INT, "clean_vnhead", NULL, 268 CTLTYPE_INT, "clean_vnhead", NULL,
269 NULL, 0, &lfs_clean_vnhead, 0, 269 NULL, 0, &lfs_clean_vnhead, 0,
270 CTL_VFS, 5, LFS_CLEAN_VNHEAD, CTL_EOL); 270 CTL_VFS, 5, LFS_CLEAN_VNHEAD, CTL_EOL);
271 sysctl_createv(clog, 0, NULL, NULL, 271 sysctl_createv(clog, 0, NULL, NULL,
272 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 272 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
273 CTLTYPE_INT, "dostats", 273 CTLTYPE_INT, "dostats",
274 SYSCTL_DESCR("Maintain statistics on LFS operations"), 274 SYSCTL_DESCR("Maintain statistics on LFS operations"),
275 sysctl_lfs_dostats, 0, &lfs_dostats, 0, 275 sysctl_lfs_dostats, 0, &lfs_dostats, 0,
276 CTL_VFS, 5, LFS_DOSTATS, CTL_EOL); 276 CTL_VFS, 5, LFS_DOSTATS, CTL_EOL);
277 sysctl_createv(clog, 0, NULL, NULL, 277 sysctl_createv(clog, 0, NULL, NULL,
278 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 278 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
279 CTLTYPE_INT, "pagetrip", 279 CTLTYPE_INT, "pagetrip",
280 SYSCTL_DESCR("How many dirty pages in fs triggers" 280 SYSCTL_DESCR("How many dirty pages in fs triggers"
281 " a flush"), 281 " a flush"),
282 NULL, 0, &lfs_fs_pagetrip, 0, 282 NULL, 0, &lfs_fs_pagetrip, 0,
283 CTL_VFS, 5, LFS_FS_PAGETRIP, CTL_EOL); 283 CTL_VFS, 5, LFS_FS_PAGETRIP, CTL_EOL);
284 sysctl_createv(clog, 0, NULL, NULL, 284 sysctl_createv(clog, 0, NULL, NULL,
285 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 285 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
286 CTLTYPE_INT, "ignore_lazy_sync", 286 CTLTYPE_INT, "ignore_lazy_sync",
287 SYSCTL_DESCR("Lazy Sync is ignored entirely"), 287 SYSCTL_DESCR("Lazy Sync is ignored entirely"),
288 NULL, 0, &lfs_ignore_lazy_sync, 0, 288 NULL, 0, &lfs_ignore_lazy_sync, 0,
289 CTL_VFS, 5, LFS_IGNORE_LAZY_SYNC, CTL_EOL); 289 CTL_VFS, 5, LFS_IGNORE_LAZY_SYNC, CTL_EOL);
290#ifdef LFS_KERNEL_RFW 290#ifdef LFS_KERNEL_RFW
291 sysctl_createv(clog, 0, NULL, NULL, 291 sysctl_createv(clog, 0, NULL, NULL,
292 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 292 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
293 CTLTYPE_INT, "rfw", 293 CTLTYPE_INT, "rfw",
294 SYSCTL_DESCR("Use in-kernel roll-forward on mount"), 294 SYSCTL_DESCR("Use in-kernel roll-forward on mount"),
295 NULL, 0, &lfs_do_rfw, 0, 295 NULL, 0, &lfs_do_rfw, 0,
296 CTL_VFS, 5, LFS_DO_RFW, CTL_EOL); 296 CTL_VFS, 5, LFS_DO_RFW, CTL_EOL);
297#endif 297#endif
298 298
299 sysctl_createv(clog, 0, NULL, NULL, 299 sysctl_createv(clog, 0, NULL, NULL,
300 CTLFLAG_PERMANENT, 300 CTLFLAG_PERMANENT,
301 CTLTYPE_NODE, "stats", 301 CTLTYPE_NODE, "stats",
302 SYSCTL_DESCR("Debugging options"), 302 SYSCTL_DESCR("Debugging options"),
303 NULL, 0, NULL, 0, 303 NULL, 0, NULL, 0,
304 CTL_VFS, 5, LFS_STATS, CTL_EOL); 304 CTL_VFS, 5, LFS_STATS, CTL_EOL);
305 for (i = 0; i < sizeof(struct lfs_stats) / sizeof(u_int); i++) { 305 for (i = 0; i < sizeof(struct lfs_stats) / sizeof(u_int); i++) {
306 sysctl_createv(clog, 0, NULL, NULL, 306 sysctl_createv(clog, 0, NULL, NULL,
307 CTLFLAG_PERMANENT|CTLFLAG_READONLY, 307 CTLFLAG_PERMANENT|CTLFLAG_READONLY,
308 CTLTYPE_INT, stat_names[i].sname, 308 CTLTYPE_INT, stat_names[i].sname,
309 SYSCTL_DESCR(stat_names[i].lname), 309 SYSCTL_DESCR(stat_names[i].lname),
310 NULL, 0, &(((u_int *)&lfs_stats.segsused)[i]), 310 NULL, 0, &(((u_int *)&lfs_stats.segsused)[i]),
311 0, CTL_VFS, 5, LFS_STATS, i, CTL_EOL); 311 0, CTL_VFS, 5, LFS_STATS, i, CTL_EOL);
312 } 312 }
313 313
314#ifdef DEBUG 314#ifdef DEBUG
315 sysctl_createv(clog, 0, NULL, NULL, 315 sysctl_createv(clog, 0, NULL, NULL,
316 CTLFLAG_PERMANENT, 316 CTLFLAG_PERMANENT,
317 CTLTYPE_NODE, "debug", 317 CTLTYPE_NODE, "debug",
318 SYSCTL_DESCR("Debugging options"), 318 SYSCTL_DESCR("Debugging options"),
319 NULL, 0, NULL, 0, 319 NULL, 0, NULL, 0,
320 CTL_VFS, 5, LFS_DEBUGLOG, CTL_EOL); 320 CTL_VFS, 5, LFS_DEBUGLOG, CTL_EOL);
321 for (i = 0; i < DLOG_MAX; i++) { 321 for (i = 0; i < DLOG_MAX; i++) {
322 sysctl_createv(clog, 0, NULL, NULL, 322 sysctl_createv(clog, 0, NULL, NULL,
323 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 323 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
324 CTLTYPE_INT, dlog_names[i].sname, 324 CTLTYPE_INT, dlog_names[i].sname,
325 SYSCTL_DESCR(dlog_names[i].lname), 325 SYSCTL_DESCR(dlog_names[i].lname),
326 NULL, 0, &(lfs_debug_log_subsys[i]), 0, 326 NULL, 0, &(lfs_debug_log_subsys[i]), 0,
327 CTL_VFS, 5, LFS_DEBUGLOG, i, CTL_EOL); 327 CTL_VFS, 5, LFS_DEBUGLOG, i, CTL_EOL);
328 } 328 }
329#endif 329#endif
330} 330}
331 331
332/* old cleaner syscall interface. see VOP_FCNTL() */ 332/* old cleaner syscall interface. see VOP_FCNTL() */
333static const struct syscall_package lfs_syscalls[] = { 333static const struct syscall_package lfs_syscalls[] = {
334 { SYS_lfs_bmapv, 0, (sy_call_t *)sys_lfs_bmapv }, 334 { SYS_lfs_bmapv, 0, (sy_call_t *)sys_lfs_bmapv },
335 { SYS_lfs_markv, 0, (sy_call_t *)sys_lfs_markv }, 335 { SYS_lfs_markv, 0, (sy_call_t *)sys_lfs_markv },
336 { SYS___lfs_segwait50, 0, (sy_call_t *)sys___lfs_segwait50 }, 336 { SYS___lfs_segwait50, 0, (sy_call_t *)sys___lfs_segwait50 },
337 { SYS_lfs_segclean, 0, (sy_call_t *)sys_lfs_segclean }, 337 { SYS_lfs_segclean, 0, (sy_call_t *)sys_lfs_segclean },
338 { 0, 0, NULL }, 338 { 0, 0, NULL },
339}; 339};
340 340
341static int 341static int
342lfs_modcmd(modcmd_t cmd, void *arg) 342lfs_modcmd(modcmd_t cmd, void *arg)
343{ 343{
344 int error; 344 int error;
345 345
346 switch (cmd) { 346 switch (cmd) {
347 case MODULE_CMD_INIT: 347 case MODULE_CMD_INIT:
348 error = syscall_establish(NULL, lfs_syscalls); 348 error = syscall_establish(NULL, lfs_syscalls);
349 if (error) 349 if (error)
350 return error; 350 return error;
351 error = vfs_attach(&lfs_vfsops); 351 error = vfs_attach(&lfs_vfsops);
352 if (error != 0) { 352 if (error != 0) {
353 syscall_disestablish(NULL, lfs_syscalls); 353 syscall_disestablish(NULL, lfs_syscalls);
354 break; 354 break;
355 } 355 }
356 lfs_sysctl_setup(&lfs_sysctl_log); 356 lfs_sysctl_setup(&lfs_sysctl_log);
357 break; 357 break;
358 case MODULE_CMD_FINI: 358 case MODULE_CMD_FINI:
359 error = vfs_detach(&lfs_vfsops); 359 error = vfs_detach(&lfs_vfsops);
360 if (error != 0) 360 if (error != 0)
361 break; 361 break;
362 syscall_disestablish(NULL, lfs_syscalls); 362 syscall_disestablish(NULL, lfs_syscalls);
363 sysctl_teardown(&lfs_sysctl_log); 363 sysctl_teardown(&lfs_sysctl_log);
364 break; 364 break;
365 default: 365 default:
366 error = ENOTTY; 366 error = ENOTTY;
367 break; 367 break;
368 } 368 }
369 369
370 return (error); 370 return (error);
371} 371}
372 372
373/* 373/*
374 * XXX Same structure as FFS inodes? Should we share a common pool? 374 * XXX Same structure as FFS inodes? Should we share a common pool?
375 */ 375 */
376struct pool lfs_inode_pool; 376struct pool lfs_inode_pool;
377struct pool lfs_dinode_pool; 377struct pool lfs_dinode_pool;
378struct pool lfs_inoext_pool; 378struct pool lfs_inoext_pool;
379struct pool lfs_lbnentry_pool; 379struct pool lfs_lbnentry_pool;
380 380
381/* 381/*
382 * The writer daemon. UVM keeps track of how many dirty pages we are holding 382 * The writer daemon. UVM keeps track of how many dirty pages we are holding
383 * in lfs_subsys_pages; the daemon flushes the filesystem when this value 383 * in lfs_subsys_pages; the daemon flushes the filesystem when this value
384 * crosses the (user-defined) threshhold LFS_MAX_PAGES. 384 * crosses the (user-defined) threshhold LFS_MAX_PAGES.
385 */ 385 */
386static void 386static void
387lfs_writerd(void *arg) 387lfs_writerd(void *arg)
388{ 388{
389 struct mount *mp, *nmp; 389 struct mount *mp, *nmp;
390 struct lfs *fs; 390 struct lfs *fs;
391 struct vfsops *vfs = NULL; 391 struct vfsops *vfs = NULL;
392 int fsflags; 392 int fsflags;
393 int skipc; 393 int skipc;
394 int lfsc; 394 int lfsc;
395 int wrote_something = 0; 395 int wrote_something = 0;
396  396
397 mutex_enter(&lfs_lock); 397 mutex_enter(&lfs_lock);
398 lfs_writer_daemon = curproc->p_pid; 398 lfs_writer_daemon = curproc->p_pid;
399 lfs_writer_lid = curlwp->l_lid; 399 lfs_writer_lid = curlwp->l_lid;
400 mutex_exit(&lfs_lock); 400 mutex_exit(&lfs_lock);
401 401
402 /* Take an extra reference to the LFS vfsops. */ 402 /* Take an extra reference to the LFS vfsops. */
403 vfs = vfs_getopsbyname(MOUNT_LFS); 403 vfs = vfs_getopsbyname(MOUNT_LFS);
404  404
405 mutex_enter(&lfs_lock); 405 mutex_enter(&lfs_lock);
406 for (;;) { 406 for (;;) {
407 KASSERT(mutex_owned(&lfs_lock)); 407 KASSERT(mutex_owned(&lfs_lock));
408 if (wrote_something == 0) 408 if (wrote_something == 0)
409 mtsleep(&lfs_writer_daemon, PVM, "lfswriter", hz/10 + 1, 409 mtsleep(&lfs_writer_daemon, PVM, "lfswriter", hz/10 + 1,
410 &lfs_lock); 410 &lfs_lock);
411 411
412 KASSERT(mutex_owned(&lfs_lock)); 412 KASSERT(mutex_owned(&lfs_lock));
413 wrote_something = 0; 413 wrote_something = 0;
414 414
415 /* 415 /*
416 * If global state wants a flush, flush everything. 416 * If global state wants a flush, flush everything.
417 */ 417 */
418 if (lfs_do_flush || locked_queue_count > LFS_MAX_BUFS || 418 if (lfs_do_flush || locked_queue_count > LFS_MAX_BUFS ||
419 locked_queue_bytes > LFS_MAX_BYTES || 419 locked_queue_bytes > LFS_MAX_BYTES ||
420 lfs_subsys_pages > LFS_MAX_PAGES) { 420 lfs_subsys_pages > LFS_MAX_PAGES) {
421 421
422 if (lfs_do_flush) { 422 if (lfs_do_flush) {
423 DLOG((DLOG_FLUSH, "lfs_writerd: lfs_do_flush\n")); 423 DLOG((DLOG_FLUSH, "lfs_writerd: lfs_do_flush\n"));
424 } 424 }
425 if (locked_queue_count > LFS_MAX_BUFS) { 425 if (locked_queue_count > LFS_MAX_BUFS) {
426 DLOG((DLOG_FLUSH, "lfs_writerd: lqc = %d, max %d\n", 426 DLOG((DLOG_FLUSH, "lfs_writerd: lqc = %d, max %d\n",
427 locked_queue_count, LFS_MAX_BUFS)); 427 locked_queue_count, LFS_MAX_BUFS));
428 } 428 }
429 if (locked_queue_bytes > LFS_MAX_BYTES) { 429 if (locked_queue_bytes > LFS_MAX_BYTES) {
430 DLOG((DLOG_FLUSH, "lfs_writerd: lqb = %ld, max %ld\n", 430 DLOG((DLOG_FLUSH, "lfs_writerd: lqb = %ld, max %ld\n",
431 locked_queue_bytes, LFS_MAX_BYTES)); 431 locked_queue_bytes, LFS_MAX_BYTES));
432 } 432 }
433 if (lfs_subsys_pages > LFS_MAX_PAGES) { 433 if (lfs_subsys_pages > LFS_MAX_PAGES) {
434 DLOG((DLOG_FLUSH, "lfs_writerd: lssp = %d, max %d\n", 434 DLOG((DLOG_FLUSH, "lfs_writerd: lssp = %d, max %d\n",
435 lfs_subsys_pages, LFS_MAX_PAGES)); 435 lfs_subsys_pages, LFS_MAX_PAGES));
436 } 436 }
437 437
438 lfs_flush(NULL, SEGM_WRITERD, 0); 438 lfs_flush(NULL, SEGM_WRITERD, 0);
439 lfs_do_flush = 0; 439 lfs_do_flush = 0;
440 KASSERT(mutex_owned(&lfs_lock)); 440 KASSERT(mutex_owned(&lfs_lock));
441 continue; 441 continue;
442 } 442 }
443 KASSERT(mutex_owned(&lfs_lock)); 443 KASSERT(mutex_owned(&lfs_lock));
444 mutex_exit(&lfs_lock); 444 mutex_exit(&lfs_lock);
445  445
446 /* 446 /*
447 * Look through the list of LFSs to see if any of them 447 * Look through the list of LFSs to see if any of them
448 * have requested pageouts. 448 * have requested pageouts.
449 */ 449 */
450 mutex_enter(&mountlist_lock); 450 mutex_enter(&mountlist_lock);
451 lfsc = 0; 451 lfsc = 0;
452 skipc = 0; 452 skipc = 0;
453 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 453 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
454 if (vfs_busy(mp, &nmp)) { 454 if (vfs_busy(mp, &nmp)) {
455 ++skipc; 455 ++skipc;
456 continue; 456 continue;
457 } 457 }
458 KASSERT(!mutex_owned(&lfs_lock)); 458 KASSERT(!mutex_owned(&lfs_lock));
459 if (strncmp(mp->mnt_stat.f_fstypename, MOUNT_LFS, 459 if (strncmp(mp->mnt_stat.f_fstypename, MOUNT_LFS,
460 sizeof(mp->mnt_stat.f_fstypename)) == 0) { 460 sizeof(mp->mnt_stat.f_fstypename)) == 0) {
461 ++lfsc; 461 ++lfsc;
462 fs = VFSTOULFS(mp)->um_lfs; 462 fs = VFSTOULFS(mp)->um_lfs;
463 daddr_t ooffset = 0; 463 daddr_t ooffset = 0;
464 fsflags = SEGM_SINGLE; 464 fsflags = SEGM_SINGLE;
465 465
466 mutex_enter(&lfs_lock); 466 mutex_enter(&lfs_lock);
467 ooffset = lfs_sb_getoffset(fs); 467 ooffset = lfs_sb_getoffset(fs);
468 468
469 if (lfs_sb_getnextseg(fs) < lfs_sb_getcurseg(fs) && fs->lfs_nowrap) { 469 if (lfs_sb_getnextseg(fs) < lfs_sb_getcurseg(fs) && fs->lfs_nowrap) {
470 /* Don't try to write if we're suspended */ 470 /* Don't try to write if we're suspended */
471 mutex_exit(&lfs_lock); 471 mutex_exit(&lfs_lock);
472 vfs_unbusy(mp, false, &nmp); 472 vfs_unbusy(mp, false, &nmp);
473 continue; 473 continue;
474 } 474 }
475 if (LFS_STARVED_FOR_SEGS(fs)) { 475 if (LFS_STARVED_FOR_SEGS(fs)) {
476 mutex_exit(&lfs_lock); 476 mutex_exit(&lfs_lock);
477 477
478 DLOG((DLOG_FLUSH, "lfs_writerd: need cleaning before writing possible\n")); 478 DLOG((DLOG_FLUSH, "lfs_writerd: need cleaning before writing possible\n"));
479 lfs_wakeup_cleaner(fs); 479 lfs_wakeup_cleaner(fs);
480 vfs_unbusy(mp, false, &nmp); 480 vfs_unbusy(mp, false, &nmp);
481 continue; 481 continue;
482 } 482 }
483 483
484 if ((fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) || 484 if ((fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
485 lfs_dirvcount > LFS_MAX_DIROP) && 485 lfs_dirvcount > LFS_MAX_DIROP) &&
486 fs->lfs_dirops == 0) { 486 fs->lfs_dirops == 0) {
487 fsflags &= ~SEGM_SINGLE; 487 fsflags &= ~SEGM_SINGLE;
488 fsflags |= SEGM_CKP; 488 fsflags |= SEGM_CKP;
489 DLOG((DLOG_FLUSH, "lfs_writerd: checkpoint\n")); 489 DLOG((DLOG_FLUSH, "lfs_writerd: checkpoint\n"));
490 lfs_flush_fs(fs, fsflags); 490 lfs_flush_fs(fs, fsflags);
491 } else if (fs->lfs_pdflush) { 491 } else if (fs->lfs_pdflush) {
492 DLOG((DLOG_FLUSH, "lfs_writerd: pdflush set\n")); 492 DLOG((DLOG_FLUSH, "lfs_writerd: pdflush set\n"));
493 lfs_flush_fs(fs, fsflags); 493 lfs_flush_fs(fs, fsflags);
494 } else if (!TAILQ_EMPTY(&fs->lfs_pchainhd)) { 494 } else if (!TAILQ_EMPTY(&fs->lfs_pchainhd)) {
495 DLOG((DLOG_FLUSH, "lfs_writerd: pchain non-empty\n")); 495 DLOG((DLOG_FLUSH, "lfs_writerd: pchain non-empty\n"));
496 mutex_exit(&lfs_lock); 496 mutex_exit(&lfs_lock);
497 lfs_writer_enter(fs, "wrdirop"); 497 lfs_writer_enter(fs, "wrdirop");
498 lfs_flush_pchain(fs); 498 lfs_flush_pchain(fs);
499 lfs_writer_leave(fs); 499 lfs_writer_leave(fs);
500 mutex_enter(&lfs_lock); 500 mutex_enter(&lfs_lock);
501 } 501 }
502 if (lfs_sb_getoffset(fs) != ooffset) 502 if (lfs_sb_getoffset(fs) != ooffset)
503 ++wrote_something; 503 ++wrote_something;
504 mutex_exit(&lfs_lock); 504 mutex_exit(&lfs_lock);
505 } 505 }
506 KASSERT(!mutex_owned(&lfs_lock)); 506 KASSERT(!mutex_owned(&lfs_lock));
507 vfs_unbusy(mp, false, &nmp); 507 vfs_unbusy(mp, false, &nmp);
508 } 508 }
509 if (lfsc + skipc == 0) { 509 if (lfsc + skipc == 0) {
510 mutex_enter(&lfs_lock); 510 mutex_enter(&lfs_lock);
511 lfs_writer_daemon = 0; 511 lfs_writer_daemon = 0;
512 lfs_writer_lid = 0; 512 lfs_writer_lid = 0;
513 mutex_exit(&lfs_lock); 513 mutex_exit(&lfs_lock);
514 mutex_exit(&mountlist_lock); 514 mutex_exit(&mountlist_lock);
515 break; 515 break;
516 } 516 }
517 mutex_exit(&mountlist_lock); 517 mutex_exit(&mountlist_lock);
518  518
519 mutex_enter(&lfs_lock); 519 mutex_enter(&lfs_lock);
520 } 520 }
521 KASSERT(!mutex_owned(&lfs_lock)); 521 KASSERT(!mutex_owned(&lfs_lock));
522 KASSERT(!mutex_owned(&mountlist_lock)); 522 KASSERT(!mutex_owned(&mountlist_lock));
523 523
524 /* Give up our extra reference so the module can be unloaded. */ 524 /* Give up our extra reference so the module can be unloaded. */
525 mutex_enter(&vfs_list_lock); 525 mutex_enter(&vfs_list_lock);
526 if (vfs != NULL) 526 if (vfs != NULL)
527 vfs->vfs_refcount--; 527 vfs->vfs_refcount--;
528 mutex_exit(&vfs_list_lock); 528 mutex_exit(&vfs_list_lock);
529 529
530 /* Done! */ 530 /* Done! */
531 kthread_exit(0); 531 kthread_exit(0);
532} 532}
533 533
534/* 534/*
535 * Initialize the filesystem, most work done by ulfs_init. 535 * Initialize the filesystem, most work done by ulfs_init.
536 */ 536 */
537void 537void
538lfs_init(void) 538lfs_init(void)
539{ 539{
540 540
541 /* 541 /*
542 * XXX: should we use separate pools for 32-bit and 64-bit 542 * XXX: should we use separate pools for 32-bit and 64-bit
543 * dinodes? 543 * dinodes?
544 */ 544 */
545 malloc_type_attach(M_SEGMENT); 545 malloc_type_attach(M_SEGMENT);
546 pool_init(&lfs_inode_pool, sizeof(struct inode), 0, 0, 0, 546 pool_init(&lfs_inode_pool, sizeof(struct inode), 0, 0, 0,
547 "lfsinopl", &pool_allocator_nointr, IPL_NONE); 547 "lfsinopl", &pool_allocator_nointr, IPL_NONE);
548 pool_init(&lfs_dinode_pool, sizeof(union lfs_dinode), 0, 0, 0, 548 pool_init(&lfs_dinode_pool, sizeof(union lfs_dinode), 0, 0, 0,
549 "lfsdinopl", &pool_allocator_nointr, IPL_NONE); 549 "lfsdinopl", &pool_allocator_nointr, IPL_NONE);
550 pool_init(&lfs_inoext_pool, sizeof(struct lfs_inode_ext), 8, 0, 0, 550 pool_init(&lfs_inoext_pool, sizeof(struct lfs_inode_ext), 8, 0, 0,
551 "lfsinoextpl", &pool_allocator_nointr, IPL_NONE); 551 "lfsinoextpl", &pool_allocator_nointr, IPL_NONE);
552 pool_init(&lfs_lbnentry_pool, sizeof(struct lbnentry), 0, 0, 0, 552 pool_init(&lfs_lbnentry_pool, sizeof(struct lbnentry), 0, 0, 0,
553 "lfslbnpool", &pool_allocator_nointr, IPL_NONE); 553 "lfslbnpool", &pool_allocator_nointr, IPL_NONE);
554 ulfs_init(); 554 ulfs_init();
555 555
556#ifdef DEBUG 556#ifdef DEBUG
557 memset(lfs_log, 0, sizeof(lfs_log)); 557 memset(lfs_log, 0, sizeof(lfs_log));
558#endif 558#endif
559 mutex_init(&lfs_lock, MUTEX_DEFAULT, IPL_NONE); 559 mutex_init(&lfs_lock, MUTEX_DEFAULT, IPL_NONE);
560 cv_init(&locked_queue_cv, "lfsbuf"); 560 cv_init(&locked_queue_cv, "lfsbuf");
561 cv_init(&lfs_writing_cv, "lfsflush"); 561 cv_init(&lfs_writing_cv, "lfsflush");
562} 562}
563 563
564void 564void
565lfs_reinit(void) 565lfs_reinit(void)
566{ 566{
567 ulfs_reinit(); 567 ulfs_reinit();
568} 568}
569 569
570void 570void
571lfs_done(void) 571lfs_done(void)
572{ 572{
573 ulfs_done(); 573 ulfs_done();
574 mutex_destroy(&lfs_lock); 574 mutex_destroy(&lfs_lock);
575 cv_destroy(&locked_queue_cv); 575 cv_destroy(&locked_queue_cv);
576 cv_destroy(&lfs_writing_cv); 576 cv_destroy(&lfs_writing_cv);
577 pool_destroy(&lfs_inode_pool); 577 pool_destroy(&lfs_inode_pool);
578 pool_destroy(&lfs_dinode_pool); 578 pool_destroy(&lfs_dinode_pool);
579 pool_destroy(&lfs_inoext_pool); 579 pool_destroy(&lfs_inoext_pool);
580 pool_destroy(&lfs_lbnentry_pool); 580 pool_destroy(&lfs_lbnentry_pool);
581 malloc_type_detach(M_SEGMENT); 581 malloc_type_detach(M_SEGMENT);
582} 582}
583 583
584/* 584/*
585 * Called by main() when ulfs is going to be mounted as root. 585 * Called by main() when ulfs is going to be mounted as root.
586 */ 586 */
587int 587int
588lfs_mountroot(void) 588lfs_mountroot(void)
589{ 589{
590 extern struct vnode *rootvp; 590 extern struct vnode *rootvp;
591 struct lfs *fs = NULL; /* LFS */ 591 struct lfs *fs = NULL; /* LFS */
592 struct mount *mp; 592 struct mount *mp;
593 struct lwp *l = curlwp; 593 struct lwp *l = curlwp;
594 struct ulfsmount *ump; 594 struct ulfsmount *ump;
595 int error; 595 int error;
596 596
597 if (device_class(root_device) != DV_DISK) 597 if (device_class(root_device) != DV_DISK)
598 return (ENODEV); 598 return (ENODEV);
599 599
600 if (rootdev == NODEV) 600 if (rootdev == NODEV)
601 return (ENODEV); 601 return (ENODEV);
602 if ((error = vfs_rootmountalloc(MOUNT_LFS, "root_device", &mp))) { 602 if ((error = vfs_rootmountalloc(MOUNT_LFS, "root_device", &mp))) {
603 vrele(rootvp); 603 vrele(rootvp);
604 return (error); 604 return (error);
605 } 605 }
606 if ((error = lfs_mountfs(rootvp, mp, l))) { 606 if ((error = lfs_mountfs(rootvp, mp, l))) {
607 vfs_unbusy(mp, false, NULL); 607 vfs_unbusy(mp, false, NULL);
608 vfs_destroy(mp); 608 vfs_destroy(mp);
609 return (error); 609 return (error);
610 } 610 }
611 mountlist_append(mp); 611 mountlist_append(mp);
612 ump = VFSTOULFS(mp); 612 ump = VFSTOULFS(mp);
613 fs = ump->um_lfs; 613 fs = ump->um_lfs;
614 lfs_sb_setfsmnt(fs, mp->mnt_stat.f_mntonname); 614 lfs_sb_setfsmnt(fs, mp->mnt_stat.f_mntonname);
615 (void)lfs_statvfs(mp, &mp->mnt_stat); 615 (void)lfs_statvfs(mp, &mp->mnt_stat);
616 vfs_unbusy(mp, false, NULL); 616 vfs_unbusy(mp, false, NULL);
617 setrootfstime((time_t)lfs_sb_gettstamp(VFSTOULFS(mp)->um_lfs)); 617 setrootfstime((time_t)lfs_sb_gettstamp(VFSTOULFS(mp)->um_lfs));
618 return (0); 618 return (0);
619} 619}
620 620
621/* 621/*
622 * VFS Operations. 622 * VFS Operations.
623 * 623 *
624 * mount system call 624 * mount system call
625 */ 625 */
626int 626int
627lfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len) 627lfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
628{ 628{
629 struct lwp *l = curlwp; 629 struct lwp *l = curlwp;
630 struct vnode *devvp; 630 struct vnode *devvp;
631 struct ulfs_args *args = data; 631 struct ulfs_args *args = data;
632 struct ulfsmount *ump = NULL; 632 struct ulfsmount *ump = NULL;
633 struct lfs *fs = NULL; /* LFS */ 633 struct lfs *fs = NULL; /* LFS */
634 int error = 0, update; 634 int error = 0, update;
635 mode_t accessmode; 635 mode_t accessmode;
636 636
637 if (args == NULL) 637 if (args == NULL)
638 return EINVAL; 638 return EINVAL;
639 if (*data_len < sizeof *args) 639 if (*data_len < sizeof *args)
640 return EINVAL; 640 return EINVAL;
641 641
642 if (mp->mnt_flag & MNT_GETARGS) { 642 if (mp->mnt_flag & MNT_GETARGS) {
643 ump = VFSTOULFS(mp); 643 ump = VFSTOULFS(mp);
644 if (ump == NULL) 644 if (ump == NULL)
645 return EIO; 645 return EIO;
646 args->fspec = NULL; 646 args->fspec = NULL;
647 *data_len = sizeof *args; 647 *data_len = sizeof *args;
648 return 0; 648 return 0;
649 } 649 }
650 650
651 update = mp->mnt_flag & MNT_UPDATE; 651 update = mp->mnt_flag & MNT_UPDATE;
652 652
653 /* Check arguments */ 653 /* Check arguments */
654 if (args->fspec != NULL) { 654 if (args->fspec != NULL) {
655 /* 655 /*
656 * Look up the name and verify that it's sane. 656 * Look up the name and verify that it's sane.
657 */ 657 */
658 error = namei_simple_user(args->fspec, 658 error = namei_simple_user(args->fspec,
659 NSM_FOLLOW_NOEMULROOT, &devvp); 659 NSM_FOLLOW_NOEMULROOT, &devvp);
660 if (error != 0) 660 if (error != 0)
661 return (error); 661 return (error);
662 662
663 if (!update) { 663 if (!update) {
664 /* 664 /*
665 * Be sure this is a valid block device 665 * Be sure this is a valid block device
666 */ 666 */
667 if (devvp->v_type != VBLK) 667 if (devvp->v_type != VBLK)
668 error = ENOTBLK; 668 error = ENOTBLK;
669 else if (bdevsw_lookup(devvp->v_rdev) == NULL) 669 else if (bdevsw_lookup(devvp->v_rdev) == NULL)
670 error = ENXIO; 670 error = ENXIO;
671 } else { 671 } else {
672 /* 672 /*
673 * Be sure we're still naming the same device 673 * Be sure we're still naming the same device
674 * used for our initial mount 674 * used for our initial mount
675 * 675 *
676 * XXX dholland 20151010: if namei gives us a 676 * XXX dholland 20151010: if namei gives us a
677 * different vnode for the same device, 677 * different vnode for the same device,
678 * wouldn't it be better to use it going 678 * wouldn't it be better to use it going
679 * forward rather than ignore it in favor of 679 * forward rather than ignore it in favor of
680 * the old one? 680 * the old one?
681 */ 681 */
682 ump = VFSTOULFS(mp); 682 ump = VFSTOULFS(mp);
683 fs = ump->um_lfs; 683 fs = ump->um_lfs;
684 if (devvp != fs->lfs_devvp) { 684 if (devvp != fs->lfs_devvp) {
685 if (devvp->v_rdev != fs->lfs_devvp->v_rdev) 685 if (devvp->v_rdev != fs->lfs_devvp->v_rdev)
686 error = EINVAL; 686 error = EINVAL;
687 else { 687 else {
688 vrele(devvp); 688 vrele(devvp);
689 devvp = fs->lfs_devvp; 689 devvp = fs->lfs_devvp;
690 vref(devvp); 690 vref(devvp);
691 } 691 }
692 } 692 }
693 } 693 }
694 } else { 694 } else {
695 if (!update) { 695 if (!update) {
696 /* New mounts must have a filename for the device */ 696 /* New mounts must have a filename for the device */
697 return (EINVAL); 697 return (EINVAL);
698 } else { 698 } else {
699 /* Use the extant mount */ 699 /* Use the extant mount */
700 ump = VFSTOULFS(mp); 700 ump = VFSTOULFS(mp);
701 fs = ump->um_lfs; 701 fs = ump->um_lfs;
702 devvp = fs->lfs_devvp; 702 devvp = fs->lfs_devvp;
703 vref(devvp); 703 vref(devvp);
704 } 704 }
705 } 705 }
706 706
707 707
708 /* 708 /*
709 * If mount by non-root, then verify that user has necessary 709 * If mount by non-root, then verify that user has necessary
710 * permissions on the device. 710 * permissions on the device.
711 */ 711 */
712 if (error == 0) { 712 if (error == 0) {
713 accessmode = VREAD; 713 accessmode = VREAD;
714 if (update ? 714 if (update ?
715 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 : 715 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
716 (mp->mnt_flag & MNT_RDONLY) == 0) 716 (mp->mnt_flag & MNT_RDONLY) == 0)
717 accessmode |= VWRITE; 717 accessmode |= VWRITE;
718 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 718 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
719 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT, 719 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
720 KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp, 720 KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
721 KAUTH_ARG(accessmode)); 721 KAUTH_ARG(accessmode));
722 VOP_UNLOCK(devvp); 722 VOP_UNLOCK(devvp);
723 } 723 }
724 724
725 if (error) { 725 if (error) {
726 vrele(devvp); 726 vrele(devvp);
727 return (error); 727 return (error);
728 } 728 }
729 729
730 if (!update) { 730 if (!update) {
731 int flags; 731 int flags;
732 732
733 if (mp->mnt_flag & MNT_RDONLY) 733 if (mp->mnt_flag & MNT_RDONLY)
734 flags = FREAD; 734 flags = FREAD;
735 else 735 else
736 flags = FREAD|FWRITE; 736 flags = FREAD|FWRITE;
737 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 737 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
738 error = VOP_OPEN(devvp, flags, FSCRED); 738 error = VOP_OPEN(devvp, flags, FSCRED);
739 VOP_UNLOCK(devvp); 739 VOP_UNLOCK(devvp);
740 if (error) 740 if (error)
741 goto fail; 741 goto fail;
742 error = lfs_mountfs(devvp, mp, l); /* LFS */ 742 error = lfs_mountfs(devvp, mp, l); /* LFS */
743 if (error) { 743 if (error) {
744 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 744 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
745 (void)VOP_CLOSE(devvp, flags, NOCRED); 745 (void)VOP_CLOSE(devvp, flags, NOCRED);
746 VOP_UNLOCK(devvp); 746 VOP_UNLOCK(devvp);
747 goto fail; 747 goto fail;
748 } 748 }
749 749
750 ump = VFSTOULFS(mp); 750 ump = VFSTOULFS(mp);
751 fs = ump->um_lfs; 751 fs = ump->um_lfs;
752 } else { 752 } else {
753 /* 753 /*
754 * Update the mount. 754 * Update the mount.
755 */ 755 */
756 756
757 /* 757 /*
758 * The initial mount got a reference on this 758 * The initial mount got a reference on this
759 * device, so drop the one obtained via 759 * device, so drop the one obtained via
760 * namei(), above. 760 * namei(), above.
761 */ 761 */
762 vrele(devvp); 762 vrele(devvp);
763 763
764 ump = VFSTOULFS(mp); 764 ump = VFSTOULFS(mp);
765 fs = ump->um_lfs; 765 fs = ump->um_lfs;
766 766
767 if (fs->lfs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) { 767 if (fs->lfs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
768 /* 768 /*
769 * Changing from read/write to read-only. 769 * Changing from read/write to read-only.
770 * XXX: shouldn't we sync here? or does vfs do that? 770 * XXX: shouldn't we sync here? or does vfs do that?
771 */ 771 */
772#ifdef LFS_QUOTA2 772#ifdef LFS_QUOTA2
773 /* XXX: quotas should remain on when readonly */ 773 /* XXX: quotas should remain on when readonly */
774 if (fs->lfs_use_quota2) { 774 if (fs->lfs_use_quota2) {
775 error = lfsquota2_umount(mp, 0); 775 error = lfsquota2_umount(mp, 0);
776 if (error) { 776 if (error) {
777 return error; 777 return error;
778 } 778 }
779 } 779 }
780#endif 780#endif
781 } 781 }
782 782
783 if (fs->lfs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) { 783 if (fs->lfs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
784 /* 784 /*
785 * Changing from read-only to read/write. 785 * Changing from read-only to read/write.
786 * Note in the superblocks that we're writing. 786 * Note in the superblocks that we're writing.
787 */ 787 */
788 788
789 /* XXX: quotas should have been on even if readonly */ 789 /* XXX: quotas should have been on even if readonly */
790 if (fs->lfs_use_quota2) { 790 if (fs->lfs_use_quota2) {
791#ifdef LFS_QUOTA2 791#ifdef LFS_QUOTA2
792 error = lfs_quota2_mount(mp); 792 error = lfs_quota2_mount(mp);
793#else 793#else
794 uprintf("%s: no kernel support for this " 794 uprintf("%s: no kernel support for this "
795 "filesystem's quotas\n", 795 "filesystem's quotas\n",
796 mp->mnt_stat.f_mntonname); 796 mp->mnt_stat.f_mntonname);
797 if (mp->mnt_flag & MNT_FORCE) { 797 if (mp->mnt_flag & MNT_FORCE) {
798 uprintf("%s: mounting anyway; " 798 uprintf("%s: mounting anyway; "
799 "fsck afterwards\n", 799 "fsck afterwards\n",
800 mp->mnt_stat.f_mntonname); 800 mp->mnt_stat.f_mntonname);
801 } else { 801 } else {
802 error = EINVAL; 802 error = EINVAL;
803 } 803 }
804#endif 804#endif
805 if (error) { 805 if (error) {
806 return error; 806 return error;
807 } 807 }
808 } 808 }
809 809
810 fs->lfs_ronly = 0; 810 fs->lfs_ronly = 0;
811 if (lfs_sb_getpflags(fs) & LFS_PF_CLEAN) { 811 if (lfs_sb_getpflags(fs) & LFS_PF_CLEAN) {
812 lfs_sb_setpflags(fs, lfs_sb_getpflags(fs) & ~LFS_PF_CLEAN); 812 lfs_sb_setpflags(fs, lfs_sb_getpflags(fs) & ~LFS_PF_CLEAN);
813 lfs_writesuper(fs, lfs_sb_getsboff(fs, 0)); 813 lfs_writesuper(fs, lfs_sb_getsboff(fs, 0));
814 lfs_writesuper(fs, lfs_sb_getsboff(fs, 1)); 814 lfs_writesuper(fs, lfs_sb_getsboff(fs, 1));
815 } 815 }
816 } 816 }
817 if (args->fspec == NULL) 817 if (args->fspec == NULL)
818 return EINVAL; 818 return EINVAL;
819 } 819 }
820 820
821 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec, 821 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
822 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l); 822 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
823 if (error == 0) 823 if (error == 0)
824 lfs_sb_setfsmnt(fs, mp->mnt_stat.f_mntonname); 824 lfs_sb_setfsmnt(fs, mp->mnt_stat.f_mntonname);
825 return error; 825 return error;
826 826
827fail: 827fail:
828 vrele(devvp); 828 vrele(devvp);
829 return (error); 829 return (error);
830} 830}
831 831
 832/*
 833 * Helper for mountfs. Note that the fs pointer may be a dummy one
 834 * pointing into a superblock buffer. (Which is gross; see below.)
 835 */
 836static int
 837lfs_checkmagic(struct lfs *fs)
 838{
 839 switch (fs->lfs_dlfs_u.u_32.dlfs_magic) {
 840 case LFS_MAGIC:
 841 fs->lfs_is64 = false;
 842 fs->lfs_dobyteswap = false;
 843 break;
 844 case LFS64_MAGIC:
 845 fs->lfs_is64 = true;
 846 fs->lfs_dobyteswap = false;
 847 break;
 848#ifdef LFS_EI
 849 case LFS_MAGIC_SWAPPED:
 850 fs->lfs_is64 = false;
 851 fs->lfs_dobyteswap = true;
 852 break;
 853 case LFS64_MAGIC_SWAPPED:
 854 fs->lfs_is64 = true;
 855 fs->lfs_dobyteswap = true;
 856 break;
 857#endif
 858 default:
 859 /* XXX needs translation */
 860 return EINVAL;
 861 }
 862 return 0;
 863}
832 864
833/* 865/*
834 * Common code for mount and mountroot 866 * Common code for mount and mountroot
835 * LFS specific 867 * LFS specific
836 */ 868 */
837int 869int
838lfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l) 870lfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
839{ 871{
840 struct dlfs *tdfs, *dfs, *adfs; 872 struct lfs *primarysb, *altsb, *thesb;
 873 struct buf *primarybuf, *altbuf;
841 struct lfs *fs; 874 struct lfs *fs;
842 struct ulfsmount *ump; 875 struct ulfsmount *ump;
843 struct vnode *vp; 876 struct vnode *vp;
844 struct buf *bp, *abp; 
845 dev_t dev; 877 dev_t dev;
846 int error, i, ronly, fsbsize; 878 int error, i, ronly, fsbsize;
847 kauth_cred_t cred; 879 kauth_cred_t cred;
848 CLEANERINFO *cip; 880 CLEANERINFO *cip;
849 SEGUSE *sup; 881 SEGUSE *sup;
850 daddr_t sb_addr; 882 daddr_t sb_addr;
851 883
852 cred = l ? l->l_cred : NOCRED; 884 cred = l ? l->l_cred : NOCRED;
853 885
854 /* The superblock is supposed to be 512 bytes. */ 886 /* The superblock is supposed to be 512 bytes. */
855 __CTASSERT(sizeof(struct dlfs) == DEV_BSIZE); 887 __CTASSERT(sizeof(struct dlfs) == DEV_BSIZE);
856 888
857 /* 889 /*
858 * Flush out any old buffers remaining from a previous use. 890 * Flush out any old buffers remaining from a previous use.
859 */ 891 */
860 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 892 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
861 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0); 893 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
862 VOP_UNLOCK(devvp); 894 VOP_UNLOCK(devvp);
863 if (error) 895 if (error)
864 return (error); 896 return (error);
865 897
866 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 898 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
867 899
868 /* Don't free random space on error. */ 900 /* Don't free random space on error. */
869 bp = NULL; 901 primarybuf = NULL;
870 abp = NULL; 902 altbuf = NULL;
871 ump = NULL; 903 ump = NULL;
872 904
873 sb_addr = LFS_LABELPAD / DEV_BSIZE; 905 sb_addr = LFS_LABELPAD / DEV_BSIZE;
874 while (1) { 906 while (1) {
875 /* Read in the superblock. */ 907 /*
876 error = bread(devvp, sb_addr, LFS_SBPAD, 0, &bp); 908 * Read in the superblock.
 909 *
 910 * Note that because LFS_SBPAD is substantially larger
 911 * (8K) than the actual on-disk superblock (512 bytes)
 912 * the buffer contains enough space to be used as a
 913 * whole struct lfs (in-memory superblock) - we do this
 914 * only so we can set and use the is64 and dobyteswap
 915 * members. XXX this is gross and the logic here should
 916 * be reworked.
 917 */
 918 error = bread(devvp, sb_addr, LFS_SBPAD, 0, &primarybuf);
877 if (error) 919 if (error)
878 goto out; 920 goto out;
879 dfs = (struct dlfs *)bp->b_data; 921 primarysb = (struct lfs *)primarybuf->b_data;
880 922
881 /* Check the basics. */ 923 /* Check the basics. */
882 if (dfs->dlfs_magic != LFS_MAGIC || dfs->dlfs_bsize > MAXBSIZE || 924 error = lfs_checkmagic(primarysb);
883 dfs->dlfs_version > LFS_VERSION || 925 if (error) {
884 dfs->dlfs_bsize < sizeof(struct dlfs)) { 926 DLOG((DLOG_MOUNT, "lfs_mountfs: primary superblock wrong magic\n"));
 927 goto out;
 928 }
 929 if (lfs_sb_getbsize(primarysb) > MAXBSIZE ||
 930 lfs_sb_getversion(primarysb) > LFS_VERSION ||
 931 lfs_sb_getbsize(primarysb) < sizeof(struct dlfs)) {
885 DLOG((DLOG_MOUNT, "lfs_mountfs: primary superblock sanity failed\n")); 932 DLOG((DLOG_MOUNT, "lfs_mountfs: primary superblock sanity failed\n"));
886 error = EINVAL; /* XXX needs translation */ 933 /* XXX needs translation */
 934 error = EINVAL;
887 goto out; 935 goto out;
888 } 936 }
889 if (dfs->dlfs_inodefmt > LFS_MAXINODEFMT) { 937 if (lfs_sb_getinodefmt(primarysb) > LFS_MAXINODEFMT) {
890 DLOG((DLOG_MOUNT, "lfs_mountfs: unknown inode format %d\n", 938 DLOG((DLOG_MOUNT, "lfs_mountfs: unknown inode format %d\n",
891 dfs->dlfs_inodefmt)); 939 lfs_sb_getinodefmt(primarysb)));
892 error = EINVAL; 940 error = EINVAL;
893 goto out; 941 goto out;
894 } 942 }
895 943
896 if (dfs->dlfs_version == 1) 944 if (lfs_sb_getversion(primarysb) == 1)
897 fsbsize = DEV_BSIZE; 945 fsbsize = DEV_BSIZE;
898 else { 946 else {
899 fsbsize = 1 << dfs->dlfs_ffshift; 947 fsbsize = 1 << lfs_sb_getffshift(primarysb);
900 /* 948 /*
901 * Could be, if the frag size is large enough, that we 949 * Could be, if the frag size is large enough, that we
902 * don't have the "real" primary superblock. If that's 950 * don't have the "real" primary superblock. If that's
903 * the case, get the real one, and try again. 951 * the case, get the real one, and try again.
904 */ 952 */
905 if (sb_addr != (dfs->dlfs_sboffs[0] << (dfs->dlfs_ffshift - DEV_BSHIFT))) { 953 if (sb_addr != (lfs_sb_getsboff(primarysb, 0) << (lfs_sb_getffshift(primarysb) - DEV_BSHIFT))) {
906 DLOG((DLOG_MOUNT, "lfs_mountfs: sb daddr" 954 DLOG((DLOG_MOUNT, "lfs_mountfs: sb daddr"
907 " 0x%llx is not right, trying 0x%llx\n", 955 " 0x%llx is not right, trying 0x%llx\n",
908 (long long)sb_addr, 956 (long long)sb_addr,
909 (long long)(dfs->dlfs_sboffs[0] << (dfs->dlfs_ffshift - DEV_BSHIFT)))); 957 (long long)(lfs_sb_getsboff(primarysb, 0) << (lfs_sb_getffshift(primarysb) - DEV_BSHIFT))));
910 sb_addr = dfs->dlfs_sboffs[0] << (dfs->dlfs_ffshift - DEV_BSHIFT); 958 sb_addr = lfs_sb_getsboff(primarysb, 0) << (lfs_sb_getffshift(primarysb) - DEV_BSHIFT);
911 brelse(bp, 0); 959 brelse(primarybuf, BC_INVAL);
912 continue; 960 continue;
913 } 961 }
914 } 962 }
915 break; 963 break;
916 } 964 }
917 965
918 /* 966 /*
919 * Check the second superblock to see which is newer; then mount 967 * Check the second superblock to see which is newer; then mount
920 * using the older of the two. This is necessary to ensure that 968 * using the older of the two. This is necessary to ensure that
921 * the filesystem is valid if it was not unmounted cleanly. 969 * the filesystem is valid if it was not unmounted cleanly.
922 */ 970 */
923 971
924 if (dfs->dlfs_sboffs[1] && 972 if (lfs_sb_getsboff(primarysb, 1) &&
925 dfs->dlfs_sboffs[1] - LFS_LABELPAD / fsbsize > LFS_SBPAD / fsbsize) 973 lfs_sb_getsboff(primarysb, 1) - LFS_LABELPAD / fsbsize > LFS_SBPAD / fsbsize)
926 { 974 {
927 error = bread(devvp, dfs->dlfs_sboffs[1] * (fsbsize / DEV_BSIZE), 975 error = bread(devvp, lfs_sb_getsboff(primarysb, 1) * (fsbsize / DEV_BSIZE),
928 LFS_SBPAD, 0, &abp); 976 LFS_SBPAD, 0, &altbuf);
929 if (error) 977 if (error)
930 goto out; 978 goto out;
931 adfs = (struct dlfs *)abp->b_data; 979 altsb = (struct lfs *)altbuf->b_data;
932 980
933 if (dfs->dlfs_version == 1) { 981 /*
934 /* 1s resolution comparison */ 982 * Note: this used to do the sanity check only if the
935 if (adfs->dlfs_tstamp < dfs->dlfs_tstamp) 983 * timestamp/serial comparison required use of altsb;
936 tdfs = adfs; 984 * this way is less tolerant, but if altsb is corrupted
937 else 985 * enough that the magic number, version, and blocksize
938 tdfs = dfs; 986 * are bogus, why would the timestamp or serial fields
939 } else { 987 * mean anything either? If this kind of thing happens,
940 /* monotonic infinite-resolution comparison */ 988 * you need to fsck anyway.
941 if (adfs->dlfs_serial < dfs->dlfs_serial) 989 */
942 tdfs = adfs; 990
943 else 991 error = lfs_checkmagic(altsb);
944 tdfs = dfs; 992 if (error)
945 } 993 goto out;
946 994
947 /* Check the basics. */ 995 /* Check the basics. */
948 if (tdfs->dlfs_magic != LFS_MAGIC || 996 if (lfs_sb_getbsize(altsb) > MAXBSIZE ||
949 tdfs->dlfs_bsize > MAXBSIZE || 997 lfs_sb_getversion(altsb) > LFS_VERSION ||
950 tdfs->dlfs_version > LFS_VERSION || 998 lfs_sb_getbsize(altsb) < sizeof(struct dlfs)) {
951 tdfs->dlfs_bsize < sizeof(struct dlfs)) { 
952 DLOG((DLOG_MOUNT, "lfs_mountfs: alt superblock" 999 DLOG((DLOG_MOUNT, "lfs_mountfs: alt superblock"
953 " sanity failed\n")); 1000 " sanity failed\n"));
954 error = EINVAL; /* XXX needs translation */ 1001 error = EINVAL; /* XXX needs translation */
955 goto out; 1002 goto out;
956 } 1003 }
 1004
 1005 if (lfs_sb_getversion(primarysb) == 1) {
 1006 /* 1s resolution comparison */
 1007 if (lfs_sb_gettstamp(altsb) < lfs_sb_gettstamp(primarysb))
 1008 thesb = altsb;
 1009 else
 1010 thesb = primarysb;
 1011 } else {
 1012 /* monotonic infinite-resolution comparison */
 1013 if (lfs_sb_getserial(altsb) < lfs_sb_getserial(primarysb))
 1014 thesb = altsb;
 1015 else
 1016 thesb = primarysb;
 1017 }
957 } else { 1018 } else {
958 DLOG((DLOG_MOUNT, "lfs_mountfs: invalid alt superblock" 1019 DLOG((DLOG_MOUNT, "lfs_mountfs: invalid alt superblock location"
959 " daddr=0x%x\n", dfs->dlfs_sboffs[1])); 1020 " daddr=0x%x\n", lfs_sb_getsboff(primarysb, 1)));
960 error = EINVAL; 1021 error = EINVAL;
961 goto out; 1022 goto out;
962 } 1023 }
963 1024
964 /* Allocate the mount structure, copy the superblock into it. */ 1025 /*
 1026 * Allocate the mount structure, copy the superblock into it.
 1027 * Note that the 32-bit and 64-bit superblocks are the same size.
 1028 */
965 fs = kmem_zalloc(sizeof(struct lfs), KM_SLEEP); 1029 fs = kmem_zalloc(sizeof(struct lfs), KM_SLEEP);
966 memcpy(&fs->lfs_dlfs_u.u_32, tdfs, sizeof(struct dlfs)); 1030 memcpy(&fs->lfs_dlfs_u.u_32, &thesb->lfs_dlfs_u.u_32,
967 fs->lfs_is64 = false; /* XXX notyet */ 1031 sizeof(struct dlfs));
968 fs->lfs_dobyteswap = false; /* XXX notyet */ 1032 fs->lfs_is64 = thesb->lfs_is64;
 1033 fs->lfs_dobyteswap = thesb->lfs_dobyteswap;
969 fs->lfs_hasolddirfmt = false; /* set for real below */ 1034 fs->lfs_hasolddirfmt = false; /* set for real below */
970 1035
971 /* Compatibility */ 1036 /* Compatibility */
972 if (lfs_sb_getversion(fs) < 2) { 1037 if (lfs_sb_getversion(fs) < 2) {
973 lfs_sb_setsumsize(fs, LFS_V1_SUMMARY_SIZE); 1038 lfs_sb_setsumsize(fs, LFS_V1_SUMMARY_SIZE);
974 lfs_sb_setibsize(fs, lfs_sb_getbsize(fs)); 1039 lfs_sb_setibsize(fs, lfs_sb_getbsize(fs));
975 lfs_sb_sets0addr(fs, lfs_sb_getsboff(fs, 0)); 1040 lfs_sb_sets0addr(fs, lfs_sb_getsboff(fs, 0));
976 lfs_sb_settstamp(fs, lfs_sb_getotstamp(fs)); 1041 lfs_sb_settstamp(fs, lfs_sb_getotstamp(fs));
977 lfs_sb_setfsbtodb(fs, 0); 1042 lfs_sb_setfsbtodb(fs, 0);
978 } 1043 }
979 if (lfs_sb_getresvseg(fs) == 0) 1044 if (lfs_sb_getresvseg(fs) == 0)
980 lfs_sb_setresvseg(fs, MIN(lfs_sb_getminfreeseg(fs) - 1, \ 1045 lfs_sb_setresvseg(fs, MIN(lfs_sb_getminfreeseg(fs) - 1, \
981 MAX(MIN_RESV_SEGS, lfs_sb_getminfreeseg(fs) / 2 + 1))); 1046 MAX(MIN_RESV_SEGS, lfs_sb_getminfreeseg(fs) / 2 + 1)));
982 1047
983 /* 1048 /*
984 * If we aren't going to be able to write meaningfully to this 1049 * If we aren't going to be able to write meaningfully to this
985 * filesystem, and were not mounted readonly, bomb out now. 1050 * filesystem, and were not mounted readonly, bomb out now.
986 */ 1051 */
987 if (lfs_fsbtob(fs, LFS_NRESERVE(fs)) > LFS_MAX_BYTES && !ronly) { 1052 if (lfs_fsbtob(fs, LFS_NRESERVE(fs)) > LFS_MAX_BYTES && !ronly) {
988 DLOG((DLOG_MOUNT, "lfs_mount: to mount this filesystem read/write," 1053 DLOG((DLOG_MOUNT, "lfs_mount: to mount this filesystem read/write,"
989 " we need BUFPAGES >= %lld\n", 1054 " we need BUFPAGES >= %lld\n",
990 (long long)((bufmem_hiwater / bufmem_lowater) * 1055 (long long)((bufmem_hiwater / bufmem_lowater) *
991 LFS_INVERSE_MAX_BYTES( 1056 LFS_INVERSE_MAX_BYTES(
992 lfs_fsbtob(fs, LFS_NRESERVE(fs))) >> PAGE_SHIFT))); 1057 lfs_fsbtob(fs, LFS_NRESERVE(fs))) >> PAGE_SHIFT)));
993 kmem_free(fs, sizeof(struct lfs)); 1058 kmem_free(fs, sizeof(struct lfs));
994 error = EFBIG; /* XXX needs translation */ 1059 error = EFBIG; /* XXX needs translation */
995 goto out; 1060 goto out;
996 } 1061 }
997 1062
998 /* Before rolling forward, lock so vget will sleep for other procs */ 1063 /* Before rolling forward, lock so vget will sleep for other procs */
999 if (l != NULL) { 1064 if (l != NULL) {
1000 fs->lfs_flags = LFS_NOTYET; 1065 fs->lfs_flags = LFS_NOTYET;
1001 fs->lfs_rfpid = l->l_proc->p_pid; 1066 fs->lfs_rfpid = l->l_proc->p_pid;
1002 } 1067 }
1003 1068
1004 ump = kmem_zalloc(sizeof(*ump), KM_SLEEP); 1069 ump = kmem_zalloc(sizeof(*ump), KM_SLEEP);
1005 ump->um_lfs = fs; 1070 ump->um_lfs = fs;
1006 ump->um_fstype = ULFS1; 1071 ump->um_fstype = ULFS1;
1007 /* ump->um_cleaner_thread = NULL; */ 1072 /* ump->um_cleaner_thread = NULL; */
1008 if (sizeof(struct lfs) < LFS_SBPAD) { /* XXX why? */ 1073 brelse(primarybuf, BC_INVAL);
1009 brelse(bp, BC_INVAL); 1074 brelse(altbuf, BC_INVAL);
1010 brelse(abp, BC_INVAL); 1075 primarybuf = NULL;
1011 } else { 1076 altbuf = NULL;
1012 brelse(bp, 0); 
1013 brelse(abp, 0); 
1014 } 
1015 bp = NULL; 
1016 abp = NULL; 
1017 1077
1018 1078
1019 /* Set up the I/O information */ 1079 /* Set up the I/O information */
1020 fs->lfs_devbsize = DEV_BSIZE; 1080 fs->lfs_devbsize = DEV_BSIZE;
1021 fs->lfs_iocount = 0; 1081 fs->lfs_iocount = 0;
1022 fs->lfs_diropwait = 0; 1082 fs->lfs_diropwait = 0;
1023 fs->lfs_activesb = 0; 1083 fs->lfs_activesb = 0;
1024 lfs_sb_setuinodes(fs, 0); 1084 lfs_sb_setuinodes(fs, 0);
1025 fs->lfs_ravail = 0; 1085 fs->lfs_ravail = 0;
1026 fs->lfs_favail = 0; 1086 fs->lfs_favail = 0;
1027 fs->lfs_sbactive = 0; 1087 fs->lfs_sbactive = 0;
1028 1088
1029 /* Set up the ifile and lock aflags */ 1089 /* Set up the ifile and lock aflags */
1030 fs->lfs_doifile = 0; 1090 fs->lfs_doifile = 0;
1031 fs->lfs_writer = 0; 1091 fs->lfs_writer = 0;
1032 fs->lfs_dirops = 0; 1092 fs->lfs_dirops = 0;
1033 fs->lfs_nadirop = 0; 1093 fs->lfs_nadirop = 0;
1034 fs->lfs_seglock = 0; 1094 fs->lfs_seglock = 0;
1035 fs->lfs_pdflush = 0; 1095 fs->lfs_pdflush = 0;
1036 fs->lfs_sleepers = 0; 1096 fs->lfs_sleepers = 0;
1037 fs->lfs_pages = 0; 1097 fs->lfs_pages = 0;
1038 rw_init(&fs->lfs_fraglock); 1098 rw_init(&fs->lfs_fraglock);
1039 rw_init(&fs->lfs_iflock); 1099 rw_init(&fs->lfs_iflock);
1040 cv_init(&fs->lfs_stopcv, "lfsstop"); 1100 cv_init(&fs->lfs_stopcv, "lfsstop");
1041 1101
1042 /* Set the file system readonly/modify bits. */ 1102 /* Set the file system readonly/modify bits. */
1043 fs->lfs_ronly = ronly; 1103 fs->lfs_ronly = ronly;
1044 if (ronly == 0) 1104 if (ronly == 0)
1045 fs->lfs_fmod = 1; 1105 fs->lfs_fmod = 1;
1046 1106
1047 /* Device we're using */ 1107 /* Device we're using */
1048 dev = devvp->v_rdev; 1108 dev = devvp->v_rdev;
1049 fs->lfs_dev = dev; 1109 fs->lfs_dev = dev;
1050 fs->lfs_devvp = devvp; 1110 fs->lfs_devvp = devvp;
1051 1111
1052 /* ulfs-level information */ 1112 /* ulfs-level information */
1053 fs->um_flags = 0; 1113 fs->um_flags = 0;
1054 fs->um_bptrtodb = lfs_sb_getffshift(fs) - DEV_BSHIFT; 1114 fs->um_bptrtodb = lfs_sb_getffshift(fs) - DEV_BSHIFT;
1055 fs->um_seqinc = lfs_sb_getfrag(fs); 1115 fs->um_seqinc = lfs_sb_getfrag(fs);
1056 fs->um_nindir = lfs_sb_getnindir(fs); 1116 fs->um_nindir = lfs_sb_getnindir(fs);
1057 fs->um_lognindir = ffs(lfs_sb_getnindir(fs)) - 1; 1117 fs->um_lognindir = ffs(lfs_sb_getnindir(fs)) - 1;
1058 fs->um_maxsymlinklen = lfs_sb_getmaxsymlinklen(fs); 1118 fs->um_maxsymlinklen = lfs_sb_getmaxsymlinklen(fs);
1059 fs->um_dirblksiz = LFS_DIRBLKSIZ; 1119 fs->um_dirblksiz = LFS_DIRBLKSIZ;
1060 fs->um_maxfilesize = lfs_sb_getmaxfilesize(fs); 1120 fs->um_maxfilesize = lfs_sb_getmaxfilesize(fs);
1061 1121
1062 /* quota stuff */ 1122 /* quota stuff */
1063 /* XXX: these need to come from the on-disk superblock to be used */ 1123 /* XXX: these need to come from the on-disk superblock to be used */
1064 fs->lfs_use_quota2 = 0; 1124 fs->lfs_use_quota2 = 0;
1065 fs->lfs_quota_magic = 0; 1125 fs->lfs_quota_magic = 0;
1066 fs->lfs_quota_flags = 0; 1126 fs->lfs_quota_flags = 0;
1067 fs->lfs_quotaino[0] = 0; 1127 fs->lfs_quotaino[0] = 0;
1068 fs->lfs_quotaino[1] = 0; 1128 fs->lfs_quotaino[1] = 0;
1069 1129
1070 /* Initialize the mount structure. */ 1130 /* Initialize the mount structure. */
1071 mp->mnt_data = ump; 1131 mp->mnt_data = ump;
1072 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev; 1132 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
1073 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_LFS); 1133 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_LFS);
1074 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 1134 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
1075 mp->mnt_stat.f_namemax = LFS_MAXNAMLEN; 1135 mp->mnt_stat.f_namemax = LFS_MAXNAMLEN;
1076 mp->mnt_stat.f_iosize = lfs_sb_getbsize(fs); 1136 mp->mnt_stat.f_iosize = lfs_sb_getbsize(fs);
1077 mp->mnt_flag |= MNT_LOCAL; 1137 mp->mnt_flag |= MNT_LOCAL;
1078 mp->mnt_fs_bshift = lfs_sb_getbshift(fs); 1138 mp->mnt_fs_bshift = lfs_sb_getbshift(fs);
1079 if (fs->um_maxsymlinklen > 0) 1139 if (fs->um_maxsymlinklen > 0)
1080 mp->mnt_iflag |= IMNT_DTYPE; 1140 mp->mnt_iflag |= IMNT_DTYPE;
1081 else 1141 else
1082 fs->lfs_hasolddirfmt = true; 1142 fs->lfs_hasolddirfmt = true;
1083 1143
1084 ump->um_mountp = mp; 1144 ump->um_mountp = mp;
1085 for (i = 0; i < ULFS_MAXQUOTAS; i++) 1145 for (i = 0; i < ULFS_MAXQUOTAS; i++)
1086 ump->um_quotas[i] = NULLVP; 1146 ump->um_quotas[i] = NULLVP;
1087 spec_node_setmountedfs(devvp, mp); 1147 spec_node_setmountedfs(devvp, mp);
1088 1148
1089 /* Set up reserved memory for pageout */ 1149 /* Set up reserved memory for pageout */
1090 lfs_setup_resblks(fs); 1150 lfs_setup_resblks(fs);
1091 /* Set up vdirop tailq */ 1151 /* Set up vdirop tailq */
1092 TAILQ_INIT(&fs->lfs_dchainhd); 1152 TAILQ_INIT(&fs->lfs_dchainhd);
1093 /* and paging tailq */ 1153 /* and paging tailq */
1094 TAILQ_INIT(&fs->lfs_pchainhd); 1154 TAILQ_INIT(&fs->lfs_pchainhd);
1095 /* and delayed segment accounting for truncation list */ 1155 /* and delayed segment accounting for truncation list */
1096 LIST_INIT(&fs->lfs_segdhd); 1156 LIST_INIT(&fs->lfs_segdhd);
1097 1157
1098 /* 1158 /*
1099 * We use the ifile vnode for almost every operation. Instead of 1159 * We use the ifile vnode for almost every operation. Instead of
1100 * retrieving it from the hash table each time we retrieve it here, 1160 * retrieving it from the hash table each time we retrieve it here,
1101 * artificially increment the reference count and keep a pointer 1161 * artificially increment the reference count and keep a pointer
1102 * to it in the incore copy of the superblock. 1162 * to it in the incore copy of the superblock.
1103 */ 1163 */
1104 if ((error = VFS_VGET(mp, LFS_IFILE_INUM, &vp)) != 0) { 1164 if ((error = VFS_VGET(mp, LFS_IFILE_INUM, &vp)) != 0) {
1105 DLOG((DLOG_MOUNT, "lfs_mountfs: ifile vget failed, error=%d\n", error)); 1165 DLOG((DLOG_MOUNT, "lfs_mountfs: ifile vget failed, error=%d\n", error));
1106 goto out; 1166 goto out;
1107 } 1167 }
1108 fs->lfs_ivnode = vp; 1168 fs->lfs_ivnode = vp;
1109 vref(vp); 1169 vref(vp);
1110 1170
1111 /* Set up inode bitmap and order free list */ 1171 /* Set up inode bitmap and order free list */
1112 lfs_order_freelist(fs); 1172 lfs_order_freelist(fs);
1113 1173
1114 /* Set up segment usage flags for the autocleaner. */ 1174 /* Set up segment usage flags for the autocleaner. */
1115 fs->lfs_nactive = 0; 1175 fs->lfs_nactive = 0;
1116 fs->lfs_suflags = malloc(2 * sizeof(u_int32_t *), 1176 fs->lfs_suflags = malloc(2 * sizeof(u_int32_t *),
1117 M_SEGMENT, M_WAITOK); 1177 M_SEGMENT, M_WAITOK);
1118 fs->lfs_suflags[0] = malloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t), 1178 fs->lfs_suflags[0] = malloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t),
1119 M_SEGMENT, M_WAITOK); 1179 M_SEGMENT, M_WAITOK);
1120 fs->lfs_suflags[1] = malloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t), 1180 fs->lfs_suflags[1] = malloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t),
1121 M_SEGMENT, M_WAITOK); 1181 M_SEGMENT, M_WAITOK);
1122 memset(fs->lfs_suflags[1], 0, lfs_sb_getnseg(fs) * sizeof(u_int32_t)); 1182 memset(fs->lfs_suflags[1], 0, lfs_sb_getnseg(fs) * sizeof(u_int32_t));
1123 for (i = 0; i < lfs_sb_getnseg(fs); i++) { 1183 for (i = 0; i < lfs_sb_getnseg(fs); i++) {
1124 int changed; 1184 int changed;
 1185 struct buf *bp;
1125 1186
1126 LFS_SEGENTRY(sup, fs, i, bp); 1187 LFS_SEGENTRY(sup, fs, i, bp);
1127 changed = 0; 1188 changed = 0;
1128 if (!ronly) { 1189 if (!ronly) {
1129 if (sup->su_nbytes == 0 && 1190 if (sup->su_nbytes == 0 &&
1130 !(sup->su_flags & SEGUSE_EMPTY)) { 1191 !(sup->su_flags & SEGUSE_EMPTY)) {
1131 sup->su_flags |= SEGUSE_EMPTY; 1192 sup->su_flags |= SEGUSE_EMPTY;
1132 ++changed; 1193 ++changed;
1133 } else if (!(sup->su_nbytes == 0) && 1194 } else if (!(sup->su_nbytes == 0) &&
1134 (sup->su_flags & SEGUSE_EMPTY)) { 1195 (sup->su_flags & SEGUSE_EMPTY)) {
1135 sup->su_flags &= ~SEGUSE_EMPTY; 1196 sup->su_flags &= ~SEGUSE_EMPTY;
1136 ++changed; 1197 ++changed;
1137 } 1198 }
1138 if (sup->su_flags & (SEGUSE_ACTIVE|SEGUSE_INVAL)) { 1199 if (sup->su_flags & (SEGUSE_ACTIVE|SEGUSE_INVAL)) {
1139 sup->su_flags &= ~(SEGUSE_ACTIVE|SEGUSE_INVAL); 1200 sup->su_flags &= ~(SEGUSE_ACTIVE|SEGUSE_INVAL);
1140 ++changed; 1201 ++changed;
1141 } 1202 }
1142 } 1203 }
1143 fs->lfs_suflags[0][i] = sup->su_flags; 1204 fs->lfs_suflags[0][i] = sup->su_flags;
1144 if (changed) 1205 if (changed)
1145 LFS_WRITESEGENTRY(sup, fs, i, bp); 1206 LFS_WRITESEGENTRY(sup, fs, i, bp);
1146 else 1207 else
1147 brelse(bp, 0); 1208 brelse(bp, 0);
1148 } 1209 }
1149 1210
1150 /* 1211 /*
1151 * XXX: if the fs has quotas, quotas should be on even if 1212 * XXX: if the fs has quotas, quotas should be on even if
1152 * readonly. Otherwise you can't query the quota info! 1213 * readonly. Otherwise you can't query the quota info!
1153 * However, that's not how the quota2 code got written and I 1214 * However, that's not how the quota2 code got written and I
1154 * don't know if it'll behave itself if enabled while 1215 * don't know if it'll behave itself if enabled while
1155 * readonly, so for now use the same enable logic as ffs. 1216 * readonly, so for now use the same enable logic as ffs.
1156 * 1217 *
1157 * XXX: also, if you use the -f behavior allowed here (and 1218 * XXX: also, if you use the -f behavior allowed here (and
1158 * equivalently above for remount) it will corrupt the fs. It 1219 * equivalently above for remount) it will corrupt the fs. It
1159 * ought not to allow that. It should allow mounting readonly 1220 * ought not to allow that. It should allow mounting readonly
1160 * if there are quotas and the kernel doesn't have the quota 1221 * if there are quotas and the kernel doesn't have the quota
1161 * code, but only readonly. 1222 * code, but only readonly.
1162 * 1223 *
1163 * XXX: and if you use the -f behavior allowed here it will 1224 * XXX: and if you use the -f behavior allowed here it will
1164 * likely crash at unmount time (or remount time) because we 1225 * likely crash at unmount time (or remount time) because we
1165 * think quotas are active. 1226 * think quotas are active.
1166 * 1227 *
1167 * Although none of this applies until there's a way to set 1228 * Although none of this applies until there's a way to set
1168 * lfs_use_quota2 and have quotas in the fs at all. 1229 * lfs_use_quota2 and have quotas in the fs at all.
1169 */ 1230 */
1170 if (!ronly && fs->lfs_use_quota2) { 1231 if (!ronly && fs->lfs_use_quota2) {
1171#ifdef LFS_QUOTA2 1232#ifdef LFS_QUOTA2
1172 error = lfs_quota2_mount(mp); 1233 error = lfs_quota2_mount(mp);
1173#else 1234#else
1174 uprintf("%s: no kernel support for this filesystem's quotas\n", 1235 uprintf("%s: no kernel support for this filesystem's quotas\n",
1175 mp->mnt_stat.f_mntonname); 1236 mp->mnt_stat.f_mntonname);
1176 if (mp->mnt_flag & MNT_FORCE) { 1237 if (mp->mnt_flag & MNT_FORCE) {
1177 uprintf("%s: mounting anyway; fsck afterwards\n", 1238 uprintf("%s: mounting anyway; fsck afterwards\n",
1178 mp->mnt_stat.f_mntonname); 1239 mp->mnt_stat.f_mntonname);
1179 } else { 1240 } else {
1180 error = EINVAL; 1241 error = EINVAL;
1181 } 1242 }
1182#endif 1243#endif
1183 if (error) { 1244 if (error) {
1184 /* XXX XXX must clean up the stuff immediately above */ 1245 /* XXX XXX must clean up the stuff immediately above */
1185 printf("lfs_mountfs: sorry, leaking some memory\n"); 1246 printf("lfs_mountfs: sorry, leaking some memory\n");
1186 goto out; 1247 goto out;
1187 } 1248 }
1188 } 1249 }
1189 1250
1190#ifdef LFS_EXTATTR 1251#ifdef LFS_EXTATTR
1191 /* 1252 /*
1192 * Initialize file-backed extended attributes for ULFS1 file 1253 * Initialize file-backed extended attributes for ULFS1 file
1193 * systems. 1254 * systems.
1194 * 1255 *
1195 * XXX: why is this limited to ULFS1? 1256 * XXX: why is this limited to ULFS1?
1196 */ 1257 */
1197 if (ump->um_fstype == ULFS1) { 1258 if (ump->um_fstype == ULFS1) {
1198 ulfs_extattr_uepm_init(&ump->um_extattr); 1259 ulfs_extattr_uepm_init(&ump->um_extattr);
1199 } 1260 }
1200#endif 1261#endif
1201 1262
1202#ifdef LFS_KERNEL_RFW 1263#ifdef LFS_KERNEL_RFW
1203 lfs_roll_forward(fs, mp, l); 1264 lfs_roll_forward(fs, mp, l);
1204#endif 1265#endif
1205 1266
1206 /* If writing, sb is not clean; record in case of immediate crash */ 1267 /* If writing, sb is not clean; record in case of immediate crash */
1207 if (!fs->lfs_ronly) { 1268 if (!fs->lfs_ronly) {
1208 lfs_sb_setpflags(fs, lfs_sb_getpflags(fs) & ~LFS_PF_CLEAN); 1269 lfs_sb_setpflags(fs, lfs_sb_getpflags(fs) & ~LFS_PF_CLEAN);
1209 lfs_writesuper(fs, lfs_sb_getsboff(fs, 0)); 1270 lfs_writesuper(fs, lfs_sb_getsboff(fs, 0));
1210 lfs_writesuper(fs, lfs_sb_getsboff(fs, 1)); 1271 lfs_writesuper(fs, lfs_sb_getsboff(fs, 1));
1211 } 1272 }
1212 1273
1213 /* Allow vget now that roll-forward is complete */ 1274 /* Allow vget now that roll-forward is complete */
1214 fs->lfs_flags &= ~(LFS_NOTYET); 1275 fs->lfs_flags &= ~(LFS_NOTYET);
1215 wakeup(&fs->lfs_flags); 1276 wakeup(&fs->lfs_flags);
1216 1277
1217 /* 1278 /*
1218 * Initialize the ifile cleaner info with information from 1279 * Initialize the ifile cleaner info with information from
1219 * the superblock. 1280 * the superblock.
1220 */ 1281 */
1221 LFS_CLEANERINFO(cip, fs, bp); 1282 {
1222 lfs_ci_setclean(fs, cip, lfs_sb_getnclean(fs)); 1283 struct buf *bp;
1223 lfs_ci_setdirty(fs, cip, lfs_sb_getnseg(fs) - lfs_sb_getnclean(fs)); 1284
1224 lfs_ci_setavail(fs, cip, lfs_sb_getavail(fs)); 1285 LFS_CLEANERINFO(cip, fs, bp);
1225 lfs_ci_setbfree(fs, cip, lfs_sb_getbfree(fs)); 1286 lfs_ci_setclean(fs, cip, lfs_sb_getnclean(fs));
1226 (void) LFS_BWRITE_LOG(bp); /* Ifile */ 1287 lfs_ci_setdirty(fs, cip, lfs_sb_getnseg(fs) - lfs_sb_getnclean(fs));
 1288 lfs_ci_setavail(fs, cip, lfs_sb_getavail(fs));
 1289 lfs_ci_setbfree(fs, cip, lfs_sb_getbfree(fs));
 1290 (void) LFS_BWRITE_LOG(bp); /* Ifile */
 1291 }
1227 1292
1228 /* 1293 /*
1229 * Mark the current segment as ACTIVE, since we're going to 1294 * Mark the current segment as ACTIVE, since we're going to
1230 * be writing to it. 1295 * be writing to it.
1231 */ 1296 */
1232 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, lfs_sb_getoffset(fs)), bp); 1297 {
1233 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE; 1298 struct buf *bp;
1234 fs->lfs_nactive++; 1299
1235 LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, lfs_sb_getoffset(fs)), bp); /* Ifile */ 1300 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, lfs_sb_getoffset(fs)), bp);
 1301 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
 1302 fs->lfs_nactive++;
 1303 LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, lfs_sb_getoffset(fs)), bp); /* Ifile */
 1304 }
1236 1305
1237 /* Now that roll-forward is done, unlock the Ifile */ 1306 /* Now that roll-forward is done, unlock the Ifile */
1238 vput(vp); 1307 vput(vp);
1239 1308
1240 /* Start the pagedaemon-anticipating daemon */ 1309 /* Start the pagedaemon-anticipating daemon */
1241 mutex_enter(&lfs_lock); 1310 mutex_enter(&lfs_lock);
1242 if (lfs_writer_daemon == 0 && lfs_writer_lid == 0 && 1311 if (lfs_writer_daemon == 0 && lfs_writer_lid == 0 &&
1243 kthread_create(PRI_BIO, 0, NULL, 1312 kthread_create(PRI_BIO, 0, NULL,
1244 lfs_writerd, NULL, NULL, "lfs_writer") != 0) 1313 lfs_writerd, NULL, NULL, "lfs_writer") != 0)
1245 panic("fork lfs_writer"); 1314 panic("fork lfs_writer");
1246 mutex_exit(&lfs_lock); 1315 mutex_exit(&lfs_lock);
1247 1316
1248 printf("WARNING: the log-structured file system is experimental\n" 1317 printf("WARNING: the log-structured file system is experimental\n"
1249 "WARNING: it may cause system crashes and/or corrupt data\n"); 1318 "WARNING: it may cause system crashes and/or corrupt data\n");
1250 1319
1251 return (0); 1320 return (0);
1252 1321
1253out: 1322out:
1254 if (bp) 1323 if (primarybuf)
1255 brelse(bp, 0); 1324 brelse(primarybuf, BC_INVAL);
1256 if (abp) 1325 if (altbuf)
1257 brelse(abp, 0); 1326 brelse(altbuf, BC_INVAL);
1258 if (ump) { 1327 if (ump) {
1259 kmem_free(ump->um_lfs, sizeof(struct lfs)); 1328 kmem_free(ump->um_lfs, sizeof(struct lfs));
1260 kmem_free(ump, sizeof(*ump)); 1329 kmem_free(ump, sizeof(*ump));
1261 mp->mnt_data = NULL; 1330 mp->mnt_data = NULL;
1262 } 1331 }
1263 1332
1264 return (error); 1333 return (error);
1265} 1334}
1266 1335
1267/* 1336/*
1268 * unmount system call 1337 * unmount system call
1269 */ 1338 */
1270int 1339int
1271lfs_unmount(struct mount *mp, int mntflags) 1340lfs_unmount(struct mount *mp, int mntflags)
1272{ 1341{
1273 struct lwp *l = curlwp; 1342 struct lwp *l = curlwp;
1274 struct ulfsmount *ump; 1343 struct ulfsmount *ump;
1275 struct lfs *fs; 1344 struct lfs *fs;
1276 int error, flags, ronly; 1345 int error, flags, ronly;
1277 vnode_t *vp; 1346 vnode_t *vp;
1278 1347
1279 flags = 0; 1348 flags = 0;
1280 if (mntflags & MNT_FORCE) 1349 if (mntflags & MNT_FORCE)
1281 flags |= FORCECLOSE; 1350 flags |= FORCECLOSE;
1282 1351
1283 ump = VFSTOULFS(mp); 1352 ump = VFSTOULFS(mp);
1284 fs = ump->um_lfs; 1353 fs = ump->um_lfs;
1285 1354
1286 /* Two checkpoints */ 1355 /* Two checkpoints */
1287 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC); 1356 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
1288 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC); 1357 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
1289 1358
1290 /* wake up the cleaner so it can die */ 1359 /* wake up the cleaner so it can die */
1291 /* XXX: shouldn't this be *after* the error cases below? */ 1360 /* XXX: shouldn't this be *after* the error cases below? */
1292 lfs_wakeup_cleaner(fs); 1361 lfs_wakeup_cleaner(fs);
1293 mutex_enter(&lfs_lock); 1362 mutex_enter(&lfs_lock);
1294 while (fs->lfs_sleepers) 1363 while (fs->lfs_sleepers)
1295 mtsleep(&fs->lfs_sleepers, PRIBIO + 1, "lfs_sleepers", 0, 1364 mtsleep(&fs->lfs_sleepers, PRIBIO + 1, "lfs_sleepers", 0,
1296 &lfs_lock); 1365 &lfs_lock);
1297 mutex_exit(&lfs_lock); 1366 mutex_exit(&lfs_lock);
1298 1367
1299#ifdef LFS_EXTATTR 1368#ifdef LFS_EXTATTR
1300 if (ump->um_fstype == ULFS1) { 1369 if (ump->um_fstype == ULFS1) {
1301 if (ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED) { 1370 if (ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED) {
1302 ulfs_extattr_stop(mp, curlwp); 1371 ulfs_extattr_stop(mp, curlwp);
1303 } 1372 }
1304 if (ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_INITIALIZED) { 1373 if (ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_INITIALIZED) {
1305 ulfs_extattr_uepm_destroy(&ump->um_extattr); 1374 ulfs_extattr_uepm_destroy(&ump->um_extattr);
1306 } 1375 }
1307 } 1376 }
1308#endif 1377#endif
1309#ifdef LFS_QUOTA 1378#ifdef LFS_QUOTA
1310 if ((error = lfsquota1_umount(mp, flags)) != 0) 1379 if ((error = lfsquota1_umount(mp, flags)) != 0)
1311 return (error); 1380 return (error);
1312#endif 1381#endif
1313#ifdef LFS_QUOTA2 1382#ifdef LFS_QUOTA2
1314 if ((error = lfsquota2_umount(mp, flags)) != 0) 1383 if ((error = lfsquota2_umount(mp, flags)) != 0)
1315 return (error); 1384 return (error);
1316#endif 1385#endif
1317 if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0) 1386 if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0)
1318 return (error); 1387 return (error);
1319 if ((error = VFS_SYNC(mp, 1, l->l_cred)) != 0) 1388 if ((error = VFS_SYNC(mp, 1, l->l_cred)) != 0)
1320 return (error); 1389 return (error);
1321 vp = fs->lfs_ivnode; 1390 vp = fs->lfs_ivnode;
1322 mutex_enter(vp->v_interlock); 1391 mutex_enter(vp->v_interlock);
1323 if (LIST_FIRST(&vp->v_dirtyblkhd)) 1392 if (LIST_FIRST(&vp->v_dirtyblkhd))
1324 panic("lfs_unmount: still dirty blocks on ifile vnode"); 1393 panic("lfs_unmount: still dirty blocks on ifile vnode");
1325 mutex_exit(vp->v_interlock); 1394 mutex_exit(vp->v_interlock);
1326 1395
1327 /* Explicitly write the superblock, to update serial and pflags */ 1396 /* Explicitly write the superblock, to update serial and pflags */
1328 lfs_sb_setpflags(fs, lfs_sb_getpflags(fs) | LFS_PF_CLEAN); 1397 lfs_sb_setpflags(fs, lfs_sb_getpflags(fs) | LFS_PF_CLEAN);
1329 lfs_writesuper(fs, lfs_sb_getsboff(fs, 0)); 1398 lfs_writesuper(fs, lfs_sb_getsboff(fs, 0));
1330 lfs_writesuper(fs, lfs_sb_getsboff(fs, 1)); 1399 lfs_writesuper(fs, lfs_sb_getsboff(fs, 1));
1331 mutex_enter(&lfs_lock); 1400 mutex_enter(&lfs_lock);
1332 while (fs->lfs_iocount) 1401 while (fs->lfs_iocount)
1333 mtsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs_umount", 0, 1402 mtsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs_umount", 0,
1334 &lfs_lock); 1403 &lfs_lock);
1335 mutex_exit(&lfs_lock); 1404 mutex_exit(&lfs_lock);
1336 1405
1337 /* Finish with the Ifile, now that we're done with it */ 1406 /* Finish with the Ifile, now that we're done with it */
1338 vgone(fs->lfs_ivnode); 1407 vgone(fs->lfs_ivnode);
1339 1408
1340 ronly = !fs->lfs_ronly; 1409 ronly = !fs->lfs_ronly;
1341 if (fs->lfs_devvp->v_type != VBAD) 1410 if (fs->lfs_devvp->v_type != VBAD)
1342 spec_node_setmountedfs(fs->lfs_devvp, NULL); 1411 spec_node_setmountedfs(fs->lfs_devvp, NULL);
1343 vn_lock(fs->lfs_devvp, LK_EXCLUSIVE | LK_RETRY); 1412 vn_lock(fs->lfs_devvp, LK_EXCLUSIVE | LK_RETRY);
1344 error = VOP_CLOSE(fs->lfs_devvp, 1413 error = VOP_CLOSE(fs->lfs_devvp,
1345 ronly ? FREAD : FREAD|FWRITE, NOCRED); 1414 ronly ? FREAD : FREAD|FWRITE, NOCRED);
1346 vput(fs->lfs_devvp); 1415 vput(fs->lfs_devvp);
1347 1416
1348 /* Complain about page leakage */ 1417 /* Complain about page leakage */
1349 if (fs->lfs_pages > 0) 1418 if (fs->lfs_pages > 0)
1350 printf("lfs_unmount: still claim %d pages (%d in subsystem)\n", 1419 printf("lfs_unmount: still claim %d pages (%d in subsystem)\n",
1351 fs->lfs_pages, lfs_subsys_pages); 1420 fs->lfs_pages, lfs_subsys_pages);
1352 1421
1353 /* Free per-mount data structures */ 1422 /* Free per-mount data structures */
1354 free(fs->lfs_ino_bitmap, M_SEGMENT); 1423 free(fs->lfs_ino_bitmap, M_SEGMENT);
1355 free(fs->lfs_suflags[0], M_SEGMENT); 1424 free(fs->lfs_suflags[0], M_SEGMENT);
1356 free(fs->lfs_suflags[1], M_SEGMENT); 1425 free(fs->lfs_suflags[1], M_SEGMENT);
1357 free(fs->lfs_suflags, M_SEGMENT); 1426 free(fs->lfs_suflags, M_SEGMENT);
1358 lfs_free_resblks(fs); 1427 lfs_free_resblks(fs);
1359 cv_destroy(&fs->lfs_stopcv); 1428 cv_destroy(&fs->lfs_stopcv);
1360 rw_destroy(&fs->lfs_fraglock); 1429 rw_destroy(&fs->lfs_fraglock);
1361 rw_destroy(&fs->lfs_iflock); 1430 rw_destroy(&fs->lfs_iflock);
1362 1431
1363 kmem_free(fs, sizeof(struct lfs)); 1432 kmem_free(fs, sizeof(struct lfs));
1364 kmem_free(ump, sizeof(*ump)); 1433 kmem_free(ump, sizeof(*ump));
1365 1434
1366 mp->mnt_data = NULL; 1435 mp->mnt_data = NULL;
1367 mp->mnt_flag &= ~MNT_LOCAL; 1436 mp->mnt_flag &= ~MNT_LOCAL;
1368 return (error); 1437 return (error);
1369} 1438}
1370 1439
1371/* 1440/*
1372 * Get file system statistics. 1441 * Get file system statistics.
1373 * 1442 *
1374 * NB: We don't lock to access the superblock here, because it's not 1443 * NB: We don't lock to access the superblock here, because it's not
1375 * really that important if we get it wrong. 1444 * really that important if we get it wrong.
1376 */ 1445 */
1377int 1446int
1378lfs_statvfs(struct mount *mp, struct statvfs *sbp) 1447lfs_statvfs(struct mount *mp, struct statvfs *sbp)
1379{ 1448{
1380 struct lfs *fs; 1449 struct lfs *fs;
1381 struct ulfsmount *ump; 1450 struct ulfsmount *ump;
1382 1451
1383 ump = VFSTOULFS(mp); 1452 ump = VFSTOULFS(mp);
1384 fs = ump->um_lfs; 1453 fs = ump->um_lfs;
1385 1454
1386 sbp->f_bsize = lfs_sb_getbsize(fs); 1455 sbp->f_bsize = lfs_sb_getbsize(fs);
1387 sbp->f_frsize = lfs_sb_getfsize(fs); 1456 sbp->f_frsize = lfs_sb_getfsize(fs);
1388 sbp->f_iosize = lfs_sb_getbsize(fs); 1457 sbp->f_iosize = lfs_sb_getbsize(fs);
1389 sbp->f_blocks = LFS_EST_NONMETA(fs) - VTOI(fs->lfs_ivnode)->i_lfs_effnblks; 1458 sbp->f_blocks = LFS_EST_NONMETA(fs) - VTOI(fs->lfs_ivnode)->i_lfs_effnblks;
1390 1459
1391 sbp->f_bfree = LFS_EST_BFREE(fs); 1460 sbp->f_bfree = LFS_EST_BFREE(fs);
1392 /* 1461 /*
1393 * XXX this should be lfs_sb_getsize (measured in frags) 1462 * XXX this should be lfs_sb_getsize (measured in frags)
1394 * rather than dsize (measured in diskblocks). However, 1463 * rather than dsize (measured in diskblocks). However,
1395 * getsize needs a format version check (for version 1 it 1464 * getsize needs a format version check (for version 1 it
1396 * needs to be blockstofrags'd) so for the moment I'm going to 1465 * needs to be blockstofrags'd) so for the moment I'm going to
1397 * leave this... it won't fire wrongly as frags are at least 1466 * leave this... it won't fire wrongly as frags are at least
1398 * as big as diskblocks. 1467 * as big as diskblocks.
1399 */ 1468 */
1400 KASSERT(sbp->f_bfree <= lfs_sb_getdsize(fs)); 1469 KASSERT(sbp->f_bfree <= lfs_sb_getdsize(fs));
1401#if 0 1470#if 0
1402 if (sbp->f_bfree < 0) 1471 if (sbp->f_bfree < 0)
1403 sbp->f_bfree = 0; 1472 sbp->f_bfree = 0;
1404#endif 1473#endif
1405 1474
1406 sbp->f_bresvd = LFS_EST_RSVD(fs); 1475 sbp->f_bresvd = LFS_EST_RSVD(fs);
1407 if (sbp->f_bfree > sbp->f_bresvd) 1476 if (sbp->f_bfree > sbp->f_bresvd)
1408 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd; 1477 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
1409 else 1478 else
1410 sbp->f_bavail = 0; 1479 sbp->f_bavail = 0;
1411 1480
1412 /* XXX: huh? - dholland 20150728 */ 1481 /* XXX: huh? - dholland 20150728 */
1413 sbp->f_files = lfs_sb_getbfree(fs) / lfs_btofsb(fs, lfs_sb_getibsize(fs)) 1482 sbp->f_files = lfs_sb_getbfree(fs) / lfs_btofsb(fs, lfs_sb_getibsize(fs))
1414 * LFS_INOPB(fs); 1483 * LFS_INOPB(fs);
1415 sbp->f_ffree = sbp->f_files - lfs_sb_getnfiles(fs); 1484 sbp->f_ffree = sbp->f_files - lfs_sb_getnfiles(fs);
1416 sbp->f_favail = sbp->f_ffree; 1485 sbp->f_favail = sbp->f_ffree;
1417 sbp->f_fresvd = 0; 1486 sbp->f_fresvd = 0;
1418 copy_statvfs_info(sbp, mp); 1487 copy_statvfs_info(sbp, mp);
1419 return (0); 1488 return (0);
1420} 1489}
1421 1490
1422/* 1491/*
1423 * Go through the disk queues to initiate sandbagged IO; 1492 * Go through the disk queues to initiate sandbagged IO;
1424 * go through the inodes to write those that have been modified; 1493 * go through the inodes to write those that have been modified;
1425 * initiate the writing of the super block if it has been modified. 1494 * initiate the writing of the super block if it has been modified.
1426 * 1495 *
1427 * Note: we are always called with the filesystem marked `MPBUSY'. 1496 * Note: we are always called with the filesystem marked `MPBUSY'.
1428 */ 1497 */
1429int 1498int
1430lfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred) 1499lfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
1431{ 1500{
1432 int error; 1501 int error;
1433 struct lfs *fs; 1502 struct lfs *fs;
1434 1503
1435 fs = VFSTOULFS(mp)->um_lfs; 1504 fs = VFSTOULFS(mp)->um_lfs;
1436 if (fs->lfs_ronly) 1505 if (fs->lfs_ronly)
1437 return 0; 1506 return 0;
1438 1507
1439 /* Snapshots should not hose the syncer */ 1508 /* Snapshots should not hose the syncer */
1440 /* 1509 /*
1441 * XXX Sync can block here anyway, since we don't have a very 1510 * XXX Sync can block here anyway, since we don't have a very
1442 * XXX good idea of how much data is pending. If it's more 1511 * XXX good idea of how much data is pending. If it's more
1443 * XXX than a segment and lfs_nextseg is close to the end of 1512 * XXX than a segment and lfs_nextseg is close to the end of
1444 * XXX the log, we'll likely block. 1513 * XXX the log, we'll likely block.
1445 */ 1514 */
1446 mutex_enter(&lfs_lock); 1515 mutex_enter(&lfs_lock);
1447 if (fs->lfs_nowrap && lfs_sb_getnextseg(fs) < lfs_sb_getcurseg(fs)) { 1516 if (fs->lfs_nowrap && lfs_sb_getnextseg(fs) < lfs_sb_getcurseg(fs)) {
1448 mutex_exit(&lfs_lock); 1517 mutex_exit(&lfs_lock);
1449 return 0; 1518 return 0;
1450 } 1519 }
1451 mutex_exit(&lfs_lock); 1520 mutex_exit(&lfs_lock);
1452 1521
1453 lfs_writer_enter(fs, "lfs_dirops"); 1522 lfs_writer_enter(fs, "lfs_dirops");
1454 1523
1455 /* All syncs must be checkpoints until roll-forward is implemented. */ 1524 /* All syncs must be checkpoints until roll-forward is implemented. */
1456 DLOG((DLOG_FLUSH, "lfs_sync at 0x%jx\n", 1525 DLOG((DLOG_FLUSH, "lfs_sync at 0x%jx\n",
1457 (uintmax_t)lfs_sb_getoffset(fs))); 1526 (uintmax_t)lfs_sb_getoffset(fs)));
1458 error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0)); 1527 error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
1459 lfs_writer_leave(fs); 1528 lfs_writer_leave(fs);
1460#ifdef LFS_QUOTA 1529#ifdef LFS_QUOTA
1461 lfs_qsync(mp); 1530 lfs_qsync(mp);
1462#endif 1531#endif
1463 return (error); 1532 return (error);
1464} 1533}
1465 1534
1466/* 1535/*
1467 * Look up an LFS dinode number to find its incore vnode. If not already 1536 * Look up an LFS dinode number to find its incore vnode. If not already
1468 * in core, read it in from the specified device. Return the inode locked. 1537 * in core, read it in from the specified device. Return the inode locked.
1469 * Detection and handling of mount points must be done by the calling routine. 1538 * Detection and handling of mount points must be done by the calling routine.
1470 */ 1539 */
1471int 1540int
1472lfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp) 1541lfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1473{ 1542{
1474 int error; 1543 int error;
1475 1544
1476 error = vcache_get(mp, &ino, sizeof(ino), vpp); 1545 error = vcache_get(mp, &ino, sizeof(ino), vpp);
1477 if (error) 1546 if (error)
1478 return error; 1547 return error;
1479 error = vn_lock(*vpp, LK_EXCLUSIVE); 1548 error = vn_lock(*vpp, LK_EXCLUSIVE);
1480 if (error) { 1549 if (error) {
1481 vrele(*vpp); 1550 vrele(*vpp);
1482 *vpp = NULL; 1551 *vpp = NULL;
1483 return error; 1552 return error;
1484 } 1553 }
1485 1554
1486 return 0; 1555 return 0;
1487} 1556}
1488 1557
1489/* 1558/*
1490 * Create a new vnode/inode pair and initialize what fields we can. 1559 * Create a new vnode/inode pair and initialize what fields we can.
1491 */ 1560 */
1492static void 1561static void
1493lfs_init_vnode(struct ulfsmount *ump, ino_t ino, struct vnode *vp) 1562lfs_init_vnode(struct ulfsmount *ump, ino_t ino, struct vnode *vp)
1494{ 1563{
1495 struct lfs *fs = ump->um_lfs; 1564 struct lfs *fs = ump->um_lfs;
1496 struct inode *ip; 1565 struct inode *ip;
1497 union lfs_dinode *dp; 1566 union lfs_dinode *dp;
1498 1567
1499 ASSERT_NO_SEGLOCK(fs); 1568 ASSERT_NO_SEGLOCK(fs);
1500 1569
1501 /* Initialize the inode. */ 1570 /* Initialize the inode. */
1502 ip = pool_get(&lfs_inode_pool, PR_WAITOK); 1571 ip = pool_get(&lfs_inode_pool, PR_WAITOK);
1503 memset(ip, 0, sizeof(*ip)); 1572 memset(ip, 0, sizeof(*ip));
1504 dp = pool_get(&lfs_dinode_pool, PR_WAITOK); 1573 dp = pool_get(&lfs_dinode_pool, PR_WAITOK);
1505 memset(dp, 0, sizeof(*dp)); 1574 memset(dp, 0, sizeof(*dp));
1506 ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK); 1575 ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK);
1507 memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs)); 1576 memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs));
1508 ip->i_din = dp; 1577 ip->i_din = dp;
1509 ip->i_ump = ump; 1578 ip->i_ump = ump;
1510 ip->i_vnode = vp; 1579 ip->i_vnode = vp;
1511 ip->i_dev = fs->lfs_dev; 1580 ip->i_dev = fs->lfs_dev;
1512 lfs_dino_setinumber(fs, dp, ino); 1581 lfs_dino_setinumber(fs, dp, ino);
1513 ip->i_number = ino; 1582 ip->i_number = ino;
1514 ip->i_lfs = fs; 1583 ip->i_lfs = fs;
1515 ip->i_lfs_effnblks = 0; 1584 ip->i_lfs_effnblks = 0;
1516 SPLAY_INIT(&ip->i_lfs_lbtree); 1585 SPLAY_INIT(&ip->i_lfs_lbtree);
1517 ip->i_lfs_nbtree = 0; 1586 ip->i_lfs_nbtree = 0;
1518 LIST_INIT(&ip->i_lfs_segdhd); 1587 LIST_INIT(&ip->i_lfs_segdhd);
1519 1588
1520 vp->v_tag = VT_LFS; 1589 vp->v_tag = VT_LFS;
1521 vp->v_op = lfs_vnodeop_p; 1590 vp->v_op = lfs_vnodeop_p;
1522 vp->v_data = ip; 1591 vp->v_data = ip;
1523} 1592}
1524 1593
1525/* 1594/*
1526 * Undo lfs_init_vnode(). 1595 * Undo lfs_init_vnode().
1527 */ 1596 */
1528static void 1597static void
1529lfs_deinit_vnode(struct ulfsmount *ump, struct vnode *vp) 1598lfs_deinit_vnode(struct ulfsmount *ump, struct vnode *vp)
1530{ 1599{
1531 struct inode *ip = VTOI(vp); 1600 struct inode *ip = VTOI(vp);
1532 1601
1533 pool_put(&lfs_inoext_pool, ip->inode_ext.lfs); 1602 pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
1534 pool_put(&lfs_dinode_pool, ip->i_din); 1603 pool_put(&lfs_dinode_pool, ip->i_din);
1535 pool_put(&lfs_inode_pool, ip); 1604 pool_put(&lfs_inode_pool, ip);
1536 vp->v_data = NULL; 1605 vp->v_data = NULL;
1537} 1606}
1538 1607
1539/* 1608/*
1540 * Read an inode from disk and initialize this vnode / inode pair. 1609 * Read an inode from disk and initialize this vnode / inode pair.
1541 * Caller assures no other thread will try to load this inode. 1610 * Caller assures no other thread will try to load this inode.
1542 */ 1611 */
1543int 1612int
1544lfs_loadvnode(struct mount *mp, struct vnode *vp, 1613lfs_loadvnode(struct mount *mp, struct vnode *vp,
1545 const void *key, size_t key_len, const void **new_key) 1614 const void *key, size_t key_len, const void **new_key)
1546{ 1615{
1547 struct lfs *fs; 1616 struct lfs *fs;
1548 union lfs_dinode *dip; 1617 union lfs_dinode *dip;
1549 struct inode *ip; 1618 struct inode *ip;
1550 struct buf *bp; 1619 struct buf *bp;
1551 IFILE *ifp; 1620 IFILE *ifp;
1552 struct ulfsmount *ump; 1621 struct ulfsmount *ump;
1553 ino_t ino; 1622 ino_t ino;
1554 daddr_t daddr; 1623 daddr_t daddr;
1555 int error, retries; 1624 int error, retries;
1556 struct timespec ts; 1625 struct timespec ts;
1557 1626
1558 KASSERT(key_len == sizeof(ino)); 1627 KASSERT(key_len == sizeof(ino));
1559 memcpy(&ino, key, key_len); 1628 memcpy(&ino, key, key_len);
1560 1629
1561 memset(&ts, 0, sizeof ts); /* XXX gcc */ 1630 memset(&ts, 0, sizeof ts); /* XXX gcc */
1562 1631
1563 ump = VFSTOULFS(mp); 1632 ump = VFSTOULFS(mp);
1564 fs = ump->um_lfs; 1633 fs = ump->um_lfs;
1565 1634
1566 /* 1635 /*
1567 * If the filesystem is not completely mounted yet, suspend 1636 * If the filesystem is not completely mounted yet, suspend
1568 * any access requests (wait for roll-forward to complete). 1637 * any access requests (wait for roll-forward to complete).
1569 */ 1638 */
1570 mutex_enter(&lfs_lock); 1639 mutex_enter(&lfs_lock);
1571 while ((fs->lfs_flags & LFS_NOTYET) && curproc->p_pid != fs->lfs_rfpid) 1640 while ((fs->lfs_flags & LFS_NOTYET) && curproc->p_pid != fs->lfs_rfpid)
1572 mtsleep(&fs->lfs_flags, PRIBIO+1, "lfs_notyet", 0, 1641 mtsleep(&fs->lfs_flags, PRIBIO+1, "lfs_notyet", 0,
1573 &lfs_lock); 1642 &lfs_lock);
1574 mutex_exit(&lfs_lock); 1643 mutex_exit(&lfs_lock);
1575 1644
1576 /* Translate the inode number to a disk address. */ 1645 /* Translate the inode number to a disk address. */
1577 if (ino == LFS_IFILE_INUM) 1646 if (ino == LFS_IFILE_INUM)
1578 daddr = lfs_sb_getidaddr(fs); 1647 daddr = lfs_sb_getidaddr(fs);
1579 else { 1648 else {
1580 /* XXX bounds-check this too */ 1649 /* XXX bounds-check this too */
1581 LFS_IENTRY(ifp, fs, ino, bp); 1650 LFS_IENTRY(ifp, fs, ino, bp);
1582 daddr = lfs_if_getdaddr(fs, ifp); 1651 daddr = lfs_if_getdaddr(fs, ifp);
1583 if (lfs_sb_getversion(fs) > 1) { 1652 if (lfs_sb_getversion(fs) > 1) {
1584 ts.tv_sec = lfs_if_getatime_sec(fs, ifp); 1653 ts.tv_sec = lfs_if_getatime_sec(fs, ifp);
1585 ts.tv_nsec = lfs_if_getatime_nsec(fs, ifp); 1654 ts.tv_nsec = lfs_if_getatime_nsec(fs, ifp);
1586 } 1655 }
1587 1656
1588 brelse(bp, 0); 1657 brelse(bp, 0);
1589 if (daddr == LFS_UNUSED_DADDR) 1658 if (daddr == LFS_UNUSED_DADDR)
1590 return (ENOENT); 1659 return (ENOENT);
1591 } 1660 }
1592 1661
1593 /* Allocate/init new vnode/inode. */ 1662 /* Allocate/init new vnode/inode. */
1594 lfs_init_vnode(ump, ino, vp); 1663 lfs_init_vnode(ump, ino, vp);
1595 ip = VTOI(vp); 1664 ip = VTOI(vp);
1596 1665
1597 /* If the cleaner supplied the inode, use it. */ 1666 /* If the cleaner supplied the inode, use it. */
1598 if (curlwp == fs->lfs_cleaner_thread && fs->lfs_cleaner_hint != NULL && 1667 if (curlwp == fs->lfs_cleaner_thread && fs->lfs_cleaner_hint != NULL &&
1599 fs->lfs_cleaner_hint->bi_lbn == LFS_UNUSED_LBN) { 1668 fs->lfs_cleaner_hint->bi_lbn == LFS_UNUSED_LBN) {
1600 dip = fs->lfs_cleaner_hint->bi_bp; 1669 dip = fs->lfs_cleaner_hint->bi_bp;
1601 if (fs->lfs_is64) { 1670 if (fs->lfs_is64) {
1602 error = copyin(dip, &ip->i_din->u_64, 1671 error = copyin(dip, &ip->i_din->u_64,
1603 sizeof(struct lfs64_dinode)); 1672 sizeof(struct lfs64_dinode));
1604 } else { 1673 } else {
1605 error = copyin(dip, &ip->i_din->u_32, 1674 error = copyin(dip, &ip->i_din->u_32,
1606 sizeof(struct lfs32_dinode)); 1675 sizeof(struct lfs32_dinode));
1607 } 1676 }
1608 if (error) { 1677 if (error) {
1609 lfs_deinit_vnode(ump, vp); 1678 lfs_deinit_vnode(ump, vp);
1610 return error; 1679 return error;
1611 } 1680 }
1612 KASSERT(ip->i_number == ino); 1681 KASSERT(ip->i_number == ino);
1613 goto out; 1682 goto out;
1614 } 1683 }
1615 1684
1616 /* Read in the disk contents for the inode, copy into the inode. */ 1685 /* Read in the disk contents for the inode, copy into the inode. */
1617 retries = 0; 1686 retries = 0;
1618again: 1687again:
1619 error = bread(fs->lfs_devvp, LFS_FSBTODB(fs, daddr), 1688 error = bread(fs->lfs_devvp, LFS_FSBTODB(fs, daddr),
1620 (lfs_sb_getversion(fs) == 1 ? lfs_sb_getbsize(fs) : lfs_sb_getibsize(fs)), 1689 (lfs_sb_getversion(fs) == 1 ? lfs_sb_getbsize(fs) : lfs_sb_getibsize(fs)),
1621 0, &bp); 1690 0, &bp);
1622 if (error) { 1691 if (error) {
1623 lfs_deinit_vnode(ump, vp); 1692 lfs_deinit_vnode(ump, vp);
1624 return error; 1693 return error;
1625 } 1694 }
1626 1695
1627 dip = lfs_ifind(fs, ino, bp); 1696 dip = lfs_ifind(fs, ino, bp);
1628 if (dip == NULL) { 1697 if (dip == NULL) {
1629 /* Assume write has not completed yet; try again */ 1698 /* Assume write has not completed yet; try again */
1630 brelse(bp, BC_INVAL); 1699 brelse(bp, BC_INVAL);
1631 ++retries; 1700 ++retries;
1632 if (retries <= LFS_IFIND_RETRIES) { 1701 if (retries <= LFS_IFIND_RETRIES) {
1633 mutex_enter(&lfs_lock); 1702 mutex_enter(&lfs_lock);
1634 if (fs->lfs_iocount) { 1703 if (fs->lfs_iocount) {
1635 DLOG((DLOG_VNODE, 1704 DLOG((DLOG_VNODE,
1636 "%s: dinode %d not found, retrying...\n", 1705 "%s: dinode %d not found, retrying...\n",
1637 __func__, ino)); 1706 __func__, ino));
1638 (void)mtsleep(&fs->lfs_iocount, PRIBIO + 1, 1707 (void)mtsleep(&fs->lfs_iocount, PRIBIO + 1,
1639 "lfs ifind", 1, &lfs_lock); 1708 "lfs ifind", 1, &lfs_lock);
1640 } else 1709 } else
1641 retries = LFS_IFIND_RETRIES; 1710 retries = LFS_IFIND_RETRIES;
1642 mutex_exit(&lfs_lock); 1711 mutex_exit(&lfs_lock);
1643 goto again; 1712 goto again;
1644 } 1713 }
1645#ifdef DEBUG 1714#ifdef DEBUG
1646 /* If the seglock is held look at the bpp to see 1715 /* If the seglock is held look at the bpp to see
1647 what is there anyway */ 1716 what is there anyway */
1648 mutex_enter(&lfs_lock); 1717 mutex_enter(&lfs_lock);
1649 if (fs->lfs_seglock > 0) { 1718 if (fs->lfs_seglock > 0) {
1650 struct buf **bpp; 1719 struct buf **bpp;
1651 union lfs_dinode *dp; 1720 union lfs_dinode *dp;
1652 int i; 1721 int i;
1653 1722
1654 for (bpp = fs->lfs_sp->bpp; 1723 for (bpp = fs->lfs_sp->bpp;
1655 bpp != fs->lfs_sp->cbpp; ++bpp) { 1724 bpp != fs->lfs_sp->cbpp; ++bpp) {
1656 if ((*bpp)->b_vp == fs->lfs_ivnode && 1725 if ((*bpp)->b_vp == fs->lfs_ivnode &&
1657 bpp != fs->lfs_sp->bpp) { 1726 bpp != fs->lfs_sp->bpp) {
1658 /* Inode block */ 1727 /* Inode block */
1659 printf("%s: block 0x%" PRIx64 ": ", 1728 printf("%s: block 0x%" PRIx64 ": ",
1660 __func__, (*bpp)->b_blkno); 1729 __func__, (*bpp)->b_blkno);
1661 for (i = 0; i < LFS_INOPB(fs); i++) { 1730 for (i = 0; i < LFS_INOPB(fs); i++) {
1662 dp = DINO_IN_BLOCK(fs, 1731 dp = DINO_IN_BLOCK(fs,
1663 (*bpp)->b_data, i); 1732 (*bpp)->b_data, i);
1664 if (lfs_dino_getinumber(fs, dp)) 1733 if (lfs_dino_getinumber(fs, dp))
1665 printf("%ju ", 1734 printf("%ju ",
1666 (uintmax_t)lfs_dino_getinumber(fs, dp)); 1735 (uintmax_t)lfs_dino_getinumber(fs, dp));
1667 } 1736 }
1668 printf("\n"); 1737 printf("\n");
1669 } 1738 }
1670 } 1739 }
1671 } 1740 }
1672 mutex_exit(&lfs_lock); 1741 mutex_exit(&lfs_lock);
1673#endif /* DEBUG */ 1742#endif /* DEBUG */
1674 panic("lfs_loadvnode: dinode not found"); 1743 panic("lfs_loadvnode: dinode not found");
1675 } 1744 }
1676 lfs_copy_dinode(fs, ip->i_din, dip); 1745 lfs_copy_dinode(fs, ip->i_din, dip);
1677 brelse(bp, 0); 1746 brelse(bp, 0);
1678 1747
1679out:  1748out:
1680 if (lfs_sb_getversion(fs) > 1) { 1749 if (lfs_sb_getversion(fs) > 1) {
1681 lfs_dino_setatime(fs, ip->i_din, ts.tv_sec); 1750 lfs_dino_setatime(fs, ip->i_din, ts.tv_sec);
1682 lfs_dino_setatimensec(fs, ip->i_din, ts.tv_nsec); 1751 lfs_dino_setatimensec(fs, ip->i_din, ts.tv_nsec);
1683 } 1752 }
1684 1753
1685 lfs_vinit(mp, &vp); 1754 lfs_vinit(mp, &vp);
1686 1755
1687 *new_key = &ip->i_number; 1756 *new_key = &ip->i_number;
1688 return 0; 1757 return 0;
1689} 1758}
1690 1759
1691/* 1760/*
1692 * Create a new inode and initialize this vnode / inode pair. 1761 * Create a new inode and initialize this vnode / inode pair.
1693 */ 1762 */
1694int 1763int
1695lfs_newvnode(struct mount *mp, struct vnode *dvp, struct vnode *vp, 1764lfs_newvnode(struct mount *mp, struct vnode *dvp, struct vnode *vp,
1696 struct vattr *vap, kauth_cred_t cred, 1765 struct vattr *vap, kauth_cred_t cred,
1697 size_t *key_len, const void **new_key) 1766 size_t *key_len, const void **new_key)
1698{ 1767{
1699 ino_t ino; 1768 ino_t ino;
1700 struct inode *ip; 1769 struct inode *ip;
1701 struct ulfsmount *ump; 1770 struct ulfsmount *ump;
1702 struct lfs *fs; 1771 struct lfs *fs;
1703 int error, mode, gen; 1772 int error, mode, gen;
1704 1773
1705 KASSERT(dvp != NULL || vap->va_fileid > 0); 1774 KASSERT(dvp != NULL || vap->va_fileid > 0);
1706 KASSERT(dvp != NULL && dvp->v_mount == mp); 1775 KASSERT(dvp != NULL && dvp->v_mount == mp);
1707 KASSERT(vap->va_type != VNON); 1776 KASSERT(vap->va_type != VNON);
1708 1777
1709 *key_len = sizeof(ino); 1778 *key_len = sizeof(ino);
1710 ump = VFSTOULFS(mp); 1779 ump = VFSTOULFS(mp);
1711 fs = ump->um_lfs; 1780 fs = ump->um_lfs;
1712 mode = MAKEIMODE(vap->va_type, vap->va_mode); 1781 mode = MAKEIMODE(vap->va_type, vap->va_mode);
1713 1782
1714 /* 1783 /*
1715 * Allocate fresh inode. With "dvp == NULL" take the inode number 1784 * Allocate fresh inode. With "dvp == NULL" take the inode number
1716 * and version from "vap". 1785 * and version from "vap".
1717 */ 1786 */
1718 if (dvp == NULL) { 1787 if (dvp == NULL) {
1719 ino = vap->va_fileid; 1788 ino = vap->va_fileid;
1720 gen = vap->va_gen; 1789 gen = vap->va_gen;
1721 error = lfs_valloc_fixed(fs, ino, gen); 1790 error = lfs_valloc_fixed(fs, ino, gen);
1722 } else { 1791 } else {
1723 error = lfs_valloc(dvp, mode, cred, &ino, &gen); 1792 error = lfs_valloc(dvp, mode, cred, &ino, &gen);
1724 } 1793 }
1725 if (error) 1794 if (error)
1726 return error; 1795 return error;
1727 1796
1728 /* Attach inode to vnode. */ 1797 /* Attach inode to vnode. */
1729 lfs_init_vnode(ump, ino, vp); 1798 lfs_init_vnode(ump, ino, vp);
1730 ip = VTOI(vp); 1799 ip = VTOI(vp);
1731 1800
1732 mutex_enter(&lfs_lock); 1801 mutex_enter(&lfs_lock);
1733 LFS_SET_UINO(ip, IN_CHANGE); 1802 LFS_SET_UINO(ip, IN_CHANGE);
1734 mutex_exit(&lfs_lock); 1803 mutex_exit(&lfs_lock);
1735 1804
1736 /* Note no blocks yet */ 1805 /* Note no blocks yet */
1737 ip->i_lfs_hiblk = -1; 1806 ip->i_lfs_hiblk = -1;
1738 1807
1739 /* Set a new generation number for this inode. */ 1808 /* Set a new generation number for this inode. */
1740 ip->i_gen = gen; 1809 ip->i_gen = gen;
1741 lfs_dino_setgen(fs, ip->i_din, gen); 1810 lfs_dino_setgen(fs, ip->i_din, gen);
1742 1811
1743 memset(ip->i_lfs_fragsize, 0, 1812 memset(ip->i_lfs_fragsize, 0,
1744 ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize)); 1813 ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize));
1745 1814
1746 /* Set uid / gid. */ 1815 /* Set uid / gid. */
1747 if (cred == NOCRED || cred == FSCRED) { 1816 if (cred == NOCRED || cred == FSCRED) {
1748 ip->i_gid = 0; 1817 ip->i_gid = 0;
1749 ip->i_uid = 0; 1818 ip->i_uid = 0;
1750 } else { 1819 } else {
1751 ip->i_gid = VTOI(dvp)->i_gid; 1820 ip->i_gid = VTOI(dvp)->i_gid;
1752 ip->i_uid = kauth_cred_geteuid(cred); 1821 ip->i_uid = kauth_cred_geteuid(cred);
1753 } 1822 }
1754 DIP_ASSIGN(ip, gid, ip->i_gid); 1823 DIP_ASSIGN(ip, gid, ip->i_gid);
1755 DIP_ASSIGN(ip, uid, ip->i_uid); 1824 DIP_ASSIGN(ip, uid, ip->i_uid);
1756 1825
1757#if defined(LFS_QUOTA) || defined(LFS_QUOTA2) 1826#if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
1758 error = lfs_chkiq(ip, 1, cred, 0); 1827 error = lfs_chkiq(ip, 1, cred, 0);
1759 if (error) { 1828 if (error) {
1760 lfs_vfree(dvp, ino, mode); 1829 lfs_vfree(dvp, ino, mode);
1761 lfs_deinit_vnode(ump, vp); 1830 lfs_deinit_vnode(ump, vp);
1762 1831
1763 return error; 1832 return error;
1764 } 1833 }
1765#endif 1834#endif
1766 1835
1767 /* Set type and finalize. */ 1836 /* Set type and finalize. */
1768 ip->i_flags = 0; 1837 ip->i_flags = 0;
1769 DIP_ASSIGN(ip, flags, 0); 1838 DIP_ASSIGN(ip, flags, 0);
1770 ip->i_mode = mode; 1839 ip->i_mode = mode;
1771 DIP_ASSIGN(ip, mode, mode); 1840 DIP_ASSIGN(ip, mode, mode);
1772 if (vap->va_rdev != VNOVAL) { 1841 if (vap->va_rdev != VNOVAL) {
1773 /* 1842 /*
1774 * Want to be able to use this to make badblock 1843 * Want to be able to use this to make badblock
1775 * inodes, so don't truncate the dev number. 1844 * inodes, so don't truncate the dev number.
1776 */ 1845 */
1777 // XXX clean this up 1846 // XXX clean this up
1778 if (ump->um_fstype == ULFS1) 1847 if (ump->um_fstype == ULFS1)
1779 ip->i_din->u_32.di_rdev = ulfs_rw32(vap->va_rdev, 1848 ip->i_din->u_32.di_rdev = ulfs_rw32(vap->va_rdev,
1780 ULFS_MPNEEDSWAP(fs)); 1849 ULFS_MPNEEDSWAP(fs));
1781 else 1850 else
1782 ip->i_din->u_64.di_rdev = ulfs_rw64(vap->va_rdev, 1851 ip->i_din->u_64.di_rdev = ulfs_rw64(vap->va_rdev,
1783 ULFS_MPNEEDSWAP(fs)); 1852 ULFS_MPNEEDSWAP(fs));
1784 } 1853 }
1785 lfs_vinit(mp, &vp); 1854 lfs_vinit(mp, &vp);
1786 1855
1787 *new_key = &ip->i_number; 1856 *new_key = &ip->i_number;
1788 return 0; 1857 return 0;
1789} 1858}
1790 1859
1791/* 1860/*
1792 * File handle to vnode 1861 * File handle to vnode
1793 */ 1862 */
1794int 1863int
1795lfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 1864lfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1796{ 1865{
1797 struct lfid lfh; 1866 struct lfid lfh;
1798 struct lfs *fs; 1867 struct lfs *fs;
1799 1868
1800 if (fhp->fid_len != sizeof(struct lfid)) 1869 if (fhp->fid_len != sizeof(struct lfid))
1801 return EINVAL; 1870 return EINVAL;
1802 1871
1803 memcpy(&lfh, fhp, sizeof(lfh)); 1872 memcpy(&lfh, fhp, sizeof(lfh));
1804 if (lfh.lfid_ino < LFS_IFILE_INUM) 1873 if (lfh.lfid_ino < LFS_IFILE_INUM)
1805 return ESTALE; 1874 return ESTALE;
1806 1875
1807 fs = VFSTOULFS(mp)->um_lfs; 1876 fs = VFSTOULFS(mp)->um_lfs;
1808 if (lfh.lfid_ident != lfs_sb_getident(fs)) 1877 if (lfh.lfid_ident != lfs_sb_getident(fs))
1809 return ESTALE; 1878 return ESTALE;
1810 1879
1811 if (lfh.lfid_ino > 1880 if (lfh.lfid_ino >
1812 ((lfs_dino_getsize(fs, VTOI(fs->lfs_ivnode)->i_din) >> lfs_sb_getbshift(fs)) - 1881 ((lfs_dino_getsize(fs, VTOI(fs->lfs_ivnode)->i_din) >> lfs_sb_getbshift(fs)) -
1813 lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs)) 1882 lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs))
1814 return ESTALE; 1883 return ESTALE;
1815 1884
1816 return (ulfs_fhtovp(mp, &lfh.lfid_ufid, vpp)); 1885 return (ulfs_fhtovp(mp, &lfh.lfid_ufid, vpp));
1817} 1886}
1818 1887
1819/* 1888/*
1820 * Vnode pointer to File handle 1889 * Vnode pointer to File handle
1821 */ 1890 */
1822/* ARGSUSED */ 1891/* ARGSUSED */
1823int 1892int
1824lfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size) 1893lfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1825{ 1894{
1826 struct inode *ip; 1895 struct inode *ip;
1827 struct lfid lfh; 1896 struct lfid lfh;
1828 1897
1829 if (*fh_size < sizeof(struct lfid)) { 1898 if (*fh_size < sizeof(struct lfid)) {
1830 *fh_size = sizeof(struct lfid); 1899 *fh_size = sizeof(struct lfid);
1831 return E2BIG; 1900 return E2BIG;
1832 } 1901 }
1833 *fh_size = sizeof(struct lfid); 1902 *fh_size = sizeof(struct lfid);
1834 ip = VTOI(vp); 1903 ip = VTOI(vp);
1835 memset(&lfh, 0, sizeof(lfh)); 1904 memset(&lfh, 0, sizeof(lfh));
1836 lfh.lfid_len = sizeof(struct lfid); 1905 lfh.lfid_len = sizeof(struct lfid);
1837 lfh.lfid_ino = ip->i_number; 1906 lfh.lfid_ino = ip->i_number;
1838 lfh.lfid_gen = ip->i_gen; 1907 lfh.lfid_gen = ip->i_gen;
1839 lfh.lfid_ident = lfs_sb_getident(ip->i_lfs); 1908 lfh.lfid_ident = lfs_sb_getident(ip->i_lfs);
1840 memcpy(fhp, &lfh, sizeof(lfh)); 1909 memcpy(fhp, &lfh, sizeof(lfh));
1841 return (0); 1910 return (0);
1842} 1911}
1843 1912
1844/* 1913/*
1845 * ulfs_bmaparray callback function for writing. 1914 * ulfs_bmaparray callback function for writing.
1846 * 1915 *
1847 * Since blocks will be written to the new segment anyway, 1916 * Since blocks will be written to the new segment anyway,
1848 * we don't care about current daddr of them. 1917 * we don't care about current daddr of them.
1849 */ 1918 */
1850static bool 1919static bool
1851lfs_issequential_hole(const struct lfs *fs, 1920lfs_issequential_hole(const struct lfs *fs,
1852 daddr_t daddr0, daddr_t daddr1) 1921 daddr_t daddr0, daddr_t daddr1)
1853{ 1922{
1854 (void)fs; /* not used */ 1923 (void)fs; /* not used */
1855 1924
1856 KASSERT(daddr0 == UNWRITTEN || 1925 KASSERT(daddr0 == UNWRITTEN ||
1857 (0 <= daddr0 && daddr0 <= LFS_MAX_DADDR(fs))); 1926 (0 <= daddr0 && daddr0 <= LFS_MAX_DADDR(fs)));
1858 KASSERT(daddr1 == UNWRITTEN || 1927 KASSERT(daddr1 == UNWRITTEN ||
1859 (0 <= daddr1 && daddr1 <= LFS_MAX_DADDR(fs))); 1928 (0 <= daddr1 && daddr1 <= LFS_MAX_DADDR(fs)));
1860 1929
1861 /* NOTE: all we want to know here is 'hole or not'. */ 1930 /* NOTE: all we want to know here is 'hole or not'. */
1862 /* NOTE: UNASSIGNED is converted to 0 by ulfs_bmaparray. */ 1931 /* NOTE: UNASSIGNED is converted to 0 by ulfs_bmaparray. */
1863 1932
1864 /* 1933 /*
1865 * treat UNWRITTENs and all resident blocks as 'contiguous' 1934 * treat UNWRITTENs and all resident blocks as 'contiguous'
1866 */ 1935 */
1867 if (daddr0 != 0 && daddr1 != 0) 1936 if (daddr0 != 0 && daddr1 != 0)
1868 return true; 1937 return true;
1869 1938
1870 /* 1939 /*
1871 * both are in hole? 1940 * both are in hole?
1872 */ 1941 */
1873 if (daddr0 == 0 && daddr1 == 0) 1942 if (daddr0 == 0 && daddr1 == 0)
1874 return true; /* all holes are 'contiguous' for us. */ 1943 return true; /* all holes are 'contiguous' for us. */
1875 1944
1876 return false; 1945 return false;
1877} 1946}
1878 1947
1879/* 1948/*
1880 * lfs_gop_write functions exactly like genfs_gop_write, except that 1949 * lfs_gop_write functions exactly like genfs_gop_write, except that
1881 * (1) it requires the seglock to be held by its caller, and sp->fip 1950 * (1) it requires the seglock to be held by its caller, and sp->fip
1882 * to be properly initialized (it will return without re-initializing 1951 * to be properly initialized (it will return without re-initializing
1883 * sp->fip, and without calling lfs_writeseg). 1952 * sp->fip, and without calling lfs_writeseg).
1884 * (2) it uses the remaining space in the segment, rather than VOP_BMAP, 1953 * (2) it uses the remaining space in the segment, rather than VOP_BMAP,
1885 * to determine how large a block it can write at once (though it does 1954 * to determine how large a block it can write at once (though it does
1886 * still use VOP_BMAP to find holes in the file); 1955 * still use VOP_BMAP to find holes in the file);
1887 * (3) it calls lfs_gatherblock instead of VOP_STRATEGY on its blocks 1956 * (3) it calls lfs_gatherblock instead of VOP_STRATEGY on its blocks
1888 * (leaving lfs_writeseg to deal with the cluster blocks, so we might 1957 * (leaving lfs_writeseg to deal with the cluster blocks, so we might
1889 * now have clusters of clusters, ick.) 1958 * now have clusters of clusters, ick.)
1890 */ 1959 */
1891static int 1960static int
1892lfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, 1961lfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
1893 int flags) 1962 int flags)
1894{ 1963{
1895 int i, error, run, haveeof = 0; 1964 int i, error, run, haveeof = 0;
1896 int fs_bshift; 1965 int fs_bshift;
1897 vaddr_t kva; 1966 vaddr_t kva;
1898 off_t eof, offset, startoffset = 0; 1967 off_t eof, offset, startoffset = 0;
1899 size_t bytes, iobytes, skipbytes; 1968 size_t bytes, iobytes, skipbytes;
1900 bool async = (flags & PGO_SYNCIO) == 0; 1969 bool async = (flags & PGO_SYNCIO) == 0;
1901 daddr_t lbn, blkno; 1970 daddr_t lbn, blkno;
1902 struct vm_page *pg; 1971 struct vm_page *pg;
1903 struct buf *mbp, *bp; 1972 struct buf *mbp, *bp;
1904 struct vnode *devvp = VTOI(vp)->i_devvp; 1973 struct vnode *devvp = VTOI(vp)->i_devvp;
1905 struct inode *ip = VTOI(vp); 1974 struct inode *ip = VTOI(vp);
1906 struct lfs *fs = ip->i_lfs; 1975 struct lfs *fs = ip->i_lfs;
1907 struct segment *sp = fs->lfs_sp; 1976 struct segment *sp = fs->lfs_sp;
1908 SEGSUM *ssp; 1977 SEGSUM *ssp;
1909 UVMHIST_FUNC("lfs_gop_write"); UVMHIST_CALLED(ubchist); 1978 UVMHIST_FUNC("lfs_gop_write"); UVMHIST_CALLED(ubchist);
1910 const char * failreason = NULL; 1979 const char * failreason = NULL;
1911 1980
1912 ASSERT_SEGLOCK(fs); 1981 ASSERT_SEGLOCK(fs);
1913 1982
1914 /* The Ifile lives in the buffer cache */ 1983 /* The Ifile lives in the buffer cache */
1915 KASSERT(vp != fs->lfs_ivnode); 1984 KASSERT(vp != fs->lfs_ivnode);
1916 1985
1917 /* 1986 /*
1918 * We don't want to fill the disk before the cleaner has a chance 1987 * We don't want to fill the disk before the cleaner has a chance
1919 * to make room for us. If we're in danger of doing that, fail 1988 * to make room for us. If we're in danger of doing that, fail
1920 * with EAGAIN. The caller will have to notice this, unlock 1989 * with EAGAIN. The caller will have to notice this, unlock
1921 * so the cleaner can run, relock and try again. 1990 * so the cleaner can run, relock and try again.
1922 * 1991 *
1923 * We must write everything, however, if our vnode is being 1992 * We must write everything, however, if our vnode is being
1924 * reclaimed. 1993 * reclaimed.
1925 */ 1994 */
1926 mutex_enter(vp->v_interlock); 1995 mutex_enter(vp->v_interlock);
1927 if (LFS_STARVED_FOR_SEGS(fs) && vdead_check(vp, VDEAD_NOWAIT) == 0) { 1996 if (LFS_STARVED_FOR_SEGS(fs) && vdead_check(vp, VDEAD_NOWAIT) == 0) {
1928 mutex_exit(vp->v_interlock); 1997 mutex_exit(vp->v_interlock);
1929 failreason = "Starved for segs and not flushing vp"; 1998 failreason = "Starved for segs and not flushing vp";
1930 goto tryagain; 1999 goto tryagain;
1931 } 2000 }
1932 mutex_exit(vp->v_interlock); 2001 mutex_exit(vp->v_interlock);
1933 2002
1934 /* 2003 /*
1935 * Sometimes things slip past the filters in lfs_putpages, 2004 * Sometimes things slip past the filters in lfs_putpages,
1936 * and the pagedaemon tries to write pages---problem is 2005 * and the pagedaemon tries to write pages---problem is
1937 * that the pagedaemon never acquires the segment lock. 2006 * that the pagedaemon never acquires the segment lock.
1938 * 2007 *
1939 * Alternatively, pages that were clean when we called 2008 * Alternatively, pages that were clean when we called
1940 * genfs_putpages may have become dirty in the meantime. In this 2009 * genfs_putpages may have become dirty in the meantime. In this
1941 * case the segment header is not properly set up for blocks 2010 * case the segment header is not properly set up for blocks
1942 * to be added to it. 2011 * to be added to it.
1943 * 2012 *
1944 * Unbusy and unclean the pages, and put them on the ACTIVE 2013 * Unbusy and unclean the pages, and put them on the ACTIVE
1945 * queue under the hypothesis that they couldn't have got here 2014 * queue under the hypothesis that they couldn't have got here
1946 * unless they were modified *quite* recently. 2015 * unless they were modified *quite* recently.
1947 * 2016 *
1948 * XXXUBC that last statement is an oversimplification of course. 2017 * XXXUBC that last statement is an oversimplification of course.
1949 */ 2018 */
1950 if (!LFS_SEGLOCK_HELD(fs)) { 2019 if (!LFS_SEGLOCK_HELD(fs)) {
1951 failreason = "Seglock not held"; 2020 failreason = "Seglock not held";
1952 goto tryagain; 2021 goto tryagain;
1953 } 2022 }
1954 if (ip->i_lfs_iflags & LFSI_NO_GOP_WRITE) { 2023 if (ip->i_lfs_iflags & LFSI_NO_GOP_WRITE) {
1955 failreason = "Inode with no_gop_write"; 2024 failreason = "Inode with no_gop_write";
1956 goto tryagain; 2025 goto tryagain;
1957 } 2026 }
1958 if ((pgs[0]->offset & lfs_sb_getbmask(fs)) != 0) { 2027 if ((pgs[0]->offset & lfs_sb_getbmask(fs)) != 0) {
1959 failreason = "Bad page offset"; 2028 failreason = "Bad page offset";
1960 goto tryagain; 2029 goto tryagain;
1961 } 2030 }
1962 2031
1963 UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x", 2032 UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
1964 vp, pgs, npages, flags); 2033 vp, pgs, npages, flags);
1965 2034
1966 GOP_SIZE(vp, vp->v_size, &eof, 0); 2035 GOP_SIZE(vp, vp->v_size, &eof, 0);
1967 haveeof = 1; 2036 haveeof = 1;
1968 2037
1969 if (vp->v_type == VREG) 2038 if (vp->v_type == VREG)
1970 fs_bshift = vp->v_mount->mnt_fs_bshift; 2039 fs_bshift = vp->v_mount->mnt_fs_bshift;
1971 else 2040 else
1972 fs_bshift = DEV_BSHIFT; 2041 fs_bshift = DEV_BSHIFT;
1973 error = 0; 2042 error = 0;
1974 pg = pgs[0]; 2043 pg = pgs[0];
1975 startoffset = pg->offset; 2044 startoffset = pg->offset;
1976 KASSERT(eof >= 0); 2045 KASSERT(eof >= 0);
1977 2046
1978 if (startoffset >= eof) { 2047 if (startoffset >= eof) {
1979 failreason = "Offset beyond EOF"; 2048 failreason = "Offset beyond EOF";
1980 goto tryagain; 2049 goto tryagain;
1981 } else 2050 } else
1982 bytes = MIN(npages << PAGE_SHIFT, eof - startoffset); 2051 bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
1983 skipbytes = 0; 2052 skipbytes = 0;
1984 2053
1985 KASSERT(bytes != 0); 2054 KASSERT(bytes != 0);
1986 2055
1987 /* Swap PG_DELWRI for PG_PAGEOUT */ 2056 /* Swap PG_DELWRI for PG_PAGEOUT */
1988 for (i = 0; i < npages; i++) { 2057 for (i = 0; i < npages; i++) {
1989 if (pgs[i]->flags & PG_DELWRI) { 2058 if (pgs[i]->flags & PG_DELWRI) {
1990 KASSERT(!(pgs[i]->flags & PG_PAGEOUT)); 2059 KASSERT(!(pgs[i]->flags & PG_PAGEOUT));
1991 pgs[i]->flags &= ~PG_DELWRI; 2060 pgs[i]->flags &= ~PG_DELWRI;
1992 pgs[i]->flags |= PG_PAGEOUT; 2061 pgs[i]->flags |= PG_PAGEOUT;
1993 uvm_pageout_start(1); 2062 uvm_pageout_start(1);
1994 mutex_enter(vp->v_interlock); 2063 mutex_enter(vp->v_interlock);
1995 mutex_enter(&uvm_pageqlock); 2064 mutex_enter(&uvm_pageqlock);
1996 uvm_pageunwire(pgs[i]); 2065 uvm_pageunwire(pgs[i]);
1997 mutex_exit(&uvm_pageqlock); 2066 mutex_exit(&uvm_pageqlock);
1998 mutex_exit(vp->v_interlock); 2067 mutex_exit(vp->v_interlock);
1999 } 2068 }
2000 } 2069 }
2001 2070
2002 /* 2071 /*
2003 * Check to make sure we're starting on a block boundary. 2072 * Check to make sure we're starting on a block boundary.
2004 * We'll check later to make sure we always write entire 2073 * We'll check later to make sure we always write entire
2005 * blocks (or fragments). 2074 * blocks (or fragments).
2006 */ 2075 */
2007 if (startoffset & lfs_sb_getbmask(fs)) 2076 if (startoffset & lfs_sb_getbmask(fs))
2008 printf("%" PRId64 " & %" PRIu64 " = %" PRId64 "\n", 2077 printf("%" PRId64 " & %" PRIu64 " = %" PRId64 "\n",
2009 startoffset, lfs_sb_getbmask(fs), 2078 startoffset, lfs_sb_getbmask(fs),
2010 startoffset & lfs_sb_getbmask(fs)); 2079 startoffset & lfs_sb_getbmask(fs));
2011 KASSERT((startoffset & lfs_sb_getbmask(fs)) == 0); 2080 KASSERT((startoffset & lfs_sb_getbmask(fs)) == 0);
2012 if (bytes & lfs_sb_getffmask(fs)) { 2081 if (bytes & lfs_sb_getffmask(fs)) {
2013 printf("lfs_gop_write: asked to write %ld bytes\n", (long)bytes); 2082 printf("lfs_gop_write: asked to write %ld bytes\n", (long)bytes);
2014 panic("lfs_gop_write: non-integer blocks"); 2083 panic("lfs_gop_write: non-integer blocks");
2015 } 2084 }
2016 2085
2017 /* 2086 /*
2018 * We could deadlock here on pager_map with UVMPAGER_MAPIN_WAITOK. 2087 * We could deadlock here on pager_map with UVMPAGER_MAPIN_WAITOK.
2019 * If we would, write what we have and try again. If we don't 2088 * If we would, write what we have and try again. If we don't
2020 * have anything to write, we'll have to sleep. 2089 * have anything to write, we'll have to sleep.
2021 */ 2090 */
2022 ssp = (SEGSUM *)sp->segsum; 2091 ssp = (SEGSUM *)sp->segsum;
2023 if ((kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE | 2092 if ((kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE |
2024 (lfs_ss_getnfinfo(fs, ssp) < 1 ? 2093 (lfs_ss_getnfinfo(fs, ssp) < 1 ?
2025 UVMPAGER_MAPIN_WAITOK : 0))) == 0x0) { 2094 UVMPAGER_MAPIN_WAITOK : 0))) == 0x0) {
2026 DLOG((DLOG_PAGE, "lfs_gop_write: forcing write\n")); 2095 DLOG((DLOG_PAGE, "lfs_gop_write: forcing write\n"));
2027#if 0 2096#if 0
2028 " with nfinfo=%d at offset 0x%jx\n", 2097 " with nfinfo=%d at offset 0x%jx\n",
2029 (int)lfs_ss_getnfinfo(fs, ssp), 2098 (int)lfs_ss_getnfinfo(fs, ssp),
2030 (uintmax_t)lfs_sb_getoffset(fs))); 2099 (uintmax_t)lfs_sb_getoffset(fs)));
2031#endif 2100#endif
2032 lfs_updatemeta(sp); 2101 lfs_updatemeta(sp);
2033 lfs_release_finfo(fs); 2102 lfs_release_finfo(fs);
2034 (void) lfs_writeseg(fs, sp); 2103 (void) lfs_writeseg(fs, sp);
2035 2104
2036 lfs_acquire_finfo(fs, ip->i_number, ip->i_gen); 2105 lfs_acquire_finfo(fs, ip->i_number, ip->i_gen);
2037 2106
2038 /* 2107 /*
2039 * Having given up all of the pager_map we were holding, 2108 * Having given up all of the pager_map we were holding,
2040 * we can now wait for aiodoned to reclaim it for us 2109 * we can now wait for aiodoned to reclaim it for us
2041 * without fear of deadlock. 2110 * without fear of deadlock.
2042 */ 2111 */
2043 kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE | 2112 kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE |
2044 UVMPAGER_MAPIN_WAITOK); 2113 UVMPAGER_MAPIN_WAITOK);
2045 } 2114 }
2046 2115
2047 mbp = getiobuf(NULL, true); 2116 mbp = getiobuf(NULL, true);
2048 UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x", 2117 UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
2049 vp, mbp, vp->v_numoutput, bytes); 2118 vp, mbp, vp->v_numoutput, bytes);
2050 mbp->b_bufsize = npages << PAGE_SHIFT; 2119 mbp->b_bufsize = npages << PAGE_SHIFT;
2051 mbp->b_data = (void *)kva; 2120 mbp->b_data = (void *)kva;
2052 mbp->b_resid = mbp->b_bcount = bytes; 2121 mbp->b_resid = mbp->b_bcount = bytes;
2053 mbp->b_cflags = BC_BUSY|BC_AGE; 2122 mbp->b_cflags = BC_BUSY|BC_AGE;
2054 mbp->b_iodone = uvm_aio_biodone; 2123 mbp->b_iodone = uvm_aio_biodone;
2055 2124
2056 bp = NULL; 2125 bp = NULL;
2057 for (offset = startoffset; 2126 for (offset = startoffset;
2058 bytes > 0; 2127 bytes > 0;
2059 offset += iobytes, bytes -= iobytes) { 2128 offset += iobytes, bytes -= iobytes) {
2060 lbn = offset >> fs_bshift; 2129 lbn = offset >> fs_bshift;
2061 error = ulfs_bmaparray(vp, lbn, &blkno, NULL, NULL, &run, 2130 error = ulfs_bmaparray(vp, lbn, &blkno, NULL, NULL, &run,
2062 lfs_issequential_hole); 2131 lfs_issequential_hole);
2063 if (error) { 2132 if (error) {
2064 UVMHIST_LOG(ubchist, "ulfs_bmaparray() -> %d", 2133 UVMHIST_LOG(ubchist, "ulfs_bmaparray() -> %d",
2065 error,0,0,0); 2134 error,0,0,0);
2066 skipbytes += bytes; 2135 skipbytes += bytes;
2067 bytes = 0; 2136 bytes = 0;
2068 break; 2137 break;
2069 } 2138 }
2070 2139
2071 iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset, 2140 iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
2072 bytes); 2141 bytes);
2073 if (blkno == (daddr_t)-1) { 2142 if (blkno == (daddr_t)-1) {
2074 skipbytes += iobytes; 2143 skipbytes += iobytes;
2075 continue; 2144 continue;
2076 } 2145 }
2077 2146
2078 /* 2147 /*
2079 * Discover how much we can really pack into this buffer. 2148 * Discover how much we can really pack into this buffer.
2080 */ 2149 */
2081 /* If no room in the current segment, finish it up */ 2150 /* If no room in the current segment, finish it up */
2082 if (sp->sum_bytes_left < sizeof(int32_t) || 2151 if (sp->sum_bytes_left < sizeof(int32_t) ||
2083 sp->seg_bytes_left < (1 << lfs_sb_getbshift(fs))) { 2152 sp->seg_bytes_left < (1 << lfs_sb_getbshift(fs))) {
2084 int vers; 2153 int vers;
2085 2154
2086 lfs_updatemeta(sp); 2155 lfs_updatemeta(sp);
2087 vers = lfs_fi_getversion(fs, sp->fip); 2156 vers = lfs_fi_getversion(fs, sp->fip);
2088 lfs_release_finfo(fs); 2157 lfs_release_finfo(fs);
2089 (void) lfs_writeseg(fs, sp); 2158 (void) lfs_writeseg(fs, sp);
2090 2159
2091 lfs_acquire_finfo(fs, ip->i_number, vers); 2160 lfs_acquire_finfo(fs, ip->i_number, vers);
2092 } 2161 }
2093 /* Check both for space in segment and space in segsum */ 2162 /* Check both for space in segment and space in segsum */
2094 iobytes = MIN(iobytes, (sp->seg_bytes_left >> fs_bshift) 2163 iobytes = MIN(iobytes, (sp->seg_bytes_left >> fs_bshift)
2095 << fs_bshift); 2164 << fs_bshift);
2096 iobytes = MIN(iobytes, (sp->sum_bytes_left / sizeof(int32_t)) 2165 iobytes = MIN(iobytes, (sp->sum_bytes_left / sizeof(int32_t))
2097 << fs_bshift); 2166 << fs_bshift);
2098 KASSERT(iobytes > 0); 2167 KASSERT(iobytes > 0);
2099 2168
2100 /* if it's really one i/o, don't make a second buf */ 2169 /* if it's really one i/o, don't make a second buf */
2101 if (offset == startoffset && iobytes == bytes) { 2170 if (offset == startoffset && iobytes == bytes) {
2102 bp = mbp; 2171 bp = mbp;
2103 /*  2172 /*
2104 * All the LFS output is done by the segwriter. It 2173 * All the LFS output is done by the segwriter. It
2105 * will increment numoutput by one for all the bufs it 2174 * will increment numoutput by one for all the bufs it
2106 * recieves. However this buffer needs one extra to 2175 * recieves. However this buffer needs one extra to
2107 * account for aiodone. 2176 * account for aiodone.
2108 */ 2177 */
2109 mutex_enter(vp->v_interlock); 2178 mutex_enter(vp->v_interlock);
2110 vp->v_numoutput++; 2179 vp->v_numoutput++;
2111 mutex_exit(vp->v_interlock); 2180 mutex_exit(vp->v_interlock);
2112 } else { 2181 } else {
2113 bp = getiobuf(NULL, true); 2182 bp = getiobuf(NULL, true);
2114 UVMHIST_LOG(ubchist, "vp %p bp %p num now %d", 2183 UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
2115 vp, bp, vp->v_numoutput, 0); 2184 vp, bp, vp->v_numoutput, 0);
2116 nestiobuf_setup(mbp, bp, offset - pg->offset, iobytes); 2185 nestiobuf_setup(mbp, bp, offset - pg->offset, iobytes);
2117 /* 2186 /*
2118 * LFS doesn't like async I/O here, dies with 2187 * LFS doesn't like async I/O here, dies with
2119 * an assert in lfs_bwrite(). Is that assert 2188 * an assert in lfs_bwrite(). Is that assert
2120 * valid? I retained non-async behaviour when 2189 * valid? I retained non-async behaviour when
2121 * converted this to use nestiobuf --pooka 2190 * converted this to use nestiobuf --pooka
2122 */ 2191 */
2123 bp->b_flags &= ~B_ASYNC; 2192 bp->b_flags &= ~B_ASYNC;
2124 } 2193 }
2125 2194
2126 /* XXX This is silly ... is this necessary? */ 2195 /* XXX This is silly ... is this necessary? */
2127 mutex_enter(&bufcache_lock); 2196 mutex_enter(&bufcache_lock);
2128 mutex_enter(vp->v_interlock); 2197 mutex_enter(vp->v_interlock);
2129 bgetvp(vp, bp); 2198 bgetvp(vp, bp);
2130 mutex_exit(vp->v_interlock); 2199 mutex_exit(vp->v_interlock);
2131 mutex_exit(&bufcache_lock); 2200 mutex_exit(&bufcache_lock);
2132 2201
2133 bp->b_lblkno = lfs_lblkno(fs, offset); 2202 bp->b_lblkno = lfs_lblkno(fs, offset);
2134 bp->b_private = mbp; 2203 bp->b_private = mbp;
2135 if (devvp->v_type == VBLK) { 2204 if (devvp->v_type == VBLK) {
2136 bp->b_dev = devvp->v_rdev; 2205 bp->b_dev = devvp->v_rdev;
2137 } 2206 }
2138 VOP_BWRITE(bp->b_vp, bp); 2207 VOP_BWRITE(bp->b_vp, bp);
2139 while (lfs_gatherblock(sp, bp, NULL)) 2208 while (lfs_gatherblock(sp, bp, NULL))
2140 continue; 2209 continue;
2141 } 2210 }
2142 2211
2143 nestiobuf_done(mbp, skipbytes, error); 2212 nestiobuf_done(mbp, skipbytes, error);
2144 if (skipbytes) { 2213 if (skipbytes) {
2145 UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0); 2214 UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
2146 } 2215 }
2147 UVMHIST_LOG(ubchist, "returning 0", 0,0,0,0); 2216 UVMHIST_LOG(ubchist, "returning 0", 0,0,0,0);
2148 2217
2149 if (!async) { 2218 if (!async) {
2150 /* Start a segment write. */ 2219 /* Start a segment write. */
2151 UVMHIST_LOG(ubchist, "flushing", 0,0,0,0); 2220 UVMHIST_LOG(ubchist, "flushing", 0,0,0,0);
2152 mutex_enter(&lfs_lock); 2221 mutex_enter(&lfs_lock);
2153 lfs_flush(fs, 0, 1); 2222 lfs_flush(fs, 0, 1);
2154 mutex_exit(&lfs_lock); 2223 mutex_exit(&lfs_lock);
2155 } 2224 }
2156 2225
2157 if ((sp->seg_flags & SEGM_SINGLE) && lfs_sb_getcurseg(fs) != fs->lfs_startseg) 2226 if ((sp->seg_flags & SEGM_SINGLE) && lfs_sb_getcurseg(fs) != fs->lfs_startseg)
2158 return EAGAIN; 2227 return EAGAIN;
2159 2228
2160 return (0); 2229 return (0);
2161 2230
2162 tryagain: 2231 tryagain:
2163 /* 2232 /*
2164 * We can't write the pages, for whatever reason. 2233 * We can't write the pages, for whatever reason.
2165 * Clean up after ourselves, and make the caller try again. 2234 * Clean up after ourselves, and make the caller try again.
2166 */ 2235 */
2167 mutex_enter(vp->v_interlock); 2236 mutex_enter(vp->v_interlock);
2168 2237
2169 /* Tell why we're here, if we know */ 2238 /* Tell why we're here, if we know */
2170 if (failreason != NULL) { 2239 if (failreason != NULL) {
2171 DLOG((DLOG_PAGE, "lfs_gop_write: %s\n", failreason)); 2240 DLOG((DLOG_PAGE, "lfs_gop_write: %s\n", failreason));
2172 } 2241 }
2173 if (haveeof && startoffset >= eof) { 2242 if (haveeof && startoffset >= eof) {
2174 DLOG((DLOG_PAGE, "lfs_gop_write: ino %d start 0x%" PRIx64 2243 DLOG((DLOG_PAGE, "lfs_gop_write: ino %d start 0x%" PRIx64
2175 " eof 0x%" PRIx64 " npages=%d\n", VTOI(vp)->i_number, 2244 " eof 0x%" PRIx64 " npages=%d\n", VTOI(vp)->i_number,
2176 pgs[0]->offset, eof, npages)); 2245 pgs[0]->offset, eof, npages));
2177 } 2246 }
2178 2247
2179 mutex_enter(&uvm_pageqlock); 2248 mutex_enter(&uvm_pageqlock);
2180 for (i = 0; i < npages; i++) { 2249 for (i = 0; i < npages; i++) {
2181 pg = pgs[i]; 2250 pg = pgs[i];
2182 2251
2183 if (pg->flags & PG_PAGEOUT) 2252 if (pg->flags & PG_PAGEOUT)
2184 uvm_pageout_done(1); 2253 uvm_pageout_done(1);
2185 if (pg->flags & PG_DELWRI) { 2254 if (pg->flags & PG_DELWRI) {
2186 uvm_pageunwire(pg); 2255 uvm_pageunwire(pg);
2187 } 2256 }
2188 uvm_pageactivate(pg); 2257 uvm_pageactivate(pg);
2189 pg->flags &= ~(PG_CLEAN|PG_DELWRI|PG_PAGEOUT|PG_RELEASED); 2258 pg->flags &= ~(PG_CLEAN|PG_DELWRI|PG_PAGEOUT|PG_RELEASED);
2190 DLOG((DLOG_PAGE, "pg[%d] = %p (vp %p off %" PRIx64 ")\n", i, pg, 2259 DLOG((DLOG_PAGE, "pg[%d] = %p (vp %p off %" PRIx64 ")\n", i, pg,
2191 vp, pg->offset)); 2260 vp, pg->offset));
2192 DLOG((DLOG_PAGE, "pg[%d]->flags = %x\n", i, pg->flags)); 2261 DLOG((DLOG_PAGE, "pg[%d]->flags = %x\n", i, pg->flags));
2193 DLOG((DLOG_PAGE, "pg[%d]->pqflags = %x\n", i, pg->pqflags)); 2262 DLOG((DLOG_PAGE, "pg[%d]->pqflags = %x\n", i, pg->pqflags));
2194 DLOG((DLOG_PAGE, "pg[%d]->uanon = %p\n", i, pg->uanon)); 2263 DLOG((DLOG_PAGE, "pg[%d]->uanon = %p\n", i, pg->uanon));
2195 DLOG((DLOG_PAGE, "pg[%d]->uobject = %p\n", i, pg->uobject)); 2264 DLOG((DLOG_PAGE, "pg[%d]->uobject = %p\n", i, pg->uobject));
2196 DLOG((DLOG_PAGE, "pg[%d]->wire_count = %d\n", i, 2265 DLOG((DLOG_PAGE, "pg[%d]->wire_count = %d\n", i,
2197 pg->wire_count)); 2266 pg->wire_count));
2198 DLOG((DLOG_PAGE, "pg[%d]->loan_count = %d\n", i, 2267 DLOG((DLOG_PAGE, "pg[%d]->loan_count = %d\n", i,
2199 pg->loan_count)); 2268 pg->loan_count));
2200 } 2269 }
2201 /* uvm_pageunbusy takes care of PG_BUSY, PG_WANTED */ 2270 /* uvm_pageunbusy takes care of PG_BUSY, PG_WANTED */
2202 uvm_page_unbusy(pgs, npages); 2271 uvm_page_unbusy(pgs, npages);
2203 mutex_exit(&uvm_pageqlock); 2272 mutex_exit(&uvm_pageqlock);
2204 mutex_exit(vp->v_interlock); 2273 mutex_exit(vp->v_interlock);
2205 return EAGAIN; 2274 return EAGAIN;
2206} 2275}
2207 2276
2208/* 2277/*
2209 * finish vnode/inode initialization. 2278 * finish vnode/inode initialization.
2210 * used by lfs_vget. 2279 * used by lfs_vget.
2211 */ 2280 */
2212void 2281void
2213lfs_vinit(struct mount *mp, struct vnode **vpp) 2282lfs_vinit(struct mount *mp, struct vnode **vpp)
2214{ 2283{
2215 struct vnode *vp = *vpp; 2284 struct vnode *vp = *vpp;
2216 struct inode *ip = VTOI(vp); 2285 struct inode *ip = VTOI(vp);
2217 struct ulfsmount *ump = VFSTOULFS(mp); 2286 struct ulfsmount *ump = VFSTOULFS(mp);
2218 struct lfs *fs = ump->um_lfs; 2287 struct lfs *fs = ump->um_lfs;
2219 int i; 2288 int i;
2220 2289
2221 ip->i_mode = lfs_dino_getmode(fs, ip->i_din); 2290 ip->i_mode = lfs_dino_getmode(fs, ip->i_din);
2222 ip->i_nlink = lfs_dino_getnlink(fs, ip->i_din); 2291 ip->i_nlink = lfs_dino_getnlink(fs, ip->i_din);
2223 ip->i_lfs_osize = ip->i_size = lfs_dino_getsize(fs, ip->i_din); 2292 ip->i_lfs_osize = ip->i_size = lfs_dino_getsize(fs, ip->i_din);
2224 ip->i_flags = lfs_dino_getflags(fs, ip->i_din); 2293 ip->i_flags = lfs_dino_getflags(fs, ip->i_din);
2225 ip->i_gen = lfs_dino_getgen(fs, ip->i_din); 2294 ip->i_gen = lfs_dino_getgen(fs, ip->i_din);
2226 ip->i_uid = lfs_dino_getuid(fs, ip->i_din); 2295 ip->i_uid = lfs_dino_getuid(fs, ip->i_din);
2227 ip->i_gid = lfs_dino_getgid(fs, ip->i_din); 2296 ip->i_gid = lfs_dino_getgid(fs, ip->i_din);
2228 2297
2229 ip->i_lfs_effnblks = lfs_dino_getblocks(fs, ip->i_din); 2298 ip->i_lfs_effnblks = lfs_dino_getblocks(fs, ip->i_din);
2230 ip->i_lfs_odnlink = lfs_dino_getnlink(fs, ip->i_din); 2299 ip->i_lfs_odnlink = lfs_dino_getnlink(fs, ip->i_din);
2231 2300
2232 /* 2301 /*
2233 * Initialize the vnode from the inode, check for aliases. In all 2302 * Initialize the vnode from the inode, check for aliases. In all
2234 * cases re-init ip, the underlying vnode/inode may have changed. 2303 * cases re-init ip, the underlying vnode/inode may have changed.
2235 */ 2304 */
2236 ulfs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp); 2305 ulfs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
2237 ip = VTOI(vp); 2306 ip = VTOI(vp);
2238 2307
2239 memset(ip->i_lfs_fragsize, 0, ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize)); 2308 memset(ip->i_lfs_fragsize, 0, ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize));
2240 if (vp->v_type != VLNK || ip->i_size >= ip->i_lfs->um_maxsymlinklen) { 2309 if (vp->v_type != VLNK || ip->i_size >= ip->i_lfs->um_maxsymlinklen) {
2241#ifdef DEBUG 2310#ifdef DEBUG
2242 for (i = (ip->i_size + lfs_sb_getbsize(fs) - 1) >> lfs_sb_getbshift(fs); 2311 for (i = (ip->i_size + lfs_sb_getbsize(fs) - 1) >> lfs_sb_getbshift(fs);
2243 i < ULFS_NDADDR; i++) { 2312 i < ULFS_NDADDR; i++) {
2244 if ((vp->v_type == VBLK || vp->v_type == VCHR) && 2313 if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
2245 i == 0) 2314 i == 0)
2246 continue; 2315 continue;
2247 if (lfs_dino_getdb(fs, ip->i_din, i) != 0) { 2316 if (lfs_dino_getdb(fs, ip->i_din, i) != 0) {
2248 lfs_dump_dinode(fs, ip->i_din); 2317 lfs_dump_dinode(fs, ip->i_din);
2249 panic("inconsistent inode (direct)"); 2318 panic("inconsistent inode (direct)");
2250 } 2319 }
2251 } 2320 }
2252 for ( ; i < ULFS_NDADDR + ULFS_NIADDR; i++) { 2321 for ( ; i < ULFS_NDADDR + ULFS_NIADDR; i++) {
2253 if (lfs_dino_getib(fs, ip->i_din, i - ULFS_NDADDR) != 0) { 2322 if (lfs_dino_getib(fs, ip->i_din, i - ULFS_NDADDR) != 0) {
2254 lfs_dump_dinode(fs, ip->i_din); 2323 lfs_dump_dinode(fs, ip->i_din);
2255 panic("inconsistent inode (indirect)"); 2324 panic("inconsistent inode (indirect)");
2256 } 2325 }