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