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