xref: /original-bsd/sys/ufs/ufs/ufs_vnops.c (revision 87febec0)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.
4  *
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  *
17  *	@(#)ufs_vnops.c	7.9 (Berkeley) 05/09/89
18  */
19 
20 #include "param.h"
21 #include "systm.h"
22 #include "user.h"
23 #include "kernel.h"
24 #include "file.h"
25 #include "stat.h"
26 #include "buf.h"
27 #include "proc.h"
28 #include "uio.h"
29 #include "socket.h"
30 #include "socketvar.h"
31 #include "conf.h"
32 #include "mount.h"
33 #include "vnode.h"
34 #include "../ufs/inode.h"
35 #include "../ufs/fs.h"
36 #include "../ufs/quota.h"
37 
38 /*
39  * Global vfs data structures for ufs
40  */
41 
42 int	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 
72 struct 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 
104 enum vtype iftovt_tab[8] = {
105 	VNON, VCHR, VDIR, VBLK, VREG, VLNK, VSOCK, VBAD,
106 };
107 int	vttoif_tab[8] = {
108 	0, IFREG, IFDIR, IFBLK, IFCHR, IFLNK, IFSOCK, IFMT,
109 };
110 
111 /*
112  * Create a regular file
113  */
114 ufs_create(ndp, vap)
115 	struct nameidata *ndp;
116 	struct vattr *vap;
117 {
118 	struct inode *ip;
119 	int error;
120 
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);
125 }
126 
127 /*
128  * Mknod vnode call
129  */
130 /* ARGSUSED */
131 ufs_mknod(ndp, vap, cred)
132 	struct nameidata *ndp;
133 	struct ucred *cred;
134 	struct vattr *vap;
135 {
136 	struct inode *ip;
137 	int error;
138 
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 	}
149 	iput(ip);
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);
159 }
160 
161 /*
162  * Open called.
163  *
164  * Nothing to do.
165  */
166 /* ARGSUSED */
167 ufs_open(vp, mode, cred)
168 	struct vnode *vp;
169 	int mode;
170 	struct ucred *cred;
171 {
172 
173 	return (0);
174 }
175 
176 /*
177  * Close called
178  *
179  * Update the times on the inode.
180  */
181 /* ARGSUSED */
182 ufs_close(vp, fflag, cred)
183 	struct vnode *vp;
184 	int fflag;
185 	struct ucred *cred;
186 {
187 	register struct inode *ip = VTOI(vp);
188 
189 	if (vp->v_count > 1 && !(ip->i_flag & ILOCKED))
190 		ITIMES(ip, &time, &time);
191 	return (0);
192 }
193 
194 ufs_access(vp, mode, cred)
195 	struct vnode *vp;
196 	int mode;
197 	struct ucred *cred;
198 {
199 
200 	return (iaccess(VTOI(vp), mode, cred));
201 }
202 
203 /* ARGSUSED */
204 ufs_getattr(vp, vap, cred)
205 	struct vnode *vp;
206 	register struct vattr *vap;
207 	struct ucred *cred;
208 {
209 	register struct inode *ip = VTOI(vp);
210 
211 	ITIMES(ip, &time, &time);
212 	/*
213 	 * Copy from inode table
214 	 */
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;
232 	else
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);
242 }
243 
244 /*
245  * Set attribute vnode op. called from several syscalls
246  */
247 ufs_setattr(vp, vap, cred)
248 	register struct vnode *vp;
249 	register struct vattr *vap;
250 	register struct ucred *cred;
251 {
252 	register struct inode *ip = VTOI(vp);
253 	int error = 0;
254 
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);
263 	}
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);
277 	}
278 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
279 		if (cred->cr_uid != ip->i_uid &&
280 		    (error = suser(cred, &u.u_acflag)))
281 			return (error);
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);
289 	}
290 	if (vap->va_mode != (u_short)VNOVAL)
291 		error = chmod1(vp, (int)vap->va_mode, cred);
292 	return (error);
293 }
294 
295 /*
296  * Change the mode on a file.
297  * Inode must be locked before calling.
298  */
299 chmod1(vp, mode, cred)
300 	register struct vnode *vp;
301 	register int mode;
302 	struct ucred *cred;
303 {
304 	register struct inode *ip = VTOI(vp);
305 	int error;
306 
307 	if (cred->cr_uid != ip->i_uid &&
308 	    (error = suser(cred, &u.u_acflag)))
309 		return (error);
310 	ip->i_mode &= ~07777;
311 	if (cred->cr_uid) {
312 		if (vp->v_type != VDIR)
313 			mode &= ~ISVTX;
314 		if (!groupmember(ip->i_gid, cred))
315 			mode &= ~ISGID;
316 	}
317 	ip->i_mode |= mode & 07777;
318 	ip->i_flag |= ICHG;
319 	if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
320 		xrele(vp);
321 	return (0);
322 }
323 
324 /*
325  * Perform chown operation on inode ip;
326  * inode must be locked prior to call.
327  */
328 chown1(vp, uid, gid, cred)
329 	register struct vnode *vp;
330 	uid_t uid;
331 	gid_t gid;
332 	struct ucred *cred;
333 {
334 	register struct inode *ip = VTOI(vp);
335 #ifdef QUOTA
336 	register long change;
337 #endif
338 	int error;
339 
340 	if (uid == (u_short)VNOVAL)
341 		uid = ip->i_uid;
342 	if (gid == (u_short)VNOVAL)
343 		gid = ip->i_gid;
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 	 */
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);
353 #ifdef QUOTA
354 	if (ip->i_uid == uid)		/* this just speeds things a little */
355 		change = 0;
356 	else
357 		change = ip->i_blocks;
358 	(void) chkdq(ip, -change, 1);
359 	(void) chkiq(ip->i_dev, ip, ip->i_uid, 1);
360 	dqrele(ip->i_dquot);
361 #endif
362 	ip->i_uid = uid;
363 	ip->i_gid = gid;
364 	ip->i_flag |= ICHG;
365 	if (cred->cr_ruid != 0)
366 		ip->i_mode &= ~(ISUID|ISGID);
367 #ifdef QUOTA
368 	ip->i_dquot = inoquota(ip);
369 	(void) chkdq(ip, change, 1);
370 	(void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1);
371 	return (u.u_error);		/* should == 0 ALWAYS !! */
372 #else
373 	return (0);
374 #endif
375 }
376 
377 /* ARGSUSED */
378 ufs_ioctl(vp, com, data, fflag, cred)
379 	struct vnode *vp;
380 	int com;
381 	caddr_t data;
382 	int fflag;
383 	struct ucred *cred;
384 {
385 
386 	printf("ufs_ioctl called with type %d\n", vp->v_type);
387 	return (ENOTTY);
388 }
389 
390 /* ARGSUSED */
391 ufs_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 */
399 }
400 
401 /*
402  * Mmap a file
403  *
404  * NB Currently unsupported.
405  */
406 /* ARGSUSED */
407 ufs_mmap(vp, fflags, cred)
408 	struct vnode *vp;
409 	int fflags;
410 	struct ucred *cred;
411 {
412 
413 	return (EINVAL);
414 }
415 
416 /*
417  * Synch an open file.
418  */
419 /* ARGSUSED */
420 ufs_fsync(vp, fflags, cred)
421 	struct vnode *vp;
422 	int fflags;
423 	struct ucred *cred;
424 {
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);
434 }
435 
436 /*
437  * Seek on a file
438  *
439  * Nothing to do, so just return.
440  */
441 /* ARGSUSED */
442 ufs_seek(vp, oldoff, newoff, cred)
443 	struct vnode *vp;
444 	off_t oldoff, newoff;
445 	struct ucred *cred;
446 {
447 
448 	return (0);
449 }
450 
451 /*
452  * ufs remove
453  * Hard to avoid races here, especially
454  * in unlinking directories.
455  */
456 ufs_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;
468 	}
469 	if (dp == ip)
470 		vrele(ITOV(ip));
471 	else
472 		iput(ip);
473 	iput(dp);
474 	return (error);
475 }
476 
477 /*
478  * link vnode call
479  */
480 ufs_link(vp, ndp)
481 	register struct vnode *vp;
482 	register struct nameidata *ndp;
483 {
484 	register struct inode *ip = VTOI(vp);
485 	int error;
486 
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);
498 out:
499 	if (ndp->ni_dvp != vp)
500 		IUNLOCK(ip);
501 	if (error) {
502 		ip->i_nlink--;
503 		ip->i_flag |= ICHG;
504 	}
505 	return (error);
506 }
507 
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
522  *    target.  This also ensure the inode won't be deleted out
523  *    from underneath us while we work (it may be truncated by
524  *    a concurrent `trunc' or `open' for creation).
525  * 2) Link source to destination.  If destination already exists,
526  *    delete it first.
527  * 3) Unlink source reference to inode if still around. If a
528  *    directory was moved and the parent of the destination
529  *    is different from the source, patch the ".." entry in the
530  *    directory.
531  */
532 ufs_rename(fndp, tndp)
533 	register struct nameidata *fndp, *tndp;
534 {
535 	register struct inode *ip, *xp, *dp;
536 	struct dirtemplate dirbuf;
537 	int doingdirectory = 0, oldparent = 0, newparent = 0;
538 	int error = 0;
539 
540 	dp = VTOI(fndp->ni_dvp);
541 	ip = VTOI(fndp->ni_vp);
542 	ILOCK(ip);
543 	if ((ip->i_mode&IFMT) == IFDIR) {
544 		register struct direct *d = &fndp->ni_dent;
545 
546 		/*
547 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
548 		 */
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);
555 		}
556 		ip->i_flag |= IRENAME;
557 		oldparent = dp->i_number;
558 		doingdirectory++;
559 	}
560 	vrele(fndp->ni_dvp);
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;
570 	error = iupdat(ip, &time, &time, 1);
571 	IUNLOCK(ip);
572 
573 	/*
574 	 * When the target exists, both the directory
575 	 * and target vnodes are returned locked.
576 	 */
577 	dp = VTOI(tndp->ni_dvp);
578 	xp = NULL;
579 	if (tndp->ni_vp)
580 		xp = VTOI(tndp->ni_vp);
581 	/*
582 	 * If ".." must be changed (ie the directory gets a new
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().
590 	 */
591 	if (oldparent != dp->i_number)
592 		newparent = dp->i_number;
593 	if (doingdirectory && newparent) {
594 		if (error = iaccess(ip, IWRITE, tndp->ni_cred))
595 			goto bad;
596 		tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
597 		do {
598 			dp = VTOI(tndp->ni_dvp);
599 			if (xp != NULL)
600 				vput(ITOV(xp));
601 			if (error = checkpath(ip, dp, tndp->ni_cred))
602 				goto out;
603 			if (error = namei(tndp))
604 				goto out;
605 			xp = NULL;
606 			if (tndp->ni_vp)
607 				xp = VTOI(tndp->ni_vp);
608 		} while (dp != VTOI(tndp->ni_dvp));
609 	}
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 	 */
617 	if (xp == NULL) {
618 		if (dp->i_dev != ip->i_dev)
619 			panic("rename: EXDEV");
620 		/*
621 		 * Account for ".." in new directory.
622 		 * When source and destination have the same
623 		 * parent we don't fool with the link count.
624 		 */
625 		if (doingdirectory && newparent) {
626 			dp->i_nlink++;
627 			dp->i_flag |= ICHG;
628 			error = iupdat(dp, &time, &time, 1);
629 		}
630 		if (error = direnter(ip, tndp))
631 			goto out;
632 	} else {
633 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
634 			panic("rename: EXDEV");
635 		/*
636 		 * Short circuit rename(foo, foo).
637 		 */
638 		if (xp->i_number == ip->i_number)
639 			panic("rename: same file");
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 		 */
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) {
649 			error = EPERM;
650 			goto bad;
651 		}
652 		/*
653 		 * Target must be empty if a directory
654 		 * and have no links to it.
655 		 * Also, insure source and target are
656 		 * compatible (both directories, or both
657 		 * not directories).
658 		 */
659 		if ((xp->i_mode&IFMT) == IFDIR) {
660 			if (!dirempty(xp, dp->i_number, tndp->ni_cred) ||
661 			    xp->i_nlink > 2) {
662 				error = ENOTEMPTY;
663 				goto bad;
664 			}
665 			if (!doingdirectory) {
666 				error = ENOTDIR;
667 				goto bad;
668 			}
669 			cache_purge(ITOV(dp));
670 		} else if (doingdirectory) {
671 			error = EISDIR;
672 			goto bad;
673 		}
674 		if (error = dirrewrite(dp, ip, tndp))
675 			goto bad;
676 		vput(ITOV(dp));
677 		/*
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
684 		 * it above, as the remaining link would point to
685 		 * a directory without "." or ".." entries.
686 		 */
687 		xp->i_nlink--;
688 		if (doingdirectory) {
689 			if (--xp->i_nlink != 0)
690 				panic("rename: linked directory");
691 			error = itrunc(xp, (u_long)0);
692 		}
693 		xp->i_flag |= ICHG;
694 		vput(ITOV(xp));
695 		xp = NULL;
696 	}
697 
698 	/*
699 	 * 3) Unlink the source.
700 	 */
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;
708 		dp = NULL;
709 	}
710 	/*
711 	 * Ensure that the directory entry still exists and has not
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.
717 	 * The IRENAME flag ensures that it cannot be moved by another
718 	 * rename.
719 	 */
720 	if (xp != ip) {
721 		if (doingdirectory)
722 			panic("rename: lost dir entry");
723 	} else {
724 		/*
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.
729 		 */
730 		if (doingdirectory && newparent) {
731 			dp->i_nlink--;
732 			dp->i_flag |= ICHG;
733 			error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf,
734 				sizeof (struct dirtemplate), (off_t)0,
735 				UIO_SYSSPACE, tndp->ni_cred, (int *)0);
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),
746 					    (off_t)0, UIO_SYSSPACE,
747 					    tndp->ni_cred, (int *)0);
748 					cache_purge(ITOV(dp));
749 				}
750 			}
751 		}
752 		error = dirremove(fndp);
753 		if (!error) {
754 			xp->i_nlink--;
755 			xp->i_flag |= ICHG;
756 		}
757 		xp->i_flag &= ~IRENAME;
758 	}
759 	if (dp)
760 		vput(ITOV(dp));
761 	if (xp)
762 		vput(ITOV(xp));
763 	vrele(ITOV(ip));
764 	return (error);
765 
766 bad:
767 	if (xp)
768 		vput(ITOV(xp));
769 	vput(ITOV(dp));
770 out:
771 	ip->i_nlink--;
772 	ip->i_flag |= ICHG;
773 	vrele(ITOV(ip));
774 	return (error);
775 }
776 
777 /*
778  * A virgin directory (no blushing please).
779  */
780 struct dirtemplate mastertemplate = {
781 	0, 12, 1, ".",
782 	0, DIRBLKSIZ - 12, 2, ".."
783 };
784 
785 /*
786  * Mkdir system call
787  */
788 ufs_mkdir(ndp, vap)
789 	struct nameidata *ndp;
790 	struct vattr *vap;
791 {
792 	register struct inode *ip, *dp;
793 	struct inode *tip;
794 	struct vnode *dvp;
795 	struct dirtemplate dirtemplate;
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;
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 	 */
810 	error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip);
811 	if (error) {
812 		iput(dp);
813 		return (error);
814 	}
815 	ip = tip;
816 #ifdef QUOTA
817 	if (ip->i_dquot != NODQUOT)
818 		panic("mkdir: dquot");
819 #endif
820 	ip->i_flag |= IACC|IUPD|ICHG;
821 	ip->i_mode = dmode;
822 	ITOV(ip)->v_type = VDIR;	/* Rest init'd in iget() */
823 	ip->i_nlink = 2;
824 	ip->i_uid = ndp->ni_cred->cr_uid;
825 	ip->i_gid = dp->i_gid;
826 #ifdef QUOTA
827 	ip->i_dquot = inoquota(ip);
828 #endif
829 	error = iupdat(ip, &time, &time, 1);
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;
839 	error = iupdat(dp, &time, &time, 1);
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;
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) {
852 		dp->i_nlink--;
853 		dp->i_flag |= ICHG;
854 		goto bad;
855 	}
856 	if (DIRBLKSIZ > dp->i_fs->fs_fsize)
857 		panic("mkdir: blksize");     /* XXX - should grow w/balloc() */
858 	else
859 		ip->i_size = DIRBLKSIZ;
860 	/*
861 	 * Directory all set up, now
862 	 * install the entry for it in
863 	 * the parent directory.
864 	 */
865 	error = direnter(ip, ndp);
866 	dp = NULL;
867 	if (error) {
868 		ndp->ni_nameiop = LOOKUP | NOCACHE;
869 		error = namei(ndp);
870 		if (!error) {
871 			dp = VTOI(ndp->ni_vp);
872 			dp->i_nlink--;
873 			dp->i_flag |= ICHG;
874 		}
875 	}
876 bad:
877 	/*
878 	 * No need to do an explicit itrunc here,
879 	 * vrele will do this for us because we set
880 	 * the link count to 0.
881 	 */
882 	if (error) {
883 		ip->i_nlink = 0;
884 		ip->i_flag |= ICHG;
885 	}
886 	iput(ip);
887 	if (dp)
888 		iput(dp);
889 	return (error);
890 }
891 
892 /*
893  * Rmdir system call.
894  */
895 ufs_rmdir(ndp)
896 	register struct nameidata *ndp;
897 {
898 	register struct inode *ip, *dp;
899 	int error = 0;
900 
901 	ip = VTOI(ndp->ni_vp);
902 	dp = VTOI(ndp->ni_dvp);
903 	/*
904 	 * No rmdir "." please.
905 	 */
906 	if (dp == ip) {
907 		vrele(ITOV(dp));
908 		iput(ip);
909 		return (EINVAL);
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 	 */
918 	if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
919 		error = ENOTEMPTY;
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 	 */
927 	if (error = dirremove(ndp))
928 		goto out;
929 	dp->i_nlink--;
930 	dp->i_flag |= ICHG;
931 	cache_purge(ITOV(dp));
932 	iput(dp);
933 	ndp->ni_dvp = NULL;
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;
946 	error = itrunc(ip, (u_long)0);
947 	cache_purge(ITOV(ip));
948 out:
949 	if (ndp->ni_dvp)
950 		iput(dp);
951 	iput(ip);
952 	return (error);
953 }
954 
955 /*
956  * symlink -- make a symbolic link
957  */
958 ufs_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  */
978 ufs_readdir(vp, uio, offp, cred)
979 	struct vnode *vp;
980 	register struct uio *uio;
981 	off_t *offp;
982 	struct ucred *cred;
983 {
984 	register struct inode *ip = VTOI(vp);
985 	int count, error;
986 
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  */
1007 ufs_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  */
1020 ufs_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);
1030 	}
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);
1036 	}
1037 	return;
1038 }
1039 
1040 ufs_lock(vp)
1041 	struct vnode *vp;
1042 {
1043 	register struct inode *ip = VTOI(vp);
1044 
1045 	ILOCK(ip);
1046 	return (0);
1047 }
1048 
1049 ufs_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  */
1063 ufs_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));
1076 }
1077 
1078 /*
1079  * Just call the device strategy routine
1080  */
1081 ufs_strategy(bp)
1082 	register struct buf *bp;
1083 {
1084 	(*bdevsw[major(bp->b_dev)].d_strategy)(bp);
1085 	return (0);
1086 }
1087 
1088 /*
1089  * Make a new file.
1090  */
1091 maknode(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);
1148 }
1149