|
|
1.1 root 1: /*
2: * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3: *
4: * @APPLE_LICENSE_HEADER_START@
5: *
6: * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7: * Reserved. This file contains Original Code and/or Modifications of
8: * Original Code as defined in and that are subject to the Apple Public
9: * Source License Version 1.1 (the "License"). You may not use this file
10: * except in compliance with the License. Please obtain a copy of the
11: * License at http://www.apple.com/publicsource and read it before using
12: * this file.
13: *
14: * The Original Code and all software distributed under the License are
15: * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16: * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17: * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19: * License for the specific language governing rights and limitations
20: * under the License.
21: *
22: * @APPLE_LICENSE_HEADER_END@
23: */
24:
25: /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
26: /*
27: * Copyright (c) 1982, 1986, 1989, 1993
28: * The Regents of the University of California. All rights reserved.
29: * (c) UNIX System Laboratories, Inc.
30: * All or some portions of this file are derived from material licensed
31: * to the University of California by American Telephone and Telegraph
32: * Co. or Unix System Laboratories, Inc. and are reproduced herein with
33: * the permission of UNIX System Laboratories, Inc.
34: *
35: * Redistribution and use in source and binary forms, with or without
36: * modification, are permitted provided that the following conditions
37: * are met:
38: * 1. Redistributions of source code must retain the above copyright
39: * notice, this list of conditions and the following disclaimer.
40: * 2. Redistributions in binary form must reproduce the above copyright
41: * notice, this list of conditions and the following disclaimer in the
42: * documentation and/or other materials provided with the distribution.
43: * 3. All advertising materials mentioning features or use of this software
44: * must display the following acknowledgement:
45: * This product includes software developed by the University of
46: * California, Berkeley and its contributors.
47: * 4. Neither the name of the University nor the names of its contributors
48: * may be used to endorse or promote products derived from this software
49: * without specific prior written permission.
50: *
51: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61: * SUCH DAMAGE.
62: *
63: * @(#)vfs_vnops.c 8.14 (Berkeley) 6/15/95
64: *
65: * History
66: * 10-20-1997 Umesh Vaishampayan
67: * Fixed the count to be off_t rather than int.
68: */
69:
70: #include <mach_nbc.h>
71: #include <sys/param.h>
72: #include <sys/systm.h>
73: #include <sys/kernel.h>
74: #include <sys/file.h>
75: #include <sys/stat.h>
76: #include <sys/buf.h>
77: #include <sys/proc.h>
78: #include <sys/mount.h>
79: #include <sys/namei.h>
80: #include <sys/vnode.h>
81: #include <sys/ioctl.h>
82: #include <sys/tty.h>
83: #include <kern/mapfs.h>
84:
85: struct fileops vnops =
86: { vn_read, vn_write, vn_ioctl, vn_select, vn_closefile };
87:
88: /*
89: * Common code for vnode open operations.
90: * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
91: */
92: vn_open(ndp, fmode, cmode)
93: register struct nameidata *ndp;
94: int fmode, cmode;
95: {
96: register struct vnode *vp;
97: register struct proc *p = ndp->ni_cnd.cn_proc;
98: register struct ucred *cred = p->p_ucred;
99: struct vattr vat;
100: struct vattr *vap = &vat;
101: int error;
102: #if MACH_NBC
103: int no_mfs = fmode & O_NO_MFS;
104: #endif /* MACH_NBC */
105:
106: if (fmode & O_CREAT) {
107: ndp->ni_cnd.cn_nameiop = CREATE;
108: ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
109: if ((fmode & O_EXCL) == 0)
110: ndp->ni_cnd.cn_flags |= FOLLOW;
111: if (error = namei(ndp))
112: return (error);
113: if (ndp->ni_vp == NULL) {
114: VATTR_NULL(vap);
115: vap->va_type = VREG;
116: vap->va_mode = cmode;
117: if (fmode & O_EXCL)
118: vap->va_vaflags |= VA_EXCLUSIVE;
119: VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
120: if (error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
121: &ndp->ni_cnd, vap))
122: return (error);
123: fmode &= ~O_TRUNC;
124: vp = ndp->ni_vp;
125: } else {
126: VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
127: if (ndp->ni_dvp == ndp->ni_vp)
128: vrele(ndp->ni_dvp);
129: else
130: vput(ndp->ni_dvp);
131: ndp->ni_dvp = NULL;
132: vp = ndp->ni_vp;
133: if (fmode & O_EXCL) {
134: error = EEXIST;
135: goto bad;
136: }
137: fmode &= ~O_CREAT;
138: }
139: } else {
140: ndp->ni_cnd.cn_nameiop = LOOKUP;
141: ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF;
142: if (error = namei(ndp))
143: return (error);
144: vp = ndp->ni_vp;
145: }
146: if (vp->v_type == VSOCK) {
147: error = EOPNOTSUPP;
148: goto bad;
149: }
150: if ((fmode & O_CREAT) == 0) {
1.1.1.2 ! root 151: if (fmode & FREAD && fmode & (FWRITE | O_TRUNC)) {
! 152: int err = 0;
! 153: if (vp->v_type == VDIR)
! 154: err = EISDIR;
! 155: else
! 156: err = vn_writechk(vp);
! 157: if (err && !(error = VOP_ACCESS(vp, VREAD, cred, p)))
! 158: error = err;
! 159: if (error || (error = VOP_ACCESS(vp, VREAD|VWRITE,
! 160: cred, p)))
1.1 root 161: goto bad;
1.1.1.2 ! root 162: } else if (fmode & FREAD) {
! 163: if ((error = VOP_ACCESS(vp, VREAD, cred, p)))
! 164: goto bad;
! 165: } else if (fmode & (FWRITE | O_TRUNC)) {
1.1 root 166: if (vp->v_type == VDIR) {
167: error = EISDIR;
168: goto bad;
169: }
170: if ((error = vn_writechk(vp)) ||
171: (error = VOP_ACCESS(vp, VWRITE, cred, p)))
172: goto bad;
173: }
174: }
175: if (fmode & O_TRUNC) {
176: VOP_UNLOCK(vp, 0, p); /* XXX */
177: VOP_LEASE(vp, p, cred, LEASE_WRITE);
178: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); /* XXX */
179: VATTR_NULL(vap);
180: vap->va_size = 0;
181: if (error = VOP_SETATTR(vp, vap, cred, p))
182: goto bad;
183: }
184: if (error = VOP_OPEN(vp, fmode, cred, p))
185: goto bad;
186: if ((vp->v_type == VREG) && !vp->v_vm_info) // XXX WMG
187: vm_info_init(vp);
188: #if MACH_NBC
189: if ((error == 0) && (no_mfs == 0) && (vp->v_type == VREG)) {
190: /* if the file was never mapped, leave it alone */
191: if (vp->v_vm_info->pager != NULL)
192: map_vnode(vp,p);
193: }
194:
195: /*
196: * if O_NO_MFS is set on open, blow the VM cache.
197: * if someone else is using it in VM then this has no effect.
198: */
199: if ((vp->v_type == VREG) && (no_mfs != 0))
200: (void) vnode_uncache(vp);
201: #endif /* MACH_NBC */
202:
203: if (fmode & FWRITE)
1.1.1.2 ! root 204: if (++vp->v_writecount <= 0)
! 205: panic("vn_open: v_writecount");
1.1 root 206: return (0);
207: bad:
208: vput(vp);
209: return (error);
210: }
211:
212: /*
213: * Check for write permissions on the specified vnode.
214: * Prototype text segments cannot be written.
215: */
216: vn_writechk(vp)
217: register struct vnode *vp;
218: {
219:
220: /*
221: * If there's shared text associated with
222: * the vnode, try to free it up once. If
223: * we fail, we can't allow writing.
224: */
225: if ((vp->v_flag & VTEXT) && !vnode_uncache(vp))
226: return (ETXTBSY);
227: return (0);
228: }
229:
230: /*
231: * Vnode close call
232: */
233: vn_close(vp, flags, cred, p)
234: register struct vnode *vp;
235: int flags;
236: struct ucred *cred;
237: struct proc *p;
238: {
239: int error;
240:
241: #if MACH_NBC
242: if ((vp->v_type == VREG) && vp->v_vm_info)
243: unmap_vnode(vp,p);
244: #endif /* MACH_NBC */
245:
246: if (flags & FWRITE)
247: vp->v_writecount--;
248: error = VOP_CLOSE(vp, flags, cred, p);
249: vrele(vp);
250: return (error);
251: }
252:
253: /*
254: * Package up an I/O request on a vnode into a uio and do it.
255: */
256: vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
257: enum uio_rw rw;
258: struct vnode *vp;
259: caddr_t base;
260: int len;
261: off_t offset;
262: enum uio_seg segflg;
263: int ioflg;
264: struct ucred *cred;
265: int *aresid;
266: struct proc *p;
267: {
268: struct uio auio;
269: struct iovec aiov;
270: int error=0;
271:
272: /* The routines that use this under MACH_NBC, call this
273: * routine with IO_NODELOCKED on, which is incorrect.
274: * For ease of code usage in case MACH_NBC is turned off
275: * we will bypass this check and let the callers still use
276: * IO_NODELOCKED flag
277: */
278: /* FIXME XXX */
279: if ((ioflg & IO_NODELOCKED) == 0)
280: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
281: auio.uio_iov = &aiov;
282: auio.uio_iovcnt = 1;
283: aiov.iov_base = base;
284: aiov.iov_len = len;
285: auio.uio_resid = len;
286: auio.uio_offset = offset;
287: auio.uio_segflg = segflg;
288: auio.uio_rw = rw;
289: auio.uio_procp = p;
290: #if MACH_NBC
291: /* if the file was never mapped, leave it alone */
292: if ((vp->v_type == VREG) && (vp->v_vm_info->pager != NULL)) {
293: if (vp->v_vm_info->map_count == 0) {
294: /*
295: * file has a pager but MapFS is not involved yet
296: * get MapFS involved
297: */
298: map_vnode(vp,p); /* vn_close() will loose this map_count */
299: }
300: error = mapfs_io(vp, &auio, rw, ioflg, cred);
301: } else {
302: #endif /* MACH_NBC */
303: if (rw == UIO_READ)
304: error = VOP_READ(vp, &auio, ioflg, cred);
305: else
306: error = VOP_WRITE(vp, &auio, ioflg, cred);
307: #if MACH_NBC
308: }
309: #endif /* MACH_NBC */
310: if (aresid)
311: *aresid = auio.uio_resid;
312: else
313: if (auio.uio_resid && error == 0)
314: error = EIO;
315: if ((ioflg & IO_NODELOCKED) == 0)
316: VOP_UNLOCK(vp, 0, p);
317: return (error);
318: }
319:
320: /*
321: * File table vnode read routine.
322: */
323: vn_read(fp, uio, cred)
324: struct file *fp;
325: struct uio *uio;
326: struct ucred *cred;
327: {
328: struct vnode *vp = (struct vnode *)fp->f_data;
329: struct proc *p = uio->uio_procp;
330: int error;
331: off_t count;
332:
333: VOP_LEASE(vp, p, cred, LEASE_READ);
334: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
335: uio->uio_offset = fp->f_offset;
336: count = uio->uio_resid;
337: #if MACH_NBC
338: if ((vp->v_type == VREG) && (vp->v_vm_info) && (vp->v_vm_info->mapped)) {
339: error = mapfs_io(vp, uio, UIO_READ, (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0, cred);
340: } else {
341: #endif /* MACH_NBC */
342: error = VOP_READ(vp, uio, (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0, cred);
343: #if MACH_NBC
344: }
345: #endif /* MACH_NBC */
346:
347: fp->f_offset += count - uio->uio_resid;
348: VOP_UNLOCK(vp, 0, p);
349: return (error);
350: }
351:
352: /*
353: * File table vnode write routine.
354: */
355: vn_write(fp, uio, cred)
356: struct file *fp;
357: struct uio *uio;
358: struct ucred *cred;
359: {
360: struct vnode *vp = (struct vnode *)fp->f_data;
361: struct proc *p = uio->uio_procp;
362: int error, ioflag = IO_UNIT;
363: off_t count;
364:
365: if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
366: ioflag |= IO_APPEND;
367: if (fp->f_flag & FNONBLOCK)
368: ioflag |= IO_NDELAY;
369: if ((fp->f_flag & O_FSYNC) ||
370: (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
371: ioflag |= IO_SYNC;
372: VOP_LEASE(vp, p, cred, LEASE_WRITE);
373: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
374: uio->uio_offset = fp->f_offset;
375: count = uio->uio_resid;
376: #if MACH_NBC
377: if ((vp->v_type == VREG) && (vp->v_vm_info) && (vp->v_vm_info->mapped)) {
378: error = mapfs_io(vp, uio, UIO_WRITE, ioflag, cred);
379: } else {
380: #endif /* MACH_NBC */
381:
382: error = VOP_WRITE(vp, uio, ioflag, cred);
383: #if MACH_NBC
384: }
385: #endif /* MACH_NBC */
386:
387: if (ioflag & IO_APPEND)
388: fp->f_offset = uio->uio_offset;
389: else
390: fp->f_offset += count - uio->uio_resid;
391: VOP_UNLOCK(vp, 0, p);
392: return (error);
393: }
394:
395: /*
396: * File table vnode stat routine.
397: */
398: vn_stat(vp, sb, p)
399: struct vnode *vp;
400: register struct stat *sb;
401: struct proc *p;
402: {
403: struct vattr vattr;
404: register struct vattr *vap;
405: int error;
406: u_short mode;
407:
408: vap = &vattr;
409: error = VOP_GETATTR(vp, vap, p->p_ucred, p);
410: if (error)
411: return (error);
412: /*
413: * Copy from vattr table
414: */
415: sb->st_dev = vap->va_fsid;
416: sb->st_ino = vap->va_fileid;
417: mode = vap->va_mode;
418: switch (vp->v_type) {
419: case VREG:
420: mode |= S_IFREG;
421: break;
422: case VDIR:
423: mode |= S_IFDIR;
424: break;
425: case VBLK:
426: mode |= S_IFBLK;
427: break;
428: case VCHR:
429: mode |= S_IFCHR;
430: break;
431: case VLNK:
432: mode |= S_IFLNK;
433: break;
434: case VSOCK:
435: mode |= S_IFSOCK;
436: break;
437: case VFIFO:
438: mode |= S_IFIFO;
439: break;
440: default:
441: return (EBADF);
442: };
443: sb->st_mode = mode;
444: sb->st_nlink = vap->va_nlink;
445: sb->st_uid = vap->va_uid;
446: sb->st_gid = vap->va_gid;
447: sb->st_rdev = vap->va_rdev;
448: sb->st_size = vap->va_size;
449: sb->st_atimespec = vap->va_atime;
450: sb->st_mtimespec = vap->va_mtime;
451: sb->st_ctimespec = vap->va_ctime;
452: sb->st_blksize = vap->va_blocksize;
453: sb->st_flags = vap->va_flags;
454: /* Do not give the generation number out to unpriviledged users */
455: if (suser(p->p_ucred, &p->p_acflag))
456: sb->st_gen = 0;
457: else
458: sb->st_gen = vap->va_gen;
459: sb->st_blocks = vap->va_bytes / S_BLKSIZE;
460: return (0);
461: }
462:
463: /*
464: * File table vnode ioctl routine.
465: */
466: vn_ioctl(fp, com, data, p)
467: struct file *fp;
468: u_long com;
469: caddr_t data;
470: struct proc *p;
471: {
472: register struct vnode *vp = ((struct vnode *)fp->f_data);
473: struct vattr vattr;
474: int error;
475:
476: switch (vp->v_type) {
477:
478: case VREG:
479: case VDIR:
480: if (com == FIONREAD) {
481: if (error = VOP_GETATTR(vp, &vattr, p->p_ucred, p))
482: return (error);
483: *(int *)data = vattr.va_size - fp->f_offset;
484: return (0);
485: }
486: if (com == FIONBIO || com == FIOASYNC) /* XXX */
487: return (0); /* XXX */
488: /* fall into ... */
489:
490: default:
491: return (ENOTTY);
492:
493: case VFIFO:
494: case VCHR:
495: case VBLK:
496: error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
497: if (error == 0 && com == TIOCSCTTY) {
498: if (p->p_session->s_ttyvp)
499: vrele(p->p_session->s_ttyvp);
500: p->p_session->s_ttyvp = vp;
501: VREF(vp);
502: }
503: return (error);
504: }
505: }
506:
507: /*
508: * File table vnode select routine.
509: */
510: vn_select(fp, which, p)
511: struct file *fp;
512: int which;
513: struct proc *p;
514: {
515:
516: return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag,
517: fp->f_cred, p));
518: }
519:
520: /*
521: * Check that the vnode is still valid, and if so
522: * acquire requested lock.
523: */
524: int
525: vn_lock(vp, flags, p)
526: struct vnode *vp;
527: int flags;
528: struct proc *p;
529: {
530: int error;
531:
532: do {
533: if ((flags & LK_INTERLOCK) == 0)
534: simple_lock(&vp->v_interlock);
535: if (vp->v_flag & VXLOCK) {
536: vp->v_flag |= VXWANT;
537: simple_unlock(&vp->v_interlock);
538: tsleep((caddr_t)vp, PINOD, "vn_lock", 0);
539: error = ENOENT;
540: } else {
541: error = VOP_LOCK(vp, flags | LK_INTERLOCK, p);
542: if (error == 0)
543: return (error);
544: }
545: flags &= ~LK_INTERLOCK;
546: } while (flags & LK_RETRY);
547: return (error);
548: }
549:
550: /*
551: * File table vnode close routine.
552: */
553: vn_closefile(fp, p)
554: struct file *fp;
555: struct proc *p;
556: {
557:
558: return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
559: fp->f_cred, p));
560: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.