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