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