xref: /csrg-svn/sys/kern/vfs_vnops.c (revision 47540)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)vfs_vnops.c	7.27 (Berkeley) 03/17/91
8  */
9 
10 #include "param.h"
11 #include "systm.h"
12 #include "user.h"
13 #include "kernel.h"
14 #include "file.h"
15 #include "stat.h"
16 #include "buf.h"
17 #include "proc.h"
18 #include "uio.h"
19 #include "socket.h"
20 #include "socketvar.h"
21 #include "mount.h"
22 #include "vnode.h"
23 #include "ioctl.h"
24 #include "tty.h"
25 
26 int	vn_read(), vn_write(), vn_ioctl(), vn_select(), vn_close();
27 struct 	fileops vnops =
28 	{ vn_read, vn_write, vn_ioctl, vn_select, vn_close };
29 
30 /*
31  * Common code for vnode open operations.
32  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
33  */
34 vn_open(ndp, p, fmode, cmode)
35 	register struct nameidata *ndp;
36 	struct proc *p;
37 	int fmode, cmode;
38 {
39 	register struct vnode *vp;
40 	register struct ucred *cred = p->p_ucred;
41 	struct vattr vat;
42 	struct vattr *vap = &vat;
43 	int error;
44 
45 	if (fmode & FCREAT) {
46 		ndp->ni_nameiop = CREATE | LOCKPARENT | LOCKLEAF;
47 		if ((fmode & FEXCL) == 0)
48 			ndp->ni_nameiop |= FOLLOW;
49 		if (error = namei(ndp, p))
50 			return (error);
51 		if (ndp->ni_vp == NULL) {
52 			VATTR_NULL(vap);
53 			vap->va_type = VREG;
54 			vap->va_mode = cmode;
55 			if (error = VOP_CREATE(ndp, vap))
56 				return (error);
57 			fmode &= ~FTRUNC;
58 			vp = ndp->ni_vp;
59 		} else {
60 			if (ndp->ni_dvp == ndp->ni_vp)
61 				vrele(ndp->ni_dvp);
62 			else
63 				vput(ndp->ni_dvp);
64 			ndp->ni_dvp = NULL;
65 			vp = ndp->ni_vp;
66 			if (fmode & FEXCL) {
67 				error = EEXIST;
68 				goto bad;
69 			}
70 			fmode &= ~FCREAT;
71 		}
72 	} else {
73 		ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF;
74 		if (error = namei(ndp, p))
75 			return (error);
76 		vp = ndp->ni_vp;
77 	}
78 	if (vp->v_type == VSOCK) {
79 		error = EOPNOTSUPP;
80 		goto bad;
81 	}
82 	if ((fmode & FCREAT) == 0) {
83 		if (fmode & FREAD) {
84 			if (error = VOP_ACCESS(vp, VREAD, cred))
85 				goto bad;
86 		}
87 		if (fmode & (FWRITE|FTRUNC)) {
88 			if (vp->v_type == VDIR) {
89 				error = EISDIR;
90 				goto bad;
91 			}
92 			if ((error = vn_writechk(vp)) ||
93 			    (error = VOP_ACCESS(vp, VWRITE, cred)))
94 				goto bad;
95 		}
96 	}
97 	if (fmode & FTRUNC) {
98 		VATTR_NULL(vap);
99 		vap->va_size = 0;
100 		if (error = VOP_SETATTR(vp, vap, cred))
101 			goto bad;
102 	}
103 	VOP_UNLOCK(vp);
104 	error = VOP_OPEN(vp, fmode, cred);
105 	if (error)
106 		vrele(vp);
107 	return (error);
108 
109 bad:
110 	vput(vp);
111 	return (error);
112 }
113 
114 /*
115  * Check for write permissions on the specified vnode.
116  * The read-only status of the file system is checked.
117  * Also, prototype text segments cannot be written.
118  */
119 vn_writechk(vp)
120 	register struct vnode *vp;
121 {
122 
123 	/*
124 	 * Disallow write attempts on read-only file systems;
125 	 * unless the file is a socket or a block or character
126 	 * device resident on the file system.
127 	 */
128 	if (vp->v_mount->mnt_flag & MNT_RDONLY) {
129 		switch (vp->v_type) {
130 		case VREG: case VDIR: case VLNK:
131 			return (EROFS);
132 		}
133 	}
134 	/*
135 	 * If there's shared text associated with
136 	 * the vnode, try to free it up once.  If
137 	 * we fail, we can't allow writing.
138 	 */
139 	if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp))
140 		return (ETXTBSY);
141 	return (0);
142 }
143 
144 /*
145  * Vnode version of rdwri() for calls on file systems.
146  */
147 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid)
148 	enum uio_rw rw;
149 	struct vnode *vp;
150 	caddr_t base;
151 	int len;
152 	off_t offset;
153 	enum uio_seg segflg;
154 	int ioflg;
155 	struct ucred *cred;
156 	int *aresid;
157 {
158 	struct uio auio;
159 	struct iovec aiov;
160 	int error;
161 
162 	if ((ioflg & IO_NODELOCKED) == 0)
163 		VOP_LOCK(vp);
164 	auio.uio_iov = &aiov;
165 	auio.uio_iovcnt = 1;
166 	aiov.iov_base = base;
167 	aiov.iov_len = len;
168 	auio.uio_resid = len;
169 	auio.uio_offset = offset;
170 	auio.uio_segflg = segflg;
171 	auio.uio_rw = rw;
172 	if (rw == UIO_READ)
173 		error = VOP_READ(vp, &auio, ioflg, cred);
174 	else
175 		error = VOP_WRITE(vp, &auio, ioflg, cred);
176 	if (aresid)
177 		*aresid = auio.uio_resid;
178 	else
179 		if (auio.uio_resid && error == 0)
180 			error = EIO;
181 	if ((ioflg & IO_NODELOCKED) == 0)
182 		VOP_UNLOCK(vp);
183 	return (error);
184 }
185 
186 vn_read(fp, uio, cred)
187 	struct file *fp;
188 	struct uio *uio;
189 	struct ucred *cred;
190 {
191 	register struct vnode *vp = (struct vnode *)fp->f_data;
192 	int count, error;
193 
194 	VOP_LOCK(vp);
195 	uio->uio_offset = fp->f_offset;
196 	count = uio->uio_resid;
197 	error = VOP_READ(vp, uio, (fp->f_flag & FNDELAY) ? IO_NDELAY : 0, cred);
198 	fp->f_offset += count - uio->uio_resid;
199 	VOP_UNLOCK(vp);
200 	return (error);
201 }
202 
203 vn_write(fp, uio, cred)
204 	struct file *fp;
205 	struct uio *uio;
206 	struct ucred *cred;
207 {
208 	register struct vnode *vp = (struct vnode *)fp->f_data;
209 	int count, error, ioflag = 0;
210 
211 	if (vp->v_type == VREG && (fp->f_flag & FAPPEND))
212 		ioflag |= IO_APPEND;
213 	if (fp->f_flag & FNDELAY)
214 		ioflag |= IO_NDELAY;
215 	VOP_LOCK(vp);
216 	uio->uio_offset = fp->f_offset;
217 	count = uio->uio_resid;
218 	error = VOP_WRITE(vp, uio, ioflag, cred);
219 	if (ioflag & IO_APPEND)
220 		fp->f_offset = uio->uio_offset;
221 	else
222 		fp->f_offset += count - uio->uio_resid;
223 	VOP_UNLOCK(vp);
224 	return (error);
225 }
226 
227 /*
228  * Get stat info for a vnode.
229  */
230 vn_stat(vp, sb)
231 	struct vnode *vp;
232 	register struct stat *sb;
233 {
234 	struct vattr vattr;
235 	struct proc *p = curproc;		/* XXX */
236 	register struct vattr *vap;
237 	int error;
238 	u_short mode;
239 
240 	vap = &vattr;
241 	error = VOP_GETATTR(vp, vap, p->p_ucred);
242 	if (error)
243 		return (error);
244 	/*
245 	 * Copy from vattr table
246 	 */
247 	sb->st_dev = vap->va_fsid;
248 	sb->st_ino = vap->va_fileid;
249 	mode = vap->va_mode;
250 	switch (vp->v_type) {
251 	case VREG:
252 		mode |= S_IFREG;
253 		break;
254 	case VDIR:
255 		mode |= S_IFDIR;
256 		break;
257 	case VBLK:
258 		mode |= S_IFBLK;
259 		break;
260 	case VCHR:
261 		mode |= S_IFCHR;
262 		break;
263 	case VLNK:
264 		mode |= S_IFLNK;
265 		break;
266 	case VSOCK:
267 		mode |= S_IFSOCK;
268 		break;
269 	case VFIFO:
270 		mode |= S_IFIFO;
271 		break;
272 	default:
273 		return (EBADF);
274 	};
275 	sb->st_mode = mode;
276 	sb->st_nlink = vap->va_nlink;
277 	sb->st_uid = vap->va_uid;
278 	sb->st_gid = vap->va_gid;
279 	sb->st_rdev = vap->va_rdev;
280 	sb->st_size = vap->va_size;
281 	sb->st_atime = vap->va_atime.tv_sec;
282 	sb->st_spare1 = 0;
283 	sb->st_mtime = vap->va_mtime.tv_sec;
284 	sb->st_spare2 = 0;
285 	sb->st_ctime = vap->va_ctime.tv_sec;
286 	sb->st_spare3 = 0;
287 	sb->st_blksize = vap->va_blocksize;
288 	sb->st_flags = vap->va_flags;
289 	sb->st_gen = vap->va_gen;
290 	sb->st_blocks = vap->va_bytes / S_BLKSIZE;
291 	return (0);
292 }
293 
294 /*
295  * Vnode ioctl call
296  */
297 vn_ioctl(fp, com, data)
298 	struct file *fp;
299 	int com;
300 	caddr_t data;
301 {
302 	register struct vnode *vp = ((struct vnode *)fp->f_data);
303 	struct proc *p = curproc;		/* XXX */
304 	struct vattr vattr;
305 	int error;
306 
307 	switch (vp->v_type) {
308 
309 	case VREG:
310 	case VDIR:
311 		if (com == FIONREAD) {
312 			if (error = VOP_GETATTR(vp, &vattr, p->p_ucred))
313 				return (error);
314 			*(off_t *)data = vattr.va_size - fp->f_offset;
315 			return (0);
316 		}
317 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
318 			return (0);			/* XXX */
319 		/* fall into ... */
320 
321 	default:
322 		return (ENOTTY);
323 
324 	case VFIFO:
325 	case VCHR:
326 	case VBLK:
327 		error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred);
328 		if (error == 0 && com == TIOCSCTTY) {
329 			p->p_session->s_ttyvp = vp;
330 			VREF(vp);
331 		}
332 		return (error);
333 	}
334 }
335 
336 /*
337  * Vnode select call
338  */
339 vn_select(fp, which)
340 	struct file *fp;
341 	int which;
342 {
343 	struct proc *p = curproc;		/* XXX */
344 
345 	return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag,
346 		p->p_ucred));
347 }
348 
349 /*
350  * Vnode close call
351  */
352 vn_close(fp)
353 	register struct file *fp;
354 {
355 	struct vnode *vp = ((struct vnode *)fp->f_data);
356 	int error;
357 
358 	/*
359 	 * Must delete vnode reference from this file entry
360 	 * before VOP_CLOSE, so that only other references
361 	 * will prevent close.
362 	 */
363 	fp->f_data = (caddr_t) 0;
364 	error = VOP_CLOSE(vp, fp->f_flag, fp->f_cred);
365 	vrele(vp);
366 	return (error);
367 }
368 
369 /*
370  * vn_fhtovp() - convert a fh to a vnode ptr (optionally locked)
371  * 	- look up fsid in mount list (if not found ret error)
372  *	- get vp by calling VFS_FHTOVP() macro
373  *	- if lockflag lock it with VOP_LOCK()
374  */
375 vn_fhtovp(fhp, lockflag, vpp)
376 	fhandle_t *fhp;
377 	int lockflag;
378 	struct vnode **vpp;
379 {
380 	register struct mount *mp;
381 
382 	if ((mp = getvfs(&fhp->fh_fsid)) == NULL)
383 		return (ESTALE);
384 	if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp))
385 		return (ESTALE);
386 	if (!lockflag)
387 		VOP_UNLOCK(*vpp);
388 	return (0);
389 }
390