]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/ufs/ffs/ffs_vnops.c
FFS: implement special VOP_VPUT_PAIR().
[FreeBSD/FreeBSD.git] / sys / ufs / ffs / ffs_vnops.c
1 /*-
2  * SPDX-License-Identifier: (BSD-2-Clause-FreeBSD AND BSD-3-Clause)
3  *
4  * Copyright (c) 2002, 2003 Networks Associates Technology, Inc.
5  * All rights reserved.
6  *
7  * This software was developed for the FreeBSD Project by Marshall
8  * Kirk McKusick and Network Associates Laboratories, the Security
9  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
10  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11  * research program
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * Copyright (c) 1982, 1986, 1989, 1993
35  *      The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *      from: @(#)ufs_readwrite.c       8.11 (Berkeley) 5/8/95
62  * from: $FreeBSD: .../ufs/ufs_readwrite.c,v 1.96 2002/08/12 09:22:11 phk ...
63  *      @(#)ffs_vnops.c 8.15 (Berkeley) 5/14/95
64  */
65
66 #include <sys/cdefs.h>
67 __FBSDID("$FreeBSD$");
68
69 #include "opt_directio.h"
70 #include "opt_ffs.h"
71
72 #include <sys/param.h>
73 #include <sys/bio.h>
74 #include <sys/systm.h>
75 #include <sys/buf.h>
76 #include <sys/conf.h>
77 #include <sys/extattr.h>
78 #include <sys/kernel.h>
79 #include <sys/limits.h>
80 #include <sys/malloc.h>
81 #include <sys/mount.h>
82 #include <sys/priv.h>
83 #include <sys/rwlock.h>
84 #include <sys/stat.h>
85 #include <sys/sysctl.h>
86 #include <sys/vmmeter.h>
87 #include <sys/vnode.h>
88
89 #include <vm/vm.h>
90 #include <vm/vm_param.h>
91 #include <vm/vm_extern.h>
92 #include <vm/vm_object.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_pager.h>
95 #include <vm/vnode_pager.h>
96
97 #include <ufs/ufs/extattr.h>
98 #include <ufs/ufs/quota.h>
99 #include <ufs/ufs/inode.h>
100 #include <ufs/ufs/ufs_extern.h>
101 #include <ufs/ufs/ufsmount.h>
102
103 #include <ufs/ffs/fs.h>
104 #include <ufs/ffs/ffs_extern.h>
105
106 #define ALIGNED_TO(ptr, s)      \
107         (((uintptr_t)(ptr) & (_Alignof(s) - 1)) == 0)
108
109 #ifdef DIRECTIO
110 extern int      ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
111 #endif
112 static vop_fdatasync_t  ffs_fdatasync;
113 static vop_fsync_t      ffs_fsync;
114 static vop_getpages_t   ffs_getpages;
115 static vop_getpages_async_t     ffs_getpages_async;
116 static vop_lock1_t      ffs_lock;
117 #ifdef INVARIANTS
118 static vop_unlock_t     ffs_unlock_debug;
119 #endif
120 static vop_read_t       ffs_read;
121 static vop_write_t      ffs_write;
122 static int      ffs_extread(struct vnode *vp, struct uio *uio, int ioflag);
123 static int      ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag,
124                     struct ucred *cred);
125 static vop_strategy_t   ffsext_strategy;
126 static vop_closeextattr_t       ffs_closeextattr;
127 static vop_deleteextattr_t      ffs_deleteextattr;
128 static vop_getextattr_t ffs_getextattr;
129 static vop_listextattr_t        ffs_listextattr;
130 static vop_openextattr_t        ffs_openextattr;
131 static vop_setextattr_t ffs_setextattr;
132 static vop_vptofh_t     ffs_vptofh;
133 static vop_vput_pair_t  ffs_vput_pair;
134
135 /* Global vfs data structures for ufs. */
136 struct vop_vector ffs_vnodeops1 = {
137         .vop_default =          &ufs_vnodeops,
138         .vop_fsync =            ffs_fsync,
139         .vop_fdatasync =        ffs_fdatasync,
140         .vop_getpages =         ffs_getpages,
141         .vop_getpages_async =   ffs_getpages_async,
142         .vop_lock1 =            ffs_lock,
143 #ifdef INVARIANTS
144         .vop_unlock =           ffs_unlock_debug,
145 #endif
146         .vop_read =             ffs_read,
147         .vop_reallocblks =      ffs_reallocblks,
148         .vop_write =            ffs_write,
149         .vop_vptofh =           ffs_vptofh,
150         .vop_vput_pair =        ffs_vput_pair,
151 };
152 VFS_VOP_VECTOR_REGISTER(ffs_vnodeops1);
153
154 struct vop_vector ffs_fifoops1 = {
155         .vop_default =          &ufs_fifoops,
156         .vop_fsync =            ffs_fsync,
157         .vop_fdatasync =        ffs_fdatasync,
158         .vop_lock1 =            ffs_lock,
159 #ifdef INVARIANTS
160         .vop_unlock =           ffs_unlock_debug,
161 #endif
162         .vop_vptofh =           ffs_vptofh,
163 };
164 VFS_VOP_VECTOR_REGISTER(ffs_fifoops1);
165
166 /* Global vfs data structures for ufs. */
167 struct vop_vector ffs_vnodeops2 = {
168         .vop_default =          &ufs_vnodeops,
169         .vop_fsync =            ffs_fsync,
170         .vop_fdatasync =        ffs_fdatasync,
171         .vop_getpages =         ffs_getpages,
172         .vop_getpages_async =   ffs_getpages_async,
173         .vop_lock1 =            ffs_lock,
174 #ifdef INVARIANTS
175         .vop_unlock =           ffs_unlock_debug,
176 #endif
177         .vop_read =             ffs_read,
178         .vop_reallocblks =      ffs_reallocblks,
179         .vop_write =            ffs_write,
180         .vop_closeextattr =     ffs_closeextattr,
181         .vop_deleteextattr =    ffs_deleteextattr,
182         .vop_getextattr =       ffs_getextattr,
183         .vop_listextattr =      ffs_listextattr,
184         .vop_openextattr =      ffs_openextattr,
185         .vop_setextattr =       ffs_setextattr,
186         .vop_vptofh =           ffs_vptofh,
187         .vop_vput_pair =        ffs_vput_pair,
188 };
189 VFS_VOP_VECTOR_REGISTER(ffs_vnodeops2);
190
191 struct vop_vector ffs_fifoops2 = {
192         .vop_default =          &ufs_fifoops,
193         .vop_fsync =            ffs_fsync,
194         .vop_fdatasync =        ffs_fdatasync,
195         .vop_lock1 =            ffs_lock,
196 #ifdef INVARIANTS
197         .vop_unlock =           ffs_unlock_debug,
198 #endif
199         .vop_reallocblks =      ffs_reallocblks,
200         .vop_strategy =         ffsext_strategy,
201         .vop_closeextattr =     ffs_closeextattr,
202         .vop_deleteextattr =    ffs_deleteextattr,
203         .vop_getextattr =       ffs_getextattr,
204         .vop_listextattr =      ffs_listextattr,
205         .vop_openextattr =      ffs_openextattr,
206         .vop_setextattr =       ffs_setextattr,
207         .vop_vptofh =           ffs_vptofh,
208 };
209 VFS_VOP_VECTOR_REGISTER(ffs_fifoops2);
210
211 /*
212  * Synch an open file.
213  */
214 /* ARGSUSED */
215 static int
216 ffs_fsync(struct vop_fsync_args *ap)
217 {
218         struct vnode *vp;
219         struct bufobj *bo;
220         int error;
221
222         vp = ap->a_vp;
223         bo = &vp->v_bufobj;
224 retry:
225         error = ffs_syncvnode(vp, ap->a_waitfor, 0);
226         if (error)
227                 return (error);
228         if (ap->a_waitfor == MNT_WAIT && DOINGSOFTDEP(vp)) {
229                 error = softdep_fsync(vp);
230                 if (error)
231                         return (error);
232
233                 /*
234                  * The softdep_fsync() function may drop vp lock,
235                  * allowing for dirty buffers to reappear on the
236                  * bo_dirty list. Recheck and resync as needed.
237                  */
238                 BO_LOCK(bo);
239                 if ((vp->v_type == VREG || vp->v_type == VDIR) &&
240                     (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
241                         BO_UNLOCK(bo);
242                         goto retry;
243                 }
244                 BO_UNLOCK(bo);
245         }
246         if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), 0))
247                 return (ENXIO);
248         return (0);
249 }
250
251 int
252 ffs_syncvnode(struct vnode *vp, int waitfor, int flags)
253 {
254         struct inode *ip;
255         struct bufobj *bo;
256         struct ufsmount *ump;
257         struct buf *bp, *nbp;
258         ufs_lbn_t lbn;
259         int error, passes;
260         bool still_dirty, unlocked, wait;
261
262         ip = VTOI(vp);
263         bo = &vp->v_bufobj;
264         ump = VFSTOUFS(vp->v_mount);
265
266         /*
267          * When doing MNT_WAIT we must first flush all dependencies
268          * on the inode.
269          */
270         if (DOINGSOFTDEP(vp) && waitfor == MNT_WAIT &&
271             (error = softdep_sync_metadata(vp)) != 0) {
272                 if (ffs_fsfail_cleanup(ump, error))
273                         error = 0;
274                 return (error);
275         }
276
277         /*
278          * Flush all dirty buffers associated with a vnode.
279          */
280         error = 0;
281         passes = 0;
282         wait = false;   /* Always do an async pass first. */
283         unlocked = false;
284         lbn = lblkno(ITOFS(ip), (ip->i_size + ITOFS(ip)->fs_bsize - 1));
285         BO_LOCK(bo);
286 loop:
287         TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
288                 bp->b_vflags &= ~BV_SCANNED;
289         TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
290                 /*
291                  * Reasons to skip this buffer: it has already been considered
292                  * on this pass, the buffer has dependencies that will cause
293                  * it to be redirtied and it has not already been deferred,
294                  * or it is already being written.
295                  */
296                 if ((bp->b_vflags & BV_SCANNED) != 0)
297                         continue;
298                 bp->b_vflags |= BV_SCANNED;
299                 /*
300                  * Flush indirects in order, if requested.
301                  *
302                  * Note that if only datasync is requested, we can
303                  * skip indirect blocks when softupdates are not
304                  * active.  Otherwise we must flush them with data,
305                  * since dependencies prevent data block writes.
306                  */
307                 if (waitfor == MNT_WAIT && bp->b_lblkno <= -UFS_NDADDR &&
308                     (lbn_level(bp->b_lblkno) >= passes ||
309                     ((flags & DATA_ONLY) != 0 && !DOINGSOFTDEP(vp))))
310                         continue;
311                 if (bp->b_lblkno > lbn)
312                         panic("ffs_syncvnode: syncing truncated data.");
313                 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) {
314                         BO_UNLOCK(bo);
315                 } else if (wait) {
316                         if (BUF_LOCK(bp,
317                             LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
318                             BO_LOCKPTR(bo)) != 0) {
319                                 bp->b_vflags &= ~BV_SCANNED;
320                                 goto next;
321                         }
322                 } else
323                         continue;
324                 if ((bp->b_flags & B_DELWRI) == 0)
325                         panic("ffs_fsync: not dirty");
326                 /*
327                  * Check for dependencies and potentially complete them.
328                  */
329                 if (!LIST_EMPTY(&bp->b_dep) &&
330                     (error = softdep_sync_buf(vp, bp,
331                     wait ? MNT_WAIT : MNT_NOWAIT)) != 0) {
332                         /*
333                          * Lock order conflict, buffer was already unlocked,
334                          * and vnode possibly unlocked.
335                          */
336                         if (error == ERELOOKUP) {
337                                 if (vp->v_data == NULL)
338                                         return (EBADF);
339                                 unlocked = true;
340                                 if (DOINGSOFTDEP(vp) && waitfor == MNT_WAIT &&
341                                     (error = softdep_sync_metadata(vp)) != 0) {
342                                         if (ffs_fsfail_cleanup(ump, error))
343                                                 error = 0;
344                                         return (unlocked && error == 0 ?
345                                             ERELOOKUP : error);
346                                 }
347                                 /* Re-evaluate inode size */
348                                 lbn = lblkno(ITOFS(ip), (ip->i_size +
349                                     ITOFS(ip)->fs_bsize - 1));
350                                 goto next;
351                         }
352                         /* I/O error. */
353                         if (error != EBUSY) {
354                                 BUF_UNLOCK(bp);
355                                 return (error);
356                         }
357                         /* If we deferred once, don't defer again. */
358                         if ((bp->b_flags & B_DEFERRED) == 0) {
359                                 bp->b_flags |= B_DEFERRED;
360                                 BUF_UNLOCK(bp);
361                                 goto next;
362                         }
363                 }
364                 if (wait) {
365                         bremfree(bp);
366                         error = bwrite(bp);
367                         if (ffs_fsfail_cleanup(ump, error))
368                                 error = 0;
369                         if (error != 0)
370                                 return (error);
371                 } else if ((bp->b_flags & B_CLUSTEROK)) {
372                         (void) vfs_bio_awrite(bp);
373                 } else {
374                         bremfree(bp);
375                         (void) bawrite(bp);
376                 }
377 next:
378                 /*
379                  * Since we may have slept during the I/O, we need
380                  * to start from a known point.
381                  */
382                 BO_LOCK(bo);
383                 nbp = TAILQ_FIRST(&bo->bo_dirty.bv_hd);
384         }
385         if (waitfor != MNT_WAIT) {
386                 BO_UNLOCK(bo);
387                 if ((flags & NO_INO_UPDT) != 0)
388                         return (unlocked ? ERELOOKUP : 0);
389                 error = ffs_update(vp, 0);
390                 if (error == 0 && unlocked)
391                         error = ERELOOKUP;
392                 return (error);
393         }
394         /* Drain IO to see if we're done. */
395         bufobj_wwait(bo, 0, 0);
396         /*
397          * Block devices associated with filesystems may have new I/O
398          * requests posted for them even if the vnode is locked, so no
399          * amount of trying will get them clean.  We make several passes
400          * as a best effort.
401          *
402          * Regular files may need multiple passes to flush all dependency
403          * work as it is possible that we must write once per indirect
404          * level, once for the leaf, and once for the inode and each of
405          * these will be done with one sync and one async pass.
406          */
407         if (bo->bo_dirty.bv_cnt > 0) {
408                 if ((flags & DATA_ONLY) == 0) {
409                         still_dirty = true;
410                 } else {
411                         /*
412                          * For data-only sync, dirty indirect buffers
413                          * are ignored.
414                          */
415                         still_dirty = false;
416                         TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
417                                 if (bp->b_lblkno > -UFS_NDADDR) {
418                                         still_dirty = true;
419                                         break;
420                                 }
421                         }
422                 }
423
424                 if (still_dirty) {
425                         /* Write the inode after sync passes to flush deps. */
426                         if (wait && DOINGSOFTDEP(vp) &&
427                             (flags & NO_INO_UPDT) == 0) {
428                                 BO_UNLOCK(bo);
429                                 ffs_update(vp, 1);
430                                 BO_LOCK(bo);
431                         }
432                         /* switch between sync/async. */
433                         wait = !wait;
434                         if (wait || ++passes < UFS_NIADDR + 2)
435                                 goto loop;
436                 }
437         }
438         BO_UNLOCK(bo);
439         error = 0;
440         if ((flags & DATA_ONLY) == 0) {
441                 if ((flags & NO_INO_UPDT) == 0)
442                         error = ffs_update(vp, 1);
443                 if (DOINGSUJ(vp))
444                         softdep_journal_fsync(VTOI(vp));
445         } else if ((ip->i_flags & (IN_SIZEMOD | IN_IBLKDATA)) != 0) {
446                 error = ffs_update(vp, 1);
447         }
448         if (error == 0 && unlocked)
449                 error = ERELOOKUP;
450         if (error == 0)
451                 ip->i_flag &= ~IN_NEEDSYNC;
452         return (error);
453 }
454
455 static int
456 ffs_fdatasync(struct vop_fdatasync_args *ap)
457 {
458
459         return (ffs_syncvnode(ap->a_vp, MNT_WAIT, DATA_ONLY));
460 }
461
462 static int
463 ffs_lock(ap)
464         struct vop_lock1_args /* {
465                 struct vnode *a_vp;
466                 int a_flags;
467                 char *file;
468                 int line;
469         } */ *ap;
470 {
471 #if !defined(NO_FFS_SNAPSHOT) || defined(DIAGNOSTIC)
472         struct vnode *vp = ap->a_vp;
473 #endif  /* !NO_FFS_SNAPSHOT || DIAGNOSTIC */
474 #ifdef DIAGNOSTIC
475         struct inode *ip;
476 #endif  /* DIAGNOSTIC */
477         int result;
478 #ifndef NO_FFS_SNAPSHOT
479         int flags;
480         struct lock *lkp;
481
482         /*
483          * Adaptive spinning mixed with SU leads to trouble. use a giant hammer
484          * and only use it when LK_NODDLKTREAT is set. Currently this means it
485          * is only used during path lookup.
486          */
487         if ((ap->a_flags & LK_NODDLKTREAT) != 0)
488                 ap->a_flags |= LK_ADAPTIVE;
489         switch (ap->a_flags & LK_TYPE_MASK) {
490         case LK_SHARED:
491         case LK_UPGRADE:
492         case LK_EXCLUSIVE:
493                 flags = ap->a_flags;
494                 for (;;) {
495 #ifdef DEBUG_VFS_LOCKS
496                         VNPASS(vp->v_holdcnt != 0, vp);
497 #endif  /* DEBUG_VFS_LOCKS */
498                         lkp = vp->v_vnlock;
499                         result = lockmgr_lock_flags(lkp, flags,
500                             &VI_MTX(vp)->lock_object, ap->a_file, ap->a_line);
501                         if (lkp == vp->v_vnlock || result != 0)
502                                 break;
503                         /*
504                          * Apparent success, except that the vnode
505                          * mutated between snapshot file vnode and
506                          * regular file vnode while this process
507                          * slept.  The lock currently held is not the
508                          * right lock.  Release it, and try to get the
509                          * new lock.
510                          */
511                         lockmgr_unlock(lkp);
512                         if ((flags & (LK_INTERLOCK | LK_NOWAIT)) ==
513                             (LK_INTERLOCK | LK_NOWAIT))
514                                 return (EBUSY);
515                         if ((flags & LK_TYPE_MASK) == LK_UPGRADE)
516                                 flags = (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE;
517                         flags &= ~LK_INTERLOCK;
518                 }
519 #ifdef DIAGNOSTIC
520                 switch (ap->a_flags & LK_TYPE_MASK) {
521                 case LK_UPGRADE:
522                 case LK_EXCLUSIVE:
523                         if (result == 0 && vp->v_vnlock->lk_recurse == 0) {
524                                 ip = VTOI(vp);
525                                 if (ip != NULL)
526                                         ip->i_lock_gen++;
527                         }
528                 }
529 #endif  /* DIAGNOSTIC */
530                 break;
531         default:
532 #ifdef DIAGNOSTIC
533                 if ((ap->a_flags & LK_TYPE_MASK) == LK_DOWNGRADE) {
534                         ip = VTOI(vp);
535                         if (ip != NULL)
536                                 ufs_unlock_tracker(ip);
537                 }
538 #endif  /* DIAGNOSTIC */
539                 result = VOP_LOCK1_APV(&ufs_vnodeops, ap);
540                 break;
541         }
542 #else   /* NO_FFS_SNAPSHOT */
543         /*
544          * See above for an explanation.
545          */
546         if ((ap->a_flags & LK_NODDLKTREAT) != 0)
547                 ap->a_flags |= LK_ADAPTIVE;
548 #ifdef DIAGNOSTIC
549         if ((ap->a_flags & LK_TYPE_MASK) == LK_DOWNGRADE) {
550                 ip = VTOI(vp);
551                 if (ip != NULL)
552                         ufs_unlock_tracker(ip);
553         }
554 #endif  /* DIAGNOSTIC */
555         result =  VOP_LOCK1_APV(&ufs_vnodeops, ap);
556 #endif  /* NO_FFS_SNAPSHOT */
557 #ifdef DIAGNOSTIC
558         switch (ap->a_flags & LK_TYPE_MASK) {
559         case LK_UPGRADE:
560         case LK_EXCLUSIVE:
561                 if (result == 0 && vp->v_vnlock->lk_recurse == 0) {
562                         ip = VTOI(vp);
563                         if (ip != NULL)
564                                 ip->i_lock_gen++;
565                 }
566         }
567 #endif  /* DIAGNOSTIC */
568         return (result);
569 }
570
571 #ifdef INVARIANTS
572 static int
573 ffs_unlock_debug(struct vop_unlock_args *ap)
574 {
575         struct vnode *vp;
576         struct inode *ip;
577
578         vp = ap->a_vp;
579         ip = VTOI(vp);
580         if (ip->i_flag & UFS_INODE_FLAG_LAZY_MASK_ASSERTABLE) {
581                 if ((vp->v_mflag & VMP_LAZYLIST) == 0) {
582                         VI_LOCK(vp);
583                         VNASSERT((vp->v_mflag & VMP_LAZYLIST), vp,
584                             ("%s: modified vnode (%x) not on lazy list",
585                             __func__, ip->i_flag));
586                         VI_UNLOCK(vp);
587                 }
588         }
589 #ifdef DIAGNOSTIC
590         if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE && ip != NULL &&
591             vp->v_vnlock->lk_recurse == 0)
592                 ufs_unlock_tracker(ip);
593 #endif
594         return (VOP_UNLOCK_APV(&ufs_vnodeops, ap));
595 }
596 #endif
597
598 static int
599 ffs_read_hole(struct uio *uio, long xfersize, long *size)
600 {
601         ssize_t saved_resid, tlen;
602         int error;
603
604         while (xfersize > 0) {
605                 tlen = min(xfersize, ZERO_REGION_SIZE);
606                 saved_resid = uio->uio_resid;
607                 error = vn_io_fault_uiomove(__DECONST(void *, zero_region),
608                     tlen, uio);
609                 if (error != 0)
610                         return (error);
611                 tlen = saved_resid - uio->uio_resid;
612                 xfersize -= tlen;
613                 *size -= tlen;
614         }
615         return (0);
616 }
617
618 /*
619  * Vnode op for reading.
620  */
621 static int
622 ffs_read(ap)
623         struct vop_read_args /* {
624                 struct vnode *a_vp;
625                 struct uio *a_uio;
626                 int a_ioflag;
627                 struct ucred *a_cred;
628         } */ *ap;
629 {
630         struct vnode *vp;
631         struct inode *ip;
632         struct uio *uio;
633         struct fs *fs;
634         struct buf *bp;
635         ufs_lbn_t lbn, nextlbn;
636         off_t bytesinfile;
637         long size, xfersize, blkoffset;
638         ssize_t orig_resid;
639         int bflag, error, ioflag, seqcount;
640
641         vp = ap->a_vp;
642         uio = ap->a_uio;
643         ioflag = ap->a_ioflag;
644         if (ap->a_ioflag & IO_EXT)
645 #ifdef notyet
646                 return (ffs_extread(vp, uio, ioflag));
647 #else
648                 panic("ffs_read+IO_EXT");
649 #endif
650 #ifdef DIRECTIO
651         if ((ioflag & IO_DIRECT) != 0) {
652                 int workdone;
653
654                 error = ffs_rawread(vp, uio, &workdone);
655                 if (error != 0 || workdone != 0)
656                         return error;
657         }
658 #endif
659
660         seqcount = ap->a_ioflag >> IO_SEQSHIFT;
661         ip = VTOI(vp);
662
663 #ifdef INVARIANTS
664         if (uio->uio_rw != UIO_READ)
665                 panic("ffs_read: mode");
666
667         if (vp->v_type == VLNK) {
668                 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
669                         panic("ffs_read: short symlink");
670         } else if (vp->v_type != VREG && vp->v_type != VDIR)
671                 panic("ffs_read: type %d",  vp->v_type);
672 #endif
673         orig_resid = uio->uio_resid;
674         KASSERT(orig_resid >= 0, ("ffs_read: uio->uio_resid < 0"));
675         if (orig_resid == 0)
676                 return (0);
677         KASSERT(uio->uio_offset >= 0, ("ffs_read: uio->uio_offset < 0"));
678         fs = ITOFS(ip);
679         if (uio->uio_offset < ip->i_size &&
680             uio->uio_offset >= fs->fs_maxfilesize)
681                 return (EOVERFLOW);
682
683         bflag = GB_UNMAPPED | (uio->uio_segflg == UIO_NOCOPY ? 0 : GB_NOSPARSE);
684         for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
685                 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
686                         break;
687                 lbn = lblkno(fs, uio->uio_offset);
688                 nextlbn = lbn + 1;
689
690                 /*
691                  * size of buffer.  The buffer representing the
692                  * end of the file is rounded up to the size of
693                  * the block type ( fragment or full block,
694                  * depending ).
695                  */
696                 size = blksize(fs, ip, lbn);
697                 blkoffset = blkoff(fs, uio->uio_offset);
698
699                 /*
700                  * The amount we want to transfer in this iteration is
701                  * one FS block less the amount of the data before
702                  * our startpoint (duh!)
703                  */
704                 xfersize = fs->fs_bsize - blkoffset;
705
706                 /*
707                  * But if we actually want less than the block,
708                  * or the file doesn't have a whole block more of data,
709                  * then use the lesser number.
710                  */
711                 if (uio->uio_resid < xfersize)
712                         xfersize = uio->uio_resid;
713                 if (bytesinfile < xfersize)
714                         xfersize = bytesinfile;
715
716                 if (lblktosize(fs, nextlbn) >= ip->i_size) {
717                         /*
718                          * Don't do readahead if this is the end of the file.
719                          */
720                         error = bread_gb(vp, lbn, size, NOCRED, bflag, &bp);
721                 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
722                         /*
723                          * Otherwise if we are allowed to cluster,
724                          * grab as much as we can.
725                          *
726                          * XXX  This may not be a win if we are not
727                          * doing sequential access.
728                          */
729                         error = cluster_read(vp, ip->i_size, lbn,
730                             size, NOCRED, blkoffset + uio->uio_resid,
731                             seqcount, bflag, &bp);
732                 } else if (seqcount > 1) {
733                         /*
734                          * If we are NOT allowed to cluster, then
735                          * if we appear to be acting sequentially,
736                          * fire off a request for a readahead
737                          * as well as a read. Note that the 4th and 5th
738                          * arguments point to arrays of the size specified in
739                          * the 6th argument.
740                          */
741                         u_int nextsize = blksize(fs, ip, nextlbn);
742                         error = breadn_flags(vp, lbn, lbn, size, &nextlbn,
743                             &nextsize, 1, NOCRED, bflag, NULL, &bp);
744                 } else {
745                         /*
746                          * Failing all of the above, just read what the
747                          * user asked for. Interestingly, the same as
748                          * the first option above.
749                          */
750                         error = bread_gb(vp, lbn, size, NOCRED, bflag, &bp);
751                 }
752                 if (error == EJUSTRETURN) {
753                         error = ffs_read_hole(uio, xfersize, &size);
754                         if (error == 0)
755                                 continue;
756                 }
757                 if (error != 0) {
758                         brelse(bp);
759                         bp = NULL;
760                         break;
761                 }
762
763                 /*
764                  * We should only get non-zero b_resid when an I/O error
765                  * has occurred, which should cause us to break above.
766                  * However, if the short read did not cause an error,
767                  * then we want to ensure that we do not uiomove bad
768                  * or uninitialized data.
769                  */
770                 size -= bp->b_resid;
771                 if (size < xfersize) {
772                         if (size == 0)
773                                 break;
774                         xfersize = size;
775                 }
776
777                 if (buf_mapped(bp)) {
778                         error = vn_io_fault_uiomove((char *)bp->b_data +
779                             blkoffset, (int)xfersize, uio);
780                 } else {
781                         error = vn_io_fault_pgmove(bp->b_pages, blkoffset,
782                             (int)xfersize, uio);
783                 }
784                 if (error)
785                         break;
786
787                 vfs_bio_brelse(bp, ioflag);
788         }
789
790         /*
791          * This can only happen in the case of an error
792          * because the loop above resets bp to NULL on each iteration
793          * and on normal completion has not set a new value into it.
794          * so it must have come from a 'break' statement
795          */
796         if (bp != NULL)
797                 vfs_bio_brelse(bp, ioflag);
798
799         if ((error == 0 || uio->uio_resid != orig_resid) &&
800             (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
801                 UFS_INODE_SET_FLAG_SHARED(ip, IN_ACCESS);
802         return (error);
803 }
804
805 /*
806  * Vnode op for writing.
807  */
808 static int
809 ffs_write(ap)
810         struct vop_write_args /* {
811                 struct vnode *a_vp;
812                 struct uio *a_uio;
813                 int a_ioflag;
814                 struct ucred *a_cred;
815         } */ *ap;
816 {
817         struct vnode *vp;
818         struct uio *uio;
819         struct inode *ip;
820         struct fs *fs;
821         struct buf *bp;
822         ufs_lbn_t lbn;
823         off_t osize;
824         ssize_t resid;
825         int seqcount;
826         int blkoffset, error, flags, ioflag, size, xfersize;
827
828         vp = ap->a_vp;
829         uio = ap->a_uio;
830         ioflag = ap->a_ioflag;
831         if (ap->a_ioflag & IO_EXT)
832 #ifdef notyet
833                 return (ffs_extwrite(vp, uio, ioflag, ap->a_cred));
834 #else
835                 panic("ffs_write+IO_EXT");
836 #endif
837
838         seqcount = ap->a_ioflag >> IO_SEQSHIFT;
839         ip = VTOI(vp);
840
841 #ifdef INVARIANTS
842         if (uio->uio_rw != UIO_WRITE)
843                 panic("ffs_write: mode");
844 #endif
845
846         switch (vp->v_type) {
847         case VREG:
848                 if (ioflag & IO_APPEND)
849                         uio->uio_offset = ip->i_size;
850                 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
851                         return (EPERM);
852                 /* FALLTHROUGH */
853         case VLNK:
854                 break;
855         case VDIR:
856                 panic("ffs_write: dir write");
857                 break;
858         default:
859                 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
860                         (int)uio->uio_offset,
861                         (int)uio->uio_resid
862                 );
863         }
864
865         KASSERT(uio->uio_resid >= 0, ("ffs_write: uio->uio_resid < 0"));
866         KASSERT(uio->uio_offset >= 0, ("ffs_write: uio->uio_offset < 0"));
867         fs = ITOFS(ip);
868         if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize)
869                 return (EFBIG);
870         /*
871          * Maybe this should be above the vnode op call, but so long as
872          * file servers have no limits, I don't think it matters.
873          */
874         if (vn_rlimit_fsize(vp, uio, uio->uio_td))
875                 return (EFBIG);
876
877         resid = uio->uio_resid;
878         osize = ip->i_size;
879         if (seqcount > BA_SEQMAX)
880                 flags = BA_SEQMAX << BA_SEQSHIFT;
881         else
882                 flags = seqcount << BA_SEQSHIFT;
883         if (ioflag & IO_SYNC)
884                 flags |= IO_SYNC;
885         flags |= BA_UNMAPPED;
886
887         for (error = 0; uio->uio_resid > 0;) {
888                 lbn = lblkno(fs, uio->uio_offset);
889                 blkoffset = blkoff(fs, uio->uio_offset);
890                 xfersize = fs->fs_bsize - blkoffset;
891                 if (uio->uio_resid < xfersize)
892                         xfersize = uio->uio_resid;
893                 if (uio->uio_offset + xfersize > ip->i_size)
894                         vnode_pager_setsize(vp, uio->uio_offset + xfersize);
895
896                 /*
897                  * We must perform a read-before-write if the transfer size
898                  * does not cover the entire buffer.
899                  */
900                 if (fs->fs_bsize > xfersize)
901                         flags |= BA_CLRBUF;
902                 else
903                         flags &= ~BA_CLRBUF;
904 /* XXX is uio->uio_offset the right thing here? */
905                 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
906                     ap->a_cred, flags, &bp);
907                 if (error != 0) {
908                         vnode_pager_setsize(vp, ip->i_size);
909                         break;
910                 }
911                 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
912                         bp->b_flags |= B_NOCACHE;
913
914                 if (uio->uio_offset + xfersize > ip->i_size) {
915                         ip->i_size = uio->uio_offset + xfersize;
916                         DIP_SET(ip, i_size, ip->i_size);
917                         UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
918                 }
919
920                 size = blksize(fs, ip, lbn) - bp->b_resid;
921                 if (size < xfersize)
922                         xfersize = size;
923
924                 if (buf_mapped(bp)) {
925                         error = vn_io_fault_uiomove((char *)bp->b_data +
926                             blkoffset, (int)xfersize, uio);
927                 } else {
928                         error = vn_io_fault_pgmove(bp->b_pages, blkoffset,
929                             (int)xfersize, uio);
930                 }
931                 /*
932                  * If the buffer is not already filled and we encounter an
933                  * error while trying to fill it, we have to clear out any
934                  * garbage data from the pages instantiated for the buffer.
935                  * If we do not, a failed uiomove() during a write can leave
936                  * the prior contents of the pages exposed to a userland mmap.
937                  *
938                  * Note that we need only clear buffers with a transfer size
939                  * equal to the block size because buffers with a shorter
940                  * transfer size were cleared above by the call to UFS_BALLOC()
941                  * with the BA_CLRBUF flag set.
942                  *
943                  * If the source region for uiomove identically mmaps the
944                  * buffer, uiomove() performed the NOP copy, and the buffer
945                  * content remains valid because the page fault handler
946                  * validated the pages.
947                  */
948                 if (error != 0 && (bp->b_flags & B_CACHE) == 0 &&
949                     fs->fs_bsize == xfersize)
950                         vfs_bio_clrbuf(bp);
951
952                 vfs_bio_set_flags(bp, ioflag);
953
954                 /*
955                  * If IO_SYNC each buffer is written synchronously.  Otherwise
956                  * if we have a severe page deficiency write the buffer
957                  * asynchronously.  Otherwise try to cluster, and if that
958                  * doesn't do it then either do an async write (if O_DIRECT),
959                  * or a delayed write (if not).
960                  */
961                 if (ioflag & IO_SYNC) {
962                         (void)bwrite(bp);
963                 } else if (vm_page_count_severe() ||
964                             buf_dirty_count_severe() ||
965                             (ioflag & IO_ASYNC)) {
966                         bp->b_flags |= B_CLUSTEROK;
967                         bawrite(bp);
968                 } else if (xfersize + blkoffset == fs->fs_bsize) {
969                         if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
970                                 bp->b_flags |= B_CLUSTEROK;
971                                 cluster_write(vp, bp, ip->i_size, seqcount,
972                                     GB_UNMAPPED);
973                         } else {
974                                 bawrite(bp);
975                         }
976                 } else if (ioflag & IO_DIRECT) {
977                         bp->b_flags |= B_CLUSTEROK;
978                         bawrite(bp);
979                 } else {
980                         bp->b_flags |= B_CLUSTEROK;
981                         bdwrite(bp);
982                 }
983                 if (error || xfersize == 0)
984                         break;
985                 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
986         }
987         /*
988          * If we successfully wrote any data, and we are not the superuser
989          * we clear the setuid and setgid bits as a precaution against
990          * tampering.
991          */
992         if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
993             ap->a_cred) {
994                 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID)) {
995                         vn_seqc_write_begin(vp);
996                         UFS_INODE_SET_MODE(ip, ip->i_mode & ~(ISUID | ISGID));
997                         DIP_SET(ip, i_mode, ip->i_mode);
998                         vn_seqc_write_end(vp);
999                 }
1000         }
1001         if (error) {
1002                 if (ioflag & IO_UNIT) {
1003                         (void)ffs_truncate(vp, osize,
1004                             IO_NORMAL | (ioflag & IO_SYNC), ap->a_cred);
1005                         uio->uio_offset -= resid - uio->uio_resid;
1006                         uio->uio_resid = resid;
1007                 }
1008         } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) {
1009                 if (!(ioflag & IO_DATASYNC) ||
1010                     (ip->i_flags & (IN_SIZEMOD | IN_IBLKDATA)))
1011                         error = ffs_update(vp, 1);
1012                 if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), error))
1013                         error = ENXIO;
1014         }
1015         return (error);
1016 }
1017
1018 /*
1019  * Extended attribute area reading.
1020  */
1021 static int
1022 ffs_extread(struct vnode *vp, struct uio *uio, int ioflag)
1023 {
1024         struct inode *ip;
1025         struct ufs2_dinode *dp;
1026         struct fs *fs;
1027         struct buf *bp;
1028         ufs_lbn_t lbn, nextlbn;
1029         off_t bytesinfile;
1030         long size, xfersize, blkoffset;
1031         ssize_t orig_resid;
1032         int error;
1033
1034         ip = VTOI(vp);
1035         fs = ITOFS(ip);
1036         dp = ip->i_din2;
1037
1038 #ifdef INVARIANTS
1039         if (uio->uio_rw != UIO_READ || fs->fs_magic != FS_UFS2_MAGIC)
1040                 panic("ffs_extread: mode");
1041
1042 #endif
1043         orig_resid = uio->uio_resid;
1044         KASSERT(orig_resid >= 0, ("ffs_extread: uio->uio_resid < 0"));
1045         if (orig_resid == 0)
1046                 return (0);
1047         KASSERT(uio->uio_offset >= 0, ("ffs_extread: uio->uio_offset < 0"));
1048
1049         for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
1050                 if ((bytesinfile = dp->di_extsize - uio->uio_offset) <= 0)
1051                         break;
1052                 lbn = lblkno(fs, uio->uio_offset);
1053                 nextlbn = lbn + 1;
1054
1055                 /*
1056                  * size of buffer.  The buffer representing the
1057                  * end of the file is rounded up to the size of
1058                  * the block type ( fragment or full block,
1059                  * depending ).
1060                  */
1061                 size = sblksize(fs, dp->di_extsize, lbn);
1062                 blkoffset = blkoff(fs, uio->uio_offset);
1063
1064                 /*
1065                  * The amount we want to transfer in this iteration is
1066                  * one FS block less the amount of the data before
1067                  * our startpoint (duh!)
1068                  */
1069                 xfersize = fs->fs_bsize - blkoffset;
1070
1071                 /*
1072                  * But if we actually want less than the block,
1073                  * or the file doesn't have a whole block more of data,
1074                  * then use the lesser number.
1075                  */
1076                 if (uio->uio_resid < xfersize)
1077                         xfersize = uio->uio_resid;
1078                 if (bytesinfile < xfersize)
1079                         xfersize = bytesinfile;
1080
1081                 if (lblktosize(fs, nextlbn) >= dp->di_extsize) {
1082                         /*
1083                          * Don't do readahead if this is the end of the info.
1084                          */
1085                         error = bread(vp, -1 - lbn, size, NOCRED, &bp);
1086                 } else {
1087                         /*
1088                          * If we have a second block, then
1089                          * fire off a request for a readahead
1090                          * as well as a read. Note that the 4th and 5th
1091                          * arguments point to arrays of the size specified in
1092                          * the 6th argument.
1093                          */
1094                         u_int nextsize = sblksize(fs, dp->di_extsize, nextlbn);
1095
1096                         nextlbn = -1 - nextlbn;
1097                         error = breadn(vp, -1 - lbn,
1098                             size, &nextlbn, &nextsize, 1, NOCRED, &bp);
1099                 }
1100                 if (error) {
1101                         brelse(bp);
1102                         bp = NULL;
1103                         break;
1104                 }
1105
1106                 /*
1107                  * We should only get non-zero b_resid when an I/O error
1108                  * has occurred, which should cause us to break above.
1109                  * However, if the short read did not cause an error,
1110                  * then we want to ensure that we do not uiomove bad
1111                  * or uninitialized data.
1112                  */
1113                 size -= bp->b_resid;
1114                 if (size < xfersize) {
1115                         if (size == 0)
1116                                 break;
1117                         xfersize = size;
1118                 }
1119
1120                 error = uiomove((char *)bp->b_data + blkoffset,
1121                                         (int)xfersize, uio);
1122                 if (error)
1123                         break;
1124                 vfs_bio_brelse(bp, ioflag);
1125         }
1126
1127         /*
1128          * This can only happen in the case of an error
1129          * because the loop above resets bp to NULL on each iteration
1130          * and on normal completion has not set a new value into it.
1131          * so it must have come from a 'break' statement
1132          */
1133         if (bp != NULL)
1134                 vfs_bio_brelse(bp, ioflag);
1135         return (error);
1136 }
1137
1138 /*
1139  * Extended attribute area writing.
1140  */
1141 static int
1142 ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *ucred)
1143 {
1144         struct inode *ip;
1145         struct ufs2_dinode *dp;
1146         struct fs *fs;
1147         struct buf *bp;
1148         ufs_lbn_t lbn;
1149         off_t osize;
1150         ssize_t resid;
1151         int blkoffset, error, flags, size, xfersize;
1152
1153         ip = VTOI(vp);
1154         fs = ITOFS(ip);
1155         dp = ip->i_din2;
1156
1157 #ifdef INVARIANTS
1158         if (uio->uio_rw != UIO_WRITE || fs->fs_magic != FS_UFS2_MAGIC)
1159                 panic("ffs_extwrite: mode");
1160 #endif
1161
1162         if (ioflag & IO_APPEND)
1163                 uio->uio_offset = dp->di_extsize;
1164         KASSERT(uio->uio_offset >= 0, ("ffs_extwrite: uio->uio_offset < 0"));
1165         KASSERT(uio->uio_resid >= 0, ("ffs_extwrite: uio->uio_resid < 0"));
1166         if ((uoff_t)uio->uio_offset + uio->uio_resid >
1167             UFS_NXADDR * fs->fs_bsize)
1168                 return (EFBIG);
1169
1170         resid = uio->uio_resid;
1171         osize = dp->di_extsize;
1172         flags = IO_EXT;
1173         if (ioflag & IO_SYNC)
1174                 flags |= IO_SYNC;
1175
1176         for (error = 0; uio->uio_resid > 0;) {
1177                 lbn = lblkno(fs, uio->uio_offset);
1178                 blkoffset = blkoff(fs, uio->uio_offset);
1179                 xfersize = fs->fs_bsize - blkoffset;
1180                 if (uio->uio_resid < xfersize)
1181                         xfersize = uio->uio_resid;
1182
1183                 /*
1184                  * We must perform a read-before-write if the transfer size
1185                  * does not cover the entire buffer.
1186                  */
1187                 if (fs->fs_bsize > xfersize)
1188                         flags |= BA_CLRBUF;
1189                 else
1190                         flags &= ~BA_CLRBUF;
1191                 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
1192                     ucred, flags, &bp);
1193                 if (error != 0)
1194                         break;
1195                 /*
1196                  * If the buffer is not valid we have to clear out any
1197                  * garbage data from the pages instantiated for the buffer.
1198                  * If we do not, a failed uiomove() during a write can leave
1199                  * the prior contents of the pages exposed to a userland
1200                  * mmap().  XXX deal with uiomove() errors a better way.
1201                  */
1202                 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
1203                         vfs_bio_clrbuf(bp);
1204
1205                 if (uio->uio_offset + xfersize > dp->di_extsize) {
1206                         dp->di_extsize = uio->uio_offset + xfersize;
1207                         UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
1208                 }
1209
1210                 size = sblksize(fs, dp->di_extsize, lbn) - bp->b_resid;
1211                 if (size < xfersize)
1212                         xfersize = size;
1213
1214                 error =
1215                     uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
1216
1217                 vfs_bio_set_flags(bp, ioflag);
1218
1219                 /*
1220                  * If IO_SYNC each buffer is written synchronously.  Otherwise
1221                  * if we have a severe page deficiency write the buffer
1222                  * asynchronously.  Otherwise try to cluster, and if that
1223                  * doesn't do it then either do an async write (if O_DIRECT),
1224                  * or a delayed write (if not).
1225                  */
1226                 if (ioflag & IO_SYNC) {
1227                         (void)bwrite(bp);
1228                 } else if (vm_page_count_severe() ||
1229                             buf_dirty_count_severe() ||
1230                             xfersize + blkoffset == fs->fs_bsize ||
1231                             (ioflag & (IO_ASYNC | IO_DIRECT)))
1232                         bawrite(bp);
1233                 else
1234                         bdwrite(bp);
1235                 if (error || xfersize == 0)
1236                         break;
1237                 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1238         }
1239         /*
1240          * If we successfully wrote any data, and we are not the superuser
1241          * we clear the setuid and setgid bits as a precaution against
1242          * tampering.
1243          */
1244         if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid && ucred) {
1245                 if (priv_check_cred(ucred, PRIV_VFS_RETAINSUGID)) {
1246                         vn_seqc_write_begin(vp);
1247                         UFS_INODE_SET_MODE(ip, ip->i_mode & ~(ISUID | ISGID));
1248                         dp->di_mode = ip->i_mode;
1249                         vn_seqc_write_end(vp);
1250                 }
1251         }
1252         if (error) {
1253                 if (ioflag & IO_UNIT) {
1254                         (void)ffs_truncate(vp, osize,
1255                             IO_EXT | (ioflag&IO_SYNC), ucred);
1256                         uio->uio_offset -= resid - uio->uio_resid;
1257                         uio->uio_resid = resid;
1258                 }
1259         } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
1260                 error = ffs_update(vp, 1);
1261         return (error);
1262 }
1263
1264 /*
1265  * Vnode operating to retrieve a named extended attribute.
1266  *
1267  * Locate a particular EA (nspace:name) in the area (ptr:length), and return
1268  * the length of the EA, and possibly the pointer to the entry and to the data.
1269  */
1270 static int
1271 ffs_findextattr(u_char *ptr, u_int length, int nspace, const char *name,
1272     struct extattr **eapp, u_char **eac)
1273 {
1274         struct extattr *eap, *eaend;
1275         size_t nlen;
1276
1277         nlen = strlen(name);
1278         KASSERT(ALIGNED_TO(ptr, struct extattr), ("unaligned"));
1279         eap = (struct extattr *)ptr;
1280         eaend = (struct extattr *)(ptr + length);
1281         for (; eap < eaend; eap = EXTATTR_NEXT(eap)) {
1282                 KASSERT(EXTATTR_NEXT(eap) <= eaend,
1283                     ("extattr next %p beyond %p", EXTATTR_NEXT(eap), eaend));
1284                 if (eap->ea_namespace != nspace || eap->ea_namelength != nlen
1285                     || memcmp(eap->ea_name, name, nlen) != 0)
1286                         continue;
1287                 if (eapp != NULL)
1288                         *eapp = eap;
1289                 if (eac != NULL)
1290                         *eac = EXTATTR_CONTENT(eap);
1291                 return (EXTATTR_CONTENT_SIZE(eap));
1292         }
1293         return (-1);
1294 }
1295
1296 static int
1297 ffs_rdextattr(u_char **p, struct vnode *vp, struct thread *td)
1298 {
1299         const struct extattr *eap, *eaend, *eapnext;
1300         struct inode *ip;
1301         struct ufs2_dinode *dp;
1302         struct fs *fs;
1303         struct uio luio;
1304         struct iovec liovec;
1305         u_int easize;
1306         int error;
1307         u_char *eae;
1308
1309         ip = VTOI(vp);
1310         fs = ITOFS(ip);
1311         dp = ip->i_din2;
1312         easize = dp->di_extsize;
1313         if ((uoff_t)easize > UFS_NXADDR * fs->fs_bsize)
1314                 return (EFBIG);
1315
1316         eae = malloc(easize, M_TEMP, M_WAITOK);
1317
1318         liovec.iov_base = eae;
1319         liovec.iov_len = easize;
1320         luio.uio_iov = &liovec;
1321         luio.uio_iovcnt = 1;
1322         luio.uio_offset = 0;
1323         luio.uio_resid = easize;
1324         luio.uio_segflg = UIO_SYSSPACE;
1325         luio.uio_rw = UIO_READ;
1326         luio.uio_td = td;
1327
1328         error = ffs_extread(vp, &luio, IO_EXT | IO_SYNC);
1329         if (error) {
1330                 free(eae, M_TEMP);
1331                 return (error);
1332         }
1333         /* Validate disk xattrfile contents. */
1334         for (eap = (void *)eae, eaend = (void *)(eae + easize); eap < eaend;
1335             eap = eapnext) {
1336                 eapnext = EXTATTR_NEXT(eap);
1337                 /* Bogusly short entry or bogusly long entry. */
1338                 if (eap->ea_length < sizeof(*eap) || eapnext > eaend) {
1339                         free(eae, M_TEMP);
1340                         return (EINTEGRITY);
1341                 }
1342         }
1343         *p = eae;
1344         return (0);
1345 }
1346
1347 static void
1348 ffs_lock_ea(struct vnode *vp)
1349 {
1350         struct inode *ip;
1351
1352         ip = VTOI(vp);
1353         VI_LOCK(vp);
1354         while (ip->i_flag & IN_EA_LOCKED) {
1355                 UFS_INODE_SET_FLAG(ip, IN_EA_LOCKWAIT);
1356                 msleep(&ip->i_ea_refs, &vp->v_interlock, PINOD + 2, "ufs_ea",
1357                     0);
1358         }
1359         UFS_INODE_SET_FLAG(ip, IN_EA_LOCKED);
1360         VI_UNLOCK(vp);
1361 }
1362
1363 static void
1364 ffs_unlock_ea(struct vnode *vp)
1365 {
1366         struct inode *ip;
1367
1368         ip = VTOI(vp);
1369         VI_LOCK(vp);
1370         if (ip->i_flag & IN_EA_LOCKWAIT)
1371                 wakeup(&ip->i_ea_refs);
1372         ip->i_flag &= ~(IN_EA_LOCKED | IN_EA_LOCKWAIT);
1373         VI_UNLOCK(vp);
1374 }
1375
1376 static int
1377 ffs_open_ea(struct vnode *vp, struct ucred *cred, struct thread *td)
1378 {
1379         struct inode *ip;
1380         struct ufs2_dinode *dp;
1381         int error;
1382
1383         ip = VTOI(vp);
1384
1385         ffs_lock_ea(vp);
1386         if (ip->i_ea_area != NULL) {
1387                 ip->i_ea_refs++;
1388                 ffs_unlock_ea(vp);
1389                 return (0);
1390         }
1391         dp = ip->i_din2;
1392         error = ffs_rdextattr(&ip->i_ea_area, vp, td);
1393         if (error) {
1394                 ffs_unlock_ea(vp);
1395                 return (error);
1396         }
1397         ip->i_ea_len = dp->di_extsize;
1398         ip->i_ea_error = 0;
1399         ip->i_ea_refs++;
1400         ffs_unlock_ea(vp);
1401         return (0);
1402 }
1403
1404 /*
1405  * Vnode extattr transaction commit/abort
1406  */
1407 static int
1408 ffs_close_ea(struct vnode *vp, int commit, struct ucred *cred, struct thread *td)
1409 {
1410         struct inode *ip;
1411         struct uio luio;
1412         struct iovec liovec;
1413         int error;
1414         struct ufs2_dinode *dp;
1415
1416         ip = VTOI(vp);
1417
1418         ffs_lock_ea(vp);
1419         if (ip->i_ea_area == NULL) {
1420                 ffs_unlock_ea(vp);
1421                 return (EINVAL);
1422         }
1423         dp = ip->i_din2;
1424         error = ip->i_ea_error;
1425         if (commit && error == 0) {
1426                 ASSERT_VOP_ELOCKED(vp, "ffs_close_ea commit");
1427                 if (cred == NOCRED)
1428                         cred =  vp->v_mount->mnt_cred;
1429                 liovec.iov_base = ip->i_ea_area;
1430                 liovec.iov_len = ip->i_ea_len;
1431                 luio.uio_iov = &liovec;
1432                 luio.uio_iovcnt = 1;
1433                 luio.uio_offset = 0;
1434                 luio.uio_resid = ip->i_ea_len;
1435                 luio.uio_segflg = UIO_SYSSPACE;
1436                 luio.uio_rw = UIO_WRITE;
1437                 luio.uio_td = td;
1438                 /* XXX: I'm not happy about truncating to zero size */
1439                 if (ip->i_ea_len < dp->di_extsize)
1440                         error = ffs_truncate(vp, 0, IO_EXT, cred);
1441                 error = ffs_extwrite(vp, &luio, IO_EXT | IO_SYNC, cred);
1442         }
1443         if (--ip->i_ea_refs == 0) {
1444                 free(ip->i_ea_area, M_TEMP);
1445                 ip->i_ea_area = NULL;
1446                 ip->i_ea_len = 0;
1447                 ip->i_ea_error = 0;
1448         }
1449         ffs_unlock_ea(vp);
1450         return (error);
1451 }
1452
1453 /*
1454  * Vnode extattr strategy routine for fifos.
1455  *
1456  * We need to check for a read or write of the external attributes.
1457  * Otherwise we just fall through and do the usual thing.
1458  */
1459 static int
1460 ffsext_strategy(struct vop_strategy_args *ap)
1461 /*
1462 struct vop_strategy_args {
1463         struct vnodeop_desc *a_desc;
1464         struct vnode *a_vp;
1465         struct buf *a_bp;
1466 };
1467 */
1468 {
1469         struct vnode *vp;
1470         daddr_t lbn;
1471
1472         vp = ap->a_vp;
1473         lbn = ap->a_bp->b_lblkno;
1474         if (I_IS_UFS2(VTOI(vp)) && lbn < 0 && lbn >= -UFS_NXADDR)
1475                 return (VOP_STRATEGY_APV(&ufs_vnodeops, ap));
1476         if (vp->v_type == VFIFO)
1477                 return (VOP_STRATEGY_APV(&ufs_fifoops, ap));
1478         panic("spec nodes went here");
1479 }
1480
1481 /*
1482  * Vnode extattr transaction commit/abort
1483  */
1484 static int
1485 ffs_openextattr(struct vop_openextattr_args *ap)
1486 /*
1487 struct vop_openextattr_args {
1488         struct vnodeop_desc *a_desc;
1489         struct vnode *a_vp;
1490         IN struct ucred *a_cred;
1491         IN struct thread *a_td;
1492 };
1493 */
1494 {
1495
1496         if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1497                 return (EOPNOTSUPP);
1498
1499         return (ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td));
1500 }
1501
1502 /*
1503  * Vnode extattr transaction commit/abort
1504  */
1505 static int
1506 ffs_closeextattr(struct vop_closeextattr_args *ap)
1507 /*
1508 struct vop_closeextattr_args {
1509         struct vnodeop_desc *a_desc;
1510         struct vnode *a_vp;
1511         int a_commit;
1512         IN struct ucred *a_cred;
1513         IN struct thread *a_td;
1514 };
1515 */
1516 {
1517
1518         if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1519                 return (EOPNOTSUPP);
1520
1521         if (ap->a_commit && (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY))
1522                 return (EROFS);
1523
1524         return (ffs_close_ea(ap->a_vp, ap->a_commit, ap->a_cred, ap->a_td));
1525 }
1526
1527 /*
1528  * Vnode operation to remove a named attribute.
1529  */
1530 static int
1531 ffs_deleteextattr(struct vop_deleteextattr_args *ap)
1532 /*
1533 vop_deleteextattr {
1534         IN struct vnode *a_vp;
1535         IN int a_attrnamespace;
1536         IN const char *a_name;
1537         IN struct ucred *a_cred;
1538         IN struct thread *a_td;
1539 };
1540 */
1541 {
1542         struct inode *ip;
1543         struct extattr *eap;
1544         uint32_t ul;
1545         int olen, error, i, easize;
1546         u_char *eae;
1547         void *tmp;
1548
1549         ip = VTOI(ap->a_vp);
1550
1551         if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1552                 return (EOPNOTSUPP);
1553
1554         if (strlen(ap->a_name) == 0)
1555                 return (EINVAL);
1556
1557         if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1558                 return (EROFS);
1559
1560         error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1561             ap->a_cred, ap->a_td, VWRITE);
1562         if (error) {
1563                 /*
1564                  * ffs_lock_ea is not needed there, because the vnode
1565                  * must be exclusively locked.
1566                  */
1567                 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1568                         ip->i_ea_error = error;
1569                 return (error);
1570         }
1571
1572         error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1573         if (error)
1574                 return (error);
1575
1576         /* CEM: delete could be done in-place instead */
1577         eae = malloc(ip->i_ea_len, M_TEMP, M_WAITOK);
1578         bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1579         easize = ip->i_ea_len;
1580
1581         olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1582             &eap, NULL);
1583         if (olen == -1) {
1584                 /* delete but nonexistent */
1585                 free(eae, M_TEMP);
1586                 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1587                 return (ENOATTR);
1588         }
1589         ul = eap->ea_length;
1590         i = (u_char *)EXTATTR_NEXT(eap) - eae;
1591         bcopy(EXTATTR_NEXT(eap), eap, easize - i);
1592         easize -= ul;
1593
1594         tmp = ip->i_ea_area;
1595         ip->i_ea_area = eae;
1596         ip->i_ea_len = easize;
1597         free(tmp, M_TEMP);
1598         error = ffs_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td);
1599         return (error);
1600 }
1601
1602 /*
1603  * Vnode operation to retrieve a named extended attribute.
1604  */
1605 static int
1606 ffs_getextattr(struct vop_getextattr_args *ap)
1607 /*
1608 vop_getextattr {
1609         IN struct vnode *a_vp;
1610         IN int a_attrnamespace;
1611         IN const char *a_name;
1612         INOUT struct uio *a_uio;
1613         OUT size_t *a_size;
1614         IN struct ucred *a_cred;
1615         IN struct thread *a_td;
1616 };
1617 */
1618 {
1619         struct inode *ip;
1620         u_char *eae, *p;
1621         unsigned easize;
1622         int error, ealen;
1623
1624         ip = VTOI(ap->a_vp);
1625
1626         if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1627                 return (EOPNOTSUPP);
1628
1629         error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1630             ap->a_cred, ap->a_td, VREAD);
1631         if (error)
1632                 return (error);
1633
1634         error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1635         if (error)
1636                 return (error);
1637
1638         eae = ip->i_ea_area;
1639         easize = ip->i_ea_len;
1640
1641         ealen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1642             NULL, &p);
1643         if (ealen >= 0) {
1644                 error = 0;
1645                 if (ap->a_size != NULL)
1646                         *ap->a_size = ealen;
1647                 else if (ap->a_uio != NULL)
1648                         error = uiomove(p, ealen, ap->a_uio);
1649         } else
1650                 error = ENOATTR;
1651
1652         ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1653         return (error);
1654 }
1655
1656 /*
1657  * Vnode operation to retrieve extended attributes on a vnode.
1658  */
1659 static int
1660 ffs_listextattr(struct vop_listextattr_args *ap)
1661 /*
1662 vop_listextattr {
1663         IN struct vnode *a_vp;
1664         IN int a_attrnamespace;
1665         INOUT struct uio *a_uio;
1666         OUT size_t *a_size;
1667         IN struct ucred *a_cred;
1668         IN struct thread *a_td;
1669 };
1670 */
1671 {
1672         struct inode *ip;
1673         struct extattr *eap, *eaend;
1674         int error, ealen;
1675
1676         ip = VTOI(ap->a_vp);
1677
1678         if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1679                 return (EOPNOTSUPP);
1680
1681         error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1682             ap->a_cred, ap->a_td, VREAD);
1683         if (error)
1684                 return (error);
1685
1686         error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1687         if (error)
1688                 return (error);
1689
1690         error = 0;
1691         if (ap->a_size != NULL)
1692                 *ap->a_size = 0;
1693
1694         KASSERT(ALIGNED_TO(ip->i_ea_area, struct extattr), ("unaligned"));
1695         eap = (struct extattr *)ip->i_ea_area;
1696         eaend = (struct extattr *)(ip->i_ea_area + ip->i_ea_len);
1697         for (; error == 0 && eap < eaend; eap = EXTATTR_NEXT(eap)) {
1698                 KASSERT(EXTATTR_NEXT(eap) <= eaend,
1699                     ("extattr next %p beyond %p", EXTATTR_NEXT(eap), eaend));
1700                 if (eap->ea_namespace != ap->a_attrnamespace)
1701                         continue;
1702
1703                 ealen = eap->ea_namelength;
1704                 if (ap->a_size != NULL)
1705                         *ap->a_size += ealen + 1;
1706                 else if (ap->a_uio != NULL)
1707                         error = uiomove(&eap->ea_namelength, ealen + 1,
1708                             ap->a_uio);
1709         }
1710
1711         ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1712         return (error);
1713 }
1714
1715 /*
1716  * Vnode operation to set a named attribute.
1717  */
1718 static int
1719 ffs_setextattr(struct vop_setextattr_args *ap)
1720 /*
1721 vop_setextattr {
1722         IN struct vnode *a_vp;
1723         IN int a_attrnamespace;
1724         IN const char *a_name;
1725         INOUT struct uio *a_uio;
1726         IN struct ucred *a_cred;
1727         IN struct thread *a_td;
1728 };
1729 */
1730 {
1731         struct inode *ip;
1732         struct fs *fs;
1733         struct extattr *eap;
1734         uint32_t ealength, ul;
1735         ssize_t ealen;
1736         int olen, eapad1, eapad2, error, i, easize;
1737         u_char *eae;
1738         void *tmp;
1739
1740         ip = VTOI(ap->a_vp);
1741         fs = ITOFS(ip);
1742
1743         if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1744                 return (EOPNOTSUPP);
1745
1746         if (strlen(ap->a_name) == 0)
1747                 return (EINVAL);
1748
1749         /* XXX Now unsupported API to delete EAs using NULL uio. */
1750         if (ap->a_uio == NULL)
1751                 return (EOPNOTSUPP);
1752
1753         if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1754                 return (EROFS);
1755
1756         ealen = ap->a_uio->uio_resid;
1757         if (ealen < 0 || ealen > lblktosize(fs, UFS_NXADDR))
1758                 return (EINVAL);
1759
1760         error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1761             ap->a_cred, ap->a_td, VWRITE);
1762         if (error) {
1763                 /*
1764                  * ffs_lock_ea is not needed there, because the vnode
1765                  * must be exclusively locked.
1766                  */
1767                 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1768                         ip->i_ea_error = error;
1769                 return (error);
1770         }
1771
1772         error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1773         if (error)
1774                 return (error);
1775
1776         ealength = sizeof(uint32_t) + 3 + strlen(ap->a_name);
1777         eapad1 = roundup2(ealength, 8) - ealength;
1778         eapad2 = roundup2(ealen, 8) - ealen;
1779         ealength += eapad1 + ealen + eapad2;
1780
1781         /*
1782          * CEM: rewrites of the same size or smaller could be done in-place
1783          * instead.  (We don't acquire any fine-grained locks in here either,
1784          * so we could also do bigger writes in-place.)
1785          */
1786         eae = malloc(ip->i_ea_len + ealength, M_TEMP, M_WAITOK);
1787         bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1788         easize = ip->i_ea_len;
1789
1790         olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1791             &eap, NULL);
1792         if (olen == -1) {
1793                 /* new, append at end */
1794                 KASSERT(ALIGNED_TO(eae + easize, struct extattr),
1795                     ("unaligned"));
1796                 eap = (struct extattr *)(eae + easize);
1797                 easize += ealength;
1798         } else {
1799                 ul = eap->ea_length;
1800                 i = (u_char *)EXTATTR_NEXT(eap) - eae;
1801                 if (ul != ealength) {
1802                         bcopy(EXTATTR_NEXT(eap), (u_char *)eap + ealength,
1803                             easize - i);
1804                         easize += (ealength - ul);
1805                 }
1806         }
1807         if (easize > lblktosize(fs, UFS_NXADDR)) {
1808                 free(eae, M_TEMP);
1809                 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1810                 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1811                         ip->i_ea_error = ENOSPC;
1812                 return (ENOSPC);
1813         }
1814         eap->ea_length = ealength;
1815         eap->ea_namespace = ap->a_attrnamespace;
1816         eap->ea_contentpadlen = eapad2;
1817         eap->ea_namelength = strlen(ap->a_name);
1818         memcpy(eap->ea_name, ap->a_name, strlen(ap->a_name));
1819         bzero(&eap->ea_name[strlen(ap->a_name)], eapad1);
1820         error = uiomove(EXTATTR_CONTENT(eap), ealen, ap->a_uio);
1821         if (error) {
1822                 free(eae, M_TEMP);
1823                 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1824                 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1825                         ip->i_ea_error = error;
1826                 return (error);
1827         }
1828         bzero((u_char *)EXTATTR_CONTENT(eap) + ealen, eapad2);
1829
1830         tmp = ip->i_ea_area;
1831         ip->i_ea_area = eae;
1832         ip->i_ea_len = easize;
1833         free(tmp, M_TEMP);
1834         error = ffs_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td);
1835         return (error);
1836 }
1837
1838 /*
1839  * Vnode pointer to File handle
1840  */
1841 static int
1842 ffs_vptofh(struct vop_vptofh_args *ap)
1843 /*
1844 vop_vptofh {
1845         IN struct vnode *a_vp;
1846         IN struct fid *a_fhp;
1847 };
1848 */
1849 {
1850         struct inode *ip;
1851         struct ufid *ufhp;
1852
1853         ip = VTOI(ap->a_vp);
1854         ufhp = (struct ufid *)ap->a_fhp;
1855         ufhp->ufid_len = sizeof(struct ufid);
1856         ufhp->ufid_ino = ip->i_number;
1857         ufhp->ufid_gen = ip->i_gen;
1858         return (0);
1859 }
1860
1861 SYSCTL_DECL(_vfs_ffs);
1862 static int use_buf_pager = 1;
1863 SYSCTL_INT(_vfs_ffs, OID_AUTO, use_buf_pager, CTLFLAG_RWTUN, &use_buf_pager, 0,
1864     "Always use buffer pager instead of bmap");
1865
1866 static daddr_t
1867 ffs_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
1868 {
1869
1870         return (lblkno(VFSTOUFS(vp->v_mount)->um_fs, off));
1871 }
1872
1873 static int
1874 ffs_gbp_getblksz(struct vnode *vp, daddr_t lbn)
1875 {
1876
1877         return (blksize(VFSTOUFS(vp->v_mount)->um_fs, VTOI(vp), lbn));
1878 }
1879
1880 static int
1881 ffs_getpages(struct vop_getpages_args *ap)
1882 {
1883         struct vnode *vp;
1884         struct ufsmount *um;
1885
1886         vp = ap->a_vp;
1887         um = VFSTOUFS(vp->v_mount);
1888
1889         if (!use_buf_pager && um->um_devvp->v_bufobj.bo_bsize <= PAGE_SIZE)
1890                 return (vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
1891                     ap->a_rbehind, ap->a_rahead, NULL, NULL));
1892         return (vfs_bio_getpages(vp, ap->a_m, ap->a_count, ap->a_rbehind,
1893             ap->a_rahead, ffs_gbp_getblkno, ffs_gbp_getblksz));
1894 }
1895
1896 static int
1897 ffs_getpages_async(struct vop_getpages_async_args *ap)
1898 {
1899         struct vnode *vp;
1900         struct ufsmount *um;
1901         bool do_iodone;
1902         int error;
1903
1904         vp = ap->a_vp;
1905         um = VFSTOUFS(vp->v_mount);
1906         do_iodone = true;
1907
1908         if (um->um_devvp->v_bufobj.bo_bsize <= PAGE_SIZE) {
1909                 error = vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
1910                     ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg);
1911                 if (error == 0)
1912                         do_iodone = false;
1913         } else {
1914                 error = vfs_bio_getpages(vp, ap->a_m, ap->a_count,
1915                     ap->a_rbehind, ap->a_rahead, ffs_gbp_getblkno,
1916                     ffs_gbp_getblksz);
1917         }
1918         if (do_iodone && ap->a_iodone != NULL)
1919                 ap->a_iodone(ap->a_arg, ap->a_m, ap->a_count, error);
1920
1921         return (error);
1922 }
1923
1924 static int
1925 ffs_vput_pair(struct vop_vput_pair_args *ap)
1926 {
1927         struct vnode *dvp, *vp, **vpp;
1928         struct inode *dp;
1929         int error, vp_locked;
1930
1931         dvp = ap->a_dvp;
1932         dp = VTOI(dvp);
1933         vpp = ap->a_vpp;
1934         vp = vpp != NULL ? *vpp : NULL;
1935
1936         if ((dp->i_flag & IN_NEEDSYNC) == 0) {
1937                 vput(dvp);
1938                 if (vp != NULL && ap->a_unlock_vp)
1939                         vput(vp);
1940                 return (0);
1941         }
1942
1943         if (vp != NULL) {
1944                 if (ap->a_unlock_vp) {
1945                         vput(vp);
1946                 } else {
1947                         MPASS(vp->v_type != VNON);
1948                         vp_locked = VOP_ISLOCKED(vp);
1949                         VOP_UNLOCK(vp);
1950                 }
1951         }
1952
1953         do {
1954                 error = ffs_syncvnode(dvp, MNT_WAIT, 0);
1955         } while (error == ERELOOKUP);
1956         vput(dvp);
1957
1958         if (vp == NULL || ap->a_unlock_vp)
1959                 return (0);
1960
1961         /*
1962          * It is possible that vp is reclaimed at this point. Only
1963          * routines that call us with a_unlock_vp == false can find
1964          * that their vp has been reclaimed. There are three areas
1965          * that are affected:
1966          * 1) vn_open_cred() - later VOPs could fail, but
1967          *    dead_open() returns 0 to simulate successful open.
1968          * 2) ffs_snapshot() - creation of snapshot fails with EBADF.
1969          * 3) NFS server (several places) - code is prepared to detect
1970          *    and respond to dead vnodes by returning ESTALE.
1971          */
1972         VOP_LOCK(vp, vp_locked | LK_RETRY);
1973         return (0);
1974 }