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