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