move to compatibility library, make ANSI compliant, remove fgets(3)
[unix-history] / usr / src / sys / ufs / lfs / lfs_vnops.c
CommitLineData
da7c5cc6 1/*
7188ac27
KM
2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3 * All rights reserved.
da7c5cc6 4 *
7188ac27
KM
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 *
de412887 17 * @(#)lfs_vnops.c 7.9 (Berkeley) %G%
da7c5cc6 18 */
6459ebe0 19
94368568
JB
20#include "param.h"
21#include "systm.h"
94368568
JB
22#include "user.h"
23#include "kernel.h"
24#include "file.h"
25#include "stat.h"
94368568
JB
26#include "buf.h"
27#include "proc.h"
94368568
JB
28#include "uio.h"
29#include "socket.h"
30#include "socketvar.h"
7188ac27 31#include "conf.h"
94368568 32#include "mount.h"
7188ac27
KM
33#include "vnode.h"
34#include "../ufs/inode.h"
35#include "../ufs/fs.h"
36#include "../ufs/quota.h"
3e78e260 37
4f083fd7 38/*
7188ac27 39 * Global vfs data structures for ufs
4f083fd7 40 */
3e78e260 41
7188ac27
KM
42int ufs_lookup(),
43 ufs_create(),
44 ufs_mknod(),
45 ufs_open(),
46 ufs_close(),
47 ufs_access(),
48 ufs_getattr(),
49 ufs_setattr(),
50 ufs_read(),
51 ufs_write(),
52 ufs_ioctl(),
53 ufs_select(),
54 ufs_mmap(),
55 ufs_fsync(),
56 ufs_seek(),
57 ufs_remove(),
58 ufs_link(),
59 ufs_rename(),
60 ufs_mkdir(),
61 ufs_rmdir(),
62 ufs_symlink(),
63 ufs_readdir(),
64 ufs_readlink(),
65 ufs_abortop(),
66 ufs_inactive(),
67 ufs_lock(),
68 ufs_unlock(),
69 ufs_bmap(),
70 ufs_strategy();
71
72struct vnodeops ufs_vnodeops = {
73 ufs_lookup,
74 ufs_create,
75 ufs_mknod,
76 ufs_open,
77 ufs_close,
78 ufs_access,
79 ufs_getattr,
80 ufs_setattr,
81 ufs_read,
82 ufs_write,
83 ufs_ioctl,
84 ufs_select,
85 ufs_mmap,
86 ufs_fsync,
87 ufs_seek,
88 ufs_remove,
89 ufs_link,
90 ufs_rename,
91 ufs_mkdir,
92 ufs_rmdir,
93 ufs_symlink,
94 ufs_readdir,
95 ufs_readlink,
96 ufs_abortop,
97 ufs_inactive,
98 ufs_lock,
99 ufs_unlock,
100 ufs_bmap,
101 ufs_strategy,
102};
103
104enum vtype iftovt_tab[8] = {
105 VNON, VCHR, VDIR, VBLK, VREG, VLNK, VSOCK, VBAD,
106};
107int vttoif_tab[8] = {
108 0, IFREG, IFDIR, IFBLK, IFCHR, IFLNK, IFSOCK, IFMT,
109};
3e78e260 110
4f083fd7 111/*
7188ac27 112 * Create a regular file
4f083fd7 113 */
7188ac27
KM
114ufs_create(ndp, vap)
115 struct nameidata *ndp;
116 struct vattr *vap;
3e78e260 117{
7188ac27
KM
118 struct inode *ip;
119 int error;
3e78e260 120
7188ac27
KM
121 if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
122 return (error);
123 ndp->ni_vp = ITOV(ip);
124 return (0);
3e78e260
BJ
125}
126
4f083fd7 127/*
7188ac27 128 * Mknod vnode call
4f083fd7 129 */
7188ac27
KM
130/* ARGSUSED */
131ufs_mknod(ndp, vap, cred)
132 struct nameidata *ndp;
133 struct ucred *cred;
134 struct vattr *vap;
3e78e260 135{
7188ac27
KM
136 struct inode *ip;
137 int error;
3e78e260 138
7188ac27
KM
139 if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
140 return (error);
141 if (vap->va_rdev) {
142 /*
143 * Want to be able to use this to make badblock
144 * inodes, so don't truncate the dev number.
145 */
146 ITOV(ip)->v_rdev = ip->i_rdev = vap->va_rdev;
147 ip->i_flag |= IACC|IUPD|ICHG;
148 }
3e78e260 149 iput(ip);
7188ac27
KM
150 /*
151 * Remove inode so that it will be reloaded by iget and
152 * checked to see if it is an alias of an existing entry
153 * in the inode cache.
154 */
155 remque(ip);
156 ip->i_forw = ip;
157 ip->i_back = ip;
158 return (0);
3e78e260
BJ
159}
160
161/*
7188ac27
KM
162 * Open called.
163 *
164 * Nothing to do.
3e78e260 165 */
7188ac27
KM
166/* ARGSUSED */
167ufs_open(vp, mode, cred)
168 struct vnode *vp;
169 int mode;
170 struct ucred *cred;
3e78e260 171{
3e78e260 172
7188ac27 173 return (0);
3e78e260
BJ
174}
175
176/*
7188ac27
KM
177 * Close called
178 *
179 * Update the times on the inode.
3e78e260 180 */
7188ac27
KM
181/* ARGSUSED */
182ufs_close(vp, fflag, cred)
183 struct vnode *vp;
184 int fflag;
185 struct ucred *cred;
3e78e260 186{
7188ac27 187 register struct inode *ip = VTOI(vp);
3e78e260 188
7188ac27
KM
189 if (vp->v_count > 1 && !(ip->i_flag & ILOCKED))
190 ITIMES(ip, &time, &time);
191 return (0);
3e78e260
BJ
192}
193
7188ac27
KM
194ufs_access(vp, mode, cred)
195 struct vnode *vp;
196 int mode;
197 struct ucred *cred;
3e78e260 198{
3e78e260 199
7188ac27 200 return (iaccess(VTOI(vp), mode, cred));
3e78e260
BJ
201}
202
7188ac27
KM
203/* ARGSUSED */
204ufs_getattr(vp, vap, cred)
205 struct vnode *vp;
206 register struct vattr *vap;
207 struct ucred *cred;
3e78e260 208{
7188ac27 209 register struct inode *ip = VTOI(vp);
3e78e260 210
7188ac27 211 ITIMES(ip, &time, &time);
3e78e260 212 /*
7188ac27 213 * Copy from inode table
3e78e260 214 */
7188ac27
KM
215 vap->va_fsid = ip->i_dev;
216 vap->va_fileid = ip->i_number;
217 vap->va_mode = ip->i_mode & ~IFMT;
218 vap->va_nlink = ip->i_nlink;
219 vap->va_uid = ip->i_uid;
220 vap->va_gid = ip->i_gid;
221 vap->va_rdev = (dev_t)ip->i_rdev;
222 vap->va_size = ip->i_ic.ic_size.val[0];
223 vap->va_size1 = ip->i_ic.ic_size.val[1];
224 vap->va_atime.tv_sec = ip->i_atime;
225 vap->va_mtime.tv_sec = ip->i_mtime;
226 vap->va_ctime.tv_sec = ip->i_ctime;
227 /* this doesn't belong here */
228 if (vp->v_type == VBLK)
229 vap->va_blocksize = BLKDEV_IOSIZE;
230 else if (vp->v_type == VCHR)
231 vap->va_blocksize = MAXBSIZE;
8eee8525 232 else
7188ac27
KM
233 vap->va_blocksize = ip->i_fs->fs_bsize;
234 /*
235 * XXX THIS IS NOT CORRECT!!, but be sure to change vn_stat()
236 * if you change it.
237 */
238 vap->va_bytes = ip->i_blocks;
239 vap->va_bytes1 = -1;
240 vap->va_type = vp->v_type;
241 return (0);
3e78e260
BJ
242}
243
244/*
7188ac27 245 * Set attribute vnode op. called from several syscalls
3e78e260 246 */
7188ac27
KM
247ufs_setattr(vp, vap, cred)
248 register struct vnode *vp;
249 register struct vattr *vap;
250 register struct ucred *cred;
3e78e260 251{
7188ac27
KM
252 register struct inode *ip = VTOI(vp);
253 int error = 0;
b4d1aee9 254
7188ac27
KM
255 /*
256 * Check for unsetable attributes.
257 */
258 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
259 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
260 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
261 ((int)vap->va_bytes != VNOVAL)) {
262 return (EINVAL);
b4d1aee9 263 }
7188ac27
KM
264 /*
265 * Go through the fields and update iff not VNOVAL.
266 */
267 if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
268 if (error = chown1(vp, vap->va_uid, vap->va_gid, cred))
269 return (error);
270 if (vap->va_size != VNOVAL) {
271 if (vp->v_type == VDIR)
272 return (EISDIR);
273 if (error = iaccess(ip, IWRITE, cred))
274 return (error);
275 if (error = itrunc(ip, vap->va_size))
276 return (error);
3e78e260 277 }
7188ac27 278 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
de412887
KM
279 if (cred->cr_uid != ip->i_uid &&
280 (error = suser(cred, &u.u_acflag)))
281 return (error);
7188ac27
KM
282 if (vap->va_atime.tv_sec != VNOVAL)
283 ip->i_flag |= IACC;
284 if (vap->va_mtime.tv_sec != VNOVAL)
285 ip->i_flag |= IUPD;
286 ip->i_flag |= ICHG;
287 if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1))
288 return (error);
5485e062 289 }
7188ac27
KM
290 if (vap->va_mode != (u_short)VNOVAL)
291 error = chmod1(vp, (int)vap->va_mode, cred);
292 return (error);
528f664c
SL
293}
294
4f083fd7
SL
295/*
296 * Change the mode on a file.
297 * Inode must be locked before calling.
298 */
7188ac27
KM
299chmod1(vp, mode, cred)
300 register struct vnode *vp;
528f664c 301 register int mode;
7188ac27 302 struct ucred *cred;
528f664c 303{
7188ac27 304 register struct inode *ip = VTOI(vp);
de412887 305 int error;
197da11b 306
de412887
KM
307 if (cred->cr_uid != ip->i_uid &&
308 (error = suser(cred, &u.u_acflag)))
309 return (error);
3e78e260 310 ip->i_mode &= ~07777;
7188ac27
KM
311 if (cred->cr_uid) {
312 if (vp->v_type != VDIR)
47af7174 313 mode &= ~ISVTX;
7188ac27 314 if (!groupmember(ip->i_gid, cred))
bb1b75f4 315 mode &= ~ISGID;
f94ceb3b 316 }
7188ac27 317 ip->i_mode |= mode & 07777;
3e78e260 318 ip->i_flag |= ICHG;
7188ac27
KM
319 if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
320 xrele(vp);
47af7174 321 return (0);
5485e062
BJ
322}
323
528f664c
SL
324/*
325 * Perform chown operation on inode ip;
326 * inode must be locked prior to call.
327 */
7188ac27
KM
328chown1(vp, uid, gid, cred)
329 register struct vnode *vp;
330 uid_t uid;
331 gid_t gid;
332 struct ucred *cred;
528f664c 333{
7188ac27 334 register struct inode *ip = VTOI(vp);
528f664c
SL
335#ifdef QUOTA
336 register long change;
bb1b75f4 337#endif
7188ac27 338 int error;
528f664c 339
7188ac27 340 if (uid == (u_short)VNOVAL)
bb1b75f4 341 uid = ip->i_uid;
7188ac27 342 if (gid == (u_short)VNOVAL)
bb1b75f4 343 gid = ip->i_gid;
18b0bce6
KB
344 /*
345 * If we don't own the file, are trying to change the owner
346 * of the file, or are not a member of the target group,
347 * the caller must be superuser or the call fails.
348 */
7188ac27
KM
349 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
350 !groupmember((gid_t)gid, cred)) &&
351 (error = suser(cred, &u.u_acflag)))
352 return (error);
bb1b75f4 353#ifdef QUOTA
3809bf69 354 if (ip->i_uid == uid) /* this just speeds things a little */
0a77f278 355 change = 0;
2e073567
SL
356 else
357 change = ip->i_blocks;
358 (void) chkdq(ip, -change, 1);
359 (void) chkiq(ip->i_dev, ip, ip->i_uid, 1);
0a77f278 360 dqrele(ip->i_dquot);
f94ceb3b 361#endif
bb1b75f4
SL
362 ip->i_uid = uid;
363 ip->i_gid = gid;
3e78e260 364 ip->i_flag |= ICHG;
7188ac27 365 if (cred->cr_ruid != 0)
3e78e260 366 ip->i_mode &= ~(ISUID|ISGID);
528f664c 367#ifdef QUOTA
0a77f278 368 ip->i_dquot = inoquota(ip);
2e073567 369 (void) chkdq(ip, change, 1);
8011f5df 370 (void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1);
2e073567
SL
371 return (u.u_error); /* should == 0 ALWAYS !! */
372#else
bb1b75f4 373 return (0);
2e073567 374#endif
d67a03eb
BJ
375}
376
7188ac27
KM
377/* ARGSUSED */
378ufs_ioctl(vp, com, data, fflag, cred)
379 struct vnode *vp;
380 int com;
381 caddr_t data;
382 int fflag;
383 struct ucred *cred;
bb1b75f4 384{
bb1b75f4 385
7188ac27
KM
386 printf("ufs_ioctl called with type %d\n", vp->v_type);
387 return (ENOTTY);
388}
389
390/* ARGSUSED */
391ufs_select(vp, which, cred)
392 struct vnode *vp;
393 int which;
394 struct ucred *cred;
395{
396
397 printf("ufs_select called with type %d\n", vp->v_type);
398 return (1); /* XXX */
bb1b75f4 399}
d67a03eb 400
4f083fd7 401/*
7188ac27
KM
402 * Mmap a file
403 *
404 * NB Currently unsupported.
4f083fd7 405 */
7188ac27
KM
406/* ARGSUSED */
407ufs_mmap(vp, fflags, cred)
408 struct vnode *vp;
409 int fflags;
410 struct ucred *cred;
d67a03eb 411{
d67a03eb 412
7188ac27 413 return (EINVAL);
d67a03eb 414}
64d3a787 415
4f083fd7 416/*
7188ac27 417 * Synch an open file.
4f083fd7 418 */
7188ac27
KM
419/* ARGSUSED */
420ufs_fsync(vp, fflags, cred)
421 struct vnode *vp;
422 int fflags;
423 struct ucred *cred;
528f664c 424{
7188ac27
KM
425 register struct inode *ip = VTOI(vp);
426 int error;
427
428 ILOCK(ip);
429 if (fflags&FWRITE)
430 ip->i_flag |= ICHG;
431 error = syncip(ip);
432 IUNLOCK(ip);
433 return (error);
528f664c
SL
434}
435
4f083fd7 436/*
7188ac27
KM
437 * Seek on a file
438 *
439 * Nothing to do, so just return.
4f083fd7 440 */
7188ac27
KM
441/* ARGSUSED */
442ufs_seek(vp, oldoff, newoff, cred)
443 struct vnode *vp;
444 off_t oldoff, newoff;
445 struct ucred *cred;
528f664c 446{
7188ac27
KM
447
448 return (0);
449}
450
451/*
452 * ufs remove
453 * Hard to avoid races here, especially
454 * in unlinking directories.
455 */
456ufs_remove(ndp)
457 struct nameidata *ndp;
458{
459 register struct inode *ip, *dp;
460 int error;
461
462 ip = VTOI(ndp->ni_vp);
463 dp = VTOI(ndp->ni_dvp);
464 error = dirremove(ndp);
465 if (!error) {
466 ip->i_nlink--;
467 ip->i_flag |= ICHG;
528f664c 468 }
7188ac27
KM
469 if (dp == ip)
470 vrele(ITOV(ip));
471 else
472 iput(ip);
473 iput(dp);
474 return (error);
4f083fd7
SL
475}
476
477/*
7188ac27 478 * link vnode call
4f083fd7 479 */
7188ac27
KM
480ufs_link(vp, ndp)
481 register struct vnode *vp;
482 register struct nameidata *ndp;
4f083fd7 483{
7188ac27
KM
484 register struct inode *ip = VTOI(vp);
485 int error;
4f083fd7 486
7188ac27
KM
487 if (ndp->ni_dvp != vp)
488 ILOCK(ip);
489 if (ip->i_nlink == LINK_MAX - 1) {
490 error = EMLINK;
491 goto out;
492 }
493 ip->i_nlink++;
494 ip->i_flag |= ICHG;
495 error = iupdat(ip, &time, &time, 1);
496 if (!error)
497 error = direnter(ip, ndp);
498out:
499 if (ndp->ni_dvp != vp)
500 IUNLOCK(ip);
501 if (error) {
502 ip->i_nlink--;
82252d2b 503 ip->i_flag |= ICHG;
7188ac27
KM
504 }
505 return (error);
528f664c
SL
506}
507
4f083fd7
SL
508/*
509 * Rename system call.
510 * rename("foo", "bar");
511 * is essentially
512 * unlink("bar");
513 * link("foo", "bar");
514 * unlink("foo");
515 * but ``atomically''. Can't do full commit without saving state in the
516 * inode on disk which isn't feasible at this time. Best we can do is
517 * always guarantee the target exists.
518 *
519 * Basic algorithm is:
520 *
521 * 1) Bump link count on source while we're linking it to the
7188ac27 522 * target. This also ensure the inode won't be deleted out
68f21562
KM
523 * from underneath us while we work (it may be truncated by
524 * a concurrent `trunc' or `open' for creation).
4f083fd7
SL
525 * 2) Link source to destination. If destination already exists,
526 * delete it first.
68f21562
KM
527 * 3) Unlink source reference to inode if still around. If a
528 * directory was moved and the parent of the destination
4f083fd7
SL
529 * is different from the source, patch the ".." entry in the
530 * directory.
4f083fd7 531 */
7188ac27
KM
532ufs_rename(fndp, tndp)
533 register struct nameidata *fndp, *tndp;
528f664c 534{
4f083fd7 535 register struct inode *ip, *xp, *dp;
68f21562
KM
536 struct dirtemplate dirbuf;
537 int doingdirectory = 0, oldparent = 0, newparent = 0;
a5390dce 538 int error = 0;
4f083fd7 539
7188ac27
KM
540 dp = VTOI(fndp->ni_dvp);
541 ip = VTOI(fndp->ni_vp);
542 ILOCK(ip);
4f083fd7 543 if ((ip->i_mode&IFMT) == IFDIR) {
7188ac27 544 register struct direct *d = &fndp->ni_dent;
4f083fd7 545
4f083fd7 546 /*
046f18d1 547 * Avoid ".", "..", and aliases of "." for obvious reasons.
4f083fd7 548 */
7188ac27
KM
549 if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip ||
550 fndp->ni_isdotdot || (ip->i_flag & IRENAME)) {
551 IUNLOCK(ip);
552 ufs_abortop(fndp);
553 ufs_abortop(tndp);
554 return (EINVAL);
4f083fd7 555 }
68f21562 556 ip->i_flag |= IRENAME;
4f083fd7
SL
557 oldparent = dp->i_number;
558 doingdirectory++;
559 }
7188ac27 560 vrele(fndp->ni_dvp);
4f083fd7
SL
561
562 /*
563 * 1) Bump link count while we're moving stuff
564 * around. If we crash somewhere before
565 * completing our work, the link count
566 * may be wrong, but correctable.
567 */
568 ip->i_nlink++;
569 ip->i_flag |= ICHG;
7188ac27 570 error = iupdat(ip, &time, &time, 1);
a388503d 571 IUNLOCK(ip);
4f083fd7
SL
572
573 /*
574 * When the target exists, both the directory
7188ac27 575 * and target vnodes are returned locked.
4f083fd7 576 */
7188ac27
KM
577 dp = VTOI(tndp->ni_dvp);
578 xp = NULL;
579 if (tndp->ni_vp)
580 xp = VTOI(tndp->ni_vp);
046f18d1
SL
581 /*
582 * If ".." must be changed (ie the directory gets a new
81552f0f
KM
583 * parent) then the source directory must not be in the
584 * directory heirarchy above the target, as this would
585 * orphan everything below the source directory. Also
586 * the user must have write permission in the source so
587 * as to be able to change "..". We must repeat the call
588 * to namei, as the parent directory is unlocked by the
589 * call to checkpath().
046f18d1 590 */
68f21562
KM
591 if (oldparent != dp->i_number)
592 newparent = dp->i_number;
593 if (doingdirectory && newparent) {
7188ac27 594 if (error = iaccess(ip, IWRITE, tndp->ni_cred))
81552f0f 595 goto bad;
7188ac27 596 tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
81552f0f 597 do {
7188ac27 598 dp = VTOI(tndp->ni_dvp);
81552f0f 599 if (xp != NULL)
7188ac27
KM
600 vput(ITOV(xp));
601 if (error = checkpath(ip, dp, tndp->ni_cred))
81552f0f 602 goto out;
7188ac27 603 if (error = namei(tndp))
81552f0f 604 goto out;
7188ac27
KM
605 xp = NULL;
606 if (tndp->ni_vp)
607 xp = VTOI(tndp->ni_vp);
608 } while (dp != VTOI(tndp->ni_dvp));
81552f0f 609 }
4f083fd7
SL
610 /*
611 * 2) If target doesn't exist, link the target
612 * to the source and unlink the source.
613 * Otherwise, rewrite the target directory
614 * entry to reference the source inode and
615 * expunge the original entry's existence.
616 */
4f083fd7 617 if (xp == NULL) {
7188ac27
KM
618 if (dp->i_dev != ip->i_dev)
619 panic("rename: EXDEV");
4f083fd7 620 /*
68f21562
KM
621 * Account for ".." in new directory.
622 * When source and destination have the same
623 * parent we don't fool with the link count.
4f083fd7 624 */
68f21562 625 if (doingdirectory && newparent) {
4f083fd7
SL
626 dp->i_nlink++;
627 dp->i_flag |= ICHG;
7188ac27 628 error = iupdat(dp, &time, &time, 1);
4f083fd7 629 }
7188ac27 630 if (error = direnter(ip, tndp))
4f083fd7
SL
631 goto out;
632 } else {
7188ac27
KM
633 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
634 panic("rename: EXDEV");
e69c3c9c
SL
635 /*
636 * Short circuit rename(foo, foo).
637 */
638 if (xp->i_number == ip->i_number)
7188ac27 639 panic("rename: same file");
80cee150
JB
640 /*
641 * If the parent directory is "sticky", then the user must
642 * own the parent directory, or the destination of the rename,
643 * otherwise the destination may not be changed (except by
644 * root). This implements append-only directories.
645 */
7188ac27
KM
646 if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 &&
647 tndp->ni_cred->cr_uid != dp->i_uid &&
648 xp->i_uid != tndp->ni_cred->cr_uid) {
80cee150
JB
649 error = EPERM;
650 goto bad;
651 }
4f083fd7 652 /*
a5390dce
SL
653 * Target must be empty if a directory
654 * and have no links to it.
4f083fd7
SL
655 * Also, insure source and target are
656 * compatible (both directories, or both
657 * not directories).
658 */
659 if ((xp->i_mode&IFMT) == IFDIR) {
7188ac27
KM
660 if (!dirempty(xp, dp->i_number, tndp->ni_cred) ||
661 xp->i_nlink > 2) {
a5390dce 662 error = ENOTEMPTY;
4f083fd7
SL
663 goto bad;
664 }
665 if (!doingdirectory) {
a5390dce 666 error = ENOTDIR;
4f083fd7
SL
667 goto bad;
668 }
7188ac27 669 cache_purge(ITOV(dp));
4f083fd7 670 } else if (doingdirectory) {
a5390dce 671 error = EISDIR;
4f083fd7
SL
672 goto bad;
673 }
7188ac27
KM
674 if (error = dirrewrite(dp, ip, tndp))
675 goto bad;
676 vput(ITOV(dp));
4f083fd7 677 /*
a5390dce
SL
678 * Adjust the link count of the target to
679 * reflect the dirrewrite above. If this is
680 * a directory it is empty and there are
681 * no links to it, so we can squash the inode and
682 * any space associated with it. We disallowed
683 * renaming over top of a directory with links to
68f21562
KM
684 * it above, as the remaining link would point to
685 * a directory without "." or ".." entries.
4f083fd7 686 */
a5390dce 687 xp->i_nlink--;
4f083fd7 688 if (doingdirectory) {
a5390dce
SL
689 if (--xp->i_nlink != 0)
690 panic("rename: linked directory");
7188ac27 691 error = itrunc(xp, (u_long)0);
a5390dce 692 }
4f083fd7 693 xp->i_flag |= ICHG;
7188ac27 694 vput(ITOV(xp));
31db12cb 695 xp = NULL;
4f083fd7
SL
696 }
697
698 /*
699 * 3) Unlink the source.
700 */
7188ac27
KM
701 fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
702 (void)namei(fndp);
703 if (fndp->ni_vp != NULL) {
704 xp = VTOI(fndp->ni_vp);
705 dp = VTOI(fndp->ni_dvp);
706 } else {
707 xp = NULL;
4f1a9037 708 dp = NULL;
7188ac27 709 }
4f083fd7 710 /*
7188ac27 711 * Ensure that the directory entry still exists and has not
68f21562
KM
712 * changed while the new name has been entered. If the source is
713 * a file then the entry may have been unlinked or renamed. In
714 * either case there is no further work to be done. If the source
715 * is a directory then it cannot have been rmdir'ed; its link
716 * count of three would cause a rmdir to fail with ENOTEMPTY.
7188ac27 717 * The IRENAME flag ensures that it cannot be moved by another
68f21562 718 * rename.
4f083fd7 719 */
4f1a9037 720 if (xp != ip) {
68f21562 721 if (doingdirectory)
4f1a9037 722 panic("rename: lost dir entry");
68f21562 723 } else {
4f083fd7 724 /*
68f21562
KM
725 * If the source is a directory with a
726 * new parent, the link count of the old
727 * parent directory must be decremented
728 * and ".." set to point to the new parent.
4f083fd7 729 */
68f21562 730 if (doingdirectory && newparent) {
4f083fd7
SL
731 dp->i_nlink--;
732 dp->i_flag |= ICHG;
68f21562 733 error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf,
7188ac27 734 sizeof (struct dirtemplate), (off_t)0,
93e273b9 735 UIO_SYSSPACE, tndp->ni_cred, (int *)0);
68f21562
KM
736 if (error == 0) {
737 if (dirbuf.dotdot_namlen != 2 ||
738 dirbuf.dotdot_name[0] != '.' ||
739 dirbuf.dotdot_name[1] != '.') {
740 printf("rename: mangled dir\n");
741 } else {
742 dirbuf.dotdot_ino = newparent;
743 (void) rdwri(UIO_WRITE, xp,
744 (caddr_t)&dirbuf,
745 sizeof (struct dirtemplate),
93e273b9 746 (off_t)0, UIO_SYSSPACE,
7188ac27
KM
747 tndp->ni_cred, (int *)0);
748 cache_purge(ITOV(dp));
68f21562
KM
749 }
750 }
4f083fd7 751 }
7188ac27
KM
752 error = dirremove(fndp);
753 if (!error) {
68f21562
KM
754 xp->i_nlink--;
755 xp->i_flag |= ICHG;
4f083fd7 756 }
68f21562 757 xp->i_flag &= ~IRENAME;
4f083fd7 758 }
4f083fd7 759 if (dp)
7188ac27 760 vput(ITOV(dp));
68f21562 761 if (xp)
7188ac27
KM
762 vput(ITOV(xp));
763 vrele(ITOV(ip));
764 return (error);
a5390dce 765
4f083fd7 766bad:
4f083fd7 767 if (xp)
7188ac27
KM
768 vput(ITOV(xp));
769 vput(ITOV(dp));
4f083fd7
SL
770out:
771 ip->i_nlink--;
772 ip->i_flag |= ICHG;
7188ac27
KM
773 vrele(ITOV(ip));
774 return (error);
64d3a787 775}
88a7a62a
SL
776
777/*
778 * A virgin directory (no blushing please).
779 */
780struct dirtemplate mastertemplate = {
781 0, 12, 1, ".",
782 0, DIRBLKSIZ - 12, 2, ".."
783};
784
785/*
786 * Mkdir system call
787 */
7188ac27
KM
788ufs_mkdir(ndp, vap)
789 struct nameidata *ndp;
790 struct vattr *vap;
88a7a62a 791{
88a7a62a 792 register struct inode *ip, *dp;
7188ac27
KM
793 struct inode *tip;
794 struct vnode *dvp;
88a7a62a 795 struct dirtemplate dirtemplate;
7188ac27
KM
796 int error;
797 int dmode;
798
799 dvp = ndp->ni_dvp;
800 dp = VTOI(dvp);
801 dmode = vap->va_mode&0777;
802 dmode |= IFDIR;
88a7a62a
SL
803 /*
804 * Must simulate part of maknode here
805 * in order to acquire the inode, but
806 * not have it entered in the parent
807 * directory. The entry is made later
808 * after writing "." and ".." entries out.
809 */
7188ac27
KM
810 error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip);
811 if (error) {
88a7a62a 812 iput(dp);
7188ac27 813 return (error);
88a7a62a 814 }
7188ac27 815 ip = tip;
88a7a62a
SL
816#ifdef QUOTA
817 if (ip->i_dquot != NODQUOT)
818 panic("mkdir: dquot");
819#endif
820 ip->i_flag |= IACC|IUPD|ICHG;
7188ac27
KM
821 ip->i_mode = dmode;
822 ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */
88a7a62a 823 ip->i_nlink = 2;
7188ac27 824 ip->i_uid = ndp->ni_cred->cr_uid;
88a7a62a
SL
825 ip->i_gid = dp->i_gid;
826#ifdef QUOTA
827 ip->i_dquot = inoquota(ip);
828#endif
7188ac27 829 error = iupdat(ip, &time, &time, 1);
88a7a62a
SL
830
831 /*
832 * Bump link count in parent directory
833 * to reflect work done below. Should
834 * be done before reference is created
835 * so reparation is possible if we crash.
836 */
837 dp->i_nlink++;
838 dp->i_flag |= ICHG;
7188ac27 839 error = iupdat(dp, &time, &time, 1);
88a7a62a
SL
840
841 /*
842 * Initialize directory with "."
843 * and ".." from static template.
844 */
845 dirtemplate = mastertemplate;
846 dirtemplate.dot_ino = ip->i_number;
847 dirtemplate.dotdot_ino = dp->i_number;
7188ac27
KM
848 error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate,
849 sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
850 ndp->ni_cred, (int *)0);
851 if (error) {
88a7a62a
SL
852 dp->i_nlink--;
853 dp->i_flag |= ICHG;
854 goto bad;
855 }
7188ac27
KM
856 if (DIRBLKSIZ > dp->i_fs->fs_fsize)
857 panic("mkdir: blksize"); /* XXX - should grow w/balloc() */
23de9f20
KM
858 else
859 ip->i_size = DIRBLKSIZ;
88a7a62a
SL
860 /*
861 * Directory all set up, now
862 * install the entry for it in
863 * the parent directory.
864 */
7188ac27 865 error = direnter(ip, ndp);
88a7a62a 866 dp = NULL;
7188ac27 867 if (error) {
715baff1 868 ndp->ni_nameiop = LOOKUP | NOCACHE;
7188ac27
KM
869 error = namei(ndp);
870 if (!error) {
871 dp = VTOI(ndp->ni_vp);
88a7a62a
SL
872 dp->i_nlink--;
873 dp->i_flag |= ICHG;
874 }
875 }
876bad:
877 /*
878 * No need to do an explicit itrunc here,
7188ac27 879 * vrele will do this for us because we set
88a7a62a
SL
880 * the link count to 0.
881 */
7188ac27 882 if (error) {
88a7a62a
SL
883 ip->i_nlink = 0;
884 ip->i_flag |= ICHG;
885 }
7188ac27 886 iput(ip);
88a7a62a
SL
887 if (dp)
888 iput(dp);
7188ac27 889 return (error);
88a7a62a
SL
890}
891
892/*
893 * Rmdir system call.
894 */
7188ac27
KM
895ufs_rmdir(ndp)
896 register struct nameidata *ndp;
88a7a62a 897{
88a7a62a 898 register struct inode *ip, *dp;
7188ac27
KM
899 int error = 0;
900
901 ip = VTOI(ndp->ni_vp);
902 dp = VTOI(ndp->ni_dvp);
88a7a62a
SL
903 /*
904 * No rmdir "." please.
905 */
906 if (dp == ip) {
7188ac27 907 vrele(ITOV(dp));
88a7a62a 908 iput(ip);
7188ac27 909 return (EINVAL);
88a7a62a
SL
910 }
911 /*
912 * Verify the directory is empty (and valid).
913 * (Rmdir ".." won't be valid since
914 * ".." will contain a reference to
915 * the current directory and thus be
916 * non-empty.)
917 */
7188ac27
KM
918 if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
919 error = ENOTEMPTY;
88a7a62a
SL
920 goto out;
921 }
922 /*
923 * Delete reference to directory before purging
924 * inode. If we crash in between, the directory
925 * will be reattached to lost+found,
926 */
7188ac27 927 if (error = dirremove(ndp))
88a7a62a
SL
928 goto out;
929 dp->i_nlink--;
930 dp->i_flag |= ICHG;
7188ac27 931 cache_purge(ITOV(dp));
88a7a62a 932 iput(dp);
7188ac27 933 ndp->ni_dvp = NULL;
88a7a62a
SL
934 /*
935 * Truncate inode. The only stuff left
936 * in the directory is "." and "..". The
937 * "." reference is inconsequential since
938 * we're quashing it. The ".." reference
939 * has already been adjusted above. We've
940 * removed the "." reference and the reference
941 * in the parent directory, but there may be
942 * other hard links so decrement by 2 and
943 * worry about them later.
944 */
945 ip->i_nlink -= 2;
7188ac27
KM
946 error = itrunc(ip, (u_long)0);
947 cache_purge(ITOV(ip));
88a7a62a 948out:
7188ac27 949 if (ndp->ni_dvp)
88a7a62a
SL
950 iput(dp);
951 iput(ip);
7188ac27 952 return (error);
88a7a62a
SL
953}
954
7188ac27
KM
955/*
956 * symlink -- make a symbolic link
957 */
958ufs_symlink(ndp, vap, target)
959 struct nameidata *ndp;
960 struct vattr *vap;
961 char *target;
962{
963 struct inode *ip;
964 int error;
965
966 error = maknode(IFLNK | vap->va_mode, ndp, &ip);
967 if (error)
968 return (error);
969 error = rdwri(UIO_WRITE, ip, target, strlen(target), (off_t)0,
970 UIO_SYSSPACE, ndp->ni_cred, (int *)0);
971 iput(ip);
972 return (error);
973}
974
975/*
976 * Vnode op for read and write
977 */
978ufs_readdir(vp, uio, offp, cred)
979 struct vnode *vp;
980 register struct uio *uio;
981 off_t *offp;
982 struct ucred *cred;
88a7a62a 983{
7188ac27
KM
984 register struct inode *ip = VTOI(vp);
985 int count, error;
88a7a62a 986
7188ac27
KM
987 ILOCK(ip);
988 uio->uio_offset = *offp;
989 count = uio->uio_resid;
990 count &= ~(DIRBLKSIZ - 1);
991 if (vp->v_type != VDIR || uio->uio_iovcnt != 1 ||
992 (count < DIRBLKSIZ) || (uio->uio_offset & (DIRBLKSIZ -1))) {
993 IUNLOCK(ip);
994 return (EINVAL);
995 }
996 uio->uio_resid = count;
997 uio->uio_iov->iov_len = count;
998 error = readip(ip, uio, cred);
999 *offp += count - uio->uio_resid;
1000 IUNLOCK(ip);
1001 return (error);
1002}
1003
1004/*
1005 * Return target name of a symbolic link
1006 */
1007ufs_readlink(vp, uiop, cred)
1008 struct vnode *vp;
1009 struct uio *uiop;
1010 struct ucred *cred;
1011{
1012
1013 return (readip(VTOI(vp), uiop, cred));
1014}
1015
1016/*
1017 * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
1018 * done. Iff ni_vp/ni_dvp not null and locked, unlock.
1019 */
1020ufs_abortop(ndp)
1021 register struct nameidata *ndp;
1022{
1023 register struct inode *ip;
1024
1025 if (ndp->ni_vp) {
1026 ip = VTOI(ndp->ni_vp);
1027 if (ip->i_flag & ILOCKED)
1028 IUNLOCK(ip);
1029 vrele(ndp->ni_vp);
8462a185 1030 }
7188ac27
KM
1031 if (ndp->ni_dvp) {
1032 ip = VTOI(ndp->ni_dvp);
1033 if (ip->i_flag & ILOCKED)
1034 IUNLOCK(ip);
1035 vrele(ndp->ni_dvp);
88a7a62a 1036 }
7188ac27
KM
1037 return;
1038}
1039
1040ufs_lock(vp)
1041 struct vnode *vp;
1042{
1043 register struct inode *ip = VTOI(vp);
1044
1045 ILOCK(ip);
1046 return (0);
1047}
1048
1049ufs_unlock(vp)
1050 struct vnode *vp;
1051{
1052 register struct inode *ip = VTOI(vp);
1053
1054 if (!(ip->i_flag & ILOCKED))
1055 panic("ufs_unlock NOT LOCKED");
1056 IUNLOCK(ip);
1057 return (0);
1058}
1059
1060/*
1061 * Get access to bmap
1062 */
1063ufs_bmap(vp, bn, vpp, bnp)
1064 struct vnode *vp;
1065 daddr_t bn;
1066 struct vnode **vpp;
1067 daddr_t *bnp;
1068{
1069 struct inode *ip = VTOI(vp);
1070
1071 if (vpp != NULL)
1072 *vpp = ip->i_devvp;
1073 if (bnp == NULL)
1074 return (0);
1075 return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0));
88a7a62a
SL
1076}
1077
1078/*
7188ac27 1079 * Just call the device strategy routine
88a7a62a 1080 */
7188ac27
KM
1081ufs_strategy(bp)
1082 register struct buf *bp;
88a7a62a 1083{
7188ac27
KM
1084 (*bdevsw[major(bp->b_dev)].d_strategy)(bp);
1085 return (0);
1086}
88a7a62a 1087
7188ac27
KM
1088/*
1089 * Make a new file.
1090 */
1091maknode(mode, ndp, ipp)
1092 int mode;
1093 register struct nameidata *ndp;
1094 struct inode **ipp;
1095{
1096 register struct inode *ip;
1097 struct inode *tip;
1098 register struct inode *pdir = VTOI(ndp->ni_dvp);
1099 ino_t ipref;
1100 int error;
1101
1102 *ipp = 0;
1103 if ((mode & IFMT) == IFDIR)
1104 ipref = dirpref(pdir->i_fs);
1105 else
1106 ipref = pdir->i_number;
1107 error = ialloc(pdir, ipref, mode, &tip);
1108 if (error) {
1109 iput(pdir);
1110 return (error);
1111 }
1112 ip = tip;
1113#ifdef QUOTA
1114 if (ip->i_dquot != NODQUOT)
1115 panic("maknode: dquot");
1116#endif
1117 ip->i_flag |= IACC|IUPD|ICHG;
1118 if ((mode & IFMT) == 0)
1119 mode |= IFREG;
1120 ip->i_mode = mode;
1121 ITOV(ip)->v_type = IFTOVT(mode); /* Rest init'd in iget() */
1122 ip->i_nlink = 1;
1123 ip->i_uid = ndp->ni_cred->cr_uid;
1124 ip->i_gid = pdir->i_gid;
1125 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) &&
1126 suser(ndp->ni_cred, NULL))
1127 ip->i_mode &= ~ISGID;
1128#ifdef QUOTA
1129 ip->i_dquot = inoquota(ip);
1130#endif
1131
1132 /*
1133 * Make sure inode goes to disk before directory entry.
1134 */
1135 if ((error = iupdat(ip, &time, &time, 1)) ||
1136 (error = direnter(ip, ndp))) {
1137 /*
1138 * Write error occurred trying to update the inode
1139 * or the directory so must deallocate the inode.
1140 */
1141 ip->i_nlink = 0;
1142 ip->i_flag |= ICHG;
1143 iput(ip);
1144 return (error);
1145 }
1146 *ipp = ip;
1147 return (0);
88a7a62a 1148}