]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/kern/vfs_vnops.c
MFC r246832:
[FreeBSD/stable/9.git] / sys / kern / vfs_vnops.c
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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  *      @(#)vfs_vnops.c 8.2 (Berkeley) 1/21/94
35  */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/fcntl.h>
43 #include <sys/file.h>
44 #include <sys/kdb.h>
45 #include <sys/stat.h>
46 #include <sys/priv.h>
47 #include <sys/proc.h>
48 #include <sys/limits.h>
49 #include <sys/lock.h>
50 #include <sys/mount.h>
51 #include <sys/mutex.h>
52 #include <sys/namei.h>
53 #include <sys/vnode.h>
54 #include <sys/bio.h>
55 #include <sys/buf.h>
56 #include <sys/filio.h>
57 #include <sys/resourcevar.h>
58 #include <sys/sx.h>
59 #include <sys/sysctl.h>
60 #include <sys/ttycom.h>
61 #include <sys/conf.h>
62 #include <sys/syslog.h>
63 #include <sys/unistd.h>
64
65 #include <security/audit/audit.h>
66 #include <security/mac/mac_framework.h>
67
68 #include <vm/vm.h>
69 #include <vm/vm_extern.h>
70 #include <vm/pmap.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
74
75 static fo_rdwr_t        vn_read;
76 static fo_rdwr_t        vn_write;
77 static fo_rdwr_t        vn_io_fault;
78 static fo_truncate_t    vn_truncate;
79 static fo_ioctl_t       vn_ioctl;
80 static fo_poll_t        vn_poll;
81 static fo_kqfilter_t    vn_kqfilter;
82 static fo_stat_t        vn_statfile;
83 static fo_close_t       vn_closefile;
84
85 struct  fileops vnops = {
86         .fo_read = vn_io_fault,
87         .fo_write = vn_io_fault,
88         .fo_truncate = vn_truncate,
89         .fo_ioctl = vn_ioctl,
90         .fo_poll = vn_poll,
91         .fo_kqfilter = vn_kqfilter,
92         .fo_stat = vn_statfile,
93         .fo_close = vn_closefile,
94         .fo_chmod = vn_chmod,
95         .fo_chown = vn_chown,
96         .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
97 };
98
99 int
100 vn_open(ndp, flagp, cmode, fp)
101         struct nameidata *ndp;
102         int *flagp, cmode;
103         struct file *fp;
104 {
105         struct thread *td = ndp->ni_cnd.cn_thread;
106
107         return (vn_open_cred(ndp, flagp, cmode, 0, td->td_ucred, fp));
108 }
109
110 /*
111  * Common code for vnode open operations.
112  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
113  * 
114  * Note that this does NOT free nameidata for the successful case,
115  * due to the NDINIT being done elsewhere.
116  */
117 int
118 vn_open_cred(struct nameidata *ndp, int *flagp, int cmode, u_int vn_open_flags,
119     struct ucred *cred, struct file *fp)
120 {
121         struct vnode *vp;
122         struct mount *mp;
123         struct thread *td = ndp->ni_cnd.cn_thread;
124         struct vattr vat;
125         struct vattr *vap = &vat;
126         int fmode, error;
127         accmode_t accmode;
128         int vfslocked, mpsafe;
129
130         mpsafe = ndp->ni_cnd.cn_flags & MPSAFE;
131 restart:
132         vfslocked = 0;
133         fmode = *flagp;
134         if (fmode & O_CREAT) {
135                 ndp->ni_cnd.cn_nameiop = CREATE;
136                 ndp->ni_cnd.cn_flags = ISOPEN | LOCKPARENT | LOCKLEAF |
137                     MPSAFE;
138                 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
139                         ndp->ni_cnd.cn_flags |= FOLLOW;
140                 if (!(vn_open_flags & VN_OPEN_NOAUDIT))
141                         ndp->ni_cnd.cn_flags |= AUDITVNODE1;
142                 bwillwrite();
143                 if ((error = namei(ndp)) != 0)
144                         return (error);
145                 vfslocked = NDHASGIANT(ndp);
146                 if (!mpsafe)
147                         ndp->ni_cnd.cn_flags &= ~MPSAFE;
148                 if (ndp->ni_vp == NULL) {
149                         VATTR_NULL(vap);
150                         vap->va_type = VREG;
151                         vap->va_mode = cmode;
152                         if (fmode & O_EXCL)
153                                 vap->va_vaflags |= VA_EXCLUSIVE;
154                         if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
155                                 NDFREE(ndp, NDF_ONLY_PNBUF);
156                                 vput(ndp->ni_dvp);
157                                 VFS_UNLOCK_GIANT(vfslocked);
158                                 if ((error = vn_start_write(NULL, &mp,
159                                     V_XSLEEP | PCATCH)) != 0)
160                                         return (error);
161                                 goto restart;
162                         }
163 #ifdef MAC
164                         error = mac_vnode_check_create(cred, ndp->ni_dvp,
165                             &ndp->ni_cnd, vap);
166                         if (error == 0)
167 #endif
168                                 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
169                                                    &ndp->ni_cnd, vap);
170                         vput(ndp->ni_dvp);
171                         vn_finished_write(mp);
172                         if (error) {
173                                 VFS_UNLOCK_GIANT(vfslocked);
174                                 NDFREE(ndp, NDF_ONLY_PNBUF);
175                                 return (error);
176                         }
177                         fmode &= ~O_TRUNC;
178                         vp = ndp->ni_vp;
179                 } else {
180                         if (ndp->ni_dvp == ndp->ni_vp)
181                                 vrele(ndp->ni_dvp);
182                         else
183                                 vput(ndp->ni_dvp);
184                         ndp->ni_dvp = NULL;
185                         vp = ndp->ni_vp;
186                         if (fmode & O_EXCL) {
187                                 error = EEXIST;
188                                 goto bad;
189                         }
190                         fmode &= ~O_CREAT;
191                 }
192         } else {
193                 ndp->ni_cnd.cn_nameiop = LOOKUP;
194                 ndp->ni_cnd.cn_flags = ISOPEN |
195                     ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) |
196                     LOCKLEAF | MPSAFE;
197                 if (!(fmode & FWRITE))
198                         ndp->ni_cnd.cn_flags |= LOCKSHARED;
199                 if (!(vn_open_flags & VN_OPEN_NOAUDIT))
200                         ndp->ni_cnd.cn_flags |= AUDITVNODE1;
201                 if ((error = namei(ndp)) != 0)
202                         return (error);
203                 if (!mpsafe)
204                         ndp->ni_cnd.cn_flags &= ~MPSAFE;
205                 vfslocked = NDHASGIANT(ndp);
206                 vp = ndp->ni_vp;
207         }
208         if (vp->v_type == VLNK) {
209                 error = EMLINK;
210                 goto bad;
211         }
212         if (vp->v_type == VSOCK) {
213                 error = EOPNOTSUPP;
214                 goto bad;
215         }
216         if (vp->v_type != VDIR && fmode & O_DIRECTORY) {
217                 error = ENOTDIR;
218                 goto bad;
219         }
220         accmode = 0;
221         if (fmode & (FWRITE | O_TRUNC)) {
222                 if (vp->v_type == VDIR) {
223                         error = EISDIR;
224                         goto bad;
225                 }
226                 accmode |= VWRITE;
227         }
228         if (fmode & FREAD)
229                 accmode |= VREAD;
230         if (fmode & FEXEC)
231                 accmode |= VEXEC;
232         if ((fmode & O_APPEND) && (fmode & FWRITE))
233                 accmode |= VAPPEND;
234 #ifdef MAC
235         error = mac_vnode_check_open(cred, vp, accmode);
236         if (error)
237                 goto bad;
238 #endif
239         if ((fmode & O_CREAT) == 0) {
240                 if (accmode & VWRITE) {
241                         error = vn_writechk(vp);
242                         if (error)
243                                 goto bad;
244                 }
245                 if (accmode) {
246                         error = VOP_ACCESS(vp, accmode, cred, td);
247                         if (error)
248                                 goto bad;
249                 }
250         }
251         if ((error = VOP_OPEN(vp, fmode, cred, td, fp)) != 0)
252                 goto bad;
253
254         if (fmode & FWRITE)
255                 VOP_ADD_WRITECOUNT(vp, 1);
256         *flagp = fmode;
257         ASSERT_VOP_LOCKED(vp, "vn_open_cred");
258         if (!mpsafe)
259                 VFS_UNLOCK_GIANT(vfslocked);
260         return (0);
261 bad:
262         NDFREE(ndp, NDF_ONLY_PNBUF);
263         vput(vp);
264         VFS_UNLOCK_GIANT(vfslocked);
265         *flagp = fmode;
266         ndp->ni_vp = NULL;
267         return (error);
268 }
269
270 /*
271  * Check for write permissions on the specified vnode.
272  * Prototype text segments cannot be written.
273  */
274 int
275 vn_writechk(vp)
276         register struct vnode *vp;
277 {
278
279         ASSERT_VOP_LOCKED(vp, "vn_writechk");
280         /*
281          * If there's shared text associated with
282          * the vnode, try to free it up once.  If
283          * we fail, we can't allow writing.
284          */
285         if (VOP_IS_TEXT(vp))
286                 return (ETXTBSY);
287
288         return (0);
289 }
290
291 /*
292  * Vnode close call
293  */
294 int
295 vn_close(vp, flags, file_cred, td)
296         register struct vnode *vp;
297         int flags;
298         struct ucred *file_cred;
299         struct thread *td;
300 {
301         struct mount *mp;
302         int error, lock_flags;
303
304         if (!(flags & FWRITE) && vp->v_mount != NULL &&
305             vp->v_mount->mnt_kern_flag & MNTK_EXTENDED_SHARED)
306                 lock_flags = LK_SHARED;
307         else
308                 lock_flags = LK_EXCLUSIVE;
309
310         VFS_ASSERT_GIANT(vp->v_mount);
311
312         vn_start_write(vp, &mp, V_WAIT);
313         vn_lock(vp, lock_flags | LK_RETRY);
314         if (flags & FWRITE) {
315                 VNASSERT(vp->v_writecount > 0, vp, 
316                     ("vn_close: negative writecount"));
317                 VOP_ADD_WRITECOUNT(vp, -1);
318         }
319         error = VOP_CLOSE(vp, flags, file_cred, td);
320         vput(vp);
321         vn_finished_write(mp);
322         return (error);
323 }
324
325 /*
326  * Heuristic to detect sequential operation.
327  */
328 static int
329 sequential_heuristic(struct uio *uio, struct file *fp)
330 {
331
332         if (atomic_load_acq_int(&(fp->f_flag)) & FRDAHEAD)
333                 return (fp->f_seqcount << IO_SEQSHIFT);
334
335         /*
336          * Offset 0 is handled specially.  open() sets f_seqcount to 1 so
337          * that the first I/O is normally considered to be slightly
338          * sequential.  Seeking to offset 0 doesn't change sequentiality
339          * unless previous seeks have reduced f_seqcount to 0, in which
340          * case offset 0 is not special.
341          */
342         if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
343             uio->uio_offset == fp->f_nextoff) {
344                 /*
345                  * f_seqcount is in units of fixed-size blocks so that it
346                  * depends mainly on the amount of sequential I/O and not
347                  * much on the number of sequential I/O's.  The fixed size
348                  * of 16384 is hard-coded here since it is (not quite) just
349                  * a magic size that works well here.  This size is more
350                  * closely related to the best I/O size for real disks than
351                  * to any block size used by software.
352                  */
353                 fp->f_seqcount += howmany(uio->uio_resid, 16384);
354                 if (fp->f_seqcount > IO_SEQMAX)
355                         fp->f_seqcount = IO_SEQMAX;
356                 return (fp->f_seqcount << IO_SEQSHIFT);
357         }
358
359         /* Not sequential.  Quickly draw-down sequentiality. */
360         if (fp->f_seqcount > 1)
361                 fp->f_seqcount = 1;
362         else
363                 fp->f_seqcount = 0;
364         return (0);
365 }
366
367 /*
368  * Package up an I/O request on a vnode into a uio and do it.
369  */
370 int
371 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
372     enum uio_seg segflg, int ioflg, struct ucred *active_cred,
373     struct ucred *file_cred, ssize_t *aresid, struct thread *td)
374 {
375         struct uio auio;
376         struct iovec aiov;
377         struct mount *mp;
378         struct ucred *cred;
379         void *rl_cookie;
380         int error, lock_flags;
381
382         VFS_ASSERT_GIANT(vp->v_mount);
383
384         auio.uio_iov = &aiov;
385         auio.uio_iovcnt = 1;
386         aiov.iov_base = base;
387         aiov.iov_len = len;
388         auio.uio_resid = len;
389         auio.uio_offset = offset;
390         auio.uio_segflg = segflg;
391         auio.uio_rw = rw;
392         auio.uio_td = td;
393         error = 0;
394
395         if ((ioflg & IO_NODELOCKED) == 0) {
396                 if (rw == UIO_READ) {
397                         rl_cookie = vn_rangelock_rlock(vp, offset,
398                             offset + len);
399                 } else {
400                         rl_cookie = vn_rangelock_wlock(vp, offset,
401                             offset + len);
402                 }
403                 mp = NULL;
404                 if (rw == UIO_WRITE) { 
405                         if (vp->v_type != VCHR &&
406                             (error = vn_start_write(vp, &mp, V_WAIT | PCATCH))
407                             != 0)
408                                 goto out;
409                         if (MNT_SHARED_WRITES(mp) ||
410                             ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount)))
411                                 lock_flags = LK_SHARED;
412                         else
413                                 lock_flags = LK_EXCLUSIVE;
414                 } else
415                         lock_flags = LK_SHARED;
416                 vn_lock(vp, lock_flags | LK_RETRY);
417         } else
418                 rl_cookie = NULL;
419
420         ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
421 #ifdef MAC
422         if ((ioflg & IO_NOMACCHECK) == 0) {
423                 if (rw == UIO_READ)
424                         error = mac_vnode_check_read(active_cred, file_cred,
425                             vp);
426                 else
427                         error = mac_vnode_check_write(active_cred, file_cred,
428                             vp);
429         }
430 #endif
431         if (error == 0) {
432                 if (file_cred != NULL)
433                         cred = file_cred;
434                 else
435                         cred = active_cred;
436                 if (rw == UIO_READ)
437                         error = VOP_READ(vp, &auio, ioflg, cred);
438                 else
439                         error = VOP_WRITE(vp, &auio, ioflg, cred);
440         }
441         if (aresid)
442                 *aresid = auio.uio_resid;
443         else
444                 if (auio.uio_resid && error == 0)
445                         error = EIO;
446         if ((ioflg & IO_NODELOCKED) == 0) {
447                 VOP_UNLOCK(vp, 0);
448                 if (mp != NULL)
449                         vn_finished_write(mp);
450         }
451  out:
452         if (rl_cookie != NULL)
453                 vn_rangelock_unlock(vp, rl_cookie);
454         return (error);
455 }
456
457 /*
458  * Package up an I/O request on a vnode into a uio and do it.  The I/O
459  * request is split up into smaller chunks and we try to avoid saturating
460  * the buffer cache while potentially holding a vnode locked, so we 
461  * check bwillwrite() before calling vn_rdwr().  We also call kern_yield()
462  * to give other processes a chance to lock the vnode (either other processes
463  * core'ing the same binary, or unrelated processes scanning the directory).
464  */
465 int
466 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, active_cred,
467     file_cred, aresid, td)
468         enum uio_rw rw;
469         struct vnode *vp;
470         void *base;
471         size_t len;
472         off_t offset;
473         enum uio_seg segflg;
474         int ioflg;
475         struct ucred *active_cred;
476         struct ucred *file_cred;
477         size_t *aresid;
478         struct thread *td;
479 {
480         int error = 0;
481         ssize_t iaresid;
482
483         VFS_ASSERT_GIANT(vp->v_mount);
484
485         do {
486                 int chunk;
487
488                 /*
489                  * Force `offset' to a multiple of MAXBSIZE except possibly
490                  * for the first chunk, so that filesystems only need to
491                  * write full blocks except possibly for the first and last
492                  * chunks.
493                  */
494                 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
495
496                 if (chunk > len)
497                         chunk = len;
498                 if (rw != UIO_READ && vp->v_type == VREG)
499                         bwillwrite();
500                 iaresid = 0;
501                 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
502                     ioflg, active_cred, file_cred, &iaresid, td);
503                 len -= chunk;   /* aresid calc already includes length */
504                 if (error)
505                         break;
506                 offset += chunk;
507                 base = (char *)base + chunk;
508                 kern_yield(PRI_USER);
509         } while (len);
510         if (aresid)
511                 *aresid = len + iaresid;
512         return (error);
513 }
514
515 off_t
516 foffset_lock(struct file *fp, int flags)
517 {
518         struct mtx *mtxp;
519         off_t res;
520
521         KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed"));
522
523 #if OFF_MAX <= LONG_MAX
524         /*
525          * Caller only wants the current f_offset value.  Assume that
526          * the long and shorter integer types reads are atomic.
527          */
528         if ((flags & FOF_NOLOCK) != 0)
529                 return (fp->f_offset);
530 #endif
531
532         /*
533          * According to McKusick the vn lock was protecting f_offset here.
534          * It is now protected by the FOFFSET_LOCKED flag.
535          */
536         mtxp = mtx_pool_find(mtxpool_sleep, fp);
537         mtx_lock(mtxp);
538         if ((flags & FOF_NOLOCK) == 0) {
539                 while (fp->f_vnread_flags & FOFFSET_LOCKED) {
540                         fp->f_vnread_flags |= FOFFSET_LOCK_WAITING;
541                         msleep(&fp->f_vnread_flags, mtxp, PUSER -1,
542                             "vofflock", 0);
543                 }
544                 fp->f_vnread_flags |= FOFFSET_LOCKED;
545         }
546         res = fp->f_offset;
547         mtx_unlock(mtxp);
548         return (res);
549 }
550
551 void
552 foffset_unlock(struct file *fp, off_t val, int flags)
553 {
554         struct mtx *mtxp;
555
556         KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed"));
557
558 #if OFF_MAX <= LONG_MAX
559         if ((flags & FOF_NOLOCK) != 0) {
560                 if ((flags & FOF_NOUPDATE) == 0)
561                         fp->f_offset = val;
562                 if ((flags & FOF_NEXTOFF) != 0)
563                         fp->f_nextoff = val;
564                 return;
565         }
566 #endif
567
568         mtxp = mtx_pool_find(mtxpool_sleep, fp);
569         mtx_lock(mtxp);
570         if ((flags & FOF_NOUPDATE) == 0)
571                 fp->f_offset = val;
572         if ((flags & FOF_NEXTOFF) != 0)
573                 fp->f_nextoff = val;
574         if ((flags & FOF_NOLOCK) == 0) {
575                 KASSERT((fp->f_vnread_flags & FOFFSET_LOCKED) != 0,
576                     ("Lost FOFFSET_LOCKED"));
577                 if (fp->f_vnread_flags & FOFFSET_LOCK_WAITING)
578                         wakeup(&fp->f_vnread_flags);
579                 fp->f_vnread_flags = 0;
580         }
581         mtx_unlock(mtxp);
582 }
583
584 void
585 foffset_lock_uio(struct file *fp, struct uio *uio, int flags)
586 {
587
588         if ((flags & FOF_OFFSET) == 0)
589                 uio->uio_offset = foffset_lock(fp, flags);
590 }
591
592 void
593 foffset_unlock_uio(struct file *fp, struct uio *uio, int flags)
594 {
595
596         if ((flags & FOF_OFFSET) == 0)
597                 foffset_unlock(fp, uio->uio_offset, flags);
598 }
599
600 static int
601 get_advice(struct file *fp, struct uio *uio)
602 {
603         struct mtx *mtxp;
604         int ret;
605
606         ret = POSIX_FADV_NORMAL;
607         if (fp->f_advice == NULL)
608                 return (ret);
609
610         mtxp = mtx_pool_find(mtxpool_sleep, fp);
611         mtx_lock(mtxp);
612         if (uio->uio_offset >= fp->f_advice->fa_start &&
613             uio->uio_offset + uio->uio_resid <= fp->f_advice->fa_end)
614                 ret = fp->f_advice->fa_advice;
615         mtx_unlock(mtxp);
616         return (ret);
617 }
618
619 /*
620  * File table vnode read routine.
621  */
622 static int
623 vn_read(fp, uio, active_cred, flags, td)
624         struct file *fp;
625         struct uio *uio;
626         struct ucred *active_cred;
627         int flags;
628         struct thread *td;
629 {
630         struct vnode *vp;
631         struct mtx *mtxp;
632         int error, ioflag;
633         int advice, vfslocked;
634         off_t offset, start, end;
635
636         KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
637             uio->uio_td, td));
638         KASSERT(flags & FOF_OFFSET, ("No FOF_OFFSET"));
639         vp = fp->f_vnode;
640         ioflag = 0;
641         if (fp->f_flag & FNONBLOCK)
642                 ioflag |= IO_NDELAY;
643         if (fp->f_flag & O_DIRECT)
644                 ioflag |= IO_DIRECT;
645         advice = get_advice(fp, uio);
646         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
647         vn_lock(vp, LK_SHARED | LK_RETRY);
648
649         switch (advice) {
650         case POSIX_FADV_NORMAL:
651         case POSIX_FADV_SEQUENTIAL:
652         case POSIX_FADV_NOREUSE:
653                 ioflag |= sequential_heuristic(uio, fp);
654                 break;
655         case POSIX_FADV_RANDOM:
656                 /* Disable read-ahead for random I/O. */
657                 break;
658         }
659         offset = uio->uio_offset;
660
661 #ifdef MAC
662         error = mac_vnode_check_read(active_cred, fp->f_cred, vp);
663         if (error == 0)
664 #endif
665                 error = VOP_READ(vp, uio, ioflag, fp->f_cred);
666         fp->f_nextoff = uio->uio_offset;
667         VOP_UNLOCK(vp, 0);
668         if (error == 0 && advice == POSIX_FADV_NOREUSE &&
669             offset != uio->uio_offset) {
670                 /*
671                  * Use POSIX_FADV_DONTNEED to flush clean pages and
672                  * buffers for the backing file after a
673                  * POSIX_FADV_NOREUSE read(2).  To optimize the common
674                  * case of using POSIX_FADV_NOREUSE with sequential
675                  * access, track the previous implicit DONTNEED
676                  * request and grow this request to include the
677                  * current read(2) in addition to the previous
678                  * DONTNEED.  With purely sequential access this will
679                  * cause the DONTNEED requests to continously grow to
680                  * cover all of the previously read regions of the
681                  * file.  This allows filesystem blocks that are
682                  * accessed by multiple calls to read(2) to be flushed
683                  * once the last read(2) finishes.
684                  */
685                 start = offset;
686                 end = uio->uio_offset - 1;
687                 mtxp = mtx_pool_find(mtxpool_sleep, fp);
688                 mtx_lock(mtxp);
689                 if (fp->f_advice != NULL &&
690                     fp->f_advice->fa_advice == POSIX_FADV_NOREUSE) {
691                         if (start != 0 && fp->f_advice->fa_prevend + 1 == start)
692                                 start = fp->f_advice->fa_prevstart;
693                         else if (fp->f_advice->fa_prevstart != 0 &&
694                             fp->f_advice->fa_prevstart == end + 1)
695                                 end = fp->f_advice->fa_prevend;
696                         fp->f_advice->fa_prevstart = start;
697                         fp->f_advice->fa_prevend = end;
698                 }
699                 mtx_unlock(mtxp);
700                 error = VOP_ADVISE(vp, start, end, POSIX_FADV_DONTNEED);
701         }
702         VFS_UNLOCK_GIANT(vfslocked);
703         return (error);
704 }
705
706 /*
707  * File table vnode write routine.
708  */
709 static int
710 vn_write(fp, uio, active_cred, flags, td)
711         struct file *fp;
712         struct uio *uio;
713         struct ucred *active_cred;
714         int flags;
715         struct thread *td;
716 {
717         struct vnode *vp;
718         struct mount *mp;
719         struct mtx *mtxp;
720         int error, ioflag, lock_flags;
721         int advice, vfslocked;
722         off_t offset, start, end;
723
724         KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
725             uio->uio_td, td));
726         KASSERT(flags & FOF_OFFSET, ("No FOF_OFFSET"));
727         vp = fp->f_vnode;
728         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
729         if (vp->v_type == VREG)
730                 bwillwrite();
731         ioflag = IO_UNIT;
732         if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
733                 ioflag |= IO_APPEND;
734         if (fp->f_flag & FNONBLOCK)
735                 ioflag |= IO_NDELAY;
736         if (fp->f_flag & O_DIRECT)
737                 ioflag |= IO_DIRECT;
738         if ((fp->f_flag & O_FSYNC) ||
739             (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
740                 ioflag |= IO_SYNC;
741         mp = NULL;
742         if (vp->v_type != VCHR &&
743             (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
744                 goto unlock;
745
746         advice = get_advice(fp, uio);
747  
748         if ((MNT_SHARED_WRITES(mp) ||
749             ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) &&
750             (flags & FOF_OFFSET) != 0) {
751                 lock_flags = LK_SHARED;
752         } else {
753                 lock_flags = LK_EXCLUSIVE;
754         }
755
756         vn_lock(vp, lock_flags | LK_RETRY);
757         switch (advice) {
758         case POSIX_FADV_NORMAL:
759         case POSIX_FADV_SEQUENTIAL:
760         case POSIX_FADV_NOREUSE:
761                 ioflag |= sequential_heuristic(uio, fp);
762                 break;
763         case POSIX_FADV_RANDOM:
764                 /* XXX: Is this correct? */
765                 break;
766         }
767         offset = uio->uio_offset;
768
769 #ifdef MAC
770         error = mac_vnode_check_write(active_cred, fp->f_cred, vp);
771         if (error == 0)
772 #endif
773                 error = VOP_WRITE(vp, uio, ioflag, fp->f_cred);
774         fp->f_nextoff = uio->uio_offset;
775         VOP_UNLOCK(vp, 0);
776         if (vp->v_type != VCHR)
777                 vn_finished_write(mp);
778         if (error == 0 && advice == POSIX_FADV_NOREUSE &&
779             offset != uio->uio_offset) {
780                 /*
781                  * Use POSIX_FADV_DONTNEED to flush clean pages and
782                  * buffers for the backing file after a
783                  * POSIX_FADV_NOREUSE write(2).  To optimize the
784                  * common case of using POSIX_FADV_NOREUSE with
785                  * sequential access, track the previous implicit
786                  * DONTNEED request and grow this request to include
787                  * the current write(2) in addition to the previous
788                  * DONTNEED.  With purely sequential access this will
789                  * cause the DONTNEED requests to continously grow to
790                  * cover all of the previously written regions of the
791                  * file.
792                  *
793                  * Note that the blocks just written are almost
794                  * certainly still dirty, so this only works when
795                  * VOP_ADVISE() calls from subsequent writes push out
796                  * the data written by this write(2) once the backing
797                  * buffers are clean.  However, as compared to forcing
798                  * IO_DIRECT, this gives much saner behavior.  Write
799                  * clustering is still allowed, and clean pages are
800                  * merely moved to the cache page queue rather than
801                  * outright thrown away.  This means a subsequent
802                  * read(2) can still avoid hitting the disk if the
803                  * pages have not been reclaimed.
804                  *
805                  * This does make POSIX_FADV_NOREUSE largely useless
806                  * with non-sequential access.  However, sequential
807                  * access is the more common use case and the flag is
808                  * merely advisory.
809                  */
810                 start = offset;
811                 end = uio->uio_offset - 1;
812                 mtxp = mtx_pool_find(mtxpool_sleep, fp);
813                 mtx_lock(mtxp);
814                 if (fp->f_advice != NULL &&
815                     fp->f_advice->fa_advice == POSIX_FADV_NOREUSE) {
816                         if (start != 0 && fp->f_advice->fa_prevend + 1 == start)
817                                 start = fp->f_advice->fa_prevstart;
818                         else if (fp->f_advice->fa_prevstart != 0 &&
819                             fp->f_advice->fa_prevstart == end + 1)
820                                 end = fp->f_advice->fa_prevend;
821                         fp->f_advice->fa_prevstart = start;
822                         fp->f_advice->fa_prevend = end;
823                 }
824                 mtx_unlock(mtxp);
825                 error = VOP_ADVISE(vp, start, end, POSIX_FADV_DONTNEED);
826         }
827         
828 unlock:
829         VFS_UNLOCK_GIANT(vfslocked);
830         return (error);
831 }
832
833 static const int io_hold_cnt = 16;
834 static int vn_io_fault_enable = 0;
835 SYSCTL_INT(_debug, OID_AUTO, vn_io_fault_enable, CTLFLAG_RW,
836     &vn_io_fault_enable, 0, "Enable vn_io_fault lock avoidance");
837 static u_long vn_io_faults_cnt;
838 SYSCTL_ULONG(_debug, OID_AUTO, vn_io_faults, CTLFLAG_RD,
839     &vn_io_faults_cnt, 0, "Count of vn_io_fault lock avoidance triggers");
840
841 /*
842  * The vn_io_fault() is a wrapper around vn_read() and vn_write() to
843  * prevent the following deadlock:
844  *
845  * Assume that the thread A reads from the vnode vp1 into userspace
846  * buffer buf1 backed by the pages of vnode vp2.  If a page in buf1 is
847  * currently not resident, then system ends up with the call chain
848  *   vn_read() -> VOP_READ(vp1) -> uiomove() -> [Page Fault] ->
849  *     vm_fault(buf1) -> vnode_pager_getpages(vp2) -> VOP_GETPAGES(vp2)
850  * which establishes lock order vp1->vn_lock, then vp2->vn_lock.
851  * If, at the same time, thread B reads from vnode vp2 into buffer buf2
852  * backed by the pages of vnode vp1, and some page in buf2 is not
853  * resident, we get a reversed order vp2->vn_lock, then vp1->vn_lock.
854  *
855  * To prevent the lock order reversal and deadlock, vn_io_fault() does
856  * not allow page faults to happen during VOP_READ() or VOP_WRITE().
857  * Instead, it first tries to do the whole range i/o with pagefaults
858  * disabled. If all pages in the i/o buffer are resident and mapped,
859  * VOP will succeed (ignoring the genuine filesystem errors).
860  * Otherwise, we get back EFAULT, and vn_io_fault() falls back to do
861  * i/o in chunks, with all pages in the chunk prefaulted and held
862  * using vm_fault_quick_hold_pages().
863  *
864  * Filesystems using this deadlock avoidance scheme should use the
865  * array of the held pages from uio, saved in the curthread->td_ma,
866  * instead of doing uiomove().  A helper function
867  * vn_io_fault_uiomove() converts uiomove request into
868  * uiomove_fromphys() over td_ma array.
869  *
870  * Since vnode locks do not cover the whole i/o anymore, rangelocks
871  * make the current i/o request atomic with respect to other i/os and
872  * truncations.
873  */
874 static int
875 vn_io_fault(struct file *fp, struct uio *uio, struct ucred *active_cred,
876     int flags, struct thread *td)
877 {
878         vm_page_t ma[io_hold_cnt + 2];
879         struct uio *uio_clone, short_uio;
880         struct iovec short_iovec[1];
881         fo_rdwr_t *doio;
882         struct vnode *vp;
883         void *rl_cookie;
884         struct mount *mp;
885         vm_page_t *prev_td_ma;
886         int cnt, error, save, saveheld, prev_td_ma_cnt;
887         vm_offset_t addr, end;
888         vm_prot_t prot;
889         size_t len, resid;
890         ssize_t adv;
891
892         if (uio->uio_rw == UIO_READ)
893                 doio = vn_read;
894         else
895                 doio = vn_write;
896         vp = fp->f_vnode;
897         foffset_lock_uio(fp, uio, flags);
898
899         if (uio->uio_segflg != UIO_USERSPACE || vp->v_type != VREG ||
900             ((mp = vp->v_mount) != NULL &&
901             (mp->mnt_kern_flag & MNTK_NO_IOPF) == 0) ||
902             !vn_io_fault_enable) {
903                 error = doio(fp, uio, active_cred, flags | FOF_OFFSET, td);
904                 goto out_last;
905         }
906
907         /*
908          * The UFS follows IO_UNIT directive and replays back both
909          * uio_offset and uio_resid if an error is encountered during the
910          * operation.  But, since the iovec may be already advanced,
911          * uio is still in an inconsistent state.
912          *
913          * Cache a copy of the original uio, which is advanced to the redo
914          * point using UIO_NOCOPY below.
915          */
916         uio_clone = cloneuio(uio);
917         resid = uio->uio_resid;
918
919         short_uio.uio_segflg = UIO_USERSPACE;
920         short_uio.uio_rw = uio->uio_rw;
921         short_uio.uio_td = uio->uio_td;
922
923         if (uio->uio_rw == UIO_READ) {
924                 prot = VM_PROT_WRITE;
925                 rl_cookie = vn_rangelock_rlock(vp, uio->uio_offset,
926                     uio->uio_offset + uio->uio_resid);
927         } else {
928                 prot = VM_PROT_READ;
929                 if ((fp->f_flag & O_APPEND) != 0 || (flags & FOF_OFFSET) == 0)
930                         /* For appenders, punt and lock the whole range. */
931                         rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
932                 else
933                         rl_cookie = vn_rangelock_wlock(vp, uio->uio_offset,
934                             uio->uio_offset + uio->uio_resid);
935         }
936
937         save = vm_fault_disable_pagefaults();
938         error = doio(fp, uio, active_cred, flags | FOF_OFFSET, td);
939         if (error != EFAULT)
940                 goto out;
941
942         atomic_add_long(&vn_io_faults_cnt, 1);
943         uio_clone->uio_segflg = UIO_NOCOPY;
944         uiomove(NULL, resid - uio->uio_resid, uio_clone);
945         uio_clone->uio_segflg = uio->uio_segflg;
946
947         saveheld = curthread_pflags_set(TDP_UIOHELD);
948         prev_td_ma = td->td_ma;
949         prev_td_ma_cnt = td->td_ma_cnt;
950
951         while (uio_clone->uio_resid != 0) {
952                 len = uio_clone->uio_iov->iov_len;
953                 if (len == 0) {
954                         KASSERT(uio_clone->uio_iovcnt >= 1,
955                             ("iovcnt underflow"));
956                         uio_clone->uio_iov++;
957                         uio_clone->uio_iovcnt--;
958                         continue;
959                 }
960
961                 addr = (vm_offset_t)uio_clone->uio_iov->iov_base;
962                 end = round_page(addr + len);
963                 cnt = howmany(end - trunc_page(addr), PAGE_SIZE);
964                 /*
965                  * A perfectly misaligned address and length could cause
966                  * both the start and the end of the chunk to use partial
967                  * page.  +2 accounts for such a situation.
968                  */
969                 if (cnt > io_hold_cnt + 2) {
970                         len = io_hold_cnt * PAGE_SIZE;
971                         KASSERT(howmany(round_page(addr + len) -
972                             trunc_page(addr), PAGE_SIZE) <= io_hold_cnt + 2,
973                             ("cnt overflow"));
974                 }
975                 cnt = vm_fault_quick_hold_pages(&td->td_proc->p_vmspace->vm_map,
976                     addr, len, prot, ma, io_hold_cnt + 2);
977                 if (cnt == -1) {
978                         error = EFAULT;
979                         break;
980                 }
981                 short_uio.uio_iov = &short_iovec[0];
982                 short_iovec[0].iov_base = (void *)addr;
983                 short_uio.uio_iovcnt = 1;
984                 short_uio.uio_resid = short_iovec[0].iov_len = len;
985                 short_uio.uio_offset = uio_clone->uio_offset;
986                 td->td_ma = ma;
987                 td->td_ma_cnt = cnt;
988
989                 error = doio(fp, &short_uio, active_cred, flags | FOF_OFFSET,
990                     td);
991                 vm_page_unhold_pages(ma, cnt);
992                 adv = len - short_uio.uio_resid;
993
994                 uio_clone->uio_iov->iov_base =
995                     (char *)uio_clone->uio_iov->iov_base + adv;
996                 uio_clone->uio_iov->iov_len -= adv;
997                 uio_clone->uio_resid -= adv;
998                 uio_clone->uio_offset += adv;
999
1000                 uio->uio_resid -= adv;
1001                 uio->uio_offset += adv;
1002
1003                 if (error != 0 || adv == 0)
1004                         break;
1005         }
1006         td->td_ma = prev_td_ma;
1007         td->td_ma_cnt = prev_td_ma_cnt;
1008         curthread_pflags_restore(saveheld);
1009 out:
1010         vm_fault_enable_pagefaults(save);
1011         vn_rangelock_unlock(vp, rl_cookie);
1012         free(uio_clone, M_IOV);
1013 out_last:
1014         foffset_unlock_uio(fp, uio, flags);
1015         return (error);
1016 }
1017
1018 /*
1019  * Helper function to perform the requested uiomove operation using
1020  * the held pages for io->uio_iov[0].iov_base buffer instead of
1021  * copyin/copyout.  Access to the pages with uiomove_fromphys()
1022  * instead of iov_base prevents page faults that could occur due to
1023  * pmap_collect() invalidating the mapping created by
1024  * vm_fault_quick_hold_pages(), or pageout daemon, page laundry or
1025  * object cleanup revoking the write access from page mappings.
1026  *
1027  * Filesystems specified MNTK_NO_IOPF shall use vn_io_fault_uiomove()
1028  * instead of plain uiomove().
1029  */
1030 int
1031 vn_io_fault_uiomove(char *data, int xfersize, struct uio *uio)
1032 {
1033         struct uio transp_uio;
1034         struct iovec transp_iov[1];
1035         struct thread *td;
1036         size_t adv;
1037         int error, pgadv;
1038
1039         td = curthread;
1040         if ((td->td_pflags & TDP_UIOHELD) == 0 ||
1041             uio->uio_segflg != UIO_USERSPACE)
1042                 return (uiomove(data, xfersize, uio));
1043
1044         KASSERT(uio->uio_iovcnt == 1, ("uio_iovcnt %d", uio->uio_iovcnt));
1045         transp_iov[0].iov_base = data;
1046         transp_uio.uio_iov = &transp_iov[0];
1047         transp_uio.uio_iovcnt = 1;
1048         if (xfersize > uio->uio_resid)
1049                 xfersize = uio->uio_resid;
1050         transp_uio.uio_resid = transp_iov[0].iov_len = xfersize;
1051         transp_uio.uio_offset = 0;
1052         transp_uio.uio_segflg = UIO_SYSSPACE;
1053         /*
1054          * Since transp_iov points to data, and td_ma page array
1055          * corresponds to original uio->uio_iov, we need to invert the
1056          * direction of the i/o operation as passed to
1057          * uiomove_fromphys().
1058          */
1059         switch (uio->uio_rw) {
1060         case UIO_WRITE:
1061                 transp_uio.uio_rw = UIO_READ;
1062                 break;
1063         case UIO_READ:
1064                 transp_uio.uio_rw = UIO_WRITE;
1065                 break;
1066         }
1067         transp_uio.uio_td = uio->uio_td;
1068         error = uiomove_fromphys(td->td_ma,
1069             ((vm_offset_t)uio->uio_iov->iov_base) & PAGE_MASK,
1070             xfersize, &transp_uio);
1071         adv = xfersize - transp_uio.uio_resid;
1072         pgadv =
1073             (((vm_offset_t)uio->uio_iov->iov_base + adv) >> PAGE_SHIFT) -
1074             (((vm_offset_t)uio->uio_iov->iov_base) >> PAGE_SHIFT);
1075         td->td_ma += pgadv;
1076         KASSERT(td->td_ma_cnt >= pgadv, ("consumed pages %d %d", td->td_ma_cnt,
1077             pgadv));
1078         td->td_ma_cnt -= pgadv;
1079         uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + adv;
1080         uio->uio_iov->iov_len -= adv;
1081         uio->uio_resid -= adv;
1082         uio->uio_offset += adv;
1083         return (error);
1084 }
1085
1086 /*
1087  * File table truncate routine.
1088  */
1089 static int
1090 vn_truncate(struct file *fp, off_t length, struct ucred *active_cred,
1091     struct thread *td)
1092 {
1093         struct vattr vattr;
1094         struct mount *mp;
1095         struct vnode *vp;
1096         void *rl_cookie;
1097         int vfslocked;
1098         int error;
1099
1100         vp = fp->f_vnode;
1101
1102         /*
1103          * Lock the whole range for truncation.  Otherwise split i/o
1104          * might happen partly before and partly after the truncation.
1105          */
1106         rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
1107         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1108         error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
1109         if (error)
1110                 goto out1;
1111         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1112         if (vp->v_type == VDIR) {
1113                 error = EISDIR;
1114                 goto out;
1115         }
1116 #ifdef MAC
1117         error = mac_vnode_check_write(active_cred, fp->f_cred, vp);
1118         if (error)
1119                 goto out;
1120 #endif
1121         error = vn_writechk(vp);
1122         if (error == 0) {
1123                 VATTR_NULL(&vattr);
1124                 vattr.va_size = length;
1125                 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
1126         }
1127 out:
1128         VOP_UNLOCK(vp, 0);
1129         vn_finished_write(mp);
1130 out1:
1131         VFS_UNLOCK_GIANT(vfslocked);
1132         vn_rangelock_unlock(vp, rl_cookie);
1133         return (error);
1134 }
1135
1136 /*
1137  * File table vnode stat routine.
1138  */
1139 static int
1140 vn_statfile(fp, sb, active_cred, td)
1141         struct file *fp;
1142         struct stat *sb;
1143         struct ucred *active_cred;
1144         struct thread *td;
1145 {
1146         struct vnode *vp = fp->f_vnode;
1147         int vfslocked;
1148         int error;
1149
1150         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1151         vn_lock(vp, LK_SHARED | LK_RETRY);
1152         error = vn_stat(vp, sb, active_cred, fp->f_cred, td);
1153         VOP_UNLOCK(vp, 0);
1154         VFS_UNLOCK_GIANT(vfslocked);
1155
1156         return (error);
1157 }
1158
1159 /*
1160  * Stat a vnode; implementation for the stat syscall
1161  */
1162 int
1163 vn_stat(vp, sb, active_cred, file_cred, td)
1164         struct vnode *vp;
1165         register struct stat *sb;
1166         struct ucred *active_cred;
1167         struct ucred *file_cred;
1168         struct thread *td;
1169 {
1170         struct vattr vattr;
1171         register struct vattr *vap;
1172         int error;
1173         u_short mode;
1174
1175 #ifdef MAC
1176         error = mac_vnode_check_stat(active_cred, file_cred, vp);
1177         if (error)
1178                 return (error);
1179 #endif
1180
1181         vap = &vattr;
1182
1183         /*
1184          * Initialize defaults for new and unusual fields, so that file
1185          * systems which don't support these fields don't need to know
1186          * about them.
1187          */
1188         vap->va_birthtime.tv_sec = -1;
1189         vap->va_birthtime.tv_nsec = 0;
1190         vap->va_fsid = VNOVAL;
1191         vap->va_rdev = NODEV;
1192
1193         error = VOP_GETATTR(vp, vap, active_cred);
1194         if (error)
1195                 return (error);
1196
1197         /*
1198          * Zero the spare stat fields
1199          */
1200         bzero(sb, sizeof *sb);
1201
1202         /*
1203          * Copy from vattr table
1204          */
1205         if (vap->va_fsid != VNOVAL)
1206                 sb->st_dev = vap->va_fsid;
1207         else
1208                 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
1209         sb->st_ino = vap->va_fileid;
1210         mode = vap->va_mode;
1211         switch (vap->va_type) {
1212         case VREG:
1213                 mode |= S_IFREG;
1214                 break;
1215         case VDIR:
1216                 mode |= S_IFDIR;
1217                 break;
1218         case VBLK:
1219                 mode |= S_IFBLK;
1220                 break;
1221         case VCHR:
1222                 mode |= S_IFCHR;
1223                 break;
1224         case VLNK:
1225                 mode |= S_IFLNK;
1226                 break;
1227         case VSOCK:
1228                 mode |= S_IFSOCK;
1229                 break;
1230         case VFIFO:
1231                 mode |= S_IFIFO;
1232                 break;
1233         default:
1234                 return (EBADF);
1235         };
1236         sb->st_mode = mode;
1237         sb->st_nlink = vap->va_nlink;
1238         sb->st_uid = vap->va_uid;
1239         sb->st_gid = vap->va_gid;
1240         sb->st_rdev = vap->va_rdev;
1241         if (vap->va_size > OFF_MAX)
1242                 return (EOVERFLOW);
1243         sb->st_size = vap->va_size;
1244         sb->st_atim = vap->va_atime;
1245         sb->st_mtim = vap->va_mtime;
1246         sb->st_ctim = vap->va_ctime;
1247         sb->st_birthtim = vap->va_birthtime;
1248
1249         /*
1250          * According to www.opengroup.org, the meaning of st_blksize is 
1251          *   "a filesystem-specific preferred I/O block size for this 
1252          *    object.  In some filesystem types, this may vary from file
1253          *    to file"
1254          * Use miminum/default of PAGE_SIZE (e.g. for VCHR).
1255          */
1256
1257         sb->st_blksize = max(PAGE_SIZE, vap->va_blocksize);
1258         
1259         sb->st_flags = vap->va_flags;
1260         if (priv_check(td, PRIV_VFS_GENERATION))
1261                 sb->st_gen = 0;
1262         else
1263                 sb->st_gen = vap->va_gen;
1264
1265         sb->st_blocks = vap->va_bytes / S_BLKSIZE;
1266         return (0);
1267 }
1268
1269 /*
1270  * File table vnode ioctl routine.
1271  */
1272 static int
1273 vn_ioctl(fp, com, data, active_cred, td)
1274         struct file *fp;
1275         u_long com;
1276         void *data;
1277         struct ucred *active_cred;
1278         struct thread *td;
1279 {
1280         struct vnode *vp = fp->f_vnode;
1281         struct vattr vattr;
1282         int vfslocked;
1283         int error;
1284
1285         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1286         error = ENOTTY;
1287         switch (vp->v_type) {
1288         case VREG:
1289         case VDIR:
1290                 if (com == FIONREAD) {
1291                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1292                         error = VOP_GETATTR(vp, &vattr, active_cred);
1293                         VOP_UNLOCK(vp, 0);
1294                         if (!error)
1295                                 *(int *)data = vattr.va_size - fp->f_offset;
1296                 }
1297                 if (com == FIONBIO || com == FIOASYNC)  /* XXX */
1298                         error = 0;
1299                 else
1300                         error = VOP_IOCTL(vp, com, data, fp->f_flag,
1301                             active_cred, td);
1302                 break;
1303
1304         default:
1305                 break;
1306         }
1307         VFS_UNLOCK_GIANT(vfslocked);
1308         return (error);
1309 }
1310
1311 /*
1312  * File table vnode poll routine.
1313  */
1314 static int
1315 vn_poll(fp, events, active_cred, td)
1316         struct file *fp;
1317         int events;
1318         struct ucred *active_cred;
1319         struct thread *td;
1320 {
1321         struct vnode *vp;
1322         int vfslocked;
1323         int error;
1324
1325         vp = fp->f_vnode;
1326         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1327 #ifdef MAC
1328         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1329         error = mac_vnode_check_poll(active_cred, fp->f_cred, vp);
1330         VOP_UNLOCK(vp, 0);
1331         if (!error)
1332 #endif
1333
1334         error = VOP_POLL(vp, events, fp->f_cred, td);
1335         VFS_UNLOCK_GIANT(vfslocked);
1336         return (error);
1337 }
1338
1339 /*
1340  * Acquire the requested lock and then check for validity.  LK_RETRY
1341  * permits vn_lock to return doomed vnodes.
1342  */
1343 int
1344 _vn_lock(struct vnode *vp, int flags, char *file, int line)
1345 {
1346         int error;
1347
1348         VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
1349             ("vn_lock called with no locktype."));
1350         do {
1351 #ifdef DEBUG_VFS_LOCKS
1352                 KASSERT(vp->v_holdcnt != 0,
1353                     ("vn_lock %p: zero hold count", vp));
1354 #endif
1355                 error = VOP_LOCK1(vp, flags, file, line);
1356                 flags &= ~LK_INTERLOCK; /* Interlock is always dropped. */
1357                 KASSERT((flags & LK_RETRY) == 0 || error == 0,
1358                     ("LK_RETRY set with incompatible flags (0x%x) or an error occured (%d)",
1359                     flags, error));
1360                 /*
1361                  * Callers specify LK_RETRY if they wish to get dead vnodes.
1362                  * If RETRY is not set, we return ENOENT instead.
1363                  */
1364                 if (error == 0 && vp->v_iflag & VI_DOOMED &&
1365                     (flags & LK_RETRY) == 0) {
1366                         VOP_UNLOCK(vp, 0);
1367                         error = ENOENT;
1368                         break;
1369                 }
1370         } while (flags & LK_RETRY && error != 0);
1371         return (error);
1372 }
1373
1374 /*
1375  * File table vnode close routine.
1376  */
1377 static int
1378 vn_closefile(fp, td)
1379         struct file *fp;
1380         struct thread *td;
1381 {
1382         struct vnode *vp;
1383         struct flock lf;
1384         int vfslocked;
1385         int error;
1386
1387         vp = fp->f_vnode;
1388
1389         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1390         if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK) {
1391                 lf.l_whence = SEEK_SET;
1392                 lf.l_start = 0;
1393                 lf.l_len = 0;
1394                 lf.l_type = F_UNLCK;
1395                 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
1396         }
1397
1398         fp->f_ops = &badfileops;
1399
1400         error = vn_close(vp, fp->f_flag, fp->f_cred, td);
1401         VFS_UNLOCK_GIANT(vfslocked);
1402         return (error);
1403 }
1404
1405 /*
1406  * Preparing to start a filesystem write operation. If the operation is
1407  * permitted, then we bump the count of operations in progress and
1408  * proceed. If a suspend request is in progress, we wait until the
1409  * suspension is over, and then proceed.
1410  */
1411 static int
1412 vn_start_write_locked(struct mount *mp, int flags)
1413 {
1414         int error;
1415
1416         mtx_assert(MNT_MTX(mp), MA_OWNED);
1417         error = 0;
1418
1419         /*
1420          * Check on status of suspension.
1421          */
1422         if ((curthread->td_pflags & TDP_IGNSUSP) == 0 ||
1423             mp->mnt_susp_owner != curthread) {
1424                 while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1425                         if (flags & V_NOWAIT) {
1426                                 error = EWOULDBLOCK;
1427                                 goto unlock;
1428                         }
1429                         error = msleep(&mp->mnt_flag, MNT_MTX(mp),
1430                             (PUSER - 1) | (flags & PCATCH), "suspfs", 0);
1431                         if (error)
1432                                 goto unlock;
1433                 }
1434         }
1435         if (flags & V_XSLEEP)
1436                 goto unlock;
1437         mp->mnt_writeopcount++;
1438 unlock:
1439         if (error != 0 || (flags & V_XSLEEP) != 0)
1440                 MNT_REL(mp);
1441         MNT_IUNLOCK(mp);
1442         return (error);
1443 }
1444
1445 int
1446 vn_start_write(vp, mpp, flags)
1447         struct vnode *vp;
1448         struct mount **mpp;
1449         int flags;
1450 {
1451         struct mount *mp;
1452         int error;
1453
1454         error = 0;
1455         /*
1456          * If a vnode is provided, get and return the mount point that
1457          * to which it will write.
1458          */
1459         if (vp != NULL) {
1460                 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
1461                         *mpp = NULL;
1462                         if (error != EOPNOTSUPP)
1463                                 return (error);
1464                         return (0);
1465                 }
1466         }
1467         if ((mp = *mpp) == NULL)
1468                 return (0);
1469
1470         /*
1471          * VOP_GETWRITEMOUNT() returns with the mp refcount held through
1472          * a vfs_ref().
1473          * As long as a vnode is not provided we need to acquire a
1474          * refcount for the provided mountpoint too, in order to
1475          * emulate a vfs_ref().
1476          */
1477         MNT_ILOCK(mp);
1478         if (vp == NULL)
1479                 MNT_REF(mp);
1480
1481         return (vn_start_write_locked(mp, flags));
1482 }
1483
1484 /*
1485  * Secondary suspension. Used by operations such as vop_inactive
1486  * routines that are needed by the higher level functions. These
1487  * are allowed to proceed until all the higher level functions have
1488  * completed (indicated by mnt_writeopcount dropping to zero). At that
1489  * time, these operations are halted until the suspension is over.
1490  */
1491 int
1492 vn_start_secondary_write(vp, mpp, flags)
1493         struct vnode *vp;
1494         struct mount **mpp;
1495         int flags;
1496 {
1497         struct mount *mp;
1498         int error;
1499
1500  retry:
1501         if (vp != NULL) {
1502                 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
1503                         *mpp = NULL;
1504                         if (error != EOPNOTSUPP)
1505                                 return (error);
1506                         return (0);
1507                 }
1508         }
1509         /*
1510          * If we are not suspended or have not yet reached suspended
1511          * mode, then let the operation proceed.
1512          */
1513         if ((mp = *mpp) == NULL)
1514                 return (0);
1515
1516         /*
1517          * VOP_GETWRITEMOUNT() returns with the mp refcount held through
1518          * a vfs_ref().
1519          * As long as a vnode is not provided we need to acquire a
1520          * refcount for the provided mountpoint too, in order to
1521          * emulate a vfs_ref().
1522          */
1523         MNT_ILOCK(mp);
1524         if (vp == NULL)
1525                 MNT_REF(mp);
1526         if ((mp->mnt_kern_flag & (MNTK_SUSPENDED | MNTK_SUSPEND2)) == 0) {
1527                 mp->mnt_secondary_writes++;
1528                 mp->mnt_secondary_accwrites++;
1529                 MNT_IUNLOCK(mp);
1530                 return (0);
1531         }
1532         if (flags & V_NOWAIT) {
1533                 MNT_REL(mp);
1534                 MNT_IUNLOCK(mp);
1535                 return (EWOULDBLOCK);
1536         }
1537         /*
1538          * Wait for the suspension to finish.
1539          */
1540         error = msleep(&mp->mnt_flag, MNT_MTX(mp),
1541                        (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0);
1542         vfs_rel(mp);
1543         if (error == 0)
1544                 goto retry;
1545         return (error);
1546 }
1547
1548 /*
1549  * Filesystem write operation has completed. If we are suspending and this
1550  * operation is the last one, notify the suspender that the suspension is
1551  * now in effect.
1552  */
1553 void
1554 vn_finished_write(mp)
1555         struct mount *mp;
1556 {
1557         if (mp == NULL)
1558                 return;
1559         MNT_ILOCK(mp);
1560         MNT_REL(mp);
1561         mp->mnt_writeopcount--;
1562         if (mp->mnt_writeopcount < 0)
1563                 panic("vn_finished_write: neg cnt");
1564         if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1565             mp->mnt_writeopcount <= 0)
1566                 wakeup(&mp->mnt_writeopcount);
1567         MNT_IUNLOCK(mp);
1568 }
1569
1570
1571 /*
1572  * Filesystem secondary write operation has completed. If we are
1573  * suspending and this operation is the last one, notify the suspender
1574  * that the suspension is now in effect.
1575  */
1576 void
1577 vn_finished_secondary_write(mp)
1578         struct mount *mp;
1579 {
1580         if (mp == NULL)
1581                 return;
1582         MNT_ILOCK(mp);
1583         MNT_REL(mp);
1584         mp->mnt_secondary_writes--;
1585         if (mp->mnt_secondary_writes < 0)
1586                 panic("vn_finished_secondary_write: neg cnt");
1587         if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1588             mp->mnt_secondary_writes <= 0)
1589                 wakeup(&mp->mnt_secondary_writes);
1590         MNT_IUNLOCK(mp);
1591 }
1592
1593
1594
1595 /*
1596  * Request a filesystem to suspend write operations.
1597  */
1598 int
1599 vfs_write_suspend(mp)
1600         struct mount *mp;
1601 {
1602         int error;
1603
1604         MNT_ILOCK(mp);
1605         if (mp->mnt_susp_owner == curthread) {
1606                 MNT_IUNLOCK(mp);
1607                 return (EALREADY);
1608         }
1609         while (mp->mnt_kern_flag & MNTK_SUSPEND)
1610                 msleep(&mp->mnt_flag, MNT_MTX(mp), PUSER - 1, "wsuspfs", 0);
1611         mp->mnt_kern_flag |= MNTK_SUSPEND;
1612         mp->mnt_susp_owner = curthread;
1613         if (mp->mnt_writeopcount > 0)
1614                 (void) msleep(&mp->mnt_writeopcount, 
1615                     MNT_MTX(mp), (PUSER - 1)|PDROP, "suspwt", 0);
1616         else
1617                 MNT_IUNLOCK(mp);
1618         if ((error = VFS_SYNC(mp, MNT_SUSPEND)) != 0)
1619                 vfs_write_resume(mp);
1620         return (error);
1621 }
1622
1623 /*
1624  * Request a filesystem to resume write operations.
1625  */
1626 void
1627 vfs_write_resume_flags(struct mount *mp, int flags)
1628 {
1629
1630         MNT_ILOCK(mp);
1631         if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1632                 KASSERT(mp->mnt_susp_owner == curthread, ("mnt_susp_owner"));
1633                 mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPEND2 |
1634                                        MNTK_SUSPENDED);
1635                 mp->mnt_susp_owner = NULL;
1636                 wakeup(&mp->mnt_writeopcount);
1637                 wakeup(&mp->mnt_flag);
1638                 curthread->td_pflags &= ~TDP_IGNSUSP;
1639                 if ((flags & VR_START_WRITE) != 0) {
1640                         MNT_REF(mp);
1641                         mp->mnt_writeopcount++;
1642                 }
1643                 MNT_IUNLOCK(mp);
1644                 if ((flags & VR_NO_SUSPCLR) == 0)
1645                         VFS_SUSP_CLEAN(mp);
1646         } else if ((flags & VR_START_WRITE) != 0) {
1647                 MNT_REF(mp);
1648                 vn_start_write_locked(mp, 0);
1649         } else {
1650                 MNT_IUNLOCK(mp);
1651         }
1652 }
1653
1654 void
1655 vfs_write_resume(struct mount *mp)
1656 {
1657
1658         vfs_write_resume_flags(mp, 0);
1659 }
1660
1661 /*
1662  * Implement kqueues for files by translating it to vnode operation.
1663  */
1664 static int
1665 vn_kqfilter(struct file *fp, struct knote *kn)
1666 {
1667         int vfslocked;
1668         int error;
1669
1670         vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
1671         error = VOP_KQFILTER(fp->f_vnode, kn);
1672         VFS_UNLOCK_GIANT(vfslocked);
1673
1674         return error;
1675 }
1676
1677 /*
1678  * Simplified in-kernel wrapper calls for extended attribute access.
1679  * Both calls pass in a NULL credential, authorizing as "kernel" access.
1680  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1681  */
1682 int
1683 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1684     const char *attrname, int *buflen, char *buf, struct thread *td)
1685 {
1686         struct uio      auio;
1687         struct iovec    iov;
1688         int     error;
1689
1690         iov.iov_len = *buflen;
1691         iov.iov_base = buf;
1692
1693         auio.uio_iov = &iov;
1694         auio.uio_iovcnt = 1;
1695         auio.uio_rw = UIO_READ;
1696         auio.uio_segflg = UIO_SYSSPACE;
1697         auio.uio_td = td;
1698         auio.uio_offset = 0;
1699         auio.uio_resid = *buflen;
1700
1701         if ((ioflg & IO_NODELOCKED) == 0)
1702                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1703
1704         ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1705
1706         /* authorize attribute retrieval as kernel */
1707         error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
1708             td);
1709
1710         if ((ioflg & IO_NODELOCKED) == 0)
1711                 VOP_UNLOCK(vp, 0);
1712
1713         if (error == 0) {
1714                 *buflen = *buflen - auio.uio_resid;
1715         }
1716
1717         return (error);
1718 }
1719
1720 /*
1721  * XXX failure mode if partially written?
1722  */
1723 int
1724 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1725     const char *attrname, int buflen, char *buf, struct thread *td)
1726 {
1727         struct uio      auio;
1728         struct iovec    iov;
1729         struct mount    *mp;
1730         int     error;
1731
1732         iov.iov_len = buflen;
1733         iov.iov_base = buf;
1734
1735         auio.uio_iov = &iov;
1736         auio.uio_iovcnt = 1;
1737         auio.uio_rw = UIO_WRITE;
1738         auio.uio_segflg = UIO_SYSSPACE;
1739         auio.uio_td = td;
1740         auio.uio_offset = 0;
1741         auio.uio_resid = buflen;
1742
1743         if ((ioflg & IO_NODELOCKED) == 0) {
1744                 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1745                         return (error);
1746                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1747         }
1748
1749         ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1750
1751         /* authorize attribute setting as kernel */
1752         error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td);
1753
1754         if ((ioflg & IO_NODELOCKED) == 0) {
1755                 vn_finished_write(mp);
1756                 VOP_UNLOCK(vp, 0);
1757         }
1758
1759         return (error);
1760 }
1761
1762 int
1763 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1764     const char *attrname, struct thread *td)
1765 {
1766         struct mount    *mp;
1767         int     error;
1768
1769         if ((ioflg & IO_NODELOCKED) == 0) {
1770                 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1771                         return (error);
1772                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1773         }
1774
1775         ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1776
1777         /* authorize attribute removal as kernel */
1778         error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td);
1779         if (error == EOPNOTSUPP)
1780                 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
1781                     NULL, td);
1782
1783         if ((ioflg & IO_NODELOCKED) == 0) {
1784                 vn_finished_write(mp);
1785                 VOP_UNLOCK(vp, 0);
1786         }
1787
1788         return (error);
1789 }
1790
1791 int
1792 vn_vget_ino(struct vnode *vp, ino_t ino, int lkflags, struct vnode **rvp)
1793 {
1794         struct mount *mp;
1795         int ltype, error;
1796
1797         mp = vp->v_mount;
1798         ltype = VOP_ISLOCKED(vp);
1799         KASSERT(ltype == LK_EXCLUSIVE || ltype == LK_SHARED,
1800             ("vn_vget_ino: vp not locked"));
1801         error = vfs_busy(mp, MBF_NOWAIT);
1802         if (error != 0) {
1803                 vfs_ref(mp);
1804                 VOP_UNLOCK(vp, 0);
1805                 error = vfs_busy(mp, 0);
1806                 vn_lock(vp, ltype | LK_RETRY);
1807                 vfs_rel(mp);
1808                 if (error != 0)
1809                         return (ENOENT);
1810                 if (vp->v_iflag & VI_DOOMED) {
1811                         vfs_unbusy(mp);
1812                         return (ENOENT);
1813                 }
1814         }
1815         VOP_UNLOCK(vp, 0);
1816         error = VFS_VGET(mp, ino, lkflags, rvp);
1817         vfs_unbusy(mp);
1818         vn_lock(vp, ltype | LK_RETRY);
1819         if (vp->v_iflag & VI_DOOMED) {
1820                 if (error == 0)
1821                         vput(*rvp);
1822                 error = ENOENT;
1823         }
1824         return (error);
1825 }
1826
1827 int
1828 vn_rlimit_fsize(const struct vnode *vp, const struct uio *uio,
1829     const struct thread *td)
1830 {
1831
1832         if (vp->v_type != VREG || td == NULL)
1833                 return (0);
1834         PROC_LOCK(td->td_proc);
1835         if ((uoff_t)uio->uio_offset + uio->uio_resid >
1836             lim_cur(td->td_proc, RLIMIT_FSIZE)) {
1837                 kern_psignal(td->td_proc, SIGXFSZ);
1838                 PROC_UNLOCK(td->td_proc);
1839                 return (EFBIG);
1840         }
1841         PROC_UNLOCK(td->td_proc);
1842         return (0);
1843 }
1844
1845 int
1846 vn_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
1847     struct thread *td)
1848 {
1849         struct vnode *vp;
1850         int error, vfslocked;
1851
1852         vp = fp->f_vnode;
1853         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1854 #ifdef AUDIT
1855         vn_lock(vp, LK_SHARED | LK_RETRY);
1856         AUDIT_ARG_VNODE1(vp);
1857         VOP_UNLOCK(vp, 0);
1858 #endif
1859         error = setfmode(td, active_cred, vp, mode);
1860         VFS_UNLOCK_GIANT(vfslocked);
1861         return (error);
1862 }
1863
1864 int
1865 vn_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
1866     struct thread *td)
1867 {
1868         struct vnode *vp;
1869         int error, vfslocked;
1870
1871         vp = fp->f_vnode;
1872         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1873 #ifdef AUDIT
1874         vn_lock(vp, LK_SHARED | LK_RETRY);
1875         AUDIT_ARG_VNODE1(vp);
1876         VOP_UNLOCK(vp, 0);
1877 #endif
1878         error = setfown(td, active_cred, vp, uid, gid);
1879         VFS_UNLOCK_GIANT(vfslocked);
1880         return (error);
1881 }
1882
1883 void
1884 vn_pages_remove(struct vnode *vp, vm_pindex_t start, vm_pindex_t end)
1885 {
1886         vm_object_t object;
1887
1888         if ((object = vp->v_object) == NULL)
1889                 return;
1890         VM_OBJECT_LOCK(object);
1891         vm_object_page_remove(object, start, end, 0);
1892         VM_OBJECT_UNLOCK(object);
1893 }
1894
1895 int
1896 vn_bmap_seekhole(struct vnode *vp, u_long cmd, off_t *off, struct ucred *cred)
1897 {
1898         struct vattr va;
1899         daddr_t bn, bnp;
1900         uint64_t bsize;
1901         off_t noff;
1902         int error;
1903
1904         KASSERT(cmd == FIOSEEKHOLE || cmd == FIOSEEKDATA,
1905             ("Wrong command %lu", cmd));
1906
1907         if (vn_lock(vp, LK_SHARED) != 0)
1908                 return (EBADF);
1909         if (vp->v_type != VREG) {
1910                 error = ENOTTY;
1911                 goto unlock;
1912         }
1913         error = VOP_GETATTR(vp, &va, cred);
1914         if (error != 0)
1915                 goto unlock;
1916         noff = *off;
1917         if (noff >= va.va_size) {
1918                 error = ENXIO;
1919                 goto unlock;
1920         }
1921         bsize = vp->v_mount->mnt_stat.f_iosize;
1922         for (bn = noff / bsize; noff < va.va_size; bn++, noff += bsize) {
1923                 error = VOP_BMAP(vp, bn, NULL, &bnp, NULL, NULL);
1924                 if (error == EOPNOTSUPP) {
1925                         error = ENOTTY;
1926                         goto unlock;
1927                 }
1928                 if ((bnp == -1 && cmd == FIOSEEKHOLE) ||
1929                     (bnp != -1 && cmd == FIOSEEKDATA)) {
1930                         noff = bn * bsize;
1931                         if (noff < *off)
1932                                 noff = *off;
1933                         goto unlock;
1934                 }
1935         }
1936         if (noff > va.va_size)
1937                 noff = va.va_size;
1938         /* noff == va.va_size. There is an implicit hole at the end of file. */
1939         if (cmd == FIOSEEKDATA)
1940                 error = ENXIO;
1941 unlock:
1942         VOP_UNLOCK(vp, 0);
1943         if (error == 0)
1944                 *off = noff;
1945         return (error);
1946 }