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