Commit | Line | Data |
---|---|---|
3650c37d | 1 | /* vfs_syscalls.c 4.43 82/12/09 */ |
6459ebe0 | 2 | |
d67a03eb BJ |
3 | #include "../h/param.h" |
4 | #include "../h/systm.h" | |
d67a03eb BJ |
5 | #include "../h/dir.h" |
6 | #include "../h/user.h" | |
35e7c31a | 7 | #include "../h/kernel.h" |
d67a03eb | 8 | #include "../h/file.h" |
6459ebe0 | 9 | #include "../h/stat.h" |
3e78e260 | 10 | #include "../h/inode.h" |
6459ebe0 | 11 | #include "../h/fs.h" |
3e78e260 | 12 | #include "../h/buf.h" |
3e78e260 | 13 | #include "../h/proc.h" |
0a77f278 | 14 | #include "../h/quota.h" |
4147b3f6 | 15 | #include "../h/descrip.h" |
31949305 BJ |
16 | #include "../h/uio.h" |
17 | #include "../h/socket.h" | |
b32450f4 | 18 | #include "../h/socketvar.h" |
4f083fd7 | 19 | #include "../h/nami.h" |
3e78e260 | 20 | |
4f083fd7 SL |
21 | /* |
22 | * Change current working directory (``.''). | |
23 | */ | |
3e78e260 BJ |
24 | chdir() |
25 | { | |
26 | ||
27 | chdirec(&u.u_cdir); | |
28 | } | |
29 | ||
4f083fd7 SL |
30 | /* |
31 | * Change notion of root (``/'') directory. | |
32 | */ | |
3e78e260 BJ |
33 | chroot() |
34 | { | |
35 | ||
36 | if (suser()) | |
37 | chdirec(&u.u_rdir); | |
38 | } | |
39 | ||
4f083fd7 SL |
40 | /* |
41 | * Common routine for chroot and chdir. | |
42 | */ | |
3e78e260 | 43 | chdirec(ipp) |
528f664c | 44 | register struct inode **ipp; |
3e78e260 BJ |
45 | { |
46 | register struct inode *ip; | |
47 | struct a { | |
48 | char *fname; | |
49 | }; | |
50 | ||
4f083fd7 SL |
51 | ip = namei(uchar, LOOKUP, 1); |
52 | if (ip == NULL) | |
3e78e260 | 53 | return; |
4f083fd7 | 54 | if ((ip->i_mode&IFMT) != IFDIR) { |
3e78e260 BJ |
55 | u.u_error = ENOTDIR; |
56 | goto bad; | |
57 | } | |
4f083fd7 | 58 | if (access(ip, IEXEC)) |
3e78e260 | 59 | goto bad; |
ca91bded | 60 | iunlock(ip); |
8eee8525 KM |
61 | if (*ipp) |
62 | irele(*ipp); | |
3e78e260 BJ |
63 | *ipp = ip; |
64 | return; | |
65 | ||
66 | bad: | |
67 | iput(ip); | |
68 | } | |
69 | ||
70 | /* | |
71 | * Open system call. | |
72 | */ | |
73 | open() | |
74 | { | |
75 | register struct inode *ip; | |
76 | register struct a { | |
77 | char *fname; | |
528f664c SL |
78 | int flags; |
79 | int mode; | |
3e78e260 | 80 | } *uap; |
4f083fd7 | 81 | int checkpermissions = 1, flags; |
3e78e260 BJ |
82 | |
83 | uap = (struct a *)u.u_ap; | |
4f083fd7 SL |
84 | flags = uap->flags + 1; |
85 | if ((flags&FTRUNCATE) && (flags&FWRITE) == 0) { | |
86 | u.u_error = EINVAL; | |
87 | return; | |
88 | } | |
89 | if (flags&FCREATE) { | |
90 | ip = namei(uchar, CREATE, 1); | |
528f664c SL |
91 | if (ip == NULL) { |
92 | if (u.u_error) | |
93 | return; | |
94 | ip = maknode(uap->mode&07777&(~ISVTX)); | |
95 | checkpermissions = 0; | |
4f083fd7 | 96 | flags &= ~FTRUNCATE; |
528f664c SL |
97 | } |
98 | } else | |
4f083fd7 | 99 | ip = namei(uchar, LOOKUP, 1); |
3e78e260 BJ |
100 | if (ip == NULL) |
101 | return; | |
4f083fd7 | 102 | open1(ip, flags, checkpermissions); |
3e78e260 BJ |
103 | } |
104 | ||
528f664c | 105 | #ifndef NOCOMPAT |
3e78e260 BJ |
106 | /* |
107 | * Creat system call. | |
108 | */ | |
4147b3f6 | 109 | ocreat() |
3e78e260 BJ |
110 | { |
111 | register struct inode *ip; | |
112 | register struct a { | |
113 | char *fname; | |
114 | int fmode; | |
115 | } *uap; | |
116 | ||
117 | uap = (struct a *)u.u_ap; | |
4f083fd7 | 118 | ip = namei(uchar, CREATE, 1); |
3e78e260 BJ |
119 | if (ip == NULL) { |
120 | if (u.u_error) | |
121 | return; | |
122 | ip = maknode(uap->fmode&07777&(~ISVTX)); | |
528f664c | 123 | if (ip == NULL) |
3e78e260 | 124 | return; |
528f664c | 125 | open1(ip, FWRITE, 0); |
3e78e260 | 126 | } else |
4f083fd7 | 127 | open1(ip, FWRITE|FTRUNCATE, 1); |
3e78e260 | 128 | } |
528f664c | 129 | #endif |
3e78e260 BJ |
130 | |
131 | /* | |
132 | * Common code for open and creat. | |
528f664c SL |
133 | * Check permissions (if we haven't done so already), |
134 | * allocate an open file structure, and call | |
135 | * the device open routine, if any. | |
3e78e260 | 136 | */ |
528f664c | 137 | open1(ip, mode, checkpermissions) |
3e78e260 BJ |
138 | register struct inode *ip; |
139 | register mode; | |
140 | { | |
141 | register struct file *fp; | |
528f664c | 142 | int i, flags; |
3e78e260 | 143 | |
528f664c | 144 | if (checkpermissions) { |
3e78e260 | 145 | if (mode&FREAD) |
528f664c SL |
146 | if (access(ip, IREAD)) |
147 | goto bad; | |
3e78e260 | 148 | if (mode&FWRITE) { |
528f664c SL |
149 | if (access(ip, IWRITE)) |
150 | goto bad; | |
151 | if ((ip->i_mode&IFMT) == IFDIR) { | |
3e78e260 | 152 | u.u_error = EISDIR; |
528f664c SL |
153 | goto bad; |
154 | } | |
3e78e260 BJ |
155 | } |
156 | } | |
528f664c SL |
157 | |
158 | /* | |
159 | * Check locking on inode. Release "inode lock" | |
160 | * while doing so in case we block inside flocki. | |
161 | */ | |
162 | flags = 0; | |
4f083fd7 | 163 | if (mode&(FSHLOCK|FEXLOCK)) { |
528f664c SL |
164 | iunlock(ip); |
165 | flags = flocki(ip, 0, mode); | |
166 | ilock(ip); | |
167 | if (u.u_error) | |
168 | goto bad; | |
8eee8525 | 169 | } |
528f664c | 170 | if (mode&FTRUNCATE) |
4f083fd7 | 171 | itrunc(ip, (u_long)0); |
ca91bded | 172 | iunlock(ip); |
3e78e260 BJ |
173 | if ((fp = falloc()) == NULL) |
174 | goto out; | |
528f664c | 175 | fp->f_flag = mode & FMODES; |
4147b3f6 | 176 | fp->f_type = DTYPE_FILE; |
3e78e260 BJ |
177 | i = u.u_r.r_val1; |
178 | fp->f_inode = ip; | |
f45396c3 | 179 | u.u_error = openi(ip, mode); |
528f664c SL |
180 | if (u.u_error == 0) { |
181 | u.u_pofile[i] = flags; | |
3e78e260 | 182 | return; |
528f664c | 183 | } |
3e78e260 BJ |
184 | u.u_ofile[i] = NULL; |
185 | fp->f_count--; | |
186 | out: | |
8eee8525 | 187 | irele(ip); |
528f664c SL |
188 | return; |
189 | bad: | |
190 | iput(ip); | |
3e78e260 BJ |
191 | } |
192 | ||
193 | /* | |
194 | * Mknod system call | |
195 | */ | |
196 | mknod() | |
197 | { | |
198 | register struct inode *ip; | |
199 | register struct a { | |
200 | char *fname; | |
201 | int fmode; | |
202 | int dev; | |
203 | } *uap; | |
204 | ||
205 | uap = (struct a *)u.u_ap; | |
206 | if (suser()) { | |
4f083fd7 | 207 | ip = namei(uchar, CREATE, 0); |
3e78e260 BJ |
208 | if (ip != NULL) { |
209 | u.u_error = EEXIST; | |
210 | goto out; | |
211 | } | |
212 | } | |
213 | if (u.u_error) | |
214 | return; | |
215 | ip = maknode(uap->fmode); | |
216 | if (ip == NULL) | |
217 | return; | |
218 | if (uap->dev) { | |
219 | /* | |
220 | * Want to be able to use this to make badblock | |
221 | * inodes, so don't truncate the dev number. | |
222 | */ | |
6459ebe0 | 223 | ip->i_rdev = uap->dev; |
3e78e260 BJ |
224 | ip->i_flag |= IACC|IUPD|ICHG; |
225 | } | |
226 | ||
227 | out: | |
228 | iput(ip); | |
229 | } | |
230 | ||
231 | /* | |
232 | * link system call | |
233 | */ | |
234 | link() | |
235 | { | |
236 | register struct inode *ip, *xp; | |
237 | register struct a { | |
238 | char *target; | |
239 | char *linkname; | |
240 | } *uap; | |
241 | ||
242 | uap = (struct a *)u.u_ap; | |
4f083fd7 | 243 | ip = namei(uchar, LOOKUP, 1); /* well, this routine is doomed anyhow */ |
3e78e260 BJ |
244 | if (ip == NULL) |
245 | return; | |
4f083fd7 | 246 | if ((ip->i_mode&IFMT) == IFDIR && !suser()) { |
f94ceb3b BJ |
247 | iput(ip); |
248 | return; | |
249 | } | |
3e78e260 BJ |
250 | ip->i_nlink++; |
251 | ip->i_flag |= ICHG; | |
3fd23f5c | 252 | iupdat(ip, &time, &time, 1); |
ca91bded | 253 | iunlock(ip); |
3e78e260 | 254 | u.u_dirp = (caddr_t)uap->linkname; |
4f083fd7 | 255 | xp = namei(uchar, CREATE, 0); |
3e78e260 BJ |
256 | if (xp != NULL) { |
257 | u.u_error = EEXIST; | |
258 | iput(xp); | |
259 | goto out; | |
260 | } | |
261 | if (u.u_error) | |
262 | goto out; | |
263 | if (u.u_pdir->i_dev != ip->i_dev) { | |
264 | iput(u.u_pdir); | |
265 | u.u_error = EXDEV; | |
266 | goto out; | |
267 | } | |
64d3a787 | 268 | direnter(ip); |
3e78e260 BJ |
269 | out: |
270 | if (u.u_error) { | |
271 | ip->i_nlink--; | |
272 | ip->i_flag |= ICHG; | |
273 | } | |
8eee8525 | 274 | irele(ip); |
3e78e260 BJ |
275 | } |
276 | ||
277 | /* | |
278 | * symlink -- make a symbolic link | |
279 | */ | |
280 | symlink() | |
281 | { | |
282 | register struct a { | |
283 | char *target; | |
284 | char *linkname; | |
285 | } *uap; | |
286 | register struct inode *ip; | |
287 | register char *tp; | |
288 | register c, nc; | |
289 | ||
290 | uap = (struct a *)u.u_ap; | |
291 | tp = uap->target; | |
292 | nc = 0; | |
293 | while (c = fubyte(tp)) { | |
294 | if (c < 0) { | |
295 | u.u_error = EFAULT; | |
296 | return; | |
297 | } | |
298 | tp++; | |
299 | nc++; | |
300 | } | |
301 | u.u_dirp = uap->linkname; | |
4f083fd7 | 302 | ip = namei(uchar, CREATE, 0); |
3e78e260 BJ |
303 | if (ip) { |
304 | iput(ip); | |
305 | u.u_error = EEXIST; | |
306 | return; | |
307 | } | |
308 | if (u.u_error) | |
309 | return; | |
310 | ip = maknode(IFLNK | 0777); | |
311 | if (ip == NULL) | |
312 | return; | |
31949305 | 313 | u.u_error = rdwri(UIO_WRITE, ip, uap->target, nc, 0, 0, (int *)0); |
4f083fd7 | 314 | /* handle u.u_error != 0 */ |
3e78e260 BJ |
315 | iput(ip); |
316 | } | |
317 | ||
318 | /* | |
319 | * Unlink system call. | |
320 | * Hard to avoid races here, especially | |
321 | * in unlinking directories. | |
322 | */ | |
323 | unlink() | |
324 | { | |
3e78e260 BJ |
325 | struct a { |
326 | char *fname; | |
327 | }; | |
4f083fd7 | 328 | register struct inode *ip, *dp; |
3e78e260 | 329 | |
4f083fd7 SL |
330 | ip = namei(uchar, DELETE | LOCKPARENT, 0); |
331 | if (ip == NULL) | |
3e78e260 | 332 | return; |
4f083fd7 SL |
333 | dp = u.u_pdir; |
334 | if ((ip->i_mode&IFMT) == IFDIR && !suser()) | |
3e78e260 BJ |
335 | goto out; |
336 | /* | |
337 | * Don't unlink a mounted file. | |
338 | */ | |
4f083fd7 | 339 | if (ip->i_dev != dp->i_dev) { |
3e78e260 BJ |
340 | u.u_error = EBUSY; |
341 | goto out; | |
342 | } | |
343 | if (ip->i_flag&ITEXT) | |
344 | xrele(ip); /* try once to free text */ | |
64d3a787 BJ |
345 | if (dirremove()) { |
346 | ip->i_nlink--; | |
347 | ip->i_flag |= ICHG; | |
6459ebe0 | 348 | } |
3e78e260 | 349 | out: |
4f083fd7 | 350 | if (dp == ip) |
8eee8525 KM |
351 | irele(ip); |
352 | else | |
353 | iput(ip); | |
4f083fd7 | 354 | iput(dp); |
3e78e260 BJ |
355 | } |
356 | ||
357 | /* | |
358 | * Seek system call | |
359 | */ | |
35e7c31a | 360 | lseek() |
3e78e260 BJ |
361 | { |
362 | register struct file *fp; | |
363 | register struct a { | |
528f664c | 364 | int fd; |
3e78e260 BJ |
365 | off_t off; |
366 | int sbase; | |
367 | } *uap; | |
368 | ||
369 | uap = (struct a *)u.u_ap; | |
528f664c | 370 | fp = getf(uap->fd); |
3e78e260 BJ |
371 | if (fp == NULL) |
372 | return; | |
4147b3f6 | 373 | if (fp->f_type == DTYPE_SOCKET) { |
3e78e260 BJ |
374 | u.u_error = ESPIPE; |
375 | return; | |
376 | } | |
528f664c | 377 | if (uap->sbase == FSEEK_RELATIVE) |
3e78e260 | 378 | uap->off += fp->f_offset; |
528f664c | 379 | else if (uap->sbase == FSEEK_EOF) |
3e78e260 BJ |
380 | uap->off += fp->f_inode->i_size; |
381 | fp->f_offset = uap->off; | |
382 | u.u_r.r_off = uap->off; | |
383 | } | |
384 | ||
385 | /* | |
386 | * Access system call | |
387 | */ | |
388 | saccess() | |
389 | { | |
390 | register svuid, svgid; | |
391 | register struct inode *ip; | |
392 | register struct a { | |
393 | char *fname; | |
394 | int fmode; | |
395 | } *uap; | |
396 | ||
397 | uap = (struct a *)u.u_ap; | |
398 | svuid = u.u_uid; | |
399 | svgid = u.u_gid; | |
400 | u.u_uid = u.u_ruid; | |
401 | u.u_gid = u.u_rgid; | |
4f083fd7 | 402 | ip = namei(uchar, LOOKUP, 1); |
3e78e260 | 403 | if (ip != NULL) { |
4f083fd7 | 404 | if ((uap->fmode&FACCESS_READ) && access(ip, IREAD)) |
528f664c | 405 | goto done; |
4f083fd7 | 406 | if ((uap->fmode&FACCESS_WRITE) && access(ip, IWRITE)) |
528f664c | 407 | goto done; |
4f083fd7 | 408 | if ((uap->fmode&FACCESS_EXECUTE) && access(ip, IEXEC)) |
528f664c SL |
409 | goto done; |
410 | done: | |
3e78e260 BJ |
411 | iput(ip); |
412 | } | |
413 | u.u_uid = svuid; | |
414 | u.u_gid = svgid; | |
415 | } | |
d67a03eb BJ |
416 | |
417 | /* | |
418 | * the fstat system call. | |
419 | */ | |
420 | fstat() | |
421 | { | |
422 | register struct file *fp; | |
423 | register struct a { | |
528f664c | 424 | int fd; |
d67a03eb BJ |
425 | struct stat *sb; |
426 | } *uap; | |
427 | ||
428 | uap = (struct a *)u.u_ap; | |
528f664c | 429 | fp = getf(uap->fd); |
e92a04af | 430 | if (fp == NULL) |
d67a03eb | 431 | return; |
4147b3f6 | 432 | if (fp->f_type == DTYPE_SOCKET) |
cc15ab5d | 433 | u.u_error = sostat(fp->f_socket, uap->sb); |
e92a04af BJ |
434 | else |
435 | stat1(fp->f_inode, uap->sb); | |
d67a03eb BJ |
436 | } |
437 | ||
438 | /* | |
6459ebe0 | 439 | * Stat system call. This version follows links. |
d67a03eb BJ |
440 | */ |
441 | stat() | |
442 | { | |
443 | register struct inode *ip; | |
444 | register struct a { | |
445 | char *fname; | |
446 | struct stat *sb; | |
447 | } *uap; | |
448 | ||
449 | uap = (struct a *)u.u_ap; | |
4f083fd7 | 450 | ip = namei(uchar, LOOKUP, 1); |
e92a04af | 451 | if (ip == NULL) |
d67a03eb | 452 | return; |
a3aa7f92 | 453 | stat1(ip, uap->sb); |
d67a03eb BJ |
454 | iput(ip); |
455 | } | |
456 | ||
5485e062 | 457 | /* |
6459ebe0 | 458 | * Lstat system call. This version does not follow links. |
5485e062 BJ |
459 | */ |
460 | lstat() | |
461 | { | |
462 | register struct inode *ip; | |
463 | register struct a { | |
464 | char *fname; | |
465 | struct stat *sb; | |
466 | } *uap; | |
467 | ||
468 | uap = (struct a *)u.u_ap; | |
4f083fd7 | 469 | ip = namei(uchar, LOOKUP, 0); |
5485e062 BJ |
470 | if (ip == NULL) |
471 | return; | |
019b08c9 | 472 | stat1(ip, uap->sb); |
5485e062 BJ |
473 | iput(ip); |
474 | } | |
475 | ||
d67a03eb BJ |
476 | /* |
477 | * The basic routine for fstat and stat: | |
478 | * get the inode and pass appropriate parts back. | |
479 | */ | |
a3aa7f92 | 480 | stat1(ip, ub) |
e92a04af BJ |
481 | register struct inode *ip; |
482 | struct stat *ub; | |
d67a03eb | 483 | { |
d67a03eb BJ |
484 | struct stat ds; |
485 | ||
3fd23f5c | 486 | IUPDAT(ip, &time, &time, 0); |
d67a03eb | 487 | /* |
09c42945 | 488 | * Copy from inode table |
d67a03eb BJ |
489 | */ |
490 | ds.st_dev = ip->i_dev; | |
491 | ds.st_ino = ip->i_number; | |
492 | ds.st_mode = ip->i_mode; | |
493 | ds.st_nlink = ip->i_nlink; | |
494 | ds.st_uid = ip->i_uid; | |
495 | ds.st_gid = ip->i_gid; | |
6459ebe0 | 496 | ds.st_rdev = (dev_t)ip->i_rdev; |
a3aa7f92 | 497 | ds.st_size = ip->i_size; |
6459ebe0 KM |
498 | ds.st_atime = ip->i_atime; |
499 | ds.st_mtime = ip->i_mtime; | |
500 | ds.st_ctime = ip->i_ctime; | |
528f664c SL |
501 | /* this doesn't belong here */ |
502 | if ((ip->i_mode&IFMT) == IFBLK) | |
503 | ds.st_blksize = BLKDEV_IOSIZE; | |
504 | else if ((ip->i_mode&IFMT) == IFCHR) | |
505 | ds.st_blksize = MAXBSIZE; | |
506 | else | |
507 | ds.st_blksize = ip->i_fs->fs_bsize; | |
d67a03eb BJ |
508 | if (copyout((caddr_t)&ds, (caddr_t)ub, sizeof(ds)) < 0) |
509 | u.u_error = EFAULT; | |
510 | } | |
511 | ||
512 | /* | |
5485e062 BJ |
513 | * Return target name of a symbolic link |
514 | */ | |
515 | readlink() | |
516 | { | |
517 | register struct inode *ip; | |
518 | register struct a { | |
519 | char *name; | |
520 | char *buf; | |
521 | int count; | |
31949305 BJ |
522 | } *uap = (struct a *)u.u_ap; |
523 | int resid; | |
5485e062 | 524 | |
4f083fd7 | 525 | ip = namei(uchar, LOOKUP, 0); |
5485e062 BJ |
526 | if (ip == NULL) |
527 | return; | |
528 | if ((ip->i_mode&IFMT) != IFLNK) { | |
529 | u.u_error = ENXIO; | |
530 | goto out; | |
531 | } | |
31949305 | 532 | u.u_error = rdwri(UIO_READ, ip, uap->buf, uap->count, 0, 0, &resid); |
5485e062 BJ |
533 | out: |
534 | iput(ip); | |
31949305 | 535 | u.u_r.r_val1 = uap->count - resid; |
5485e062 BJ |
536 | } |
537 | ||
4f083fd7 SL |
538 | /* |
539 | * Change mode of a file given path name. | |
540 | */ | |
3e78e260 | 541 | chmod() |
5485e062 | 542 | { |
528f664c SL |
543 | struct inode *ip; |
544 | struct a { | |
3e78e260 BJ |
545 | char *fname; |
546 | int fmode; | |
5485e062 | 547 | } *uap; |
5485e062 BJ |
548 | |
549 | uap = (struct a *)u.u_ap; | |
3e78e260 | 550 | if ((ip = owner(1)) == NULL) |
5485e062 | 551 | return; |
528f664c | 552 | chmod1(ip, uap->fmode); |
4f083fd7 | 553 | iput(ip); |
528f664c | 554 | } |
f94ceb3b | 555 | |
4f083fd7 SL |
556 | /* |
557 | * Change mode of a file given a file descriptor. | |
558 | */ | |
528f664c SL |
559 | fchmod() |
560 | { | |
561 | struct a { | |
562 | int fd; | |
563 | int fmode; | |
564 | } *uap; | |
565 | register struct inode *ip; | |
566 | register struct file *fp; | |
567 | ||
568 | uap = (struct a *)u.u_ap; | |
569 | fp = getf(uap->fd); | |
570 | if (fp == NULL) | |
571 | return; | |
572 | if (fp->f_type == DTYPE_SOCKET) { | |
573 | u.u_error = EINVAL; | |
574 | return; | |
f94ceb3b | 575 | } |
528f664c | 576 | ip = fp->f_inode; |
4f083fd7 | 577 | if (u.u_uid != ip->i_uid && !suser()) |
528f664c | 578 | return; |
4f083fd7 | 579 | ilock(ip); |
528f664c | 580 | chmod1(ip, uap->fmode); |
4f083fd7 | 581 | iunlock(ip); |
528f664c SL |
582 | } |
583 | ||
4f083fd7 SL |
584 | /* |
585 | * Change the mode on a file. | |
586 | * Inode must be locked before calling. | |
587 | */ | |
528f664c SL |
588 | chmod1(ip, mode) |
589 | register struct inode *ip; | |
590 | register int mode; | |
591 | { | |
197da11b BJ |
592 | register int *gp; |
593 | ||
3e78e260 | 594 | ip->i_mode &= ~07777; |
f94ceb3b | 595 | if (u.u_uid) { |
528f664c | 596 | mode &= ~ISVTX; |
197da11b BJ |
597 | for (gp = u.u_groups; gp < &u.u_groups[NGROUPS]; gp++) |
598 | if (*gp == ip->i_gid) | |
599 | goto ok; | |
600 | mode &= ~ISGID; | |
601 | ok: | |
602 | ; | |
528f664c | 603 | #ifdef MUSH |
0a77f278 RE |
604 | if (u.u_quota->q_syflags & QF_UMASK && u.u_uid != 0 && |
605 | (ip->i_mode & IFMT) != IFCHR) | |
528f664c | 606 | mode &= ~u.u_cmask; |
0a77f278 | 607 | #endif |
f94ceb3b | 608 | } |
528f664c | 609 | ip->i_mode |= mode&07777; |
3e78e260 BJ |
610 | ip->i_flag |= ICHG; |
611 | if (ip->i_flag&ITEXT && (ip->i_mode&ISVTX)==0) | |
612 | xrele(ip); | |
5485e062 BJ |
613 | } |
614 | ||
4f083fd7 SL |
615 | /* |
616 | * Set ownership given a path name. | |
617 | */ | |
3e78e260 | 618 | chown() |
d67a03eb | 619 | { |
528f664c SL |
620 | struct inode *ip; |
621 | struct a { | |
3e78e260 BJ |
622 | char *fname; |
623 | int uid; | |
624 | int gid; | |
d67a03eb | 625 | } *uap; |
d67a03eb BJ |
626 | |
627 | uap = (struct a *)u.u_ap; | |
3e78e260 | 628 | if (!suser() || (ip = owner(0)) == NULL) |
d67a03eb | 629 | return; |
528f664c | 630 | chown1(ip, uap->uid, uap->gid); |
4f083fd7 | 631 | iput(ip); |
528f664c | 632 | } |
f94ceb3b | 633 | |
4f083fd7 SL |
634 | /* |
635 | * Set ownership given a file descriptor. | |
636 | */ | |
528f664c SL |
637 | fchown() |
638 | { | |
639 | struct a { | |
640 | int fd; | |
641 | int uid; | |
642 | int gid; | |
643 | } *uap; | |
644 | register struct inode *ip; | |
645 | register struct file *fp; | |
646 | ||
647 | uap = (struct a *)u.u_ap; | |
648 | fp = getf(uap->fd); | |
649 | if (fp == NULL) | |
650 | return; | |
651 | if (fp->f_type == DTYPE_SOCKET) { | |
652 | u.u_error = EINVAL; | |
653 | return; | |
f94ceb3b | 654 | } |
528f664c | 655 | ip = fp->f_inode; |
4f083fd7 | 656 | if (!suser()) |
528f664c | 657 | return; |
4f083fd7 | 658 | ilock(ip); |
528f664c | 659 | chown1(ip, uap->uid, uap->gid); |
4f083fd7 | 660 | iunlock(ip); |
528f664c SL |
661 | } |
662 | ||
663 | /* | |
664 | * Perform chown operation on inode ip; | |
665 | * inode must be locked prior to call. | |
666 | */ | |
667 | chown1(ip, uid, gid) | |
668 | register struct inode *ip; | |
669 | int uid, gid; | |
670 | { | |
671 | #ifdef QUOTA | |
672 | register long change; | |
673 | ||
0a77f278 RE |
674 | /* |
675 | * This doesn't allow for holes in files (which hopefully don't | |
676 | * happen often in files that we chown), and is not accurate anyway | |
677 | * (eg: it totally ignores 3 level indir blk files - but hopefully | |
678 | * noone who can make a file that big will have a quota) | |
679 | */ | |
528f664c | 680 | if (ip->i_uid == uid) |
0a77f278 RE |
681 | change = 0; |
682 | else { | |
683 | register struct fs *fs = ip->i_fs; | |
684 | ||
685 | if (ip->i_size > (change = NDADDR * fs->fs_bsize)) { | |
686 | register off_t size; | |
687 | ||
688 | size = blkroundup(fs, ip->i_size) - change; | |
689 | change += size; | |
690 | change += fs->fs_bsize; | |
528f664c | 691 | /* this assumes NIADDR <= 2 */ |
0a77f278 RE |
692 | if (size > NINDIR(fs) * fs->fs_bsize) |
693 | change += fs->fs_bsize; | |
694 | } else | |
695 | change = fragroundup(fs, ip->i_size); | |
696 | change /= DEV_BSIZE; | |
697 | } | |
4f083fd7 SL |
698 | (void)chkdq(ip, -change, 1); |
699 | (void)chkiq(ip->i_dev, ip, ip->i_uid, 1); | |
0a77f278 | 700 | dqrele(ip->i_dquot); |
f94ceb3b BJ |
701 | #endif |
702 | /* | |
528f664c SL |
703 | * keep uid/gid's in sane range -- no err, |
704 | * so chown(file, uid, -1) will do something useful | |
f94ceb3b | 705 | */ |
528f664c SL |
706 | if (uid >= 0 && uid <= 32767) /* should have a constant */ |
707 | ip->i_uid = uid; | |
708 | if (gid >= 0 && gid <= 32767) /* same here */ | |
709 | ip->i_gid = gid; | |
3e78e260 BJ |
710 | ip->i_flag |= ICHG; |
711 | if (u.u_ruid != 0) | |
712 | ip->i_mode &= ~(ISUID|ISGID); | |
528f664c | 713 | #ifdef QUOTA |
0a77f278 | 714 | ip->i_dquot = inoquota(ip); |
4f083fd7 SL |
715 | (void)chkdq(ip, change, 1); |
716 | (void)chkiq(ip->i_dev, (struct inode *)NULL, uid, 1); | |
0a77f278 | 717 | #endif |
d67a03eb BJ |
718 | } |
719 | ||
720 | /* | |
3e78e260 BJ |
721 | * Set IUPD and IACC times on file. |
722 | * Can't set ICHG. | |
d67a03eb | 723 | */ |
bc671164 | 724 | outime() |
e92a04af | 725 | { |
d67a03eb | 726 | register struct a { |
3e78e260 BJ |
727 | char *fname; |
728 | time_t *tptr; | |
d67a03eb | 729 | } *uap; |
3e78e260 BJ |
730 | register struct inode *ip; |
731 | time_t tv[2]; | |
b32450f4 | 732 | struct timeval tv0, tv1; |
d67a03eb BJ |
733 | |
734 | uap = (struct a *)u.u_ap; | |
3e78e260 | 735 | if ((ip = owner(1)) == NULL) |
d67a03eb | 736 | return; |
3e78e260 BJ |
737 | if (copyin((caddr_t)uap->tptr, (caddr_t)tv, sizeof(tv))) { |
738 | u.u_error = EFAULT; | |
739 | } else { | |
740 | ip->i_flag |= IACC|IUPD|ICHG; | |
b32450f4 BJ |
741 | tv0.tv_sec = tv[0]; tv0.tv_usec = 0; |
742 | tv1.tv_sec = tv[1]; tv1.tv_usec = 0; | |
743 | iupdat(ip, &tv0, &tv1, 0); | |
d67a03eb | 744 | } |
d67a03eb BJ |
745 | iput(ip); |
746 | } | |
747 | ||
4f083fd7 SL |
748 | /* |
749 | * Flush any pending I/O. | |
750 | */ | |
3e78e260 | 751 | sync() |
d67a03eb | 752 | { |
d67a03eb | 753 | |
3fd23f5c | 754 | update(); |
d67a03eb | 755 | } |
64d3a787 | 756 | |
4f083fd7 SL |
757 | /* |
758 | * Apply an advisory lock on a file descriptor. | |
759 | */ | |
528f664c SL |
760 | flock() |
761 | { | |
762 | struct a { | |
763 | int fd; | |
764 | int how; | |
765 | } *uap; | |
766 | register struct file *fp; | |
767 | register int cmd, flags; | |
768 | ||
769 | uap = (struct a *)u.u_ap; | |
770 | fp = getf(uap->fd); | |
771 | if (fp == NULL) | |
772 | return; | |
773 | if (fp->f_type == DTYPE_SOCKET) { /* XXX */ | |
774 | u.u_error = EINVAL; | |
775 | return; | |
776 | } | |
777 | cmd = uap->how; | |
3650c37d | 778 | flags = u.u_pofile[uap->fd] & (UF_SHLOCK|UF_EXLOCK); |
528f664c SL |
779 | if (cmd&FUNLOCK) { |
780 | if (flags == 0) { | |
781 | u.u_error = EINVAL; | |
782 | return; | |
783 | } | |
784 | funlocki(fp->f_inode, flags); | |
3650c37d | 785 | u.u_pofile[uap->fd] &= ~(UF_SHLOCK|UF_EXLOCK); |
528f664c SL |
786 | return; |
787 | } | |
788 | /* | |
789 | * No reason to write lock a file we've already | |
790 | * write locked, similarly with a read lock. | |
791 | */ | |
3650c37d SL |
792 | if ((flags&UF_EXLOCK) && (cmd&FEXLOCK) || |
793 | (flags&UF_SHLOCK) && (cmd&FSHLOCK)) | |
528f664c SL |
794 | return; |
795 | u.u_pofile[uap->fd] = flocki(fp->f_inode, u.u_pofile[uap->fd], cmd); | |
796 | } | |
797 | ||
4f083fd7 SL |
798 | /* |
799 | * Truncate a file given its path name. | |
800 | */ | |
528f664c SL |
801 | truncate() |
802 | { | |
803 | struct a { | |
804 | char *fname; | |
4f083fd7 | 805 | u_long length; |
31949305 | 806 | } *uap = (struct a *)u.u_ap; |
528f664c SL |
807 | struct inode *ip; |
808 | ||
4f083fd7 | 809 | ip = namei(uchar, LOOKUP, 1); |
528f664c SL |
810 | if (ip == NULL) |
811 | return; | |
812 | if (access(ip, IWRITE)) | |
813 | goto bad; | |
814 | if ((ip->i_mode&IFMT) == IFDIR) { | |
815 | u.u_error = EISDIR; | |
816 | goto bad; | |
817 | } | |
818 | itrunc(ip, uap->length); | |
528f664c SL |
819 | bad: |
820 | iput(ip); | |
821 | } | |
822 | ||
4f083fd7 SL |
823 | /* |
824 | * Truncate a file given a file descriptor. | |
825 | */ | |
528f664c SL |
826 | ftruncate() |
827 | { | |
828 | struct a { | |
829 | int fd; | |
4f083fd7 | 830 | u_long length; |
31949305 | 831 | } *uap = (struct a *)u.u_ap; |
528f664c SL |
832 | struct inode *ip; |
833 | struct file *fp; | |
834 | ||
835 | fp = getf(uap->fd); | |
836 | if (fp == NULL) | |
837 | return; | |
838 | if (fp->f_type == DTYPE_SOCKET) { | |
839 | u.u_error = EINVAL; | |
840 | return; | |
841 | } | |
842 | if ((fp->f_flag&FWRITE) == 0) { | |
843 | u.u_error = EINVAL; | |
844 | return; | |
845 | } | |
846 | ip = fp->f_inode; | |
847 | ilock(ip); | |
848 | itrunc(ip, uap->length); | |
4f083fd7 SL |
849 | iunlock(ip); |
850 | } | |
851 | ||
852 | /* | |
853 | * Synch an open file. | |
854 | */ | |
855 | fsync() | |
856 | { | |
857 | struct a { | |
858 | int fd; | |
859 | } *uap = (struct a *)u.u_ap; | |
860 | struct inode *ip; | |
861 | struct file *fp; | |
862 | ||
863 | fp = getf(uap->fd); | |
864 | if (fp == NULL) | |
865 | return; | |
866 | if (fp->f_type == DTYPE_SOCKET) { | |
867 | u.u_error = EINVAL; | |
868 | return; | |
869 | } | |
870 | ip = fp->f_inode; | |
871 | ilock(ip); | |
872 | syncip(ip); | |
873 | iunlock(ip); | |
528f664c SL |
874 | } |
875 | ||
4f083fd7 SL |
876 | /* |
877 | * Rename system call. | |
878 | * rename("foo", "bar"); | |
879 | * is essentially | |
880 | * unlink("bar"); | |
881 | * link("foo", "bar"); | |
882 | * unlink("foo"); | |
883 | * but ``atomically''. Can't do full commit without saving state in the | |
884 | * inode on disk which isn't feasible at this time. Best we can do is | |
885 | * always guarantee the target exists. | |
886 | * | |
887 | * Basic algorithm is: | |
888 | * | |
889 | * 1) Bump link count on source while we're linking it to the | |
890 | * target. This also insure the inode won't be deleted out | |
891 | * from underneath us while we work. | |
892 | * 2) Link source to destination. If destination already exists, | |
893 | * delete it first. | |
894 | * 3) Unlink source reference to inode if still around. | |
895 | * 4) If a directory was moved and the parent of the destination | |
896 | * is different from the source, patch the ".." entry in the | |
897 | * directory. | |
898 | * | |
899 | * Source and destination must either both be directories, or both | |
900 | * not be directories. If target is a directory, it must be empty. | |
901 | */ | |
528f664c SL |
902 | rename() |
903 | { | |
904 | struct a { | |
905 | char *from; | |
906 | char *to; | |
907 | } *uap; | |
4f083fd7 SL |
908 | register struct inode *ip, *xp, *dp; |
909 | int oldparent, parentdifferent, doingdirectory; | |
910 | ||
911 | uap = (struct a *)u.u_ap; | |
912 | ip = namei(uchar, LOOKUP | LOCKPARENT, 0); | |
913 | if (ip == NULL) | |
914 | return; | |
915 | dp = u.u_pdir; | |
916 | oldparent = 0, doingdirectory = 0; | |
917 | if ((ip->i_mode&IFMT) == IFDIR) { | |
918 | register struct direct *d; | |
919 | ||
920 | d = &u.u_dent; | |
921 | /* | |
922 | * Avoid "." and ".." for obvious reasons. | |
923 | */ | |
924 | if (d->d_name[0] == '.') { | |
925 | if (d->d_namlen == 1 || | |
926 | (d->d_namlen == 2 && d->d_name[1] == '.')) { | |
927 | u.u_error = EINVAL; | |
928 | iput(ip); | |
929 | return; | |
930 | } | |
931 | } | |
932 | oldparent = dp->i_number; | |
933 | doingdirectory++; | |
934 | } | |
935 | irele(dp); | |
936 | ||
937 | /* | |
938 | * 1) Bump link count while we're moving stuff | |
939 | * around. If we crash somewhere before | |
940 | * completing our work, the link count | |
941 | * may be wrong, but correctable. | |
942 | */ | |
943 | ip->i_nlink++; | |
944 | ip->i_flag |= ICHG; | |
945 | iupdat(ip, &time, &time, 1); | |
946 | iunlock(ip); | |
947 | ||
948 | /* | |
949 | * When the target exists, both the directory | |
950 | * and target inodes are returned locked. | |
951 | */ | |
952 | u.u_dirp = (caddr_t)uap->to; | |
953 | xp = namei(uchar, CREATE | LOCKPARENT, 0); | |
954 | if (u.u_error) | |
955 | goto out; | |
956 | dp = u.u_pdir; | |
957 | /* | |
958 | * 2) If target doesn't exist, link the target | |
959 | * to the source and unlink the source. | |
960 | * Otherwise, rewrite the target directory | |
961 | * entry to reference the source inode and | |
962 | * expunge the original entry's existence. | |
963 | */ | |
964 | parentdifferent = oldparent != dp->i_number; | |
965 | if (xp == NULL) { | |
966 | if (dp->i_dev != ip->i_dev) { | |
967 | u.u_error = EXDEV; | |
968 | goto bad; | |
969 | } | |
970 | /* | |
971 | * Account for ".." in directory. | |
972 | * When source and destination have the | |
973 | * same parent we don't fool with the | |
974 | * link count -- this isn't required | |
975 | * because we do a similar check below. | |
976 | */ | |
977 | if (doingdirectory && parentdifferent) { | |
978 | dp->i_nlink++; | |
979 | dp->i_flag |= ICHG; | |
980 | iupdat(dp, &time, &time, 1); | |
981 | } | |
982 | direnter(ip); | |
983 | if (u.u_error) | |
984 | goto out; | |
985 | } else { | |
986 | if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) { | |
987 | u.u_error = EXDEV; | |
988 | goto bad; | |
989 | } | |
990 | /* | |
991 | * Target must be empty if a directory. | |
992 | * Also, insure source and target are | |
993 | * compatible (both directories, or both | |
994 | * not directories). | |
995 | */ | |
996 | if ((xp->i_mode&IFMT) == IFDIR) { | |
997 | if (!dirempty(xp)) { | |
998 | u.u_error = EEXIST; /* XXX */ | |
999 | goto bad; | |
1000 | } | |
1001 | if (!doingdirectory) { | |
1002 | u.u_error = ENOTDIR; | |
1003 | goto bad; | |
1004 | } | |
1005 | } else if (doingdirectory) { | |
1006 | u.u_error = EISDIR; | |
1007 | goto bad; | |
1008 | } | |
1009 | dirrewrite(dp, ip); | |
1010 | if (u.u_error) | |
1011 | goto bad1; | |
1012 | /* | |
1013 | * If this is a directory we know it is | |
1014 | * empty and we can squash the inode and | |
1015 | * any space associated with it. Otherwise, | |
1016 | * we've got a plain file and the link count | |
1017 | * simply needs to be adjusted. | |
1018 | */ | |
1019 | if (doingdirectory) { | |
1020 | xp->i_nlink = 0; | |
1021 | itrunc(xp, (u_long)0); | |
1022 | } else | |
1023 | xp->i_nlink--; | |
1024 | xp->i_flag |= ICHG; | |
1025 | iput(xp); | |
1026 | } | |
1027 | ||
1028 | /* | |
1029 | * 3) Unlink the source. | |
1030 | */ | |
1031 | u.u_dirp = uap->from; | |
1032 | dp = namei(uchar, DELETE, 0); | |
1033 | /* | |
1034 | * Insure directory entry still exists and | |
1035 | * has not changed since the start of all | |
1036 | * this. If either has occured, forget about | |
1037 | * about deleting the original entry and just | |
1038 | * adjust the link count in the inode. | |
1039 | */ | |
1040 | if (dp == NULL || u.u_dent.d_ino != ip->i_number) { | |
1041 | ip->i_nlink--; | |
1042 | ip->i_flag |= ICHG; | |
1043 | } else { | |
1044 | /* | |
1045 | * If source is a directory, must adjust | |
1046 | * link count of parent directory also. | |
1047 | * If target didn't exist and source and | |
1048 | * target have the same parent, then we | |
1049 | * needn't touch the link count, it all | |
1050 | * balances out in the end. Otherwise, we | |
1051 | * must do so to reflect deletion of ".." | |
1052 | * done above. | |
1053 | */ | |
1054 | if (doingdirectory && (xp != NULL || parentdifferent)) { | |
1055 | dp->i_nlink--; | |
1056 | dp->i_flag |= ICHG; | |
1057 | } | |
1058 | if (dirremove()) { | |
1059 | ip->i_nlink--; | |
1060 | ip->i_flag |= ICHG; | |
1061 | } | |
1062 | } | |
1063 | irele(ip); | |
1064 | if (dp) | |
1065 | iput(dp); | |
1066 | ||
1067 | /* | |
1068 | * 4) Renaming a directory with the parent | |
1069 | * different requires ".." to be rewritten. | |
1070 | * The window is still there for ".." to | |
1071 | * be inconsistent, but this is unavoidable, | |
1072 | * and a lot shorter than when it was done | |
1073 | * in a user process. | |
1074 | */ | |
1075 | if (doingdirectory && parentdifferent && u.u_error == 0) { | |
1076 | struct dirtemplate dirbuf; | |
528f664c | 1077 | |
4f083fd7 SL |
1078 | u.u_dirp = uap->to; |
1079 | ip = namei(uchar, LOOKUP | LOCKPARENT, 0); | |
1080 | if (ip == NULL) { | |
1081 | printf("rename: .. went away\n"); | |
1082 | return; | |
1083 | } | |
1084 | dp = u.u_pdir; | |
1085 | if ((ip->i_mode&IFMT) != IFDIR) { | |
1086 | printf("rename: .. not a directory\n"); | |
1087 | goto stuck; | |
1088 | } | |
1089 | u.u_error = rdwri(UIO_READ, ip, (caddr_t)&dirbuf, | |
1090 | sizeof (struct dirtemplate), (off_t)0, 1, (int *)0); | |
1091 | if (u.u_error == 0) { | |
1092 | dirbuf.dotdot_ino = dp->i_number; | |
1093 | (void) rdwri(UIO_WRITE, ip, (caddr_t)&dirbuf, | |
1094 | sizeof (struct dirtemplate), (off_t)0, 1, (int *)0); | |
1095 | } | |
1096 | stuck: | |
1097 | irele(dp); | |
1098 | iput(ip); | |
1099 | } | |
1100 | return; | |
1101 | bad: | |
1102 | iput(u.u_pdir); | |
1103 | bad1: | |
1104 | if (xp) | |
1105 | irele(xp); | |
1106 | out: | |
1107 | ip->i_nlink--; | |
1108 | ip->i_flag |= ICHG; | |
1109 | irele(ip); | |
528f664c SL |
1110 | } |
1111 | ||
64d3a787 BJ |
1112 | /* |
1113 | * Make a new file. | |
1114 | */ | |
1115 | struct inode * | |
1116 | maknode(mode) | |
1117 | int mode; | |
1118 | { | |
1119 | register struct inode *ip; | |
1120 | ino_t ipref; | |
1121 | ||
1122 | if ((mode & IFMT) == IFDIR) | |
1123 | ipref = dirpref(u.u_pdir->i_fs); | |
1124 | else | |
1125 | ipref = u.u_pdir->i_number; | |
1126 | ip = ialloc(u.u_pdir, ipref, mode); | |
1127 | if (ip == NULL) { | |
1128 | iput(u.u_pdir); | |
528f664c | 1129 | return (NULL); |
64d3a787 | 1130 | } |
528f664c | 1131 | #ifdef QUOTA |
64d3a787 BJ |
1132 | if (ip->i_dquot != NODQUOT) |
1133 | panic("maknode: dquot"); | |
1134 | #endif | |
1135 | ip->i_flag |= IACC|IUPD|ICHG; | |
1136 | if ((mode & IFMT) == 0) | |
1137 | mode |= IFREG; | |
1138 | ip->i_mode = mode & ~u.u_cmask; | |
1139 | ip->i_nlink = 1; | |
1140 | ip->i_uid = u.u_uid; | |
1141 | ip->i_gid = u.u_pdir->i_gid; | |
528f664c | 1142 | #ifdef QUOTA |
64d3a787 BJ |
1143 | ip->i_dquot = inoquota(ip); |
1144 | #endif | |
1145 | ||
1146 | /* | |
1147 | * Make sure inode goes to disk before directory entry. | |
1148 | */ | |
3fd23f5c | 1149 | iupdat(ip, &time, &time, 1); |
64d3a787 BJ |
1150 | direnter(ip); |
1151 | if (u.u_error) { | |
1152 | /* | |
1153 | * write error occurred trying to update directory | |
1154 | * so must deallocate the inode | |
1155 | */ | |
1156 | ip->i_nlink = 0; | |
1157 | ip->i_flag |= ICHG; | |
1158 | iput(ip); | |
528f664c | 1159 | return (NULL); |
64d3a787 | 1160 | } |
528f664c | 1161 | return (ip); |
64d3a787 | 1162 | } |