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