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