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