ufs_vnops.c revision 101744
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. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	@(#)ufs_vnops.c	8.27 (Berkeley) 5/27/95
39 * $FreeBSD: head/sys/ufs/ufs/ufs_vnops.c 101744 2002-08-12 16:43:04Z rwatson $
40 */
41
42#include "opt_mac.h"
43#include "opt_quota.h"
44#include "opt_suiddir.h"
45#include "opt_ufs.h"
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/malloc.h>
50#include <sys/namei.h>
51#include <sys/kernel.h>
52#include <sys/fcntl.h>
53#include <sys/stat.h>
54#include <sys/bio.h>
55#include <sys/buf.h>
56#include <sys/mount.h>
57#include <sys/unistd.h>
58#include <sys/vnode.h>
59#include <sys/dirent.h>
60#include <sys/lockf.h>
61#include <sys/event.h>
62#include <sys/conf.h>
63#include <sys/acl.h>
64#include <sys/mac.h>
65
66#include <machine/mutex.h>
67
68#include <sys/file.h>		/* XXX */
69
70#include <vm/vm.h>
71#include <vm/vm_extern.h>
72
73#include <fs/fifofs/fifo.h>
74
75#include <ufs/ufs/acl.h>
76#include <ufs/ufs/extattr.h>
77#include <ufs/ufs/quota.h>
78#include <ufs/ufs/inode.h>
79#include <ufs/ufs/dir.h>
80#include <ufs/ufs/ufsmount.h>
81#include <ufs/ufs/ufs_extern.h>
82#ifdef UFS_DIRHASH
83#include <ufs/ufs/dirhash.h>
84#endif
85
86static int ufs_access(struct vop_access_args *);
87static int ufs_advlock(struct vop_advlock_args *);
88static int ufs_chmod(struct vnode *, int, struct ucred *, struct thread *);
89static int ufs_chown(struct vnode *, uid_t, gid_t, struct ucred *, struct thread *);
90static int ufs_close(struct vop_close_args *);
91static int ufs_create(struct vop_create_args *);
92static int ufs_getattr(struct vop_getattr_args *);
93static int ufs_link(struct vop_link_args *);
94static int ufs_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
95static int ufs_mkdir(struct vop_mkdir_args *);
96static int ufs_mknod(struct vop_mknod_args *);
97static int ufs_open(struct vop_open_args *);
98static int ufs_pathconf(struct vop_pathconf_args *);
99static int ufs_print(struct vop_print_args *);
100static int ufs_readlink(struct vop_readlink_args *);
101static int ufs_remove(struct vop_remove_args *);
102static int ufs_rename(struct vop_rename_args *);
103static int ufs_rmdir(struct vop_rmdir_args *);
104static int ufs_setattr(struct vop_setattr_args *);
105static int ufs_strategy(struct vop_strategy_args *);
106static int ufs_symlink(struct vop_symlink_args *);
107static int ufs_whiteout(struct vop_whiteout_args *);
108static int ufsfifo_close(struct vop_close_args *);
109static int ufsfifo_kqfilter(struct vop_kqfilter_args *);
110static int ufsfifo_read(struct vop_read_args *);
111static int ufsfifo_write(struct vop_write_args *);
112static int ufsspec_close(struct vop_close_args *);
113static int ufsspec_read(struct vop_read_args *);
114static int ufsspec_write(struct vop_write_args *);
115static int filt_ufsread(struct knote *kn, long hint);
116static int filt_ufswrite(struct knote *kn, long hint);
117static int filt_ufsvnode(struct knote *kn, long hint);
118static void filt_ufsdetach(struct knote *kn);
119static int ufs_kqfilter(struct vop_kqfilter_args *ap);
120
121union _qcvt {
122	int64_t qcvt;
123	int32_t val[2];
124};
125#define SETHIGH(q, h) { \
126	union _qcvt tmp; \
127	tmp.qcvt = (q); \
128	tmp.val[_QUAD_HIGHWORD] = (h); \
129	(q) = tmp.qcvt; \
130}
131#define SETLOW(q, l) { \
132	union _qcvt tmp; \
133	tmp.qcvt = (q); \
134	tmp.val[_QUAD_LOWWORD] = (l); \
135	(q) = tmp.qcvt; \
136}
137
138/*
139 * A virgin directory (no blushing please).
140 */
141static struct dirtemplate mastertemplate = {
142	0, 12, DT_DIR, 1, ".",
143	0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
144};
145static struct odirtemplate omastertemplate = {
146	0, 12, 1, ".",
147	0, DIRBLKSIZ - 12, 2, ".."
148};
149
150void
151ufs_itimes(vp)
152	struct vnode *vp;
153{
154	struct inode *ip;
155	struct timespec ts;
156
157	ip = VTOI(vp);
158	if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
159		return;
160	if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
161		ip->i_flag |= IN_LAZYMOD;
162	else
163		ip->i_flag |= IN_MODIFIED;
164	if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
165		vfs_timestamp(&ts);
166		if (ip->i_flag & IN_ACCESS) {
167			DIP(ip, i_atime) = ts.tv_sec;
168			DIP(ip, i_atimensec) = ts.tv_nsec;
169		}
170		if (ip->i_flag & IN_UPDATE) {
171			DIP(ip, i_mtime) = ts.tv_sec;
172			DIP(ip, i_mtimensec) = ts.tv_nsec;
173			ip->i_modrev++;
174		}
175		if (ip->i_flag & IN_CHANGE) {
176			DIP(ip, i_ctime) = ts.tv_sec;
177			DIP(ip, i_ctimensec) = ts.tv_nsec;
178		}
179	}
180	ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
181}
182
183/*
184 * Create a regular file
185 */
186int
187ufs_create(ap)
188	struct vop_create_args /* {
189		struct vnode *a_dvp;
190		struct vnode **a_vpp;
191		struct componentname *a_cnp;
192		struct vattr *a_vap;
193	} */ *ap;
194{
195	int error;
196
197	error =
198	    ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
199	    ap->a_dvp, ap->a_vpp, ap->a_cnp);
200	if (error)
201		return (error);
202	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
203	return (0);
204}
205
206/*
207 * Mknod vnode call
208 */
209/* ARGSUSED */
210int
211ufs_mknod(ap)
212	struct vop_mknod_args /* {
213		struct vnode *a_dvp;
214		struct vnode **a_vpp;
215		struct componentname *a_cnp;
216		struct vattr *a_vap;
217	} */ *ap;
218{
219	struct vattr *vap = ap->a_vap;
220	struct vnode **vpp = ap->a_vpp;
221	struct inode *ip;
222	ino_t ino;
223	int error;
224
225	error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
226	    ap->a_dvp, vpp, ap->a_cnp);
227	if (error)
228		return (error);
229	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
230	ip = VTOI(*vpp);
231	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
232	if (vap->va_rdev != VNOVAL) {
233		/*
234		 * Want to be able to use this to make badblock
235		 * inodes, so don't truncate the dev number.
236		 */
237		DIP(ip, i_rdev) = vap->va_rdev;
238	}
239	/*
240	 * Remove inode, then reload it through VFS_VGET so it is
241	 * checked to see if it is an alias of an existing entry in
242	 * the inode cache.
243	 */
244	vput(*vpp);
245	(*vpp)->v_type = VNON;
246	ino = ip->i_number;	/* Save this before vgone() invalidates ip. */
247	vgone(*vpp);
248	error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
249	if (error) {
250		*vpp = NULL;
251		return (error);
252	}
253	return (0);
254}
255
256/*
257 * Open called.
258 *
259 * Nothing to do.
260 */
261/* ARGSUSED */
262int
263ufs_open(ap)
264	struct vop_open_args /* {
265		struct vnode *a_vp;
266		int  a_mode;
267		struct ucred *a_cred;
268		struct thread *a_td;
269	} */ *ap;
270{
271
272	/*
273	 * Files marked append-only must be opened for appending.
274	 */
275	if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
276	    (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
277		return (EPERM);
278	return (0);
279}
280
281/*
282 * Close called.
283 *
284 * Update the times on the inode.
285 */
286/* ARGSUSED */
287int
288ufs_close(ap)
289	struct vop_close_args /* {
290		struct vnode *a_vp;
291		int  a_fflag;
292		struct ucred *a_cred;
293		struct thread *a_td;
294	} */ *ap;
295{
296	struct vnode *vp = ap->a_vp;
297	struct mount *mp;
298
299	mtx_lock(&vp->v_interlock);
300	if (vp->v_usecount > 1) {
301		ufs_itimes(vp);
302		mtx_unlock(&vp->v_interlock);
303	} else {
304		mtx_unlock(&vp->v_interlock);
305		/*
306		 * If we are closing the last reference to an unlinked
307		 * file, then it will be freed by the inactive routine.
308		 * Because the freeing causes a the filesystem to be
309		 * modified, it must be held up during periods when the
310		 * filesystem is suspended.
311		 *
312		 * XXX - EAGAIN is returned to prevent vn_close from
313		 * repeating the vrele operation.
314		 */
315		if (vp->v_type == VREG && VTOI(vp)->i_effnlink == 0) {
316			(void) vn_start_write(vp, &mp, V_WAIT);
317			vrele(vp);
318			vn_finished_write(mp);
319			return (EAGAIN);
320		}
321	}
322	return (0);
323}
324
325int
326ufs_access(ap)
327	struct vop_access_args /* {
328		struct vnode *a_vp;
329		int  a_mode;
330		struct ucred *a_cred;
331		struct thread *a_td;
332	} */ *ap;
333{
334	struct vnode *vp = ap->a_vp;
335	struct inode *ip = VTOI(vp);
336	mode_t mode = ap->a_mode;
337	int error;
338#ifdef UFS_ACL
339	struct acl *acl;
340	int len;
341#endif
342
343	/*
344	 * Disallow write attempts on read-only filesystems;
345	 * unless the file is a socket, fifo, or a block or
346	 * character device resident on the filesystem.
347	 */
348	if (mode & VWRITE) {
349		switch (vp->v_type) {
350		case VDIR:
351		case VLNK:
352		case VREG:
353			if (vp->v_mount->mnt_flag & MNT_RDONLY)
354				return (EROFS);
355#ifdef QUOTA
356			if ((error = getinoquota(ip)) != 0)
357				return (error);
358#endif
359			break;
360		default:
361			break;
362		}
363	}
364
365	/* If immutable bit set, nobody gets to write it. */
366	if ((mode & VWRITE) && (ip->i_flags & (IMMUTABLE | SF_SNAPSHOT)))
367		return (EPERM);
368
369#ifdef UFS_ACL
370	MALLOC(acl, struct acl *, sizeof(*acl), M_ACL, M_WAITOK);
371	len = sizeof(*acl);
372	error = VOP_GETACL(vp, ACL_TYPE_ACCESS, acl, ap->a_cred, ap->a_td);
373	switch (error) {
374	case EOPNOTSUPP:
375		error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
376		    ap->a_mode, ap->a_cred, NULL);
377		break;
378	case 0:
379		error = vaccess_acl_posix1e(vp->v_type, ip->i_uid, ip->i_gid,
380		    acl, ap->a_mode, ap->a_cred, NULL);
381		break;
382	default:
383		printf("ufs_access(): Error retrieving ACL on object (%d).\n",
384		    error);
385		/*
386		 * XXX: Fall back until debugged.  Should eventually
387		 * possibly log an error, and return EPERM for safety.
388		 */
389		error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
390		    ap->a_mode, ap->a_cred, NULL);
391	}
392	FREE(acl, M_ACL);
393#else
394	error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
395	    ap->a_mode, ap->a_cred, NULL);
396#endif
397	return (error);
398}
399
400/* ARGSUSED */
401int
402ufs_getattr(ap)
403	struct vop_getattr_args /* {
404		struct vnode *a_vp;
405		struct vattr *a_vap;
406		struct ucred *a_cred;
407		struct thread *a_td;
408	} */ *ap;
409{
410	struct vnode *vp = ap->a_vp;
411	struct inode *ip = VTOI(vp);
412	struct vattr *vap = ap->a_vap;
413
414	ufs_itimes(vp);
415	/*
416	 * Copy from inode table
417	 */
418	vap->va_fsid = dev2udev(ip->i_dev);
419	vap->va_fileid = ip->i_number;
420	vap->va_mode = ip->i_mode & ~IFMT;
421	vap->va_nlink = ip->i_effnlink;
422	vap->va_uid = ip->i_uid;
423	vap->va_gid = ip->i_gid;
424	if (ip->i_ump->um_fstype == UFS1) {
425		vap->va_rdev = ip->i_din1->di_rdev;
426		vap->va_size = ip->i_din1->di_size;
427		vap->va_atime.tv_sec = ip->i_din1->di_atime;
428		vap->va_atime.tv_nsec = ip->i_din1->di_atimensec;
429		vap->va_mtime.tv_sec = ip->i_din1->di_mtime;
430		vap->va_mtime.tv_nsec = ip->i_din1->di_mtimensec;
431		vap->va_ctime.tv_sec = ip->i_din1->di_ctime;
432		vap->va_ctime.tv_nsec = ip->i_din1->di_ctimensec;
433		vap->va_birthtime.tv_sec = 0;
434		vap->va_birthtime.tv_nsec = 0;
435		vap->va_bytes = dbtob((u_quad_t)ip->i_din1->di_blocks);
436	} else {
437		vap->va_rdev = ip->i_din2->di_rdev;
438		vap->va_size = ip->i_din2->di_size;
439		vap->va_atime.tv_sec = ip->i_din2->di_atime;
440		vap->va_atime.tv_nsec = ip->i_din2->di_atimensec;
441		vap->va_mtime.tv_sec = ip->i_din2->di_mtime;
442		vap->va_mtime.tv_nsec = ip->i_din2->di_mtimensec;
443		vap->va_ctime.tv_sec = ip->i_din2->di_ctime;
444		vap->va_ctime.tv_nsec = ip->i_din2->di_ctimensec;
445		vap->va_birthtime.tv_sec = ip->i_din2->di_birthtime;
446		vap->va_birthtime.tv_nsec = ip->i_din2->di_birthnsec;
447		vap->va_bytes = dbtob((u_quad_t)ip->i_din2->di_blocks);
448	}
449	vap->va_flags = ip->i_flags;
450	vap->va_gen = ip->i_gen;
451	vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
452	vap->va_type = IFTOVT(ip->i_mode);
453	vap->va_filerev = ip->i_modrev;
454	return (0);
455}
456
457/*
458 * Set attribute vnode op. called from several syscalls
459 */
460int
461ufs_setattr(ap)
462	struct vop_setattr_args /* {
463		struct vnode *a_vp;
464		struct vattr *a_vap;
465		struct ucred *a_cred;
466		struct thread *a_td;
467	} */ *ap;
468{
469	struct vattr *vap = ap->a_vap;
470	struct vnode *vp = ap->a_vp;
471	struct inode *ip = VTOI(vp);
472	struct ucred *cred = ap->a_cred;
473	struct thread *td = ap->a_td;
474	int error;
475
476	/*
477	 * Check for unsettable attributes.
478	 */
479	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
480	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
481	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
482	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
483		return (EINVAL);
484	}
485	if (vap->va_flags != VNOVAL) {
486		if (vp->v_mount->mnt_flag & MNT_RDONLY)
487			return (EROFS);
488		/*
489		 * Callers may only modify the file flags on objects they
490		 * have VADMIN rights for.
491		 */
492		if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
493			return (error);
494		/*
495		 * Unprivileged processes and privileged processes in
496		 * jail() are not permitted to unset system flags, or
497		 * modify flags if any system flags are set.
498		 * Privileged non-jail processes may not modify system flags
499		 * if securelevel > 0 and any existing system flags are set.
500		 */
501		if (!suser_cred(cred, PRISON_ROOT)) {
502			if (ip->i_flags
503			    & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
504				error = securelevel_gt(cred, 0);
505				if (error)
506					return (error);
507			}
508			/* Snapshot flag cannot be set or cleared */
509			if (((vap->va_flags & SF_SNAPSHOT) != 0 &&
510			     (ip->i_flags & SF_SNAPSHOT) == 0) ||
511			    ((vap->va_flags & SF_SNAPSHOT) == 0 &&
512			     (ip->i_flags & SF_SNAPSHOT) != 0))
513				return (EPERM);
514			ip->i_flags = vap->va_flags;
515			DIP(ip, i_flags) = vap->va_flags;
516		} else {
517			if (ip->i_flags
518			    & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
519			    (vap->va_flags & UF_SETTABLE) != vap->va_flags)
520				return (EPERM);
521			ip->i_flags &= SF_SETTABLE;
522			ip->i_flags |= (vap->va_flags & UF_SETTABLE);
523			DIP(ip, i_flags) = ip->i_flags;
524		}
525		ip->i_flag |= IN_CHANGE;
526		if (vap->va_flags & (IMMUTABLE | APPEND))
527			return (0);
528	}
529	if (ip->i_flags & (IMMUTABLE | APPEND))
530		return (EPERM);
531	/*
532	 * Go through the fields and update iff not VNOVAL.
533	 */
534	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
535		if (vp->v_mount->mnt_flag & MNT_RDONLY)
536			return (EROFS);
537		if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred,
538		    td)) != 0)
539			return (error);
540	}
541	if (vap->va_size != VNOVAL) {
542		/*
543		 * Disallow write attempts on read-only filesystems;
544		 * unless the file is a socket, fifo, or a block or
545		 * character device resident on the filesystem.
546		 */
547		switch (vp->v_type) {
548		case VDIR:
549			return (EISDIR);
550		case VLNK:
551		case VREG:
552			if (vp->v_mount->mnt_flag & MNT_RDONLY)
553				return (EROFS);
554			if ((ip->i_flags & SF_SNAPSHOT) != 0)
555				return (EPERM);
556			break;
557		default:
558			break;
559		}
560		if ((error = UFS_TRUNCATE(vp, vap->va_size, IO_NORMAL,
561		    cred, td)) != 0)
562			return (error);
563	}
564	if (vap->va_atime.tv_sec != VNOVAL ||
565	    vap->va_mtime.tv_sec != VNOVAL ||
566	    vap->va_birthtime.tv_sec != VNOVAL) {
567		if (vp->v_mount->mnt_flag & MNT_RDONLY)
568			return (EROFS);
569		if ((ip->i_flags & SF_SNAPSHOT) != 0)
570			return (EPERM);
571		/*
572		 * From utimes(2):
573		 * If times is NULL, ... The caller must be the owner of
574		 * the file, have permission to write the file, or be the
575		 * super-user.
576		 * If times is non-NULL, ... The caller must be the owner of
577		 * the file or be the super-user.
578		 */
579		if ((error = VOP_ACCESS(vp, VADMIN, cred, td)) &&
580		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
581		    (error = VOP_ACCESS(vp, VWRITE, cred, td))))
582			return (error);
583		if (vap->va_atime.tv_sec != VNOVAL)
584			ip->i_flag |= IN_ACCESS;
585		if (vap->va_mtime.tv_sec != VNOVAL)
586			ip->i_flag |= IN_CHANGE | IN_UPDATE;
587		if (vap->va_birthtime.tv_sec != VNOVAL &&
588		    ip->i_ump->um_fstype == UFS2)
589			ip->i_flag |= IN_MODIFIED;
590		ufs_itimes(vp);
591		if (vap->va_atime.tv_sec != VNOVAL) {
592			DIP(ip, i_atime) = vap->va_atime.tv_sec;
593			DIP(ip, i_atimensec) = vap->va_atime.tv_nsec;
594		}
595		if (vap->va_mtime.tv_sec != VNOVAL) {
596			DIP(ip, i_mtime) = vap->va_mtime.tv_sec;
597			DIP(ip, i_mtimensec) = vap->va_mtime.tv_nsec;
598		}
599		if (vap->va_birthtime.tv_sec != VNOVAL &&
600		    ip->i_ump->um_fstype == UFS2) {
601			ip->i_din2->di_birthtime = vap->va_birthtime.tv_sec;
602			ip->i_din2->di_birthnsec = vap->va_birthtime.tv_nsec;
603		}
604		error = UFS_UPDATE(vp, 0);
605		if (error)
606			return (error);
607	}
608	error = 0;
609	if (vap->va_mode != (mode_t)VNOVAL) {
610		if (vp->v_mount->mnt_flag & MNT_RDONLY)
611			return (EROFS);
612		if ((ip->i_flags & SF_SNAPSHOT) != 0 && (vap->va_mode &
613		   (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP | S_IXOTH | S_IWOTH)))
614			return (EPERM);
615		error = ufs_chmod(vp, (int)vap->va_mode, cred, td);
616	}
617	VN_KNOTE(vp, NOTE_ATTRIB);
618	return (error);
619}
620
621/*
622 * Change the mode on a file.
623 * Inode must be locked before calling.
624 */
625static int
626ufs_chmod(vp, mode, cred, td)
627	struct vnode *vp;
628	int mode;
629	struct ucred *cred;
630	struct thread *td;
631{
632	struct inode *ip = VTOI(vp);
633	int error;
634
635	/*
636	 * To modify the permissions on a file, must possess VADMIN
637	 * for that file.
638	 */
639	if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
640		return (error);
641	/*
642	 * Privileged processes may set the sticky bit on non-directories,
643	 * as well as set the setgid bit on a file with a group that the
644	 * process is not a member of.
645	 */
646	if (suser_cred(cred, PRISON_ROOT)) {
647		if (vp->v_type != VDIR && (mode & S_ISTXT))
648			return (EFTYPE);
649		if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
650			return (EPERM);
651	}
652	ip->i_mode &= ~ALLPERMS;
653	ip->i_mode |= (mode & ALLPERMS);
654	DIP(ip, i_mode) = ip->i_mode;
655	ip->i_flag |= IN_CHANGE;
656	return (0);
657}
658
659/*
660 * Perform chown operation on inode ip;
661 * inode must be locked prior to call.
662 */
663static int
664ufs_chown(vp, uid, gid, cred, td)
665	struct vnode *vp;
666	uid_t uid;
667	gid_t gid;
668	struct ucred *cred;
669	struct thread *td;
670{
671	struct inode *ip = VTOI(vp);
672	uid_t ouid;
673	gid_t ogid;
674	int error = 0;
675#ifdef QUOTA
676	int i;
677	ufs2_daddr_t change;
678#endif
679
680	if (uid == (uid_t)VNOVAL)
681		uid = ip->i_uid;
682	if (gid == (gid_t)VNOVAL)
683		gid = ip->i_gid;
684	/*
685	 * To modify the ownership of a file, must possess VADMIN
686	 * for that file.
687	 */
688	if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
689		return (error);
690	/*
691	 * To change the owner of a file, or change the group of a file
692	 * to a group of which we are not a member, the caller must
693	 * have privilege.
694	 */
695	if ((uid != ip->i_uid ||
696	    (gid != ip->i_gid && !groupmember(gid, cred))) &&
697	    (error = suser_cred(cred, PRISON_ROOT)))
698		return (error);
699	ogid = ip->i_gid;
700	ouid = ip->i_uid;
701#ifdef QUOTA
702	if ((error = getinoquota(ip)) != 0)
703		return (error);
704	if (ouid == uid) {
705		dqrele(vp, ip->i_dquot[USRQUOTA]);
706		ip->i_dquot[USRQUOTA] = NODQUOT;
707	}
708	if (ogid == gid) {
709		dqrele(vp, ip->i_dquot[GRPQUOTA]);
710		ip->i_dquot[GRPQUOTA] = NODQUOT;
711	}
712	change = DIP(ip, i_blocks);
713	(void) chkdq(ip, -change, cred, CHOWN);
714	(void) chkiq(ip, -1, cred, CHOWN);
715	for (i = 0; i < MAXQUOTAS; i++) {
716		dqrele(vp, ip->i_dquot[i]);
717		ip->i_dquot[i] = NODQUOT;
718	}
719#endif
720	ip->i_gid = gid;
721	DIP(ip, i_gid) = gid;
722	ip->i_uid = uid;
723	DIP(ip, i_uid) = uid;
724#ifdef QUOTA
725	if ((error = getinoquota(ip)) == 0) {
726		if (ouid == uid) {
727			dqrele(vp, ip->i_dquot[USRQUOTA]);
728			ip->i_dquot[USRQUOTA] = NODQUOT;
729		}
730		if (ogid == gid) {
731			dqrele(vp, ip->i_dquot[GRPQUOTA]);
732			ip->i_dquot[GRPQUOTA] = NODQUOT;
733		}
734		if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
735			if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
736				goto good;
737			else
738				(void) chkdq(ip, -change, cred, CHOWN|FORCE);
739		}
740		for (i = 0; i < MAXQUOTAS; i++) {
741			dqrele(vp, ip->i_dquot[i]);
742			ip->i_dquot[i] = NODQUOT;
743		}
744	}
745	ip->i_gid = ogid;
746	DIP(ip, i_gid) = ogid;
747	ip->i_uid = ouid;
748	DIP(ip, i_uid) = ouid;
749	if (getinoquota(ip) == 0) {
750		if (ouid == uid) {
751			dqrele(vp, ip->i_dquot[USRQUOTA]);
752			ip->i_dquot[USRQUOTA] = NODQUOT;
753		}
754		if (ogid == gid) {
755			dqrele(vp, ip->i_dquot[GRPQUOTA]);
756			ip->i_dquot[GRPQUOTA] = NODQUOT;
757		}
758		(void) chkdq(ip, change, cred, FORCE|CHOWN);
759		(void) chkiq(ip, 1, cred, FORCE|CHOWN);
760		(void) getinoquota(ip);
761	}
762	return (error);
763good:
764	if (getinoquota(ip))
765		panic("ufs_chown: lost quota");
766#endif /* QUOTA */
767	ip->i_flag |= IN_CHANGE;
768	if (suser_cred(cred, PRISON_ROOT) && (ouid != uid || ogid != gid)) {
769		ip->i_mode &= ~(ISUID | ISGID);
770		DIP(ip, i_mode) = ip->i_mode;
771	}
772	return (0);
773}
774
775int
776ufs_remove(ap)
777	struct vop_remove_args /* {
778		struct vnode *a_dvp;
779		struct vnode *a_vp;
780		struct componentname *a_cnp;
781	} */ *ap;
782{
783	struct inode *ip;
784	struct vnode *vp = ap->a_vp;
785	struct vnode *dvp = ap->a_dvp;
786	int error;
787
788	ip = VTOI(vp);
789	if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
790	    (VTOI(dvp)->i_flags & APPEND)) {
791		error = EPERM;
792		goto out;
793	}
794	error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
795	if (ip->i_nlink <= 0)
796		vp->v_vflag |= VV_NOSYNC;
797	VN_KNOTE(vp, NOTE_DELETE);
798	VN_KNOTE(dvp, NOTE_WRITE);
799out:
800	return (error);
801}
802
803/*
804 * link vnode call
805 */
806int
807ufs_link(ap)
808	struct vop_link_args /* {
809		struct vnode *a_tdvp;
810		struct vnode *a_vp;
811		struct componentname *a_cnp;
812	} */ *ap;
813{
814	struct vnode *vp = ap->a_vp;
815	struct vnode *tdvp = ap->a_tdvp;
816	struct componentname *cnp = ap->a_cnp;
817	struct thread *td = cnp->cn_thread;
818	struct inode *ip;
819	struct direct newdir;
820	int error;
821
822#ifdef DIAGNOSTIC
823	if ((cnp->cn_flags & HASBUF) == 0)
824		panic("ufs_link: no name");
825#endif
826	if (tdvp->v_mount != vp->v_mount) {
827		error = EXDEV;
828		goto out2;
829	}
830	if (tdvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE, td))) {
831		goto out2;
832	}
833	ip = VTOI(vp);
834	if ((nlink_t)ip->i_nlink >= LINK_MAX) {
835		error = EMLINK;
836		goto out1;
837	}
838	if (ip->i_flags & (IMMUTABLE | APPEND)) {
839		error = EPERM;
840		goto out1;
841	}
842	ip->i_effnlink++;
843	ip->i_nlink++;
844	DIP(ip, i_nlink) = ip->i_nlink;
845	ip->i_flag |= IN_CHANGE;
846	if (DOINGSOFTDEP(vp))
847		softdep_change_linkcnt(ip);
848	error = UFS_UPDATE(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
849	if (!error) {
850		ufs_makedirentry(ip, cnp, &newdir);
851		error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
852	}
853
854	if (error) {
855		ip->i_effnlink--;
856		ip->i_nlink--;
857		DIP(ip, i_nlink) = ip->i_nlink;
858		ip->i_flag |= IN_CHANGE;
859		if (DOINGSOFTDEP(vp))
860			softdep_change_linkcnt(ip);
861	}
862out1:
863	if (tdvp != vp)
864		VOP_UNLOCK(vp, 0, td);
865out2:
866	VN_KNOTE(vp, NOTE_LINK);
867	VN_KNOTE(tdvp, NOTE_WRITE);
868	return (error);
869}
870
871/*
872 * whiteout vnode call
873 */
874int
875ufs_whiteout(ap)
876	struct vop_whiteout_args /* {
877		struct vnode *a_dvp;
878		struct componentname *a_cnp;
879		int a_flags;
880	} */ *ap;
881{
882	struct vnode *dvp = ap->a_dvp;
883	struct componentname *cnp = ap->a_cnp;
884	struct direct newdir;
885	int error = 0;
886
887	switch (ap->a_flags) {
888	case LOOKUP:
889		/* 4.4 format directories support whiteout operations */
890		if (dvp->v_mount->mnt_maxsymlinklen > 0)
891			return (0);
892		return (EOPNOTSUPP);
893
894	case CREATE:
895		/* create a new directory whiteout */
896#ifdef DIAGNOSTIC
897		if ((cnp->cn_flags & SAVENAME) == 0)
898			panic("ufs_whiteout: missing name");
899		if (dvp->v_mount->mnt_maxsymlinklen <= 0)
900			panic("ufs_whiteout: old format filesystem");
901#endif
902
903		newdir.d_ino = WINO;
904		newdir.d_namlen = cnp->cn_namelen;
905		bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
906		newdir.d_type = DT_WHT;
907		error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
908		break;
909
910	case DELETE:
911		/* remove an existing directory whiteout */
912#ifdef DIAGNOSTIC
913		if (dvp->v_mount->mnt_maxsymlinklen <= 0)
914			panic("ufs_whiteout: old format filesystem");
915#endif
916
917		cnp->cn_flags &= ~DOWHITEOUT;
918		error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
919		break;
920	default:
921		panic("ufs_whiteout: unknown op");
922	}
923	return (error);
924}
925
926/*
927 * Rename system call.
928 * 	rename("foo", "bar");
929 * is essentially
930 *	unlink("bar");
931 *	link("foo", "bar");
932 *	unlink("foo");
933 * but ``atomically''.  Can't do full commit without saving state in the
934 * inode on disk which isn't feasible at this time.  Best we can do is
935 * always guarantee the target exists.
936 *
937 * Basic algorithm is:
938 *
939 * 1) Bump link count on source while we're linking it to the
940 *    target.  This also ensure the inode won't be deleted out
941 *    from underneath us while we work (it may be truncated by
942 *    a concurrent `trunc' or `open' for creation).
943 * 2) Link source to destination.  If destination already exists,
944 *    delete it first.
945 * 3) Unlink source reference to inode if still around. If a
946 *    directory was moved and the parent of the destination
947 *    is different from the source, patch the ".." entry in the
948 *    directory.
949 */
950int
951ufs_rename(ap)
952	struct vop_rename_args  /* {
953		struct vnode *a_fdvp;
954		struct vnode *a_fvp;
955		struct componentname *a_fcnp;
956		struct vnode *a_tdvp;
957		struct vnode *a_tvp;
958		struct componentname *a_tcnp;
959	} */ *ap;
960{
961	struct vnode *tvp = ap->a_tvp;
962	struct vnode *tdvp = ap->a_tdvp;
963	struct vnode *fvp = ap->a_fvp;
964	struct vnode *fdvp = ap->a_fdvp;
965	struct componentname *tcnp = ap->a_tcnp;
966	struct componentname *fcnp = ap->a_fcnp;
967	struct thread *td = fcnp->cn_thread;
968	struct inode *ip, *xp, *dp;
969	struct direct newdir;
970	int doingdirectory = 0, oldparent = 0, newparent = 0;
971	int error = 0, ioflag;
972
973#ifdef DIAGNOSTIC
974	if ((tcnp->cn_flags & HASBUF) == 0 ||
975	    (fcnp->cn_flags & HASBUF) == 0)
976		panic("ufs_rename: no name");
977#endif
978	/*
979	 * Check for cross-device rename.
980	 */
981	if ((fvp->v_mount != tdvp->v_mount) ||
982	    (tvp && (fvp->v_mount != tvp->v_mount))) {
983		error = EXDEV;
984abortit:
985		if (tdvp == tvp)
986			vrele(tdvp);
987		else
988			vput(tdvp);
989		if (tvp)
990			vput(tvp);
991		vrele(fdvp);
992		vrele(fvp);
993		return (error);
994	}
995
996	if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
997	    (VTOI(tdvp)->i_flags & APPEND))) {
998		error = EPERM;
999		goto abortit;
1000	}
1001
1002	/*
1003	 * Check if just deleting a link name or if we've lost a race.
1004	 * If another process completes the same rename after we've looked
1005	 * up the source and have blocked looking up the target, then the
1006	 * source and target inodes may be identical now although the
1007	 * names were never linked.
1008	 */
1009	if (fvp == tvp) {
1010		if (fvp->v_type == VDIR) {
1011			/*
1012			 * Linked directories are impossible, so we must
1013			 * have lost the race.  Pretend that the rename
1014			 * completed before the lookup.
1015			 */
1016#ifdef UFS_RENAME_DEBUG
1017			printf("ufs_rename: fvp == tvp for directories\n");
1018#endif
1019			error = ENOENT;
1020			goto abortit;
1021		}
1022
1023		/* Release destination completely. */
1024		vput(tdvp);
1025		vput(tvp);
1026
1027		/*
1028		 * Delete source.  There is another race now that everything
1029		 * is unlocked, but this doesn't cause any new complications.
1030		 * Relookup() may find a file that is unrelated to the
1031		 * original one, or it may fail.  Too bad.
1032		 */
1033		vrele(fdvp);
1034		vrele(fvp);
1035		fcnp->cn_flags &= ~MODMASK;
1036		fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1037		if ((fcnp->cn_flags & SAVESTART) == 0)
1038			panic("ufs_rename: lost from startdir");
1039		fcnp->cn_nameiop = DELETE;
1040		VREF(fdvp);
1041		error = relookup(fdvp, &fvp, fcnp);
1042		if (error == 0)
1043			vrele(fdvp);
1044		if (fvp == NULL) {
1045#ifdef UFS_RENAME_DEBUG
1046			printf("ufs_rename: from name disappeared\n");
1047#endif
1048			return (ENOENT);
1049		}
1050		error = VOP_REMOVE(fdvp, fvp, fcnp);
1051		if (fdvp == fvp)
1052			vrele(fdvp);
1053		else
1054			vput(fdvp);
1055		vput(fvp);
1056		return (error);
1057	}
1058	if ((error = vn_lock(fvp, LK_EXCLUSIVE, td)) != 0)
1059		goto abortit;
1060	dp = VTOI(fdvp);
1061	ip = VTOI(fvp);
1062	if (ip->i_nlink >= LINK_MAX) {
1063		VOP_UNLOCK(fvp, 0, td);
1064		error = EMLINK;
1065		goto abortit;
1066	}
1067	if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
1068	    || (dp->i_flags & APPEND)) {
1069		VOP_UNLOCK(fvp, 0, td);
1070		error = EPERM;
1071		goto abortit;
1072	}
1073	if ((ip->i_mode & IFMT) == IFDIR) {
1074		/*
1075		 * Avoid ".", "..", and aliases of "." for obvious reasons.
1076		 */
1077		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1078		    dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
1079		    (ip->i_flag & IN_RENAME)) {
1080			VOP_UNLOCK(fvp, 0, td);
1081			error = EINVAL;
1082			goto abortit;
1083		}
1084		ip->i_flag |= IN_RENAME;
1085		oldparent = dp->i_number;
1086		doingdirectory = 1;
1087	}
1088	VN_KNOTE(fdvp, NOTE_WRITE);		/* XXX right place? */
1089	vrele(fdvp);
1090
1091	/*
1092	 * When the target exists, both the directory
1093	 * and target vnodes are returned locked.
1094	 */
1095	dp = VTOI(tdvp);
1096	xp = NULL;
1097	if (tvp)
1098		xp = VTOI(tvp);
1099
1100	/*
1101	 * 1) Bump link count while we're moving stuff
1102	 *    around.  If we crash somewhere before
1103	 *    completing our work, the link count
1104	 *    may be wrong, but correctable.
1105	 */
1106	ip->i_effnlink++;
1107	ip->i_nlink++;
1108	DIP(ip, i_nlink) = ip->i_nlink;
1109	ip->i_flag |= IN_CHANGE;
1110	if (DOINGSOFTDEP(fvp))
1111		softdep_change_linkcnt(ip);
1112	if ((error = UFS_UPDATE(fvp, !(DOINGSOFTDEP(fvp) |
1113				       DOINGASYNC(fvp)))) != 0) {
1114		VOP_UNLOCK(fvp, 0, td);
1115		goto bad;
1116	}
1117
1118	/*
1119	 * If ".." must be changed (ie the directory gets a new
1120	 * parent) then the source directory must not be in the
1121	 * directory heirarchy above the target, as this would
1122	 * orphan everything below the source directory. Also
1123	 * the user must have write permission in the source so
1124	 * as to be able to change "..". We must repeat the call
1125	 * to namei, as the parent directory is unlocked by the
1126	 * call to checkpath().
1127	 */
1128	error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
1129	VOP_UNLOCK(fvp, 0, td);
1130	if (oldparent != dp->i_number)
1131		newparent = dp->i_number;
1132	if (doingdirectory && newparent) {
1133		if (error)	/* write access check above */
1134			goto bad;
1135		if (xp != NULL)
1136			vput(tvp);
1137		error = ufs_checkpath(ip, dp, tcnp->cn_cred);
1138		if (error)
1139			goto out;
1140		if ((tcnp->cn_flags & SAVESTART) == 0)
1141			panic("ufs_rename: lost to startdir");
1142		VREF(tdvp);
1143		error = relookup(tdvp, &tvp, tcnp);
1144		if (error)
1145			goto out;
1146		vrele(tdvp);
1147		dp = VTOI(tdvp);
1148		xp = NULL;
1149		if (tvp)
1150			xp = VTOI(tvp);
1151	}
1152	/*
1153	 * 2) If target doesn't exist, link the target
1154	 *    to the source and unlink the source.
1155	 *    Otherwise, rewrite the target directory
1156	 *    entry to reference the source inode and
1157	 *    expunge the original entry's existence.
1158	 */
1159	if (xp == NULL) {
1160		if (dp->i_dev != ip->i_dev)
1161			panic("ufs_rename: EXDEV");
1162		/*
1163		 * Account for ".." in new directory.
1164		 * When source and destination have the same
1165		 * parent we don't fool with the link count.
1166		 */
1167		if (doingdirectory && newparent) {
1168			if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1169				error = EMLINK;
1170				goto bad;
1171			}
1172			dp->i_effnlink++;
1173			dp->i_nlink++;
1174			DIP(dp, i_nlink) = dp->i_nlink;
1175			dp->i_flag |= IN_CHANGE;
1176			if (DOINGSOFTDEP(tdvp))
1177				softdep_change_linkcnt(dp);
1178			error = UFS_UPDATE(tdvp, !(DOINGSOFTDEP(tdvp) |
1179						   DOINGASYNC(tdvp)));
1180			if (error)
1181				goto bad;
1182		}
1183		ufs_makedirentry(ip, tcnp, &newdir);
1184		error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1185		if (error) {
1186			if (doingdirectory && newparent) {
1187				dp->i_effnlink--;
1188				dp->i_nlink--;
1189				DIP(dp, i_nlink) = dp->i_nlink;
1190				dp->i_flag |= IN_CHANGE;
1191				if (DOINGSOFTDEP(tdvp))
1192					softdep_change_linkcnt(dp);
1193				(void)UFS_UPDATE(tdvp, 1);
1194			}
1195			goto bad;
1196		}
1197		VN_KNOTE(tdvp, NOTE_WRITE);
1198		vput(tdvp);
1199	} else {
1200		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1201			panic("ufs_rename: EXDEV");
1202		/*
1203		 * Short circuit rename(foo, foo).
1204		 */
1205		if (xp->i_number == ip->i_number)
1206			panic("ufs_rename: same file");
1207		/*
1208		 * If the parent directory is "sticky", then the caller
1209		 * must possess VADMIN for the parent directory, or the
1210		 * destination of the rename.  This implements append-only
1211		 * directories.
1212		 */
1213		if ((dp->i_mode & S_ISTXT) &&
1214		    VOP_ACCESS(tdvp, VADMIN, tcnp->cn_cred, td) &&
1215		    VOP_ACCESS(tvp, VADMIN, tcnp->cn_cred, td)) {
1216			error = EPERM;
1217			goto bad;
1218		}
1219		/*
1220		 * Target must be empty if a directory and have no links
1221		 * to it. Also, ensure source and target are compatible
1222		 * (both directories, or both not directories).
1223		 */
1224		if ((xp->i_mode&IFMT) == IFDIR) {
1225			if ((xp->i_effnlink > 2) ||
1226			    !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1227				error = ENOTEMPTY;
1228				goto bad;
1229			}
1230			if (!doingdirectory) {
1231				error = ENOTDIR;
1232				goto bad;
1233			}
1234			cache_purge(tdvp);
1235		} else if (doingdirectory) {
1236			error = EISDIR;
1237			goto bad;
1238		}
1239		error = ufs_dirrewrite(dp, xp, ip->i_number,
1240		    IFTODT(ip->i_mode),
1241		    (doingdirectory && newparent) ? newparent : doingdirectory);
1242		if (error)
1243			goto bad;
1244		if (doingdirectory) {
1245			if (!newparent) {
1246				dp->i_effnlink--;
1247				if (DOINGSOFTDEP(tdvp))
1248					softdep_change_linkcnt(dp);
1249			}
1250			xp->i_effnlink--;
1251			if (DOINGSOFTDEP(tvp))
1252				softdep_change_linkcnt(xp);
1253		}
1254		if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1255			/*
1256			 * Truncate inode. The only stuff left in the directory
1257			 * is "." and "..". The "." reference is inconsequential
1258			 * since we are quashing it. We have removed the "."
1259			 * reference and the reference in the parent directory,
1260			 * but there may be other hard links. The soft
1261			 * dependency code will arrange to do these operations
1262			 * after the parent directory entry has been deleted on
1263			 * disk, so when running with that code we avoid doing
1264			 * them now.
1265			 */
1266			if (!newparent) {
1267				dp->i_nlink--;
1268				DIP(dp, i_nlink) = dp->i_nlink;
1269				dp->i_flag |= IN_CHANGE;
1270			}
1271			xp->i_nlink--;
1272			DIP(xp, i_nlink) = xp->i_nlink;
1273			xp->i_flag |= IN_CHANGE;
1274			ioflag = IO_NORMAL;
1275			if (DOINGASYNC(tvp))
1276				ioflag |= IO_SYNC;
1277			if ((error = UFS_TRUNCATE(tvp, (off_t)0, ioflag,
1278			    tcnp->cn_cred, tcnp->cn_thread)) != 0)
1279				goto bad;
1280		}
1281		VN_KNOTE(tdvp, NOTE_WRITE);
1282		vput(tdvp);
1283		VN_KNOTE(tvp, NOTE_DELETE);
1284		vput(tvp);
1285		xp = NULL;
1286	}
1287
1288	/*
1289	 * 3) Unlink the source.
1290	 */
1291	fcnp->cn_flags &= ~MODMASK;
1292	fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1293	if ((fcnp->cn_flags & SAVESTART) == 0)
1294		panic("ufs_rename: lost from startdir");
1295	VREF(fdvp);
1296	error = relookup(fdvp, &fvp, fcnp);
1297	if (error == 0)
1298		vrele(fdvp);
1299	if (fvp != NULL) {
1300		xp = VTOI(fvp);
1301		dp = VTOI(fdvp);
1302	} else {
1303		/*
1304		 * From name has disappeared.  IN_RENAME is not sufficient
1305		 * to protect against directory races due to timing windows,
1306		 * so we have to remove the panic.  XXX the only real way
1307		 * to solve this issue is at a much higher level.  By the
1308		 * time we hit ufs_rename() it's too late.
1309		 */
1310#if 0
1311		if (doingdirectory)
1312			panic("ufs_rename: lost dir entry");
1313#endif
1314		vrele(ap->a_fvp);
1315		return (0);
1316	}
1317	/*
1318	 * Ensure that the directory entry still exists and has not
1319	 * changed while the new name has been entered. If the source is
1320	 * a file then the entry may have been unlinked or renamed. In
1321	 * either case there is no further work to be done. If the source
1322	 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1323	 * flag ensures that it cannot be moved by another rename or removed
1324	 * by a rmdir.
1325	 */
1326	if (xp != ip) {
1327		/*
1328		 * From name resolves to a different inode.  IN_RENAME is
1329		 * not sufficient protection against timing window races
1330		 * so we can't panic here.  XXX the only real way
1331		 * to solve this issue is at a much higher level.  By the
1332		 * time we hit ufs_rename() it's too late.
1333		 */
1334#if 0
1335		if (doingdirectory)
1336			panic("ufs_rename: lost dir entry");
1337#endif
1338	} else {
1339		/*
1340		 * If the source is a directory with a
1341		 * new parent, the link count of the old
1342		 * parent directory must be decremented
1343		 * and ".." set to point to the new parent.
1344		 */
1345		if (doingdirectory && newparent) {
1346			xp->i_offset = mastertemplate.dot_reclen;
1347			ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1348			cache_purge(fdvp);
1349		}
1350		error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1351		xp->i_flag &= ~IN_RENAME;
1352	}
1353	VN_KNOTE(fvp, NOTE_RENAME);
1354	if (dp)
1355		vput(fdvp);
1356	if (xp)
1357		vput(fvp);
1358	vrele(ap->a_fvp);
1359	return (error);
1360
1361bad:
1362	if (xp)
1363		vput(ITOV(xp));
1364	vput(ITOV(dp));
1365out:
1366	if (doingdirectory)
1367		ip->i_flag &= ~IN_RENAME;
1368	if (vn_lock(fvp, LK_EXCLUSIVE, td) == 0) {
1369		ip->i_effnlink--;
1370		ip->i_nlink--;
1371		DIP(ip, i_nlink) = ip->i_nlink;
1372		ip->i_flag |= IN_CHANGE;
1373		ip->i_flag &= ~IN_RENAME;
1374		if (DOINGSOFTDEP(fvp))
1375			softdep_change_linkcnt(ip);
1376		vput(fvp);
1377	} else
1378		vrele(fvp);
1379	return (error);
1380}
1381
1382/*
1383 * Mkdir system call
1384 */
1385int
1386ufs_mkdir(ap)
1387	struct vop_mkdir_args /* {
1388		struct vnode *a_dvp;
1389		struct vnode **a_vpp;
1390		struct componentname *a_cnp;
1391		struct vattr *a_vap;
1392	} */ *ap;
1393{
1394	struct vnode *dvp = ap->a_dvp;
1395	struct vattr *vap = ap->a_vap;
1396	struct componentname *cnp = ap->a_cnp;
1397	struct inode *ip, *dp;
1398	struct vnode *tvp;
1399	struct buf *bp;
1400	struct dirtemplate dirtemplate, *dtp;
1401	struct direct newdir;
1402#ifdef UFS_ACL
1403	struct acl *acl, *dacl;
1404#endif
1405	int error, dmode;
1406	long blkoff;
1407
1408#ifdef DIAGNOSTIC
1409	if ((cnp->cn_flags & HASBUF) == 0)
1410		panic("ufs_mkdir: no name");
1411#endif
1412	dp = VTOI(dvp);
1413	if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1414		error = EMLINK;
1415		goto out;
1416	}
1417	dmode = vap->va_mode & 0777;
1418	dmode |= IFDIR;
1419	/*
1420	 * Must simulate part of ufs_makeinode here to acquire the inode,
1421	 * but not have it entered in the parent directory. The entry is
1422	 * made later after writing "." and ".." entries.
1423	 */
1424	error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
1425	if (error)
1426		goto out;
1427	ip = VTOI(tvp);
1428	ip->i_gid = dp->i_gid;
1429	DIP(ip, i_gid) = dp->i_gid;
1430#ifdef SUIDDIR
1431	{
1432#ifdef QUOTA
1433		struct ucred ucred, *ucp;
1434		ucp = cnp->cn_cred;
1435#endif
1436		/*
1437		 * If we are hacking owners here, (only do this where told to)
1438		 * and we are not giving it TO root, (would subvert quotas)
1439		 * then go ahead and give it to the other user.
1440		 * The new directory also inherits the SUID bit.
1441		 * If user's UID and dir UID are the same,
1442		 * 'give it away' so that the SUID is still forced on.
1443		 */
1444		if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1445		    (dp->i_mode & ISUID) && dp->i_uid) {
1446			dmode |= ISUID;
1447			ip->i_uid = dp->i_uid;
1448			DIP(ip, i_uid) = dp->i_uid;
1449#ifdef QUOTA
1450			if (dp->i_uid != cnp->cn_cred->cr_uid) {
1451				/*
1452				 * Make sure the correct user gets charged
1453				 * for the space.
1454				 * Make a dummy credential for the victim.
1455				 * XXX This seems to never be accessed out of
1456				 * our context so a stack variable is ok.
1457				 */
1458				ucred.cr_ref = 1;
1459				ucred.cr_uid = ip->i_uid;
1460				ucred.cr_ngroups = 1;
1461				ucred.cr_groups[0] = dp->i_gid;
1462				ucp = &ucred;
1463			}
1464#endif
1465		} else {
1466			ip->i_uid = cnp->cn_cred->cr_uid;
1467			DIP(ip, i_uid) = ip->i_uid;
1468		}
1469#ifdef QUOTA
1470		if ((error = getinoquota(ip)) ||
1471	    	    (error = chkiq(ip, 1, ucp, 0))) {
1472			UFS_VFREE(tvp, ip->i_number, dmode);
1473			vput(tvp);
1474			return (error);
1475		}
1476#endif
1477	}
1478#else	/* !SUIDDIR */
1479	ip->i_uid = cnp->cn_cred->cr_uid;
1480	DIP(ip, i_uid) = ip->i_uid;
1481#ifdef QUOTA
1482	if ((error = getinoquota(ip)) ||
1483	    (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1484		UFS_VFREE(tvp, ip->i_number, dmode);
1485		vput(tvp);
1486		return (error);
1487	}
1488#endif
1489#endif	/* !SUIDDIR */
1490	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1491#ifdef UFS_ACL
1492	MALLOC(acl, struct acl *, sizeof(*acl), M_ACL, M_WAITOK);
1493	MALLOC(dacl, struct acl *, sizeof(*acl), M_ACL, M_WAITOK);
1494
1495	/*
1496	 * Retrieve default ACL from parent, if any.
1497	 */
1498	error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cnp->cn_cred,
1499	    cnp->cn_thread);
1500	switch (error) {
1501	case 0:
1502		/*
1503		 * Retrieved a default ACL, so merge mode and ACL if
1504		 * necessary.
1505		 */
1506		if (acl->acl_cnt != 0) {
1507			/*
1508			 * Two possible ways for default ACL to not be
1509			 * present.  First, the EA can be undefined,
1510			 * or second, the default ACL can be blank.
1511			 * If it's blank, fall through to the it's
1512			 * not defined case.
1513			 */
1514			ip->i_mode = dmode;
1515			DIP(ip, i_mode) = dmode;
1516			*dacl = *acl;
1517			ufs_sync_acl_from_inode(ip, acl);
1518			break;
1519		}
1520		/* FALLTHROUGH */
1521
1522	case EOPNOTSUPP:
1523		/*
1524		 * Just use the mode as-is.
1525		 */
1526		ip->i_mode = dmode;
1527		DIP(ip, i_mode) = dmode;
1528		FREE(acl, M_ACL);
1529		FREE(dacl, M_ACL);
1530		dacl = acl = NULL;
1531		break;
1532
1533	default:
1534		UFS_VFREE(tvp, ip->i_number, dmode);
1535		vput(tvp);
1536		return (error);
1537	}
1538#else /* !UFS_ACL */
1539	ip->i_mode = dmode;
1540	DIP(ip, i_mode) = dmode;
1541#endif /* !UFS_ACL */
1542	tvp->v_type = VDIR;	/* Rest init'd in getnewvnode(). */
1543	ip->i_effnlink = 2;
1544	ip->i_nlink = 2;
1545	DIP(ip, i_nlink) = 2;
1546	if (DOINGSOFTDEP(tvp))
1547		softdep_change_linkcnt(ip);
1548	if (cnp->cn_flags & ISWHITEOUT) {
1549		ip->i_flags |= UF_OPAQUE;
1550		DIP(ip, i_flags) = ip->i_flags;
1551	}
1552
1553	/*
1554	 * Bump link count in parent directory to reflect work done below.
1555	 * Should be done before reference is created so cleanup is
1556	 * possible if we crash.
1557	 */
1558	dp->i_effnlink++;
1559	dp->i_nlink++;
1560	DIP(dp, i_nlink) = dp->i_nlink;
1561	dp->i_flag |= IN_CHANGE;
1562	if (DOINGSOFTDEP(dvp))
1563		softdep_change_linkcnt(dp);
1564	error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1565	if (error)
1566		goto bad;
1567#ifdef MAC
1568	error = vop_stdcreatevnode_ea(dvp, tvp, cnp->cn_cred);
1569	if (error)
1570		goto bad;
1571#endif
1572#ifdef UFS_ACL
1573	if (acl != NULL) {
1574		/*
1575		 * XXX: If we abort now, will Soft Updates notify the extattr
1576		 * code that the EAs for the file need to be released?
1577		 */
1578		error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cnp->cn_cred,
1579		    cnp->cn_thread);
1580		if (error == 0)
1581			error = VOP_SETACL(tvp, ACL_TYPE_DEFAULT, dacl,
1582			    cnp->cn_cred, cnp->cn_thread);
1583		switch (error) {
1584		case 0:
1585			break;
1586
1587		case EOPNOTSUPP:
1588			/*
1589			 * XXX: This should not happen, as EOPNOTSUPP above
1590			 * was supposed to free acl.
1591			 */
1592			printf("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()\n");
1593			/*
1594			panic("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()");
1595			 */
1596			break;
1597
1598		default:
1599			FREE(acl, M_ACL);
1600			FREE(dacl, M_ACL);
1601			goto bad;
1602		}
1603		FREE(acl, M_ACL);
1604		FREE(dacl, M_ACL);
1605	}
1606#endif /* !UFS_ACL */
1607
1608	/*
1609	 * Initialize directory with "." and ".." from static template.
1610	 */
1611	if (dvp->v_mount->mnt_maxsymlinklen > 0
1612	)
1613		dtp = &mastertemplate;
1614	else
1615		dtp = (struct dirtemplate *)&omastertemplate;
1616	dirtemplate = *dtp;
1617	dirtemplate.dot_ino = ip->i_number;
1618	dirtemplate.dotdot_ino = dp->i_number;
1619	if ((error = UFS_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
1620	    BA_CLRBUF, &bp)) != 0)
1621		goto bad;
1622	ip->i_size = DIRBLKSIZ;
1623	DIP(ip, i_size) = DIRBLKSIZ;
1624	ip->i_flag |= IN_CHANGE | IN_UPDATE;
1625	vnode_pager_setsize(tvp, (u_long)ip->i_size);
1626	bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1627	if (DOINGSOFTDEP(tvp)) {
1628		/*
1629		 * Ensure that the entire newly allocated block is a
1630		 * valid directory so that future growth within the
1631		 * block does not have to ensure that the block is
1632		 * written before the inode.
1633		 */
1634		blkoff = DIRBLKSIZ;
1635		while (blkoff < bp->b_bcount) {
1636			((struct direct *)
1637			   (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1638			blkoff += DIRBLKSIZ;
1639		}
1640	}
1641	if ((error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) |
1642				       DOINGASYNC(tvp)))) != 0) {
1643		(void)BUF_WRITE(bp);
1644		goto bad;
1645	}
1646	/*
1647	 * Directory set up, now install its entry in the parent directory.
1648	 *
1649	 * If we are not doing soft dependencies, then we must write out the
1650	 * buffer containing the new directory body before entering the new
1651	 * name in the parent. If we are doing soft dependencies, then the
1652	 * buffer containing the new directory body will be passed to and
1653	 * released in the soft dependency code after the code has attached
1654	 * an appropriate ordering dependency to the buffer which ensures that
1655	 * the buffer is written before the new name is written in the parent.
1656	 */
1657	if (DOINGASYNC(dvp))
1658		bdwrite(bp);
1659	else if (!DOINGSOFTDEP(dvp) && ((error = BUF_WRITE(bp))))
1660		goto bad;
1661	ufs_makedirentry(ip, cnp, &newdir);
1662	error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1663
1664bad:
1665	if (error == 0) {
1666		VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1667		*ap->a_vpp = tvp;
1668	} else {
1669		dp->i_effnlink--;
1670		dp->i_nlink--;
1671		DIP(dp, i_nlink) = dp->i_nlink;
1672		dp->i_flag |= IN_CHANGE;
1673		if (DOINGSOFTDEP(dvp))
1674			softdep_change_linkcnt(dp);
1675		/*
1676		 * No need to do an explicit VOP_TRUNCATE here, vrele will
1677		 * do this for us because we set the link count to 0.
1678		 */
1679		ip->i_effnlink = 0;
1680		ip->i_nlink = 0;
1681		DIP(ip, i_nlink) = 0;
1682		ip->i_flag |= IN_CHANGE;
1683		if (DOINGSOFTDEP(tvp))
1684			softdep_change_linkcnt(ip);
1685		vput(tvp);
1686	}
1687out:
1688	return (error);
1689}
1690
1691/*
1692 * Rmdir system call.
1693 */
1694int
1695ufs_rmdir(ap)
1696	struct vop_rmdir_args /* {
1697		struct vnode *a_dvp;
1698		struct vnode *a_vp;
1699		struct componentname *a_cnp;
1700	} */ *ap;
1701{
1702	struct vnode *vp = ap->a_vp;
1703	struct vnode *dvp = ap->a_dvp;
1704	struct componentname *cnp = ap->a_cnp;
1705	struct inode *ip, *dp;
1706	int error, ioflag;
1707
1708	ip = VTOI(vp);
1709	dp = VTOI(dvp);
1710
1711	/*
1712	 * Do not remove a directory that is in the process of being renamed.
1713	 * Verify the directory is empty (and valid). Rmdir ".." will not be
1714	 * valid since ".." will contain a reference to the current directory
1715	 * and thus be non-empty. Do not allow the removal of mounted on
1716	 * directories (this can happen when an NFS exported filesystem
1717	 * tries to remove a locally mounted on directory).
1718	 */
1719	error = 0;
1720	if (ip->i_flag & IN_RENAME) {
1721		error = EINVAL;
1722		goto out;
1723	}
1724	if (ip->i_effnlink != 2 ||
1725	    !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1726		error = ENOTEMPTY;
1727		goto out;
1728	}
1729	if ((dp->i_flags & APPEND)
1730	    || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1731		error = EPERM;
1732		goto out;
1733	}
1734	if (vp->v_mountedhere != 0) {
1735		error = EINVAL;
1736		goto out;
1737	}
1738	/*
1739	 * Delete reference to directory before purging
1740	 * inode.  If we crash in between, the directory
1741	 * will be reattached to lost+found,
1742	 */
1743	dp->i_effnlink--;
1744	ip->i_effnlink--;
1745	if (DOINGSOFTDEP(vp)) {
1746		softdep_change_linkcnt(dp);
1747		softdep_change_linkcnt(ip);
1748	}
1749	error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1750	if (error) {
1751		dp->i_effnlink++;
1752		ip->i_effnlink++;
1753		if (DOINGSOFTDEP(vp)) {
1754			softdep_change_linkcnt(dp);
1755			softdep_change_linkcnt(ip);
1756		}
1757		goto out;
1758	}
1759	VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1760	cache_purge(dvp);
1761	/*
1762	 * Truncate inode. The only stuff left in the directory is "." and
1763	 * "..". The "." reference is inconsequential since we are quashing
1764	 * it. The soft dependency code will arrange to do these operations
1765	 * after the parent directory entry has been deleted on disk, so
1766	 * when running with that code we avoid doing them now.
1767	 */
1768	if (!DOINGSOFTDEP(vp)) {
1769		dp->i_nlink--;
1770		DIP(dp, i_nlink) = dp->i_nlink;
1771		dp->i_flag |= IN_CHANGE;
1772		ip->i_nlink--;
1773		DIP(ip, i_nlink) = ip->i_nlink;
1774		ip->i_flag |= IN_CHANGE;
1775		ioflag = IO_NORMAL;
1776		if (DOINGASYNC(vp))
1777			ioflag |= IO_SYNC;
1778		error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred,
1779		    cnp->cn_thread);
1780	}
1781	cache_purge(vp);
1782#ifdef UFS_DIRHASH
1783	/* Kill any active hash; i_effnlink == 0, so it will not come back. */
1784	if (ip->i_dirhash != NULL)
1785		ufsdirhash_free(ip);
1786#endif
1787out:
1788	VN_KNOTE(vp, NOTE_DELETE);
1789	return (error);
1790}
1791
1792/*
1793 * symlink -- make a symbolic link
1794 */
1795int
1796ufs_symlink(ap)
1797	struct vop_symlink_args /* {
1798		struct vnode *a_dvp;
1799		struct vnode **a_vpp;
1800		struct componentname *a_cnp;
1801		struct vattr *a_vap;
1802		char *a_target;
1803	} */ *ap;
1804{
1805	struct vnode *vp, **vpp = ap->a_vpp;
1806	struct inode *ip;
1807	int len, error;
1808
1809	error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1810	    vpp, ap->a_cnp);
1811	if (error)
1812		return (error);
1813	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1814	vp = *vpp;
1815	len = strlen(ap->a_target);
1816	if (len < vp->v_mount->mnt_maxsymlinklen) {
1817		ip = VTOI(vp);
1818		bcopy(ap->a_target, SHORTLINK(ip), len);
1819		ip->i_size = len;
1820		DIP(ip, i_size) = len;
1821		ip->i_flag |= IN_CHANGE | IN_UPDATE;
1822	} else
1823		error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1824		    UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1825		    ap->a_cnp->cn_cred, (int *)0, (struct thread *)0);
1826	if (error)
1827		vput(vp);
1828	return (error);
1829}
1830
1831/*
1832 * Vnode op for reading directories.
1833 *
1834 * The routine below assumes that the on-disk format of a directory
1835 * is the same as that defined by <sys/dirent.h>. If the on-disk
1836 * format changes, then it will be necessary to do a conversion
1837 * from the on-disk format that read returns to the format defined
1838 * by <sys/dirent.h>.
1839 */
1840int
1841ufs_readdir(ap)
1842	struct vop_readdir_args /* {
1843		struct vnode *a_vp;
1844		struct uio *a_uio;
1845		struct ucred *a_cred;
1846		int *a_eofflag;
1847		int *ncookies;
1848		u_long **a_cookies;
1849	} */ *ap;
1850{
1851	struct uio *uio = ap->a_uio;
1852	int error;
1853	size_t count, lost;
1854	off_t off;
1855
1856	if (ap->a_ncookies != NULL)
1857		/*
1858		 * Ensure that the block is aligned.  The caller can use
1859		 * the cookies to determine where in the block to start.
1860		 */
1861		uio->uio_offset &= ~(DIRBLKSIZ - 1);
1862	off = uio->uio_offset;
1863	count = uio->uio_resid;
1864	/* Make sure we don't return partial entries. */
1865	if (count <= ((uio->uio_offset + count) & (DIRBLKSIZ -1)))
1866		return (EINVAL);
1867	count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1868	lost = uio->uio_resid - count;
1869	uio->uio_resid = count;
1870	uio->uio_iov->iov_len = count;
1871#	if (BYTE_ORDER == LITTLE_ENDIAN)
1872		if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
1873			error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1874		} else {
1875			struct dirent *dp, *edp;
1876			struct uio auio;
1877			struct iovec aiov;
1878			caddr_t dirbuf;
1879			int readcnt;
1880			u_char tmp;
1881
1882			auio = *uio;
1883			auio.uio_iov = &aiov;
1884			auio.uio_iovcnt = 1;
1885			auio.uio_segflg = UIO_SYSSPACE;
1886			aiov.iov_len = count;
1887			MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
1888			aiov.iov_base = dirbuf;
1889			error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
1890			if (error == 0) {
1891				readcnt = count - auio.uio_resid;
1892				edp = (struct dirent *)&dirbuf[readcnt];
1893				for (dp = (struct dirent *)dirbuf; dp < edp; ) {
1894					tmp = dp->d_namlen;
1895					dp->d_namlen = dp->d_type;
1896					dp->d_type = tmp;
1897					if (dp->d_reclen > 0) {
1898						dp = (struct dirent *)
1899						    ((char *)dp + dp->d_reclen);
1900					} else {
1901						error = EIO;
1902						break;
1903					}
1904				}
1905				if (dp >= edp)
1906					error = uiomove(dirbuf, readcnt, uio);
1907			}
1908			FREE(dirbuf, M_TEMP);
1909		}
1910#	else
1911		error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1912#	endif
1913	if (!error && ap->a_ncookies != NULL) {
1914		struct dirent* dpStart;
1915		struct dirent* dpEnd;
1916		struct dirent* dp;
1917		int ncookies;
1918		u_long *cookies;
1919		u_long *cookiep;
1920
1921		if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1922			panic("ufs_readdir: unexpected uio from NFS server");
1923		dpStart = (struct dirent *)
1924		     (uio->uio_iov->iov_base - (uio->uio_offset - off));
1925		dpEnd = (struct dirent *) uio->uio_iov->iov_base;
1926		for (dp = dpStart, ncookies = 0;
1927		     dp < dpEnd;
1928		     dp = (struct dirent *)((caddr_t) dp + dp->d_reclen))
1929			ncookies++;
1930		MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1931		    M_WAITOK);
1932		for (dp = dpStart, cookiep = cookies;
1933		     dp < dpEnd;
1934		     dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
1935			off += dp->d_reclen;
1936			*cookiep++ = (u_long) off;
1937		}
1938		*ap->a_ncookies = ncookies;
1939		*ap->a_cookies = cookies;
1940	}
1941	uio->uio_resid += lost;
1942	if (ap->a_eofflag)
1943	    *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
1944	return (error);
1945}
1946
1947/*
1948 * Return target name of a symbolic link
1949 */
1950int
1951ufs_readlink(ap)
1952	struct vop_readlink_args /* {
1953		struct vnode *a_vp;
1954		struct uio *a_uio;
1955		struct ucred *a_cred;
1956	} */ *ap;
1957{
1958	struct vnode *vp = ap->a_vp;
1959	struct inode *ip = VTOI(vp);
1960	doff_t isize;
1961
1962	isize = ip->i_size;
1963	if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1964	    DIP(ip, i_blocks) == 0) { /* XXX - for old fastlink support */
1965		uiomove(SHORTLINK(ip), isize, ap->a_uio);
1966		return (0);
1967	}
1968	return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1969}
1970
1971/*
1972 * Calculate the logical to physical mapping if not done already,
1973 * then call the device strategy routine.
1974 *
1975 * In order to be able to swap to a file, the ufs_bmaparray() operation may not
1976 * deadlock on memory.  See ufs_bmap() for details.
1977 */
1978int
1979ufs_strategy(ap)
1980	struct vop_strategy_args /* {
1981		struct vnode *a_vp;
1982		struct buf *a_bp;
1983	} */ *ap;
1984{
1985	struct buf *bp = ap->a_bp;
1986	struct vnode *vp = ap->a_vp;
1987	struct inode *ip;
1988	ufs2_daddr_t blkno;
1989	int error;
1990
1991	ip = VTOI(vp);
1992	if (vp->v_type == VBLK || vp->v_type == VCHR)
1993		panic("ufs_strategy: spec");
1994	if (bp->b_blkno == bp->b_lblkno) {
1995		error = ufs_bmaparray(vp, bp->b_lblkno, &blkno, bp, NULL, NULL);
1996		bp->b_blkno = blkno;
1997		if (error) {
1998			bp->b_error = error;
1999			bp->b_ioflags |= BIO_ERROR;
2000			bufdone(bp);
2001			return (error);
2002		}
2003		if ((long)bp->b_blkno == -1)
2004			vfs_bio_clrbuf(bp);
2005	}
2006	if ((long)bp->b_blkno == -1) {
2007		bufdone(bp);
2008		return (0);
2009	}
2010	vp = ip->i_devvp;
2011	bp->b_dev = vp->v_rdev;
2012	VOP_STRATEGY(vp, bp);
2013	return (0);
2014}
2015
2016/*
2017 * Print out the contents of an inode.
2018 */
2019int
2020ufs_print(ap)
2021	struct vop_print_args /* {
2022		struct vnode *a_vp;
2023	} */ *ap;
2024{
2025	struct vnode *vp = ap->a_vp;
2026	struct inode *ip = VTOI(vp);
2027
2028	printf("tag VT_UFS, ino %lu, on dev %s (%d, %d)",
2029	    (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev),
2030	    minor(ip->i_dev));
2031	if (vp->v_type == VFIFO)
2032		fifo_printinfo(vp);
2033	lockmgr_printinfo(&vp->v_lock);
2034	printf("\n");
2035	return (0);
2036}
2037
2038/*
2039 * Read wrapper for special devices.
2040 */
2041int
2042ufsspec_read(ap)
2043	struct vop_read_args /* {
2044		struct vnode *a_vp;
2045		struct uio *a_uio;
2046		int  a_ioflag;
2047		struct ucred *a_cred;
2048	} */ *ap;
2049{
2050	int error, resid;
2051	struct inode *ip;
2052	struct uio *uio;
2053
2054	uio = ap->a_uio;
2055	resid = uio->uio_resid;
2056	error = VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap);
2057	/*
2058	 * The inode may have been revoked during the call, so it must not
2059	 * be accessed blindly here or in the other wrapper functions.
2060	 */
2061	ip = VTOI(ap->a_vp);
2062	if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
2063		ip->i_flag |= IN_ACCESS;
2064	return (error);
2065}
2066
2067/*
2068 * Write wrapper for special devices.
2069 */
2070int
2071ufsspec_write(ap)
2072	struct vop_write_args /* {
2073		struct vnode *a_vp;
2074		struct uio *a_uio;
2075		int  a_ioflag;
2076		struct ucred *a_cred;
2077	} */ *ap;
2078{
2079	int error, resid;
2080	struct inode *ip;
2081	struct uio *uio;
2082
2083	uio = ap->a_uio;
2084	resid = uio->uio_resid;
2085	error = VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap);
2086	ip = VTOI(ap->a_vp);
2087	if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
2088		VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
2089	return (error);
2090}
2091
2092/*
2093 * Close wrapper for special devices.
2094 *
2095 * Update the times on the inode then do device close.
2096 */
2097int
2098ufsspec_close(ap)
2099	struct vop_close_args /* {
2100		struct vnode *a_vp;
2101		int  a_fflag;
2102		struct ucred *a_cred;
2103		struct thread *a_td;
2104	} */ *ap;
2105{
2106	struct vnode *vp = ap->a_vp;
2107
2108	mtx_lock(&vp->v_interlock);
2109	if (vp->v_usecount > 1)
2110		ufs_itimes(vp);
2111	mtx_unlock(&vp->v_interlock);
2112	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
2113}
2114
2115/*
2116 * Read wrapper for fifos.
2117 */
2118int
2119ufsfifo_read(ap)
2120	struct vop_read_args /* {
2121		struct vnode *a_vp;
2122		struct uio *a_uio;
2123		int  a_ioflag;
2124		struct ucred *a_cred;
2125	} */ *ap;
2126{
2127	int error, resid;
2128	struct inode *ip;
2129	struct uio *uio;
2130
2131	uio = ap->a_uio;
2132	resid = uio->uio_resid;
2133	error = VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap);
2134	ip = VTOI(ap->a_vp);
2135	if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL &&
2136	    (uio->uio_resid != resid || (error == 0 && resid != 0)))
2137		VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
2138	return (error);
2139}
2140
2141/*
2142 * Write wrapper for fifos.
2143 */
2144int
2145ufsfifo_write(ap)
2146	struct vop_write_args /* {
2147		struct vnode *a_vp;
2148		struct uio *a_uio;
2149		int  a_ioflag;
2150		struct ucred *a_cred;
2151	} */ *ap;
2152{
2153	int error, resid;
2154	struct inode *ip;
2155	struct uio *uio;
2156
2157	uio = ap->a_uio;
2158	resid = uio->uio_resid;
2159	error = VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap);
2160	ip = VTOI(ap->a_vp);
2161	if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
2162		VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
2163	return (error);
2164}
2165
2166/*
2167 * Close wrapper for fifos.
2168 *
2169 * Update the times on the inode then do device close.
2170 */
2171int
2172ufsfifo_close(ap)
2173	struct vop_close_args /* {
2174		struct vnode *a_vp;
2175		int  a_fflag;
2176		struct ucred *a_cred;
2177		struct thread *a_td;
2178	} */ *ap;
2179{
2180	struct vnode *vp = ap->a_vp;
2181
2182	mtx_lock(&vp->v_interlock);
2183	if (vp->v_usecount > 1)
2184		ufs_itimes(vp);
2185	mtx_unlock(&vp->v_interlock);
2186	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
2187}
2188
2189/*
2190 * Kqfilter wrapper for fifos.
2191 *
2192 * Fall through to ufs kqfilter routines if needed
2193 */
2194int
2195ufsfifo_kqfilter(ap)
2196	struct vop_kqfilter_args *ap;
2197{
2198	int error;
2199
2200	error = VOCALL(fifo_vnodeop_p, VOFFSET(vop_kqfilter), ap);
2201	if (error)
2202		error = ufs_kqfilter(ap);
2203	return (error);
2204}
2205
2206/*
2207 * Return POSIX pathconf information applicable to ufs filesystems.
2208 */
2209int
2210ufs_pathconf(ap)
2211	struct vop_pathconf_args /* {
2212		struct vnode *a_vp;
2213		int a_name;
2214		int *a_retval;
2215	} */ *ap;
2216{
2217
2218	switch (ap->a_name) {
2219	case _PC_LINK_MAX:
2220		*ap->a_retval = LINK_MAX;
2221		return (0);
2222	case _PC_NAME_MAX:
2223		*ap->a_retval = NAME_MAX;
2224		return (0);
2225	case _PC_PATH_MAX:
2226		*ap->a_retval = PATH_MAX;
2227		return (0);
2228	case _PC_PIPE_BUF:
2229		*ap->a_retval = PIPE_BUF;
2230		return (0);
2231	case _PC_CHOWN_RESTRICTED:
2232		*ap->a_retval = 1;
2233		return (0);
2234	case _PC_NO_TRUNC:
2235		*ap->a_retval = 1;
2236		return (0);
2237	default:
2238		return (EINVAL);
2239	}
2240	/* NOTREACHED */
2241}
2242
2243/*
2244 * Advisory record locking support
2245 */
2246int
2247ufs_advlock(ap)
2248	struct vop_advlock_args /* {
2249		struct vnode *a_vp;
2250		caddr_t  a_id;
2251		int  a_op;
2252		struct flock *a_fl;
2253		int  a_flags;
2254	} */ *ap;
2255{
2256	struct inode *ip = VTOI(ap->a_vp);
2257
2258	return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
2259}
2260
2261/*
2262 * Initialize the vnode associated with a new inode, handle aliased
2263 * vnodes.
2264 */
2265int
2266ufs_vinit(mntp, specops, fifoops, vpp)
2267	struct mount *mntp;
2268	vop_t **specops;
2269	vop_t **fifoops;
2270	struct vnode **vpp;
2271{
2272	struct inode *ip;
2273	struct vnode *vp;
2274	struct timeval tv;
2275
2276	vp = *vpp;
2277	ip = VTOI(vp);
2278	switch(vp->v_type = IFTOVT(ip->i_mode)) {
2279	case VCHR:
2280	case VBLK:
2281		vp->v_op = specops;
2282		vp = addaliasu(vp, DIP(ip, i_rdev));
2283		ip->i_vnode = vp;
2284		break;
2285	case VFIFO:
2286		vp->v_op = fifoops;
2287		break;
2288	default:
2289		break;
2290
2291	}
2292	ASSERT_VOP_LOCKED(vp, "ufs_vinit");
2293	if (ip->i_number == ROOTINO)
2294		vp->v_vflag |= VV_ROOT;
2295	/*
2296	 * Initialize modrev times
2297	 */
2298	getmicrouptime(&tv);
2299	SETHIGH(ip->i_modrev, tv.tv_sec);
2300	SETLOW(ip->i_modrev, tv.tv_usec * 4294);
2301	*vpp = vp;
2302	return (0);
2303}
2304
2305/*
2306 * Allocate a new inode.
2307 * Vnode dvp must be locked.
2308 */
2309int
2310ufs_makeinode(mode, dvp, vpp, cnp)
2311	int mode;
2312	struct vnode *dvp;
2313	struct vnode **vpp;
2314	struct componentname *cnp;
2315{
2316	struct inode *ip, *pdir;
2317	struct direct newdir;
2318	struct vnode *tvp;
2319#ifdef UFS_ACL
2320	struct acl *acl;
2321#endif
2322	int error;
2323
2324	pdir = VTOI(dvp);
2325#ifdef DIAGNOSTIC
2326	if ((cnp->cn_flags & HASBUF) == 0)
2327		panic("ufs_makeinode: no name");
2328#endif
2329	*vpp = NULL;
2330	if ((mode & IFMT) == 0)
2331		mode |= IFREG;
2332
2333	error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2334	if (error)
2335		return (error);
2336	ip = VTOI(tvp);
2337	ip->i_gid = pdir->i_gid;
2338	DIP(ip, i_gid) = pdir->i_gid;
2339#ifdef SUIDDIR
2340	{
2341#ifdef QUOTA
2342		struct ucred ucred, *ucp;
2343		ucp = cnp->cn_cred;
2344#endif
2345		/*
2346		 * If we are not the owner of the directory,
2347		 * and we are hacking owners here, (only do this where told to)
2348		 * and we are not giving it TO root, (would subvert quotas)
2349		 * then go ahead and give it to the other user.
2350		 * Note that this drops off the execute bits for security.
2351		 */
2352		if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2353		    (pdir->i_mode & ISUID) &&
2354		    (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2355			ip->i_uid = pdir->i_uid;
2356			DIP(ip, i_uid) = ip->i_uid;
2357			mode &= ~07111;
2358#ifdef QUOTA
2359			/*
2360			 * Make sure the correct user gets charged
2361			 * for the space.
2362			 * Quickly knock up a dummy credential for the victim.
2363			 * XXX This seems to never be accessed out of our
2364			 * context so a stack variable is ok.
2365			 */
2366			ucred.cr_ref = 1;
2367			ucred.cr_uid = ip->i_uid;
2368			ucred.cr_ngroups = 1;
2369			ucred.cr_groups[0] = pdir->i_gid;
2370			ucp = &ucred;
2371#endif
2372		} else {
2373			ip->i_uid = cnp->cn_cred->cr_uid;
2374			DIP(ip, i_uid) = ip->i_uid;
2375		}
2376
2377#ifdef QUOTA
2378		if ((error = getinoquota(ip)) ||
2379	    	    (error = chkiq(ip, 1, ucp, 0))) {
2380			UFS_VFREE(tvp, ip->i_number, mode);
2381			vput(tvp);
2382			return (error);
2383		}
2384#endif
2385	}
2386#else	/* !SUIDDIR */
2387	ip->i_uid = cnp->cn_cred->cr_uid;
2388	DIP(ip, i_uid) = ip->i_uid;
2389#ifdef QUOTA
2390	if ((error = getinoquota(ip)) ||
2391	    (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2392		UFS_VFREE(tvp, ip->i_number, mode);
2393		vput(tvp);
2394		return (error);
2395	}
2396#endif
2397#endif	/* !SUIDDIR */
2398	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2399#ifdef UFS_ACL
2400	MALLOC(acl, struct acl *, sizeof(*acl), M_ACL, M_WAITOK);
2401	/*
2402	 * Retrieve default ACL for parent, if any.
2403	 */
2404	error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cnp->cn_cred,
2405	    cnp->cn_thread);
2406	switch (error) {
2407	case 0:
2408		/*
2409		 * Retrieved a default ACL, so merge mode and ACL if
2410		 * necessary.
2411		 */
2412		if (acl->acl_cnt != 0) {
2413			/*
2414			 * Two possible ways for default ACL to not be
2415			 * present.  First, the EA can be undefined,
2416			 * or second, the default ACL can be blank.
2417			 * If it's blank, fall through to the it's
2418			 * not defined case.
2419			 */
2420			ip->i_mode = mode;
2421			DIP(ip, i_mode) = mode;
2422			ufs_sync_acl_from_inode(ip, acl);
2423			break;
2424		}
2425
2426	case EOPNOTSUPP:
2427		/*
2428		 * Just use the mode as-is.
2429		 */
2430		ip->i_mode = mode;
2431		DIP(ip, i_mode) = mode;
2432		FREE(acl, M_ACL);
2433		acl = NULL;
2434		break;
2435
2436	default:
2437		UFS_VFREE(tvp, ip->i_number, mode);
2438		vput(tvp);
2439		return (error);
2440	}
2441#else /* !UFS_ACL */
2442	ip->i_mode = mode;
2443	DIP(ip, i_mode) = mode;
2444#endif /* !UFS_ACL */
2445	tvp->v_type = IFTOVT(mode);	/* Rest init'd in getnewvnode(). */
2446	ip->i_effnlink = 1;
2447	ip->i_nlink = 1;
2448	DIP(ip, i_nlink) = 1;
2449	if (DOINGSOFTDEP(tvp))
2450		softdep_change_linkcnt(ip);
2451	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2452	    suser_cred(cnp->cn_cred, PRISON_ROOT)) {
2453		ip->i_mode &= ~ISGID;
2454		DIP(ip, i_mode) = ip->i_mode;
2455	}
2456
2457	if (cnp->cn_flags & ISWHITEOUT) {
2458		ip->i_flags |= UF_OPAQUE;
2459		DIP(ip, i_flags) = ip->i_flags;
2460	}
2461
2462	/*
2463	 * Make sure inode goes to disk before directory entry.
2464	 */
2465	error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2466	if (error)
2467		goto bad;
2468#ifdef MAC
2469	error = vop_stdcreatevnode_ea(dvp, tvp, cnp->cn_cred);
2470	if (error)
2471		goto bad;
2472#endif
2473#ifdef UFS_ACL
2474	if (acl != NULL) {
2475		/*
2476		 * XXX: If we abort now, will Soft Updates notify the extattr
2477		 * code that the EAs for the file need to be released?
2478		 */
2479		error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cnp->cn_cred,
2480		    cnp->cn_thread);
2481		switch (error) {
2482		case 0:
2483			break;
2484
2485		case EOPNOTSUPP:
2486			/*
2487			 * XXX: This should not happen, as EOPNOTSUPP above was
2488			 * supposed to free acl.
2489			 */
2490			printf("ufs_makeinode: VOP_GETACL() but no "
2491			    "VOP_SETACL()\n");
2492			/* panic("ufs_makeinode: VOP_GETACL() but no "
2493			    "VOP_SETACL()"); */
2494			break;
2495
2496		default:
2497			FREE(acl, M_ACL);
2498			goto bad;
2499		}
2500		FREE(acl, M_ACL);
2501	}
2502#endif /* !UFS_ACL */
2503	ufs_makedirentry(ip, cnp, &newdir);
2504	error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2505	if (error)
2506		goto bad;
2507	*vpp = tvp;
2508	return (0);
2509
2510bad:
2511	/*
2512	 * Write error occurred trying to update the inode
2513	 * or the directory so must deallocate the inode.
2514	 */
2515	ip->i_effnlink = 0;
2516	ip->i_nlink = 0;
2517	DIP(ip, i_nlink) = 0;
2518	ip->i_flag |= IN_CHANGE;
2519	if (DOINGSOFTDEP(tvp))
2520		softdep_change_linkcnt(ip);
2521	vput(tvp);
2522	return (error);
2523}
2524
2525static struct filterops ufsread_filtops =
2526	{ 1, NULL, filt_ufsdetach, filt_ufsread };
2527static struct filterops ufswrite_filtops =
2528	{ 1, NULL, filt_ufsdetach, filt_ufswrite };
2529static struct filterops ufsvnode_filtops =
2530	{ 1, NULL, filt_ufsdetach, filt_ufsvnode };
2531
2532static int
2533ufs_kqfilter(ap)
2534	struct vop_kqfilter_args /* {
2535		struct vnode *a_vp;
2536		struct knote *a_kn;
2537	} */ *ap;
2538{
2539	struct vnode *vp = ap->a_vp;
2540	struct knote *kn = ap->a_kn;
2541
2542	switch (kn->kn_filter) {
2543	case EVFILT_READ:
2544		kn->kn_fop = &ufsread_filtops;
2545		break;
2546	case EVFILT_WRITE:
2547		kn->kn_fop = &ufswrite_filtops;
2548		break;
2549	case EVFILT_VNODE:
2550		kn->kn_fop = &ufsvnode_filtops;
2551		break;
2552	default:
2553		return (1);
2554	}
2555
2556	kn->kn_hook = (caddr_t)vp;
2557
2558	if (vp->v_pollinfo == NULL)
2559		v_addpollinfo(vp);
2560	mtx_lock(&vp->v_pollinfo->vpi_lock);
2561	SLIST_INSERT_HEAD(&vp->v_pollinfo->vpi_selinfo.si_note, kn, kn_selnext);
2562	mtx_unlock(&vp->v_pollinfo->vpi_lock);
2563
2564	return (0);
2565}
2566
2567static void
2568filt_ufsdetach(struct knote *kn)
2569{
2570	struct vnode *vp = (struct vnode *)kn->kn_hook;
2571
2572	KASSERT(vp->v_pollinfo != NULL, ("Mising v_pollinfo"));
2573	mtx_lock(&vp->v_pollinfo->vpi_lock);
2574	SLIST_REMOVE(&vp->v_pollinfo->vpi_selinfo.si_note,
2575	    kn, knote, kn_selnext);
2576	mtx_unlock(&vp->v_pollinfo->vpi_lock);
2577}
2578
2579/*ARGSUSED*/
2580static int
2581filt_ufsread(struct knote *kn, long hint)
2582{
2583	struct vnode *vp = (struct vnode *)kn->kn_hook;
2584	struct inode *ip = VTOI(vp);
2585
2586	/*
2587	 * filesystem is gone, so set the EOF flag and schedule
2588	 * the knote for deletion.
2589	 */
2590	if (hint == NOTE_REVOKE) {
2591		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2592		return (1);
2593	}
2594
2595        kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
2596        return (kn->kn_data != 0);
2597}
2598
2599/*ARGSUSED*/
2600static int
2601filt_ufswrite(struct knote *kn, long hint)
2602{
2603
2604	/*
2605	 * filesystem is gone, so set the EOF flag and schedule
2606	 * the knote for deletion.
2607	 */
2608	if (hint == NOTE_REVOKE)
2609		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2610
2611        kn->kn_data = 0;
2612        return (1);
2613}
2614
2615static int
2616filt_ufsvnode(struct knote *kn, long hint)
2617{
2618
2619	if (kn->kn_sfflags & hint)
2620		kn->kn_fflags |= hint;
2621	if (hint == NOTE_REVOKE) {
2622		kn->kn_flags |= EV_EOF;
2623		return (1);
2624	}
2625	return (kn->kn_fflags != 0);
2626}
2627
2628/* Global vfs data structures for ufs. */
2629static vop_t **ufs_vnodeop_p;
2630static struct vnodeopv_entry_desc ufs_vnodeop_entries[] = {
2631	{ &vop_default_desc,		(vop_t *) vop_defaultop },
2632	{ &vop_fsync_desc,		(vop_t *) vop_panic },
2633	{ &vop_read_desc,		(vop_t *) vop_panic },
2634	{ &vop_reallocblks_desc,	(vop_t *) vop_panic },
2635	{ &vop_write_desc,		(vop_t *) vop_panic },
2636	{ &vop_access_desc,		(vop_t *) ufs_access },
2637	{ &vop_advlock_desc,		(vop_t *) ufs_advlock },
2638	{ &vop_bmap_desc,		(vop_t *) ufs_bmap },
2639	{ &vop_cachedlookup_desc,	(vop_t *) ufs_lookup },
2640	{ &vop_close_desc,		(vop_t *) ufs_close },
2641	{ &vop_create_desc,		(vop_t *) ufs_create },
2642	{ &vop_getattr_desc,		(vop_t *) ufs_getattr },
2643	{ &vop_inactive_desc,		(vop_t *) ufs_inactive },
2644	{ &vop_islocked_desc,		(vop_t *) vop_stdislocked },
2645	{ &vop_link_desc,		(vop_t *) ufs_link },
2646	{ &vop_lock_desc,		(vop_t *) vop_stdlock },
2647	{ &vop_lookup_desc,		(vop_t *) vfs_cache_lookup },
2648	{ &vop_mkdir_desc,		(vop_t *) ufs_mkdir },
2649	{ &vop_mknod_desc,		(vop_t *) ufs_mknod },
2650	{ &vop_open_desc,		(vop_t *) ufs_open },
2651	{ &vop_pathconf_desc,		(vop_t *) ufs_pathconf },
2652	{ &vop_poll_desc,		(vop_t *) vop_stdpoll },
2653	{ &vop_kqfilter_desc,		(vop_t *) ufs_kqfilter },
2654	{ &vop_getwritemount_desc, 	(vop_t *) vop_stdgetwritemount },
2655	{ &vop_print_desc,		(vop_t *) ufs_print },
2656	{ &vop_readdir_desc,		(vop_t *) ufs_readdir },
2657	{ &vop_readlink_desc,		(vop_t *) ufs_readlink },
2658	{ &vop_reclaim_desc,		(vop_t *) ufs_reclaim },
2659#ifdef MAC
2660	{ &vop_refreshlabel_desc,	(vop_t *) vop_stdrefreshlabel_ea },
2661#endif
2662	{ &vop_remove_desc,		(vop_t *) ufs_remove },
2663	{ &vop_rename_desc,		(vop_t *) ufs_rename },
2664	{ &vop_rmdir_desc,		(vop_t *) ufs_rmdir },
2665	{ &vop_setattr_desc,		(vop_t *) ufs_setattr },
2666#ifdef MAC
2667	{ &vop_setlabel_desc,		(vop_t *) vop_stdsetlabel_ea },
2668#endif
2669	{ &vop_strategy_desc,		(vop_t *) ufs_strategy },
2670	{ &vop_symlink_desc,		(vop_t *) ufs_symlink },
2671	{ &vop_unlock_desc,		(vop_t *) vop_stdunlock },
2672	{ &vop_whiteout_desc,		(vop_t *) ufs_whiteout },
2673#ifdef UFS_EXTATTR
2674	{ &vop_getextattr_desc,		(vop_t *) ufs_getextattr },
2675	{ &vop_setextattr_desc,		(vop_t *) ufs_setextattr },
2676#endif
2677#ifdef UFS_ACL
2678	{ &vop_getacl_desc,		(vop_t *) ufs_getacl },
2679	{ &vop_setacl_desc,		(vop_t *) ufs_setacl },
2680	{ &vop_aclcheck_desc,		(vop_t *) ufs_aclcheck },
2681#endif
2682	{ NULL, NULL }
2683};
2684static struct vnodeopv_desc ufs_vnodeop_opv_desc =
2685	{ &ufs_vnodeop_p, ufs_vnodeop_entries };
2686
2687static vop_t **ufs_specop_p;
2688static struct vnodeopv_entry_desc ufs_specop_entries[] = {
2689	{ &vop_default_desc,		(vop_t *) spec_vnoperate },
2690	{ &vop_fsync_desc,		(vop_t *) vop_panic },
2691	{ &vop_access_desc,		(vop_t *) ufs_access },
2692	{ &vop_close_desc,		(vop_t *) ufsspec_close },
2693	{ &vop_getattr_desc,		(vop_t *) ufs_getattr },
2694	{ &vop_inactive_desc,		(vop_t *) ufs_inactive },
2695	{ &vop_islocked_desc,		(vop_t *) vop_stdislocked },
2696	{ &vop_lock_desc,		(vop_t *) vop_stdlock },
2697	{ &vop_print_desc,		(vop_t *) ufs_print },
2698	{ &vop_read_desc,		(vop_t *) ufsspec_read },
2699	{ &vop_reclaim_desc,		(vop_t *) ufs_reclaim },
2700#ifdef MAC
2701	{ &vop_refreshlabel_desc,	(vop_t *) vop_stdrefreshlabel_ea },
2702#endif
2703	{ &vop_setattr_desc,		(vop_t *) ufs_setattr },
2704#ifdef MAC
2705	{ &vop_setlabel_desc,		(vop_t *) vop_stdsetlabel_ea },
2706#endif
2707	{ &vop_unlock_desc,		(vop_t *) vop_stdunlock },
2708	{ &vop_write_desc,		(vop_t *) ufsspec_write },
2709#ifdef UFS_EXTATTR
2710	{ &vop_getextattr_desc,		(vop_t *) ufs_getextattr },
2711	{ &vop_setextattr_desc,		(vop_t *) ufs_setextattr },
2712#endif
2713#ifdef UFS_ACL
2714	{ &vop_getacl_desc,		(vop_t *) ufs_getacl },
2715	{ &vop_setacl_desc,		(vop_t *) ufs_setacl },
2716	{ &vop_aclcheck_desc,		(vop_t *) ufs_aclcheck },
2717#endif
2718	{NULL, NULL}
2719};
2720static struct vnodeopv_desc ufs_specop_opv_desc =
2721	{ &ufs_specop_p, ufs_specop_entries };
2722
2723static vop_t **ufs_fifoop_p;
2724static struct vnodeopv_entry_desc ufs_fifoop_entries[] = {
2725	{ &vop_default_desc,		(vop_t *) fifo_vnoperate },
2726	{ &vop_fsync_desc,		(vop_t *) vop_panic },
2727	{ &vop_access_desc,		(vop_t *) ufs_access },
2728	{ &vop_close_desc,		(vop_t *) ufsfifo_close },
2729	{ &vop_getattr_desc,		(vop_t *) ufs_getattr },
2730	{ &vop_inactive_desc,		(vop_t *) ufs_inactive },
2731	{ &vop_islocked_desc,		(vop_t *) vop_stdislocked },
2732	{ &vop_kqfilter_desc,		(vop_t *) ufsfifo_kqfilter },
2733	{ &vop_lock_desc,		(vop_t *) vop_stdlock },
2734	{ &vop_print_desc,		(vop_t *) ufs_print },
2735	{ &vop_read_desc,		(vop_t *) ufsfifo_read },
2736	{ &vop_reclaim_desc,		(vop_t *) ufs_reclaim },
2737#ifdef MAC
2738	{ &vop_refreshlabel_desc,	(vop_t *) vop_stdrefreshlabel_ea },
2739#endif
2740	{ &vop_setattr_desc,		(vop_t *) ufs_setattr },
2741#ifdef MAC
2742	{ &vop_setlabel_desc,		(vop_t *) vop_stdsetlabel_ea },
2743#endif
2744	{ &vop_unlock_desc,		(vop_t *) vop_stdunlock },
2745	{ &vop_write_desc,		(vop_t *) ufsfifo_write },
2746#ifdef UFS_EXTATTR
2747	{ &vop_getextattr_desc,		(vop_t *) ufs_getextattr },
2748	{ &vop_setextattr_desc,		(vop_t *) ufs_setextattr },
2749#endif
2750#ifdef UFS_ACL
2751	{ &vop_getacl_desc,		(vop_t *) ufs_getacl },
2752	{ &vop_setacl_desc,		(vop_t *) ufs_setacl },
2753	{ &vop_aclcheck_desc,		(vop_t *) ufs_aclcheck },
2754#endif
2755	{ NULL, NULL }
2756};
2757static struct vnodeopv_desc ufs_fifoop_opv_desc =
2758	{ &ufs_fifoop_p, ufs_fifoop_entries };
2759
2760VNODEOP_SET(ufs_vnodeop_opv_desc);
2761VNODEOP_SET(ufs_specop_opv_desc);
2762VNODEOP_SET(ufs_fifoop_opv_desc);
2763
2764int
2765ufs_vnoperate(ap)
2766	struct vop_generic_args /* {
2767		struct vnodeop_desc *a_desc;
2768	} */ *ap;
2769{
2770	return (VOCALL(ufs_vnodeop_p, ap->a_desc->vdesc_offset, ap));
2771}
2772
2773int
2774ufs_vnoperatefifo(ap)
2775	struct vop_generic_args /* {
2776		struct vnodeop_desc *a_desc;
2777	} */ *ap;
2778{
2779	return (VOCALL(ufs_fifoop_p, ap->a_desc->vdesc_offset, ap));
2780}
2781
2782int
2783ufs_vnoperatespec(ap)
2784	struct vop_generic_args /* {
2785		struct vnodeop_desc *a_desc;
2786	} */ *ap;
2787{
2788	return (VOCALL(ufs_specop_p, ap->a_desc->vdesc_offset, ap));
2789}
2790