xref: /original-bsd/sys/ufs/ffs/ufs_vnops.c (revision 07d71086)
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.14 (Berkeley) 08/10/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_atime.tv_usec = 0;
226 	vap->va_mtime.tv_sec = ip->i_mtime;
227 	vap->va_mtime.tv_usec = 0;
228 	vap->va_ctime.tv_sec = ip->i_ctime;
229 	vap->va_ctime.tv_usec = 0;
230 	vap->va_flags = ip->i_flags;
231 	vap->va_gen = ip->i_gen;
232 	/* this doesn't belong here */
233 	if (vp->v_type == VBLK)
234 		vap->va_blocksize = BLKDEV_IOSIZE;
235 	else if (vp->v_type == VCHR)
236 		vap->va_blocksize = MAXBSIZE;
237 	else
238 		vap->va_blocksize = ip->i_fs->fs_bsize;
239 	/*
240 	 * XXX THIS IS NOT CORRECT!!, but be sure to change vn_stat()
241 	 * if you change it.
242 	 */
243 	vap->va_bytes = ip->i_blocks;
244 	vap->va_bytes1 = -1;
245 	vap->va_type = vp->v_type;
246 	return (0);
247 }
248 
249 /*
250  * Set attribute vnode op. called from several syscalls
251  */
252 ufs_setattr(vp, vap, cred)
253 	register struct vnode *vp;
254 	register struct vattr *vap;
255 	register struct ucred *cred;
256 {
257 	register struct inode *ip = VTOI(vp);
258 	int error = 0;
259 
260 	/*
261 	 * Check for unsetable attributes.
262 	 */
263 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
264 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
265 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
266 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
267 		return (EINVAL);
268 	}
269 	/*
270 	 * Go through the fields and update iff not VNOVAL.
271 	 */
272 	if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
273 		if (error = chown1(vp, vap->va_uid, vap->va_gid, cred))
274 			return (error);
275 	if (vap->va_size != VNOVAL) {
276 		if (vp->v_type == VDIR)
277 			return (EISDIR);
278 		if (error = itrunc(ip, vap->va_size))
279 			return (error);
280 	}
281 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
282 		if (cred->cr_uid != ip->i_uid &&
283 		    (error = suser(cred, &u.u_acflag)))
284 			return (error);
285 		if (vap->va_atime.tv_sec != VNOVAL)
286 			ip->i_flag |= IACC;
287 		if (vap->va_mtime.tv_sec != VNOVAL)
288 			ip->i_flag |= IUPD;
289 		ip->i_flag |= ICHG;
290 		if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1))
291 			return (error);
292 	}
293 	if (vap->va_mode != (u_short)VNOVAL)
294 		error = chmod1(vp, (int)vap->va_mode, cred);
295 	if (vap->va_flags != VNOVAL) {
296 		if (cred->cr_uid != ip->i_uid &&
297 		    (error = suser(cred, &u.u_acflag)))
298 			return (error);
299 		if (cred->cr_uid == 0) {
300 			ip->i_flags = vap->va_flags;
301 		} else {
302 			ip->i_flags &= 0xffff0000;
303 			ip->i_flags |= (vap->va_flags & 0xffff);
304 		}
305 		ip->i_flag |= ICHG;
306 	}
307 	return (error);
308 }
309 
310 /*
311  * Change the mode on a file.
312  * Inode must be locked before calling.
313  */
314 chmod1(vp, mode, cred)
315 	register struct vnode *vp;
316 	register int mode;
317 	struct ucred *cred;
318 {
319 	register struct inode *ip = VTOI(vp);
320 	int error;
321 
322 	if (cred->cr_uid != ip->i_uid &&
323 	    (error = suser(cred, &u.u_acflag)))
324 		return (error);
325 	ip->i_mode &= ~07777;
326 	if (cred->cr_uid) {
327 		if (vp->v_type != VDIR)
328 			mode &= ~ISVTX;
329 		if (!groupmember(ip->i_gid, cred))
330 			mode &= ~ISGID;
331 	}
332 	ip->i_mode |= mode & 07777;
333 	ip->i_flag |= ICHG;
334 	if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
335 		xrele(vp);
336 	return (0);
337 }
338 
339 /*
340  * Perform chown operation on inode ip;
341  * inode must be locked prior to call.
342  */
343 chown1(vp, uid, gid, cred)
344 	register struct vnode *vp;
345 	uid_t uid;
346 	gid_t gid;
347 	struct ucred *cred;
348 {
349 	register struct inode *ip = VTOI(vp);
350 #ifdef QUOTA
351 	register long change;
352 #endif
353 	int error;
354 
355 	if (uid == (u_short)VNOVAL)
356 		uid = ip->i_uid;
357 	if (gid == (u_short)VNOVAL)
358 		gid = ip->i_gid;
359 	/*
360 	 * If we don't own the file, are trying to change the owner
361 	 * of the file, or are not a member of the target group,
362 	 * the caller must be superuser or the call fails.
363 	 */
364 	if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
365 	    !groupmember((gid_t)gid, cred)) &&
366 	    (error = suser(cred, &u.u_acflag)))
367 		return (error);
368 #ifdef QUOTA
369 	if (ip->i_uid == uid)		/* this just speeds things a little */
370 		change = 0;
371 	else
372 		change = ip->i_blocks;
373 	(void) chkdq(ip, -change, 1);
374 	(void) chkiq(ip->i_dev, ip, ip->i_uid, 1);
375 	dqrele(ip->i_dquot);
376 #endif
377 	ip->i_uid = uid;
378 	ip->i_gid = gid;
379 	ip->i_flag |= ICHG;
380 	if (cred->cr_ruid != 0)
381 		ip->i_mode &= ~(ISUID|ISGID);
382 #ifdef QUOTA
383 	ip->i_dquot = inoquota(ip);
384 	(void) chkdq(ip, change, 1);
385 	(void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1);
386 	return (u.u_error);		/* should == 0 ALWAYS !! */
387 #else
388 	return (0);
389 #endif
390 }
391 
392 /* ARGSUSED */
393 ufs_ioctl(vp, com, data, fflag, cred)
394 	struct vnode *vp;
395 	int com;
396 	caddr_t data;
397 	int fflag;
398 	struct ucred *cred;
399 {
400 
401 	printf("ufs_ioctl called with type %d\n", vp->v_type);
402 	return (ENOTTY);
403 }
404 
405 /* ARGSUSED */
406 ufs_select(vp, which, cred)
407 	struct vnode *vp;
408 	int which;
409 	struct ucred *cred;
410 {
411 
412 	printf("ufs_select called with type %d\n", vp->v_type);
413 	return (1);		/* XXX */
414 }
415 
416 /*
417  * Mmap a file
418  *
419  * NB Currently unsupported.
420  */
421 /* ARGSUSED */
422 ufs_mmap(vp, fflags, cred)
423 	struct vnode *vp;
424 	int fflags;
425 	struct ucred *cred;
426 {
427 
428 	return (EINVAL);
429 }
430 
431 /*
432  * Synch an open file.
433  */
434 /* ARGSUSED */
435 ufs_fsync(vp, fflags, cred)
436 	struct vnode *vp;
437 	int fflags;
438 	struct ucred *cred;
439 {
440 	register struct inode *ip = VTOI(vp);
441 	int error;
442 
443 	ILOCK(ip);
444 	if (fflags&FWRITE)
445 		ip->i_flag |= ICHG;
446 	error = syncip(ip);
447 	IUNLOCK(ip);
448 	return (error);
449 }
450 
451 /*
452  * Seek on a file
453  *
454  * Nothing to do, so just return.
455  */
456 /* ARGSUSED */
457 ufs_seek(vp, oldoff, newoff, cred)
458 	struct vnode *vp;
459 	off_t oldoff, newoff;
460 	struct ucred *cred;
461 {
462 
463 	return (0);
464 }
465 
466 /*
467  * ufs remove
468  * Hard to avoid races here, especially
469  * in unlinking directories.
470  */
471 ufs_remove(ndp)
472 	struct nameidata *ndp;
473 {
474 	register struct inode *ip, *dp;
475 	int error;
476 
477 	ip = VTOI(ndp->ni_vp);
478 	dp = VTOI(ndp->ni_dvp);
479 	error = dirremove(ndp);
480 	if (!error) {
481 		ip->i_nlink--;
482 		ip->i_flag |= ICHG;
483 	}
484 	if (dp == ip)
485 		vrele(ITOV(ip));
486 	else
487 		iput(ip);
488 	iput(dp);
489 	return (error);
490 }
491 
492 /*
493  * link vnode call
494  */
495 ufs_link(vp, ndp)
496 	register struct vnode *vp;
497 	register struct nameidata *ndp;
498 {
499 	register struct inode *ip = VTOI(vp);
500 	int error;
501 
502 	if (ndp->ni_dvp != vp)
503 		ILOCK(ip);
504 	if (ip->i_nlink == LINK_MAX - 1) {
505 		error = EMLINK;
506 		goto out;
507 	}
508 	ip->i_nlink++;
509 	ip->i_flag |= ICHG;
510 	error = iupdat(ip, &time, &time, 1);
511 	if (!error)
512 		error = direnter(ip, ndp);
513 out:
514 	if (ndp->ni_dvp != vp)
515 		IUNLOCK(ip);
516 	if (error) {
517 		ip->i_nlink--;
518 		ip->i_flag |= ICHG;
519 	}
520 	return (error);
521 }
522 
523 /*
524  * Rename system call.
525  * 	rename("foo", "bar");
526  * is essentially
527  *	unlink("bar");
528  *	link("foo", "bar");
529  *	unlink("foo");
530  * but ``atomically''.  Can't do full commit without saving state in the
531  * inode on disk which isn't feasible at this time.  Best we can do is
532  * always guarantee the target exists.
533  *
534  * Basic algorithm is:
535  *
536  * 1) Bump link count on source while we're linking it to the
537  *    target.  This also ensure the inode won't be deleted out
538  *    from underneath us while we work (it may be truncated by
539  *    a concurrent `trunc' or `open' for creation).
540  * 2) Link source to destination.  If destination already exists,
541  *    delete it first.
542  * 3) Unlink source reference to inode if still around. If a
543  *    directory was moved and the parent of the destination
544  *    is different from the source, patch the ".." entry in the
545  *    directory.
546  */
547 ufs_rename(fndp, tndp)
548 	register struct nameidata *fndp, *tndp;
549 {
550 	register struct inode *ip, *xp, *dp;
551 	struct dirtemplate dirbuf;
552 	int doingdirectory = 0, oldparent = 0, newparent = 0;
553 	int error = 0;
554 
555 	dp = VTOI(fndp->ni_dvp);
556 	ip = VTOI(fndp->ni_vp);
557 	ILOCK(ip);
558 	if ((ip->i_mode&IFMT) == IFDIR) {
559 		register struct direct *d = &fndp->ni_dent;
560 
561 		/*
562 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
563 		 */
564 		if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip ||
565 		    fndp->ni_isdotdot || (ip->i_flag & IRENAME)) {
566 			IUNLOCK(ip);
567 			ufs_abortop(fndp);
568 			ufs_abortop(tndp);
569 			return (EINVAL);
570 		}
571 		ip->i_flag |= IRENAME;
572 		oldparent = dp->i_number;
573 		doingdirectory++;
574 	}
575 	vrele(fndp->ni_dvp);
576 
577 	/*
578 	 * 1) Bump link count while we're moving stuff
579 	 *    around.  If we crash somewhere before
580 	 *    completing our work, the link count
581 	 *    may be wrong, but correctable.
582 	 */
583 	ip->i_nlink++;
584 	ip->i_flag |= ICHG;
585 	error = iupdat(ip, &time, &time, 1);
586 	IUNLOCK(ip);
587 
588 	/*
589 	 * When the target exists, both the directory
590 	 * and target vnodes are returned locked.
591 	 */
592 	dp = VTOI(tndp->ni_dvp);
593 	xp = NULL;
594 	if (tndp->ni_vp)
595 		xp = VTOI(tndp->ni_vp);
596 	/*
597 	 * If ".." must be changed (ie the directory gets a new
598 	 * parent) then the source directory must not be in the
599 	 * directory heirarchy above the target, as this would
600 	 * orphan everything below the source directory. Also
601 	 * the user must have write permission in the source so
602 	 * as to be able to change "..". We must repeat the call
603 	 * to namei, as the parent directory is unlocked by the
604 	 * call to checkpath().
605 	 */
606 	if (oldparent != dp->i_number)
607 		newparent = dp->i_number;
608 	if (doingdirectory && newparent) {
609 		if (error = iaccess(ip, IWRITE, tndp->ni_cred))
610 			goto bad;
611 		tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
612 		do {
613 			dp = VTOI(tndp->ni_dvp);
614 			if (xp != NULL)
615 				iput(xp);
616 			if (error = checkpath(ip, dp, tndp->ni_cred))
617 				goto out;
618 			if (error = namei(tndp))
619 				goto out;
620 			xp = NULL;
621 			if (tndp->ni_vp)
622 				xp = VTOI(tndp->ni_vp);
623 		} while (dp != VTOI(tndp->ni_dvp));
624 	}
625 	/*
626 	 * 2) If target doesn't exist, link the target
627 	 *    to the source and unlink the source.
628 	 *    Otherwise, rewrite the target directory
629 	 *    entry to reference the source inode and
630 	 *    expunge the original entry's existence.
631 	 */
632 	if (xp == NULL) {
633 		if (dp->i_dev != ip->i_dev)
634 			panic("rename: EXDEV");
635 		/*
636 		 * Account for ".." in new directory.
637 		 * When source and destination have the same
638 		 * parent we don't fool with the link count.
639 		 */
640 		if (doingdirectory && newparent) {
641 			dp->i_nlink++;
642 			dp->i_flag |= ICHG;
643 			error = iupdat(dp, &time, &time, 1);
644 		}
645 		if (error = direnter(ip, tndp))
646 			goto out;
647 	} else {
648 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
649 			panic("rename: EXDEV");
650 		/*
651 		 * Short circuit rename(foo, foo).
652 		 */
653 		if (xp->i_number == ip->i_number)
654 			panic("rename: same file");
655 		/*
656 		 * If the parent directory is "sticky", then the user must
657 		 * own the parent directory, or the destination of the rename,
658 		 * otherwise the destination may not be changed (except by
659 		 * root). This implements append-only directories.
660 		 */
661 		if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 &&
662 		    tndp->ni_cred->cr_uid != dp->i_uid &&
663 		    xp->i_uid != tndp->ni_cred->cr_uid) {
664 			error = EPERM;
665 			goto bad;
666 		}
667 		/*
668 		 * Target must be empty if a directory
669 		 * and have no links to it.
670 		 * Also, insure source and target are
671 		 * compatible (both directories, or both
672 		 * not directories).
673 		 */
674 		if ((xp->i_mode&IFMT) == IFDIR) {
675 			if (!dirempty(xp, dp->i_number, tndp->ni_cred) ||
676 			    xp->i_nlink > 2) {
677 				error = ENOTEMPTY;
678 				goto bad;
679 			}
680 			if (!doingdirectory) {
681 				error = ENOTDIR;
682 				goto bad;
683 			}
684 			cache_purge(ITOV(dp));
685 		} else if (doingdirectory) {
686 			error = EISDIR;
687 			goto bad;
688 		}
689 		if (error = dirrewrite(dp, ip, tndp))
690 			goto bad;
691 		vput(ITOV(dp));
692 		/*
693 		 * Adjust the link count of the target to
694 		 * reflect the dirrewrite above.  If this is
695 		 * a directory it is empty and there are
696 		 * no links to it, so we can squash the inode and
697 		 * any space associated with it.  We disallowed
698 		 * renaming over top of a directory with links to
699 		 * it above, as the remaining link would point to
700 		 * a directory without "." or ".." entries.
701 		 */
702 		xp->i_nlink--;
703 		if (doingdirectory) {
704 			if (--xp->i_nlink != 0)
705 				panic("rename: linked directory");
706 			error = itrunc(xp, (u_long)0);
707 		}
708 		xp->i_flag |= ICHG;
709 		iput(xp);
710 		xp = NULL;
711 	}
712 
713 	/*
714 	 * 3) Unlink the source.
715 	 */
716 	fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
717 	(void)namei(fndp);
718 	if (fndp->ni_vp != NULL) {
719 		xp = VTOI(fndp->ni_vp);
720 		dp = VTOI(fndp->ni_dvp);
721 	} else {
722 		if (fndp->ni_dvp != NULL)
723 			vput(fndp->ni_dvp);
724 		xp = NULL;
725 		dp = NULL;
726 	}
727 	/*
728 	 * Ensure that the directory entry still exists and has not
729 	 * changed while the new name has been entered. If the source is
730 	 * a file then the entry may have been unlinked or renamed. In
731 	 * either case there is no further work to be done. If the source
732 	 * is a directory then it cannot have been rmdir'ed; its link
733 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
734 	 * The IRENAME flag ensures that it cannot be moved by another
735 	 * rename.
736 	 */
737 	if (xp != ip) {
738 		if (doingdirectory)
739 			panic("rename: lost dir entry");
740 	} else {
741 		/*
742 		 * If the source is a directory with a
743 		 * new parent, the link count of the old
744 		 * parent directory must be decremented
745 		 * and ".." set to point to the new parent.
746 		 */
747 		if (doingdirectory && newparent) {
748 			dp->i_nlink--;
749 			dp->i_flag |= ICHG;
750 			error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf,
751 				sizeof (struct dirtemplate), (off_t)0,
752 				UIO_SYSSPACE, tndp->ni_cred, (int *)0);
753 			if (error == 0) {
754 				if (dirbuf.dotdot_namlen != 2 ||
755 				    dirbuf.dotdot_name[0] != '.' ||
756 				    dirbuf.dotdot_name[1] != '.') {
757 					printf("rename: mangled dir\n");
758 				} else {
759 					dirbuf.dotdot_ino = newparent;
760 					(void) rdwri(UIO_WRITE, xp,
761 					    (caddr_t)&dirbuf,
762 					    sizeof (struct dirtemplate),
763 					    (off_t)0, UIO_SYSSPACE,
764 					    tndp->ni_cred, (int *)0);
765 					cache_purge(ITOV(dp));
766 				}
767 			}
768 		}
769 		error = dirremove(fndp);
770 		if (!error) {
771 			xp->i_nlink--;
772 			xp->i_flag |= ICHG;
773 		}
774 		xp->i_flag &= ~IRENAME;
775 	}
776 	if (dp)
777 		vput(ITOV(dp));
778 	if (xp)
779 		vput(ITOV(xp));
780 	vrele(ITOV(ip));
781 	return (error);
782 
783 bad:
784 	if (xp)
785 		vput(ITOV(xp));
786 	vput(ITOV(dp));
787 out:
788 	ip->i_nlink--;
789 	ip->i_flag |= ICHG;
790 	vrele(ITOV(ip));
791 	return (error);
792 }
793 
794 /*
795  * A virgin directory (no blushing please).
796  */
797 struct dirtemplate mastertemplate = {
798 	0, 12, 1, ".",
799 	0, DIRBLKSIZ - 12, 2, ".."
800 };
801 
802 /*
803  * Mkdir system call
804  */
805 ufs_mkdir(ndp, vap)
806 	struct nameidata *ndp;
807 	struct vattr *vap;
808 {
809 	register struct inode *ip, *dp;
810 	struct inode *tip;
811 	struct vnode *dvp;
812 	struct dirtemplate dirtemplate;
813 	int error;
814 	int dmode;
815 
816 	dvp = ndp->ni_dvp;
817 	dp = VTOI(dvp);
818 	dmode = vap->va_mode&0777;
819 	dmode |= IFDIR;
820 	/*
821 	 * Must simulate part of maknode here
822 	 * in order to acquire the inode, but
823 	 * not have it entered in the parent
824 	 * directory.  The entry is made later
825 	 * after writing "." and ".." entries out.
826 	 */
827 	error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip);
828 	if (error) {
829 		iput(dp);
830 		return (error);
831 	}
832 	ip = tip;
833 #ifdef QUOTA
834 	if (ip->i_dquot != NODQUOT)
835 		panic("mkdir: dquot");
836 #endif
837 	ip->i_flag |= IACC|IUPD|ICHG;
838 	ip->i_mode = dmode;
839 	ITOV(ip)->v_type = VDIR;	/* Rest init'd in iget() */
840 	ip->i_nlink = 2;
841 	ip->i_uid = ndp->ni_cred->cr_uid;
842 	ip->i_gid = dp->i_gid;
843 #ifdef QUOTA
844 	ip->i_dquot = inoquota(ip);
845 #endif
846 	error = iupdat(ip, &time, &time, 1);
847 
848 	/*
849 	 * Bump link count in parent directory
850 	 * to reflect work done below.  Should
851 	 * be done before reference is created
852 	 * so reparation is possible if we crash.
853 	 */
854 	dp->i_nlink++;
855 	dp->i_flag |= ICHG;
856 	error = iupdat(dp, &time, &time, 1);
857 
858 	/*
859 	 * Initialize directory with "."
860 	 * and ".." from static template.
861 	 */
862 	dirtemplate = mastertemplate;
863 	dirtemplate.dot_ino = ip->i_number;
864 	dirtemplate.dotdot_ino = dp->i_number;
865 	error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate,
866 		sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
867 		ndp->ni_cred, (int *)0);
868 	if (error) {
869 		dp->i_nlink--;
870 		dp->i_flag |= ICHG;
871 		goto bad;
872 	}
873 	if (DIRBLKSIZ > dp->i_fs->fs_fsize)
874 		panic("mkdir: blksize");     /* XXX - should grow w/balloc() */
875 	else
876 		ip->i_size = DIRBLKSIZ;
877 	/*
878 	 * Directory all set up, now
879 	 * install the entry for it in
880 	 * the parent directory.
881 	 */
882 	error = direnter(ip, ndp);
883 	dp = NULL;
884 	if (error) {
885 		ndp->ni_nameiop = LOOKUP | NOCACHE;
886 		error = namei(ndp);
887 		if (!error) {
888 			dp = VTOI(ndp->ni_vp);
889 			dp->i_nlink--;
890 			dp->i_flag |= ICHG;
891 		}
892 	}
893 bad:
894 	/*
895 	 * No need to do an explicit itrunc here,
896 	 * vrele will do this for us because we set
897 	 * the link count to 0.
898 	 */
899 	if (error) {
900 		ip->i_nlink = 0;
901 		ip->i_flag |= ICHG;
902 		iput(ip);
903 	} else
904 		ndp->ni_vp = ITOV(ip);
905 	if (dp)
906 		iput(dp);
907 	return (error);
908 }
909 
910 /*
911  * Rmdir system call.
912  */
913 ufs_rmdir(ndp)
914 	register struct nameidata *ndp;
915 {
916 	register struct inode *ip, *dp;
917 	int error = 0;
918 
919 	ip = VTOI(ndp->ni_vp);
920 	dp = VTOI(ndp->ni_dvp);
921 	/*
922 	 * No rmdir "." please.
923 	 */
924 	if (dp == ip) {
925 		vrele(ITOV(dp));
926 		iput(ip);
927 		return (EINVAL);
928 	}
929 	/*
930 	 * Verify the directory is empty (and valid).
931 	 * (Rmdir ".." won't be valid since
932 	 *  ".." will contain a reference to
933 	 *  the current directory and thus be
934 	 *  non-empty.)
935 	 */
936 	if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
937 		error = ENOTEMPTY;
938 		goto out;
939 	}
940 	/*
941 	 * Delete reference to directory before purging
942 	 * inode.  If we crash in between, the directory
943 	 * will be reattached to lost+found,
944 	 */
945 	if (error = dirremove(ndp))
946 		goto out;
947 	dp->i_nlink--;
948 	dp->i_flag |= ICHG;
949 	cache_purge(ITOV(dp));
950 	iput(dp);
951 	ndp->ni_dvp = NULL;
952 	/*
953 	 * Truncate inode.  The only stuff left
954 	 * in the directory is "." and "..".  The
955 	 * "." reference is inconsequential since
956 	 * we're quashing it.  The ".." reference
957 	 * has already been adjusted above.  We've
958 	 * removed the "." reference and the reference
959 	 * in the parent directory, but there may be
960 	 * other hard links so decrement by 2 and
961 	 * worry about them later.
962 	 */
963 	ip->i_nlink -= 2;
964 	error = itrunc(ip, (u_long)0);
965 	cache_purge(ITOV(ip));
966 out:
967 	if (ndp->ni_dvp)
968 		iput(dp);
969 	iput(ip);
970 	return (error);
971 }
972 
973 /*
974  * symlink -- make a symbolic link
975  */
976 ufs_symlink(ndp, vap, target)
977 	struct nameidata *ndp;
978 	struct vattr *vap;
979 	char *target;
980 {
981 	struct inode *ip;
982 	int error;
983 
984 	error = maknode(IFLNK | vap->va_mode, ndp, &ip);
985 	if (error)
986 		return (error);
987 	error = rdwri(UIO_WRITE, ip, target, strlen(target), (off_t)0,
988 		UIO_SYSSPACE, ndp->ni_cred, (int *)0);
989 	iput(ip);
990 	return (error);
991 }
992 
993 /*
994  * Vnode op for read and write
995  */
996 ufs_readdir(vp, uio, offp, cred)
997 	struct vnode *vp;
998 	register struct uio *uio;
999 	off_t *offp;
1000 	struct ucred *cred;
1001 {
1002 	register struct inode *ip = VTOI(vp);
1003 	int count, error;
1004 
1005 	ILOCK(ip);
1006 	uio->uio_offset = *offp;
1007 	count = uio->uio_resid;
1008 	count &= ~(DIRBLKSIZ - 1);
1009 	if (vp->v_type != VDIR || uio->uio_iovcnt != 1 ||
1010 	    (count < DIRBLKSIZ) || (uio->uio_offset & (DIRBLKSIZ -1))) {
1011 		IUNLOCK(ip);
1012 		return (EINVAL);
1013 	}
1014 	uio->uio_resid = count;
1015 	uio->uio_iov->iov_len = count;
1016 	error = readip(ip, uio, cred);
1017 	*offp += count - uio->uio_resid;
1018 	IUNLOCK(ip);
1019 	return (error);
1020 }
1021 
1022 /*
1023  * Return target name of a symbolic link
1024  */
1025 ufs_readlink(vp, uiop, cred)
1026 	struct vnode *vp;
1027 	struct uio *uiop;
1028 	struct ucred *cred;
1029 {
1030 
1031 	return (readip(VTOI(vp), uiop, cred));
1032 }
1033 
1034 /*
1035  * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
1036  * done. Iff ni_vp/ni_dvp not null and locked, unlock.
1037  */
1038 ufs_abortop(ndp)
1039 	register struct nameidata *ndp;
1040 {
1041 	register struct inode *ip;
1042 
1043 	if (ndp->ni_vp) {
1044 		ip = VTOI(ndp->ni_vp);
1045 		if (ip->i_flag & ILOCKED)
1046 			IUNLOCK(ip);
1047 		vrele(ndp->ni_vp);
1048 	}
1049 	if (ndp->ni_dvp) {
1050 		ip = VTOI(ndp->ni_dvp);
1051 		if (ip->i_flag & ILOCKED)
1052 			IUNLOCK(ip);
1053 		vrele(ndp->ni_dvp);
1054 	}
1055 	return;
1056 }
1057 
1058 ufs_lock(vp)
1059 	struct vnode *vp;
1060 {
1061 	register struct inode *ip = VTOI(vp);
1062 
1063 	ILOCK(ip);
1064 	return (0);
1065 }
1066 
1067 ufs_unlock(vp)
1068 	struct vnode *vp;
1069 {
1070 	register struct inode *ip = VTOI(vp);
1071 
1072 	if (!(ip->i_flag & ILOCKED))
1073 		panic("ufs_unlock NOT LOCKED");
1074 	IUNLOCK(ip);
1075 	return (0);
1076 }
1077 
1078 /*
1079  * Get access to bmap
1080  */
1081 ufs_bmap(vp, bn, vpp, bnp)
1082 	struct vnode *vp;
1083 	daddr_t bn;
1084 	struct vnode **vpp;
1085 	daddr_t *bnp;
1086 {
1087 	struct inode *ip = VTOI(vp);
1088 
1089 	if (vpp != NULL)
1090 		*vpp = ip->i_devvp;
1091 	if (bnp == NULL)
1092 		return (0);
1093 	return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0));
1094 }
1095 
1096 /*
1097  * Just call the device strategy routine
1098  */
1099 ufs_strategy(bp)
1100 	register struct buf *bp;
1101 {
1102 	(*bdevsw[major(bp->b_dev)].d_strategy)(bp);
1103 	return (0);
1104 }
1105 
1106 /*
1107  * Make a new file.
1108  */
1109 maknode(mode, ndp, ipp)
1110 	int mode;
1111 	register struct nameidata *ndp;
1112 	struct inode **ipp;
1113 {
1114 	register struct inode *ip;
1115 	struct inode *tip;
1116 	register struct inode *pdir = VTOI(ndp->ni_dvp);
1117 	ino_t ipref;
1118 	int error;
1119 
1120 	*ipp = 0;
1121 	if ((mode & IFMT) == IFDIR)
1122 		ipref = dirpref(pdir->i_fs);
1123 	else
1124 		ipref = pdir->i_number;
1125 	error = ialloc(pdir, ipref, mode, &tip);
1126 	if (error) {
1127 		iput(pdir);
1128 		return (error);
1129 	}
1130 	ip = tip;
1131 #ifdef QUOTA
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;
1139 	ITOV(ip)->v_type = IFTOVT(mode);	/* Rest init'd in iget() */
1140 	ip->i_nlink = 1;
1141 	ip->i_uid = ndp->ni_cred->cr_uid;
1142 	ip->i_gid = pdir->i_gid;
1143 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) &&
1144 	    suser(ndp->ni_cred, NULL))
1145 		ip->i_mode &= ~ISGID;
1146 #ifdef QUOTA
1147 	ip->i_dquot = inoquota(ip);
1148 #endif
1149 
1150 	/*
1151 	 * Make sure inode goes to disk before directory entry.
1152 	 */
1153 	if ((error = iupdat(ip, &time, &time, 1)) ||
1154 	    (error = direnter(ip, ndp))) {
1155 		/*
1156 		 * Write error occurred trying to update the inode
1157 		 * or the directory so must deallocate the inode.
1158 		 */
1159 		ip->i_nlink = 0;
1160 		ip->i_flag |= ICHG;
1161 		iput(ip);
1162 		return (error);
1163 	}
1164 	*ipp = ip;
1165 	return (0);
1166 }
1167