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