]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/kern/vfs_vnops.c
MFC r207662 (trasz):
[FreeBSD/stable/8.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/ttycom.h>
60 #include <sys/conf.h>
61 #include <sys/syslog.h>
62 #include <sys/unistd.h>
63
64 #include <security/mac/mac_framework.h>
65
66 static fo_rdwr_t        vn_read;
67 static fo_rdwr_t        vn_write;
68 static fo_truncate_t    vn_truncate;
69 static fo_ioctl_t       vn_ioctl;
70 static fo_poll_t        vn_poll;
71 static fo_kqfilter_t    vn_kqfilter;
72 static fo_stat_t        vn_statfile;
73 static fo_close_t       vn_closefile;
74
75 struct  fileops vnops = {
76         .fo_read = vn_read,
77         .fo_write = vn_write,
78         .fo_truncate = vn_truncate,
79         .fo_ioctl = vn_ioctl,
80         .fo_poll = vn_poll,
81         .fo_kqfilter = vn_kqfilter,
82         .fo_stat = vn_statfile,
83         .fo_close = vn_closefile,
84         .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
85 };
86
87 int
88 vn_open(ndp, flagp, cmode, fp)
89         struct nameidata *ndp;
90         int *flagp, cmode;
91         struct file *fp;
92 {
93         struct thread *td = ndp->ni_cnd.cn_thread;
94
95         return (vn_open_cred(ndp, flagp, cmode, 0, td->td_ucred, fp));
96 }
97
98 /*
99  * Common code for vnode open operations.
100  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
101  * 
102  * Note that this does NOT free nameidata for the successful case,
103  * due to the NDINIT being done elsewhere.
104  */
105 int
106 vn_open_cred(struct nameidata *ndp, int *flagp, int cmode, u_int vn_open_flags,
107     struct ucred *cred, struct file *fp)
108 {
109         struct vnode *vp;
110         struct mount *mp;
111         struct thread *td = ndp->ni_cnd.cn_thread;
112         struct vattr vat;
113         struct vattr *vap = &vat;
114         int fmode, error;
115         accmode_t accmode;
116         int vfslocked, mpsafe;
117
118         mpsafe = ndp->ni_cnd.cn_flags & MPSAFE;
119 restart:
120         vfslocked = 0;
121         fmode = *flagp;
122         if (fmode & O_CREAT) {
123                 ndp->ni_cnd.cn_nameiop = CREATE;
124                 ndp->ni_cnd.cn_flags = ISOPEN | LOCKPARENT | LOCKLEAF |
125                     MPSAFE;
126                 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
127                         ndp->ni_cnd.cn_flags |= FOLLOW;
128                 if (!(vn_open_flags & VN_OPEN_NOAUDIT))
129                         ndp->ni_cnd.cn_flags |= AUDITVNODE1;
130                 bwillwrite();
131                 if ((error = namei(ndp)) != 0)
132                         return (error);
133                 vfslocked = NDHASGIANT(ndp);
134                 if (!mpsafe)
135                         ndp->ni_cnd.cn_flags &= ~MPSAFE;
136                 if (ndp->ni_vp == NULL) {
137                         VATTR_NULL(vap);
138                         vap->va_type = VREG;
139                         vap->va_mode = cmode;
140                         if (fmode & O_EXCL)
141                                 vap->va_vaflags |= VA_EXCLUSIVE;
142                         if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
143                                 NDFREE(ndp, NDF_ONLY_PNBUF);
144                                 vput(ndp->ni_dvp);
145                                 VFS_UNLOCK_GIANT(vfslocked);
146                                 if ((error = vn_start_write(NULL, &mp,
147                                     V_XSLEEP | PCATCH)) != 0)
148                                         return (error);
149                                 goto restart;
150                         }
151 #ifdef MAC
152                         error = mac_vnode_check_create(cred, ndp->ni_dvp,
153                             &ndp->ni_cnd, vap);
154                         if (error == 0)
155 #endif
156                                 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
157                                                    &ndp->ni_cnd, vap);
158                         vput(ndp->ni_dvp);
159                         vn_finished_write(mp);
160                         if (error) {
161                                 VFS_UNLOCK_GIANT(vfslocked);
162                                 NDFREE(ndp, NDF_ONLY_PNBUF);
163                                 return (error);
164                         }
165                         fmode &= ~O_TRUNC;
166                         vp = ndp->ni_vp;
167                 } else {
168                         if (ndp->ni_dvp == ndp->ni_vp)
169                                 vrele(ndp->ni_dvp);
170                         else
171                                 vput(ndp->ni_dvp);
172                         ndp->ni_dvp = NULL;
173                         vp = ndp->ni_vp;
174                         if (fmode & O_EXCL) {
175                                 error = EEXIST;
176                                 goto bad;
177                         }
178                         fmode &= ~O_CREAT;
179                 }
180         } else {
181                 ndp->ni_cnd.cn_nameiop = LOOKUP;
182                 ndp->ni_cnd.cn_flags = ISOPEN |
183                     ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) |
184                     LOCKLEAF | MPSAFE;
185                 if (!(fmode & FWRITE))
186                         ndp->ni_cnd.cn_flags |= LOCKSHARED;
187                 if (!(vn_open_flags & VN_OPEN_NOAUDIT))
188                         ndp->ni_cnd.cn_flags |= AUDITVNODE1;
189                 if ((error = namei(ndp)) != 0)
190                         return (error);
191                 if (!mpsafe)
192                         ndp->ni_cnd.cn_flags &= ~MPSAFE;
193                 vfslocked = NDHASGIANT(ndp);
194                 vp = ndp->ni_vp;
195         }
196         if (vp->v_type == VLNK) {
197                 error = EMLINK;
198                 goto bad;
199         }
200         if (vp->v_type == VSOCK) {
201                 error = EOPNOTSUPP;
202                 goto bad;
203         }
204         accmode = 0;
205         if (fmode & (FWRITE | O_TRUNC)) {
206                 if (vp->v_type == VDIR) {
207                         error = EISDIR;
208                         goto bad;
209                 }
210                 accmode |= VWRITE;
211         }
212         if (fmode & FREAD)
213                 accmode |= VREAD;
214         if (fmode & FEXEC)
215                 accmode |= VEXEC;
216         if ((fmode & O_APPEND) && (fmode & FWRITE))
217                 accmode |= VAPPEND;
218 #ifdef MAC
219         error = mac_vnode_check_open(cred, vp, accmode);
220         if (error)
221                 goto bad;
222 #endif
223         if ((fmode & O_CREAT) == 0) {
224                 if (accmode & VWRITE) {
225                         error = vn_writechk(vp);
226                         if (error)
227                                 goto bad;
228                 }
229                 if (accmode) {
230                         error = VOP_ACCESS(vp, accmode, cred, td);
231                         if (error)
232                                 goto bad;
233                 }
234         }
235         if ((error = VOP_OPEN(vp, fmode, cred, td, fp)) != 0)
236                 goto bad;
237
238         if (fmode & FWRITE)
239                 vp->v_writecount++;
240         *flagp = fmode;
241         ASSERT_VOP_LOCKED(vp, "vn_open_cred");
242         if (!mpsafe)
243                 VFS_UNLOCK_GIANT(vfslocked);
244         return (0);
245 bad:
246         NDFREE(ndp, NDF_ONLY_PNBUF);
247         vput(vp);
248         VFS_UNLOCK_GIANT(vfslocked);
249         *flagp = fmode;
250         ndp->ni_vp = NULL;
251         return (error);
252 }
253
254 /*
255  * Check for write permissions on the specified vnode.
256  * Prototype text segments cannot be written.
257  */
258 int
259 vn_writechk(vp)
260         register struct vnode *vp;
261 {
262
263         ASSERT_VOP_LOCKED(vp, "vn_writechk");
264         /*
265          * If there's shared text associated with
266          * the vnode, try to free it up once.  If
267          * we fail, we can't allow writing.
268          */
269         if (vp->v_vflag & VV_TEXT)
270                 return (ETXTBSY);
271
272         return (0);
273 }
274
275 /*
276  * Vnode close call
277  */
278 int
279 vn_close(vp, flags, file_cred, td)
280         register struct vnode *vp;
281         int flags;
282         struct ucred *file_cred;
283         struct thread *td;
284 {
285         struct mount *mp;
286         int error, lock_flags;
287
288         if (!(flags & FWRITE) && vp->v_mount != NULL &&
289             vp->v_mount->mnt_kern_flag & MNTK_EXTENDED_SHARED)
290                 lock_flags = LK_SHARED;
291         else
292                 lock_flags = LK_EXCLUSIVE;
293
294         VFS_ASSERT_GIANT(vp->v_mount);
295
296         vn_start_write(vp, &mp, V_WAIT);
297         vn_lock(vp, lock_flags | LK_RETRY);
298         if (flags & FWRITE) {
299                 VNASSERT(vp->v_writecount > 0, vp, 
300                     ("vn_close: negative writecount"));
301                 vp->v_writecount--;
302         }
303         error = VOP_CLOSE(vp, flags, file_cred, td);
304         vput(vp);
305         vn_finished_write(mp);
306         return (error);
307 }
308
309 /*
310  * Heuristic to detect sequential operation.
311  */
312 static int
313 sequential_heuristic(struct uio *uio, struct file *fp)
314 {
315
316         if (atomic_load_acq_int(&(fp->f_flag)) & FRDAHEAD)
317                 return (fp->f_seqcount << IO_SEQSHIFT);
318
319         /*
320          * Offset 0 is handled specially.  open() sets f_seqcount to 1 so
321          * that the first I/O is normally considered to be slightly
322          * sequential.  Seeking to offset 0 doesn't change sequentiality
323          * unless previous seeks have reduced f_seqcount to 0, in which
324          * case offset 0 is not special.
325          */
326         if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
327             uio->uio_offset == fp->f_nextoff) {
328                 /*
329                  * f_seqcount is in units of fixed-size blocks so that it
330                  * depends mainly on the amount of sequential I/O and not
331                  * much on the number of sequential I/O's.  The fixed size
332                  * of 16384 is hard-coded here since it is (not quite) just
333                  * a magic size that works well here.  This size is more
334                  * closely related to the best I/O size for real disks than
335                  * to any block size used by software.
336                  */
337                 fp->f_seqcount += howmany(uio->uio_resid, 16384);
338                 if (fp->f_seqcount > IO_SEQMAX)
339                         fp->f_seqcount = IO_SEQMAX;
340                 return (fp->f_seqcount << IO_SEQSHIFT);
341         }
342
343         /* Not sequential.  Quickly draw-down sequentiality. */
344         if (fp->f_seqcount > 1)
345                 fp->f_seqcount = 1;
346         else
347                 fp->f_seqcount = 0;
348         return (0);
349 }
350
351 /*
352  * Package up an I/O request on a vnode into a uio and do it.
353  */
354 int
355 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, active_cred, file_cred,
356     aresid, td)
357         enum uio_rw rw;
358         struct vnode *vp;
359         void *base;
360         int len;
361         off_t offset;
362         enum uio_seg segflg;
363         int ioflg;
364         struct ucred *active_cred;
365         struct ucred *file_cred;
366         int *aresid;
367         struct thread *td;
368 {
369         struct uio auio;
370         struct iovec aiov;
371         struct mount *mp;
372         struct ucred *cred;
373         int error, lock_flags;
374
375         VFS_ASSERT_GIANT(vp->v_mount);
376
377         if ((ioflg & IO_NODELOCKED) == 0) {
378                 mp = NULL;
379                 if (rw == UIO_WRITE) { 
380                         if (vp->v_type != VCHR &&
381                             (error = vn_start_write(vp, &mp, V_WAIT | PCATCH))
382                             != 0)
383                                 return (error);
384                         if (MNT_SHARED_WRITES(mp) ||
385                             ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
386                                 lock_flags = LK_SHARED;
387                         } else {
388                                 lock_flags = LK_EXCLUSIVE;
389                         }
390                         vn_lock(vp, lock_flags | LK_RETRY);
391                 } else
392                         vn_lock(vp, LK_SHARED | LK_RETRY);
393
394         }
395         ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
396         auio.uio_iov = &aiov;
397         auio.uio_iovcnt = 1;
398         aiov.iov_base = base;
399         aiov.iov_len = len;
400         auio.uio_resid = len;
401         auio.uio_offset = offset;
402         auio.uio_segflg = segflg;
403         auio.uio_rw = rw;
404         auio.uio_td = td;
405         error = 0;
406 #ifdef MAC
407         if ((ioflg & IO_NOMACCHECK) == 0) {
408                 if (rw == UIO_READ)
409                         error = mac_vnode_check_read(active_cred, file_cred,
410                             vp);
411                 else
412                         error = mac_vnode_check_write(active_cred, file_cred,
413                             vp);
414         }
415 #endif
416         if (error == 0) {
417                 if (file_cred)
418                         cred = file_cred;
419                 else
420                         cred = active_cred;
421                 if (rw == UIO_READ)
422                         error = VOP_READ(vp, &auio, ioflg, cred);
423                 else
424                         error = VOP_WRITE(vp, &auio, ioflg, cred);
425         }
426         if (aresid)
427                 *aresid = auio.uio_resid;
428         else
429                 if (auio.uio_resid && error == 0)
430                         error = EIO;
431         if ((ioflg & IO_NODELOCKED) == 0) {
432                 if (rw == UIO_WRITE && vp->v_type != VCHR)
433                         vn_finished_write(mp);
434                 VOP_UNLOCK(vp, 0);
435         }
436         return (error);
437 }
438
439 /*
440  * Package up an I/O request on a vnode into a uio and do it.  The I/O
441  * request is split up into smaller chunks and we try to avoid saturating
442  * the buffer cache while potentially holding a vnode locked, so we 
443  * check bwillwrite() before calling vn_rdwr().  We also call uio_yield()
444  * to give other processes a chance to lock the vnode (either other processes
445  * core'ing the same binary, or unrelated processes scanning the directory).
446  */
447 int
448 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, active_cred,
449     file_cred, aresid, td)
450         enum uio_rw rw;
451         struct vnode *vp;
452         void *base;
453         size_t len;
454         off_t offset;
455         enum uio_seg segflg;
456         int ioflg;
457         struct ucred *active_cred;
458         struct ucred *file_cred;
459         size_t *aresid;
460         struct thread *td;
461 {
462         int error = 0;
463         int iaresid;
464
465         VFS_ASSERT_GIANT(vp->v_mount);
466
467         do {
468                 int chunk;
469
470                 /*
471                  * Force `offset' to a multiple of MAXBSIZE except possibly
472                  * for the first chunk, so that filesystems only need to
473                  * write full blocks except possibly for the first and last
474                  * chunks.
475                  */
476                 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
477
478                 if (chunk > len)
479                         chunk = len;
480                 if (rw != UIO_READ && vp->v_type == VREG)
481                         bwillwrite();
482                 iaresid = 0;
483                 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
484                     ioflg, active_cred, file_cred, &iaresid, td);
485                 len -= chunk;   /* aresid calc already includes length */
486                 if (error)
487                         break;
488                 offset += chunk;
489                 base = (char *)base + chunk;
490                 uio_yield();
491         } while (len);
492         if (aresid)
493                 *aresid = len + iaresid;
494         return (error);
495 }
496
497 /*
498  * File table vnode read routine.
499  */
500 static int
501 vn_read(fp, uio, active_cred, flags, td)
502         struct file *fp;
503         struct uio *uio;
504         struct ucred *active_cred;
505         struct thread *td;
506         int flags;
507 {
508         struct vnode *vp;
509         int error, ioflag;
510         struct mtx *mtxp;
511         int vfslocked;
512
513         KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
514             uio->uio_td, td));
515         mtxp = NULL;
516         vp = fp->f_vnode;
517         ioflag = 0;
518         if (fp->f_flag & FNONBLOCK)
519                 ioflag |= IO_NDELAY;
520         if (fp->f_flag & O_DIRECT)
521                 ioflag |= IO_DIRECT;
522         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
523         /*
524          * According to McKusick the vn lock was protecting f_offset here.
525          * It is now protected by the FOFFSET_LOCKED flag.
526          */
527         if ((flags & FOF_OFFSET) == 0) {
528                 mtxp = mtx_pool_find(mtxpool_sleep, fp);
529                 mtx_lock(mtxp);
530                 while(fp->f_vnread_flags & FOFFSET_LOCKED) {
531                         fp->f_vnread_flags |= FOFFSET_LOCK_WAITING;
532                         msleep(&fp->f_vnread_flags, mtxp, PUSER -1,
533                             "vnread offlock", 0);
534                 }
535                 fp->f_vnread_flags |= FOFFSET_LOCKED;
536                 mtx_unlock(mtxp);
537                 vn_lock(vp, LK_SHARED | LK_RETRY);
538                 uio->uio_offset = fp->f_offset;
539         } else
540                 vn_lock(vp, LK_SHARED | LK_RETRY);
541
542         ioflag |= sequential_heuristic(uio, fp);
543
544 #ifdef MAC
545         error = mac_vnode_check_read(active_cred, fp->f_cred, vp);
546         if (error == 0)
547 #endif
548                 error = VOP_READ(vp, uio, ioflag, fp->f_cred);
549         if ((flags & FOF_OFFSET) == 0) {
550                 fp->f_offset = uio->uio_offset;
551                 mtx_lock(mtxp);
552                 if (fp->f_vnread_flags & FOFFSET_LOCK_WAITING)
553                         wakeup(&fp->f_vnread_flags);
554                 fp->f_vnread_flags = 0;
555                 mtx_unlock(mtxp);
556         }
557         fp->f_nextoff = uio->uio_offset;
558         VOP_UNLOCK(vp, 0);
559         VFS_UNLOCK_GIANT(vfslocked);
560         return (error);
561 }
562
563 /*
564  * File table vnode write routine.
565  */
566 static int
567 vn_write(fp, uio, active_cred, flags, td)
568         struct file *fp;
569         struct uio *uio;
570         struct ucred *active_cred;
571         struct thread *td;
572         int flags;
573 {
574         struct vnode *vp;
575         struct mount *mp;
576         int error, ioflag, lock_flags;
577         int vfslocked;
578
579         KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
580             uio->uio_td, td));
581         vp = fp->f_vnode;
582         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
583         if (vp->v_type == VREG)
584                 bwillwrite();
585         ioflag = IO_UNIT;
586         if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
587                 ioflag |= IO_APPEND;
588         if (fp->f_flag & FNONBLOCK)
589                 ioflag |= IO_NDELAY;
590         if (fp->f_flag & O_DIRECT)
591                 ioflag |= IO_DIRECT;
592         if ((fp->f_flag & O_FSYNC) ||
593             (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
594                 ioflag |= IO_SYNC;
595         mp = NULL;
596         if (vp->v_type != VCHR &&
597             (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
598                 goto unlock;
599  
600         if ((MNT_SHARED_WRITES(mp) ||
601             ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) &&
602             (flags & FOF_OFFSET) != 0) {
603                 lock_flags = LK_SHARED;
604         } else {
605                 lock_flags = LK_EXCLUSIVE;
606         }
607
608         vn_lock(vp, lock_flags | LK_RETRY);
609         if ((flags & FOF_OFFSET) == 0)
610                 uio->uio_offset = fp->f_offset;
611         ioflag |= sequential_heuristic(uio, fp);
612 #ifdef MAC
613         error = mac_vnode_check_write(active_cred, fp->f_cred, vp);
614         if (error == 0)
615 #endif
616                 error = VOP_WRITE(vp, uio, ioflag, fp->f_cred);
617         if ((flags & FOF_OFFSET) == 0)
618                 fp->f_offset = uio->uio_offset;
619         fp->f_nextoff = uio->uio_offset;
620         VOP_UNLOCK(vp, 0);
621         if (vp->v_type != VCHR)
622                 vn_finished_write(mp);
623 unlock:
624         VFS_UNLOCK_GIANT(vfslocked);
625         return (error);
626 }
627
628 /*
629  * File table truncate routine.
630  */
631 static int
632 vn_truncate(fp, length, active_cred, td)
633         struct file *fp;
634         off_t length;
635         struct ucred *active_cred;
636         struct thread *td;
637 {
638         struct vattr vattr;
639         struct mount *mp;
640         struct vnode *vp;
641         int vfslocked;
642         int error;
643
644         vp = fp->f_vnode;
645         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
646         error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
647         if (error) {
648                 VFS_UNLOCK_GIANT(vfslocked);
649                 return (error);
650         }
651         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
652         if (vp->v_type == VDIR) {
653                 error = EISDIR;
654                 goto out;
655         }
656 #ifdef MAC
657         error = mac_vnode_check_write(active_cred, fp->f_cred, vp);
658         if (error)
659                 goto out;
660 #endif
661         error = vn_writechk(vp);
662         if (error == 0) {
663                 VATTR_NULL(&vattr);
664                 vattr.va_size = length;
665                 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
666         }
667 out:
668         VOP_UNLOCK(vp, 0);
669         vn_finished_write(mp);
670         VFS_UNLOCK_GIANT(vfslocked);
671         return (error);
672 }
673
674 /*
675  * File table vnode stat routine.
676  */
677 static int
678 vn_statfile(fp, sb, active_cred, td)
679         struct file *fp;
680         struct stat *sb;
681         struct ucred *active_cred;
682         struct thread *td;
683 {
684         struct vnode *vp = fp->f_vnode;
685         int vfslocked;
686         int error;
687
688         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
689         vn_lock(vp, LK_SHARED | LK_RETRY);
690         error = vn_stat(vp, sb, active_cred, fp->f_cred, td);
691         VOP_UNLOCK(vp, 0);
692         VFS_UNLOCK_GIANT(vfslocked);
693
694         return (error);
695 }
696
697 /*
698  * Stat a vnode; implementation for the stat syscall
699  */
700 int
701 vn_stat(vp, sb, active_cred, file_cred, td)
702         struct vnode *vp;
703         register struct stat *sb;
704         struct ucred *active_cred;
705         struct ucred *file_cred;
706         struct thread *td;
707 {
708         struct vattr vattr;
709         register struct vattr *vap;
710         int error;
711         u_short mode;
712
713 #ifdef MAC
714         error = mac_vnode_check_stat(active_cred, file_cred, vp);
715         if (error)
716                 return (error);
717 #endif
718
719         vap = &vattr;
720
721         /*
722          * Initialize defaults for new and unusual fields, so that file
723          * systems which don't support these fields don't need to know
724          * about them.
725          */
726         vap->va_birthtime.tv_sec = -1;
727         vap->va_birthtime.tv_nsec = 0;
728         vap->va_fsid = VNOVAL;
729         vap->va_rdev = NODEV;
730
731         error = VOP_GETATTR(vp, vap, active_cred);
732         if (error)
733                 return (error);
734
735         /*
736          * Zero the spare stat fields
737          */
738         bzero(sb, sizeof *sb);
739
740         /*
741          * Copy from vattr table
742          */
743         if (vap->va_fsid != VNOVAL)
744                 sb->st_dev = vap->va_fsid;
745         else
746                 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
747         sb->st_ino = vap->va_fileid;
748         mode = vap->va_mode;
749         switch (vap->va_type) {
750         case VREG:
751                 mode |= S_IFREG;
752                 break;
753         case VDIR:
754                 mode |= S_IFDIR;
755                 break;
756         case VBLK:
757                 mode |= S_IFBLK;
758                 break;
759         case VCHR:
760                 mode |= S_IFCHR;
761                 break;
762         case VLNK:
763                 mode |= S_IFLNK;
764                 break;
765         case VSOCK:
766                 mode |= S_IFSOCK;
767                 break;
768         case VFIFO:
769                 mode |= S_IFIFO;
770                 break;
771         default:
772                 return (EBADF);
773         };
774         sb->st_mode = mode;
775         sb->st_nlink = vap->va_nlink;
776         sb->st_uid = vap->va_uid;
777         sb->st_gid = vap->va_gid;
778         sb->st_rdev = vap->va_rdev;
779         if (vap->va_size > OFF_MAX)
780                 return (EOVERFLOW);
781         sb->st_size = vap->va_size;
782         sb->st_atimespec = vap->va_atime;
783         sb->st_mtimespec = vap->va_mtime;
784         sb->st_ctimespec = vap->va_ctime;
785         sb->st_birthtimespec = vap->va_birthtime;
786
787         /*
788          * According to www.opengroup.org, the meaning of st_blksize is 
789          *   "a filesystem-specific preferred I/O block size for this 
790          *    object.  In some filesystem types, this may vary from file
791          *    to file"
792          * Use miminum/default of PAGE_SIZE (e.g. for VCHR).
793          */
794
795         sb->st_blksize = max(PAGE_SIZE, vap->va_blocksize);
796         
797         sb->st_flags = vap->va_flags;
798         if (priv_check(td, PRIV_VFS_GENERATION))
799                 sb->st_gen = 0;
800         else
801                 sb->st_gen = vap->va_gen;
802
803         sb->st_blocks = vap->va_bytes / S_BLKSIZE;
804         return (0);
805 }
806
807 /*
808  * File table vnode ioctl routine.
809  */
810 static int
811 vn_ioctl(fp, com, data, active_cred, td)
812         struct file *fp;
813         u_long com;
814         void *data;
815         struct ucred *active_cred;
816         struct thread *td;
817 {
818         struct vnode *vp = fp->f_vnode;
819         struct vattr vattr;
820         int vfslocked;
821         int error;
822
823         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
824         error = ENOTTY;
825         switch (vp->v_type) {
826         case VREG:
827         case VDIR:
828                 if (com == FIONREAD) {
829                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
830                         error = VOP_GETATTR(vp, &vattr, active_cred);
831                         VOP_UNLOCK(vp, 0);
832                         if (!error)
833                                 *(int *)data = vattr.va_size - fp->f_offset;
834                 }
835                 if (com == FIONBIO || com == FIOASYNC)  /* XXX */
836                         error = 0;
837                 else
838                         error = VOP_IOCTL(vp, com, data, fp->f_flag,
839                             active_cred, td);
840                 break;
841
842         default:
843                 break;
844         }
845         VFS_UNLOCK_GIANT(vfslocked);
846         return (error);
847 }
848
849 /*
850  * File table vnode poll routine.
851  */
852 static int
853 vn_poll(fp, events, active_cred, td)
854         struct file *fp;
855         int events;
856         struct ucred *active_cred;
857         struct thread *td;
858 {
859         struct vnode *vp;
860         int vfslocked;
861         int error;
862
863         vp = fp->f_vnode;
864         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
865 #ifdef MAC
866         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
867         error = mac_vnode_check_poll(active_cred, fp->f_cred, vp);
868         VOP_UNLOCK(vp, 0);
869         if (!error)
870 #endif
871
872         error = VOP_POLL(vp, events, fp->f_cred, td);
873         VFS_UNLOCK_GIANT(vfslocked);
874         return (error);
875 }
876
877 /*
878  * Acquire the requested lock and then check for validity.  LK_RETRY
879  * permits vn_lock to return doomed vnodes.
880  */
881 int
882 _vn_lock(struct vnode *vp, int flags, char *file, int line)
883 {
884         int error;
885
886         VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
887             ("vn_lock called with no locktype."));
888         do {
889 #ifdef DEBUG_VFS_LOCKS
890                 KASSERT(vp->v_holdcnt != 0,
891                     ("vn_lock %p: zero hold count", vp));
892 #endif
893                 error = VOP_LOCK1(vp, flags, file, line);
894                 flags &= ~LK_INTERLOCK; /* Interlock is always dropped. */
895                 KASSERT((flags & LK_RETRY) == 0 || error == 0,
896                     ("LK_RETRY set with incompatible flags (0x%x) or an error occured (%d)",
897                     flags, error));
898                 /*
899                  * Callers specify LK_RETRY if they wish to get dead vnodes.
900                  * If RETRY is not set, we return ENOENT instead.
901                  */
902                 if (error == 0 && vp->v_iflag & VI_DOOMED &&
903                     (flags & LK_RETRY) == 0) {
904                         VOP_UNLOCK(vp, 0);
905                         error = ENOENT;
906                         break;
907                 }
908         } while (flags & LK_RETRY && error != 0);
909         return (error);
910 }
911
912 /*
913  * File table vnode close routine.
914  */
915 static int
916 vn_closefile(fp, td)
917         struct file *fp;
918         struct thread *td;
919 {
920         struct vnode *vp;
921         struct flock lf;
922         int vfslocked;
923         int error;
924
925         vp = fp->f_vnode;
926
927         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
928         if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK) {
929                 lf.l_whence = SEEK_SET;
930                 lf.l_start = 0;
931                 lf.l_len = 0;
932                 lf.l_type = F_UNLCK;
933                 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
934         }
935
936         fp->f_ops = &badfileops;
937
938         error = vn_close(vp, fp->f_flag, fp->f_cred, td);
939         VFS_UNLOCK_GIANT(vfslocked);
940         return (error);
941 }
942
943 /*
944  * Preparing to start a filesystem write operation. If the operation is
945  * permitted, then we bump the count of operations in progress and
946  * proceed. If a suspend request is in progress, we wait until the
947  * suspension is over, and then proceed.
948  */
949 int
950 vn_start_write(vp, mpp, flags)
951         struct vnode *vp;
952         struct mount **mpp;
953         int flags;
954 {
955         struct mount *mp;
956         int error;
957
958         error = 0;
959         /*
960          * If a vnode is provided, get and return the mount point that
961          * to which it will write.
962          */
963         if (vp != NULL) {
964                 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
965                         *mpp = NULL;
966                         if (error != EOPNOTSUPP)
967                                 return (error);
968                         return (0);
969                 }
970         }
971         if ((mp = *mpp) == NULL)
972                 return (0);
973
974         /*
975          * VOP_GETWRITEMOUNT() returns with the mp refcount held through
976          * a vfs_ref().
977          * As long as a vnode is not provided we need to acquire a
978          * refcount for the provided mountpoint too, in order to
979          * emulate a vfs_ref().
980          */
981         MNT_ILOCK(mp);
982         if (vp == NULL)
983                 MNT_REF(mp);
984
985         /*
986          * Check on status of suspension.
987          */
988         if ((curthread->td_pflags & TDP_IGNSUSP) == 0 ||
989             mp->mnt_susp_owner != curthread) {
990                 while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
991                         if (flags & V_NOWAIT) {
992                                 error = EWOULDBLOCK;
993                                 goto unlock;
994                         }
995                         error = msleep(&mp->mnt_flag, MNT_MTX(mp),
996                             (PUSER - 1) | (flags & PCATCH), "suspfs", 0);
997                         if (error)
998                                 goto unlock;
999                 }
1000         }
1001         if (flags & V_XSLEEP)
1002                 goto unlock;
1003         mp->mnt_writeopcount++;
1004 unlock:
1005         if (error != 0 || (flags & V_XSLEEP) != 0)
1006                 MNT_REL(mp);
1007         MNT_IUNLOCK(mp);
1008         return (error);
1009 }
1010
1011 /*
1012  * Secondary suspension. Used by operations such as vop_inactive
1013  * routines that are needed by the higher level functions. These
1014  * are allowed to proceed until all the higher level functions have
1015  * completed (indicated by mnt_writeopcount dropping to zero). At that
1016  * time, these operations are halted until the suspension is over.
1017  */
1018 int
1019 vn_start_secondary_write(vp, mpp, flags)
1020         struct vnode *vp;
1021         struct mount **mpp;
1022         int flags;
1023 {
1024         struct mount *mp;
1025         int error;
1026
1027  retry:
1028         if (vp != NULL) {
1029                 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
1030                         *mpp = NULL;
1031                         if (error != EOPNOTSUPP)
1032                                 return (error);
1033                         return (0);
1034                 }
1035         }
1036         /*
1037          * If we are not suspended or have not yet reached suspended
1038          * mode, then let the operation proceed.
1039          */
1040         if ((mp = *mpp) == NULL)
1041                 return (0);
1042
1043         /*
1044          * VOP_GETWRITEMOUNT() returns with the mp refcount held through
1045          * a vfs_ref().
1046          * As long as a vnode is not provided we need to acquire a
1047          * refcount for the provided mountpoint too, in order to
1048          * emulate a vfs_ref().
1049          */
1050         MNT_ILOCK(mp);
1051         if (vp == NULL)
1052                 MNT_REF(mp);
1053         if ((mp->mnt_kern_flag & (MNTK_SUSPENDED | MNTK_SUSPEND2)) == 0) {
1054                 mp->mnt_secondary_writes++;
1055                 mp->mnt_secondary_accwrites++;
1056                 MNT_IUNLOCK(mp);
1057                 return (0);
1058         }
1059         if (flags & V_NOWAIT) {
1060                 MNT_REL(mp);
1061                 MNT_IUNLOCK(mp);
1062                 return (EWOULDBLOCK);
1063         }
1064         /*
1065          * Wait for the suspension to finish.
1066          */
1067         error = msleep(&mp->mnt_flag, MNT_MTX(mp),
1068                        (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0);
1069         vfs_rel(mp);
1070         if (error == 0)
1071                 goto retry;
1072         return (error);
1073 }
1074
1075 /*
1076  * Filesystem write operation has completed. If we are suspending and this
1077  * operation is the last one, notify the suspender that the suspension is
1078  * now in effect.
1079  */
1080 void
1081 vn_finished_write(mp)
1082         struct mount *mp;
1083 {
1084         if (mp == NULL)
1085                 return;
1086         MNT_ILOCK(mp);
1087         MNT_REL(mp);
1088         mp->mnt_writeopcount--;
1089         if (mp->mnt_writeopcount < 0)
1090                 panic("vn_finished_write: neg cnt");
1091         if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1092             mp->mnt_writeopcount <= 0)
1093                 wakeup(&mp->mnt_writeopcount);
1094         MNT_IUNLOCK(mp);
1095 }
1096
1097
1098 /*
1099  * Filesystem secondary write operation has completed. If we are
1100  * suspending and this operation is the last one, notify the suspender
1101  * that the suspension is now in effect.
1102  */
1103 void
1104 vn_finished_secondary_write(mp)
1105         struct mount *mp;
1106 {
1107         if (mp == NULL)
1108                 return;
1109         MNT_ILOCK(mp);
1110         MNT_REL(mp);
1111         mp->mnt_secondary_writes--;
1112         if (mp->mnt_secondary_writes < 0)
1113                 panic("vn_finished_secondary_write: neg cnt");
1114         if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1115             mp->mnt_secondary_writes <= 0)
1116                 wakeup(&mp->mnt_secondary_writes);
1117         MNT_IUNLOCK(mp);
1118 }
1119
1120
1121
1122 /*
1123  * Request a filesystem to suspend write operations.
1124  */
1125 int
1126 vfs_write_suspend(mp)
1127         struct mount *mp;
1128 {
1129         int error;
1130
1131         MNT_ILOCK(mp);
1132         if (mp->mnt_susp_owner == curthread) {
1133                 MNT_IUNLOCK(mp);
1134                 return (EALREADY);
1135         }
1136         while (mp->mnt_kern_flag & MNTK_SUSPEND)
1137                 msleep(&mp->mnt_flag, MNT_MTX(mp), PUSER - 1, "wsuspfs", 0);
1138         mp->mnt_kern_flag |= MNTK_SUSPEND;
1139         mp->mnt_susp_owner = curthread;
1140         if (mp->mnt_writeopcount > 0)
1141                 (void) msleep(&mp->mnt_writeopcount, 
1142                     MNT_MTX(mp), (PUSER - 1)|PDROP, "suspwt", 0);
1143         else
1144                 MNT_IUNLOCK(mp);
1145         if ((error = VFS_SYNC(mp, MNT_SUSPEND)) != 0)
1146                 vfs_write_resume(mp);
1147         return (error);
1148 }
1149
1150 /*
1151  * Request a filesystem to resume write operations.
1152  */
1153 void
1154 vfs_write_resume(mp)
1155         struct mount *mp;
1156 {
1157
1158         MNT_ILOCK(mp);
1159         if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1160                 KASSERT(mp->mnt_susp_owner == curthread, ("mnt_susp_owner"));
1161                 mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPEND2 |
1162                                        MNTK_SUSPENDED);
1163                 mp->mnt_susp_owner = NULL;
1164                 wakeup(&mp->mnt_writeopcount);
1165                 wakeup(&mp->mnt_flag);
1166                 curthread->td_pflags &= ~TDP_IGNSUSP;
1167                 MNT_IUNLOCK(mp);
1168                 VFS_SUSP_CLEAN(mp);
1169         } else
1170                 MNT_IUNLOCK(mp);
1171 }
1172
1173 /*
1174  * Implement kqueues for files by translating it to vnode operation.
1175  */
1176 static int
1177 vn_kqfilter(struct file *fp, struct knote *kn)
1178 {
1179         int vfslocked;
1180         int error;
1181
1182         vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
1183         error = VOP_KQFILTER(fp->f_vnode, kn);
1184         VFS_UNLOCK_GIANT(vfslocked);
1185
1186         return error;
1187 }
1188
1189 /*
1190  * Simplified in-kernel wrapper calls for extended attribute access.
1191  * Both calls pass in a NULL credential, authorizing as "kernel" access.
1192  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1193  */
1194 int
1195 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1196     const char *attrname, int *buflen, char *buf, struct thread *td)
1197 {
1198         struct uio      auio;
1199         struct iovec    iov;
1200         int     error;
1201
1202         iov.iov_len = *buflen;
1203         iov.iov_base = buf;
1204
1205         auio.uio_iov = &iov;
1206         auio.uio_iovcnt = 1;
1207         auio.uio_rw = UIO_READ;
1208         auio.uio_segflg = UIO_SYSSPACE;
1209         auio.uio_td = td;
1210         auio.uio_offset = 0;
1211         auio.uio_resid = *buflen;
1212
1213         if ((ioflg & IO_NODELOCKED) == 0)
1214                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1215
1216         ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1217
1218         /* authorize attribute retrieval as kernel */
1219         error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
1220             td);
1221
1222         if ((ioflg & IO_NODELOCKED) == 0)
1223                 VOP_UNLOCK(vp, 0);
1224
1225         if (error == 0) {
1226                 *buflen = *buflen - auio.uio_resid;
1227         }
1228
1229         return (error);
1230 }
1231
1232 /*
1233  * XXX failure mode if partially written?
1234  */
1235 int
1236 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1237     const char *attrname, int buflen, char *buf, struct thread *td)
1238 {
1239         struct uio      auio;
1240         struct iovec    iov;
1241         struct mount    *mp;
1242         int     error;
1243
1244         iov.iov_len = buflen;
1245         iov.iov_base = buf;
1246
1247         auio.uio_iov = &iov;
1248         auio.uio_iovcnt = 1;
1249         auio.uio_rw = UIO_WRITE;
1250         auio.uio_segflg = UIO_SYSSPACE;
1251         auio.uio_td = td;
1252         auio.uio_offset = 0;
1253         auio.uio_resid = buflen;
1254
1255         if ((ioflg & IO_NODELOCKED) == 0) {
1256                 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1257                         return (error);
1258                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1259         }
1260
1261         ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1262
1263         /* authorize attribute setting as kernel */
1264         error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td);
1265
1266         if ((ioflg & IO_NODELOCKED) == 0) {
1267                 vn_finished_write(mp);
1268                 VOP_UNLOCK(vp, 0);
1269         }
1270
1271         return (error);
1272 }
1273
1274 int
1275 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1276     const char *attrname, struct thread *td)
1277 {
1278         struct mount    *mp;
1279         int     error;
1280
1281         if ((ioflg & IO_NODELOCKED) == 0) {
1282                 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1283                         return (error);
1284                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1285         }
1286
1287         ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1288
1289         /* authorize attribute removal as kernel */
1290         error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td);
1291         if (error == EOPNOTSUPP)
1292                 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
1293                     NULL, td);
1294
1295         if ((ioflg & IO_NODELOCKED) == 0) {
1296                 vn_finished_write(mp);
1297                 VOP_UNLOCK(vp, 0);
1298         }
1299
1300         return (error);
1301 }
1302
1303 int
1304 vn_vget_ino(struct vnode *vp, ino_t ino, int lkflags, struct vnode **rvp)
1305 {
1306         struct mount *mp;
1307         int ltype, error;
1308
1309         mp = vp->v_mount;
1310         ltype = VOP_ISLOCKED(vp);
1311         KASSERT(ltype == LK_EXCLUSIVE || ltype == LK_SHARED,
1312             ("vn_vget_ino: vp not locked"));
1313         error = vfs_busy(mp, MBF_NOWAIT);
1314         if (error != 0) {
1315                 vfs_ref(mp);
1316                 VOP_UNLOCK(vp, 0);
1317                 error = vfs_busy(mp, 0);
1318                 vn_lock(vp, ltype | LK_RETRY);
1319                 vfs_rel(mp);
1320                 if (error != 0)
1321                         return (ENOENT);
1322                 if (vp->v_iflag & VI_DOOMED) {
1323                         vfs_unbusy(mp);
1324                         return (ENOENT);
1325                 }
1326         }
1327         VOP_UNLOCK(vp, 0);
1328         error = VFS_VGET(mp, ino, lkflags, rvp);
1329         vfs_unbusy(mp);
1330         vn_lock(vp, ltype | LK_RETRY);
1331         if (vp->v_iflag & VI_DOOMED) {
1332                 if (error == 0)
1333                         vput(*rvp);
1334                 error = ENOENT;
1335         }
1336         return (error);
1337 }
1338
1339 int
1340 vn_rlimit_fsize(const struct vnode *vp, const struct uio *uio, const struct thread *td)
1341 {
1342         if (vp->v_type != VREG || td == NULL)
1343                 return (0);
1344
1345         PROC_LOCK(td->td_proc);
1346         if (uio->uio_offset + uio->uio_resid >
1347             lim_cur(td->td_proc, RLIMIT_FSIZE)) {
1348                 psignal(td->td_proc, SIGXFSZ);
1349                 PROC_UNLOCK(td->td_proc);
1350                 return (EFBIG);
1351         }
1352         PROC_UNLOCK(td->td_proc);
1353
1354         return (0);
1355 }