xref: /dragonfly/sys/vfs/ufs/ufs_vnops.c (revision 75a74ed8)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)ufs_vnops.c	8.27 (Berkeley) 5/27/95
35  * $FreeBSD: src/sys/ufs/ufs/ufs_vnops.c,v 1.131.2.8 2003/01/02 17:26:19 bde Exp $
36  */
37 
38 #include "opt_quota.h"
39 #include "opt_suiddir.h"
40 #include "opt_ufs.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/fcntl.h>
46 #include <sys/stat.h>
47 #include <sys/buf.h>
48 #include <sys/proc.h>
49 #include <sys/priv.h>
50 #include <sys/namei.h>
51 #include <sys/mount.h>
52 #include <sys/unistd.h>
53 #include <sys/vnode.h>
54 #include <sys/malloc.h>
55 #include <sys/dirent.h>
56 #include <sys/lockf.h>
57 #include <sys/event.h>
58 #include <sys/conf.h>
59 
60 #include <sys/file.h>		/* XXX */
61 #include <sys/jail.h>
62 
63 #include <vm/vm.h>
64 #include <vm/vm_extern.h>
65 
66 #include <vfs/fifofs/fifo.h>
67 
68 #include "quota.h"
69 #include "inode.h"
70 #include "dir.h"
71 #include "ufsmount.h"
72 #include "ufs_extern.h"
73 #include "ffs_extern.h"
74 #include "fs.h"
75 #ifdef UFS_DIRHASH
76 #include "dirhash.h"
77 #endif
78 
79 static int ufs_access (struct vop_access_args *);
80 static int ufs_advlock (struct vop_advlock_args *);
81 static int ufs_chmod (struct vnode *, int, struct ucred *);
82 static int ufs_chown (struct vnode *, uid_t, gid_t, struct ucred *);
83 static int ufs_close (struct vop_close_args *);
84 static int ufs_create (struct vop_old_create_args *);
85 static int ufs_getattr (struct vop_getattr_args *);
86 static int ufs_link (struct vop_old_link_args *);
87 static int ufs_makeinode (int mode, struct vnode *, struct vnode **, struct componentname *);
88 static int ufs_markatime (struct vop_markatime_args *);
89 static int ufs_missingop (struct vop_generic_args *ap);
90 static int ufs_mkdir (struct vop_old_mkdir_args *);
91 static int ufs_mknod (struct vop_old_mknod_args *);
92 static int ufs_print (struct vop_print_args *);
93 static int ufs_readdir (struct vop_readdir_args *);
94 static int ufs_readlink (struct vop_readlink_args *);
95 static int ufs_remove (struct vop_old_remove_args *);
96 static int ufs_rename (struct vop_old_rename_args *);
97 static int ufs_rmdir (struct vop_old_rmdir_args *);
98 static int ufs_setattr (struct vop_setattr_args *);
99 static int ufs_strategy (struct vop_strategy_args *);
100 static int ufs_symlink (struct vop_old_symlink_args *);
101 static int ufs_whiteout (struct vop_old_whiteout_args *);
102 static int ufsfifo_close (struct vop_close_args *);
103 static int ufsfifo_kqfilter (struct vop_kqfilter_args *);
104 static int ufsfifo_read (struct vop_read_args *);
105 static int ufsfifo_write (struct vop_write_args *);
106 static int filt_ufsread (struct knote *kn, long hint);
107 static int filt_ufswrite (struct knote *kn, long hint);
108 static int filt_ufsvnode (struct knote *kn, long hint);
109 static void filt_ufsdetach (struct knote *kn);
110 static int ufs_kqfilter (struct vop_kqfilter_args *ap);
111 
112 union _qcvt {
113 	int64_t qcvt;
114 	int32_t val[2];
115 };
116 #define SETHIGH(q, h) { \
117 	union _qcvt tmp; \
118 	tmp.qcvt = (q); \
119 	tmp.val[_QUAD_HIGHWORD] = (h); \
120 	(q) = tmp.qcvt; \
121 }
122 #define SETLOW(q, l) { \
123 	union _qcvt tmp; \
124 	tmp.qcvt = (q); \
125 	tmp.val[_QUAD_LOWWORD] = (l); \
126 	(q) = tmp.qcvt; \
127 }
128 #define VN_KNOTE(vp, b) \
129 	KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, (b))
130 
131 #define OFSFMT(vp)		((vp)->v_mount->mnt_maxsymlinklen <= 0)
132 
133 /*
134  * A virgin directory (no blushing please).
135  */
136 static struct dirtemplate mastertemplate = {
137 	0, 12, DT_DIR, 1, ".",
138 	0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
139 };
140 static struct odirtemplate omastertemplate = {
141 	0, 12, 1, ".",
142 	0, DIRBLKSIZ - 12, 2, ".."
143 };
144 
145 void
146 ufs_itimes(struct vnode *vp)
147 {
148 	struct inode *ip;
149 	struct timespec ts;
150 
151 	ip = VTOI(vp);
152 	if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
153 		return;
154 	if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
155 		ip->i_flag |= IN_LAZYMOD;
156 	else
157 		ip->i_flag |= IN_MODIFIED;
158 
159 	if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
160 		vfs_timestamp(&ts);
161 		if (ip->i_flag & IN_ACCESS) {
162 			ip->i_atime = (uint32_t)ts.tv_sec;
163 			ip->i_atime_ext = ts.tv_sec >> 32;
164 			ip->i_atimensec = ts.tv_nsec;
165 		}
166 		if (ip->i_flag & IN_CHANGE) {
167 			ip->i_ctime = (uint32_t)ts.tv_sec;
168 			ip->i_ctime_ext = ts.tv_sec >> 32;
169 			ip->i_ctimensec = ts.tv_nsec;
170 		}
171 		if (ip->i_flag & IN_UPDATE) {
172 			if (ip->i_flag & IN_NOCOPYWRITE) {
173 				if (vp->v_flag & VLASTWRITETS) {
174 					ip->i_mtime = (uint32_t)
175 						vp->v_lastwrite_ts.tv_sec;
176 					ip->i_mtime_ext =
177 						vp->v_lastwrite_ts.tv_sec >> 32;
178 					ip->i_mtimensec =
179 						vp->v_lastwrite_ts.tv_nsec;
180 				}
181 			} else {
182 				ip->i_mtime = (uint32_t)ts.tv_sec;
183 				ip->i_mtime_ext = ts.tv_sec >> 32;
184 				ip->i_mtimensec = ts.tv_nsec;
185 			}
186 			ip->i_modrev++;
187 		}
188 	}
189 	ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
190 }
191 
192 /*
193  * Create a regular file
194  *
195  * ufs_create(struct vnode *a_dvp, struct vnode **a_vpp,
196  *	      struct componentname *a_cnp, struct vattr *a_vap)
197  */
198 static
199 int
200 ufs_create(struct vop_old_create_args *ap)
201 {
202 	int error;
203 
204 	error =
205 	    ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
206 	    ap->a_dvp, ap->a_vpp, ap->a_cnp);
207 	if (error)
208 		return (error);
209 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
210 	return (0);
211 }
212 
213 /*
214  * Mknod vnode call
215  *
216  * ufs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
217  *	     struct componentname *a_cnp, struct vattr *a_vap)
218  */
219 /* ARGSUSED */
220 static
221 int
222 ufs_mknod(struct vop_old_mknod_args *ap)
223 {
224 	struct vattr *vap = ap->a_vap;
225 	struct vnode **vpp = ap->a_vpp;
226 	struct inode *ip;
227 	ino_t ino;
228 	int error;
229 
230 	/*
231 	 * UFS cannot represent the entire major/minor range supported by
232 	 * the kernel.
233 	 */
234 	if (vap->va_rmajor != VNOVAL &&
235 	    makeudev(vap->va_rmajor, vap->va_rminor) == NOUDEV) {
236 		return(EINVAL);
237 	}
238 
239 	/* no special directory support */
240 	if (vap->va_type == VDIR)
241 		return(EINVAL);
242 
243 	error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
244 	    ap->a_dvp, vpp, ap->a_cnp);
245 	if (error)
246 		return (error);
247 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
248 	ip = VTOI(*vpp);
249 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
250 	if (vap->va_rmajor != VNOVAL) {
251 		/*
252 		 * Want to be able to use this to make badblock
253 		 * inodes, so don't truncate the dev number.
254 		 */
255 		ip->i_rdev = makeudev(vap->va_rmajor, vap->va_rminor);
256 	}
257 	/*
258 	 * Remove inode, then reload it through VFS_VGET so it is
259 	 * checked to see if it is an alias of an existing entry in
260 	 * the inode cache.
261 	 */
262 	(*vpp)->v_type = VNON;
263 	ino = ip->i_number;	/* Save this before vgone() invalidates ip. */
264 	vgone_vxlocked(*vpp);
265 	vput(*vpp);
266 	error = VFS_VGET(ap->a_dvp->v_mount, NULL, ino, vpp);
267 	if (error) {
268 		*vpp = NULL;
269 		return (error);
270 	}
271 	return (0);
272 }
273 
274 /*
275  * Close called.
276  *
277  * Update the times on the inode.
278  *
279  * ufs_close(struct vnode *a_vp, int a_fflag)
280  */
281 /* ARGSUSED */
282 static
283 int
284 ufs_close(struct vop_close_args *ap)
285 {
286 	struct vnode *vp = ap->a_vp;
287 
288 	if (VREFCNT(vp) > 1)
289 		ufs_itimes(vp);
290 	return (vop_stdclose(ap));
291 }
292 
293 /*
294  * ufs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
295  */
296 static
297 int
298 ufs_access(struct vop_access_args *ap)
299 {
300 	struct vnode *vp = ap->a_vp;
301 	struct inode *ip = VTOI(vp);
302 	int error;
303 
304 #ifdef QUOTA
305 	if (ap->a_mode & VWRITE) {
306 		switch (vp->v_type) {
307 		case VDIR:
308 		case VLNK:
309 		case VREG:
310 			if ((error = ufs_getinoquota(ip)) != 0)
311 				return (error);
312 			break;
313 		default:
314 			break;
315 		}
316 	}
317 #endif
318 
319 	error = vop_helper_access(ap, ip->i_uid, ip->i_gid, ip->i_mode, 0);
320 	return (error);
321 }
322 
323 /*
324  * ufs_getattr(struct vnode *a_vp, struct vattr *a_vap)
325  */
326 /* ARGSUSED */
327 static
328 int
329 ufs_getattr(struct vop_getattr_args *ap)
330 {
331 	struct vnode *vp = ap->a_vp;
332 	struct inode *ip = VTOI(vp);
333 	struct vattr *vap = ap->a_vap;
334 
335 	ufs_itimes(vp);
336 	/*
337 	 * Copy from inode table
338 	 */
339 	vap->va_fsid = dev2udev(ip->i_dev);
340 	vap->va_fileid = ip->i_number;
341 	vap->va_mode = ip->i_mode & ~IFMT;
342 	vap->va_nlink = VFSTOUFS(vp->v_mount)->um_i_effnlink_valid ?
343 	    ip->i_effnlink : ip->i_nlink;
344 	vap->va_uid = ip->i_uid;
345 	vap->va_gid = ip->i_gid;
346 	vap->va_rmajor = umajor(ip->i_rdev);
347 	vap->va_rminor = uminor(ip->i_rdev);
348 	vap->va_size = ip->i_din.di_size;
349 	vap->va_atime.tv_sec =
350 		(time_t)(ip->i_atime | ((uint64_t)ip->i_atime_ext << 32));
351 	vap->va_atime.tv_nsec = ip->i_atimensec;
352 	vap->va_mtime.tv_sec =
353 		(time_t)(ip->i_mtime | ((uint64_t)ip->i_mtime_ext << 32));
354 	vap->va_mtime.tv_nsec = ip->i_mtimensec;
355 	vap->va_ctime.tv_sec =
356 		(time_t)(ip->i_ctime | ((uint64_t)ip->i_ctime_ext << 32));
357 	vap->va_ctime.tv_nsec = ip->i_ctimensec;
358 	vap->va_flags = ip->i_flags;
359 	vap->va_gen = ip->i_gen;
360 	vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
361 	vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
362 	vap->va_type = IFTOVT(ip->i_mode);
363 	vap->va_filerev = ip->i_modrev;
364 	return (0);
365 }
366 
367 static
368 int
369 ufs_markatime(struct vop_markatime_args *ap)
370 {
371 	struct vnode *vp = ap->a_vp;
372 	struct inode *ip = VTOI(vp);
373 
374 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
375 		return (EROFS);
376 	if (vp->v_mount->mnt_flag & MNT_NOATIME)
377 		return (0);
378 	ip->i_flag |= IN_ACCESS;
379 	VN_KNOTE(vp, NOTE_ATTRIB);
380 	return (0);
381 }
382 
383 /*
384  * Set attribute vnode op. called from several syscalls
385  *
386  * ufs_setattr(struct vnode *a_vp, struct vattr *a_vap,
387  *		struct ucred *a_cred)
388  */
389 static
390 int
391 ufs_setattr(struct vop_setattr_args *ap)
392 {
393 	struct vattr *vap = ap->a_vap;
394 	struct vnode *vp = ap->a_vp;
395 	struct inode *ip = VTOI(vp);
396 	struct ucred *cred = ap->a_cred;
397 	int error;
398 
399 	/*
400 	 * Check for unsettable attributes.
401 	 */
402 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
403 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
404 	    (vap->va_blocksize != VNOVAL) || (vap->va_rmajor != VNOVAL) ||
405 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
406 		return (EINVAL);
407 	}
408 	if (vap->va_flags != VNOVAL) {
409 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
410 			return (EROFS);
411 		if (cred->cr_uid != ip->i_uid &&
412 		    (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)))
413 			return (error);
414 		/*
415 		 * Note that a root chflags becomes a user chflags when
416 		 * we are jailed, unless the jail.chflags_allowed sysctl
417 		 * is set.
418 		 */
419 		if (cred->cr_uid == 0 &&
420 		    (!jailed(cred) || jail_chflags_allowed)) {
421 			if ((ip->i_flags
422 			    & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) &&
423 			    securelevel > 0)
424 				return (EPERM);
425 			ip->i_flags = vap->va_flags;
426 		} else {
427 			if (ip->i_flags
428 			    & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
429 			    (vap->va_flags & UF_SETTABLE) != vap->va_flags)
430 				return (EPERM);
431 			ip->i_flags &= SF_SETTABLE;
432 			ip->i_flags |= (vap->va_flags & UF_SETTABLE);
433 		}
434 		ip->i_flag |= IN_CHANGE;
435 		if (vap->va_flags & (IMMUTABLE | APPEND))
436 			return (0);
437 	}
438 	if (ip->i_flags & (IMMUTABLE | APPEND))
439 		return (EPERM);
440 	/*
441 	 * Go through the fields and update iff not VNOVAL.
442 	 */
443 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
444 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
445 			return (EROFS);
446 		if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred)) != 0)
447 			return (error);
448 	}
449 	if (vap->va_size != VNOVAL) {
450 		/*
451 		 * Disallow write attempts on read-only filesystems;
452 		 * unless the file is a socket, fifo, or a block or
453 		 * character device resident on the filesystem.
454 		 */
455 		switch (vp->v_type) {
456 		case VDIR:
457 			return (EISDIR);
458 		case VLNK:
459 		case VREG:
460 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
461 				return (EROFS);
462 			break;
463 		default:
464 			break;
465 		}
466 		if ((error = ffs_truncate(vp, vap->va_size, 0, cred)) != 0)
467 			return (error);
468 	}
469 	ip = VTOI(vp);
470 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
471 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
472 			return (EROFS);
473 		if (cred->cr_uid != ip->i_uid &&
474 		    (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)) &&
475 		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
476 		    (error = VOP_EACCESS(vp, VWRITE, cred))))
477 			return (error);
478 		if (vap->va_atime.tv_sec != VNOVAL)
479 			ip->i_flag |= IN_ACCESS;
480 		if (vap->va_mtime.tv_sec != VNOVAL)
481 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
482 		ufs_itimes(vp);
483 		if (vap->va_atime.tv_sec != VNOVAL) {
484 			ip->i_atime = (uint32_t)vap->va_atime.tv_sec;
485 			ip->i_atime_ext = vap->va_atime.tv_sec >> 32;
486 			ip->i_atimensec = vap->va_atime.tv_nsec;
487 		}
488 		if (vap->va_mtime.tv_sec != VNOVAL) {
489 			ip->i_mtime = (uint32_t)vap->va_mtime.tv_sec;
490 			ip->i_mtime_ext = vap->va_mtime.tv_sec >> 32;
491 			ip->i_mtimensec = vap->va_mtime.tv_nsec;
492 			vclrflags(vp, VLASTWRITETS);
493 		}
494 		error = ffs_update(vp, 0);
495 		if (error)
496 			return (error);
497 	}
498 	error = 0;
499 	if (vap->va_mode != (mode_t)VNOVAL) {
500 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
501 			return (EROFS);
502 		error = ufs_chmod(vp, (int)vap->va_mode, cred);
503 	}
504 	VN_KNOTE(vp, NOTE_ATTRIB);
505 	return (error);
506 }
507 
508 /*
509  * Change the mode on a file.
510  * Inode must be locked before calling.
511  */
512 static int
513 ufs_chmod(struct vnode *vp, int mode, struct ucred *cred)
514 {
515 	struct inode *ip = VTOI(vp);
516 	int error;
517 	mode_t	cur_mode = ip->i_mode;
518 
519 	error = vop_helper_chmod(vp, mode, cred, ip->i_uid, ip->i_gid,
520 				 &cur_mode);
521 	if (error)
522 		return (error);
523 #if 0
524 	if (cred->cr_uid != ip->i_uid) {
525 	    error = priv_check_cred(cred, PRIV_VFS_CHMOD, 0);
526 	    if (error)
527 		return (error);
528 	}
529 	if (cred->cr_uid) {
530 		if (vp->v_type != VDIR && (mode & S_ISTXT))
531 			return (EFTYPE);
532 		if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
533 			return (EPERM);
534 	}
535 #endif
536 	ip->i_mode = cur_mode;
537 	ip->i_flag |= IN_CHANGE;
538 	return (0);
539 }
540 
541 /*
542  * Perform chown operation on inode ip;
543  * inode must be locked prior to call.
544  */
545 static int
546 ufs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred)
547 {
548 	struct inode *ip = VTOI(vp);
549 	uid_t ouid;
550 	gid_t ogid;
551 	int error = 0;
552 #ifdef QUOTA
553 	int i;
554 	long change;
555 #endif
556 
557 	if (uid == (uid_t)VNOVAL)
558 		uid = ip->i_uid;
559 	if (gid == (gid_t)VNOVAL)
560 		gid = ip->i_gid;
561 	/*
562 	 * If we don't own the file, are trying to change the owner
563 	 * of the file, or are not a member of the target group,
564 	 * the caller must be superuser or the call fails.
565 	 */
566 	if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
567 	    (gid != ip->i_gid && !(cred->cr_gid == gid ||
568 	    groupmember(gid, cred)))) &&
569 	    (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
570 		return (error);
571 	ogid = ip->i_gid;
572 	ouid = ip->i_uid;
573 #ifdef QUOTA
574 	if ((error = ufs_getinoquota(ip)) != 0)
575 		return (error);
576 	if (ouid == uid) {
577 		ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
578 		ip->i_dquot[USRQUOTA] = NODQUOT;
579 	}
580 	if (ogid == gid) {
581 		ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
582 		ip->i_dquot[GRPQUOTA] = NODQUOT;
583 	}
584 	change = ip->i_blocks;
585 	(void) ufs_chkdq(ip, -change, cred, CHOWN);
586 	(void) ufs_chkiq(ip, -1, cred, CHOWN);
587 	for (i = 0; i < MAXQUOTAS; i++) {
588 		ufs_dqrele(vp, ip->i_dquot[i]);
589 		ip->i_dquot[i] = NODQUOT;
590 	}
591 #endif
592 	ip->i_gid = gid;
593 	ip->i_uid = uid;
594 #ifdef QUOTA
595 	if ((error = ufs_getinoquota(ip)) == 0) {
596 		if (ouid == uid) {
597 			ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
598 			ip->i_dquot[USRQUOTA] = NODQUOT;
599 		}
600 		if (ogid == gid) {
601 			ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
602 			ip->i_dquot[GRPQUOTA] = NODQUOT;
603 		}
604 		if ((error = ufs_chkdq(ip, change, cred, CHOWN)) == 0) {
605 			if ((error = ufs_chkiq(ip, 1, cred, CHOWN)) == 0)
606 				goto good;
607 			else
608 				(void)ufs_chkdq(ip, -change, cred, CHOWN|FORCE);
609 		}
610 		for (i = 0; i < MAXQUOTAS; i++) {
611 			ufs_dqrele(vp, ip->i_dquot[i]);
612 			ip->i_dquot[i] = NODQUOT;
613 		}
614 	}
615 	ip->i_gid = ogid;
616 	ip->i_uid = ouid;
617 	if (ufs_getinoquota(ip) == 0) {
618 		if (ouid == uid) {
619 			ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
620 			ip->i_dquot[USRQUOTA] = NODQUOT;
621 		}
622 		if (ogid == gid) {
623 			ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
624 			ip->i_dquot[GRPQUOTA] = NODQUOT;
625 		}
626 		(void) ufs_chkdq(ip, change, cred, FORCE|CHOWN);
627 		(void) ufs_chkiq(ip, 1, cred, FORCE|CHOWN);
628 		(void) ufs_getinoquota(ip);
629 	}
630 	return (error);
631 good:
632 	if (ufs_getinoquota(ip))
633 		panic("ufs_chown: lost quota");
634 #endif /* QUOTA */
635 	ip->i_flag |= IN_CHANGE;
636 	if (cred->cr_uid != 0 && (ouid != uid || ogid != gid))
637 		ip->i_mode &= ~(ISUID | ISGID);
638 	return (0);
639 }
640 
641 /*
642  * ufs_remove(struct vnode *a_dvp, struct vnode *a_vp,
643  *	      struct componentname *a_cnp)
644  */
645 static
646 int
647 ufs_remove(struct vop_old_remove_args *ap)
648 {
649 	struct inode *ip;
650 	struct vnode *vp = ap->a_vp;
651 	struct vnode *dvp = ap->a_dvp;
652 	int error;
653 
654 	ip = VTOI(vp);
655 #if 0	/* handled by kernel now */
656 	if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
657 	    (VTOI(dvp)->i_flags & APPEND)) {
658 		error = EPERM;
659 		goto out;
660 	}
661 #endif
662 	error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
663 	VN_KNOTE(vp, NOTE_DELETE);
664 	VN_KNOTE(dvp, NOTE_WRITE);
665 #if 0
666 out:
667 #endif
668 	return (error);
669 }
670 
671 /*
672  * link vnode call
673  *
674  * ufs_link(struct vnode *a_tdvp, struct vnode *a_vp,
675  *	    struct componentname *a_cnp)
676  */
677 static
678 int
679 ufs_link(struct vop_old_link_args *ap)
680 {
681 	struct vnode *vp = ap->a_vp;
682 	struct vnode *tdvp = ap->a_tdvp;
683 	struct componentname *cnp = ap->a_cnp;
684 	struct inode *ip;
685 	struct direct newdir;
686 	int error;
687 
688 	if (tdvp->v_mount != vp->v_mount) {
689 		error = EXDEV;
690 		goto out2;
691 	}
692 	if (tdvp != vp) {
693 		error = vn_lock(vp, LK_EXCLUSIVE | LK_FAILRECLAIM);
694 		if (error)
695 			goto out2;
696 	}
697 	ip = VTOI(vp);
698 	if ((nlink_t)ip->i_nlink >= LINK_MAX) {
699 		error = EMLINK;
700 		goto out1;
701 	}
702 #if 0	/* handled by kernel now, also DragonFly allows this */
703 	if (ip->i_flags & (IMMUTABLE | APPEND)) {
704 		error = EPERM;
705 		goto out1;
706 	}
707 #endif
708 	ip->i_effnlink++;
709 	ip->i_nlink++;
710 	ip->i_flag |= IN_CHANGE;
711 	if (DOINGSOFTDEP(vp))
712 		softdep_change_linkcnt(ip);
713 	error = ffs_update(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
714 	if (!error) {
715 		ufs_makedirentry(ip, cnp, &newdir);
716 		error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
717 	}
718 
719 	if (error) {
720 		ip->i_effnlink--;
721 		ip->i_nlink--;
722 		ip->i_flag |= IN_CHANGE;
723 		if (DOINGSOFTDEP(vp))
724 			softdep_change_linkcnt(ip);
725 	}
726 out1:
727 	if (tdvp != vp)
728 		vn_unlock(vp);
729 out2:
730 	VN_KNOTE(vp, NOTE_LINK);
731 	VN_KNOTE(tdvp, NOTE_WRITE);
732 	return (error);
733 }
734 
735 /*
736  * whiteout vnode call
737  *
738  * ufs_whiteout(struct vnode *a_dvp, struct componentname *a_cnp, int a_flags)
739  */
740 static
741 int
742 ufs_whiteout(struct vop_old_whiteout_args *ap)
743 {
744 	struct vnode *dvp = ap->a_dvp;
745 	struct componentname *cnp = ap->a_cnp;
746 	struct direct newdir;
747 	int error = 0;
748 
749 	switch (ap->a_flags) {
750 	case NAMEI_LOOKUP:
751 		/* 4.4 format directories support whiteout operations */
752 		if (dvp->v_mount->mnt_maxsymlinklen > 0)
753 			return (0);
754 		return (EOPNOTSUPP);
755 
756 	case NAMEI_CREATE:
757 		/* create a new directory whiteout */
758 #ifdef DIAGNOSTIC
759 		if (dvp->v_mount->mnt_maxsymlinklen <= 0)
760 			panic("ufs_whiteout: old format filesystem");
761 #endif
762 
763 		newdir.d_ino = UFS_WINO;
764 		newdir.d_namlen = cnp->cn_namelen;
765 		bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
766 		newdir.d_type = DT_WHT;
767 		error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
768 		break;
769 
770 	case NAMEI_DELETE:
771 		/* remove an existing directory whiteout */
772 #ifdef DIAGNOSTIC
773 		if (dvp->v_mount->mnt_maxsymlinklen <= 0)
774 			panic("ufs_whiteout: old format filesystem");
775 #endif
776 
777 		cnp->cn_flags &= ~CNP_DOWHITEOUT;
778 		error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
779 		break;
780 	default:
781 		panic("ufs_whiteout: unknown op");
782 	}
783 	return (error);
784 }
785 
786 /*
787  * Rename system call.
788  * 	rename("foo", "bar");
789  * is essentially
790  *	unlink("bar");
791  *	link("foo", "bar");
792  *	unlink("foo");
793  * but ``atomically''.  Can't do full commit without saving state in the
794  * inode on disk which isn't feasible at this time.  Best we can do is
795  * always guarantee the target exists.
796  *
797  * Basic algorithm is:
798  *
799  * 1) Bump link count on source while we're linking it to the
800  *    target.  This also ensure the inode won't be deleted out
801  *    from underneath us while we work (it may be truncated by
802  *    a concurrent `trunc' or `open' for creation).
803  * 2) Link source to destination.  If destination already exists,
804  *    delete it first.
805  * 3) Unlink source reference to inode if still around. If a
806  *    directory was moved and the parent of the destination
807  *    is different from the source, patch the ".." entry in the
808  *    directory.
809  *
810  * ufs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
811  *	      struct componentname *a_fcnp, struct vnode *a_tdvp,
812  *	      struct vnode *a_tvp, struct componentname *a_tcnp)
813  */
814 static
815 int
816 ufs_rename(struct vop_old_rename_args *ap)
817 {
818 	struct vnode *tvp = ap->a_tvp;
819 	struct vnode *tdvp = ap->a_tdvp;
820 	struct vnode *fvp = ap->a_fvp;
821 	struct vnode *fdvp = ap->a_fdvp;
822 	struct componentname *tcnp = ap->a_tcnp;
823 	struct componentname *fcnp = ap->a_fcnp;
824 	struct inode *ip, *xp, *dp;
825 	struct direct newdir;
826 	ino_t oldparent = 0, newparent = 0;
827 	int doingdirectory = 0;
828 	int error = 0, ioflag;
829 
830 	/*
831 	 * Check for cross-device rename.
832 	 */
833 	if ((fvp->v_mount != tdvp->v_mount) ||
834 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
835 		error = EXDEV;
836 abortit:
837 		if (tdvp == tvp)
838 			vrele(tdvp);
839 		else
840 			vput(tdvp);
841 		if (tvp)
842 			vput(tvp);
843 		vrele(fdvp);
844 		vrele(fvp);
845 		return (error);
846 	}
847 
848 #if 0	/* handled by kernel now */
849 	if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
850 	    (VTOI(tdvp)->i_flags & APPEND))) {
851 		error = EPERM;
852 		goto abortit;
853 	}
854 #endif
855 
856 	/*
857 	 * Renaming a file to itself has no effect.  The upper layers should
858 	 * not call us in that case.  Temporarily just warn if they do.
859 	 */
860 	if (fvp == tvp) {
861 		kprintf("ufs_rename: fvp == tvp (can't happen)\n");
862 		error = 0;
863 		goto abortit;
864 	}
865 
866 	error = vn_lock(fvp, LK_EXCLUSIVE | LK_FAILRECLAIM);
867 	if (error)
868 		goto abortit;
869 
870 	/*
871 	 * Note: now that fvp is locked we have to be sure to unlock it before
872 	 * using the 'abortit' target.
873 	 */
874 	dp = VTOI(fdvp);
875 	ip = VTOI(fvp);
876 	if (ip->i_nlink >= LINK_MAX) {
877 		vn_unlock(fvp);
878 		error = EMLINK;
879 		goto abortit;
880 	}
881 #if 0	/* handled by kernel now */
882 	if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
883 	    || (dp->i_flags & APPEND)) {
884 		vn_unlock(fvp);
885 		error = EPERM;
886 		goto abortit;
887 	}
888 #endif
889 	if ((ip->i_mode & IFMT) == IFDIR) {
890 		/*
891 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
892 		 */
893 		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
894 		    dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & CNP_ISDOTDOT ||
895 		    (ip->i_flag & IN_RENAME)) {
896 			vn_unlock(fvp);
897 			error = EINVAL;
898 			goto abortit;
899 		}
900 		ip->i_flag |= IN_RENAME;
901 		oldparent = dp->i_number;
902 		doingdirectory = 1;
903 	}
904 	VN_KNOTE(fdvp, NOTE_WRITE);		/* XXX right place? */
905 
906 	/*
907 	 * fvp still locked.  ip->i_flag has IN_RENAME set if doingdirectory.
908 	 * Cleanup fvp requirements so we can unlock it.
909 	 *
910 	 * tvp and tdvp are locked.  tvp may be NULL.  Now that dp and xp
911 	 * is setup we can use the 'bad' target if we unlock fvp.  We cannot
912 	 * use the abortit target anymore because of IN_RENAME.
913 	 */
914 	dp = VTOI(tdvp);
915 	if (tvp)
916 		xp = VTOI(tvp);
917 	else
918 		xp = NULL;
919 
920 	/*
921 	 * 1) Bump link count while we're moving stuff
922 	 *    around.  If we crash somewhere before
923 	 *    completing our work, the link count
924 	 *    may be wrong, but correctable.
925 	 */
926 	ip->i_effnlink++;
927 	ip->i_nlink++;
928 	ip->i_flag |= IN_CHANGE;
929 	if (DOINGSOFTDEP(fvp))
930 		softdep_change_linkcnt(ip);
931 	if ((error = ffs_update(fvp, !(DOINGSOFTDEP(fvp) |
932 				       DOINGASYNC(fvp)))) != 0) {
933 		vn_unlock(fvp);
934 		goto bad;
935 	}
936 
937 	/*
938 	 * If ".." must be changed (ie the directory gets a new
939 	 * parent) then the source directory must not be in the
940 	 * directory heirarchy above the target, as this would
941 	 * orphan everything below the source directory. Also
942 	 * the user must have write permission in the source so
943 	 * as to be able to change "..". We must repeat the call
944 	 * to namei, as the parent directory is unlocked by the
945 	 * call to checkpath().
946 	 */
947 	error = VOP_EACCESS(fvp, VWRITE, tcnp->cn_cred);
948 	vn_unlock(fvp);
949 
950 	/*
951 	 * We are now back to where we were in that fvp, fdvp are unlocked
952 	 * and tvp, tdvp are locked.  tvp may be NULL.  IN_RENAME may be
953 	 * set.  Only the bad target or, if we clean up tvp and tdvp, the
954 	 * out target, may be used.
955 	 */
956 	if (oldparent != dp->i_number)
957 		newparent = dp->i_number;
958 	if (doingdirectory && newparent) {
959 		if (error)	/* write access check above */
960 			goto bad;
961 
962 		/*
963 		 * Once we start messing with tvp and tdvp we cannot use the
964 		 * 'bad' target, only finish cleaning tdvp and tvp up and
965 		 * use the 'out' target.
966 		 *
967 		 * This cleans up tvp.
968 		 */
969 		if (xp != NULL) {
970 			vput(tvp);
971 			xp = NULL;
972 		}
973 
974 		/*
975 		 * This is a real mess. ufs_checkpath vput's the target
976 		 * directory so retain an extra ref and note that tdvp will
977 		 * lose its lock on return.  This leaves us with one good
978 		 * ref after ufs_checkpath returns.
979 		 */
980 		vref(tdvp);
981 		error = ufs_checkpath(ip, dp, tcnp->cn_cred);
982 		tcnp->cn_flags |= CNP_PDIRUNLOCK;
983 		if (error) {
984 			vrele(tdvp);
985 			goto out;
986 	        }
987 
988 		/*
989 		 * relookup no longer messes with tdvp's refs. tdvp must be
990 		 * unlocked on entry and will be locked on a successful
991 		 * return.
992 		 */
993 		error = relookup(tdvp, &tvp, tcnp);
994 		if (error) {
995 			if (tcnp->cn_flags & CNP_PDIRUNLOCK)
996 				vrele(tdvp);
997 			else
998 				vput(tdvp);
999 			goto out;
1000 		}
1001 		KKASSERT((tcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
1002 		dp = VTOI(tdvp);
1003 		if (tvp)
1004 			xp = VTOI(tvp);
1005 	}
1006 
1007 	/*
1008 	 * We are back to fvp, fdvp unlocked, tvp, tdvp locked.  tvp may
1009 	 * be NULL (xp will also be NULL in that case), and IN_RENAME will
1010 	 * be set if doingdirectory.  This means we can use the 'bad' target
1011 	 * again.
1012 	 */
1013 
1014 	/*
1015 	 * 2) If target doesn't exist, link the target
1016 	 *    to the source and unlink the source.
1017 	 *    Otherwise, rewrite the target directory
1018 	 *    entry to reference the source inode and
1019 	 *    expunge the original entry's existence.
1020 	 */
1021 	if (xp == NULL) {
1022 		if (dp->i_dev != ip->i_dev)
1023 			panic("ufs_rename: EXDEV");
1024 		/*
1025 		 * Account for ".." in new directory.
1026 		 * When source and destination have the same
1027 		 * parent we don't fool with the link count.
1028 		 */
1029 		if (doingdirectory && newparent) {
1030 			if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1031 				error = EMLINK;
1032 				goto bad;
1033 			}
1034 			dp->i_effnlink++;
1035 			dp->i_nlink++;
1036 			dp->i_flag |= IN_CHANGE;
1037 			if (DOINGSOFTDEP(tdvp))
1038 				softdep_change_linkcnt(dp);
1039 			error = ffs_update(tdvp, !(DOINGSOFTDEP(tdvp) |
1040 						   DOINGASYNC(tdvp)));
1041 			if (error)
1042 				goto bad;
1043 		}
1044 		ufs_makedirentry(ip, tcnp, &newdir);
1045 		error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1046 		if (error) {
1047 			if (doingdirectory && newparent) {
1048 				dp->i_effnlink--;
1049 				dp->i_nlink--;
1050 				dp->i_flag |= IN_CHANGE;
1051 				if (DOINGSOFTDEP(tdvp))
1052 					softdep_change_linkcnt(dp);
1053 				(void)ffs_update(tdvp, 1);
1054 			}
1055 			goto bad;
1056 		}
1057 		VN_KNOTE(tdvp, NOTE_WRITE);
1058 		vput(tdvp);
1059 	} else {
1060 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1061 			panic("ufs_rename: EXDEV");
1062 		/*
1063 		 * Short circuit rename(foo, foo).
1064 		 */
1065 		if (xp->i_number == ip->i_number)
1066 			panic("ufs_rename: same file");
1067 		/*
1068 		 * If the parent directory is "sticky", then the user must
1069 		 * own the parent directory, or the destination of the rename,
1070 		 * otherwise the destination may not be changed (except by
1071 		 * root). This implements append-only directories.
1072 		 */
1073 		if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
1074 		    tcnp->cn_cred->cr_uid != dp->i_uid &&
1075 		    xp->i_uid != tcnp->cn_cred->cr_uid) {
1076 			error = EPERM;
1077 			goto bad;
1078 		}
1079 		/*
1080 		 * Target must be empty if a directory and have no links
1081 		 * to it. Also, ensure source and target are compatible
1082 		 * (both directories, or both not directories).
1083 		 *
1084 		 * Purge the file or directory being replaced from the
1085 		 * nameccache.
1086 		 */
1087 		if ((xp->i_mode&IFMT) == IFDIR) {
1088 			if ((xp->i_effnlink > 2) ||
1089 			    !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1090 				error = ENOTEMPTY;
1091 				goto bad;
1092 			}
1093 			if (!doingdirectory) {
1094 				error = ENOTDIR;
1095 				goto bad;
1096 			}
1097 			/* cache_purge removed - handled by VFS compat layer */
1098 		} else if (doingdirectory == 0) {
1099 			/* cache_purge removed - handled by VFS compat layer */
1100 		} else {
1101 			error = EISDIR;
1102 			goto bad;
1103 		}
1104 		/*
1105 		 * note: inode passed to ufs_dirrewrite() is 0 for a
1106 		 * non-directory file rename, 1 for a directory rename
1107 		 * in the same directory, and > 1 for an inode representing
1108 		 * the new directory.
1109 		 */
1110 		error = ufs_dirrewrite(dp, xp, ip->i_number,
1111 		    IFTODT(ip->i_mode),
1112 		    (doingdirectory && newparent) ?
1113 			newparent : (ino_t)doingdirectory);
1114 		if (error)
1115 			goto bad;
1116 		if (doingdirectory) {
1117 			if (!newparent) {
1118 				dp->i_effnlink--;
1119 				if (DOINGSOFTDEP(tdvp))
1120 					softdep_change_linkcnt(dp);
1121 			}
1122 			xp->i_effnlink--;
1123 			if (DOINGSOFTDEP(tvp))
1124 				softdep_change_linkcnt(xp);
1125 		}
1126 		if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1127 			/*
1128 			 * Truncate inode. The only stuff left in the directory
1129 			 * is "." and "..". The "." reference is inconsequential
1130 			 * since we are quashing it. We have removed the "."
1131 			 * reference and the reference in the parent directory,
1132 			 * but there may be other hard links. The soft
1133 			 * dependency code will arrange to do these operations
1134 			 * after the parent directory entry has been deleted on
1135 			 * disk, so when running with that code we avoid doing
1136 			 * them now.
1137 			 */
1138 			if (!newparent) {
1139 				dp->i_nlink--;
1140 				dp->i_flag |= IN_CHANGE;
1141 			}
1142 			xp->i_nlink--;
1143 			xp->i_flag |= IN_CHANGE;
1144 			ioflag = DOINGASYNC(tvp) ? 0 : IO_SYNC;
1145 			error = ffs_truncate(tvp, (off_t)0, ioflag,
1146 					     tcnp->cn_cred);
1147 			if (error)
1148 				goto bad;
1149 		}
1150 		VN_KNOTE(tdvp, NOTE_WRITE);
1151 		vput(tdvp);
1152 		VN_KNOTE(tvp, NOTE_DELETE);
1153 		vput(tvp);
1154 		xp = NULL;
1155 	}
1156 
1157 	/*
1158 	 * tvp and tdvp have been cleaned up.  only fvp and fdvp (both
1159 	 * unlocked) remain.  We are about to overwrite fvp but we have to
1160 	 * keep 'ip' intact so we cannot release the old fvp, which is still
1161 	 * refd and accessible via ap->a_fvp.
1162 	 *
1163 	 * This means we cannot use either 'bad' or 'out' to cleanup any
1164 	 * more.
1165 	 */
1166 
1167 	/*
1168 	 * 3) Unlink the source.
1169 	 */
1170 	fcnp->cn_flags &= ~CNP_MODMASK;
1171 	fcnp->cn_flags |= CNP_LOCKPARENT;
1172 	error = relookup(fdvp, &fvp, fcnp);
1173 	if (error || fvp == NULL) {
1174 		/*
1175 		 * From name has disappeared.  IN_RENAME will not be set if
1176 		 * we get past the panic so we don't have to clean it up.
1177 		 */
1178 		if (doingdirectory)
1179 			panic("ufs_rename: lost dir entry");
1180 		vrele(ap->a_fvp);
1181 		if (fcnp->cn_flags & CNP_PDIRUNLOCK)
1182 			vrele(fdvp);
1183 		else
1184 			vput(fdvp);
1185 		return(0);
1186 	}
1187 	KKASSERT((fcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
1188 
1189 	/*
1190 	 * fdvp and fvp are locked.
1191 	 */
1192 	xp = VTOI(fvp);
1193 	dp = VTOI(fdvp);
1194 
1195 	/*
1196 	 * Ensure that the directory entry still exists and has not
1197 	 * changed while the new name has been entered. If the source is
1198 	 * a file then the entry may have been unlinked or renamed. In
1199 	 * either case there is no further work to be done. If the source
1200 	 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1201 	 * flag ensures that it cannot be moved by another rename or removed
1202 	 * by a rmdir.  Cleanup IN_RENAME.
1203 	 */
1204 	if (xp != ip) {
1205 		if (doingdirectory)
1206 			panic("ufs_rename: lost dir entry");
1207 	} else {
1208 		/*
1209 		 * If the source is a directory with a
1210 		 * new parent, the link count of the old
1211 		 * parent directory must be decremented
1212 		 * and ".." set to point to the new parent.
1213 		 */
1214 		if (doingdirectory && newparent) {
1215 			xp->i_offset = mastertemplate.dot_reclen;
1216 			ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1217 			/* cache_purge removed - handled by VFS compat layer */
1218 		}
1219 		error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1220 		xp->i_flag &= ~IN_RENAME;
1221 	}
1222 
1223 	VN_KNOTE(fvp, NOTE_RENAME);
1224 	vput(fdvp);
1225 	vput(fvp);
1226 	vrele(ap->a_fvp);
1227 	return (error);
1228 
1229 bad:
1230 	if (xp)
1231 		vput(ITOV(xp));
1232 	vput(ITOV(dp));
1233 out:
1234 	if (doingdirectory)
1235 		ip->i_flag &= ~IN_RENAME;
1236 	if (vn_lock(fvp, LK_EXCLUSIVE | LK_FAILRECLAIM) == 0) {
1237 		ip->i_effnlink--;
1238 		ip->i_nlink--;
1239 		ip->i_flag |= IN_CHANGE;
1240 		ip->i_flag &= ~IN_RENAME;
1241 		if (DOINGSOFTDEP(fvp))
1242 			softdep_change_linkcnt(ip);
1243 		vput(fvp);
1244 	} else {
1245 		vrele(fvp);
1246 	}
1247 	return (error);
1248 }
1249 
1250 /*
1251  * Mkdir system call
1252  *
1253  * ufs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1254  *	     struct componentname *a_cnp, struct vattr *a_vap)
1255  */
1256 static
1257 int
1258 ufs_mkdir(struct vop_old_mkdir_args *ap)
1259 {
1260 	struct vnode *dvp = ap->a_dvp;
1261 	struct vattr *vap = ap->a_vap;
1262 	struct componentname *cnp = ap->a_cnp;
1263 	struct inode *ip, *dp;
1264 	struct vnode *tvp;
1265 	struct buf *bp;
1266 	struct dirtemplate dirtemplate, *dtp;
1267 	struct direct newdir;
1268 	int error, dmode;
1269 	long blkoff;
1270 
1271 	dp = VTOI(dvp);
1272 	if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1273 		error = EMLINK;
1274 		goto out;
1275 	}
1276 	dmode = vap->va_mode & 0777;
1277 	dmode |= IFDIR;
1278 	/*
1279 	 * Must simulate part of ufs_makeinode here to acquire the inode,
1280 	 * but not have it entered in the parent directory. The entry is
1281 	 * made later after writing "." and ".." entries.
1282 	 */
1283 	error = ffs_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1284 	if (error)
1285 		goto out;
1286 	ip = VTOI(tvp);
1287 	ip->i_gid = dp->i_gid;
1288 #ifdef SUIDDIR
1289 	{
1290 #ifdef QUOTA
1291 		struct ucred ucred, *ucp;
1292 		ucp = cnp->cn_cred;
1293 #endif
1294 		/*
1295 		 * If we are hacking owners here, (only do this where told to)
1296 		 * and we are not giving it TO root, (would subvert quotas)
1297 		 * then go ahead and give it to the other user.
1298 		 * The new directory also inherits the SUID bit.
1299 		 * If user's UID and dir UID are the same,
1300 		 * 'give it away' so that the SUID is still forced on.
1301 		 */
1302 		if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1303 		    (dp->i_mode & ISUID) && dp->i_uid) {
1304 			dmode |= ISUID;
1305 			ip->i_uid = dp->i_uid;
1306 #ifdef QUOTA
1307 			if (dp->i_uid != cnp->cn_cred->cr_uid) {
1308 				/*
1309 				 * Make sure the correct user gets charged
1310 				 * for the space.
1311 				 * Make a dummy credential for the victim.
1312 				 * XXX This seems to never be accessed out of
1313 				 * our context so a stack variable is ok.
1314 				 */
1315 				ucred.cr_ref = 1;
1316 				ucred.cr_uid = ip->i_uid;
1317 				ucred.cr_ngroups = 1;
1318 				ucred.cr_groups[0] = dp->i_gid;
1319 				ucp = &ucred;
1320 			}
1321 #endif
1322 		} else
1323 			ip->i_uid = cnp->cn_cred->cr_uid;
1324 #ifdef QUOTA
1325 		if ((error = ufs_getinoquota(ip)) ||
1326 	    	    (error = ufs_chkiq(ip, 1, ucp, 0))) {
1327 			ffs_vfree(tvp, ip->i_number, dmode);
1328 			vput(tvp);
1329 			return (error);
1330 		}
1331 #endif
1332 	}
1333 #else	/* !SUIDDIR */
1334 	ip->i_uid = cnp->cn_cred->cr_uid;
1335 #ifdef QUOTA
1336 	if ((error = ufs_getinoquota(ip)) ||
1337 	    (error = ufs_chkiq(ip, 1, cnp->cn_cred, 0))) {
1338 		ffs_vfree(tvp, ip->i_number, dmode);
1339 		vput(tvp);
1340 		return (error);
1341 	}
1342 #endif
1343 #endif	/* !SUIDDIR */
1344 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1345 	ip->i_mode = dmode;
1346 	tvp->v_type = VDIR;	/* Rest init'd in getnewvnode(). */
1347 	ip->i_effnlink = 2;
1348 	ip->i_nlink = 2;
1349 	if (DOINGSOFTDEP(tvp))
1350 		softdep_change_linkcnt(ip);
1351 	if (cnp->cn_flags & CNP_ISWHITEOUT)
1352 		ip->i_flags |= UF_OPAQUE;
1353 
1354 	/*
1355 	 * Bump link count in parent directory to reflect work done below.
1356 	 * Should be done before reference is created so cleanup is
1357 	 * possible if we crash.
1358 	 */
1359 	dp->i_effnlink++;
1360 	dp->i_nlink++;
1361 	dp->i_flag |= IN_CHANGE;
1362 	if (DOINGSOFTDEP(dvp))
1363 		softdep_change_linkcnt(dp);
1364 	error = ffs_update(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1365 	if (error)
1366 		goto bad;
1367 
1368 	/*
1369 	 * The vnode must have a VM object in order to issue buffer cache
1370 	 * ops on it.
1371 	 */
1372 	vinitvmio(tvp, DIRBLKSIZ, DIRBLKSIZ, -1);
1373 
1374 	/*
1375 	 * Initialize directory with "." and ".." from static template.
1376 	 */
1377 	if (dvp->v_mount->mnt_maxsymlinklen > 0)
1378 		dtp = &mastertemplate;
1379 	else
1380 		dtp = (struct dirtemplate *)&omastertemplate;
1381 	dirtemplate = *dtp;
1382 	dirtemplate.dot_ino = ip->i_number;
1383 	dirtemplate.dotdot_ino = dp->i_number;
1384 	nvnode_pager_setsize(tvp, DIRBLKSIZ, DIRBLKSIZ, -1);
1385 	error = VOP_BALLOC(tvp, 0LL, DIRBLKSIZ, cnp->cn_cred, B_CLRBUF, &bp);
1386 	if (error)
1387 		goto bad;
1388 	ip->i_size = DIRBLKSIZ;
1389 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
1390 	bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1391 	if (DOINGSOFTDEP(tvp)) {
1392 		/*
1393 		 * Ensure that the entire newly allocated block is a
1394 		 * valid directory so that future growth within the
1395 		 * block does not have to ensure that the block is
1396 		 * written before the inode.
1397 		 */
1398 		blkoff = DIRBLKSIZ;
1399 		while (blkoff < bp->b_bcount) {
1400 			((struct direct *)
1401 			   (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1402 			blkoff += DIRBLKSIZ;
1403 		}
1404 	}
1405 	if ((error = ffs_update(tvp, !(DOINGSOFTDEP(tvp) |
1406 				       DOINGASYNC(tvp)))) != 0) {
1407 		bwrite(bp);
1408 		goto bad;
1409 	}
1410 	/*
1411 	 * Directory set up, now install its entry in the parent directory.
1412 	 *
1413 	 * If we are not doing soft dependencies, then we must write out the
1414 	 * buffer containing the new directory body before entering the new
1415 	 * name in the parent. If we are doing soft dependencies, then the
1416 	 * buffer containing the new directory body will be passed to and
1417 	 * released in the soft dependency code after the code has attached
1418 	 * an appropriate ordering dependency to the buffer which ensures that
1419 	 * the buffer is written before the new name is written in the parent.
1420 	 */
1421 	if (DOINGASYNC(dvp))
1422 		bdwrite(bp);
1423 	else if (!DOINGSOFTDEP(dvp) && (error = bwrite(bp)) != 0)
1424 		goto bad;
1425 	ufs_makedirentry(ip, cnp, &newdir);
1426 	error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1427 
1428 bad:
1429 	if (error == 0) {
1430 		VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1431 		*ap->a_vpp = tvp;
1432 	} else {
1433 		dp->i_effnlink--;
1434 		dp->i_nlink--;
1435 		dp->i_flag |= IN_CHANGE;
1436 		if (DOINGSOFTDEP(dvp))
1437 			softdep_change_linkcnt(dp);
1438 		/*
1439 		 * No need to do an explicit VOP_TRUNCATE here, vrele will
1440 		 * do this for us because we set the link count to 0.
1441 		 */
1442 		ip->i_effnlink = 0;
1443 		ip->i_nlink = 0;
1444 		ip->i_flag |= IN_CHANGE;
1445 		if (DOINGSOFTDEP(tvp))
1446 			softdep_change_linkcnt(ip);
1447 		vput(tvp);
1448 	}
1449 out:
1450 	return (error);
1451 }
1452 
1453 /*
1454  * Rmdir system call.
1455  *
1456  * ufs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1457  *	     struct componentname *a_cnp)
1458  */
1459 static
1460 int
1461 ufs_rmdir(struct vop_old_rmdir_args *ap)
1462 {
1463 	struct vnode *vp = ap->a_vp;
1464 	struct vnode *dvp = ap->a_dvp;
1465 	struct componentname *cnp = ap->a_cnp;
1466 	struct inode *ip, *dp;
1467 	int error, ioflag;
1468 
1469 	ip = VTOI(vp);
1470 	dp = VTOI(dvp);
1471 
1472 	/*
1473 	 * Do not remove a directory that is in the process of being renamed.
1474 	 * Verify the directory is empty (and valid). Rmdir ".." will not be
1475 	 * valid since ".." will contain a reference to the current directory
1476 	 * and thus be non-empty. Do not allow the removal of mounted on
1477 	 * directories (this can happen when an NFS exported filesystem
1478 	 * tries to remove a locally mounted on directory).
1479 	 */
1480 	error = 0;
1481 	if (ip->i_flag & IN_RENAME) {
1482 		error = EINVAL;
1483 		goto out;
1484 	}
1485 	if (ip->i_effnlink != 2 ||
1486 	    !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1487 		error = ENOTEMPTY;
1488 		goto out;
1489 	}
1490 #if 0	/* handled by kernel now */
1491 	if ((dp->i_flags & APPEND)
1492 	    || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1493 		error = EPERM;
1494 		goto out;
1495 	}
1496 #endif
1497 	/*
1498 	 * Delete reference to directory before purging
1499 	 * inode.  If we crash in between, the directory
1500 	 * will be reattached to lost+found,
1501 	 */
1502 	dp->i_effnlink--;
1503 	ip->i_effnlink--;
1504 	if (DOINGSOFTDEP(vp)) {
1505 		softdep_change_linkcnt(dp);
1506 		softdep_change_linkcnt(ip);
1507 	}
1508 	error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1509 	if (error) {
1510 		dp->i_effnlink++;
1511 		ip->i_effnlink++;
1512 		if (DOINGSOFTDEP(vp)) {
1513 			softdep_change_linkcnt(dp);
1514 			softdep_change_linkcnt(ip);
1515 		}
1516 		goto out;
1517 	}
1518 	VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1519 	/*
1520 	 * Truncate inode. The only stuff left in the directory is "." and
1521 	 * "..". The "." reference is inconsequential since we are quashing
1522 	 * it. The soft dependency code will arrange to do these operations
1523 	 * after the parent directory entry has been deleted on disk, so
1524 	 * when running with that code we avoid doing them now.
1525 	 */
1526 	if (!DOINGSOFTDEP(vp)) {
1527 		dp->i_nlink--;
1528 		dp->i_flag |= IN_CHANGE;
1529 		ip->i_nlink--;
1530 		ip->i_flag |= IN_CHANGE;
1531 		ioflag = DOINGASYNC(vp) ? 0 : IO_SYNC;
1532 		error = ffs_truncate(vp, (off_t)0, ioflag, cnp->cn_cred);
1533 	}
1534 	/* cache_purge removed - handled by VFS compat layer */
1535 #ifdef UFS_DIRHASH
1536 	/* Kill any active hash; i_effnlink == 0, so it will not come back. */
1537 	if (ip->i_dirhash != NULL)
1538 		ufsdirhash_free(ip);
1539 #endif
1540 out:
1541 	VN_KNOTE(vp, NOTE_DELETE);
1542 	return (error);
1543 }
1544 
1545 /*
1546  * symlink -- make a symbolic link
1547  *
1548  * ufs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1549  *		struct componentname *a_cnp, struct vattr *a_vap,
1550  *		char *a_target)
1551  */
1552 static
1553 int
1554 ufs_symlink(struct vop_old_symlink_args *ap)
1555 {
1556 	struct vnode *vp, **vpp = ap->a_vpp;
1557 	struct inode *ip;
1558 	int len, error;
1559 
1560 	error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1561 			      vpp, ap->a_cnp);
1562 	if (error)
1563 		return (error);
1564 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1565 	vp = *vpp;
1566 	len = strlen(ap->a_target);
1567 	if (len < vp->v_mount->mnt_maxsymlinklen) {
1568 		ip = VTOI(vp);
1569 		bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1570 		ip->i_size = len;
1571 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
1572 	} else {
1573 		/*
1574 		 * Make sure we have a VM object in order to use
1575 		 * the buffer cache.
1576 		 */
1577 		if (vp->v_object == NULL)
1578 			vinitvmio(vp, 0, PAGE_SIZE, -1);
1579 		error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1580 				UIO_SYSSPACE, IO_NODELOCKED,
1581 				ap->a_cnp->cn_cred, NULL);
1582 	}
1583 	if (error)
1584 		vput(vp);
1585 	return (error);
1586 }
1587 
1588 /*
1589  * Vnode op for reading directories.
1590  *
1591  * ufs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
1592  *		int *a_eofflag, int *ncookies, off_t **a_cookies)
1593  */
1594 static
1595 int
1596 ufs_readdir(struct vop_readdir_args *ap)
1597 {
1598 	struct uio *uio = ap->a_uio;
1599 	struct vnode *vp = ap->a_vp;
1600 	struct direct *dp;
1601 	struct buf *bp;
1602 	int retval;
1603 	int error;
1604 	int offset;	/* offset into buffer cache buffer */
1605 	int eoffset;	/* end of buffer clipped to file EOF */
1606 	int pickup;	/* pickup point */
1607 	int ncookies;
1608 	int cookie_index;
1609 	off_t *cookies;
1610 
1611 	if (uio->uio_offset < 0)
1612 		return (EINVAL);
1613 	/*
1614 	 * Guess the number of cookies needed.  Make sure we compute at
1615 	 * least 1, and no more then a reasonable limit.
1616 	 */
1617 	if (ap->a_ncookies) {
1618 		ncookies = uio->uio_resid / 16 + 1;
1619 		if (ncookies > 1024)
1620 			ncookies = 1024;
1621 		cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1622 	} else {
1623 		ncookies = -1;	/* force conditionals below */
1624 		cookies = NULL;
1625 	}
1626 	cookie_index = 0;
1627 
1628 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
1629 	if (error)
1630 		return (error);
1631 
1632 	/*
1633 	 * Past or at EOF
1634 	 */
1635 	if (uio->uio_offset >= VTOI(vp)->i_size) {
1636 		if (ap->a_eofflag)
1637 			*ap->a_eofflag = 1;
1638 		if (ap->a_ncookies) {
1639 			*ap->a_ncookies = cookie_index;
1640 			*ap->a_cookies = cookies;
1641 		}
1642 		goto done;
1643 	}
1644 
1645 	/*
1646 	 * Loop until we run out of cookies, we run out of user buffer,
1647 	 * or we hit the directory EOF.
1648 	 *
1649 	 * Always start scans at the beginning of the buffer, don't trust
1650 	 * the offset supplied by userland.
1651 	 */
1652 	while ((error = ffs_blkatoff_ra(vp, uio->uio_offset, NULL, &bp, 2)) == 0) {
1653 		pickup = (int)(uio->uio_offset - bp->b_loffset);
1654 		offset = 0;
1655 		retval = 0;
1656 		if (bp->b_loffset + bp->b_bcount > VTOI(vp)->i_size)
1657 			eoffset = (int)(VTOI(vp)->i_size - bp->b_loffset);
1658 		else
1659 			eoffset = bp->b_bcount;
1660 
1661 		while (offset < eoffset) {
1662 			dp = (struct direct *)(bp->b_data + offset);
1663 			if (dp->d_reclen <= 0 || (dp->d_reclen & 3) ||
1664 			    offset + dp->d_reclen > bp->b_bcount) {
1665 				error = EIO;
1666 				break;
1667 			}
1668 			if (offsetof(struct direct, d_name[dp->d_namlen]) >				     dp->d_reclen) {
1669 				error = EIO;
1670 				break;
1671 			}
1672 			if (offset < pickup) {
1673 				offset += dp->d_reclen;
1674 				continue;
1675 			}
1676 #if BYTE_ORDER == LITTLE_ENDIAN
1677 			if (OFSFMT(vp)) {
1678 				retval = vop_write_dirent(&error, uio,
1679 				    dp->d_ino, dp->d_namlen, dp->d_type,
1680 				    dp->d_name);
1681 			} else
1682 #endif
1683 			{
1684 				retval = vop_write_dirent(&error, uio,
1685 				    dp->d_ino, dp->d_type, dp->d_namlen,
1686 				    dp->d_name);
1687 			}
1688 			if (retval)
1689 				break;
1690 			if (cookies)
1691 				cookies[cookie_index] = bp->b_loffset + offset;
1692 			++cookie_index;
1693 			offset += dp->d_reclen;
1694 			if (cookie_index == ncookies)
1695 				break;
1696 		}
1697 
1698 		/*
1699 		 * This will align the next loop to the beginning of the
1700 		 * next block, and pickup will calculate to 0.
1701 		 */
1702 		uio->uio_offset = bp->b_loffset + offset;
1703 		brelse(bp);
1704 
1705 		if (retval || error || cookie_index == ncookies ||
1706 		    uio->uio_offset >= VTOI(vp)->i_size) {
1707 			break;
1708 		}
1709 	}
1710 	if (ap->a_eofflag)
1711 		*ap->a_eofflag = VTOI(vp)->i_size <= uio->uio_offset;
1712 
1713 	/*
1714 	 * Report errors only if we didn't manage to read anything
1715 	 */
1716 	if (error && cookie_index == 0) {
1717 		if (cookies) {
1718 			kfree(cookies, M_TEMP);
1719 			*ap->a_ncookies = 0;
1720 			*ap->a_cookies = NULL;
1721 		}
1722 	} else {
1723 		error = 0;
1724 		if (cookies) {
1725 			*ap->a_ncookies = cookie_index;
1726 			*ap->a_cookies = cookies;
1727 		}
1728 	}
1729 done:
1730 	vn_unlock(vp);
1731         return (error);
1732 }
1733 
1734 /*
1735  * Return target name of a symbolic link
1736  *
1737  * ufs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1738  */
1739 static
1740 int
1741 ufs_readlink(struct vop_readlink_args *ap)
1742 {
1743 	struct vnode *vp = ap->a_vp;
1744 	struct inode *ip = VTOI(vp);
1745 	int isize;
1746 
1747 	isize = ip->i_size;
1748 	if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1749 	    (ip->i_din.di_blocks == 0)) {   /* XXX - for old fastlink support */
1750 		uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1751 		return (0);
1752 	}
1753 
1754 	/*
1755 	 * Perform the equivalent of an OPEN on vp so we can issue a
1756 	 * VOP_READ.
1757 	 */
1758 	return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1759 }
1760 
1761 /*
1762  * Calculate the logical to physical mapping if not done already,
1763  * then call the device strategy routine.
1764  *
1765  * In order to be able to swap to a file, the VOP_BMAP operation may not
1766  * deadlock on memory.  See ufs_bmap() for details.
1767  *
1768  * ufs_strategy(struct vnode *a_vp, struct bio *a_bio)
1769  */
1770 static
1771 int
1772 ufs_strategy(struct vop_strategy_args *ap)
1773 {
1774 	struct bio *bio = ap->a_bio;
1775 	struct bio *nbio;
1776 	struct buf *bp = bio->bio_buf;
1777 	struct vnode *vp = ap->a_vp;
1778 	struct inode *ip;
1779 	int error;
1780 
1781 	ip = VTOI(vp);
1782 	if (vp->v_type == VBLK || vp->v_type == VCHR)
1783 		panic("ufs_strategy: spec");
1784 	nbio = push_bio(bio);
1785 	if (nbio->bio_offset == NOOFFSET) {
1786 		error = VOP_BMAP(vp, bio->bio_offset, &nbio->bio_offset,
1787 				 NULL, NULL, bp->b_cmd);
1788 		if (error) {
1789 			bp->b_error = error;
1790 			bp->b_flags |= B_ERROR;
1791 			/* I/O was never started on nbio, must biodone(bio) */
1792 			biodone(bio);
1793 			return (error);
1794 		}
1795 		if (nbio->bio_offset == NOOFFSET)
1796 			vfs_bio_clrbuf(bp);
1797 	}
1798 	if (nbio->bio_offset == NOOFFSET) {
1799 		/*
1800 		 * We hit a hole in the file.  The buffer has been zero-filled
1801 		 * so just biodone() it.
1802 		 */
1803 		biodone(bio);
1804 	} else {
1805 		vn_strategy(ip->i_devvp, nbio);
1806 	}
1807 	return (0);
1808 }
1809 
1810 /*
1811  * Print out the contents of an inode.
1812  *
1813  * ufs_print(struct vnode *a_vp)
1814  */
1815 static
1816 int
1817 ufs_print(struct vop_print_args *ap)
1818 {
1819 	struct vnode *vp = ap->a_vp;
1820 	struct inode *ip = VTOI(vp);
1821 
1822 	kprintf("tag VT_UFS, ino %lu, on dev %s (%d, %d)",
1823 	    (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev),
1824 	    minor(ip->i_dev));
1825 	if (vp->v_type == VFIFO)
1826 		fifo_printinfo(vp);
1827 	lockmgr_printinfo(&vp->v_lock);
1828 	kprintf("\n");
1829 	return (0);
1830 }
1831 
1832 /*
1833  * Read wrapper for fifos.
1834  *
1835  * ufsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1836  *		struct ucred *a_cred)
1837  */
1838 static
1839 int
1840 ufsfifo_read(struct vop_read_args *ap)
1841 {
1842 	int error, resid;
1843 	struct inode *ip;
1844 	struct uio *uio;
1845 
1846 	uio = ap->a_uio;
1847 	resid = uio->uio_resid;
1848 	error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1849 	ip = VTOI(ap->a_vp);
1850 	if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL &&
1851 	    (uio->uio_resid != resid || (error == 0 && resid != 0)))
1852 		VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1853 	return (error);
1854 }
1855 
1856 /*
1857  * Write wrapper for fifos.
1858  *
1859  * ufsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1860  *		 struct ucred *a_cred)
1861  */
1862 static
1863 int
1864 ufsfifo_write(struct vop_write_args *ap)
1865 {
1866 	int error, resid;
1867 	struct inode *ip;
1868 	struct uio *uio;
1869 
1870 	uio = ap->a_uio;
1871 	resid = uio->uio_resid;
1872 	error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1873 	ip = VTOI(ap->a_vp);
1874 	if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1875 		VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1876 	return (error);
1877 }
1878 
1879 /*
1880  * Close wrapper for fifos.
1881  *
1882  * Update the times on the inode then do device close.
1883  *
1884  * ufsfifo_close(struct vnode *a_vp, int a_fflag)
1885  */
1886 static
1887 int
1888 ufsfifo_close(struct vop_close_args *ap)
1889 {
1890 	struct vnode *vp = ap->a_vp;
1891 
1892 	if (VREFCNT(vp) > 1)
1893 		ufs_itimes(vp);
1894 	return (VOCALL(&fifo_vnode_vops, &ap->a_head));
1895 }
1896 
1897 /*
1898  * Kqfilter wrapper for fifos.
1899  *
1900  * Fall through to ufs kqfilter routines if needed
1901  */
1902 static
1903 int
1904 ufsfifo_kqfilter(struct vop_kqfilter_args *ap)
1905 {
1906 	int error;
1907 
1908 	error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1909 	if (error)
1910 		error = ufs_kqfilter(ap);
1911 	return (error);
1912 }
1913 
1914 /*
1915  * Advisory record locking support
1916  *
1917  * ufs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
1918  *	       int a_flags)
1919  */
1920 static
1921 int
1922 ufs_advlock(struct vop_advlock_args *ap)
1923 {
1924 	struct inode *ip = VTOI(ap->a_vp);
1925 
1926 	return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
1927 }
1928 
1929 /*
1930  * Initialize the vnode associated with a new inode, handle aliased
1931  * vnodes.
1932  *
1933  * Make sure directories have their VM object now rather then later,
1934  * saving us from having to check on all the myrid directory VOPs
1935  * that might be executed without a VOP_OPEN being performed.
1936  */
1937 int
1938 ufs_vinit(struct mount *mntp, struct vnode **vpp)
1939 {
1940 	struct inode *ip;
1941 	struct vnode *vp;
1942 	struct timeval tv;
1943 
1944 	vp = *vpp;
1945 	ip = VTOI(vp);
1946 
1947 	vp->v_type = IFTOVT(ip->i_mode);
1948 
1949 	switch(vp->v_type) {
1950 	case VCHR:
1951 	case VBLK:
1952 		vp->v_ops = &mntp->mnt_vn_spec_ops;
1953 		addaliasu(vp, umajor(ip->i_rdev), uminor(ip->i_rdev));
1954 		break;
1955 	case VFIFO:
1956 		vp->v_ops = &mntp->mnt_vn_fifo_ops;
1957 		break;
1958 	case VDIR:
1959 	case VREG:
1960 		vinitvmio(vp, ip->i_size,
1961 			  blkoffsize(ip->i_fs, ip, ip->i_size),
1962 			  blkoff(ip->i_fs, ip->i_size));
1963 		break;
1964 	case VLNK:
1965 		if (ip->i_size >= vp->v_mount->mnt_maxsymlinklen) {
1966 			vinitvmio(vp, ip->i_size,
1967 				  blkoffsize(ip->i_fs, ip, ip->i_size),
1968 				  blkoff(ip->i_fs, ip->i_size));
1969 		}
1970 		break;
1971 	default:
1972 		break;
1973 
1974 	}
1975 
1976 	if (ip->i_number == UFS_ROOTINO)
1977 		vsetflags(vp, VROOT);
1978 	/*
1979 	 * Initialize modrev times
1980 	 */
1981 	getmicrouptime(&tv);
1982 	SETHIGH(ip->i_modrev, tv.tv_sec);
1983 	SETLOW(ip->i_modrev, tv.tv_usec * 4294);
1984 	*vpp = vp;
1985 	return (0);
1986 }
1987 
1988 /*
1989  * Allocate a new inode.
1990  */
1991 static
1992 int
1993 ufs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
1994 	      struct componentname *cnp)
1995 {
1996 	struct inode *ip, *pdir;
1997 	struct direct newdir;
1998 	struct vnode *tvp;
1999 	int error;
2000 
2001 	pdir = VTOI(dvp);
2002 	*vpp = NULL;
2003 	if ((mode & IFMT) == 0)
2004 		mode |= IFREG;
2005 
2006 	error = ffs_valloc(dvp, mode, cnp->cn_cred, &tvp);
2007 	if (error)
2008 		return (error);
2009 	ip = VTOI(tvp);
2010 	ip->i_flags = pdir->i_flags & (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP);
2011 	ip->i_gid = pdir->i_gid;
2012 #ifdef SUIDDIR
2013 	{
2014 #ifdef QUOTA
2015 		struct ucred ucred, *ucp;
2016 		ucp = cnp->cn_cred;
2017 #endif
2018 		/*
2019 		 * If we are not the owner of the directory,
2020 		 * and we are hacking owners here, (only do this where told to)
2021 		 * and we are not giving it TO root, (would subvert quotas)
2022 		 * then go ahead and give it to the other user.
2023 		 * Note that this drops off the execute bits for security.
2024 		 */
2025 		if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2026 		    (pdir->i_mode & ISUID) &&
2027 		    (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2028 			ip->i_uid = pdir->i_uid;
2029 			mode &= ~07111;
2030 #ifdef QUOTA
2031 			/*
2032 			 * Make sure the correct user gets charged
2033 			 * for the space.
2034 			 * Quickly knock up a dummy credential for the victim.
2035 			 * XXX This seems to never be accessed out of our
2036 			 * context so a stack variable is ok.
2037 			 */
2038 			ucred.cr_ref = 1;
2039 			ucred.cr_uid = ip->i_uid;
2040 			ucred.cr_ngroups = 1;
2041 			ucred.cr_groups[0] = pdir->i_gid;
2042 			ucp = &ucred;
2043 #endif
2044 		} else
2045 			ip->i_uid = cnp->cn_cred->cr_uid;
2046 
2047 #ifdef QUOTA
2048 		if ((error = ufs_getinoquota(ip)) ||
2049 	    	    (error = ufs_chkiq(ip, 1, ucp, 0))) {
2050 			ffs_vfree(tvp, ip->i_number, mode);
2051 			vput(tvp);
2052 			return (error);
2053 		}
2054 #endif
2055 	}
2056 #else	/* !SUIDDIR */
2057 	ip->i_uid = cnp->cn_cred->cr_uid;
2058 #ifdef QUOTA
2059 	if ((error = ufs_getinoquota(ip)) ||
2060 	    (error = ufs_chkiq(ip, 1, cnp->cn_cred, 0))) {
2061 		ffs_vfree(tvp, ip->i_number, mode);
2062 		vput(tvp);
2063 		return (error);
2064 	}
2065 #endif
2066 #endif	/* !SUIDDIR */
2067 	ip->i_din.di_spare7E = 0;
2068 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2069 	ip->i_mode = mode;
2070 	tvp->v_type = IFTOVT(mode);	/* Rest init'd in getnewvnode(). */
2071 	ip->i_effnlink = 1;
2072 	ip->i_nlink = 1;
2073 	if (DOINGSOFTDEP(tvp))
2074 		softdep_change_linkcnt(ip);
2075 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2076 	    priv_check_cred(cnp->cn_cred, PRIV_VFS_SETGID, 0)) {
2077 		ip->i_mode &= ~ISGID;
2078 	}
2079 
2080 	if (cnp->cn_flags & CNP_ISWHITEOUT)
2081 		ip->i_flags |= UF_OPAQUE;
2082 
2083 	/*
2084 	 * Regular files and directories need VM objects.  Softlinks do
2085 	 * not (not immediately anyway).
2086 	 */
2087 	if (tvp->v_type == VREG || tvp->v_type == VDIR)
2088 		vinitvmio(tvp, 0, PAGE_SIZE, -1);
2089 
2090 	/*
2091 	 * Make sure inode goes to disk before directory entry.
2092 	 */
2093 	error = ffs_update(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2094 	if (error)
2095 		goto bad;
2096 	ufs_makedirentry(ip, cnp, &newdir);
2097 	error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2098 	if (error)
2099 		goto bad;
2100 	*vpp = tvp;
2101 	return (0);
2102 
2103 bad:
2104 	/*
2105 	 * Write error occurred trying to update the inode
2106 	 * or the directory so must deallocate the inode.
2107 	 */
2108 	ip->i_effnlink = 0;
2109 	ip->i_nlink = 0;
2110 	ip->i_flag |= IN_CHANGE;
2111 	if (DOINGSOFTDEP(tvp))
2112 		softdep_change_linkcnt(ip);
2113 	vput(tvp);
2114 	return (error);
2115 }
2116 
2117 static int
2118 ufs_missingop(struct vop_generic_args *ap)
2119 {
2120 	panic("no vop function for %s in ufs child", ap->a_desc->sd_name);
2121 	return (EOPNOTSUPP);
2122 }
2123 
2124 static struct filterops ufsread_filtops =
2125 	{ FILTEROP_ISFD, NULL, filt_ufsdetach, filt_ufsread };
2126 static struct filterops ufswrite_filtops =
2127 	{ FILTEROP_ISFD, NULL, filt_ufsdetach, filt_ufswrite };
2128 static struct filterops ufsvnode_filtops =
2129 	{ FILTEROP_ISFD, NULL, filt_ufsdetach, filt_ufsvnode };
2130 
2131 /*
2132  * ufs_kqfilter(struct vnode *a_vp, struct knote *a_kn)
2133  */
2134 static int
2135 ufs_kqfilter(struct vop_kqfilter_args *ap)
2136 {
2137 	struct vnode *vp = ap->a_vp;
2138 	struct knote *kn = ap->a_kn;
2139 
2140 	switch (kn->kn_filter) {
2141 	case EVFILT_READ:
2142 		kn->kn_fop = &ufsread_filtops;
2143 		break;
2144 	case EVFILT_WRITE:
2145 		kn->kn_fop = &ufswrite_filtops;
2146 		break;
2147 	case EVFILT_VNODE:
2148 		kn->kn_fop = &ufsvnode_filtops;
2149 		break;
2150 	default:
2151 		return (EOPNOTSUPP);
2152 	}
2153 
2154 	kn->kn_hook = (caddr_t)vp;
2155 
2156 	/* XXX: kq token actually protects the list */
2157 	lwkt_gettoken(&vp->v_token);
2158 	knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2159 	lwkt_reltoken(&vp->v_token);
2160 
2161 	return (0);
2162 }
2163 
2164 static void
2165 filt_ufsdetach(struct knote *kn)
2166 {
2167 	struct vnode *vp = (struct vnode *)kn->kn_hook;
2168 
2169 	lwkt_gettoken(&vp->v_token);
2170 	knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2171 	lwkt_reltoken(&vp->v_token);
2172 }
2173 
2174 /*ARGSUSED*/
2175 static int
2176 filt_ufsread(struct knote *kn, long hint)
2177 {
2178 	struct vnode *vp = (struct vnode *)kn->kn_hook;
2179 	struct inode *ip = VTOI(vp);
2180 	off_t off;
2181 
2182 	/*
2183 	 * filesystem is gone, so set the EOF flag and schedule
2184 	 * the knote for deletion.
2185 	 */
2186 	if (hint == NOTE_REVOKE) {
2187 		kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2188 		return (1);
2189 	}
2190 
2191 	off = ip->i_size - kn->kn_fp->f_offset;
2192 	kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2193 	if (kn->kn_sfflags & NOTE_OLDAPI)
2194 		return(1);
2195         return (kn->kn_data != 0);
2196 }
2197 
2198 /*ARGSUSED*/
2199 static int
2200 filt_ufswrite(struct knote *kn, long hint)
2201 {
2202 	/*
2203 	 * filesystem is gone, so set the EOF flag and schedule
2204 	 * the knote for deletion.
2205 	 */
2206 	if (hint == NOTE_REVOKE)
2207 		kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2208 
2209         kn->kn_data = 0;
2210         return (1);
2211 }
2212 
2213 static int
2214 filt_ufsvnode(struct knote *kn, long hint)
2215 {
2216 	if (kn->kn_sfflags & hint)
2217 		kn->kn_fflags |= hint;
2218 	if (hint == NOTE_REVOKE) {
2219 		kn->kn_flags |= (EV_EOF | EV_NODATA);
2220 		return (1);
2221 	}
2222 	return (kn->kn_fflags != 0);
2223 }
2224 
2225 /* Global vfs data structures for ufs. */
2226 static struct vop_ops ufs_vnode_vops = {
2227 	.vop_default =		vop_defaultop,
2228 	.vop_fsync =		(void *)ufs_missingop,
2229 	.vop_read =		(void *)ufs_missingop,
2230 	.vop_reallocblks =	(void *)ufs_missingop,
2231 	.vop_write =		(void *)ufs_missingop,
2232 	.vop_access =		ufs_access,
2233 	.vop_advlock =		ufs_advlock,
2234 	.vop_bmap =		ufs_bmap,
2235 	.vop_old_lookup =	ufs_lookup,
2236 	.vop_close =		ufs_close,
2237 	.vop_old_create =	ufs_create,
2238 	.vop_getattr =		ufs_getattr,
2239 	.vop_inactive =		ufs_inactive,
2240 	.vop_old_link =		ufs_link,
2241 	.vop_old_mkdir =	ufs_mkdir,
2242 	.vop_old_mknod =	ufs_mknod,
2243 	.vop_open =		vop_stdopen,
2244 	.vop_pathconf =		vop_stdpathconf,
2245 	.vop_kqfilter =		ufs_kqfilter,
2246 	.vop_print =		ufs_print,
2247 	.vop_readdir =		ufs_readdir,
2248 	.vop_readlink =		ufs_readlink,
2249 	.vop_reclaim =		ufs_reclaim,
2250 	.vop_old_remove =	ufs_remove,
2251 	.vop_old_rename =	ufs_rename,
2252 	.vop_old_rmdir =	ufs_rmdir,
2253 	.vop_setattr =		ufs_setattr,
2254 	.vop_markatime =	ufs_markatime,
2255 	.vop_strategy =		ufs_strategy,
2256 	.vop_old_symlink =	ufs_symlink,
2257 	.vop_old_whiteout =	ufs_whiteout
2258 };
2259 
2260 static struct vop_ops ufs_spec_vops = {
2261 	.vop_default =		vop_defaultop,
2262 	.vop_fsync =		(void *)ufs_missingop,
2263 	.vop_access =		ufs_access,
2264 	.vop_close =		ufs_close,
2265 	.vop_getattr =		ufs_getattr,
2266 	.vop_inactive =		ufs_inactive,
2267 	.vop_print =		ufs_print,
2268 	.vop_read =		vop_stdnoread,
2269 	.vop_reclaim =		ufs_reclaim,
2270 	.vop_setattr =		ufs_setattr,
2271 	.vop_markatime =	ufs_markatime,
2272 	.vop_write =		vop_stdnowrite
2273 };
2274 
2275 static struct vop_ops ufs_fifo_vops = {
2276 	.vop_default =		fifo_vnoperate,
2277 	.vop_fsync =		(void *)ufs_missingop,
2278 	.vop_access =		ufs_access,
2279 	.vop_close =		ufsfifo_close,
2280 	.vop_getattr =		ufs_getattr,
2281 	.vop_inactive =		ufs_inactive,
2282 	.vop_kqfilter =		ufsfifo_kqfilter,
2283 	.vop_print =		ufs_print,
2284 	.vop_read =		ufsfifo_read,
2285 	.vop_reclaim =		ufs_reclaim,
2286 	.vop_setattr =		ufs_setattr,
2287 	.vop_markatime =	ufs_markatime,
2288 	.vop_write =		ufsfifo_write
2289 };
2290 
2291 VNODEOP_SET(ufs_vnode_vops);
2292 VNODEOP_SET(ufs_spec_vops);
2293 VNODEOP_SET(ufs_fifo_vops);
2294 
2295 /*
2296  * ufs_vnoperate()
2297  */
2298 int
2299 ufs_vnoperate(struct vop_generic_args *ap)
2300 {
2301 	return (VOCALL(&ufs_vnode_vops, ap));
2302 }
2303 
2304 /*
2305  * ufs_vnoperatefifo()
2306  */
2307 int
2308 ufs_vnoperatefifo(struct vop_generic_args *ap)
2309 {
2310 	return (VOCALL(&ufs_fifo_vops, ap));
2311 }
2312 
2313 /*
2314  * ufs_vnoperatespec()
2315  */
2316 int
2317 ufs_vnoperatespec(struct vop_generic_args *ap)
2318 {
2319 	return (VOCALL(&ufs_spec_vops, ap));
2320 }
2321