lfs_vnops.c revision 1.268 1 /* $NetBSD: lfs_vnops.c,v 1.268 2014/05/17 07:10:27 dholland Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 /*
32 * Copyright (c) 1986, 1989, 1991, 1993, 1995
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * @(#)lfs_vnops.c 8.13 (Berkeley) 6/10/95
60 */
61
62 /* from NetBSD: ufs_vnops.c,v 1.213 2013/06/08 05:47:02 kardel Exp */
63 /*-
64 * Copyright (c) 2008 The NetBSD Foundation, Inc.
65 * All rights reserved.
66 *
67 * This code is derived from software contributed to The NetBSD Foundation
68 * by Wasabi Systems, Inc.
69 *
70 * Redistribution and use in source and binary forms, with or without
71 * modification, are permitted provided that the following conditions
72 * are met:
73 * 1. Redistributions of source code must retain the above copyright
74 * notice, this list of conditions and the following disclaimer.
75 * 2. Redistributions in binary form must reproduce the above copyright
76 * notice, this list of conditions and the following disclaimer in the
77 * documentation and/or other materials provided with the distribution.
78 *
79 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
80 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
81 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
82 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
83 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
84 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
85 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
86 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
87 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
88 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
89 * POSSIBILITY OF SUCH DAMAGE.
90 */
91 /*
92 * Copyright (c) 1982, 1986, 1989, 1993, 1995
93 * The Regents of the University of California. All rights reserved.
94 * (c) UNIX System Laboratories, Inc.
95 * All or some portions of this file are derived from material licensed
96 * to the University of California by American Telephone and Telegraph
97 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
98 * the permission of UNIX System Laboratories, Inc.
99 *
100 * Redistribution and use in source and binary forms, with or without
101 * modification, are permitted provided that the following conditions
102 * are met:
103 * 1. Redistributions of source code must retain the above copyright
104 * notice, this list of conditions and the following disclaimer.
105 * 2. Redistributions in binary form must reproduce the above copyright
106 * notice, this list of conditions and the following disclaimer in the
107 * documentation and/or other materials provided with the distribution.
108 * 3. Neither the name of the University nor the names of its contributors
109 * may be used to endorse or promote products derived from this software
110 * without specific prior written permission.
111 *
112 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
113 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
114 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
115 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
116 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
117 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
118 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
119 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
120 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
121 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
122 * SUCH DAMAGE.
123 *
124 * @(#)ufs_vnops.c 8.28 (Berkeley) 7/31/95
125 */
126
127 #include <sys/cdefs.h>
128 __KERNEL_RCSID(0, "$NetBSD: lfs_vnops.c,v 1.268 2014/05/17 07:10:27 dholland Exp $");
129
130 #ifdef _KERNEL_OPT
131 #include "opt_compat_netbsd.h"
132 #include "opt_uvm_page_trkown.h"
133 #endif
134
135 #include <sys/param.h>
136 #include <sys/systm.h>
137 #include <sys/namei.h>
138 #include <sys/resourcevar.h>
139 #include <sys/kernel.h>
140 #include <sys/file.h>
141 #include <sys/stat.h>
142 #include <sys/buf.h>
143 #include <sys/proc.h>
144 #include <sys/mount.h>
145 #include <sys/vnode.h>
146 #include <sys/pool.h>
147 #include <sys/signalvar.h>
148 #include <sys/kauth.h>
149 #include <sys/syslog.h>
150 #include <sys/fstrans.h>
151
152 #include <miscfs/fifofs/fifo.h>
153 #include <miscfs/genfs/genfs.h>
154 #include <miscfs/specfs/specdev.h>
155
156 #include <ufs/lfs/ulfs_inode.h>
157 #include <ufs/lfs/ulfsmount.h>
158 #include <ufs/lfs/ulfs_bswap.h>
159 #include <ufs/lfs/ulfs_extern.h>
160
161 #include <uvm/uvm.h>
162 #include <uvm/uvm_pmap.h>
163 #include <uvm/uvm_stat.h>
164 #include <uvm/uvm_pager.h>
165
166 #include <ufs/lfs/lfs.h>
167 #include <ufs/lfs/lfs_kernel.h>
168 #include <ufs/lfs/lfs_extern.h>
169
170 extern pid_t lfs_writer_daemon;
171 int lfs_ignore_lazy_sync = 1;
172
173 static int lfs_openextattr(void *v);
174 static int lfs_closeextattr(void *v);
175 static int lfs_getextattr(void *v);
176 static int lfs_setextattr(void *v);
177 static int lfs_listextattr(void *v);
178 static int lfs_deleteextattr(void *v);
179
180 /*
181 * A virgin directory (no blushing please).
182 */
183 static const struct lfs_dirtemplate mastertemplate = {
184 0, 12, LFS_DT_DIR, 1, ".",
185 0, LFS_DIRBLKSIZ - 12, LFS_DT_DIR, 2, ".."
186 };
187
188 /* Global vfs data structures for lfs. */
189 int (**lfs_vnodeop_p)(void *);
190 const struct vnodeopv_entry_desc lfs_vnodeop_entries[] = {
191 { &vop_default_desc, vn_default_error },
192 { &vop_lookup_desc, ulfs_lookup }, /* lookup */
193 { &vop_create_desc, lfs_create }, /* create */
194 { &vop_whiteout_desc, ulfs_whiteout }, /* whiteout */
195 { &vop_mknod_desc, lfs_mknod }, /* mknod */
196 { &vop_open_desc, ulfs_open }, /* open */
197 { &vop_close_desc, lfs_close }, /* close */
198 { &vop_access_desc, ulfs_access }, /* access */
199 { &vop_getattr_desc, lfs_getattr }, /* getattr */
200 { &vop_setattr_desc, lfs_setattr }, /* setattr */
201 { &vop_read_desc, lfs_read }, /* read */
202 { &vop_write_desc, lfs_write }, /* write */
203 { &vop_ioctl_desc, ulfs_ioctl }, /* ioctl */
204 { &vop_fcntl_desc, lfs_fcntl }, /* fcntl */
205 { &vop_poll_desc, ulfs_poll }, /* poll */
206 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
207 { &vop_revoke_desc, ulfs_revoke }, /* revoke */
208 { &vop_mmap_desc, lfs_mmap }, /* mmap */
209 { &vop_fsync_desc, lfs_fsync }, /* fsync */
210 { &vop_seek_desc, ulfs_seek }, /* seek */
211 { &vop_remove_desc, lfs_remove }, /* remove */
212 { &vop_link_desc, lfs_link }, /* link */
213 { &vop_rename_desc, lfs_rename }, /* rename */
214 { &vop_mkdir_desc, lfs_mkdir }, /* mkdir */
215 { &vop_rmdir_desc, lfs_rmdir }, /* rmdir */
216 { &vop_symlink_desc, lfs_symlink }, /* symlink */
217 { &vop_readdir_desc, ulfs_readdir }, /* readdir */
218 { &vop_readlink_desc, ulfs_readlink }, /* readlink */
219 { &vop_abortop_desc, ulfs_abortop }, /* abortop */
220 { &vop_inactive_desc, lfs_inactive }, /* inactive */
221 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
222 { &vop_lock_desc, ulfs_lock }, /* lock */
223 { &vop_unlock_desc, ulfs_unlock }, /* unlock */
224 { &vop_bmap_desc, ulfs_bmap }, /* bmap */
225 { &vop_strategy_desc, lfs_strategy }, /* strategy */
226 { &vop_print_desc, ulfs_print }, /* print */
227 { &vop_islocked_desc, ulfs_islocked }, /* islocked */
228 { &vop_pathconf_desc, ulfs_pathconf }, /* pathconf */
229 { &vop_advlock_desc, ulfs_advlock }, /* advlock */
230 { &vop_bwrite_desc, lfs_bwrite }, /* bwrite */
231 { &vop_getpages_desc, lfs_getpages }, /* getpages */
232 { &vop_putpages_desc, lfs_putpages }, /* putpages */
233 { &vop_openextattr_desc, lfs_openextattr }, /* openextattr */
234 { &vop_closeextattr_desc, lfs_closeextattr }, /* closeextattr */
235 { &vop_getextattr_desc, lfs_getextattr }, /* getextattr */
236 { &vop_setextattr_desc, lfs_setextattr }, /* setextattr */
237 { &vop_listextattr_desc, lfs_listextattr }, /* listextattr */
238 { &vop_deleteextattr_desc, lfs_deleteextattr }, /* deleteextattr */
239 { NULL, NULL }
240 };
241 const struct vnodeopv_desc lfs_vnodeop_opv_desc =
242 { &lfs_vnodeop_p, lfs_vnodeop_entries };
243
244 int (**lfs_specop_p)(void *);
245 const struct vnodeopv_entry_desc lfs_specop_entries[] = {
246 { &vop_default_desc, vn_default_error },
247 { &vop_lookup_desc, spec_lookup }, /* lookup */
248 { &vop_create_desc, spec_create }, /* create */
249 { &vop_mknod_desc, spec_mknod }, /* mknod */
250 { &vop_open_desc, spec_open }, /* open */
251 { &vop_close_desc, lfsspec_close }, /* close */
252 { &vop_access_desc, ulfs_access }, /* access */
253 { &vop_getattr_desc, lfs_getattr }, /* getattr */
254 { &vop_setattr_desc, lfs_setattr }, /* setattr */
255 { &vop_read_desc, ulfsspec_read }, /* read */
256 { &vop_write_desc, ulfsspec_write }, /* write */
257 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
258 { &vop_fcntl_desc, ulfs_fcntl }, /* fcntl */
259 { &vop_poll_desc, spec_poll }, /* poll */
260 { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
261 { &vop_revoke_desc, spec_revoke }, /* revoke */
262 { &vop_mmap_desc, spec_mmap }, /* mmap */
263 { &vop_fsync_desc, spec_fsync }, /* fsync */
264 { &vop_seek_desc, spec_seek }, /* seek */
265 { &vop_remove_desc, spec_remove }, /* remove */
266 { &vop_link_desc, spec_link }, /* link */
267 { &vop_rename_desc, spec_rename }, /* rename */
268 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
269 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
270 { &vop_symlink_desc, spec_symlink }, /* symlink */
271 { &vop_readdir_desc, spec_readdir }, /* readdir */
272 { &vop_readlink_desc, spec_readlink }, /* readlink */
273 { &vop_abortop_desc, spec_abortop }, /* abortop */
274 { &vop_inactive_desc, lfs_inactive }, /* inactive */
275 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
276 { &vop_lock_desc, ulfs_lock }, /* lock */
277 { &vop_unlock_desc, ulfs_unlock }, /* unlock */
278 { &vop_bmap_desc, spec_bmap }, /* bmap */
279 { &vop_strategy_desc, spec_strategy }, /* strategy */
280 { &vop_print_desc, ulfs_print }, /* print */
281 { &vop_islocked_desc, ulfs_islocked }, /* islocked */
282 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
283 { &vop_advlock_desc, spec_advlock }, /* advlock */
284 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
285 { &vop_getpages_desc, spec_getpages }, /* getpages */
286 { &vop_putpages_desc, spec_putpages }, /* putpages */
287 { &vop_openextattr_desc, lfs_openextattr }, /* openextattr */
288 { &vop_closeextattr_desc, lfs_closeextattr }, /* closeextattr */
289 { &vop_getextattr_desc, lfs_getextattr }, /* getextattr */
290 { &vop_setextattr_desc, lfs_setextattr }, /* setextattr */
291 { &vop_listextattr_desc, lfs_listextattr }, /* listextattr */
292 { &vop_deleteextattr_desc, lfs_deleteextattr }, /* deleteextattr */
293 { NULL, NULL }
294 };
295 const struct vnodeopv_desc lfs_specop_opv_desc =
296 { &lfs_specop_p, lfs_specop_entries };
297
298 int (**lfs_fifoop_p)(void *);
299 const struct vnodeopv_entry_desc lfs_fifoop_entries[] = {
300 { &vop_default_desc, vn_default_error },
301 { &vop_lookup_desc, vn_fifo_bypass }, /* lookup */
302 { &vop_create_desc, vn_fifo_bypass }, /* create */
303 { &vop_mknod_desc, vn_fifo_bypass }, /* mknod */
304 { &vop_open_desc, vn_fifo_bypass }, /* open */
305 { &vop_close_desc, lfsfifo_close }, /* close */
306 { &vop_access_desc, ulfs_access }, /* access */
307 { &vop_getattr_desc, lfs_getattr }, /* getattr */
308 { &vop_setattr_desc, lfs_setattr }, /* setattr */
309 { &vop_read_desc, ulfsfifo_read }, /* read */
310 { &vop_write_desc, ulfsfifo_write }, /* write */
311 { &vop_ioctl_desc, vn_fifo_bypass }, /* ioctl */
312 { &vop_fcntl_desc, ulfs_fcntl }, /* fcntl */
313 { &vop_poll_desc, vn_fifo_bypass }, /* poll */
314 { &vop_kqfilter_desc, vn_fifo_bypass }, /* kqfilter */
315 { &vop_revoke_desc, vn_fifo_bypass }, /* revoke */
316 { &vop_mmap_desc, vn_fifo_bypass }, /* mmap */
317 { &vop_fsync_desc, vn_fifo_bypass }, /* fsync */
318 { &vop_seek_desc, vn_fifo_bypass }, /* seek */
319 { &vop_remove_desc, vn_fifo_bypass }, /* remove */
320 { &vop_link_desc, vn_fifo_bypass }, /* link */
321 { &vop_rename_desc, vn_fifo_bypass }, /* rename */
322 { &vop_mkdir_desc, vn_fifo_bypass }, /* mkdir */
323 { &vop_rmdir_desc, vn_fifo_bypass }, /* rmdir */
324 { &vop_symlink_desc, vn_fifo_bypass }, /* symlink */
325 { &vop_readdir_desc, vn_fifo_bypass }, /* readdir */
326 { &vop_readlink_desc, vn_fifo_bypass }, /* readlink */
327 { &vop_abortop_desc, vn_fifo_bypass }, /* abortop */
328 { &vop_inactive_desc, lfs_inactive }, /* inactive */
329 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
330 { &vop_lock_desc, ulfs_lock }, /* lock */
331 { &vop_unlock_desc, ulfs_unlock }, /* unlock */
332 { &vop_bmap_desc, vn_fifo_bypass }, /* bmap */
333 { &vop_strategy_desc, vn_fifo_bypass }, /* strategy */
334 { &vop_print_desc, ulfs_print }, /* print */
335 { &vop_islocked_desc, ulfs_islocked }, /* islocked */
336 { &vop_pathconf_desc, vn_fifo_bypass }, /* pathconf */
337 { &vop_advlock_desc, vn_fifo_bypass }, /* advlock */
338 { &vop_bwrite_desc, lfs_bwrite }, /* bwrite */
339 { &vop_putpages_desc, vn_fifo_bypass }, /* putpages */
340 { &vop_openextattr_desc, lfs_openextattr }, /* openextattr */
341 { &vop_closeextattr_desc, lfs_closeextattr }, /* closeextattr */
342 { &vop_getextattr_desc, lfs_getextattr }, /* getextattr */
343 { &vop_setextattr_desc, lfs_setextattr }, /* setextattr */
344 { &vop_listextattr_desc, lfs_listextattr }, /* listextattr */
345 { &vop_deleteextattr_desc, lfs_deleteextattr }, /* deleteextattr */
346 { NULL, NULL }
347 };
348 const struct vnodeopv_desc lfs_fifoop_opv_desc =
349 { &lfs_fifoop_p, lfs_fifoop_entries };
350
351 #define LFS_READWRITE
352 #include <ufs/lfs/ulfs_readwrite.c>
353 #undef LFS_READWRITE
354
355 /*
356 * Synch an open file.
357 */
358 /* ARGSUSED */
359 int
360 lfs_fsync(void *v)
361 {
362 struct vop_fsync_args /* {
363 struct vnode *a_vp;
364 kauth_cred_t a_cred;
365 int a_flags;
366 off_t offlo;
367 off_t offhi;
368 } */ *ap = v;
369 struct vnode *vp = ap->a_vp;
370 int error, wait;
371 struct inode *ip = VTOI(vp);
372 struct lfs *fs = ip->i_lfs;
373
374 /* If we're mounted read-only, don't try to sync. */
375 if (fs->lfs_ronly)
376 return 0;
377
378 /* If a removed vnode is being cleaned, no need to sync here. */
379 if ((ap->a_flags & FSYNC_RECLAIM) != 0 && ip->i_mode == 0)
380 return 0;
381
382 /*
383 * Trickle sync simply adds this vnode to the pager list, as if
384 * the pagedaemon had requested a pageout.
385 */
386 if (ap->a_flags & FSYNC_LAZY) {
387 if (lfs_ignore_lazy_sync == 0) {
388 mutex_enter(&lfs_lock);
389 if (!(ip->i_flags & IN_PAGING)) {
390 ip->i_flags |= IN_PAGING;
391 TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip,
392 i_lfs_pchain);
393 }
394 wakeup(&lfs_writer_daemon);
395 mutex_exit(&lfs_lock);
396 }
397 return 0;
398 }
399
400 /*
401 * If a vnode is bring cleaned, flush it out before we try to
402 * reuse it. This prevents the cleaner from writing files twice
403 * in the same partial segment, causing an accounting underflow.
404 */
405 if (ap->a_flags & FSYNC_RECLAIM && ip->i_flags & IN_CLEANING) {
406 lfs_vflush(vp);
407 }
408
409 wait = (ap->a_flags & FSYNC_WAIT);
410 do {
411 mutex_enter(vp->v_interlock);
412 error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
413 round_page(ap->a_offhi),
414 PGO_CLEANIT | (wait ? PGO_SYNCIO : 0));
415 if (error == EAGAIN) {
416 mutex_enter(&lfs_lock);
417 mtsleep(&fs->lfs_avail, PCATCH | PUSER, "lfs_fsync",
418 hz / 100 + 1, &lfs_lock);
419 mutex_exit(&lfs_lock);
420 }
421 } while (error == EAGAIN);
422 if (error)
423 return error;
424
425 if ((ap->a_flags & FSYNC_DATAONLY) == 0)
426 error = lfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
427
428 if (error == 0 && ap->a_flags & FSYNC_CACHE) {
429 int l = 0;
430 error = VOP_IOCTL(ip->i_devvp, DIOCCACHESYNC, &l, FWRITE,
431 curlwp->l_cred);
432 }
433 if (wait && !VPISEMPTY(vp))
434 LFS_SET_UINO(ip, IN_MODIFIED);
435
436 return error;
437 }
438
439 /*
440 * Take IN_ADIROP off, then call ulfs_inactive.
441 */
442 int
443 lfs_inactive(void *v)
444 {
445 struct vop_inactive_args /* {
446 struct vnode *a_vp;
447 } */ *ap = v;
448
449 lfs_unmark_vnode(ap->a_vp);
450
451 /*
452 * The Ifile is only ever inactivated on unmount.
453 * Streamline this process by not giving it more dirty blocks.
454 */
455 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM) {
456 mutex_enter(&lfs_lock);
457 LFS_CLR_UINO(VTOI(ap->a_vp), IN_ALLMOD);
458 mutex_exit(&lfs_lock);
459 VOP_UNLOCK(ap->a_vp);
460 return 0;
461 }
462
463 #ifdef DEBUG
464 /*
465 * This might happen on unmount.
466 * XXX If it happens at any other time, it should be a panic.
467 */
468 if (ap->a_vp->v_uflag & VU_DIROP) {
469 struct inode *ip = VTOI(ap->a_vp);
470 printf("lfs_inactive: inactivating VU_DIROP? ino = %d\n", (int)ip->i_number);
471 }
472 #endif /* DIAGNOSTIC */
473
474 return ulfs_inactive(v);
475 }
476
477 int
478 lfs_set_dirop(struct vnode *dvp, struct vnode *vp)
479 {
480 struct lfs *fs;
481 int error;
482
483 KASSERT(VOP_ISLOCKED(dvp));
484 KASSERT(vp == NULL || VOP_ISLOCKED(vp));
485
486 fs = VTOI(dvp)->i_lfs;
487
488 ASSERT_NO_SEGLOCK(fs);
489 /*
490 * LFS_NRESERVE calculates direct and indirect blocks as well
491 * as an inode block; an overestimate in most cases.
492 */
493 if ((error = lfs_reserve(fs, dvp, vp, LFS_NRESERVE(fs))) != 0)
494 return (error);
495
496 restart:
497 mutex_enter(&lfs_lock);
498 if (fs->lfs_dirops == 0) {
499 mutex_exit(&lfs_lock);
500 lfs_check(dvp, LFS_UNUSED_LBN, 0);
501 mutex_enter(&lfs_lock);
502 }
503 while (fs->lfs_writer) {
504 error = mtsleep(&fs->lfs_dirops, (PRIBIO + 1) | PCATCH,
505 "lfs_sdirop", 0, &lfs_lock);
506 if (error == EINTR) {
507 mutex_exit(&lfs_lock);
508 goto unreserve;
509 }
510 }
511 if (lfs_dirvcount > LFS_MAX_DIROP && fs->lfs_dirops == 0) {
512 wakeup(&lfs_writer_daemon);
513 mutex_exit(&lfs_lock);
514 preempt();
515 goto restart;
516 }
517
518 if (lfs_dirvcount > LFS_MAX_DIROP) {
519 DLOG((DLOG_DIROP, "lfs_set_dirop: sleeping with dirops=%d, "
520 "dirvcount=%d\n", fs->lfs_dirops, lfs_dirvcount));
521 if ((error = mtsleep(&lfs_dirvcount,
522 PCATCH | PUSER | PNORELOCK, "lfs_maxdirop", 0,
523 &lfs_lock)) != 0) {
524 goto unreserve;
525 }
526 goto restart;
527 }
528
529 ++fs->lfs_dirops;
530 /* fs->lfs_doifile = 1; */ /* XXX why? --ks */
531 mutex_exit(&lfs_lock);
532
533 /* Hold a reference so SET_ENDOP will be happy */
534 vref(dvp);
535 if (vp) {
536 vref(vp);
537 MARK_VNODE(vp);
538 }
539
540 MARK_VNODE(dvp);
541 return 0;
542
543 unreserve:
544 lfs_reserve(fs, dvp, vp, -LFS_NRESERVE(fs));
545 return error;
546 }
547
548 /*
549 * Opposite of lfs_set_dirop... mostly. For now at least must call
550 * UNMARK_VNODE(dvp) explicitly first. (XXX: clean that up)
551 */
552 void
553 lfs_unset_dirop(struct lfs *fs, struct vnode *dvp, const char *str)
554 {
555 mutex_enter(&lfs_lock);
556 --fs->lfs_dirops;
557 if (!fs->lfs_dirops) {
558 if (fs->lfs_nadirop) {
559 panic("lfs_unset_dirop: %s: no dirops but "
560 " nadirop=%d", str,
561 fs->lfs_nadirop);
562 }
563 wakeup(&fs->lfs_writer);
564 mutex_exit(&lfs_lock);
565 lfs_check(dvp, LFS_UNUSED_LBN, 0);
566 } else {
567 mutex_exit(&lfs_lock);
568 }
569 lfs_reserve(fs, dvp, NULL, -LFS_NRESERVE(fs));
570 }
571
572 void
573 lfs_mark_vnode(struct vnode *vp)
574 {
575 struct inode *ip = VTOI(vp);
576 struct lfs *fs = ip->i_lfs;
577
578 mutex_enter(&lfs_lock);
579 if (!(ip->i_flag & IN_ADIROP)) {
580 if (!(vp->v_uflag & VU_DIROP)) {
581 mutex_exit(&lfs_lock);
582 mutex_enter(vp->v_interlock);
583 if (lfs_vref(vp) != 0)
584 panic("lfs_mark_vnode: could not vref");
585 mutex_enter(&lfs_lock);
586 ++lfs_dirvcount;
587 ++fs->lfs_dirvcount;
588 TAILQ_INSERT_TAIL(&fs->lfs_dchainhd, ip, i_lfs_dchain);
589 vp->v_uflag |= VU_DIROP;
590 }
591 ++fs->lfs_nadirop;
592 ip->i_flag &= ~IN_CDIROP;
593 ip->i_flag |= IN_ADIROP;
594 } else
595 KASSERT(vp->v_uflag & VU_DIROP);
596 mutex_exit(&lfs_lock);
597 }
598
599 void
600 lfs_unmark_vnode(struct vnode *vp)
601 {
602 struct inode *ip = VTOI(vp);
603
604 mutex_enter(&lfs_lock);
605 if (ip && (ip->i_flag & IN_ADIROP)) {
606 KASSERT(vp->v_uflag & VU_DIROP);
607 --ip->i_lfs->lfs_nadirop;
608 ip->i_flag &= ~IN_ADIROP;
609 }
610 mutex_exit(&lfs_lock);
611 }
612
613 int
614 lfs_symlink(void *v)
615 {
616 struct vop_symlink_v3_args /* {
617 struct vnode *a_dvp;
618 struct vnode **a_vpp;
619 struct componentname *a_cnp;
620 struct vattr *a_vap;
621 char *a_target;
622 } */ *ap = v;
623 struct lfs *fs;
624 struct vnode *dvp, **vpp;
625 struct inode *ip;
626 struct ulfs_lookup_results *ulr;
627 ssize_t len; /* XXX should be size_t */
628 int error;
629
630 dvp = ap->a_dvp;
631 vpp = ap->a_vpp;
632
633 KASSERT(vpp != NULL);
634 KASSERT(*vpp == NULL);
635
636 /* XXX should handle this material another way */
637 ulr = &VTOI(ap->a_dvp)->i_crap;
638 ULFS_CHECK_CRAPCOUNTER(VTOI(ap->a_dvp));
639
640 fs = VFSTOULFS(dvp->v_mount)->um_lfs;
641 ASSERT_NO_SEGLOCK(fs);
642 if (fs->lfs_ronly) {
643 return EROFS;
644 }
645
646 /*
647 * Get a new vnode *before* adjusting the dirop count, to
648 * avoid a deadlock in getnewvnode(), if we have a stacked
649 * filesystem mounted on top of us.
650 *
651 * NB: this means we have to destroy the new vnode on error.
652 */
653
654 error = getnewvnode(VT_LFS, dvp->v_mount, lfs_vnodeop_p, NULL, vpp);
655 if (error) {
656 DLOG((DLOG_ALLOC, "lfs_mkdir: dvp %p error %d\n", dvp, error));
657 return error;
658 }
659 KASSERT(*vpp != NULL);
660
661 error = lfs_set_dirop(dvp, NULL);
662 if (error) {
663 ungetnewvnode(*vpp);
664 *vpp = NULL;
665 return error;
666 }
667
668 fstrans_start(dvp->v_mount, FSTRANS_SHARED);
669 error = ulfs_makeinode(LFS_IFLNK | ap->a_vap->va_mode, dvp, ulr,
670 vpp, ap->a_cnp);
671 if (error) {
672 goto out;
673 }
674
675 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
676 ip = VTOI(*vpp);
677
678 len = strlen(ap->a_target);
679 if (len < ip->i_lfs->um_maxsymlinklen) {
680 memcpy((char *)SHORTLINK(ip), ap->a_target, len);
681 ip->i_size = len;
682 DIP_ASSIGN(ip, size, len);
683 uvm_vnp_setsize(*vpp, ip->i_size);
684 ip->i_flag |= IN_CHANGE | IN_UPDATE;
685 if ((*vpp)->v_mount->mnt_flag & MNT_RELATIME)
686 ip->i_flag |= IN_ACCESS;
687 } else {
688 error = vn_rdwr(UIO_WRITE, *vpp, ap->a_target, len, (off_t)0,
689 UIO_SYSSPACE, IO_NODELOCKED | IO_JOURNALLOCKED,
690 ap->a_cnp->cn_cred, NULL, NULL);
691 }
692
693 VOP_UNLOCK(*vpp);
694 if (error)
695 vrele(*vpp);
696
697 out:
698 fstrans_done(dvp->v_mount);
699
700 UNMARK_VNODE(dvp);
701 /* XXX: is it even possible for the symlink to get MARK'd? */
702 UNMARK_VNODE(*vpp);
703 if (!((*vpp)->v_uflag & VU_DIROP)) {
704 KASSERT(error != 0);
705 ungetnewvnode(*vpp);
706 *vpp = NULL;
707 }
708 else {
709 KASSERT(error == 0);
710 }
711 lfs_unset_dirop(fs, dvp, "symlink");
712
713 vrele(dvp);
714 return (error);
715 }
716
717 int
718 lfs_mknod(void *v)
719 {
720 struct vop_mknod_v3_args /* {
721 struct vnode *a_dvp;
722 struct vnode **a_vpp;
723 struct componentname *a_cnp;
724 struct vattr *a_vap;
725 } */ *ap = v;
726 struct lfs *fs;
727 struct vnode *dvp, **vpp;
728 struct vattr *vap;
729 struct inode *ip;
730 int error;
731 struct mount *mp;
732 ino_t ino;
733 struct ulfs_lookup_results *ulr;
734
735 dvp = ap->a_dvp;
736 vpp = ap->a_vpp;
737 vap = ap->a_vap;
738
739 KASSERT(vpp != NULL);
740 KASSERT(*vpp == NULL);
741
742 /* XXX should handle this material another way */
743 ulr = &VTOI(dvp)->i_crap;
744 ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
745
746 fs = VFSTOULFS(dvp->v_mount)->um_lfs;
747 ASSERT_NO_SEGLOCK(fs);
748 if (fs->lfs_ronly) {
749 return EROFS;
750 }
751
752 /*
753 * Get a new vnode *before* adjusting the dirop count, to
754 * avoid a deadlock in getnewvnode(), if we have a stacked
755 * filesystem mounted on top of us.
756 *
757 * NB: this means we have to destroy the new vnode on error.
758 */
759
760 error = getnewvnode(VT_LFS, dvp->v_mount, lfs_vnodeop_p, NULL, vpp);
761 if (error) {
762 DLOG((DLOG_ALLOC, "lfs_mknod: dvp %p error %d\n", dvp, error));
763 return error;
764 }
765 KASSERT(*vpp != NULL);
766
767 error = lfs_set_dirop(dvp, NULL);
768 if (error) {
769 ungetnewvnode(*vpp);
770 *vpp = NULL;
771 return error;
772 }
773
774 fstrans_start(ap->a_dvp->v_mount, FSTRANS_SHARED);
775 error = ulfs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
776 dvp, ulr, vpp, ap->a_cnp);
777
778 /* Either way we're done with the dirop at this point */
779 UNMARK_VNODE(dvp);
780 UNMARK_VNODE(*vpp);
781 if (!((*vpp)->v_uflag & VU_DIROP)) {
782 KASSERT(error != 0);
783 ungetnewvnode(*vpp);
784 *vpp = NULL;
785 }
786 else {
787 KASSERT(error == 0);
788 }
789 lfs_unset_dirop(fs, dvp, "mknod");
790 /*
791 * XXX this is where this used to be (though inside some evil
792 * macros) but it clearly should be moved further down.
793 * - dholland 20140515
794 */
795 vrele(dvp);
796
797 if (error) {
798 fstrans_done(ap->a_dvp->v_mount);
799 *vpp = NULL;
800 return (error);
801 }
802
803 VN_KNOTE(dvp, NOTE_WRITE);
804 ip = VTOI(*vpp);
805 mp = (*vpp)->v_mount;
806 ino = ip->i_number;
807 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
808 if (vap->va_rdev != VNOVAL) {
809 struct ulfsmount *ump = ip->i_ump;
810 KASSERT(fs == ip->i_lfs);
811 /*
812 * Want to be able to use this to make badblock
813 * inodes, so don't truncate the dev number.
814 */
815 if (ump->um_fstype == ULFS1)
816 ip->i_ffs1_rdev = ulfs_rw32(vap->va_rdev,
817 ULFS_MPNEEDSWAP(fs));
818 else
819 ip->i_ffs2_rdev = ulfs_rw64(vap->va_rdev,
820 ULFS_MPNEEDSWAP(fs));
821 }
822
823 /*
824 * Call fsync to write the vnode so that we don't have to deal with
825 * flushing it when it's marked VU_DIROP or reclaiming.
826 *
827 * XXX KS - If we can't flush we also can't call vgone(), so must
828 * return. But, that leaves this vnode in limbo, also not good.
829 * Can this ever happen (barring hardware failure)?
830 */
831 if ((error = VOP_FSYNC(*vpp, NOCRED, FSYNC_WAIT, 0, 0)) != 0) {
832 panic("lfs_mknod: couldn't fsync (ino %llu)",
833 (unsigned long long)ino);
834 /* return (error); */
835 }
836 /*
837 * Remove vnode so that it will be reloaded by VFS_VGET and
838 * checked to see if it is an alias of an existing entry in
839 * the inode cache.
840 */
841 /* Used to be vput, but that causes us to call VOP_INACTIVE twice. */
842
843 (*vpp)->v_type = VNON;
844 VOP_UNLOCK(*vpp);
845 vgone(*vpp);
846 error = VFS_VGET(mp, ino, vpp);
847
848 fstrans_done(ap->a_dvp->v_mount);
849 if (error != 0) {
850 *vpp = NULL;
851 return (error);
852 }
853 VOP_UNLOCK(*vpp);
854 return (0);
855 }
856
857 /*
858 * Create a regular file
859 */
860 int
861 lfs_create(void *v)
862 {
863 struct vop_create_v3_args /* {
864 struct vnode *a_dvp;
865 struct vnode **a_vpp;
866 struct componentname *a_cnp;
867 struct vattr *a_vap;
868 } */ *ap = v;
869 struct lfs *fs;
870 struct vnode *dvp, **vpp;
871 struct vattr *vap;
872 struct ulfs_lookup_results *ulr;
873 int error;
874
875 dvp = ap->a_dvp;
876 vpp = ap->a_vpp;
877 vap = ap->a_vap;
878
879 KASSERT(vpp != NULL);
880 KASSERT(*vpp == NULL);
881
882 /* XXX should handle this material another way */
883 ulr = &VTOI(dvp)->i_crap;
884 ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
885
886 fs = VFSTOULFS(dvp->v_mount)->um_lfs;
887 ASSERT_NO_SEGLOCK(fs);
888 if (fs->lfs_ronly) {
889 return EROFS;
890 }
891
892 /*
893 * Get a new vnode *before* adjusting the dirop count, to
894 * avoid a deadlock in getnewvnode(), if we have a stacked
895 * filesystem mounted on top of us.
896 *
897 * NB: this means we have to destroy the new vnode on error.
898 */
899
900 error = getnewvnode(VT_LFS, dvp->v_mount, lfs_vnodeop_p, NULL, vpp);
901 if (error) {
902 DLOG((DLOG_ALLOC, "lfs_create: dvp %p error %d\n", dvp,error));
903 return error;
904 }
905 error = lfs_set_dirop(dvp, NULL);
906 if (error) {
907 ungetnewvnode(*vpp);
908 *vpp = NULL;
909 return error;
910 }
911
912 fstrans_start(dvp->v_mount, FSTRANS_SHARED);
913 error = ulfs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
914 dvp, ulr, vpp, ap->a_cnp);
915 if (error) {
916 fstrans_done(dvp->v_mount);
917 goto out;
918 }
919 fstrans_done(dvp->v_mount);
920 VN_KNOTE(dvp, NOTE_WRITE);
921 VOP_UNLOCK(*vpp);
922
923 out:
924
925 UNMARK_VNODE(dvp);
926 UNMARK_VNODE(*vpp);
927 if (!((*vpp)->v_uflag & VU_DIROP)) {
928 KASSERT(error != 0);
929 ungetnewvnode(*vpp);
930 *vpp = NULL;
931 }
932 else {
933 KASSERT(error == 0);
934 }
935 lfs_unset_dirop(fs, dvp, "create");
936
937 vrele(dvp);
938 return (error);
939 }
940
941 int
942 lfs_mkdir(void *v)
943 {
944 struct vop_mkdir_v3_args /* {
945 struct vnode *a_dvp;
946 struct vnode **a_vpp;
947 struct componentname *a_cnp;
948 struct vattr *a_vap;
949 } */ *ap = v;
950 struct lfs *fs;
951 struct vnode *dvp, *tvp, **vpp;
952 struct inode *dp, *ip;
953 struct componentname *cnp;
954 struct vattr *vap;
955 struct ulfs_lookup_results *ulr;
956 struct buf *bp;
957 struct lfs_dirtemplate dirtemplate;
958 struct lfs_direct *newdir;
959 int dirblksiz;
960 int dmode;
961 int error;
962
963 dvp = ap->a_dvp;
964 tvp = NULL;
965 vpp = ap->a_vpp;
966 cnp = ap->a_cnp;
967 vap = ap->a_vap;
968
969 dp = VTOI(dvp);
970 ip = NULL;
971
972 KASSERT(vpp != NULL);
973 KASSERT(*vpp == NULL);
974
975 /* XXX should handle this material another way */
976 ulr = &dp->i_crap;
977 ULFS_CHECK_CRAPCOUNTER(dp);
978
979 fs = VFSTOULFS(dvp->v_mount)->um_lfs;
980 ASSERT_NO_SEGLOCK(fs);
981 if (fs->lfs_ronly) {
982 return EROFS;
983 }
984 dirblksiz = fs->um_dirblksiz;
985
986 /*
987 * Get a new vnode *before* adjusting the dirop count, to
988 * avoid a deadlock in getnewvnode(), if we have a stacked
989 * filesystem mounted on top of us.
990 *
991 * NB: this means we have to destroy the new vnode on error.
992 */
993
994 error = getnewvnode(VT_LFS, dvp->v_mount, lfs_vnodeop_p, NULL, vpp);
995 if (error) {
996 DLOG((DLOG_ALLOC, "lfs_mkdir: dvp %p error %d\n", dvp, error));
997 return error;
998 }
999 error = lfs_set_dirop(dvp, NULL);
1000 if (error) {
1001 ungetnewvnode(*vpp);
1002 *vpp = NULL;
1003 return error;
1004 }
1005
1006 fstrans_start(dvp->v_mount, FSTRANS_SHARED);
1007
1008 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1009 error = EMLINK;
1010 goto out;
1011 }
1012
1013 dmode = vap->va_mode & ACCESSPERMS;
1014 dmode |= LFS_IFDIR;
1015 /*
1016 * Must simulate part of ulfs_makeinode here to acquire the inode,
1017 * but not have it entered in the parent directory. The entry is
1018 * made later after writing "." and ".." entries.
1019 */
1020 if ((error = lfs_valloc(dvp, dmode, cnp->cn_cred, vpp)) != 0)
1021 goto out;
1022
1023 tvp = *vpp;
1024 ip = VTOI(tvp);
1025
1026 ip->i_uid = kauth_cred_geteuid(cnp->cn_cred);
1027 DIP_ASSIGN(ip, uid, ip->i_uid);
1028 ip->i_gid = dp->i_gid;
1029 DIP_ASSIGN(ip, gid, ip->i_gid);
1030 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
1031 if ((error = lfs_chkiq(ip, 1, cnp->cn_cred, 0))) {
1032 lfs_vfree(tvp, ip->i_number, dmode);
1033 fstrans_done(dvp->v_mount);
1034 vput(tvp);
1035 goto out2;
1036 }
1037 #endif
1038 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1039 ip->i_mode = dmode;
1040 DIP_ASSIGN(ip, mode, dmode);
1041 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1042 ip->i_nlink = 2;
1043 DIP_ASSIGN(ip, nlink, 2);
1044 if (cnp->cn_flags & ISWHITEOUT) {
1045 ip->i_flags |= UF_OPAQUE;
1046 DIP_ASSIGN(ip, flags, ip->i_flags);
1047 }
1048
1049 /*
1050 * Bump link count in parent directory to reflect work done below.
1051 */
1052 dp->i_nlink++;
1053 DIP_ASSIGN(dp, nlink, dp->i_nlink);
1054 dp->i_flag |= IN_CHANGE;
1055 if ((error = lfs_update(dvp, NULL, NULL, UPDATE_DIROP)) != 0)
1056 goto bad;
1057
1058 /*
1059 * Initialize directory with "." and ".." from static template.
1060 */
1061 dirtemplate = mastertemplate;
1062 dirtemplate.dotdot_reclen = dirblksiz - dirtemplate.dot_reclen;
1063 dirtemplate.dot_ino = ulfs_rw32(ip->i_number, ULFS_MPNEEDSWAP(fs));
1064 dirtemplate.dotdot_ino = ulfs_rw32(dp->i_number, ULFS_MPNEEDSWAP(fs));
1065 dirtemplate.dot_reclen = ulfs_rw16(dirtemplate.dot_reclen,
1066 ULFS_MPNEEDSWAP(fs));
1067 dirtemplate.dotdot_reclen = ulfs_rw16(dirtemplate.dotdot_reclen,
1068 ULFS_MPNEEDSWAP(fs));
1069 if (fs->um_maxsymlinklen <= 0) {
1070 #if BYTE_ORDER == LITTLE_ENDIAN
1071 if (ULFS_MPNEEDSWAP(fs) == 0)
1072 #else
1073 if (ULFS_MPNEEDSWAP(fs) != 0)
1074 #endif
1075 {
1076 dirtemplate.dot_type = dirtemplate.dot_namlen;
1077 dirtemplate.dotdot_type = dirtemplate.dotdot_namlen;
1078 dirtemplate.dot_namlen = dirtemplate.dotdot_namlen = 0;
1079 } else
1080 dirtemplate.dot_type = dirtemplate.dotdot_type = 0;
1081 }
1082 if ((error = lfs_balloc(tvp, (off_t)0, dirblksiz, cnp->cn_cred,
1083 B_CLRBUF, &bp)) != 0)
1084 goto bad;
1085 ip->i_size = dirblksiz;
1086 DIP_ASSIGN(ip, size, dirblksiz);
1087 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1088 uvm_vnp_setsize(tvp, ip->i_size);
1089 memcpy((void *)bp->b_data, (void *)&dirtemplate, sizeof dirtemplate);
1090
1091 /*
1092 * Directory set up; now install its entry in the parent directory.
1093 */
1094 if ((error = VOP_BWRITE(bp->b_vp, bp)) != 0)
1095 goto bad;
1096 if ((error = lfs_update(tvp, NULL, NULL, UPDATE_DIROP)) != 0) {
1097 goto bad;
1098 }
1099 newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
1100 ulfs_makedirentry(ip, cnp, newdir);
1101 error = ulfs_direnter(dvp, ulr, tvp, newdir, cnp, bp);
1102 pool_cache_put(ulfs_direct_cache, newdir);
1103 bad:
1104 if (error == 0) {
1105 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1106 VOP_UNLOCK(tvp);
1107 } else {
1108 dp->i_nlink--;
1109 DIP_ASSIGN(dp, nlink, dp->i_nlink);
1110 dp->i_flag |= IN_CHANGE;
1111 /*
1112 * No need to do an explicit lfs_truncate here, vrele will
1113 * do this for us because we set the link count to 0.
1114 */
1115 ip->i_nlink = 0;
1116 DIP_ASSIGN(ip, nlink, 0);
1117 ip->i_flag |= IN_CHANGE;
1118 /* If IN_ADIROP, account for it */
1119 lfs_unmark_vnode(tvp);
1120 vput(tvp);
1121 }
1122
1123 out:
1124 fstrans_done(dvp->v_mount);
1125 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
1126 out2:
1127 #endif
1128
1129 UNMARK_VNODE(dvp);
1130 UNMARK_VNODE(*vpp);
1131 if (!((*vpp)->v_uflag & VU_DIROP)) {
1132 KASSERT(error != 0);
1133 ungetnewvnode(*vpp);
1134 *vpp = NULL;
1135 }
1136 else {
1137 KASSERT(error == 0);
1138 }
1139 lfs_unset_dirop(fs, dvp, "mkdir");
1140
1141 vrele(dvp);
1142 return (error);
1143 }
1144
1145 int
1146 lfs_remove(void *v)
1147 {
1148 struct vop_remove_args /* {
1149 struct vnode *a_dvp;
1150 struct vnode *a_vp;
1151 struct componentname *a_cnp;
1152 } */ *ap = v;
1153 struct vnode *dvp, *vp;
1154 struct inode *ip;
1155 int error;
1156
1157 dvp = ap->a_dvp;
1158 vp = ap->a_vp;
1159 ip = VTOI(vp);
1160 if ((error = lfs_set_dirop(dvp, vp)) != 0) {
1161 if (dvp == vp)
1162 vrele(vp);
1163 else
1164 vput(vp);
1165 vput(dvp);
1166 return error;
1167 }
1168 error = ulfs_remove(ap);
1169 if (ip->i_nlink == 0)
1170 lfs_orphan(ip->i_lfs, ip->i_number);
1171
1172 UNMARK_VNODE(dvp);
1173 if (ap->a_vp) {
1174 UNMARK_VNODE(ap->a_vp);
1175 }
1176 lfs_unset_dirop(ip->i_lfs, dvp, "remove");
1177 vrele(dvp);
1178 if (ap->a_vp) {
1179 vrele(ap->a_vp);
1180 }
1181
1182 return (error);
1183 }
1184
1185 int
1186 lfs_rmdir(void *v)
1187 {
1188 struct vop_rmdir_args /* {
1189 struct vnodeop_desc *a_desc;
1190 struct vnode *a_dvp;
1191 struct vnode *a_vp;
1192 struct componentname *a_cnp;
1193 } */ *ap = v;
1194 struct vnode *vp;
1195 struct inode *ip;
1196 int error;
1197
1198 vp = ap->a_vp;
1199 ip = VTOI(vp);
1200 if ((error = lfs_set_dirop(ap->a_dvp, ap->a_vp)) != 0) {
1201 if (ap->a_dvp == vp)
1202 vrele(ap->a_dvp);
1203 else
1204 vput(ap->a_dvp);
1205 vput(vp);
1206 return error;
1207 }
1208 error = ulfs_rmdir(ap);
1209 if (ip->i_nlink == 0)
1210 lfs_orphan(ip->i_lfs, ip->i_number);
1211
1212 UNMARK_VNODE(ap->a_dvp);
1213 if (ap->a_vp) {
1214 UNMARK_VNODE(ap->a_vp);
1215 }
1216 lfs_unset_dirop(ip->i_lfs, ap->a_dvp, "rmdir");
1217 vrele(ap->a_dvp);
1218 if (ap->a_vp) {
1219 vrele(ap->a_vp);
1220 }
1221
1222 return (error);
1223 }
1224
1225 int
1226 lfs_link(void *v)
1227 {
1228 struct vop_link_args /* {
1229 struct vnode *a_dvp;
1230 struct vnode *a_vp;
1231 struct componentname *a_cnp;
1232 } */ *ap = v;
1233 struct lfs *fs;
1234 struct vnode *dvp;
1235 int error;
1236
1237 dvp = ap->a_dvp;
1238
1239 fs = VFSTOULFS(dvp->v_mount)->um_lfs;
1240 ASSERT_NO_SEGLOCK(fs);
1241 if (fs->lfs_ronly) {
1242 return EROFS;
1243 }
1244
1245 error = lfs_set_dirop(dvp, NULL);
1246 if (error) {
1247 /*
1248 * XXX dholland 20140515 this was here before but must
1249 * be wrong.
1250 */
1251 vput(dvp);
1252
1253 return error;
1254 }
1255
1256 error = ulfs_link(ap);
1257
1258 UNMARK_VNODE(dvp);
1259 lfs_unset_dirop(fs, dvp, "link");
1260 vrele(dvp);
1261
1262 return (error);
1263 }
1264
1265 /* XXX hack to avoid calling ITIMES in getattr */
1266 int
1267 lfs_getattr(void *v)
1268 {
1269 struct vop_getattr_args /* {
1270 struct vnode *a_vp;
1271 struct vattr *a_vap;
1272 kauth_cred_t a_cred;
1273 } */ *ap = v;
1274 struct vnode *vp = ap->a_vp;
1275 struct inode *ip = VTOI(vp);
1276 struct vattr *vap = ap->a_vap;
1277 struct lfs *fs = ip->i_lfs;
1278
1279 fstrans_start(vp->v_mount, FSTRANS_SHARED);
1280 /*
1281 * Copy from inode table
1282 */
1283 vap->va_fsid = ip->i_dev;
1284 vap->va_fileid = ip->i_number;
1285 vap->va_mode = ip->i_mode & ~LFS_IFMT;
1286 vap->va_nlink = ip->i_nlink;
1287 vap->va_uid = ip->i_uid;
1288 vap->va_gid = ip->i_gid;
1289 vap->va_rdev = (dev_t)ip->i_ffs1_rdev;
1290 vap->va_size = vp->v_size;
1291 vap->va_atime.tv_sec = ip->i_ffs1_atime;
1292 vap->va_atime.tv_nsec = ip->i_ffs1_atimensec;
1293 vap->va_mtime.tv_sec = ip->i_ffs1_mtime;
1294 vap->va_mtime.tv_nsec = ip->i_ffs1_mtimensec;
1295 vap->va_ctime.tv_sec = ip->i_ffs1_ctime;
1296 vap->va_ctime.tv_nsec = ip->i_ffs1_ctimensec;
1297 vap->va_flags = ip->i_flags;
1298 vap->va_gen = ip->i_gen;
1299 /* this doesn't belong here */
1300 if (vp->v_type == VBLK)
1301 vap->va_blocksize = BLKDEV_IOSIZE;
1302 else if (vp->v_type == VCHR)
1303 vap->va_blocksize = MAXBSIZE;
1304 else
1305 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
1306 vap->va_bytes = lfs_fsbtob(fs, (u_quad_t)ip->i_lfs_effnblks);
1307 vap->va_type = vp->v_type;
1308 vap->va_filerev = ip->i_modrev;
1309 fstrans_done(vp->v_mount);
1310 return (0);
1311 }
1312
1313 /*
1314 * Check to make sure the inode blocks won't choke the buffer
1315 * cache, then call ulfs_setattr as usual.
1316 */
1317 int
1318 lfs_setattr(void *v)
1319 {
1320 struct vop_setattr_args /* {
1321 struct vnode *a_vp;
1322 struct vattr *a_vap;
1323 kauth_cred_t a_cred;
1324 } */ *ap = v;
1325 struct vnode *vp = ap->a_vp;
1326
1327 lfs_check(vp, LFS_UNUSED_LBN, 0);
1328 return ulfs_setattr(v);
1329 }
1330
1331 /*
1332 * Release the block we hold on lfs_newseg wrapping. Called on file close,
1333 * or explicitly from LFCNWRAPGO. Called with the interlock held.
1334 */
1335 static int
1336 lfs_wrapgo(struct lfs *fs, struct inode *ip, int waitfor)
1337 {
1338 if (fs->lfs_stoplwp != curlwp)
1339 return EBUSY;
1340
1341 fs->lfs_stoplwp = NULL;
1342 cv_signal(&fs->lfs_stopcv);
1343
1344 KASSERT(fs->lfs_nowrap > 0);
1345 if (fs->lfs_nowrap <= 0) {
1346 return 0;
1347 }
1348
1349 if (--fs->lfs_nowrap == 0) {
1350 log(LOG_NOTICE, "%s: re-enabled log wrap\n", fs->lfs_fsmnt);
1351 wakeup(&fs->lfs_wrappass);
1352 lfs_wakeup_cleaner(fs);
1353 }
1354 if (waitfor) {
1355 mtsleep(&fs->lfs_nextseg, PCATCH | PUSER, "segment",
1356 0, &lfs_lock);
1357 }
1358
1359 return 0;
1360 }
1361
1362 /*
1363 * Close called.
1364 *
1365 * Update the times on the inode.
1366 */
1367 /* ARGSUSED */
1368 int
1369 lfs_close(void *v)
1370 {
1371 struct vop_close_args /* {
1372 struct vnode *a_vp;
1373 int a_fflag;
1374 kauth_cred_t a_cred;
1375 } */ *ap = v;
1376 struct vnode *vp = ap->a_vp;
1377 struct inode *ip = VTOI(vp);
1378 struct lfs *fs = ip->i_lfs;
1379
1380 if ((ip->i_number == ULFS_ROOTINO || ip->i_number == LFS_IFILE_INUM) &&
1381 fs->lfs_stoplwp == curlwp) {
1382 mutex_enter(&lfs_lock);
1383 log(LOG_NOTICE, "lfs_close: releasing log wrap control\n");
1384 lfs_wrapgo(fs, ip, 0);
1385 mutex_exit(&lfs_lock);
1386 }
1387
1388 if (vp == ip->i_lfs->lfs_ivnode &&
1389 vp->v_mount->mnt_iflag & IMNT_UNMOUNT)
1390 return 0;
1391
1392 fstrans_start(vp->v_mount, FSTRANS_SHARED);
1393 if (vp->v_usecount > 1 && vp != ip->i_lfs->lfs_ivnode) {
1394 LFS_ITIMES(ip, NULL, NULL, NULL);
1395 }
1396 fstrans_done(vp->v_mount);
1397 return (0);
1398 }
1399
1400 /*
1401 * Close wrapper for special devices.
1402 *
1403 * Update the times on the inode then do device close.
1404 */
1405 int
1406 lfsspec_close(void *v)
1407 {
1408 struct vop_close_args /* {
1409 struct vnode *a_vp;
1410 int a_fflag;
1411 kauth_cred_t a_cred;
1412 } */ *ap = v;
1413 struct vnode *vp;
1414 struct inode *ip;
1415
1416 vp = ap->a_vp;
1417 ip = VTOI(vp);
1418 if (vp->v_usecount > 1) {
1419 LFS_ITIMES(ip, NULL, NULL, NULL);
1420 }
1421 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
1422 }
1423
1424 /*
1425 * Close wrapper for fifo's.
1426 *
1427 * Update the times on the inode then do device close.
1428 */
1429 int
1430 lfsfifo_close(void *v)
1431 {
1432 struct vop_close_args /* {
1433 struct vnode *a_vp;
1434 int a_fflag;
1435 kauth_cred_ a_cred;
1436 } */ *ap = v;
1437 struct vnode *vp;
1438 struct inode *ip;
1439
1440 vp = ap->a_vp;
1441 ip = VTOI(vp);
1442 if (ap->a_vp->v_usecount > 1) {
1443 LFS_ITIMES(ip, NULL, NULL, NULL);
1444 }
1445 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
1446 }
1447
1448 /*
1449 * Reclaim an inode so that it can be used for other purposes.
1450 */
1451
1452 int
1453 lfs_reclaim(void *v)
1454 {
1455 struct vop_reclaim_args /* {
1456 struct vnode *a_vp;
1457 } */ *ap = v;
1458 struct vnode *vp = ap->a_vp;
1459 struct inode *ip = VTOI(vp);
1460 struct lfs *fs = ip->i_lfs;
1461 int error;
1462
1463 /*
1464 * The inode must be freed and updated before being removed
1465 * from its hash chain. Other threads trying to gain a hold
1466 * or lock on the inode will be stalled.
1467 */
1468 if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1469 lfs_vfree(vp, ip->i_number, ip->i_omode);
1470
1471 mutex_enter(&lfs_lock);
1472 LFS_CLR_UINO(ip, IN_ALLMOD);
1473 mutex_exit(&lfs_lock);
1474 if ((error = ulfs_reclaim(vp)))
1475 return (error);
1476
1477 /*
1478 * Take us off the paging and/or dirop queues if we were on them.
1479 * We shouldn't be on them.
1480 */
1481 mutex_enter(&lfs_lock);
1482 if (ip->i_flags & IN_PAGING) {
1483 log(LOG_WARNING, "%s: reclaimed vnode is IN_PAGING\n",
1484 fs->lfs_fsmnt);
1485 ip->i_flags &= ~IN_PAGING;
1486 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
1487 }
1488 if (vp->v_uflag & VU_DIROP) {
1489 panic("reclaimed vnode is VU_DIROP");
1490 vp->v_uflag &= ~VU_DIROP;
1491 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
1492 }
1493 mutex_exit(&lfs_lock);
1494
1495 pool_put(&lfs_dinode_pool, ip->i_din.ffs1_din);
1496 lfs_deregister_all(vp);
1497 pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
1498 ip->inode_ext.lfs = NULL;
1499 genfs_node_destroy(vp);
1500 pool_put(&lfs_inode_pool, vp->v_data);
1501 vp->v_data = NULL;
1502 return (0);
1503 }
1504
1505 /*
1506 * Read a block from a storage device.
1507 *
1508 * Calculate the logical to physical mapping if not done already,
1509 * then call the device strategy routine.
1510 *
1511 * In order to avoid reading blocks that are in the process of being
1512 * written by the cleaner---and hence are not mutexed by the normal
1513 * buffer cache / page cache mechanisms---check for collisions before
1514 * reading.
1515 *
1516 * We inline ulfs_strategy to make sure that the VOP_BMAP occurs *before*
1517 * the active cleaner test.
1518 *
1519 * XXX This code assumes that lfs_markv makes synchronous checkpoints.
1520 */
1521 int
1522 lfs_strategy(void *v)
1523 {
1524 struct vop_strategy_args /* {
1525 struct vnode *a_vp;
1526 struct buf *a_bp;
1527 } */ *ap = v;
1528 struct buf *bp;
1529 struct lfs *fs;
1530 struct vnode *vp;
1531 struct inode *ip;
1532 daddr_t tbn;
1533 #define MAXLOOP 25
1534 int i, sn, error, slept, loopcount;
1535
1536 bp = ap->a_bp;
1537 vp = ap->a_vp;
1538 ip = VTOI(vp);
1539 fs = ip->i_lfs;
1540
1541 /* lfs uses its strategy routine only for read */
1542 KASSERT(bp->b_flags & B_READ);
1543
1544 if (vp->v_type == VBLK || vp->v_type == VCHR)
1545 panic("lfs_strategy: spec");
1546 KASSERT(bp->b_bcount != 0);
1547 if (bp->b_blkno == bp->b_lblkno) {
1548 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1549 NULL);
1550 if (error) {
1551 bp->b_error = error;
1552 bp->b_resid = bp->b_bcount;
1553 biodone(bp);
1554 return (error);
1555 }
1556 if ((long)bp->b_blkno == -1) /* no valid data */
1557 clrbuf(bp);
1558 }
1559 if ((long)bp->b_blkno < 0) { /* block is not on disk */
1560 bp->b_resid = bp->b_bcount;
1561 biodone(bp);
1562 return (0);
1563 }
1564
1565 slept = 1;
1566 loopcount = 0;
1567 mutex_enter(&lfs_lock);
1568 while (slept && fs->lfs_seglock) {
1569 mutex_exit(&lfs_lock);
1570 /*
1571 * Look through list of intervals.
1572 * There will only be intervals to look through
1573 * if the cleaner holds the seglock.
1574 * Since the cleaner is synchronous, we can trust
1575 * the list of intervals to be current.
1576 */
1577 tbn = LFS_DBTOFSB(fs, bp->b_blkno);
1578 sn = lfs_dtosn(fs, tbn);
1579 slept = 0;
1580 for (i = 0; i < fs->lfs_cleanind; i++) {
1581 if (sn == lfs_dtosn(fs, fs->lfs_cleanint[i]) &&
1582 tbn >= fs->lfs_cleanint[i]) {
1583 DLOG((DLOG_CLEAN,
1584 "lfs_strategy: ino %d lbn %" PRId64
1585 " ind %d sn %d fsb %" PRIx32
1586 " given sn %d fsb %" PRIx64 "\n",
1587 ip->i_number, bp->b_lblkno, i,
1588 lfs_dtosn(fs, fs->lfs_cleanint[i]),
1589 fs->lfs_cleanint[i], sn, tbn));
1590 DLOG((DLOG_CLEAN,
1591 "lfs_strategy: sleeping on ino %d lbn %"
1592 PRId64 "\n", ip->i_number, bp->b_lblkno));
1593 mutex_enter(&lfs_lock);
1594 if (LFS_SEGLOCK_HELD(fs) && fs->lfs_iocount) {
1595 /*
1596 * Cleaner can't wait for itself.
1597 * Instead, wait for the blocks
1598 * to be written to disk.
1599 * XXX we need pribio in the test
1600 * XXX here.
1601 */
1602 mtsleep(&fs->lfs_iocount,
1603 (PRIBIO + 1) | PNORELOCK,
1604 "clean2", hz/10 + 1,
1605 &lfs_lock);
1606 slept = 1;
1607 ++loopcount;
1608 break;
1609 } else if (fs->lfs_seglock) {
1610 mtsleep(&fs->lfs_seglock,
1611 (PRIBIO + 1) | PNORELOCK,
1612 "clean1", 0,
1613 &lfs_lock);
1614 slept = 1;
1615 break;
1616 }
1617 mutex_exit(&lfs_lock);
1618 }
1619 }
1620 mutex_enter(&lfs_lock);
1621 if (loopcount > MAXLOOP) {
1622 printf("lfs_strategy: breaking out of clean2 loop\n");
1623 break;
1624 }
1625 }
1626 mutex_exit(&lfs_lock);
1627
1628 vp = ip->i_devvp;
1629 return VOP_STRATEGY(vp, bp);
1630 }
1631
1632 /*
1633 * Inline lfs_segwrite/lfs_writevnodes, but just for dirops.
1634 * Technically this is a checkpoint (the on-disk state is valid)
1635 * even though we are leaving out all the file data.
1636 */
1637 int
1638 lfs_flush_dirops(struct lfs *fs)
1639 {
1640 struct inode *ip, *nip;
1641 struct vnode *vp;
1642 extern int lfs_dostats;
1643 struct segment *sp;
1644 int flags = 0;
1645 int error = 0;
1646
1647 ASSERT_MAYBE_SEGLOCK(fs);
1648 KASSERT(fs->lfs_nadirop == 0);
1649
1650 if (fs->lfs_ronly)
1651 return EROFS;
1652
1653 mutex_enter(&lfs_lock);
1654 if (TAILQ_FIRST(&fs->lfs_dchainhd) == NULL) {
1655 mutex_exit(&lfs_lock);
1656 return 0;
1657 } else
1658 mutex_exit(&lfs_lock);
1659
1660 if (lfs_dostats)
1661 ++lfs_stats.flush_invoked;
1662
1663 lfs_imtime(fs);
1664 lfs_seglock(fs, flags);
1665 sp = fs->lfs_sp;
1666
1667 /*
1668 * lfs_writevnodes, optimized to get dirops out of the way.
1669 * Only write dirops, and don't flush files' pages, only
1670 * blocks from the directories.
1671 *
1672 * We don't need to vref these files because they are
1673 * dirops and so hold an extra reference until the
1674 * segunlock clears them of that status.
1675 *
1676 * We don't need to check for IN_ADIROP because we know that
1677 * no dirops are active.
1678 *
1679 */
1680 mutex_enter(&lfs_lock);
1681 for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
1682 nip = TAILQ_NEXT(ip, i_lfs_dchain);
1683 mutex_exit(&lfs_lock);
1684 vp = ITOV(ip);
1685 mutex_enter(vp->v_interlock);
1686
1687 KASSERT((ip->i_flag & IN_ADIROP) == 0);
1688 KASSERT(vp->v_uflag & VU_DIROP);
1689 KASSERT(vdead_check(vp, VDEAD_NOWAIT) == 0);
1690
1691 /*
1692 * All writes to directories come from dirops; all
1693 * writes to files' direct blocks go through the page
1694 * cache, which we're not touching. Reads to files
1695 * and/or directories will not be affected by writing
1696 * directory blocks inodes and file inodes. So we don't
1697 * really need to lock.
1698 */
1699 if (vdead_check(vp, VDEAD_NOWAIT) != 0) {
1700 mutex_exit(vp->v_interlock);
1701 mutex_enter(&lfs_lock);
1702 continue;
1703 }
1704 mutex_exit(vp->v_interlock);
1705 /* XXX see below
1706 * waslocked = VOP_ISLOCKED(vp);
1707 */
1708 if (vp->v_type != VREG &&
1709 ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp))) {
1710 error = lfs_writefile(fs, sp, vp);
1711 if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
1712 !(ip->i_flag & IN_ALLMOD)) {
1713 mutex_enter(&lfs_lock);
1714 LFS_SET_UINO(ip, IN_MODIFIED);
1715 mutex_exit(&lfs_lock);
1716 }
1717 if (error && (sp->seg_flags & SEGM_SINGLE)) {
1718 mutex_enter(&lfs_lock);
1719 error = EAGAIN;
1720 break;
1721 }
1722 }
1723 KDASSERT(ip->i_number != LFS_IFILE_INUM);
1724 error = lfs_writeinode(fs, sp, ip);
1725 mutex_enter(&lfs_lock);
1726 if (error && (sp->seg_flags & SEGM_SINGLE)) {
1727 error = EAGAIN;
1728 break;
1729 }
1730
1731 /*
1732 * We might need to update these inodes again,
1733 * for example, if they have data blocks to write.
1734 * Make sure that after this flush, they are still
1735 * marked IN_MODIFIED so that we don't forget to
1736 * write them.
1737 */
1738 /* XXX only for non-directories? --KS */
1739 LFS_SET_UINO(ip, IN_MODIFIED);
1740 }
1741 mutex_exit(&lfs_lock);
1742 /* We've written all the dirops there are */
1743 ((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
1744 lfs_finalize_fs_seguse(fs);
1745 (void) lfs_writeseg(fs, sp);
1746 lfs_segunlock(fs);
1747
1748 return error;
1749 }
1750
1751 /*
1752 * Flush all vnodes for which the pagedaemon has requested pageouts.
1753 * Skip over any files that are marked VU_DIROP (since lfs_flush_dirop()
1754 * has just run, this would be an error). If we have to skip a vnode
1755 * for any reason, just skip it; if we have to wait for the cleaner,
1756 * abort. The writer daemon will call us again later.
1757 */
1758 int
1759 lfs_flush_pchain(struct lfs *fs)
1760 {
1761 struct inode *ip, *nip;
1762 struct vnode *vp;
1763 extern int lfs_dostats;
1764 struct segment *sp;
1765 int error, error2;
1766
1767 ASSERT_NO_SEGLOCK(fs);
1768
1769 if (fs->lfs_ronly)
1770 return EROFS;
1771
1772 mutex_enter(&lfs_lock);
1773 if (TAILQ_FIRST(&fs->lfs_pchainhd) == NULL) {
1774 mutex_exit(&lfs_lock);
1775 return 0;
1776 } else
1777 mutex_exit(&lfs_lock);
1778
1779 /* Get dirops out of the way */
1780 if ((error = lfs_flush_dirops(fs)) != 0)
1781 return error;
1782
1783 if (lfs_dostats)
1784 ++lfs_stats.flush_invoked;
1785
1786 /*
1787 * Inline lfs_segwrite/lfs_writevnodes, but just for pageouts.
1788 */
1789 lfs_imtime(fs);
1790 lfs_seglock(fs, 0);
1791 sp = fs->lfs_sp;
1792
1793 /*
1794 * lfs_writevnodes, optimized to clear pageout requests.
1795 * Only write non-dirop files that are in the pageout queue.
1796 * We're very conservative about what we write; we want to be
1797 * fast and async.
1798 */
1799 mutex_enter(&lfs_lock);
1800 top:
1801 for (ip = TAILQ_FIRST(&fs->lfs_pchainhd); ip != NULL; ip = nip) {
1802 nip = TAILQ_NEXT(ip, i_lfs_pchain);
1803 vp = ITOV(ip);
1804
1805 if (!(ip->i_flags & IN_PAGING))
1806 goto top;
1807
1808 mutex_enter(vp->v_interlock);
1809 if (vdead_check(vp, VDEAD_NOWAIT) != 0 ||
1810 (vp->v_uflag & VU_DIROP) != 0) {
1811 mutex_exit(vp->v_interlock);
1812 continue;
1813 }
1814 if (vp->v_type != VREG) {
1815 mutex_exit(vp->v_interlock);
1816 continue;
1817 }
1818 if (lfs_vref(vp))
1819 continue;
1820 mutex_exit(&lfs_lock);
1821
1822 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_RETRY) != 0) {
1823 lfs_vunref(vp);
1824 mutex_enter(&lfs_lock);
1825 continue;
1826 }
1827
1828 error = lfs_writefile(fs, sp, vp);
1829 if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
1830 !(ip->i_flag & IN_ALLMOD)) {
1831 mutex_enter(&lfs_lock);
1832 LFS_SET_UINO(ip, IN_MODIFIED);
1833 mutex_exit(&lfs_lock);
1834 }
1835 KDASSERT(ip->i_number != LFS_IFILE_INUM);
1836 error2 = lfs_writeinode(fs, sp, ip);
1837
1838 VOP_UNLOCK(vp);
1839 lfs_vunref(vp);
1840
1841 if (error == EAGAIN || error2 == EAGAIN) {
1842 lfs_writeseg(fs, sp);
1843 mutex_enter(&lfs_lock);
1844 break;
1845 }
1846 mutex_enter(&lfs_lock);
1847 }
1848 mutex_exit(&lfs_lock);
1849 (void) lfs_writeseg(fs, sp);
1850 lfs_segunlock(fs);
1851
1852 return 0;
1853 }
1854
1855 /*
1856 * Provide a fcntl interface to sys_lfs_{segwait,bmapv,markv}.
1857 */
1858 int
1859 lfs_fcntl(void *v)
1860 {
1861 struct vop_fcntl_args /* {
1862 struct vnode *a_vp;
1863 u_int a_command;
1864 void * a_data;
1865 int a_fflag;
1866 kauth_cred_t a_cred;
1867 } */ *ap = v;
1868 struct timeval tv;
1869 struct timeval *tvp;
1870 BLOCK_INFO *blkiov;
1871 CLEANERINFO *cip;
1872 SEGUSE *sup;
1873 int blkcnt, error;
1874 size_t fh_size;
1875 struct lfs_fcntl_markv blkvp;
1876 struct lwp *l;
1877 fsid_t *fsidp;
1878 struct lfs *fs;
1879 struct buf *bp;
1880 fhandle_t *fhp;
1881 daddr_t off;
1882 int oclean;
1883
1884 /* Only respect LFS fcntls on fs root or Ifile */
1885 if (VTOI(ap->a_vp)->i_number != ULFS_ROOTINO &&
1886 VTOI(ap->a_vp)->i_number != LFS_IFILE_INUM) {
1887 return ulfs_fcntl(v);
1888 }
1889
1890 /* Avoid locking a draining lock */
1891 if (ap->a_vp->v_mount->mnt_iflag & IMNT_UNMOUNT) {
1892 return ESHUTDOWN;
1893 }
1894
1895 /* LFS control and monitoring fcntls are available only to root */
1896 l = curlwp;
1897 if (((ap->a_command & 0xff00) >> 8) == 'L' &&
1898 (error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
1899 KAUTH_REQ_SYSTEM_LFS_FCNTL, NULL, NULL, NULL)) != 0)
1900 return (error);
1901
1902 fs = VTOI(ap->a_vp)->i_lfs;
1903 fsidp = &ap->a_vp->v_mount->mnt_stat.f_fsidx;
1904
1905 error = 0;
1906 switch ((int)ap->a_command) {
1907 case LFCNSEGWAITALL_COMPAT_50:
1908 case LFCNSEGWAITALL_COMPAT:
1909 fsidp = NULL;
1910 /* FALLSTHROUGH */
1911 case LFCNSEGWAIT_COMPAT_50:
1912 case LFCNSEGWAIT_COMPAT:
1913 {
1914 struct timeval50 *tvp50
1915 = (struct timeval50 *)ap->a_data;
1916 timeval50_to_timeval(tvp50, &tv);
1917 tvp = &tv;
1918 }
1919 goto segwait_common;
1920 case LFCNSEGWAITALL:
1921 fsidp = NULL;
1922 /* FALLSTHROUGH */
1923 case LFCNSEGWAIT:
1924 tvp = (struct timeval *)ap->a_data;
1925 segwait_common:
1926 mutex_enter(&lfs_lock);
1927 ++fs->lfs_sleepers;
1928 mutex_exit(&lfs_lock);
1929
1930 error = lfs_segwait(fsidp, tvp);
1931
1932 mutex_enter(&lfs_lock);
1933 if (--fs->lfs_sleepers == 0)
1934 wakeup(&fs->lfs_sleepers);
1935 mutex_exit(&lfs_lock);
1936 return error;
1937
1938 case LFCNBMAPV:
1939 case LFCNMARKV:
1940 blkvp = *(struct lfs_fcntl_markv *)ap->a_data;
1941
1942 blkcnt = blkvp.blkcnt;
1943 if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
1944 return (EINVAL);
1945 blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
1946 if ((error = copyin(blkvp.blkiov, blkiov,
1947 blkcnt * sizeof(BLOCK_INFO))) != 0) {
1948 lfs_free(fs, blkiov, LFS_NB_BLKIOV);
1949 return error;
1950 }
1951
1952 mutex_enter(&lfs_lock);
1953 ++fs->lfs_sleepers;
1954 mutex_exit(&lfs_lock);
1955 if (ap->a_command == LFCNBMAPV)
1956 error = lfs_bmapv(l->l_proc, fsidp, blkiov, blkcnt);
1957 else /* LFCNMARKV */
1958 error = lfs_markv(l->l_proc, fsidp, blkiov, blkcnt);
1959 if (error == 0)
1960 error = copyout(blkiov, blkvp.blkiov,
1961 blkcnt * sizeof(BLOCK_INFO));
1962 mutex_enter(&lfs_lock);
1963 if (--fs->lfs_sleepers == 0)
1964 wakeup(&fs->lfs_sleepers);
1965 mutex_exit(&lfs_lock);
1966 lfs_free(fs, blkiov, LFS_NB_BLKIOV);
1967 return error;
1968
1969 case LFCNRECLAIM:
1970 /*
1971 * Flush dirops and write Ifile, allowing empty segments
1972 * to be immediately reclaimed.
1973 */
1974 lfs_writer_enter(fs, "pndirop");
1975 off = fs->lfs_offset;
1976 lfs_seglock(fs, SEGM_FORCE_CKP | SEGM_CKP);
1977 lfs_flush_dirops(fs);
1978 LFS_CLEANERINFO(cip, fs, bp);
1979 oclean = cip->clean;
1980 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1981 lfs_segwrite(ap->a_vp->v_mount, SEGM_FORCE_CKP);
1982 fs->lfs_sp->seg_flags |= SEGM_PROT;
1983 lfs_segunlock(fs);
1984 lfs_writer_leave(fs);
1985
1986 #ifdef DEBUG
1987 LFS_CLEANERINFO(cip, fs, bp);
1988 DLOG((DLOG_CLEAN, "lfs_fcntl: reclaim wrote %" PRId64
1989 " blocks, cleaned %" PRId32 " segments (activesb %d)\n",
1990 fs->lfs_offset - off, cip->clean - oclean,
1991 fs->lfs_activesb));
1992 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
1993 #else
1994 __USE(oclean);
1995 __USE(off);
1996 #endif
1997
1998 return 0;
1999
2000 case LFCNIFILEFH_COMPAT:
2001 /* Return the filehandle of the Ifile */
2002 if ((error = kauth_authorize_system(l->l_cred,
2003 KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)) != 0)
2004 return (error);
2005 fhp = (struct fhandle *)ap->a_data;
2006 fhp->fh_fsid = *fsidp;
2007 fh_size = 16; /* former VFS_MAXFIDSIZ */
2008 return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
2009
2010 case LFCNIFILEFH_COMPAT2:
2011 case LFCNIFILEFH:
2012 /* Return the filehandle of the Ifile */
2013 fhp = (struct fhandle *)ap->a_data;
2014 fhp->fh_fsid = *fsidp;
2015 fh_size = sizeof(struct lfs_fhandle) -
2016 offsetof(fhandle_t, fh_fid);
2017 return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
2018
2019 case LFCNREWIND:
2020 /* Move lfs_offset to the lowest-numbered segment */
2021 return lfs_rewind(fs, *(int *)ap->a_data);
2022
2023 case LFCNINVAL:
2024 /* Mark a segment SEGUSE_INVAL */
2025 LFS_SEGENTRY(sup, fs, *(int *)ap->a_data, bp);
2026 if (sup->su_nbytes > 0) {
2027 brelse(bp, 0);
2028 lfs_unset_inval_all(fs);
2029 return EBUSY;
2030 }
2031 sup->su_flags |= SEGUSE_INVAL;
2032 VOP_BWRITE(bp->b_vp, bp);
2033 return 0;
2034
2035 case LFCNRESIZE:
2036 /* Resize the filesystem */
2037 return lfs_resize_fs(fs, *(int *)ap->a_data);
2038
2039 case LFCNWRAPSTOP:
2040 case LFCNWRAPSTOP_COMPAT:
2041 /*
2042 * Hold lfs_newseg at segment 0; if requested, sleep until
2043 * the filesystem wraps around. To support external agents
2044 * (dump, fsck-based regression test) that need to look at
2045 * a snapshot of the filesystem, without necessarily
2046 * requiring that all fs activity stops.
2047 */
2048 if (fs->lfs_stoplwp == curlwp)
2049 return EALREADY;
2050
2051 mutex_enter(&lfs_lock);
2052 while (fs->lfs_stoplwp != NULL)
2053 cv_wait(&fs->lfs_stopcv, &lfs_lock);
2054 fs->lfs_stoplwp = curlwp;
2055 if (fs->lfs_nowrap == 0)
2056 log(LOG_NOTICE, "%s: disabled log wrap\n", fs->lfs_fsmnt);
2057 ++fs->lfs_nowrap;
2058 if (*(int *)ap->a_data == 1
2059 || ap->a_command == LFCNWRAPSTOP_COMPAT) {
2060 log(LOG_NOTICE, "LFCNSTOPWRAP waiting for log wrap\n");
2061 error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
2062 "segwrap", 0, &lfs_lock);
2063 log(LOG_NOTICE, "LFCNSTOPWRAP done waiting\n");
2064 if (error) {
2065 lfs_wrapgo(fs, VTOI(ap->a_vp), 0);
2066 }
2067 }
2068 mutex_exit(&lfs_lock);
2069 return 0;
2070
2071 case LFCNWRAPGO:
2072 case LFCNWRAPGO_COMPAT:
2073 /*
2074 * Having done its work, the agent wakes up the writer.
2075 * If the argument is 1, it sleeps until a new segment
2076 * is selected.
2077 */
2078 mutex_enter(&lfs_lock);
2079 error = lfs_wrapgo(fs, VTOI(ap->a_vp),
2080 ap->a_command == LFCNWRAPGO_COMPAT ? 1 :
2081 *((int *)ap->a_data));
2082 mutex_exit(&lfs_lock);
2083 return error;
2084
2085 case LFCNWRAPPASS:
2086 if ((VTOI(ap->a_vp)->i_lfs_iflags & LFSI_WRAPWAIT))
2087 return EALREADY;
2088 mutex_enter(&lfs_lock);
2089 if (fs->lfs_stoplwp != curlwp) {
2090 mutex_exit(&lfs_lock);
2091 return EALREADY;
2092 }
2093 if (fs->lfs_nowrap == 0) {
2094 mutex_exit(&lfs_lock);
2095 return EBUSY;
2096 }
2097 fs->lfs_wrappass = 1;
2098 wakeup(&fs->lfs_wrappass);
2099 /* Wait for the log to wrap, if asked */
2100 if (*(int *)ap->a_data) {
2101 mutex_enter(ap->a_vp->v_interlock);
2102 if (lfs_vref(ap->a_vp) != 0)
2103 panic("LFCNWRAPPASS: lfs_vref failed");
2104 VTOI(ap->a_vp)->i_lfs_iflags |= LFSI_WRAPWAIT;
2105 log(LOG_NOTICE, "LFCNPASS waiting for log wrap\n");
2106 error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
2107 "segwrap", 0, &lfs_lock);
2108 log(LOG_NOTICE, "LFCNPASS done waiting\n");
2109 VTOI(ap->a_vp)->i_lfs_iflags &= ~LFSI_WRAPWAIT;
2110 lfs_vunref(ap->a_vp);
2111 }
2112 mutex_exit(&lfs_lock);
2113 return error;
2114
2115 case LFCNWRAPSTATUS:
2116 mutex_enter(&lfs_lock);
2117 *(int *)ap->a_data = fs->lfs_wrapstatus;
2118 mutex_exit(&lfs_lock);
2119 return 0;
2120
2121 default:
2122 return ulfs_fcntl(v);
2123 }
2124 return 0;
2125 }
2126
2127 /*
2128 * Return the last logical file offset that should be written for this file
2129 * if we're doing a write that ends at "size". If writing, we need to know
2130 * about sizes on disk, i.e. fragments if there are any; if reading, we need
2131 * to know about entire blocks.
2132 */
2133 void
2134 lfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
2135 {
2136 struct inode *ip = VTOI(vp);
2137 struct lfs *fs = ip->i_lfs;
2138 daddr_t olbn, nlbn;
2139
2140 olbn = lfs_lblkno(fs, ip->i_size);
2141 nlbn = lfs_lblkno(fs, size);
2142 if (!(flags & GOP_SIZE_MEM) && nlbn < ULFS_NDADDR && olbn <= nlbn) {
2143 *eobp = lfs_fragroundup(fs, size);
2144 } else {
2145 *eobp = lfs_blkroundup(fs, size);
2146 }
2147 }
2148
2149 #ifdef DEBUG
2150 void lfs_dump_vop(void *);
2151
2152 void
2153 lfs_dump_vop(void *v)
2154 {
2155 struct vop_putpages_args /* {
2156 struct vnode *a_vp;
2157 voff_t a_offlo;
2158 voff_t a_offhi;
2159 int a_flags;
2160 } */ *ap = v;
2161
2162 #ifdef DDB
2163 vfs_vnode_print(ap->a_vp, 0, printf);
2164 #endif
2165 lfs_dump_dinode(VTOI(ap->a_vp)->i_din.ffs1_din);
2166 }
2167 #endif
2168
2169 int
2170 lfs_mmap(void *v)
2171 {
2172 struct vop_mmap_args /* {
2173 const struct vnodeop_desc *a_desc;
2174 struct vnode *a_vp;
2175 vm_prot_t a_prot;
2176 kauth_cred_t a_cred;
2177 } */ *ap = v;
2178
2179 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM)
2180 return EOPNOTSUPP;
2181 return ulfs_mmap(v);
2182 }
2183
2184 static int
2185 lfs_openextattr(void *v)
2186 {
2187 struct vop_openextattr_args /* {
2188 struct vnode *a_vp;
2189 kauth_cred_t a_cred;
2190 struct proc *a_p;
2191 } */ *ap = v;
2192 struct inode *ip = VTOI(ap->a_vp);
2193 struct ulfsmount *ump = ip->i_ump;
2194 //struct lfs *fs = ip->i_lfs;
2195
2196 /* Not supported for ULFS1 file systems. */
2197 if (ump->um_fstype == ULFS1)
2198 return (EOPNOTSUPP);
2199
2200 /* XXX Not implemented for ULFS2 file systems. */
2201 return (EOPNOTSUPP);
2202 }
2203
2204 static int
2205 lfs_closeextattr(void *v)
2206 {
2207 struct vop_closeextattr_args /* {
2208 struct vnode *a_vp;
2209 int a_commit;
2210 kauth_cred_t a_cred;
2211 struct proc *a_p;
2212 } */ *ap = v;
2213 struct inode *ip = VTOI(ap->a_vp);
2214 struct ulfsmount *ump = ip->i_ump;
2215 //struct lfs *fs = ip->i_lfs;
2216
2217 /* Not supported for ULFS1 file systems. */
2218 if (ump->um_fstype == ULFS1)
2219 return (EOPNOTSUPP);
2220
2221 /* XXX Not implemented for ULFS2 file systems. */
2222 return (EOPNOTSUPP);
2223 }
2224
2225 static int
2226 lfs_getextattr(void *v)
2227 {
2228 struct vop_getextattr_args /* {
2229 struct vnode *a_vp;
2230 int a_attrnamespace;
2231 const char *a_name;
2232 struct uio *a_uio;
2233 size_t *a_size;
2234 kauth_cred_t a_cred;
2235 struct proc *a_p;
2236 } */ *ap = v;
2237 struct vnode *vp = ap->a_vp;
2238 struct inode *ip = VTOI(vp);
2239 struct ulfsmount *ump = ip->i_ump;
2240 //struct lfs *fs = ip->i_lfs;
2241 int error;
2242
2243 if (ump->um_fstype == ULFS1) {
2244 #ifdef LFS_EXTATTR
2245 fstrans_start(vp->v_mount, FSTRANS_SHARED);
2246 error = ulfs_getextattr(ap);
2247 fstrans_done(vp->v_mount);
2248 #else
2249 error = EOPNOTSUPP;
2250 #endif
2251 return error;
2252 }
2253
2254 /* XXX Not implemented for ULFS2 file systems. */
2255 return (EOPNOTSUPP);
2256 }
2257
2258 static int
2259 lfs_setextattr(void *v)
2260 {
2261 struct vop_setextattr_args /* {
2262 struct vnode *a_vp;
2263 int a_attrnamespace;
2264 const char *a_name;
2265 struct uio *a_uio;
2266 kauth_cred_t a_cred;
2267 struct proc *a_p;
2268 } */ *ap = v;
2269 struct vnode *vp = ap->a_vp;
2270 struct inode *ip = VTOI(vp);
2271 struct ulfsmount *ump = ip->i_ump;
2272 //struct lfs *fs = ip->i_lfs;
2273 int error;
2274
2275 if (ump->um_fstype == ULFS1) {
2276 #ifdef LFS_EXTATTR
2277 fstrans_start(vp->v_mount, FSTRANS_SHARED);
2278 error = ulfs_setextattr(ap);
2279 fstrans_done(vp->v_mount);
2280 #else
2281 error = EOPNOTSUPP;
2282 #endif
2283 return error;
2284 }
2285
2286 /* XXX Not implemented for ULFS2 file systems. */
2287 return (EOPNOTSUPP);
2288 }
2289
2290 static int
2291 lfs_listextattr(void *v)
2292 {
2293 struct vop_listextattr_args /* {
2294 struct vnode *a_vp;
2295 int a_attrnamespace;
2296 struct uio *a_uio;
2297 size_t *a_size;
2298 kauth_cred_t a_cred;
2299 struct proc *a_p;
2300 } */ *ap = v;
2301 struct vnode *vp = ap->a_vp;
2302 struct inode *ip = VTOI(vp);
2303 struct ulfsmount *ump = ip->i_ump;
2304 //struct lfs *fs = ip->i_lfs;
2305 int error;
2306
2307 if (ump->um_fstype == ULFS1) {
2308 #ifdef LFS_EXTATTR
2309 fstrans_start(vp->v_mount, FSTRANS_SHARED);
2310 error = ulfs_listextattr(ap);
2311 fstrans_done(vp->v_mount);
2312 #else
2313 error = EOPNOTSUPP;
2314 #endif
2315 return error;
2316 }
2317
2318 /* XXX Not implemented for ULFS2 file systems. */
2319 return (EOPNOTSUPP);
2320 }
2321
2322 static int
2323 lfs_deleteextattr(void *v)
2324 {
2325 struct vop_deleteextattr_args /* {
2326 struct vnode *a_vp;
2327 int a_attrnamespace;
2328 kauth_cred_t a_cred;
2329 struct proc *a_p;
2330 } */ *ap = v;
2331 struct vnode *vp = ap->a_vp;
2332 struct inode *ip = VTOI(vp);
2333 struct ulfsmount *ump = ip->i_ump;
2334 //struct fs *fs = ip->i_lfs;
2335 int error;
2336
2337 if (ump->um_fstype == ULFS1) {
2338 #ifdef LFS_EXTATTR
2339 fstrans_start(vp->v_mount, FSTRANS_SHARED);
2340 error = ulfs_deleteextattr(ap);
2341 fstrans_done(vp->v_mount);
2342 #else
2343 error = EOPNOTSUPP;
2344 #endif
2345 return error;
2346 }
2347
2348 /* XXX Not implemented for ULFS2 file systems. */
2349 return (EOPNOTSUPP);
2350 }
2351