lfs_vnops.c revision 1.267 1 /* $NetBSD: lfs_vnops.c,v 1.267 2014/05/17 07:09:59 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.267 2014/05/17 07:09:59 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 ulfs_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 int error;
870 struct vnode *dvp = ap->a_dvp;
871 struct ulfs_lookup_results *ulr;
872
873 /* XXX should handle this material another way */
874 ulr = &VTOI(dvp)->i_crap;
875 ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
876
877 fstrans_start(dvp->v_mount, FSTRANS_SHARED);
878 error =
879 ulfs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
880 dvp, ulr, ap->a_vpp, ap->a_cnp);
881 if (error) {
882 fstrans_done(dvp->v_mount);
883 return (error);
884 }
885 fstrans_done(dvp->v_mount);
886 VN_KNOTE(dvp, NOTE_WRITE);
887 VOP_UNLOCK(*ap->a_vpp);
888 return (0);
889 }
890
891 int
892 lfs_create(void *v)
893 {
894 struct vop_create_v3_args /* {
895 struct vnode *a_dvp;
896 struct vnode **a_vpp;
897 struct componentname *a_cnp;
898 struct vattr *a_vap;
899 } */ *ap = v;
900 struct lfs *fs;
901 struct vnode *dvp, **vpp;
902 int error;
903
904 dvp = ap->a_dvp;
905 vpp = ap->a_vpp;
906
907 KASSERT(vpp != NULL);
908 KASSERT(*vpp == NULL);
909
910 fs = VFSTOULFS(dvp->v_mount)->um_lfs;
911 ASSERT_NO_SEGLOCK(fs);
912 if (fs->lfs_ronly) {
913 return EROFS;
914 }
915
916 /*
917 * Get a new vnode *before* adjusting the dirop count, to
918 * avoid a deadlock in getnewvnode(), if we have a stacked
919 * filesystem mounted on top of us.
920 *
921 * NB: this means we have to destroy the new vnode on error.
922 */
923
924 error = getnewvnode(VT_LFS, dvp->v_mount, lfs_vnodeop_p, NULL, vpp);
925 if (error) {
926 DLOG((DLOG_ALLOC, "lfs_create: dvp %p error %d\n", dvp,error));
927 return error;
928 }
929 error = lfs_set_dirop(dvp, NULL);
930 if (error) {
931 ungetnewvnode(*vpp);
932 *vpp = NULL;
933 return error;
934 }
935
936 error = ulfs_create(ap);
937
938 UNMARK_VNODE(dvp);
939 UNMARK_VNODE(*vpp);
940 if (!((*vpp)->v_uflag & VU_DIROP)) {
941 KASSERT(error != 0);
942 ungetnewvnode(*vpp);
943 *vpp = NULL;
944 }
945 else {
946 KASSERT(error == 0);
947 }
948 lfs_unset_dirop(fs, dvp, "create");
949
950 vrele(dvp);
951 return (error);
952 }
953
954 int
955 lfs_mkdir(void *v)
956 {
957 struct vop_mkdir_v3_args /* {
958 struct vnode *a_dvp;
959 struct vnode **a_vpp;
960 struct componentname *a_cnp;
961 struct vattr *a_vap;
962 } */ *ap = v;
963 struct lfs *fs;
964 struct vnode *dvp, *tvp, **vpp;
965 struct inode *dp, *ip;
966 struct componentname *cnp;
967 struct vattr *vap;
968 struct ulfs_lookup_results *ulr;
969 struct buf *bp;
970 struct lfs_dirtemplate dirtemplate;
971 struct lfs_direct *newdir;
972 int dirblksiz;
973 int dmode;
974 int error;
975
976 dvp = ap->a_dvp;
977 tvp = NULL;
978 vpp = ap->a_vpp;
979 cnp = ap->a_cnp;
980 vap = ap->a_vap;
981
982 dp = VTOI(dvp);
983 ip = NULL;
984
985 KASSERT(vpp != NULL);
986 KASSERT(*vpp == NULL);
987
988 /* XXX should handle this material another way */
989 ulr = &dp->i_crap;
990 ULFS_CHECK_CRAPCOUNTER(dp);
991
992 fs = VFSTOULFS(dvp->v_mount)->um_lfs;
993 ASSERT_NO_SEGLOCK(fs);
994 if (fs->lfs_ronly) {
995 return EROFS;
996 }
997 dirblksiz = fs->um_dirblksiz;
998
999 /*
1000 * Get a new vnode *before* adjusting the dirop count, to
1001 * avoid a deadlock in getnewvnode(), if we have a stacked
1002 * filesystem mounted on top of us.
1003 *
1004 * NB: this means we have to destroy the new vnode on error.
1005 */
1006
1007 error = getnewvnode(VT_LFS, dvp->v_mount, lfs_vnodeop_p, NULL, vpp);
1008 if (error) {
1009 DLOG((DLOG_ALLOC, "lfs_mkdir: dvp %p error %d\n", dvp, error));
1010 return error;
1011 }
1012 error = lfs_set_dirop(dvp, NULL);
1013 if (error) {
1014 ungetnewvnode(*vpp);
1015 *vpp = NULL;
1016 return error;
1017 }
1018
1019 fstrans_start(dvp->v_mount, FSTRANS_SHARED);
1020
1021 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1022 error = EMLINK;
1023 goto out;
1024 }
1025
1026 dmode = vap->va_mode & ACCESSPERMS;
1027 dmode |= LFS_IFDIR;
1028 /*
1029 * Must simulate part of ulfs_makeinode here to acquire the inode,
1030 * but not have it entered in the parent directory. The entry is
1031 * made later after writing "." and ".." entries.
1032 */
1033 if ((error = lfs_valloc(dvp, dmode, cnp->cn_cred, vpp)) != 0)
1034 goto out;
1035
1036 tvp = *vpp;
1037 ip = VTOI(tvp);
1038
1039 ip->i_uid = kauth_cred_geteuid(cnp->cn_cred);
1040 DIP_ASSIGN(ip, uid, ip->i_uid);
1041 ip->i_gid = dp->i_gid;
1042 DIP_ASSIGN(ip, gid, ip->i_gid);
1043 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
1044 if ((error = lfs_chkiq(ip, 1, cnp->cn_cred, 0))) {
1045 lfs_vfree(tvp, ip->i_number, dmode);
1046 fstrans_done(dvp->v_mount);
1047 vput(tvp);
1048 goto out2;
1049 }
1050 #endif
1051 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1052 ip->i_mode = dmode;
1053 DIP_ASSIGN(ip, mode, dmode);
1054 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1055 ip->i_nlink = 2;
1056 DIP_ASSIGN(ip, nlink, 2);
1057 if (cnp->cn_flags & ISWHITEOUT) {
1058 ip->i_flags |= UF_OPAQUE;
1059 DIP_ASSIGN(ip, flags, ip->i_flags);
1060 }
1061
1062 /*
1063 * Bump link count in parent directory to reflect work done below.
1064 */
1065 dp->i_nlink++;
1066 DIP_ASSIGN(dp, nlink, dp->i_nlink);
1067 dp->i_flag |= IN_CHANGE;
1068 if ((error = lfs_update(dvp, NULL, NULL, UPDATE_DIROP)) != 0)
1069 goto bad;
1070
1071 /*
1072 * Initialize directory with "." and ".." from static template.
1073 */
1074 dirtemplate = mastertemplate;
1075 dirtemplate.dotdot_reclen = dirblksiz - dirtemplate.dot_reclen;
1076 dirtemplate.dot_ino = ulfs_rw32(ip->i_number, ULFS_MPNEEDSWAP(fs));
1077 dirtemplate.dotdot_ino = ulfs_rw32(dp->i_number, ULFS_MPNEEDSWAP(fs));
1078 dirtemplate.dot_reclen = ulfs_rw16(dirtemplate.dot_reclen,
1079 ULFS_MPNEEDSWAP(fs));
1080 dirtemplate.dotdot_reclen = ulfs_rw16(dirtemplate.dotdot_reclen,
1081 ULFS_MPNEEDSWAP(fs));
1082 if (fs->um_maxsymlinklen <= 0) {
1083 #if BYTE_ORDER == LITTLE_ENDIAN
1084 if (ULFS_MPNEEDSWAP(fs) == 0)
1085 #else
1086 if (ULFS_MPNEEDSWAP(fs) != 0)
1087 #endif
1088 {
1089 dirtemplate.dot_type = dirtemplate.dot_namlen;
1090 dirtemplate.dotdot_type = dirtemplate.dotdot_namlen;
1091 dirtemplate.dot_namlen = dirtemplate.dotdot_namlen = 0;
1092 } else
1093 dirtemplate.dot_type = dirtemplate.dotdot_type = 0;
1094 }
1095 if ((error = lfs_balloc(tvp, (off_t)0, dirblksiz, cnp->cn_cred,
1096 B_CLRBUF, &bp)) != 0)
1097 goto bad;
1098 ip->i_size = dirblksiz;
1099 DIP_ASSIGN(ip, size, dirblksiz);
1100 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1101 uvm_vnp_setsize(tvp, ip->i_size);
1102 memcpy((void *)bp->b_data, (void *)&dirtemplate, sizeof dirtemplate);
1103
1104 /*
1105 * Directory set up; now install its entry in the parent directory.
1106 */
1107 if ((error = VOP_BWRITE(bp->b_vp, bp)) != 0)
1108 goto bad;
1109 if ((error = lfs_update(tvp, NULL, NULL, UPDATE_DIROP)) != 0) {
1110 goto bad;
1111 }
1112 newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
1113 ulfs_makedirentry(ip, cnp, newdir);
1114 error = ulfs_direnter(dvp, ulr, tvp, newdir, cnp, bp);
1115 pool_cache_put(ulfs_direct_cache, newdir);
1116 bad:
1117 if (error == 0) {
1118 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1119 VOP_UNLOCK(tvp);
1120 } else {
1121 dp->i_nlink--;
1122 DIP_ASSIGN(dp, nlink, dp->i_nlink);
1123 dp->i_flag |= IN_CHANGE;
1124 /*
1125 * No need to do an explicit lfs_truncate here, vrele will
1126 * do this for us because we set the link count to 0.
1127 */
1128 ip->i_nlink = 0;
1129 DIP_ASSIGN(ip, nlink, 0);
1130 ip->i_flag |= IN_CHANGE;
1131 /* If IN_ADIROP, account for it */
1132 lfs_unmark_vnode(tvp);
1133 vput(tvp);
1134 }
1135
1136 out:
1137 fstrans_done(dvp->v_mount);
1138 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
1139 out2:
1140 #endif
1141
1142 UNMARK_VNODE(dvp);
1143 UNMARK_VNODE(*vpp);
1144 if (!((*vpp)->v_uflag & VU_DIROP)) {
1145 KASSERT(error != 0);
1146 ungetnewvnode(*vpp);
1147 *vpp = NULL;
1148 }
1149 else {
1150 KASSERT(error == 0);
1151 }
1152 lfs_unset_dirop(fs, dvp, "mkdir");
1153
1154 vrele(dvp);
1155 return (error);
1156 }
1157
1158 int
1159 lfs_remove(void *v)
1160 {
1161 struct vop_remove_args /* {
1162 struct vnode *a_dvp;
1163 struct vnode *a_vp;
1164 struct componentname *a_cnp;
1165 } */ *ap = v;
1166 struct vnode *dvp, *vp;
1167 struct inode *ip;
1168 int error;
1169
1170 dvp = ap->a_dvp;
1171 vp = ap->a_vp;
1172 ip = VTOI(vp);
1173 if ((error = lfs_set_dirop(dvp, vp)) != 0) {
1174 if (dvp == vp)
1175 vrele(vp);
1176 else
1177 vput(vp);
1178 vput(dvp);
1179 return error;
1180 }
1181 error = ulfs_remove(ap);
1182 if (ip->i_nlink == 0)
1183 lfs_orphan(ip->i_lfs, ip->i_number);
1184
1185 UNMARK_VNODE(dvp);
1186 if (ap->a_vp) {
1187 UNMARK_VNODE(ap->a_vp);
1188 }
1189 lfs_unset_dirop(ip->i_lfs, dvp, "remove");
1190 vrele(dvp);
1191 if (ap->a_vp) {
1192 vrele(ap->a_vp);
1193 }
1194
1195 return (error);
1196 }
1197
1198 int
1199 lfs_rmdir(void *v)
1200 {
1201 struct vop_rmdir_args /* {
1202 struct vnodeop_desc *a_desc;
1203 struct vnode *a_dvp;
1204 struct vnode *a_vp;
1205 struct componentname *a_cnp;
1206 } */ *ap = v;
1207 struct vnode *vp;
1208 struct inode *ip;
1209 int error;
1210
1211 vp = ap->a_vp;
1212 ip = VTOI(vp);
1213 if ((error = lfs_set_dirop(ap->a_dvp, ap->a_vp)) != 0) {
1214 if (ap->a_dvp == vp)
1215 vrele(ap->a_dvp);
1216 else
1217 vput(ap->a_dvp);
1218 vput(vp);
1219 return error;
1220 }
1221 error = ulfs_rmdir(ap);
1222 if (ip->i_nlink == 0)
1223 lfs_orphan(ip->i_lfs, ip->i_number);
1224
1225 UNMARK_VNODE(ap->a_dvp);
1226 if (ap->a_vp) {
1227 UNMARK_VNODE(ap->a_vp);
1228 }
1229 lfs_unset_dirop(ip->i_lfs, ap->a_dvp, "rmdir");
1230 vrele(ap->a_dvp);
1231 if (ap->a_vp) {
1232 vrele(ap->a_vp);
1233 }
1234
1235 return (error);
1236 }
1237
1238 int
1239 lfs_link(void *v)
1240 {
1241 struct vop_link_args /* {
1242 struct vnode *a_dvp;
1243 struct vnode *a_vp;
1244 struct componentname *a_cnp;
1245 } */ *ap = v;
1246 struct lfs *fs;
1247 struct vnode *dvp;
1248 int error;
1249
1250 dvp = ap->a_dvp;
1251
1252 fs = VFSTOULFS(dvp->v_mount)->um_lfs;
1253 ASSERT_NO_SEGLOCK(fs);
1254 if (fs->lfs_ronly) {
1255 return EROFS;
1256 }
1257
1258 error = lfs_set_dirop(dvp, NULL);
1259 if (error) {
1260 /*
1261 * XXX dholland 20140515 this was here before but must
1262 * be wrong.
1263 */
1264 vput(dvp);
1265
1266 return error;
1267 }
1268
1269 error = ulfs_link(ap);
1270
1271 UNMARK_VNODE(dvp);
1272 lfs_unset_dirop(fs, dvp, "link");
1273 vrele(dvp);
1274
1275 return (error);
1276 }
1277
1278 /* XXX hack to avoid calling ITIMES in getattr */
1279 int
1280 lfs_getattr(void *v)
1281 {
1282 struct vop_getattr_args /* {
1283 struct vnode *a_vp;
1284 struct vattr *a_vap;
1285 kauth_cred_t a_cred;
1286 } */ *ap = v;
1287 struct vnode *vp = ap->a_vp;
1288 struct inode *ip = VTOI(vp);
1289 struct vattr *vap = ap->a_vap;
1290 struct lfs *fs = ip->i_lfs;
1291
1292 fstrans_start(vp->v_mount, FSTRANS_SHARED);
1293 /*
1294 * Copy from inode table
1295 */
1296 vap->va_fsid = ip->i_dev;
1297 vap->va_fileid = ip->i_number;
1298 vap->va_mode = ip->i_mode & ~LFS_IFMT;
1299 vap->va_nlink = ip->i_nlink;
1300 vap->va_uid = ip->i_uid;
1301 vap->va_gid = ip->i_gid;
1302 vap->va_rdev = (dev_t)ip->i_ffs1_rdev;
1303 vap->va_size = vp->v_size;
1304 vap->va_atime.tv_sec = ip->i_ffs1_atime;
1305 vap->va_atime.tv_nsec = ip->i_ffs1_atimensec;
1306 vap->va_mtime.tv_sec = ip->i_ffs1_mtime;
1307 vap->va_mtime.tv_nsec = ip->i_ffs1_mtimensec;
1308 vap->va_ctime.tv_sec = ip->i_ffs1_ctime;
1309 vap->va_ctime.tv_nsec = ip->i_ffs1_ctimensec;
1310 vap->va_flags = ip->i_flags;
1311 vap->va_gen = ip->i_gen;
1312 /* this doesn't belong here */
1313 if (vp->v_type == VBLK)
1314 vap->va_blocksize = BLKDEV_IOSIZE;
1315 else if (vp->v_type == VCHR)
1316 vap->va_blocksize = MAXBSIZE;
1317 else
1318 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
1319 vap->va_bytes = lfs_fsbtob(fs, (u_quad_t)ip->i_lfs_effnblks);
1320 vap->va_type = vp->v_type;
1321 vap->va_filerev = ip->i_modrev;
1322 fstrans_done(vp->v_mount);
1323 return (0);
1324 }
1325
1326 /*
1327 * Check to make sure the inode blocks won't choke the buffer
1328 * cache, then call ulfs_setattr as usual.
1329 */
1330 int
1331 lfs_setattr(void *v)
1332 {
1333 struct vop_setattr_args /* {
1334 struct vnode *a_vp;
1335 struct vattr *a_vap;
1336 kauth_cred_t a_cred;
1337 } */ *ap = v;
1338 struct vnode *vp = ap->a_vp;
1339
1340 lfs_check(vp, LFS_UNUSED_LBN, 0);
1341 return ulfs_setattr(v);
1342 }
1343
1344 /*
1345 * Release the block we hold on lfs_newseg wrapping. Called on file close,
1346 * or explicitly from LFCNWRAPGO. Called with the interlock held.
1347 */
1348 static int
1349 lfs_wrapgo(struct lfs *fs, struct inode *ip, int waitfor)
1350 {
1351 if (fs->lfs_stoplwp != curlwp)
1352 return EBUSY;
1353
1354 fs->lfs_stoplwp = NULL;
1355 cv_signal(&fs->lfs_stopcv);
1356
1357 KASSERT(fs->lfs_nowrap > 0);
1358 if (fs->lfs_nowrap <= 0) {
1359 return 0;
1360 }
1361
1362 if (--fs->lfs_nowrap == 0) {
1363 log(LOG_NOTICE, "%s: re-enabled log wrap\n", fs->lfs_fsmnt);
1364 wakeup(&fs->lfs_wrappass);
1365 lfs_wakeup_cleaner(fs);
1366 }
1367 if (waitfor) {
1368 mtsleep(&fs->lfs_nextseg, PCATCH | PUSER, "segment",
1369 0, &lfs_lock);
1370 }
1371
1372 return 0;
1373 }
1374
1375 /*
1376 * Close called.
1377 *
1378 * Update the times on the inode.
1379 */
1380 /* ARGSUSED */
1381 int
1382 lfs_close(void *v)
1383 {
1384 struct vop_close_args /* {
1385 struct vnode *a_vp;
1386 int a_fflag;
1387 kauth_cred_t a_cred;
1388 } */ *ap = v;
1389 struct vnode *vp = ap->a_vp;
1390 struct inode *ip = VTOI(vp);
1391 struct lfs *fs = ip->i_lfs;
1392
1393 if ((ip->i_number == ULFS_ROOTINO || ip->i_number == LFS_IFILE_INUM) &&
1394 fs->lfs_stoplwp == curlwp) {
1395 mutex_enter(&lfs_lock);
1396 log(LOG_NOTICE, "lfs_close: releasing log wrap control\n");
1397 lfs_wrapgo(fs, ip, 0);
1398 mutex_exit(&lfs_lock);
1399 }
1400
1401 if (vp == ip->i_lfs->lfs_ivnode &&
1402 vp->v_mount->mnt_iflag & IMNT_UNMOUNT)
1403 return 0;
1404
1405 fstrans_start(vp->v_mount, FSTRANS_SHARED);
1406 if (vp->v_usecount > 1 && vp != ip->i_lfs->lfs_ivnode) {
1407 LFS_ITIMES(ip, NULL, NULL, NULL);
1408 }
1409 fstrans_done(vp->v_mount);
1410 return (0);
1411 }
1412
1413 /*
1414 * Close wrapper for special devices.
1415 *
1416 * Update the times on the inode then do device close.
1417 */
1418 int
1419 lfsspec_close(void *v)
1420 {
1421 struct vop_close_args /* {
1422 struct vnode *a_vp;
1423 int a_fflag;
1424 kauth_cred_t a_cred;
1425 } */ *ap = v;
1426 struct vnode *vp;
1427 struct inode *ip;
1428
1429 vp = ap->a_vp;
1430 ip = VTOI(vp);
1431 if (vp->v_usecount > 1) {
1432 LFS_ITIMES(ip, NULL, NULL, NULL);
1433 }
1434 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
1435 }
1436
1437 /*
1438 * Close wrapper for fifo's.
1439 *
1440 * Update the times on the inode then do device close.
1441 */
1442 int
1443 lfsfifo_close(void *v)
1444 {
1445 struct vop_close_args /* {
1446 struct vnode *a_vp;
1447 int a_fflag;
1448 kauth_cred_ a_cred;
1449 } */ *ap = v;
1450 struct vnode *vp;
1451 struct inode *ip;
1452
1453 vp = ap->a_vp;
1454 ip = VTOI(vp);
1455 if (ap->a_vp->v_usecount > 1) {
1456 LFS_ITIMES(ip, NULL, NULL, NULL);
1457 }
1458 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
1459 }
1460
1461 /*
1462 * Reclaim an inode so that it can be used for other purposes.
1463 */
1464
1465 int
1466 lfs_reclaim(void *v)
1467 {
1468 struct vop_reclaim_args /* {
1469 struct vnode *a_vp;
1470 } */ *ap = v;
1471 struct vnode *vp = ap->a_vp;
1472 struct inode *ip = VTOI(vp);
1473 struct lfs *fs = ip->i_lfs;
1474 int error;
1475
1476 /*
1477 * The inode must be freed and updated before being removed
1478 * from its hash chain. Other threads trying to gain a hold
1479 * or lock on the inode will be stalled.
1480 */
1481 if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1482 lfs_vfree(vp, ip->i_number, ip->i_omode);
1483
1484 mutex_enter(&lfs_lock);
1485 LFS_CLR_UINO(ip, IN_ALLMOD);
1486 mutex_exit(&lfs_lock);
1487 if ((error = ulfs_reclaim(vp)))
1488 return (error);
1489
1490 /*
1491 * Take us off the paging and/or dirop queues if we were on them.
1492 * We shouldn't be on them.
1493 */
1494 mutex_enter(&lfs_lock);
1495 if (ip->i_flags & IN_PAGING) {
1496 log(LOG_WARNING, "%s: reclaimed vnode is IN_PAGING\n",
1497 fs->lfs_fsmnt);
1498 ip->i_flags &= ~IN_PAGING;
1499 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
1500 }
1501 if (vp->v_uflag & VU_DIROP) {
1502 panic("reclaimed vnode is VU_DIROP");
1503 vp->v_uflag &= ~VU_DIROP;
1504 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
1505 }
1506 mutex_exit(&lfs_lock);
1507
1508 pool_put(&lfs_dinode_pool, ip->i_din.ffs1_din);
1509 lfs_deregister_all(vp);
1510 pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
1511 ip->inode_ext.lfs = NULL;
1512 genfs_node_destroy(vp);
1513 pool_put(&lfs_inode_pool, vp->v_data);
1514 vp->v_data = NULL;
1515 return (0);
1516 }
1517
1518 /*
1519 * Read a block from a storage device.
1520 *
1521 * Calculate the logical to physical mapping if not done already,
1522 * then call the device strategy routine.
1523 *
1524 * In order to avoid reading blocks that are in the process of being
1525 * written by the cleaner---and hence are not mutexed by the normal
1526 * buffer cache / page cache mechanisms---check for collisions before
1527 * reading.
1528 *
1529 * We inline ulfs_strategy to make sure that the VOP_BMAP occurs *before*
1530 * the active cleaner test.
1531 *
1532 * XXX This code assumes that lfs_markv makes synchronous checkpoints.
1533 */
1534 int
1535 lfs_strategy(void *v)
1536 {
1537 struct vop_strategy_args /* {
1538 struct vnode *a_vp;
1539 struct buf *a_bp;
1540 } */ *ap = v;
1541 struct buf *bp;
1542 struct lfs *fs;
1543 struct vnode *vp;
1544 struct inode *ip;
1545 daddr_t tbn;
1546 #define MAXLOOP 25
1547 int i, sn, error, slept, loopcount;
1548
1549 bp = ap->a_bp;
1550 vp = ap->a_vp;
1551 ip = VTOI(vp);
1552 fs = ip->i_lfs;
1553
1554 /* lfs uses its strategy routine only for read */
1555 KASSERT(bp->b_flags & B_READ);
1556
1557 if (vp->v_type == VBLK || vp->v_type == VCHR)
1558 panic("lfs_strategy: spec");
1559 KASSERT(bp->b_bcount != 0);
1560 if (bp->b_blkno == bp->b_lblkno) {
1561 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1562 NULL);
1563 if (error) {
1564 bp->b_error = error;
1565 bp->b_resid = bp->b_bcount;
1566 biodone(bp);
1567 return (error);
1568 }
1569 if ((long)bp->b_blkno == -1) /* no valid data */
1570 clrbuf(bp);
1571 }
1572 if ((long)bp->b_blkno < 0) { /* block is not on disk */
1573 bp->b_resid = bp->b_bcount;
1574 biodone(bp);
1575 return (0);
1576 }
1577
1578 slept = 1;
1579 loopcount = 0;
1580 mutex_enter(&lfs_lock);
1581 while (slept && fs->lfs_seglock) {
1582 mutex_exit(&lfs_lock);
1583 /*
1584 * Look through list of intervals.
1585 * There will only be intervals to look through
1586 * if the cleaner holds the seglock.
1587 * Since the cleaner is synchronous, we can trust
1588 * the list of intervals to be current.
1589 */
1590 tbn = LFS_DBTOFSB(fs, bp->b_blkno);
1591 sn = lfs_dtosn(fs, tbn);
1592 slept = 0;
1593 for (i = 0; i < fs->lfs_cleanind; i++) {
1594 if (sn == lfs_dtosn(fs, fs->lfs_cleanint[i]) &&
1595 tbn >= fs->lfs_cleanint[i]) {
1596 DLOG((DLOG_CLEAN,
1597 "lfs_strategy: ino %d lbn %" PRId64
1598 " ind %d sn %d fsb %" PRIx32
1599 " given sn %d fsb %" PRIx64 "\n",
1600 ip->i_number, bp->b_lblkno, i,
1601 lfs_dtosn(fs, fs->lfs_cleanint[i]),
1602 fs->lfs_cleanint[i], sn, tbn));
1603 DLOG((DLOG_CLEAN,
1604 "lfs_strategy: sleeping on ino %d lbn %"
1605 PRId64 "\n", ip->i_number, bp->b_lblkno));
1606 mutex_enter(&lfs_lock);
1607 if (LFS_SEGLOCK_HELD(fs) && fs->lfs_iocount) {
1608 /*
1609 * Cleaner can't wait for itself.
1610 * Instead, wait for the blocks
1611 * to be written to disk.
1612 * XXX we need pribio in the test
1613 * XXX here.
1614 */
1615 mtsleep(&fs->lfs_iocount,
1616 (PRIBIO + 1) | PNORELOCK,
1617 "clean2", hz/10 + 1,
1618 &lfs_lock);
1619 slept = 1;
1620 ++loopcount;
1621 break;
1622 } else if (fs->lfs_seglock) {
1623 mtsleep(&fs->lfs_seglock,
1624 (PRIBIO + 1) | PNORELOCK,
1625 "clean1", 0,
1626 &lfs_lock);
1627 slept = 1;
1628 break;
1629 }
1630 mutex_exit(&lfs_lock);
1631 }
1632 }
1633 mutex_enter(&lfs_lock);
1634 if (loopcount > MAXLOOP) {
1635 printf("lfs_strategy: breaking out of clean2 loop\n");
1636 break;
1637 }
1638 }
1639 mutex_exit(&lfs_lock);
1640
1641 vp = ip->i_devvp;
1642 return VOP_STRATEGY(vp, bp);
1643 }
1644
1645 /*
1646 * Inline lfs_segwrite/lfs_writevnodes, but just for dirops.
1647 * Technically this is a checkpoint (the on-disk state is valid)
1648 * even though we are leaving out all the file data.
1649 */
1650 int
1651 lfs_flush_dirops(struct lfs *fs)
1652 {
1653 struct inode *ip, *nip;
1654 struct vnode *vp;
1655 extern int lfs_dostats;
1656 struct segment *sp;
1657 int flags = 0;
1658 int error = 0;
1659
1660 ASSERT_MAYBE_SEGLOCK(fs);
1661 KASSERT(fs->lfs_nadirop == 0);
1662
1663 if (fs->lfs_ronly)
1664 return EROFS;
1665
1666 mutex_enter(&lfs_lock);
1667 if (TAILQ_FIRST(&fs->lfs_dchainhd) == NULL) {
1668 mutex_exit(&lfs_lock);
1669 return 0;
1670 } else
1671 mutex_exit(&lfs_lock);
1672
1673 if (lfs_dostats)
1674 ++lfs_stats.flush_invoked;
1675
1676 lfs_imtime(fs);
1677 lfs_seglock(fs, flags);
1678 sp = fs->lfs_sp;
1679
1680 /*
1681 * lfs_writevnodes, optimized to get dirops out of the way.
1682 * Only write dirops, and don't flush files' pages, only
1683 * blocks from the directories.
1684 *
1685 * We don't need to vref these files because they are
1686 * dirops and so hold an extra reference until the
1687 * segunlock clears them of that status.
1688 *
1689 * We don't need to check for IN_ADIROP because we know that
1690 * no dirops are active.
1691 *
1692 */
1693 mutex_enter(&lfs_lock);
1694 for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
1695 nip = TAILQ_NEXT(ip, i_lfs_dchain);
1696 mutex_exit(&lfs_lock);
1697 vp = ITOV(ip);
1698 mutex_enter(vp->v_interlock);
1699
1700 KASSERT((ip->i_flag & IN_ADIROP) == 0);
1701 KASSERT(vp->v_uflag & VU_DIROP);
1702 KASSERT(vdead_check(vp, VDEAD_NOWAIT) == 0);
1703
1704 /*
1705 * All writes to directories come from dirops; all
1706 * writes to files' direct blocks go through the page
1707 * cache, which we're not touching. Reads to files
1708 * and/or directories will not be affected by writing
1709 * directory blocks inodes and file inodes. So we don't
1710 * really need to lock.
1711 */
1712 if (vdead_check(vp, VDEAD_NOWAIT) != 0) {
1713 mutex_exit(vp->v_interlock);
1714 mutex_enter(&lfs_lock);
1715 continue;
1716 }
1717 mutex_exit(vp->v_interlock);
1718 /* XXX see below
1719 * waslocked = VOP_ISLOCKED(vp);
1720 */
1721 if (vp->v_type != VREG &&
1722 ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp))) {
1723 error = lfs_writefile(fs, sp, vp);
1724 if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
1725 !(ip->i_flag & IN_ALLMOD)) {
1726 mutex_enter(&lfs_lock);
1727 LFS_SET_UINO(ip, IN_MODIFIED);
1728 mutex_exit(&lfs_lock);
1729 }
1730 if (error && (sp->seg_flags & SEGM_SINGLE)) {
1731 mutex_enter(&lfs_lock);
1732 error = EAGAIN;
1733 break;
1734 }
1735 }
1736 KDASSERT(ip->i_number != LFS_IFILE_INUM);
1737 error = lfs_writeinode(fs, sp, ip);
1738 mutex_enter(&lfs_lock);
1739 if (error && (sp->seg_flags & SEGM_SINGLE)) {
1740 error = EAGAIN;
1741 break;
1742 }
1743
1744 /*
1745 * We might need to update these inodes again,
1746 * for example, if they have data blocks to write.
1747 * Make sure that after this flush, they are still
1748 * marked IN_MODIFIED so that we don't forget to
1749 * write them.
1750 */
1751 /* XXX only for non-directories? --KS */
1752 LFS_SET_UINO(ip, IN_MODIFIED);
1753 }
1754 mutex_exit(&lfs_lock);
1755 /* We've written all the dirops there are */
1756 ((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
1757 lfs_finalize_fs_seguse(fs);
1758 (void) lfs_writeseg(fs, sp);
1759 lfs_segunlock(fs);
1760
1761 return error;
1762 }
1763
1764 /*
1765 * Flush all vnodes for which the pagedaemon has requested pageouts.
1766 * Skip over any files that are marked VU_DIROP (since lfs_flush_dirop()
1767 * has just run, this would be an error). If we have to skip a vnode
1768 * for any reason, just skip it; if we have to wait for the cleaner,
1769 * abort. The writer daemon will call us again later.
1770 */
1771 int
1772 lfs_flush_pchain(struct lfs *fs)
1773 {
1774 struct inode *ip, *nip;
1775 struct vnode *vp;
1776 extern int lfs_dostats;
1777 struct segment *sp;
1778 int error, error2;
1779
1780 ASSERT_NO_SEGLOCK(fs);
1781
1782 if (fs->lfs_ronly)
1783 return EROFS;
1784
1785 mutex_enter(&lfs_lock);
1786 if (TAILQ_FIRST(&fs->lfs_pchainhd) == NULL) {
1787 mutex_exit(&lfs_lock);
1788 return 0;
1789 } else
1790 mutex_exit(&lfs_lock);
1791
1792 /* Get dirops out of the way */
1793 if ((error = lfs_flush_dirops(fs)) != 0)
1794 return error;
1795
1796 if (lfs_dostats)
1797 ++lfs_stats.flush_invoked;
1798
1799 /*
1800 * Inline lfs_segwrite/lfs_writevnodes, but just for pageouts.
1801 */
1802 lfs_imtime(fs);
1803 lfs_seglock(fs, 0);
1804 sp = fs->lfs_sp;
1805
1806 /*
1807 * lfs_writevnodes, optimized to clear pageout requests.
1808 * Only write non-dirop files that are in the pageout queue.
1809 * We're very conservative about what we write; we want to be
1810 * fast and async.
1811 */
1812 mutex_enter(&lfs_lock);
1813 top:
1814 for (ip = TAILQ_FIRST(&fs->lfs_pchainhd); ip != NULL; ip = nip) {
1815 nip = TAILQ_NEXT(ip, i_lfs_pchain);
1816 vp = ITOV(ip);
1817
1818 if (!(ip->i_flags & IN_PAGING))
1819 goto top;
1820
1821 mutex_enter(vp->v_interlock);
1822 if (vdead_check(vp, VDEAD_NOWAIT) != 0 ||
1823 (vp->v_uflag & VU_DIROP) != 0) {
1824 mutex_exit(vp->v_interlock);
1825 continue;
1826 }
1827 if (vp->v_type != VREG) {
1828 mutex_exit(vp->v_interlock);
1829 continue;
1830 }
1831 if (lfs_vref(vp))
1832 continue;
1833 mutex_exit(&lfs_lock);
1834
1835 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_RETRY) != 0) {
1836 lfs_vunref(vp);
1837 mutex_enter(&lfs_lock);
1838 continue;
1839 }
1840
1841 error = lfs_writefile(fs, sp, vp);
1842 if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
1843 !(ip->i_flag & IN_ALLMOD)) {
1844 mutex_enter(&lfs_lock);
1845 LFS_SET_UINO(ip, IN_MODIFIED);
1846 mutex_exit(&lfs_lock);
1847 }
1848 KDASSERT(ip->i_number != LFS_IFILE_INUM);
1849 error2 = lfs_writeinode(fs, sp, ip);
1850
1851 VOP_UNLOCK(vp);
1852 lfs_vunref(vp);
1853
1854 if (error == EAGAIN || error2 == EAGAIN) {
1855 lfs_writeseg(fs, sp);
1856 mutex_enter(&lfs_lock);
1857 break;
1858 }
1859 mutex_enter(&lfs_lock);
1860 }
1861 mutex_exit(&lfs_lock);
1862 (void) lfs_writeseg(fs, sp);
1863 lfs_segunlock(fs);
1864
1865 return 0;
1866 }
1867
1868 /*
1869 * Provide a fcntl interface to sys_lfs_{segwait,bmapv,markv}.
1870 */
1871 int
1872 lfs_fcntl(void *v)
1873 {
1874 struct vop_fcntl_args /* {
1875 struct vnode *a_vp;
1876 u_int a_command;
1877 void * a_data;
1878 int a_fflag;
1879 kauth_cred_t a_cred;
1880 } */ *ap = v;
1881 struct timeval tv;
1882 struct timeval *tvp;
1883 BLOCK_INFO *blkiov;
1884 CLEANERINFO *cip;
1885 SEGUSE *sup;
1886 int blkcnt, error;
1887 size_t fh_size;
1888 struct lfs_fcntl_markv blkvp;
1889 struct lwp *l;
1890 fsid_t *fsidp;
1891 struct lfs *fs;
1892 struct buf *bp;
1893 fhandle_t *fhp;
1894 daddr_t off;
1895 int oclean;
1896
1897 /* Only respect LFS fcntls on fs root or Ifile */
1898 if (VTOI(ap->a_vp)->i_number != ULFS_ROOTINO &&
1899 VTOI(ap->a_vp)->i_number != LFS_IFILE_INUM) {
1900 return ulfs_fcntl(v);
1901 }
1902
1903 /* Avoid locking a draining lock */
1904 if (ap->a_vp->v_mount->mnt_iflag & IMNT_UNMOUNT) {
1905 return ESHUTDOWN;
1906 }
1907
1908 /* LFS control and monitoring fcntls are available only to root */
1909 l = curlwp;
1910 if (((ap->a_command & 0xff00) >> 8) == 'L' &&
1911 (error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
1912 KAUTH_REQ_SYSTEM_LFS_FCNTL, NULL, NULL, NULL)) != 0)
1913 return (error);
1914
1915 fs = VTOI(ap->a_vp)->i_lfs;
1916 fsidp = &ap->a_vp->v_mount->mnt_stat.f_fsidx;
1917
1918 error = 0;
1919 switch ((int)ap->a_command) {
1920 case LFCNSEGWAITALL_COMPAT_50:
1921 case LFCNSEGWAITALL_COMPAT:
1922 fsidp = NULL;
1923 /* FALLSTHROUGH */
1924 case LFCNSEGWAIT_COMPAT_50:
1925 case LFCNSEGWAIT_COMPAT:
1926 {
1927 struct timeval50 *tvp50
1928 = (struct timeval50 *)ap->a_data;
1929 timeval50_to_timeval(tvp50, &tv);
1930 tvp = &tv;
1931 }
1932 goto segwait_common;
1933 case LFCNSEGWAITALL:
1934 fsidp = NULL;
1935 /* FALLSTHROUGH */
1936 case LFCNSEGWAIT:
1937 tvp = (struct timeval *)ap->a_data;
1938 segwait_common:
1939 mutex_enter(&lfs_lock);
1940 ++fs->lfs_sleepers;
1941 mutex_exit(&lfs_lock);
1942
1943 error = lfs_segwait(fsidp, tvp);
1944
1945 mutex_enter(&lfs_lock);
1946 if (--fs->lfs_sleepers == 0)
1947 wakeup(&fs->lfs_sleepers);
1948 mutex_exit(&lfs_lock);
1949 return error;
1950
1951 case LFCNBMAPV:
1952 case LFCNMARKV:
1953 blkvp = *(struct lfs_fcntl_markv *)ap->a_data;
1954
1955 blkcnt = blkvp.blkcnt;
1956 if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
1957 return (EINVAL);
1958 blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
1959 if ((error = copyin(blkvp.blkiov, blkiov,
1960 blkcnt * sizeof(BLOCK_INFO))) != 0) {
1961 lfs_free(fs, blkiov, LFS_NB_BLKIOV);
1962 return error;
1963 }
1964
1965 mutex_enter(&lfs_lock);
1966 ++fs->lfs_sleepers;
1967 mutex_exit(&lfs_lock);
1968 if (ap->a_command == LFCNBMAPV)
1969 error = lfs_bmapv(l->l_proc, fsidp, blkiov, blkcnt);
1970 else /* LFCNMARKV */
1971 error = lfs_markv(l->l_proc, fsidp, blkiov, blkcnt);
1972 if (error == 0)
1973 error = copyout(blkiov, blkvp.blkiov,
1974 blkcnt * sizeof(BLOCK_INFO));
1975 mutex_enter(&lfs_lock);
1976 if (--fs->lfs_sleepers == 0)
1977 wakeup(&fs->lfs_sleepers);
1978 mutex_exit(&lfs_lock);
1979 lfs_free(fs, blkiov, LFS_NB_BLKIOV);
1980 return error;
1981
1982 case LFCNRECLAIM:
1983 /*
1984 * Flush dirops and write Ifile, allowing empty segments
1985 * to be immediately reclaimed.
1986 */
1987 lfs_writer_enter(fs, "pndirop");
1988 off = fs->lfs_offset;
1989 lfs_seglock(fs, SEGM_FORCE_CKP | SEGM_CKP);
1990 lfs_flush_dirops(fs);
1991 LFS_CLEANERINFO(cip, fs, bp);
1992 oclean = cip->clean;
1993 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1994 lfs_segwrite(ap->a_vp->v_mount, SEGM_FORCE_CKP);
1995 fs->lfs_sp->seg_flags |= SEGM_PROT;
1996 lfs_segunlock(fs);
1997 lfs_writer_leave(fs);
1998
1999 #ifdef DEBUG
2000 LFS_CLEANERINFO(cip, fs, bp);
2001 DLOG((DLOG_CLEAN, "lfs_fcntl: reclaim wrote %" PRId64
2002 " blocks, cleaned %" PRId32 " segments (activesb %d)\n",
2003 fs->lfs_offset - off, cip->clean - oclean,
2004 fs->lfs_activesb));
2005 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
2006 #else
2007 __USE(oclean);
2008 __USE(off);
2009 #endif
2010
2011 return 0;
2012
2013 case LFCNIFILEFH_COMPAT:
2014 /* Return the filehandle of the Ifile */
2015 if ((error = kauth_authorize_system(l->l_cred,
2016 KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)) != 0)
2017 return (error);
2018 fhp = (struct fhandle *)ap->a_data;
2019 fhp->fh_fsid = *fsidp;
2020 fh_size = 16; /* former VFS_MAXFIDSIZ */
2021 return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
2022
2023 case LFCNIFILEFH_COMPAT2:
2024 case LFCNIFILEFH:
2025 /* Return the filehandle of the Ifile */
2026 fhp = (struct fhandle *)ap->a_data;
2027 fhp->fh_fsid = *fsidp;
2028 fh_size = sizeof(struct lfs_fhandle) -
2029 offsetof(fhandle_t, fh_fid);
2030 return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
2031
2032 case LFCNREWIND:
2033 /* Move lfs_offset to the lowest-numbered segment */
2034 return lfs_rewind(fs, *(int *)ap->a_data);
2035
2036 case LFCNINVAL:
2037 /* Mark a segment SEGUSE_INVAL */
2038 LFS_SEGENTRY(sup, fs, *(int *)ap->a_data, bp);
2039 if (sup->su_nbytes > 0) {
2040 brelse(bp, 0);
2041 lfs_unset_inval_all(fs);
2042 return EBUSY;
2043 }
2044 sup->su_flags |= SEGUSE_INVAL;
2045 VOP_BWRITE(bp->b_vp, bp);
2046 return 0;
2047
2048 case LFCNRESIZE:
2049 /* Resize the filesystem */
2050 return lfs_resize_fs(fs, *(int *)ap->a_data);
2051
2052 case LFCNWRAPSTOP:
2053 case LFCNWRAPSTOP_COMPAT:
2054 /*
2055 * Hold lfs_newseg at segment 0; if requested, sleep until
2056 * the filesystem wraps around. To support external agents
2057 * (dump, fsck-based regression test) that need to look at
2058 * a snapshot of the filesystem, without necessarily
2059 * requiring that all fs activity stops.
2060 */
2061 if (fs->lfs_stoplwp == curlwp)
2062 return EALREADY;
2063
2064 mutex_enter(&lfs_lock);
2065 while (fs->lfs_stoplwp != NULL)
2066 cv_wait(&fs->lfs_stopcv, &lfs_lock);
2067 fs->lfs_stoplwp = curlwp;
2068 if (fs->lfs_nowrap == 0)
2069 log(LOG_NOTICE, "%s: disabled log wrap\n", fs->lfs_fsmnt);
2070 ++fs->lfs_nowrap;
2071 if (*(int *)ap->a_data == 1
2072 || ap->a_command == LFCNWRAPSTOP_COMPAT) {
2073 log(LOG_NOTICE, "LFCNSTOPWRAP waiting for log wrap\n");
2074 error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
2075 "segwrap", 0, &lfs_lock);
2076 log(LOG_NOTICE, "LFCNSTOPWRAP done waiting\n");
2077 if (error) {
2078 lfs_wrapgo(fs, VTOI(ap->a_vp), 0);
2079 }
2080 }
2081 mutex_exit(&lfs_lock);
2082 return 0;
2083
2084 case LFCNWRAPGO:
2085 case LFCNWRAPGO_COMPAT:
2086 /*
2087 * Having done its work, the agent wakes up the writer.
2088 * If the argument is 1, it sleeps until a new segment
2089 * is selected.
2090 */
2091 mutex_enter(&lfs_lock);
2092 error = lfs_wrapgo(fs, VTOI(ap->a_vp),
2093 ap->a_command == LFCNWRAPGO_COMPAT ? 1 :
2094 *((int *)ap->a_data));
2095 mutex_exit(&lfs_lock);
2096 return error;
2097
2098 case LFCNWRAPPASS:
2099 if ((VTOI(ap->a_vp)->i_lfs_iflags & LFSI_WRAPWAIT))
2100 return EALREADY;
2101 mutex_enter(&lfs_lock);
2102 if (fs->lfs_stoplwp != curlwp) {
2103 mutex_exit(&lfs_lock);
2104 return EALREADY;
2105 }
2106 if (fs->lfs_nowrap == 0) {
2107 mutex_exit(&lfs_lock);
2108 return EBUSY;
2109 }
2110 fs->lfs_wrappass = 1;
2111 wakeup(&fs->lfs_wrappass);
2112 /* Wait for the log to wrap, if asked */
2113 if (*(int *)ap->a_data) {
2114 mutex_enter(ap->a_vp->v_interlock);
2115 if (lfs_vref(ap->a_vp) != 0)
2116 panic("LFCNWRAPPASS: lfs_vref failed");
2117 VTOI(ap->a_vp)->i_lfs_iflags |= LFSI_WRAPWAIT;
2118 log(LOG_NOTICE, "LFCNPASS waiting for log wrap\n");
2119 error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
2120 "segwrap", 0, &lfs_lock);
2121 log(LOG_NOTICE, "LFCNPASS done waiting\n");
2122 VTOI(ap->a_vp)->i_lfs_iflags &= ~LFSI_WRAPWAIT;
2123 lfs_vunref(ap->a_vp);
2124 }
2125 mutex_exit(&lfs_lock);
2126 return error;
2127
2128 case LFCNWRAPSTATUS:
2129 mutex_enter(&lfs_lock);
2130 *(int *)ap->a_data = fs->lfs_wrapstatus;
2131 mutex_exit(&lfs_lock);
2132 return 0;
2133
2134 default:
2135 return ulfs_fcntl(v);
2136 }
2137 return 0;
2138 }
2139
2140 /*
2141 * Return the last logical file offset that should be written for this file
2142 * if we're doing a write that ends at "size". If writing, we need to know
2143 * about sizes on disk, i.e. fragments if there are any; if reading, we need
2144 * to know about entire blocks.
2145 */
2146 void
2147 lfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
2148 {
2149 struct inode *ip = VTOI(vp);
2150 struct lfs *fs = ip->i_lfs;
2151 daddr_t olbn, nlbn;
2152
2153 olbn = lfs_lblkno(fs, ip->i_size);
2154 nlbn = lfs_lblkno(fs, size);
2155 if (!(flags & GOP_SIZE_MEM) && nlbn < ULFS_NDADDR && olbn <= nlbn) {
2156 *eobp = lfs_fragroundup(fs, size);
2157 } else {
2158 *eobp = lfs_blkroundup(fs, size);
2159 }
2160 }
2161
2162 #ifdef DEBUG
2163 void lfs_dump_vop(void *);
2164
2165 void
2166 lfs_dump_vop(void *v)
2167 {
2168 struct vop_putpages_args /* {
2169 struct vnode *a_vp;
2170 voff_t a_offlo;
2171 voff_t a_offhi;
2172 int a_flags;
2173 } */ *ap = v;
2174
2175 #ifdef DDB
2176 vfs_vnode_print(ap->a_vp, 0, printf);
2177 #endif
2178 lfs_dump_dinode(VTOI(ap->a_vp)->i_din.ffs1_din);
2179 }
2180 #endif
2181
2182 int
2183 lfs_mmap(void *v)
2184 {
2185 struct vop_mmap_args /* {
2186 const struct vnodeop_desc *a_desc;
2187 struct vnode *a_vp;
2188 vm_prot_t a_prot;
2189 kauth_cred_t a_cred;
2190 } */ *ap = v;
2191
2192 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM)
2193 return EOPNOTSUPP;
2194 return ulfs_mmap(v);
2195 }
2196
2197 static int
2198 lfs_openextattr(void *v)
2199 {
2200 struct vop_openextattr_args /* {
2201 struct vnode *a_vp;
2202 kauth_cred_t a_cred;
2203 struct proc *a_p;
2204 } */ *ap = v;
2205 struct inode *ip = VTOI(ap->a_vp);
2206 struct ulfsmount *ump = ip->i_ump;
2207 //struct lfs *fs = ip->i_lfs;
2208
2209 /* Not supported for ULFS1 file systems. */
2210 if (ump->um_fstype == ULFS1)
2211 return (EOPNOTSUPP);
2212
2213 /* XXX Not implemented for ULFS2 file systems. */
2214 return (EOPNOTSUPP);
2215 }
2216
2217 static int
2218 lfs_closeextattr(void *v)
2219 {
2220 struct vop_closeextattr_args /* {
2221 struct vnode *a_vp;
2222 int a_commit;
2223 kauth_cred_t a_cred;
2224 struct proc *a_p;
2225 } */ *ap = v;
2226 struct inode *ip = VTOI(ap->a_vp);
2227 struct ulfsmount *ump = ip->i_ump;
2228 //struct lfs *fs = ip->i_lfs;
2229
2230 /* Not supported for ULFS1 file systems. */
2231 if (ump->um_fstype == ULFS1)
2232 return (EOPNOTSUPP);
2233
2234 /* XXX Not implemented for ULFS2 file systems. */
2235 return (EOPNOTSUPP);
2236 }
2237
2238 static int
2239 lfs_getextattr(void *v)
2240 {
2241 struct vop_getextattr_args /* {
2242 struct vnode *a_vp;
2243 int a_attrnamespace;
2244 const char *a_name;
2245 struct uio *a_uio;
2246 size_t *a_size;
2247 kauth_cred_t a_cred;
2248 struct proc *a_p;
2249 } */ *ap = v;
2250 struct vnode *vp = ap->a_vp;
2251 struct inode *ip = VTOI(vp);
2252 struct ulfsmount *ump = ip->i_ump;
2253 //struct lfs *fs = ip->i_lfs;
2254 int error;
2255
2256 if (ump->um_fstype == ULFS1) {
2257 #ifdef LFS_EXTATTR
2258 fstrans_start(vp->v_mount, FSTRANS_SHARED);
2259 error = ulfs_getextattr(ap);
2260 fstrans_done(vp->v_mount);
2261 #else
2262 error = EOPNOTSUPP;
2263 #endif
2264 return error;
2265 }
2266
2267 /* XXX Not implemented for ULFS2 file systems. */
2268 return (EOPNOTSUPP);
2269 }
2270
2271 static int
2272 lfs_setextattr(void *v)
2273 {
2274 struct vop_setextattr_args /* {
2275 struct vnode *a_vp;
2276 int a_attrnamespace;
2277 const char *a_name;
2278 struct uio *a_uio;
2279 kauth_cred_t a_cred;
2280 struct proc *a_p;
2281 } */ *ap = v;
2282 struct vnode *vp = ap->a_vp;
2283 struct inode *ip = VTOI(vp);
2284 struct ulfsmount *ump = ip->i_ump;
2285 //struct lfs *fs = ip->i_lfs;
2286 int error;
2287
2288 if (ump->um_fstype == ULFS1) {
2289 #ifdef LFS_EXTATTR
2290 fstrans_start(vp->v_mount, FSTRANS_SHARED);
2291 error = ulfs_setextattr(ap);
2292 fstrans_done(vp->v_mount);
2293 #else
2294 error = EOPNOTSUPP;
2295 #endif
2296 return error;
2297 }
2298
2299 /* XXX Not implemented for ULFS2 file systems. */
2300 return (EOPNOTSUPP);
2301 }
2302
2303 static int
2304 lfs_listextattr(void *v)
2305 {
2306 struct vop_listextattr_args /* {
2307 struct vnode *a_vp;
2308 int a_attrnamespace;
2309 struct uio *a_uio;
2310 size_t *a_size;
2311 kauth_cred_t a_cred;
2312 struct proc *a_p;
2313 } */ *ap = v;
2314 struct vnode *vp = ap->a_vp;
2315 struct inode *ip = VTOI(vp);
2316 struct ulfsmount *ump = ip->i_ump;
2317 //struct lfs *fs = ip->i_lfs;
2318 int error;
2319
2320 if (ump->um_fstype == ULFS1) {
2321 #ifdef LFS_EXTATTR
2322 fstrans_start(vp->v_mount, FSTRANS_SHARED);
2323 error = ulfs_listextattr(ap);
2324 fstrans_done(vp->v_mount);
2325 #else
2326 error = EOPNOTSUPP;
2327 #endif
2328 return error;
2329 }
2330
2331 /* XXX Not implemented for ULFS2 file systems. */
2332 return (EOPNOTSUPP);
2333 }
2334
2335 static int
2336 lfs_deleteextattr(void *v)
2337 {
2338 struct vop_deleteextattr_args /* {
2339 struct vnode *a_vp;
2340 int a_attrnamespace;
2341 kauth_cred_t a_cred;
2342 struct proc *a_p;
2343 } */ *ap = v;
2344 struct vnode *vp = ap->a_vp;
2345 struct inode *ip = VTOI(vp);
2346 struct ulfsmount *ump = ip->i_ump;
2347 //struct fs *fs = ip->i_lfs;
2348 int error;
2349
2350 if (ump->um_fstype == ULFS1) {
2351 #ifdef LFS_EXTATTR
2352 fstrans_start(vp->v_mount, FSTRANS_SHARED);
2353 error = ulfs_deleteextattr(ap);
2354 fstrans_done(vp->v_mount);
2355 #else
2356 error = EOPNOTSUPP;
2357 #endif
2358 return error;
2359 }
2360
2361 /* XXX Not implemented for ULFS2 file systems. */
2362 return (EOPNOTSUPP);
2363 }
2364