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