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