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