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