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