print user@hostname; filter header lines
[unix-history] / usr / src / sys / ufs / lfs / lfs_vnops.c
CommitLineData
2e073567 1/* lfs_vnops.c 4.56 83/05/21 */
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
24chdir()
25{
26
27 chdirec(&u.u_cdir);
28}
29
4f083fd7
SL
30/*
31 * Change notion of root (``/'') directory.
32 */
3e78e260
BJ
33chroot()
34{
35
36 if (suser())
37 chdirec(&u.u_rdir);
38}
39
4f083fd7
SL
40/*
41 * Common routine for chroot and chdir.
42 */
3e78e260 43chdirec(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
66bad:
67 iput(ip);
68}
69
70/*
71 * Open system call.
72 */
73open()
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 109ocreat()
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 137open1(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--;
186out:
8eee8525 187 irele(ip);
528f664c
SL
188 return;
189bad:
190 iput(ip);
3e78e260
BJ
191}
192
193/*
194 * Mknod system call
195 */
196mknod()
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
227out:
228 iput(ip);
229}
230
231/*
232 * link system call
233 */
234link()
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 }
f2a5ad78 268 u.u_error = direnter(ip);
3e78e260
BJ
269out:
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 */
280symlink()
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 */
323unlink()
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 349out:
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 360lseek()
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 */
388saccess()
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;
410done:
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 */
420fstat()
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 */
441stat()
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 */
460lstat()
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 480stat1(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 498 ds.st_atime = ip->i_atime;
f9559304 499 ds.st_spare1 = 0;
6459ebe0 500 ds.st_mtime = ip->i_mtime;
f9559304 501 ds.st_spare2 = 0;
6459ebe0 502 ds.st_ctime = ip->i_ctime;
f9559304 503 ds.st_spare3 = 0;
528f664c
SL
504 /* this doesn't belong here */
505 if ((ip->i_mode&IFMT) == IFBLK)
506 ds.st_blksize = BLKDEV_IOSIZE;
507 else if ((ip->i_mode&IFMT) == IFCHR)
508 ds.st_blksize = MAXBSIZE;
509 else
510 ds.st_blksize = ip->i_fs->fs_bsize;
2e073567
SL
511 ds.st_blocks = ip->i_blocks;
512 ds.st_spare4[0] = ds.st_spare4[1] = 0;
127f7d76 513 u.u_error = copyout((caddr_t)&ds, (caddr_t)ub, sizeof(ds));
d67a03eb
BJ
514}
515
516/*
5485e062
BJ
517 * Return target name of a symbolic link
518 */
519readlink()
520{
521 register struct inode *ip;
522 register struct a {
523 char *name;
524 char *buf;
525 int count;
31949305
BJ
526 } *uap = (struct a *)u.u_ap;
527 int resid;
5485e062 528
4f083fd7 529 ip = namei(uchar, LOOKUP, 0);
5485e062
BJ
530 if (ip == NULL)
531 return;
532 if ((ip->i_mode&IFMT) != IFLNK) {
533 u.u_error = ENXIO;
534 goto out;
535 }
31949305 536 u.u_error = rdwri(UIO_READ, ip, uap->buf, uap->count, 0, 0, &resid);
5485e062
BJ
537out:
538 iput(ip);
31949305 539 u.u_r.r_val1 = uap->count - resid;
5485e062
BJ
540}
541
4f083fd7
SL
542/*
543 * Change mode of a file given path name.
544 */
3e78e260 545chmod()
5485e062 546{
528f664c
SL
547 struct inode *ip;
548 struct a {
3e78e260
BJ
549 char *fname;
550 int fmode;
5485e062 551 } *uap;
5485e062
BJ
552
553 uap = (struct a *)u.u_ap;
3e78e260 554 if ((ip = owner(1)) == NULL)
5485e062 555 return;
528f664c 556 chmod1(ip, uap->fmode);
4f083fd7 557 iput(ip);
528f664c 558}
f94ceb3b 559
4f083fd7
SL
560/*
561 * Change mode of a file given a file descriptor.
562 */
528f664c
SL
563fchmod()
564{
565 struct a {
566 int fd;
567 int fmode;
568 } *uap;
569 register struct inode *ip;
570 register struct file *fp;
571
572 uap = (struct a *)u.u_ap;
573 fp = getf(uap->fd);
574 if (fp == NULL)
575 return;
576 if (fp->f_type == DTYPE_SOCKET) {
577 u.u_error = EINVAL;
578 return;
f94ceb3b 579 }
528f664c 580 ip = fp->f_inode;
4f083fd7 581 if (u.u_uid != ip->i_uid && !suser())
528f664c 582 return;
4f083fd7 583 ilock(ip);
528f664c 584 chmod1(ip, uap->fmode);
4f083fd7 585 iunlock(ip);
528f664c
SL
586}
587
4f083fd7
SL
588/*
589 * Change the mode on a file.
590 * Inode must be locked before calling.
591 */
528f664c
SL
592chmod1(ip, mode)
593 register struct inode *ip;
594 register int mode;
595{
197da11b
BJ
596 register int *gp;
597
3e78e260 598 ip->i_mode &= ~07777;
f94ceb3b 599 if (u.u_uid) {
528f664c 600 mode &= ~ISVTX;
bb1b75f4
SL
601 if (!groupmember(ip->i_gid))
602 mode &= ~ISGID;
f94ceb3b 603 }
528f664c 604 ip->i_mode |= mode&07777;
3e78e260
BJ
605 ip->i_flag |= ICHG;
606 if (ip->i_flag&ITEXT && (ip->i_mode&ISVTX)==0)
607 xrele(ip);
5485e062
BJ
608}
609
4f083fd7
SL
610/*
611 * Set ownership given a path name.
612 */
3e78e260 613chown()
d67a03eb 614{
528f664c
SL
615 struct inode *ip;
616 struct a {
3e78e260
BJ
617 char *fname;
618 int uid;
619 int gid;
d67a03eb 620 } *uap;
d67a03eb
BJ
621
622 uap = (struct a *)u.u_ap;
aad8ef38 623 if (!suser() || (ip = owner(0)) == NULL)
d67a03eb 624 return;
bb1b75f4 625 u.u_error = chown1(ip, uap->uid, uap->gid);
4f083fd7 626 iput(ip);
528f664c 627}
f94ceb3b 628
4f083fd7
SL
629/*
630 * Set ownership given a file descriptor.
631 */
528f664c
SL
632fchown()
633{
634 struct a {
635 int fd;
636 int uid;
637 int gid;
638 } *uap;
639 register struct inode *ip;
640 register struct file *fp;
641
642 uap = (struct a *)u.u_ap;
643 fp = getf(uap->fd);
644 if (fp == NULL)
645 return;
646 if (fp->f_type == DTYPE_SOCKET) {
647 u.u_error = EINVAL;
648 return;
f94ceb3b 649 }
528f664c 650 ip = fp->f_inode;
aad8ef38 651 if (!suser())
528f664c 652 return;
4f083fd7 653 ilock(ip);
bb1b75f4 654 u.u_error = chown1(ip, uap->uid, uap->gid);
4f083fd7 655 iunlock(ip);
528f664c
SL
656}
657
658/*
659 * Perform chown operation on inode ip;
660 * inode must be locked prior to call.
661 */
662chown1(ip, uid, gid)
663 register struct inode *ip;
664 int uid, gid;
665{
666#ifdef QUOTA
667 register long change;
bb1b75f4 668#endif
528f664c 669
bb1b75f4
SL
670 if (uid == -1)
671 uid = ip->i_uid;
672 if (gid == -1)
673 gid = ip->i_gid;
bb1b75f4 674#ifdef QUOTA
2e073567 675 if (ip->i_uid != uid) /* this just speeds things a little */
0a77f278 676 change = 0;
2e073567
SL
677 else
678 change = ip->i_blocks;
679 (void) chkdq(ip, -change, 1);
680 (void) chkiq(ip->i_dev, ip, ip->i_uid, 1);
0a77f278 681 dqrele(ip->i_dquot);
f94ceb3b 682#endif
bb1b75f4
SL
683 ip->i_uid = uid;
684 ip->i_gid = gid;
3e78e260
BJ
685 ip->i_flag |= ICHG;
686 if (u.u_ruid != 0)
687 ip->i_mode &= ~(ISUID|ISGID);
528f664c 688#ifdef QUOTA
0a77f278 689 ip->i_dquot = inoquota(ip);
2e073567
SL
690 (void) chkdq(ip, change, 1);
691 (void) chkiq(ip->i_dev, (struct inode *)NULL, uid, 1);
692 return (u.u_error); /* should == 0 ALWAYS !! */
693#else
bb1b75f4 694 return (0);
2e073567 695#endif
d67a03eb
BJ
696}
697
bb1b75f4 698#ifndef NOCOMPAT
d67a03eb 699/*
3e78e260
BJ
700 * Set IUPD and IACC times on file.
701 * Can't set ICHG.
d67a03eb 702 */
9702e942 703outime()
e92a04af 704{
d67a03eb 705 register struct a {
3e78e260
BJ
706 char *fname;
707 time_t *tptr;
bb1b75f4 708 } *uap = (struct a *)u.u_ap;
3e78e260
BJ
709 register struct inode *ip;
710 time_t tv[2];
b32450f4 711 struct timeval tv0, tv1;
d67a03eb 712
3e78e260 713 if ((ip = owner(1)) == NULL)
d67a03eb 714 return;
bb1b75f4 715 u.u_error = copyin((caddr_t)uap->tptr, (caddr_t)tv, sizeof (tv));
127f7d76 716 if (u.u_error == 0) {
3e78e260 717 ip->i_flag |= IACC|IUPD|ICHG;
b32450f4
BJ
718 tv0.tv_sec = tv[0]; tv0.tv_usec = 0;
719 tv1.tv_sec = tv[1]; tv1.tv_usec = 0;
720 iupdat(ip, &tv0, &tv1, 0);
d67a03eb 721 }
d67a03eb
BJ
722 iput(ip);
723}
bb1b75f4
SL
724#endif
725
726utimes()
727{
728 register struct a {
729 char *fname;
730 struct timeval *tptr;
731 } *uap = (struct a *)u.u_ap;
732 register struct inode *ip;
733 struct timeval tv[2];
734
735 if ((ip = owner(1)) == NULL)
736 return;
737 u.u_error = copyin((caddr_t)uap->tptr, (caddr_t)tv, sizeof (tv));
738 if (u.u_error == 0) {
739 ip->i_flag |= IACC|IUPD|ICHG;
740 iupdat(ip, &tv[0], &tv[1], 0);
741 }
742 iput(ip);
743}
d67a03eb 744
4f083fd7
SL
745/*
746 * Flush any pending I/O.
747 */
3e78e260 748sync()
d67a03eb 749{
d67a03eb 750
3fd23f5c 751 update();
d67a03eb 752}
64d3a787 753
4f083fd7
SL
754/*
755 * Apply an advisory lock on a file descriptor.
756 */
528f664c
SL
757flock()
758{
759 struct a {
760 int fd;
761 int how;
762 } *uap;
763 register struct file *fp;
764 register int cmd, flags;
765
766 uap = (struct a *)u.u_ap;
767 fp = getf(uap->fd);
768 if (fp == NULL)
769 return;
770 if (fp->f_type == DTYPE_SOCKET) { /* XXX */
771 u.u_error = EINVAL;
772 return;
773 }
774 cmd = uap->how;
3650c37d 775 flags = u.u_pofile[uap->fd] & (UF_SHLOCK|UF_EXLOCK);
528f664c
SL
776 if (cmd&FUNLOCK) {
777 if (flags == 0) {
778 u.u_error = EINVAL;
779 return;
780 }
781 funlocki(fp->f_inode, flags);
3650c37d 782 u.u_pofile[uap->fd] &= ~(UF_SHLOCK|UF_EXLOCK);
528f664c
SL
783 return;
784 }
785 /*
786 * No reason to write lock a file we've already
787 * write locked, similarly with a read lock.
788 */
3650c37d
SL
789 if ((flags&UF_EXLOCK) && (cmd&FEXLOCK) ||
790 (flags&UF_SHLOCK) && (cmd&FSHLOCK))
528f664c
SL
791 return;
792 u.u_pofile[uap->fd] = flocki(fp->f_inode, u.u_pofile[uap->fd], cmd);
793}
794
4f083fd7
SL
795/*
796 * Truncate a file given its path name.
797 */
528f664c
SL
798truncate()
799{
800 struct a {
801 char *fname;
4f083fd7 802 u_long length;
31949305 803 } *uap = (struct a *)u.u_ap;
528f664c
SL
804 struct inode *ip;
805
4f083fd7 806 ip = namei(uchar, LOOKUP, 1);
528f664c
SL
807 if (ip == NULL)
808 return;
809 if (access(ip, IWRITE))
810 goto bad;
811 if ((ip->i_mode&IFMT) == IFDIR) {
812 u.u_error = EISDIR;
813 goto bad;
814 }
815 itrunc(ip, uap->length);
528f664c
SL
816bad:
817 iput(ip);
818}
819
4f083fd7
SL
820/*
821 * Truncate a file given a file descriptor.
822 */
528f664c
SL
823ftruncate()
824{
825 struct a {
826 int fd;
4f083fd7 827 u_long length;
31949305 828 } *uap = (struct a *)u.u_ap;
528f664c
SL
829 struct inode *ip;
830 struct file *fp;
831
832 fp = getf(uap->fd);
833 if (fp == NULL)
834 return;
835 if (fp->f_type == DTYPE_SOCKET) {
836 u.u_error = EINVAL;
837 return;
838 }
839 if ((fp->f_flag&FWRITE) == 0) {
840 u.u_error = EINVAL;
841 return;
842 }
843 ip = fp->f_inode;
844 ilock(ip);
845 itrunc(ip, uap->length);
4f083fd7
SL
846 iunlock(ip);
847}
848
849/*
850 * Synch an open file.
851 */
852fsync()
853{
854 struct a {
855 int fd;
856 } *uap = (struct a *)u.u_ap;
857 struct inode *ip;
858 struct file *fp;
859
860 fp = getf(uap->fd);
861 if (fp == NULL)
862 return;
863 if (fp->f_type == DTYPE_SOCKET) {
864 u.u_error = EINVAL;
865 return;
866 }
867 ip = fp->f_inode;
868 ilock(ip);
869 syncip(ip);
870 iunlock(ip);
528f664c
SL
871}
872
4f083fd7
SL
873/*
874 * Rename system call.
875 * rename("foo", "bar");
876 * is essentially
877 * unlink("bar");
878 * link("foo", "bar");
879 * unlink("foo");
880 * but ``atomically''. Can't do full commit without saving state in the
881 * inode on disk which isn't feasible at this time. Best we can do is
882 * always guarantee the target exists.
883 *
884 * Basic algorithm is:
885 *
886 * 1) Bump link count on source while we're linking it to the
887 * target. This also insure the inode won't be deleted out
888 * from underneath us while we work.
889 * 2) Link source to destination. If destination already exists,
890 * delete it first.
891 * 3) Unlink source reference to inode if still around.
892 * 4) If a directory was moved and the parent of the destination
893 * is different from the source, patch the ".." entry in the
894 * directory.
895 *
896 * Source and destination must either both be directories, or both
897 * not be directories. If target is a directory, it must be empty.
898 */
528f664c
SL
899rename()
900{
901 struct a {
902 char *from;
903 char *to;
904 } *uap;
4f083fd7
SL
905 register struct inode *ip, *xp, *dp;
906 int oldparent, parentdifferent, doingdirectory;
a5390dce 907 int error = 0;
4f083fd7
SL
908
909 uap = (struct a *)u.u_ap;
046f18d1 910 ip = namei(uchar, DELETE | LOCKPARENT, 0);
4f083fd7
SL
911 if (ip == NULL)
912 return;
913 dp = u.u_pdir;
914 oldparent = 0, doingdirectory = 0;
915 if ((ip->i_mode&IFMT) == IFDIR) {
916 register struct direct *d;
917
918 d = &u.u_dent;
919 /*
046f18d1 920 * Avoid ".", "..", and aliases of "." for obvious reasons.
4f083fd7 921 */
046f18d1
SL
922 if ((d->d_namlen == 1 && d->d_name[0] == '.') ||
923 (d->d_namlen == 2 && bcmp(d->d_name, "..", 2) == 0) ||
924 (dp == ip)) {
925 iput(dp);
926 if (dp == ip)
927 irele(ip);
928 else
4f083fd7 929 iput(ip);
046f18d1
SL
930 u.u_error = EINVAL;
931 return;
4f083fd7
SL
932 }
933 oldparent = dp->i_number;
934 doingdirectory++;
935 }
046f18d1 936 iput(dp);
4f083fd7
SL
937
938 /*
939 * 1) Bump link count while we're moving stuff
940 * around. If we crash somewhere before
941 * completing our work, the link count
942 * may be wrong, but correctable.
943 */
944 ip->i_nlink++;
945 ip->i_flag |= ICHG;
946 iupdat(ip, &time, &time, 1);
947 iunlock(ip);
948
949 /*
950 * When the target exists, both the directory
951 * and target inodes are returned locked.
952 */
953 u.u_dirp = (caddr_t)uap->to;
954 xp = namei(uchar, CREATE | LOCKPARENT, 0);
a5390dce
SL
955 if (u.u_error) {
956 error = u.u_error;
4f083fd7 957 goto out;
a5390dce 958 }
4f083fd7 959 dp = u.u_pdir;
046f18d1
SL
960 /*
961 * If ".." must be changed (ie the directory gets a new
962 * parent) then the user must have write permission.
963 */
964 parentdifferent = oldparent != dp->i_number;
5463e92b 965 if (doingdirectory && parentdifferent && access(ip, IWRITE))
046f18d1 966 goto bad;
4f083fd7
SL
967 /*
968 * 2) If target doesn't exist, link the target
969 * to the source and unlink the source.
970 * Otherwise, rewrite the target directory
971 * entry to reference the source inode and
972 * expunge the original entry's existence.
973 */
4f083fd7
SL
974 if (xp == NULL) {
975 if (dp->i_dev != ip->i_dev) {
a5390dce 976 error = EXDEV;
4f083fd7
SL
977 goto bad;
978 }
e69c3c9c
SL
979 /*
980 * Disallow rename(foo, foo/bar).
981 */
982 if (dp->i_number == ip->i_number) {
983 error = EEXIST;
984 goto bad;
985 }
4f083fd7
SL
986 /*
987 * Account for ".." in directory.
988 * When source and destination have the
989 * same parent we don't fool with the
990 * link count -- this isn't required
991 * because we do a similar check below.
992 */
993 if (doingdirectory && parentdifferent) {
994 dp->i_nlink++;
995 dp->i_flag |= ICHG;
996 iupdat(dp, &time, &time, 1);
997 }
f2a5ad78
SL
998 error = direnter(ip);
999 if (error)
4f083fd7
SL
1000 goto out;
1001 } else {
1002 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) {
a5390dce 1003 error = EXDEV;
4f083fd7
SL
1004 goto bad;
1005 }
e69c3c9c
SL
1006 /*
1007 * Short circuit rename(foo, foo).
1008 */
1009 if (xp->i_number == ip->i_number)
1010 goto bad;
4f083fd7 1011 /*
a5390dce
SL
1012 * Target must be empty if a directory
1013 * and have no links to it.
4f083fd7
SL
1014 * Also, insure source and target are
1015 * compatible (both directories, or both
1016 * not directories).
1017 */
1018 if ((xp->i_mode&IFMT) == IFDIR) {
a5390dce
SL
1019 if (!dirempty(xp) || xp->i_nlink > 2) {
1020 error = ENOTEMPTY;
4f083fd7
SL
1021 goto bad;
1022 }
1023 if (!doingdirectory) {
a5390dce 1024 error = ENOTDIR;
4f083fd7
SL
1025 goto bad;
1026 }
1027 } else if (doingdirectory) {
a5390dce 1028 error = EISDIR;
4f083fd7
SL
1029 goto bad;
1030 }
1031 dirrewrite(dp, ip);
a5390dce
SL
1032 if (u.u_error) {
1033 error = u.u_error;
4f083fd7 1034 goto bad1;
a5390dce 1035 }
4f083fd7 1036 /*
a5390dce
SL
1037 * Adjust the link count of the target to
1038 * reflect the dirrewrite above. If this is
1039 * a directory it is empty and there are
1040 * no links to it, so we can squash the inode and
1041 * any space associated with it. We disallowed
1042 * renaming over top of a directory with links to
1043 * it above, as we've no way to determine if
1044 * we've got a link or the directory itself, and
1045 * if we get a link, then ".." will be screwed up.
4f083fd7 1046 */
a5390dce 1047 xp->i_nlink--;
4f083fd7 1048 if (doingdirectory) {
a5390dce
SL
1049 if (--xp->i_nlink != 0)
1050 panic("rename: linked directory");
4f083fd7 1051 itrunc(xp, (u_long)0);
a5390dce 1052 }
4f083fd7
SL
1053 xp->i_flag |= ICHG;
1054 iput(xp);
31db12cb 1055 xp = NULL;
4f083fd7
SL
1056 }
1057
1058 /*
1059 * 3) Unlink the source.
1060 */
1061 u.u_dirp = uap->from;
1062 dp = namei(uchar, DELETE, 0);
1063 /*
1064 * Insure directory entry still exists and
1065 * has not changed since the start of all
1066 * this. If either has occured, forget about
1067 * about deleting the original entry and just
1068 * adjust the link count in the inode.
1069 */
1070 if (dp == NULL || u.u_dent.d_ino != ip->i_number) {
1071 ip->i_nlink--;
1072 ip->i_flag |= ICHG;
1073 } else {
1074 /*
1075 * If source is a directory, must adjust
1076 * link count of parent directory also.
1077 * If target didn't exist and source and
1078 * target have the same parent, then we
1079 * needn't touch the link count, it all
1080 * balances out in the end. Otherwise, we
1081 * must do so to reflect deletion of ".."
1082 * done above.
1083 */
1084 if (doingdirectory && (xp != NULL || parentdifferent)) {
1085 dp->i_nlink--;
1086 dp->i_flag |= ICHG;
1087 }
1088 if (dirremove()) {
1089 ip->i_nlink--;
1090 ip->i_flag |= ICHG;
1091 }
a5390dce
SL
1092 if (error == 0) /* conservative */
1093 error = u.u_error;
4f083fd7
SL
1094 }
1095 irele(ip);
1096 if (dp)
1097 iput(dp);
1098
1099 /*
1100 * 4) Renaming a directory with the parent
1101 * different requires ".." to be rewritten.
1102 * The window is still there for ".." to
1103 * be inconsistent, but this is unavoidable,
1104 * and a lot shorter than when it was done
1105 * in a user process.
1106 */
a5390dce 1107 if (doingdirectory && parentdifferent && error == 0) {
4f083fd7 1108 struct dirtemplate dirbuf;
528f664c 1109
4f083fd7
SL
1110 u.u_dirp = uap->to;
1111 ip = namei(uchar, LOOKUP | LOCKPARENT, 0);
1112 if (ip == NULL) {
1113 printf("rename: .. went away\n");
1114 return;
1115 }
1116 dp = u.u_pdir;
1117 if ((ip->i_mode&IFMT) != IFDIR) {
1118 printf("rename: .. not a directory\n");
1119 goto stuck;
1120 }
a5390dce 1121 error = rdwri(UIO_READ, ip, (caddr_t)&dirbuf,
4f083fd7 1122 sizeof (struct dirtemplate), (off_t)0, 1, (int *)0);
a5390dce 1123 if (error == 0) {
4f083fd7
SL
1124 dirbuf.dotdot_ino = dp->i_number;
1125 (void) rdwri(UIO_WRITE, ip, (caddr_t)&dirbuf,
1126 sizeof (struct dirtemplate), (off_t)0, 1, (int *)0);
1127 }
1128stuck:
1129 irele(dp);
1130 iput(ip);
1131 }
a5390dce
SL
1132 goto done;
1133
4f083fd7 1134bad:
31db12cb 1135 iput(dp);
4f083fd7
SL
1136bad1:
1137 if (xp)
31db12cb 1138 iput(xp);
4f083fd7
SL
1139out:
1140 ip->i_nlink--;
1141 ip->i_flag |= ICHG;
1142 irele(ip);
a5390dce
SL
1143done:
1144 if (error)
1145 u.u_error = error;
528f664c
SL
1146}
1147
64d3a787
BJ
1148/*
1149 * Make a new file.
1150 */
1151struct inode *
1152maknode(mode)
1153 int mode;
1154{
1155 register struct inode *ip;
1156 ino_t ipref;
1157
1158 if ((mode & IFMT) == IFDIR)
1159 ipref = dirpref(u.u_pdir->i_fs);
1160 else
1161 ipref = u.u_pdir->i_number;
1162 ip = ialloc(u.u_pdir, ipref, mode);
1163 if (ip == NULL) {
1164 iput(u.u_pdir);
528f664c 1165 return (NULL);
64d3a787 1166 }
528f664c 1167#ifdef QUOTA
64d3a787
BJ
1168 if (ip->i_dquot != NODQUOT)
1169 panic("maknode: dquot");
1170#endif
1171 ip->i_flag |= IACC|IUPD|ICHG;
1172 if ((mode & IFMT) == 0)
1173 mode |= IFREG;
1174 ip->i_mode = mode & ~u.u_cmask;
1175 ip->i_nlink = 1;
1176 ip->i_uid = u.u_uid;
1177 ip->i_gid = u.u_pdir->i_gid;
bb1b75f4
SL
1178 if (ip->i_mode & ISGID && !groupmember(ip->i_gid))
1179 ip->i_mode &= ~ISGID;
528f664c 1180#ifdef QUOTA
64d3a787
BJ
1181 ip->i_dquot = inoquota(ip);
1182#endif
1183
1184 /*
1185 * Make sure inode goes to disk before directory entry.
1186 */
3fd23f5c 1187 iupdat(ip, &time, &time, 1);
f2a5ad78 1188 u.u_error = direnter(ip);
64d3a787
BJ
1189 if (u.u_error) {
1190 /*
f2a5ad78
SL
1191 * Write error occurred trying to update directory
1192 * so must deallocate the inode.
64d3a787
BJ
1193 */
1194 ip->i_nlink = 0;
1195 ip->i_flag |= ICHG;
1196 iput(ip);
528f664c 1197 return (NULL);
64d3a787 1198 }
528f664c 1199 return (ip);
64d3a787 1200}