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