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