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