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