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