bug fixes from elz and utcsrgv!thomson
[unix-history] / usr / src / sys / kern / vfs_vnops.c
CommitLineData
5485e062 1/* vfs_vnops.c 4.22 82/02/27 */
fc96c615
BJ
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"
f5b8937b 13#include "../h/mount.h"
ae921915
BJ
14#include "../h/socket.h"
15#include "../h/socketvar.h"
f3156a73 16#include "../h/proc.h"
fc96c615
BJ
17
18/*
fdd90ee3
BJ
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.
fc96c615
BJ
22 */
23struct file *
24getf(f)
fdd90ee3 25 register int f;
fc96c615
BJ
26{
27 register struct file *fp;
28
75a44ba5
BJ
29 if ((unsigned)f >= NOFILE || (fp = u.u_ofile[f]) == NULL) {
30 u.u_error = EBADF;
31 return (NULL);
fc96c615 32 }
75a44ba5 33 return (fp);
fc96c615
BJ
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.
f3156a73
BJ
46 * Nouser indicates that the user isn't available to present
47 * errors to.
fc96c615 48 */
f3156a73 49closef(fp, nouser)
fdd90ee3 50 register struct file *fp;
fc96c615
BJ
51{
52 register struct inode *ip;
f5b8937b 53 register struct mount *mp;
fc96c615
BJ
54 int flag, mode;
55 dev_t dev;
56 register int (*cfunc)();
fc96c615 57
fdd90ee3 58 if (fp == NULL)
fc96c615
BJ
59 return;
60 if (fp->f_count > 1) {
61 fp->f_count--;
62 return;
63 }
fc96c615 64 flag = fp->f_flag;
fdd90ee3 65 if (flag & FSOCKET) {
8f4851aa 66 u.u_error = 0; /* XXX */
f3156a73 67 soclose(fp->f_socket, nouser);
8f4851aa 68 if (nouser == 0 && u.u_error)
f3156a73 69 return;
fdd90ee3 70 fp->f_socket = 0;
eb681afb
BJ
71 fp->f_count = 0;
72 return;
73 }
74 ip = fp->f_inode;
fc96c615 75 dev = (dev_t)ip->i_un.i_rdev;
f5b8937b 76 mode = ip->i_mode & IFMT;
fdd90ee3 77 ilock(ip);
fc96c615 78 iput(ip);
eb681afb 79 fp->f_count = 0;
fc96c615 80
eb681afb 81 switch (mode) {
fc96c615
BJ
82
83 case IFCHR:
fc96c615
BJ
84 cfunc = cdevsw[major(dev)].d_close;
85 break;
86
87 case IFBLK:
f5b8937b
RE
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;
fc96c615
BJ
94 cfunc = bdevsw[major(dev)].d_close;
95 break;
fdd90ee3 96
fc96c615
BJ
97 default:
98 return;
99 }
fdd90ee3
BJ
100 for (fp = file; fp < fileNFILE; fp++) {
101 if (fp->f_flag & FSOCKET)
102 continue;
231ed58b 103 if (fp->f_count && (ip = fp->f_inode) &&
fdd90ee3 104 ip->i_un.i_rdev == dev && (ip->i_mode&IFMT) == mode)
231ed58b
BJ
105 return;
106 }
107 if (mode == IFBLK) {
108 /*
fdd90ee3 109 * On last close of a block device (that isn't mounted)
231ed58b
BJ
110 * we must invalidate any in core blocks
111 */
112 bflush(dev);
113 binval(dev);
f5b8937b 114 }
e6aa77eb 115 (*cfunc)(dev, flag, fp);
fc96c615
BJ
116}
117
118/*
fdd90ee3 119 * Openi called to allow handler
fc96c615
BJ
120 * of special files to initialize and
121 * validate before actual IO.
122 */
123openi(ip, rw)
fdd90ee3 124 register struct inode *ip;
fc96c615
BJ
125{
126 dev_t dev;
127 register unsigned int maj;
128
129 dev = (dev_t)ip->i_un.i_rdev;
130 maj = major(dev);
fdd90ee3 131 switch (ip->i_mode&IFMT) {
fc96c615
BJ
132
133 case IFCHR:
fdd90ee3 134 if (maj >= nchrdev)
fc96c615
BJ
135 goto bad;
136 (*cdevsw[maj].d_open)(dev, rw);
137 break;
138
139 case IFBLK:
fdd90ee3 140 if (maj >= nblkdev)
fc96c615
BJ
141 goto bad;
142 (*bdevsw[maj].d_open)(dev, rw);
143 }
144 return;
145
146bad:
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 */
163access(ip, mode)
fdd90ee3
BJ
164 register struct inode *ip;
165 int mode;
fc96c615
BJ
166{
167 register m;
168
169 m = mode;
fdd90ee3
BJ
170 if (m == IWRITE) {
171 if (getfs(ip->i_dev)->s_ronly != 0) {
fc96c615 172 u.u_error = EROFS;
fdd90ee3 173 return (1);
fc96c615
BJ
174 }
175 if (ip->i_flag&ITEXT) /* try to free text */
176 xrele(ip);
fdd90ee3 177 if (ip->i_flag & ITEXT) {
fc96c615 178 u.u_error = ETXTBSY;
fdd90ee3 179 return (1);
fc96c615
BJ
180 }
181 }
fdd90ee3
BJ
182 if (u.u_uid == 0)
183 return (0);
184 if (u.u_uid != ip->i_uid) {
fc96c615 185 m >>= 3;
4f4caf05
BJ
186 if (ip->i_gid >= NGRPS ||
187 (u.u_grps[ip->i_gid/(sizeof(int)*8)] &
188 (1 << ip->i_gid%(sizeof(int)*8)) == 0))
fc96c615
BJ
189 m >>= 3;
190 }
fdd90ee3
BJ
191 if ((ip->i_mode&m) != 0)
192 return (0);
fc96c615 193 u.u_error = EACCES;
fdd90ee3 194 return (1);
fc96c615
BJ
195}
196
197/*
198 * Look up a pathname and test if
199 * the resultant inode is owned by the
200 * current user.
201 * If not, try for super-user.
202 * If permission is granted,
203 * return inode pointer.
204 */
205struct inode *
5485e062
BJ
206owner(follow)
207 int follow;
fc96c615
BJ
208{
209 register struct inode *ip;
210
5485e062 211 ip = namei(uchar, 0, follow);
fdd90ee3
BJ
212 if (ip == NULL)
213 return (NULL);
214 if (u.u_uid == ip->i_uid)
215 return (ip);
216 if (suser())
217 return (ip);
fc96c615 218 iput(ip);
fdd90ee3 219 return (NULL);
fc96c615
BJ
220}
221
222/*
223 * Test if the current user is the
224 * super user.
225 */
226suser()
227{
228
fdd90ee3 229 if (u.u_uid == 0) {
fc96c615 230 u.u_acflag |= ASU;
fdd90ee3 231 return (1);
fc96c615
BJ
232 }
233 u.u_error = EPERM;
fdd90ee3 234 return (0);
fc96c615
BJ
235}
236
237/*
238 * Allocate a user file descriptor.
239 */
240ufalloc()
241{
242 register i;
243
fdd90ee3
BJ
244 for (i=0; i<NOFILE; i++)
245 if (u.u_ofile[i] == NULL) {
fc96c615
BJ
246 u.u_r.r_val1 = i;
247 u.u_pofile[i] = 0;
fdd90ee3 248 return (i);
fc96c615
BJ
249 }
250 u.u_error = EMFILE;
fdd90ee3 251 return (-1);
fc96c615
BJ
252}
253
0a34b6fd 254struct file *lastf;
fc96c615
BJ
255/*
256 * Allocate a user file descriptor
257 * and a file structure.
258 * Initialize the descriptor
259 * to point at the file structure.
fc96c615
BJ
260 */
261struct file *
262falloc()
263{
264 register struct file *fp;
265 register i;
266
267 i = ufalloc();
0a34b6fd 268 if (i < 0)
fdd90ee3 269 return (NULL);
0a34b6fd
BJ
270 if (lastf == 0)
271 lastf = file;
272 for (fp = lastf; fp < fileNFILE; fp++)
273 if (fp->f_count == 0)
9c98386a 274 goto slot;
0a34b6fd
BJ
275 for (fp = file; fp < lastf; fp++)
276 if (fp->f_count == 0)
9c98386a 277 goto slot;
73317613 278 tablefull("file");
fc96c615 279 u.u_error = ENFILE;
0a34b6fd 280 return (NULL);
9c98386a
BJ
281slot:
282 u.u_ofile[i] = fp;
283 fp->f_count++;
fdd90ee3 284 fp->f_offset = 0;
eede782b 285 fp->f_inode = 0;
9c98386a 286 lastf = fp + 1;
0a34b6fd 287 return (fp);
fc96c615 288}