vfs_vnops.c revision 1.160.2.1 1 /* $NetBSD: vfs_vnops.c,v 1.160.2.1 2009/01/19 13:19:40 skrll Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)vfs_vnops.c 8.14 (Berkeley) 6/15/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.160.2.1 2009/01/19 13:19:40 skrll Exp $");
41
42 #include "veriexec.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/file.h>
48 #include <sys/stat.h>
49 #include <sys/buf.h>
50 #include <sys/proc.h>
51 #include <sys/mount.h>
52 #include <sys/namei.h>
53 #include <sys/vnode.h>
54 #include <sys/ioctl.h>
55 #include <sys/tty.h>
56 #include <sys/poll.h>
57 #include <sys/kauth.h>
58 #include <sys/syslog.h>
59 #include <sys/fstrans.h>
60 #include <sys/atomic.h>
61 #include <sys/filedesc.h>
62 #include <sys/wapbl.h>
63
64 #include <miscfs/specfs/specdev.h>
65
66 #include <uvm/uvm_extern.h>
67 #include <uvm/uvm_readahead.h>
68
69 #ifdef UNION
70 #include <fs/union/union.h>
71 #endif
72
73 int (*vn_union_readdir_hook) (struct vnode **, struct file *, struct lwp *);
74
75 #include <sys/verified_exec.h>
76
77 static int vn_read(file_t *fp, off_t *offset, struct uio *uio,
78 kauth_cred_t cred, int flags);
79 static int vn_write(file_t *fp, off_t *offset, struct uio *uio,
80 kauth_cred_t cred, int flags);
81 static int vn_closefile(file_t *fp);
82 static int vn_poll(file_t *fp, int events);
83 static int vn_fcntl(file_t *fp, u_int com, void *data);
84 static int vn_statfile(file_t *fp, struct stat *sb);
85 static int vn_ioctl(file_t *fp, u_long com, void *data);
86
87 const struct fileops vnops = {
88 vn_read, vn_write, vn_ioctl, vn_fcntl, vn_poll,
89 vn_statfile, vn_closefile, vn_kqfilter
90 };
91
92 /*
93 * Common code for vnode open operations.
94 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
95 */
96 int
97 vn_open(struct nameidata *ndp, int fmode, int cmode)
98 {
99 struct vnode *vp;
100 struct lwp *l = curlwp;
101 kauth_cred_t cred = l->l_cred;
102 struct vattr va;
103 int error;
104 char *path;
105
106 ndp->ni_cnd.cn_flags &= TRYEMULROOT;
107
108 if (fmode & O_CREAT) {
109 ndp->ni_cnd.cn_nameiop = CREATE;
110 ndp->ni_cnd.cn_flags |= LOCKPARENT | LOCKLEAF;
111 if ((fmode & O_EXCL) == 0 &&
112 ((fmode & O_NOFOLLOW) == 0))
113 ndp->ni_cnd.cn_flags |= FOLLOW;
114 } else {
115 ndp->ni_cnd.cn_nameiop = LOOKUP;
116 ndp->ni_cnd.cn_flags |= LOCKLEAF;
117 if ((fmode & O_NOFOLLOW) == 0)
118 ndp->ni_cnd.cn_flags |= FOLLOW;
119 }
120
121 VERIEXEC_PATH_GET(ndp->ni_dirp, ndp->ni_segflg, ndp->ni_dirp, path);
122
123 error = namei(ndp);
124 if (error)
125 goto out;
126
127 vp = ndp->ni_vp;
128
129 #if NVERIEXEC > 0
130 error = veriexec_openchk(l, ndp->ni_vp, ndp->ni_dirp, fmode);
131 if (error)
132 goto bad;
133 #endif /* NVERIEXEC > 0 */
134
135 if (fmode & O_CREAT) {
136 if (ndp->ni_vp == NULL) {
137 VATTR_NULL(&va);
138 va.va_type = VREG;
139 va.va_mode = cmode;
140 if (fmode & O_EXCL)
141 va.va_vaflags |= VA_EXCLUSIVE;
142 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
143 &ndp->ni_cnd, &va);
144 if (error)
145 goto out;
146 fmode &= ~O_TRUNC;
147 vp = ndp->ni_vp;
148 } else {
149 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
150 if (ndp->ni_dvp == ndp->ni_vp)
151 vrele(ndp->ni_dvp);
152 else
153 vput(ndp->ni_dvp);
154 ndp->ni_dvp = NULL;
155 vp = ndp->ni_vp;
156 if (fmode & O_EXCL) {
157 error = EEXIST;
158 goto bad;
159 }
160 fmode &= ~O_CREAT;
161 }
162 } else {
163 vp = ndp->ni_vp;
164 }
165 if (vp->v_type == VSOCK) {
166 error = EOPNOTSUPP;
167 goto bad;
168 }
169 if (ndp->ni_vp->v_type == VLNK) {
170 error = EFTYPE;
171 goto bad;
172 }
173
174 if ((fmode & O_CREAT) == 0) {
175 error = vn_openchk(vp, cred, fmode);
176 if (error != 0)
177 goto bad;
178 }
179
180 if (fmode & O_TRUNC) {
181 VOP_UNLOCK(vp, 0); /* XXX */
182
183 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
184 VATTR_NULL(&va);
185 va.va_size = 0;
186 error = VOP_SETATTR(vp, &va, cred);
187 if (error != 0)
188 goto bad;
189 }
190 if ((error = VOP_OPEN(vp, fmode, cred)) != 0)
191 goto bad;
192 if (fmode & FWRITE) {
193 mutex_enter(&vp->v_interlock);
194 vp->v_writecount++;
195 mutex_exit(&vp->v_interlock);
196 }
197
198 bad:
199 if (error)
200 vput(vp);
201 out:
202 VERIEXEC_PATH_PUT(path);
203 return (error);
204 }
205
206 /*
207 * Check for write permissions on the specified vnode.
208 * Prototype text segments cannot be written.
209 */
210 int
211 vn_writechk(struct vnode *vp)
212 {
213
214 /*
215 * If the vnode is in use as a process's text,
216 * we can't allow writing.
217 */
218 if (vp->v_iflag & VI_TEXT)
219 return (ETXTBSY);
220 return (0);
221 }
222
223 int
224 vn_openchk(struct vnode *vp, kauth_cred_t cred, int fflags)
225 {
226 int permbits = 0;
227 int error;
228
229 if ((fflags & FREAD) != 0) {
230 permbits = VREAD;
231 }
232 if ((fflags & (FWRITE | O_TRUNC)) != 0) {
233 permbits |= VWRITE;
234 if (vp->v_type == VDIR) {
235 error = EISDIR;
236 goto bad;
237 }
238 error = vn_writechk(vp);
239 if (error != 0)
240 goto bad;
241 }
242 error = VOP_ACCESS(vp, permbits, cred);
243 bad:
244 return error;
245 }
246
247 /*
248 * Mark a vnode as having executable mappings.
249 */
250 void
251 vn_markexec(struct vnode *vp)
252 {
253
254 if ((vp->v_iflag & VI_EXECMAP) != 0) {
255 /* Safe unlocked, as long as caller holds a reference. */
256 return;
257 }
258
259 mutex_enter(&vp->v_interlock);
260 if ((vp->v_iflag & VI_EXECMAP) == 0) {
261 atomic_add_int(&uvmexp.filepages, -vp->v_uobj.uo_npages);
262 atomic_add_int(&uvmexp.execpages, vp->v_uobj.uo_npages);
263 vp->v_iflag |= VI_EXECMAP;
264 }
265 mutex_exit(&vp->v_interlock);
266 }
267
268 /*
269 * Mark a vnode as being the text of a process.
270 * Fail if the vnode is currently writable.
271 */
272 int
273 vn_marktext(struct vnode *vp)
274 {
275
276 if ((vp->v_iflag & (VI_TEXT|VI_EXECMAP)) == (VI_TEXT|VI_EXECMAP)) {
277 /* Safe unlocked, as long as caller holds a reference. */
278 return (0);
279 }
280
281 mutex_enter(&vp->v_interlock);
282 if (vp->v_writecount != 0) {
283 KASSERT((vp->v_iflag & VI_TEXT) == 0);
284 mutex_exit(&vp->v_interlock);
285 return (ETXTBSY);
286 }
287 if ((vp->v_iflag & VI_EXECMAP) == 0) {
288 atomic_add_int(&uvmexp.filepages, -vp->v_uobj.uo_npages);
289 atomic_add_int(&uvmexp.execpages, vp->v_uobj.uo_npages);
290 }
291 vp->v_iflag |= (VI_TEXT | VI_EXECMAP);
292 mutex_exit(&vp->v_interlock);
293 return (0);
294 }
295
296 /*
297 * Vnode close call
298 *
299 * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
300 */
301 int
302 vn_close(struct vnode *vp, int flags, kauth_cred_t cred)
303 {
304 int error;
305
306 if (flags & FWRITE) {
307 mutex_enter(&vp->v_interlock);
308 vp->v_writecount--;
309 mutex_exit(&vp->v_interlock);
310 }
311 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
312 error = VOP_CLOSE(vp, flags, cred);
313 vput(vp);
314 return (error);
315 }
316
317 /*
318 * Package up an I/O request on a vnode into a uio and do it.
319 */
320 int
321 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
322 enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid,
323 struct lwp *l)
324 {
325 struct uio auio;
326 struct iovec aiov;
327 int error;
328
329 if ((ioflg & IO_NODELOCKED) == 0) {
330 if (rw == UIO_READ) {
331 vn_lock(vp, LK_SHARED | LK_RETRY);
332 } else /* UIO_WRITE */ {
333 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
334 }
335 }
336 auio.uio_iov = &aiov;
337 auio.uio_iovcnt = 1;
338 aiov.iov_base = base;
339 aiov.iov_len = len;
340 auio.uio_resid = len;
341 auio.uio_offset = offset;
342 auio.uio_rw = rw;
343 if (segflg == UIO_SYSSPACE) {
344 UIO_SETUP_SYSSPACE(&auio);
345 } else {
346 auio.uio_vmspace = l->l_proc->p_vmspace;
347 }
348 if (rw == UIO_READ) {
349 error = VOP_READ(vp, &auio, ioflg, cred);
350 } else {
351 error = VOP_WRITE(vp, &auio, ioflg, cred);
352 }
353 if (aresid)
354 *aresid = auio.uio_resid;
355 else
356 if (auio.uio_resid && error == 0)
357 error = EIO;
358 if ((ioflg & IO_NODELOCKED) == 0) {
359 VOP_UNLOCK(vp, 0);
360 }
361 return (error);
362 }
363
364 int
365 vn_readdir(file_t *fp, char *bf, int segflg, u_int count, int *done,
366 struct lwp *l, off_t **cookies, int *ncookies)
367 {
368 struct vnode *vp = (struct vnode *)fp->f_data;
369 struct iovec aiov;
370 struct uio auio;
371 int error, eofflag;
372
373 /* Limit the size on any kernel buffers used by VOP_READDIR */
374 count = min(MAXBSIZE, count);
375
376 unionread:
377 if (vp->v_type != VDIR)
378 return (EINVAL);
379 aiov.iov_base = bf;
380 aiov.iov_len = count;
381 auio.uio_iov = &aiov;
382 auio.uio_iovcnt = 1;
383 auio.uio_rw = UIO_READ;
384 if (segflg == UIO_SYSSPACE) {
385 UIO_SETUP_SYSSPACE(&auio);
386 } else {
387 KASSERT(l == curlwp);
388 auio.uio_vmspace = l->l_proc->p_vmspace;
389 }
390 auio.uio_resid = count;
391 vn_lock(vp, LK_SHARED | LK_RETRY);
392 auio.uio_offset = fp->f_offset;
393 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
394 ncookies);
395 FILE_LOCK(fp);
396 fp->f_offset = auio.uio_offset;
397 FILE_UNLOCK(fp);
398 VOP_UNLOCK(vp, 0);
399 if (error)
400 return (error);
401
402 if (count == auio.uio_resid && vn_union_readdir_hook) {
403 struct vnode *ovp = vp;
404
405 error = (*vn_union_readdir_hook)(&vp, fp, l);
406 if (error)
407 return (error);
408 if (vp != ovp)
409 goto unionread;
410 }
411
412 if (count == auio.uio_resid && (vp->v_vflag & VV_ROOT) &&
413 (vp->v_mount->mnt_flag & MNT_UNION)) {
414 struct vnode *tvp = vp;
415 vp = vp->v_mount->mnt_vnodecovered;
416 VREF(vp);
417 FILE_LOCK(fp);
418 fp->f_data = vp;
419 fp->f_offset = 0;
420 FILE_UNLOCK(fp);
421 vrele(tvp);
422 goto unionread;
423 }
424 *done = count - auio.uio_resid;
425 return error;
426 }
427
428 /*
429 * File table vnode read routine.
430 */
431 static int
432 vn_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
433 int flags)
434 {
435 struct vnode *vp = (struct vnode *)fp->f_data;
436 int count, error, ioflag, fflag;
437
438 ioflag = IO_ADV_ENCODE(fp->f_advice);
439 fflag = fp->f_flag;
440 if (fflag & FNONBLOCK)
441 ioflag |= IO_NDELAY;
442 if ((fflag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
443 ioflag |= IO_SYNC;
444 if (fflag & FALTIO)
445 ioflag |= IO_ALTSEMANTICS;
446 if (fflag & FDIRECT)
447 ioflag |= IO_DIRECT;
448 vn_lock(vp, LK_SHARED | LK_RETRY);
449 uio->uio_offset = *offset;
450 count = uio->uio_resid;
451 error = VOP_READ(vp, uio, ioflag, cred);
452 if (flags & FOF_UPDATE_OFFSET)
453 *offset += count - uio->uio_resid;
454 VOP_UNLOCK(vp, 0);
455 return (error);
456 }
457
458 /*
459 * File table vnode write routine.
460 */
461 static int
462 vn_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
463 int flags)
464 {
465 struct vnode *vp = (struct vnode *)fp->f_data;
466 int count, error, ioflag, fflag;
467
468 ioflag = IO_ADV_ENCODE(fp->f_advice) | IO_UNIT;
469 fflag = fp->f_flag;
470 if (vp->v_type == VREG && (fflag & O_APPEND))
471 ioflag |= IO_APPEND;
472 if (fflag & FNONBLOCK)
473 ioflag |= IO_NDELAY;
474 if (fflag & FFSYNC ||
475 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
476 ioflag |= IO_SYNC;
477 else if (fflag & FDSYNC)
478 ioflag |= IO_DSYNC;
479 if (fflag & FALTIO)
480 ioflag |= IO_ALTSEMANTICS;
481 if (fflag & FDIRECT)
482 ioflag |= IO_DIRECT;
483 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
484 uio->uio_offset = *offset;
485 count = uio->uio_resid;
486 error = VOP_WRITE(vp, uio, ioflag, cred);
487 if (flags & FOF_UPDATE_OFFSET) {
488 if (ioflag & IO_APPEND) {
489 /*
490 * SUSv3 describes behaviour for count = 0 as following:
491 * "Before any action ... is taken, and if nbyte is zero
492 * and the file is a regular file, the write() function
493 * ... in the absence of errors ... shall return zero
494 * and have no other results."
495 */
496 if (count)
497 *offset = uio->uio_offset;
498 } else
499 *offset += count - uio->uio_resid;
500 }
501 VOP_UNLOCK(vp, 0);
502 return (error);
503 }
504
505 /*
506 * File table vnode stat routine.
507 */
508 static int
509 vn_statfile(file_t *fp, struct stat *sb)
510 {
511 struct vnode *vp = (struct vnode *)fp->f_data;
512
513 return vn_stat(vp, sb);
514 }
515
516 int
517 vn_stat(struct vnode *vp, struct stat *sb)
518 {
519 struct vattr va;
520 int error;
521 mode_t mode;
522
523 error = VOP_GETATTR(vp, &va, kauth_cred_get());
524 if (error)
525 return (error);
526 /*
527 * Copy from vattr table
528 */
529 sb->st_dev = va.va_fsid;
530 sb->st_ino = va.va_fileid;
531 mode = va.va_mode;
532 switch (vp->v_type) {
533 case VREG:
534 mode |= S_IFREG;
535 break;
536 case VDIR:
537 mode |= S_IFDIR;
538 break;
539 case VBLK:
540 mode |= S_IFBLK;
541 break;
542 case VCHR:
543 mode |= S_IFCHR;
544 break;
545 case VLNK:
546 mode |= S_IFLNK;
547 break;
548 case VSOCK:
549 mode |= S_IFSOCK;
550 break;
551 case VFIFO:
552 mode |= S_IFIFO;
553 break;
554 default:
555 return (EBADF);
556 };
557 sb->st_mode = mode;
558 sb->st_nlink = va.va_nlink;
559 sb->st_uid = va.va_uid;
560 sb->st_gid = va.va_gid;
561 sb->st_rdev = va.va_rdev;
562 sb->st_size = va.va_size;
563 sb->st_atimespec = va.va_atime;
564 sb->st_mtimespec = va.va_mtime;
565 sb->st_ctimespec = va.va_ctime;
566 sb->st_birthtimespec = va.va_birthtime;
567 sb->st_blksize = va.va_blocksize;
568 sb->st_flags = va.va_flags;
569 sb->st_gen = 0;
570 sb->st_blocks = va.va_bytes / S_BLKSIZE;
571 return (0);
572 }
573
574 /*
575 * File table vnode fcntl routine.
576 */
577 static int
578 vn_fcntl(file_t *fp, u_int com, void *data)
579 {
580 struct vnode *vp = fp->f_data;
581 int error;
582
583 error = VOP_FCNTL(vp, com, data, fp->f_flag, kauth_cred_get());
584 return (error);
585 }
586
587 /*
588 * File table vnode ioctl routine.
589 */
590 static int
591 vn_ioctl(file_t *fp, u_long com, void *data)
592 {
593 struct vnode *vp = fp->f_data, *ovp;
594 struct vattr vattr;
595 int error;
596
597 switch (vp->v_type) {
598
599 case VREG:
600 case VDIR:
601 if (com == FIONREAD) {
602 error = VOP_GETATTR(vp, &vattr,
603 kauth_cred_get());
604 if (error)
605 return (error);
606 *(int *)data = vattr.va_size - fp->f_offset;
607 return (0);
608 }
609 if ((com == FIONWRITE) || (com == FIONSPACE)) {
610 /*
611 * Files don't have send queues, so there never
612 * are any bytes in them, nor is there any
613 * open space in them.
614 */
615 *(int *)data = 0;
616 return (0);
617 }
618 if (com == FIOGETBMAP) {
619 daddr_t *block;
620
621 if (*(daddr_t *)data < 0)
622 return (EINVAL);
623 block = (daddr_t *)data;
624 return (VOP_BMAP(vp, *block, NULL, block, NULL));
625 }
626 if (com == OFIOGETBMAP) {
627 daddr_t ibn, obn;
628
629 if (*(int32_t *)data < 0)
630 return (EINVAL);
631 ibn = (daddr_t)*(int32_t *)data;
632 error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
633 *(int32_t *)data = (int32_t)obn;
634 return error;
635 }
636 if (com == FIONBIO || com == FIOASYNC) /* XXX */
637 return (0); /* XXX */
638 /* fall into ... */
639 case VFIFO:
640 case VCHR:
641 case VBLK:
642 error = VOP_IOCTL(vp, com, data, fp->f_flag,
643 kauth_cred_get());
644 if (error == 0 && com == TIOCSCTTY) {
645 VREF(vp);
646 mutex_enter(proc_lock);
647 ovp = curproc->p_session->s_ttyvp;
648 curproc->p_session->s_ttyvp = vp;
649 mutex_exit(proc_lock);
650 if (ovp != NULL)
651 vrele(ovp);
652 }
653 return (error);
654
655 default:
656 return (EPASSTHROUGH);
657 }
658 }
659
660 /*
661 * File table vnode poll routine.
662 */
663 static int
664 vn_poll(file_t *fp, int events)
665 {
666
667 return (VOP_POLL(fp->f_data, events));
668 }
669
670 /*
671 * File table vnode kqfilter routine.
672 */
673 int
674 vn_kqfilter(file_t *fp, struct knote *kn)
675 {
676
677 return (VOP_KQFILTER(fp->f_data, kn));
678 }
679
680 /*
681 * Check that the vnode is still valid, and if so
682 * acquire requested lock.
683 */
684 int
685 vn_lock(struct vnode *vp, int flags)
686 {
687 int error;
688
689 #if 0
690 KASSERT(vp->v_usecount > 0 || (flags & LK_INTERLOCK) != 0
691 || (vp->v_iflag & VI_ONWORKLST) != 0);
692 #endif
693 KASSERT((flags &
694 ~(LK_INTERLOCK|LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT|LK_RETRY|
695 LK_CANRECURSE))
696 == 0);
697
698 #ifdef DIAGNOSTIC
699 if (wapbl_vphaswapbl(vp))
700 WAPBL_JUNLOCK_ASSERT(wapbl_vptomp(vp));
701 #endif
702
703 do {
704 /*
705 * XXX PR 37706 forced unmount of file systems is unsafe.
706 * Race between vclean() and this the remaining problem.
707 */
708 if (vp->v_iflag & VI_XLOCK) {
709 if ((flags & LK_INTERLOCK) == 0) {
710 mutex_enter(&vp->v_interlock);
711 }
712 flags &= ~LK_INTERLOCK;
713 if (flags & LK_NOWAIT) {
714 mutex_exit(&vp->v_interlock);
715 return EBUSY;
716 }
717 vwait(vp, VI_XLOCK);
718 mutex_exit(&vp->v_interlock);
719 error = ENOENT;
720 } else {
721 if ((flags & LK_INTERLOCK) != 0) {
722 mutex_exit(&vp->v_interlock);
723 }
724 flags &= ~LK_INTERLOCK;
725 error = VOP_LOCK(vp, (flags & ~LK_RETRY));
726 if (error == 0 || error == EDEADLK || error == EBUSY)
727 return (error);
728 }
729 } while (flags & LK_RETRY);
730 return (error);
731 }
732
733 /*
734 * File table vnode close routine.
735 */
736 static int
737 vn_closefile(file_t *fp)
738 {
739
740 return vn_close(fp->f_data, fp->f_flag, fp->f_cred);
741 }
742
743 /*
744 * Enable LK_CANRECURSE on lock. Return prior status.
745 */
746 u_int
747 vn_setrecurse(struct vnode *vp)
748 {
749 struct vnlock *lkp;
750
751 lkp = (vp->v_vnlock != NULL ? vp->v_vnlock : &vp->v_lock);
752 atomic_inc_uint(&lkp->vl_canrecurse);
753
754 return 0;
755 }
756
757 /*
758 * Called when done with locksetrecurse.
759 */
760 void
761 vn_restorerecurse(struct vnode *vp, u_int flags)
762 {
763 struct vnlock *lkp;
764
765 lkp = (vp->v_vnlock != NULL ? vp->v_vnlock : &vp->v_lock);
766 atomic_dec_uint(&lkp->vl_canrecurse);
767 }
768
769 /*
770 * Simplified in-kernel wrapper calls for extended attribute access.
771 * Both calls pass in a NULL credential, authorizing a "kernel" access.
772 * Set IO_NODELOCKED in ioflg if the vnode is already locked.
773 */
774 int
775 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
776 const char *attrname, size_t *buflen, void *bf, struct lwp *l)
777 {
778 struct uio auio;
779 struct iovec aiov;
780 int error;
781
782 aiov.iov_len = *buflen;
783 aiov.iov_base = bf;
784
785 auio.uio_iov = &aiov;
786 auio.uio_iovcnt = 1;
787 auio.uio_rw = UIO_READ;
788 auio.uio_offset = 0;
789 auio.uio_resid = *buflen;
790 UIO_SETUP_SYSSPACE(&auio);
791
792 if ((ioflg & IO_NODELOCKED) == 0)
793 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
794
795 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL);
796
797 if ((ioflg & IO_NODELOCKED) == 0)
798 VOP_UNLOCK(vp, 0);
799
800 if (error == 0)
801 *buflen = *buflen - auio.uio_resid;
802
803 return (error);
804 }
805
806 /*
807 * XXX Failure mode if partially written?
808 */
809 int
810 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
811 const char *attrname, size_t buflen, const void *bf, struct lwp *l)
812 {
813 struct uio auio;
814 struct iovec aiov;
815 int error;
816
817 aiov.iov_len = buflen;
818 aiov.iov_base = __UNCONST(bf); /* XXXUNCONST kills const */
819
820 auio.uio_iov = &aiov;
821 auio.uio_iovcnt = 1;
822 auio.uio_rw = UIO_WRITE;
823 auio.uio_offset = 0;
824 auio.uio_resid = buflen;
825 UIO_SETUP_SYSSPACE(&auio);
826
827 if ((ioflg & IO_NODELOCKED) == 0) {
828 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
829 }
830
831 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL);
832
833 if ((ioflg & IO_NODELOCKED) == 0) {
834 VOP_UNLOCK(vp, 0);
835 }
836
837 return (error);
838 }
839
840 int
841 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
842 const char *attrname, struct lwp *l)
843 {
844 int error;
845
846 if ((ioflg & IO_NODELOCKED) == 0) {
847 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
848 }
849
850 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL);
851 if (error == EOPNOTSUPP)
852 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, NULL);
853
854 if ((ioflg & IO_NODELOCKED) == 0) {
855 VOP_UNLOCK(vp, 0);
856 }
857
858 return (error);
859 }
860
861 void
862 vn_ra_allocctx(struct vnode *vp)
863 {
864 struct uvm_ractx *ra = NULL;
865
866 KASSERT(mutex_owned(&vp->v_interlock));
867
868 if (vp->v_type != VREG) {
869 return;
870 }
871 if (vp->v_ractx != NULL) {
872 return;
873 }
874 if (vp->v_ractx == NULL) {
875 mutex_exit(&vp->v_interlock);
876 ra = uvm_ra_allocctx();
877 mutex_enter(&vp->v_interlock);
878 if (ra != NULL && vp->v_ractx == NULL) {
879 vp->v_ractx = ra;
880 ra = NULL;
881 }
882 }
883 if (ra != NULL) {
884 uvm_ra_freectx(ra);
885 }
886 }
887