xref: /csrg-svn/sys/kern/vfs_vnops.c (revision 5621)
1 /*	vfs_vnops.c	4.20	82/01/25	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/dir.h"
6 #include "../h/user.h"
7 #include "../h/filsys.h"
8 #include "../h/file.h"
9 #include "../h/conf.h"
10 #include "../h/inode.h"
11 #include "../h/reg.h"
12 #include "../h/acct.h"
13 #include "../h/mount.h"
14 #include "../h/socket.h"
15 #include "../h/socketvar.h"
16 #include "../h/proc.h"
17 
18 /*
19  * Convert a user supplied file descriptor into a pointer
20  * to a file structure.  Only task is to check range of the descriptor.
21  * Critical paths should use the GETF macro, defined in inline.h.
22  */
23 struct file *
24 getf(f)
25 	register int f;
26 {
27 	register struct file *fp;
28 
29 	if ((unsigned)f >= NOFILE || (fp = u.u_ofile[f]) == NULL) {
30 		u.u_error = EBADF;
31 		return (NULL);
32 	}
33 	return (fp);
34 }
35 
36 /*
37  * Internal form of close.
38  * Decrement reference count on
39  * file structure.
40  * Also make sure the pipe protocol
41  * does not constipate.
42  *
43  * Decrement reference count on the inode following
44  * removal to the referencing file structure.
45  * Call device handler on last close.
46  * Nouser indicates that the user isn't available to present
47  * errors to.
48  */
49 closef(fp, nouser)
50 	register struct file *fp;
51 {
52 	register struct inode *ip;
53 	register struct mount *mp;
54 	int flag, mode;
55 	dev_t dev;
56 	register int (*cfunc)();
57 
58 	if (fp == NULL)
59 		return;
60 	if (fp->f_count > 1) {
61 		fp->f_count--;
62 		return;
63 	}
64 	flag = fp->f_flag;
65 	if (flag & FSOCKET) {
66 		u.u_error = 0;			/* XXX */
67 		soclose(fp->f_socket, nouser);
68 		if (nouser == 0 && u.u_error)
69 			return;
70 		fp->f_socket = 0;
71 		fp->f_count = 0;
72 		return;
73 	}
74 	ip = fp->f_inode;
75 	dev = (dev_t)ip->i_un.i_rdev;
76 	mode = ip->i_mode & IFMT;
77 	ilock(ip);
78 	iput(ip);
79 	fp->f_count = 0;
80 
81 	switch (mode) {
82 
83 	case IFCHR:
84 		cfunc = cdevsw[major(dev)].d_close;
85 		break;
86 
87 	case IFBLK:
88 		/*
89 		 * We don't want to really close the device if it is mounted
90 		 */
91 		for (mp = mount; mp < &mount[NMOUNT]; mp++)
92 			if (mp->m_bufp != NULL && mp->m_dev == dev)
93 				return;
94 		cfunc = bdevsw[major(dev)].d_close;
95 		break;
96 
97 	default:
98 		return;
99 	}
100 	for (fp = file; fp < fileNFILE; fp++) {
101 		if (fp->f_flag & FSOCKET)
102 			continue;
103 		if (fp->f_count && (ip = fp->f_inode) &&
104 		    ip->i_un.i_rdev == dev && (ip->i_mode&IFMT) == mode)
105 			return;
106 	}
107 	if (mode == IFBLK) {
108 		/*
109 		 * On last close of a block device (that isn't mounted)
110 		 * we must invalidate any in core blocks
111 		 */
112 		bflush(dev);
113 		binval(dev);
114 	}
115 	(*cfunc)(dev, flag, fp);
116 }
117 
118 /*
119  * Openi called to allow handler
120  * of special files to initialize and
121  * validate before actual IO.
122  */
123 openi(ip, rw)
124 	register struct inode *ip;
125 {
126 	dev_t dev;
127 	register unsigned int maj;
128 
129 	dev = (dev_t)ip->i_un.i_rdev;
130 	maj = major(dev);
131 	switch (ip->i_mode&IFMT) {
132 
133 	case IFCHR:
134 		if (maj >= nchrdev)
135 			goto bad;
136 		(*cdevsw[maj].d_open)(dev, rw);
137 		break;
138 
139 	case IFBLK:
140 		if (maj >= nblkdev)
141 			goto bad;
142 		(*bdevsw[maj].d_open)(dev, rw);
143 	}
144 	return;
145 
146 bad:
147 	u.u_error = ENXIO;
148 }
149 
150 /*
151  * Check mode permission on inode pointer.
152  * Mode is READ, WRITE or EXEC.
153  * In the case of WRITE, the
154  * read-only status of the file
155  * system is checked.
156  * Also in WRITE, prototype text
157  * segments cannot be written.
158  * The mode is shifted to select
159  * the owner/group/other fields.
160  * The super user is granted all
161  * permissions.
162  */
163 access(ip, mode)
164 	register struct inode *ip;
165 	int mode;
166 {
167 	register m;
168 
169 	m = mode;
170 	if (m == IWRITE) {
171 		if (getfs(ip->i_dev)->s_ronly != 0) {
172 			u.u_error = EROFS;
173 			return (1);
174 		}
175 		if (ip->i_flag&ITEXT)		/* try to free text */
176 			xrele(ip);
177 		if (ip->i_flag & ITEXT) {
178 			u.u_error = ETXTBSY;
179 			return (1);
180 		}
181 	}
182 	if (u.u_uid == 0)
183 		return (0);
184 	if (u.u_uid != ip->i_uid) {
185 		m >>= 3;
186 		if (u.u_gid != ip->i_gid)
187 			m >>= 3;
188 	}
189 	if ((ip->i_mode&m) != 0)
190 		return (0);
191 	u.u_error = EACCES;
192 	return (1);
193 }
194 
195 /*
196  * Look up a pathname and test if
197  * the resultant inode is owned by the
198  * current user.
199  * If not, try for super-user.
200  * If permission is granted,
201  * return inode pointer.
202  */
203 struct inode *
204 owner()
205 {
206 	register struct inode *ip;
207 
208 	ip = namei(uchar, 0);
209 	if (ip == NULL)
210 		return (NULL);
211 	if (u.u_uid == ip->i_uid)
212 		return (ip);
213 	if (suser())
214 		return (ip);
215 	iput(ip);
216 	return (NULL);
217 }
218 
219 /*
220  * Test if the current user is the
221  * super user.
222  */
223 suser()
224 {
225 
226 	if (u.u_uid == 0) {
227 		u.u_acflag |= ASU;
228 		return (1);
229 	}
230 	u.u_error = EPERM;
231 	return (0);
232 }
233 
234 /*
235  * Allocate a user file descriptor.
236  */
237 ufalloc()
238 {
239 	register i;
240 
241 	for (i=0; i<NOFILE; i++)
242 		if (u.u_ofile[i] == NULL) {
243 			u.u_r.r_val1 = i;
244 			u.u_pofile[i] = 0;
245 			return (i);
246 		}
247 	u.u_error = EMFILE;
248 	return (-1);
249 }
250 
251 struct	file *lastf;
252 /*
253  * Allocate a user file descriptor
254  * and a file structure.
255  * Initialize the descriptor
256  * to point at the file structure.
257  */
258 struct file *
259 falloc()
260 {
261 	register struct file *fp;
262 	register i;
263 
264 	i = ufalloc();
265 	if (i < 0)
266 		return (NULL);
267 	if (lastf == 0)
268 		lastf = file;
269 	for (fp = lastf; fp < fileNFILE; fp++)
270 		if (fp->f_count == 0)
271 			goto slot;
272 	for (fp = file; fp < lastf; fp++)
273 		if (fp->f_count == 0)
274 			goto slot;
275 	tablefull("file");
276 	u.u_error = ENFILE;
277 	return (NULL);
278 slot:
279 	u.u_ofile[i] = fp;
280 	fp->f_count++;
281 	fp->f_offset = 0;
282 	fp->f_inode = 0;
283 	lastf = fp + 1;
284 	return (fp);
285 }
286