xref: /openbsd/sys/ufs/ext2fs/ext2fs_vnops.c (revision 5af055cd)
1 /*	$OpenBSD: ext2fs_vnops.c,v 1.77 2016/03/19 12:04:16 natano Exp $	*/
2 /*	$NetBSD: ext2fs_vnops.c,v 1.1 1997/06/11 09:34:09 bouyer Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Manuel Bouyer.
6  * Copyright (c) 1982, 1986, 1989, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  * (c) UNIX System Laboratories, Inc.
9  * All or some portions of this file are derived from material licensed
10  * to the University of California by American Telephone and Telegraph
11  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12  * the permission of UNIX System Laboratories, Inc.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. 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.14 (Berkeley) 10/26/94
39  * Modified for ext2fs by Manuel Bouyer.
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/resourcevar.h>
45 #include <sys/kernel.h>
46 #include <sys/file.h>
47 #include <sys/stat.h>
48 #include <sys/proc.h>
49 #include <sys/conf.h>
50 #include <sys/mount.h>
51 #include <sys/namei.h>
52 #include <sys/vnode.h>
53 #include <sys/lockf.h>
54 #include <sys/malloc.h>
55 #include <sys/pool.h>
56 #include <sys/signalvar.h>
57 #include <sys/specdev.h>
58 #include <sys/unistd.h>
59 
60 #include <miscfs/fifofs/fifo.h>
61 
62 #include <ufs/ufs/quota.h>
63 #include <ufs/ufs/inode.h>
64 #include <ufs/ufs/ufs_extern.h>
65 #include <ufs/ufs/ufsmount.h>
66 
67 #include <ufs/ext2fs/ext2fs.h>
68 #include <ufs/ext2fs/ext2fs_extern.h>
69 #include <ufs/ext2fs/ext2fs_dir.h>
70 
71 #include <uvm/uvm_extern.h>
72 
73 static int ext2fs_chmod(struct vnode *, mode_t, struct ucred *, struct proc *);
74 static int ext2fs_chown(struct vnode *, uid_t, gid_t, struct ucred *, struct proc *);
75 
76 /*
77  * Create a regular file
78  */
79 int
80 ext2fs_create(void *v)
81 {
82 	struct vop_create_args *ap = v;
83 	return ext2fs_makeinode(MAKEIMODE(ap->a_vap->va_type,
84 					  ap->a_vap->va_mode),
85 			  	ap->a_dvp, ap->a_vpp, ap->a_cnp);
86 }
87 
88 /*
89  * Mknod vnode call
90  */
91 /* ARGSUSED */
92 int
93 ext2fs_mknod(void *v)
94 {
95 	struct vop_mknod_args *ap = v;
96 	struct vattr *vap = ap->a_vap;
97 	struct vnode **vpp = ap->a_vpp;
98 	struct inode *ip;
99 	int error;
100 
101 	if ((error =
102 		ext2fs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
103 		ap->a_dvp, vpp, ap->a_cnp)) != 0)
104 		return (error);
105 	ip = VTOI(*vpp);
106 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
107 	if (vap->va_rdev != VNOVAL) {
108 		/*
109 		 * Want to be able to use this to make badblock
110 		 * inodes, so don't truncate the dev number.
111 		 */
112 		ip->i_e2din->e2di_rdev = htole32(vap->va_rdev);
113 	}
114 	/*
115 	 * Remove inode so that it will be reloaded by VFS_VGET and
116 	 * checked to see if it is an alias of an existing entry in
117 	 * the inode cache.
118 	 */
119 	vput(*vpp);
120 	(*vpp)->v_type = VNON;
121 	vgone(*vpp);
122 	*vpp = NULL;
123 	return (0);
124 }
125 
126 /*
127  * Open called.
128  *
129  * Just check the APPEND flag.
130  */
131 /* ARGSUSED */
132 int
133 ext2fs_open(void *v)
134 {
135 	struct vop_open_args *ap = v;
136 
137 	/*
138 	 * Files marked append-only must be opened for appending.
139 	 */
140 	if ((VTOI(ap->a_vp)->i_e2fs_flags & EXT2_APPEND) &&
141 		(ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
142 		return (EPERM);
143 	return (0);
144 }
145 
146 int
147 ext2fs_access(void *v)
148 {
149 	struct vop_access_args *ap = v;
150 	struct vnode *vp = ap->a_vp;
151 	struct inode *ip = VTOI(vp);
152 	mode_t mode = ap->a_mode;
153 
154 	/* If immutable bit set, nobody gets to write it. */
155 	if ((mode & VWRITE) && (ip->i_e2fs_flags & EXT2_IMMUTABLE))
156 		return (EPERM);
157 
158 	return (vaccess(vp->v_type, ip->i_e2fs_mode, ip->i_e2fs_uid,
159 			ip->i_e2fs_gid, mode, ap->a_cred));
160 }
161 
162 /* ARGSUSED */
163 int
164 ext2fs_getattr(void *v)
165 {
166 	struct vop_getattr_args *ap = v;
167 	struct vnode *vp = ap->a_vp;
168 	struct inode *ip = VTOI(vp);
169 	struct vattr *vap = ap->a_vap;
170 
171 	EXT2FS_ITIMES(ip);
172 	/*
173 	 * Copy from inode table
174 	 */
175 	vap->va_fsid = ip->i_dev;
176 	vap->va_fileid = ip->i_number;
177 	vap->va_mode = ip->i_e2fs_mode & ALLPERMS;
178 	vap->va_nlink = ip->i_e2fs_nlink;
179 	vap->va_uid = ip->i_e2fs_uid;
180 	vap->va_gid = ip->i_e2fs_gid;
181 	vap->va_rdev = (dev_t) letoh32(ip->i_e2din->e2di_rdev);
182 	vap->va_size = ext2fs_size(ip);
183 	vap->va_atime.tv_sec = ip->i_e2fs_atime;
184 	vap->va_atime.tv_nsec = 0;
185 	vap->va_mtime.tv_sec = ip->i_e2fs_mtime;
186 	vap->va_mtime.tv_nsec = 0;
187 	vap->va_ctime.tv_sec = ip->i_e2fs_ctime;
188 	vap->va_ctime.tv_nsec = 0;
189 #ifdef EXT2FS_SYSTEM_FLAGS
190 	vap->va_flags = (ip->i_e2fs_flags & EXT2_APPEND) ? SF_APPEND : 0;
191 	vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0;
192 #else
193 	vap->va_flags = (ip->i_e2fs_flags & EXT2_APPEND) ? UF_APPEND : 0;
194 	vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? UF_IMMUTABLE : 0;
195 #endif
196 	vap->va_gen = ip->i_e2fs_gen;
197 	/* this doesn't belong here */
198 	if (vp->v_type == VBLK)
199 		vap->va_blocksize = BLKDEV_IOSIZE;
200 	else if (vp->v_type == VCHR)
201 		vap->va_blocksize = MAXBSIZE;
202 	else
203 		vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
204 	vap->va_bytes = dbtob((u_quad_t)ip->i_e2fs_nblock);
205 	vap->va_type = vp->v_type;
206 	vap->va_filerev = ip->i_modrev;
207 	return (0);
208 }
209 
210 /*
211  * Set attribute vnode op. called from several syscalls
212  */
213 int
214 ext2fs_setattr(void *v)
215 {
216 	struct vop_setattr_args *ap = v;
217 	struct vattr *vap = ap->a_vap;
218 	struct vnode *vp = ap->a_vp;
219 	struct inode *ip = VTOI(vp);
220 	struct ucred *cred = ap->a_cred;
221 	struct proc *p = ap->a_p;
222 	int error;
223 
224 	/*
225 	 * Check for unsettable attributes.
226 	 */
227 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
228 		(vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
229 		(vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
230 		((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
231 		return (EINVAL);
232 	}
233 	if (vap->va_flags != VNOVAL) {
234 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
235 			return (EROFS);
236 		if (cred->cr_uid != ip->i_e2fs_uid &&
237 			(error = suser_ucred(cred)))
238 			return (error);
239 #ifdef EXT2FS_SYSTEM_FLAGS
240 		if (cred->cr_uid == 0) {
241 			if ((ip->i_e2fs_flags &
242 			    (EXT2_APPEND | EXT2_IMMUTABLE)) && securelevel > 0)
243 				return (EPERM);
244 			ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE);
245 			ip->i_e2fs_flags |=
246 			    (vap->va_flags & SF_APPEND) ? EXT2_APPEND : 0 |
247 			    (vap->va_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE: 0;
248 		} else {
249 			return (EPERM);
250 		}
251 #else
252 		ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE);
253 		ip->i_e2fs_flags |=
254 		    (vap->va_flags & UF_APPEND) ? EXT2_APPEND : 0 |
255 		    (vap->va_flags & UF_IMMUTABLE) ? EXT2_IMMUTABLE: 0;
256 #endif
257 		ip->i_flag |= IN_CHANGE;
258 		if (vap->va_flags & (IMMUTABLE | APPEND))
259 			return (0);
260 	}
261 	if (ip->i_e2fs_flags & (EXT2_APPEND | EXT2_IMMUTABLE))
262 		return (EPERM);
263 	/*
264 	 * Go through the fields and update iff not VNOVAL.
265 	 */
266 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
267 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
268 			return (EROFS);
269 		error = ext2fs_chown(vp, vap->va_uid, vap->va_gid, cred, p);
270 		if (error)
271 			return (error);
272 	}
273 	if (vap->va_size != VNOVAL) {
274 		/*
275 		 * Disallow write attempts on read-only file systems;
276 		 * unless the file is a socket, fifo, or a block or
277 		 * character device resident on the file system.
278 		 */
279 		switch (vp->v_type) {
280 		case VDIR:
281 			return (EISDIR);
282 		case VLNK:
283 		case VREG:
284 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
285 				return (EROFS);
286 		default:
287 			break;
288 		}
289 		error = ext2fs_truncate(ip, vap->va_size, 0, cred);
290 		if (error)
291 			return (error);
292 	}
293 	if ((vap->va_vaflags & VA_UTIMES_CHANGE) ||
294 	    vap->va_atime.tv_nsec != VNOVAL ||
295 	    vap->va_mtime.tv_nsec != VNOVAL) {
296 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
297 			return (EROFS);
298 		if (cred->cr_uid != ip->i_e2fs_uid &&
299 			(error = suser_ucred(cred)) &&
300 			((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
301 			(error = VOP_ACCESS(vp, VWRITE, cred, p))))
302 			return (error);
303 		if (vap->va_mtime.tv_nsec != VNOVAL)
304 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
305 		else if (vap->va_vaflags & VA_UTIMES_CHANGE)
306 			ip->i_flag |= IN_CHANGE;
307 		if (vap->va_atime.tv_nsec != VNOVAL) {
308 			if (!(vp->v_mount->mnt_flag & MNT_NOATIME) ||
309 			    (ip->i_flag & (IN_CHANGE | IN_UPDATE)))
310 				ip->i_flag |= IN_ACCESS;
311 		}
312 		EXT2FS_ITIMES(ip);
313 		if (vap->va_mtime.tv_nsec != VNOVAL)
314 			ip->i_e2fs_mtime = vap->va_mtime.tv_sec;
315 		if (vap->va_atime.tv_nsec != VNOVAL)
316 			ip->i_e2fs_atime = vap->va_atime.tv_sec;
317 		error = ext2fs_update(ip, 1);
318 		if (error)
319 			return (error);
320 	}
321 	error = 0;
322 	if (vap->va_mode != (mode_t)VNOVAL) {
323 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
324 			return (EROFS);
325 		error = ext2fs_chmod(vp, vap->va_mode, cred, p);
326 	}
327 	return (error);
328 }
329 
330 /*
331  * Change the mode on a file.
332  * Inode must be locked before calling.
333  */
334 static int
335 ext2fs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct proc *p)
336 {
337 	struct inode *ip = VTOI(vp);
338 	int error;
339 
340 	if (cred->cr_uid != ip->i_e2fs_uid && (error = suser_ucred(cred)))
341 		return (error);
342 	if (cred->cr_uid) {
343 		if (vp->v_type != VDIR && (mode & S_ISTXT))
344 			return (EFTYPE);
345 		if (!groupmember(ip->i_e2fs_gid, cred) && (mode & ISGID))
346 			return (EPERM);
347 	}
348 	ip->i_e2fs_mode &= ~ALLPERMS;
349 	ip->i_e2fs_mode |= (mode & ALLPERMS);
350 	ip->i_flag |= IN_CHANGE;
351 	if ((vp->v_flag & VTEXT) && (ip->i_e2fs_mode & S_ISTXT) == 0)
352 		(void) uvm_vnp_uncache(vp);
353 	return (0);
354 }
355 
356 /*
357  * Perform chown operation on inode ip;
358  * inode must be locked prior to call.
359  */
360 static int
361 ext2fs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred, struct proc *p)
362 {
363 	struct inode *ip = VTOI(vp);
364 	uid_t ouid;
365 	gid_t ogid;
366 	int error = 0;
367 
368 	if (uid == (uid_t)VNOVAL)
369 		uid = ip->i_e2fs_uid;
370 	if (gid == (gid_t)VNOVAL)
371 		gid = ip->i_e2fs_gid;
372 	/*
373 	 * If we don't own the file, are trying to change the owner
374 	 * of the file, or are not a member of the target group,
375 	 * the caller must be superuser or the call fails.
376 	 */
377 	if ((cred->cr_uid != ip->i_e2fs_uid || uid != ip->i_e2fs_uid ||
378 		(gid != ip->i_e2fs_gid && !groupmember(gid, cred))) &&
379 		(error = suser_ucred(cred)))
380 		return (error);
381 	ogid = ip->i_e2fs_gid;
382 	ouid = ip->i_e2fs_uid;
383 
384 	ip->i_e2fs_gid = gid;
385 	ip->i_e2fs_uid = uid;
386 	if (ouid != uid || ogid != gid)
387 		ip->i_flag |= IN_CHANGE;
388 	if (ouid != uid && cred->cr_uid != 0)
389 		ip->i_e2fs_mode &= ~ISUID;
390 	if (ogid != gid && cred->cr_uid != 0)
391 		ip->i_e2fs_mode &= ~ISGID;
392 	return (0);
393 }
394 
395 int
396 ext2fs_remove(void *v)
397 {
398 	struct vop_remove_args *ap = v;
399 	struct inode *ip;
400 	struct vnode *vp = ap->a_vp;
401 	struct vnode *dvp = ap->a_dvp;
402 	int error;
403 
404 	ip = VTOI(vp);
405 	if (vp->v_type == VDIR ||
406 		(ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
407 	    	(VTOI(dvp)->i_e2fs_flags & EXT2_APPEND)) {
408 		error = EPERM;
409 		goto out;
410 	}
411 	error = ext2fs_dirremove(dvp, ap->a_cnp);
412 	if (error == 0) {
413 		ip->i_e2fs_nlink--;
414 		ip->i_flag |= IN_CHANGE;
415 	}
416 out:
417 	if (dvp == vp)
418 		vrele(vp);
419 	else
420 		vput(vp);
421 	vput(dvp);
422 	return (error);
423 }
424 
425 /*
426  * link vnode call
427  */
428 int
429 ext2fs_link(void *v)
430 {
431 	struct vop_link_args *ap = v;
432 	struct vnode *dvp = ap->a_dvp;
433 	struct vnode *vp = ap->a_vp;
434 	struct componentname *cnp = ap->a_cnp;
435 	struct proc *p = cnp->cn_proc;
436 	struct inode *ip;
437 	int error;
438 
439 #ifdef DIAGNOSTIC
440 	if ((cnp->cn_flags & HASBUF) == 0)
441 		panic("ext2fs_link: no name");
442 #endif
443 	if (vp->v_type == VDIR) {
444 		VOP_ABORTOP(dvp, cnp);
445 		error = EISDIR;
446 		goto out2;
447 	}
448 	if (dvp->v_mount != vp->v_mount) {
449 		VOP_ABORTOP(dvp, cnp);
450 		error = EXDEV;
451 		goto out2;
452 	}
453 	if (dvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE, p))) {
454 		VOP_ABORTOP(dvp, cnp);
455 		goto out2;
456 	}
457 	ip = VTOI(vp);
458 	if ((nlink_t)ip->i_e2fs_nlink >= LINK_MAX) {
459 		VOP_ABORTOP(dvp, cnp);
460 		error = EMLINK;
461 		goto out1;
462 	}
463 	if (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) {
464 		VOP_ABORTOP(dvp, cnp);
465 		error = EPERM;
466 		goto out1;
467 	}
468 	ip->i_e2fs_nlink++;
469 	ip->i_flag |= IN_CHANGE;
470 	error = ext2fs_update(ip, 1);
471 	if (!error)
472 		error = ext2fs_direnter(ip, dvp, cnp);
473 	if (error) {
474 		ip->i_e2fs_nlink--;
475 		ip->i_flag |= IN_CHANGE;
476 	}
477 	pool_put(&namei_pool, cnp->cn_pnbuf);
478 out1:
479 	if (dvp != vp)
480 		VOP_UNLOCK(vp, p);
481 out2:
482 	vput(dvp);
483 	return (error);
484 }
485 
486 /*
487  * Rename system call.
488  * 	rename("foo", "bar");
489  * is essentially
490  *	unlink("bar");
491  *	link("foo", "bar");
492  *	unlink("foo");
493  * but ``atomically''.  Can't do full commit without saving state in the
494  * inode on disk which isn't feasible at this time.  Best we can do is
495  * always guarantee the target exists.
496  *
497  * Basic algorithm is:
498  *
499  * 1) Bump link count on source while we're linking it to the
500  *    target.  This also ensure the inode won't be deleted out
501  *    from underneath us while we work (it may be truncated by
502  *    a concurrent `trunc' or `open' for creation).
503  * 2) Link source to destination.  If destination already exists,
504  *    delete it first.
505  * 3) Unlink source reference to inode if still around. If a
506  *    directory was moved and the parent of the destination
507  *    is different from the source, patch the ".." entry in the
508  *    directory.
509  */
510 int
511 ext2fs_rename(void *v)
512 {
513 	struct vop_rename_args  *ap = v;
514 	struct vnode *tvp = ap->a_tvp;
515 	struct vnode *tdvp = ap->a_tdvp;
516 	struct vnode *fvp = ap->a_fvp;
517 	struct vnode *fdvp = ap->a_fdvp;
518 	struct componentname *tcnp = ap->a_tcnp;
519 	struct componentname *fcnp = ap->a_fcnp;
520 	struct inode *ip, *xp, *dp;
521 	struct proc *p = fcnp->cn_proc;
522 	struct ext2fs_dirtemplate dirbuf;
523 	/* struct timespec ts; */
524 	int doingdirectory = 0, oldparent = 0, newparent = 0;
525 	int error = 0;
526 	u_char namlen;
527 
528 #ifdef DIAGNOSTIC
529 	if ((tcnp->cn_flags & HASBUF) == 0 ||
530 	    (fcnp->cn_flags & HASBUF) == 0)
531 		panic("ext2fs_rename: no name");
532 #endif
533 	/*
534 	 * Check for cross-device rename.
535 	 */
536 	if ((fvp->v_mount != tdvp->v_mount) ||
537 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
538 		error = EXDEV;
539 abortit:
540 		VOP_ABORTOP(tdvp, tcnp); /* XXX, why not in NFS? */
541 		if (tdvp == tvp)
542 			vrele(tdvp);
543 		else
544 			vput(tdvp);
545 		if (tvp)
546 			vput(tvp);
547 		VOP_ABORTOP(fdvp, fcnp); /* XXX, why not in NFS? */
548 		vrele(fdvp);
549 		vrele(fvp);
550 		return (error);
551 	}
552 
553 	/*
554 	 * Check if just deleting a link name.
555 	 */
556 	if (tvp && ((VTOI(tvp)->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
557 	    (VTOI(tdvp)->i_e2fs_flags & EXT2_APPEND))) {
558 		error = EPERM;
559 		goto abortit;
560 	}
561 	if (fvp == tvp) {
562 		if (fvp->v_type == VDIR) {
563 			error = EINVAL;
564 			goto abortit;
565 		}
566 
567 		/* Release destination completely. */
568 		VOP_ABORTOP(tdvp, tcnp);
569 		vput(tdvp);
570 		vput(tvp);
571 
572 		/* Delete source. */
573 		vrele(fdvp);
574 		vrele(fvp);
575 		fcnp->cn_flags &= ~MODMASK;
576 		fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
577 		if ((fcnp->cn_flags & SAVESTART) == 0)
578 			panic("ext2fs_rename: lost from startdir");
579 		fcnp->cn_nameiop = DELETE;
580 		(void) vfs_relookup(fdvp, &fvp, fcnp);
581 		return (VOP_REMOVE(fdvp, fvp, fcnp));
582 	}
583 	if ((error = vn_lock(fvp, LK_EXCLUSIVE, p)) != 0)
584 		goto abortit;
585 	dp = VTOI(fdvp);
586 	ip = VTOI(fvp);
587 	if ((nlink_t)ip->i_e2fs_nlink >= LINK_MAX) {
588 		VOP_UNLOCK(fvp, p);
589 		error = EMLINK;
590 		goto abortit;
591 	}
592 	if ((ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
593 		(dp->i_e2fs_flags & EXT2_APPEND)) {
594 		VOP_UNLOCK(fvp, p);
595 		error = EPERM;
596 		goto abortit;
597 	}
598 	if ((ip->i_e2fs_mode & IFMT) == IFDIR) {
599         	error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
600         	if (!error && tvp)
601                 	error = VOP_ACCESS(tvp, VWRITE, tcnp->cn_cred,
602 			    tcnp->cn_proc);
603         	if (error) {
604                 	VOP_UNLOCK(fvp, p);
605                 	error = EACCES;
606                 	goto abortit;
607         	}
608 		/*
609 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
610 		 */
611 		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
612 		    dp == ip ||
613 			(fcnp->cn_flags&ISDOTDOT) ||
614 			(tcnp->cn_flags & ISDOTDOT) ||
615 		    (ip->i_flag & IN_RENAME)) {
616 			VOP_UNLOCK(fvp, p);
617 			error = EINVAL;
618 			goto abortit;
619 		}
620 		ip->i_flag |= IN_RENAME;
621 		oldparent = dp->i_number;
622 		doingdirectory++;
623 	}
624 	vrele(fdvp);
625 
626 	/*
627 	 * When the target exists, both the directory
628 	 * and target vnodes are returned locked.
629 	 */
630 	dp = VTOI(tdvp);
631 	xp = NULL;
632 	if (tvp)
633 		xp = VTOI(tvp);
634 
635 	/*
636 	 * 1) Bump link count while we're moving stuff
637 	 *    around.  If we crash somewhere before
638 	 *    completing our work, the link count
639 	 *    may be wrong, but correctable.
640 	 */
641 	ip->i_e2fs_nlink++;
642 	ip->i_flag |= IN_CHANGE;
643 	if ((error = ext2fs_update(ip, 1)) != 0) {
644 		VOP_UNLOCK(fvp, p);
645 		goto bad;
646 	}
647 
648 	/*
649 	 * If ".." must be changed (ie the directory gets a new
650 	 * parent) then the source directory must not be in the
651 	 * directory hierarchy above the target, as this would
652 	 * orphan everything below the source directory. Also
653 	 * the user must have write permission in the source so
654 	 * as to be able to change "..". We must repeat the call
655 	 * to namei, as the parent directory is unlocked by the
656 	 * call to checkpath().
657 	 */
658 	error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
659 	VOP_UNLOCK(fvp, p);
660 	if (oldparent != dp->i_number)
661 		newparent = dp->i_number;
662 	if (doingdirectory && newparent) {
663 		if (error)	/* write access check above */
664 			goto bad;
665 		if (xp != NULL)
666 			vput(tvp);
667 		error = ext2fs_checkpath(ip, dp, tcnp->cn_cred);
668 		if (error != 0)
669 			goto out;
670 		if ((tcnp->cn_flags & SAVESTART) == 0)
671 			panic("ext2fs_rename: lost to startdir");
672 		if ((error = vfs_relookup(tdvp, &tvp, tcnp)) != 0)
673 			goto out;
674 		dp = VTOI(tdvp);
675 		xp = NULL;
676 		if (tvp)
677 			xp = VTOI(tvp);
678 	}
679 	/*
680 	 * 2) If target doesn't exist, link the target
681 	 *    to the source and unlink the source.
682 	 *    Otherwise, rewrite the target directory
683 	 *    entry to reference the source inode and
684 	 *    expunge the original entry's existence.
685 	 */
686 	if (xp == NULL) {
687 		if (dp->i_dev != ip->i_dev)
688 			panic("rename: EXDEV");
689 		/*
690 		 * Account for ".." in new directory.
691 		 * When source and destination have the same
692 		 * parent we don't fool with the link count.
693 		 */
694 		if (doingdirectory && newparent) {
695 			if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) {
696 				error = EMLINK;
697 				goto bad;
698 			}
699 			dp->i_e2fs_nlink++;
700 			dp->i_flag |= IN_CHANGE;
701 			if ((error = ext2fs_update(dp, 1)) != 0)
702 				goto bad;
703 		}
704 		error = ext2fs_direnter(ip, tdvp, tcnp);
705 		if (error != 0) {
706 			if (doingdirectory && newparent) {
707 				dp->i_e2fs_nlink--;
708 				dp->i_flag |= IN_CHANGE;
709 				(void)ext2fs_update(dp, 1);
710 			}
711 			goto bad;
712 		}
713 		vput(tdvp);
714 	} else {
715 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
716 			panic("rename: EXDEV");
717 		/*
718 		 * Short circuit rename(foo, foo).
719 		 */
720 		if (xp->i_number == ip->i_number)
721 			panic("rename: same file");
722 		/*
723 		 * If the parent directory is "sticky", then the user must
724 		 * own the parent directory, or the destination of the rename,
725 		 * otherwise the destination may not be changed (except by
726 		 * root). This implements append-only directories.
727 		 */
728 		if ((dp->i_e2fs_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
729 		    tcnp->cn_cred->cr_uid != dp->i_e2fs_uid &&
730 		    xp->i_e2fs_uid != tcnp->cn_cred->cr_uid) {
731 			error = EPERM;
732 			goto bad;
733 		}
734 		/*
735 		 * Target must be empty if a directory and have no links
736 		 * to it. Also, ensure source and target are compatible
737 		 * (both directories, or both not directories).
738 		 */
739 		if ((xp->i_e2fs_mode & IFMT) == IFDIR) {
740 			if (!ext2fs_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
741 				xp->i_e2fs_nlink > 2) {
742 				error = ENOTEMPTY;
743 				goto bad;
744 			}
745 			if (!doingdirectory) {
746 				error = ENOTDIR;
747 				goto bad;
748 			}
749 			cache_purge(tdvp);
750 		} else if (doingdirectory) {
751 			error = EISDIR;
752 			goto bad;
753 		}
754 		error = ext2fs_dirrewrite(dp, ip, tcnp);
755 		if (error != 0)
756 			goto bad;
757 		/*
758 		 * If the target directory is in the same
759 		 * directory as the source directory,
760 		 * decrement the link count on the parent
761 		 * of the target directory.
762 		 */
763 		 if (doingdirectory && !newparent) {
764 			dp->i_e2fs_nlink--;
765 			dp->i_flag |= IN_CHANGE;
766 		}
767 		vput(tdvp);
768 		/*
769 		 * Adjust the link count of the target to
770 		 * reflect the dirrewrite above.  If this is
771 		 * a directory it is empty and there are
772 		 * no links to it, so we can squash the inode and
773 		 * any space associated with it.  We disallowed
774 		 * renaming over top of a directory with links to
775 		 * it above, as the remaining link would point to
776 		 * a directory without "." or ".." entries.
777 		 */
778 		xp->i_e2fs_nlink--;
779 		if (doingdirectory) {
780 			if (--xp->i_e2fs_nlink != 0)
781 				panic("rename: linked directory");
782 			error = ext2fs_truncate(xp, (off_t)0, IO_SYNC,
783 			    tcnp->cn_cred);
784 		}
785 		xp->i_flag |= IN_CHANGE;
786 		vput(tvp);
787 		xp = NULL;
788 	}
789 
790 	/*
791 	 * 3) Unlink the source.
792 	 */
793 	fcnp->cn_flags &= ~MODMASK;
794 	fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
795 	if ((fcnp->cn_flags & SAVESTART) == 0)
796 		panic("ext2fs_rename: lost from startdir");
797 	(void) vfs_relookup(fdvp, &fvp, fcnp);
798 	if (fvp != NULL) {
799 		xp = VTOI(fvp);
800 		dp = VTOI(fdvp);
801 	} else {
802 		/*
803 		 * From name has disappeared.
804 		 */
805 		if (doingdirectory)
806 			panic("ext2fs_rename: lost dir entry");
807 		vrele(ap->a_fvp);
808 		return (0);
809 	}
810 	/*
811 	 * Ensure that the directory entry still exists and has not
812 	 * changed while the new name has been entered. If the source is
813 	 * a file then the entry may have been unlinked or renamed. In
814 	 * either case there is no further work to be done. If the source
815 	 * is a directory then it cannot have been rmdir'ed; its link
816 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
817 	 * The IRENAME flag ensures that it cannot be moved by another
818 	 * rename.
819 	 */
820 	if (xp != ip) {
821 		if (doingdirectory)
822 			panic("ext2fs_rename: lost dir entry");
823 	} else {
824 		/*
825 		 * If the source is a directory with a
826 		 * new parent, the link count of the old
827 		 * parent directory must be decremented
828 		 * and ".." set to point to the new parent.
829 		 */
830 		if (doingdirectory && newparent) {
831 			dp->i_e2fs_nlink--;
832 			dp->i_flag |= IN_CHANGE;
833 			error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
834 				sizeof (struct ext2fs_dirtemplate), (off_t)0,
835 				UIO_SYSSPACE, IO_NODELOCKED,
836 				tcnp->cn_cred, NULL, curproc);
837 			if (error == 0) {
838 					namlen = dirbuf.dotdot_namlen;
839 				if (namlen != 2 ||
840 				    dirbuf.dotdot_name[0] != '.' ||
841 				    dirbuf.dotdot_name[1] != '.') {
842 					ufs_dirbad(xp, (doff_t)12,
843 					    "ext2fs_rename: mangled dir");
844 				} else {
845 					dirbuf.dotdot_ino = htole32(newparent);
846 					(void) vn_rdwr(UIO_WRITE, fvp,
847 					    (caddr_t)&dirbuf,
848 					    sizeof (struct dirtemplate),
849 					    (off_t)0, UIO_SYSSPACE,
850 					    IO_NODELOCKED|IO_SYNC,
851 					    tcnp->cn_cred, NULL, curproc);
852 					cache_purge(fdvp);
853 				}
854 			}
855 		}
856 		error = ext2fs_dirremove(fdvp, fcnp);
857 		if (!error) {
858 			xp->i_e2fs_nlink--;
859 			xp->i_flag |= IN_CHANGE;
860 		}
861 		xp->i_flag &= ~IN_RENAME;
862 	}
863 	if (dp)
864 		vput(fdvp);
865 	if (xp)
866 		vput(fvp);
867 	vrele(ap->a_fvp);
868 	return (error);
869 
870 bad:
871 	if (xp)
872 		vput(ITOV(xp));
873 	vput(ITOV(dp));
874 out:
875 	if (doingdirectory)
876 		ip->i_flag &= ~IN_RENAME;
877 	if (vn_lock(fvp, LK_EXCLUSIVE, p) == 0) {
878 		ip->i_e2fs_nlink--;
879 		ip->i_flag |= IN_CHANGE;
880 		vput(fvp);
881 	} else
882 		vrele(fvp);
883 	return (error);
884 }
885 
886 /*
887  * Mkdir system call
888  */
889 int
890 ext2fs_mkdir(void *v)
891 {
892 	struct vop_mkdir_args *ap = v;
893 	struct vnode *dvp = ap->a_dvp;
894 	struct vattr *vap = ap->a_vap;
895 	struct componentname *cnp = ap->a_cnp;
896 	struct inode *ip, *dp;
897 	struct vnode *tvp;
898 	struct ext2fs_dirtemplate dirtemplate;
899 	mode_t dmode;
900 	int error;
901 
902 #ifdef DIAGNOSTIC
903 	if ((cnp->cn_flags & HASBUF) == 0)
904 		panic("ext2fs_mkdir: no name");
905 #endif
906 	dp = VTOI(dvp);
907 	if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) {
908 		error = EMLINK;
909 		goto out;
910 	}
911 	dmode = vap->va_mode & ACCESSPERMS;
912 	dmode |= IFDIR;
913 	/*
914 	 * Must simulate part of ext2fs_makeinode here to acquire the inode,
915 	 * but not have it entered in the parent directory. The entry is
916 	 * made later after writing "." and ".." entries.
917 	 */
918 	if ((error = ext2fs_inode_alloc(dp, dmode, cnp->cn_cred, &tvp)) != 0)
919 		goto out;
920 	ip = VTOI(tvp);
921 	ip->i_e2fs_uid = cnp->cn_cred->cr_uid;
922 	ip->i_e2fs_gid = dp->i_e2fs_gid;
923 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
924 	ip->i_e2fs_mode = dmode;
925 	tvp->v_type = VDIR;	/* Rest init'd in getnewvnode(). */
926 	ip->i_e2fs_nlink = 2;
927 	error = ext2fs_update(ip, 1);
928 
929 	/*
930 	 * Bump link count in parent directory
931 	 * to reflect work done below.  Should
932 	 * be done before reference is created
933 	 * so reparation is possible if we crash.
934 	 */
935 	dp->i_e2fs_nlink++;
936 	dp->i_flag |= IN_CHANGE;
937 	if ((error = ext2fs_update(dp, 1)) != 0)
938 		goto bad;
939 
940 	/* Initialize directory with "." and ".." from static template. */
941 	memset(&dirtemplate, 0, sizeof(dirtemplate));
942 	dirtemplate.dot_ino = htole32(ip->i_number);
943 	dirtemplate.dot_reclen = htole16(12);
944 	dirtemplate.dot_namlen = 1;
945 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
946 	    (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
947 		dirtemplate.dot_type = EXT2_FT_DIR;
948 	}
949 	dirtemplate.dot_name[0] = '.';
950 	dirtemplate.dotdot_ino = htole32(dp->i_number);
951 	dirtemplate.dotdot_reclen = htole16(VTOI(dvp)->i_e2fs->e2fs_bsize - 12);
952 	dirtemplate.dotdot_namlen = 2;
953 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
954 	    (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
955 		dirtemplate.dotdot_type = EXT2_FT_DIR;
956 	}
957 	dirtemplate.dotdot_name[0] = dirtemplate.dotdot_name[1] = '.';
958 	error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
959 	    sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
960 	    IO_NODELOCKED|IO_SYNC, cnp->cn_cred, NULL, curproc);
961 	if (error) {
962 		dp->i_e2fs_nlink--;
963 		dp->i_flag |= IN_CHANGE;
964 		goto bad;
965 	}
966 	if (VTOI(dvp)->i_e2fs->e2fs_bsize >
967 							VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
968 		panic("ext2fs_mkdir: blksize"); /* XXX should grow with balloc() */
969 	else {
970 		error = ext2fs_setsize(ip, VTOI(dvp)->i_e2fs->e2fs_bsize);
971   	        if (error) {
972   	        	dp->i_e2fs_nlink--;
973   	        	dp->i_flag |= IN_CHANGE;
974   	        	goto bad;
975   	        }
976 		ip->i_flag |= IN_CHANGE;
977 	}
978 
979 	/* Directory set up, now install its entry in the parent directory. */
980 	error = ext2fs_direnter(ip, dvp, cnp);
981 	if (error != 0) {
982 		dp->i_e2fs_nlink--;
983 		dp->i_flag |= IN_CHANGE;
984 	}
985 bad:
986 	/*
987 	 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
988 	 * for us because we set the link count to 0.
989 	 */
990 	if (error) {
991 		ip->i_e2fs_nlink = 0;
992 		ip->i_flag |= IN_CHANGE;
993 		vput(tvp);
994 	} else
995 		*ap->a_vpp = tvp;
996 out:
997 	pool_put(&namei_pool, cnp->cn_pnbuf);
998 	vput(dvp);
999 	return (error);
1000 }
1001 
1002 /*
1003  * Rmdir system call.
1004  */
1005 int
1006 ext2fs_rmdir(void *v)
1007 {
1008 	struct vop_rmdir_args *ap = v;
1009 	struct vnode *vp = ap->a_vp;
1010 	struct vnode *dvp = ap->a_dvp;
1011 	struct componentname *cnp = ap->a_cnp;
1012 	struct inode *ip, *dp;
1013 	int error;
1014 
1015 	ip = VTOI(vp);
1016 	dp = VTOI(dvp);
1017 	/*
1018 	 * No rmdir "." please.
1019 	 */
1020 	if (dp == ip) {
1021 		vrele(dvp);
1022 		vput(vp);
1023 		return (EINVAL);
1024 	}
1025 	/*
1026 	 * Verify the directory is empty (and valid).
1027 	 * (Rmdir ".." won't be valid since
1028 	 *  ".." will contain a reference to
1029 	 *  the current directory and thus be
1030 	 *  non-empty.)
1031 	 */
1032 	error = 0;
1033 	if (ip->i_e2fs_nlink != 2 ||
1034 	    !ext2fs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1035 		error = ENOTEMPTY;
1036 		goto out;
1037 	}
1038 	if ((dp->i_e2fs_flags & EXT2_APPEND) ||
1039 				 (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND))) {
1040 		error = EPERM;
1041 		goto out;
1042 	}
1043 	/*
1044 	 * Delete reference to directory before purging
1045 	 * inode.  If we crash in between, the directory
1046 	 * will be reattached to lost+found,
1047 	 */
1048 	error = ext2fs_dirremove(dvp, cnp);
1049 	if (error != 0)
1050 		goto out;
1051 	dp->i_e2fs_nlink--;
1052 	dp->i_flag |= IN_CHANGE;
1053 	cache_purge(dvp);
1054 	vput(dvp);
1055 	dvp = NULL;
1056 	/*
1057 	 * Truncate inode.  The only stuff left
1058 	 * in the directory is "." and "..".  The
1059 	 * "." reference is inconsequential since
1060 	 * we're quashing it.  The ".." reference
1061 	 * has already been adjusted above.  We've
1062 	 * removed the "." reference and the reference
1063 	 * in the parent directory, but there may be
1064 	 * other hard links so decrement by 2 and
1065 	 * worry about them later.
1066 	 */
1067 	ip->i_e2fs_nlink -= 2;
1068 	error = ext2fs_truncate(ip, (off_t)0, IO_SYNC, cnp->cn_cred);
1069 	cache_purge(ITOV(ip));
1070 out:
1071 	if (dvp)
1072 		vput(dvp);
1073 	vput(vp);
1074 	return (error);
1075 }
1076 
1077 /*
1078  * symlink -- make a symbolic link
1079  */
1080 int
1081 ext2fs_symlink(void *v)
1082 {
1083 	struct vop_symlink_args *ap = v;
1084 	struct vnode *vp, **vpp = ap->a_vpp;
1085 	struct inode *ip;
1086 	int len, error;
1087 
1088 	error = ext2fs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1089 			      vpp, ap->a_cnp);
1090 	if (error)
1091 		return (error);
1092 	vp = *vpp;
1093 	len = strlen(ap->a_target);
1094 	if (len < EXT2_MAXSYMLINKLEN) {
1095 		ip = VTOI(vp);
1096 		memcpy(ip->i_e2din->e2di_shortlink, ap->a_target, len);
1097 		error = ext2fs_setsize(ip, len);
1098 		if (error)
1099 			goto bad;
1100 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
1101 	} else
1102 		error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1103 		    UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred, NULL,
1104 		    curproc);
1105 bad:
1106 	vput(vp);
1107 	return (error);
1108 }
1109 
1110 /*
1111  * Return target name of a symbolic link
1112  */
1113 int
1114 ext2fs_readlink(void *v)
1115 {
1116 	struct vop_readlink_args *ap = v;
1117 	struct vnode *vp = ap->a_vp;
1118 	struct inode *ip = VTOI(vp);
1119 	u_int64_t isize;
1120 
1121 	isize = ext2fs_size(ip);
1122 	if (isize < EXT2_MAXSYMLINKLEN) {
1123 		return (uiomove((char *)ip->i_e2din->e2di_shortlink, isize,
1124 		    ap->a_uio));
1125 	}
1126 	return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1127 }
1128 
1129 /*
1130  * Return POSIX pathconf information applicable to ext2 filesystems.
1131  */
1132 int
1133 ext2fs_pathconf(void *v)
1134 {
1135 	struct vop_pathconf_args *ap = v;
1136 	int error = 0;
1137 
1138 	switch (ap->a_name) {
1139 	case _PC_TIMESTAMP_RESOLUTION:
1140 		*ap->a_retval = 1000000000;	/* 1 billion nanoseconds */
1141 		break;
1142 	default:
1143 		return (ufs_pathconf(v));
1144 	}
1145 
1146 	return (error);
1147 }
1148 
1149 /*
1150  * Advisory record locking support
1151  */
1152 int
1153 ext2fs_advlock(void *v)
1154 {
1155 	struct vop_advlock_args *ap = v;
1156 	struct inode *ip = VTOI(ap->a_vp);
1157 
1158 	return (lf_advlock(&ip->i_lockf, ext2fs_size(ip), ap->a_id, ap->a_op,
1159 	    ap->a_fl, ap->a_flags));
1160 }
1161 
1162 /*
1163  * Allocate a new inode.
1164  */
1165 int
1166 ext2fs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
1167     struct componentname *cnp)
1168 {
1169 	struct inode *ip, *pdir;
1170 	struct vnode *tvp;
1171 	int error;
1172 
1173 	pdir = VTOI(dvp);
1174 #ifdef DIAGNOSTIC
1175 	if ((cnp->cn_flags & HASBUF) == 0)
1176 		panic("ext2fs_makeinode: no name");
1177 #endif
1178 	*vpp = NULL;
1179 	if ((mode & IFMT) == 0)
1180 		mode |= IFREG;
1181 
1182 	if ((error = ext2fs_inode_alloc(pdir, mode, cnp->cn_cred, &tvp))
1183 	    != 0) {
1184 		pool_put(&namei_pool, cnp->cn_pnbuf);
1185 		vput(dvp);
1186 		return (error);
1187 	}
1188 	ip = VTOI(tvp);
1189 	ip->i_e2fs_gid = pdir->i_e2fs_gid;
1190 	ip->i_e2fs_uid = cnp->cn_cred->cr_uid;
1191 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1192 	ip->i_e2fs_mode = mode;
1193 	tvp->v_type = IFTOVT(mode);	/* Rest init'd in getnewvnode(). */
1194 	ip->i_e2fs_nlink = 1;
1195 	if ((ip->i_e2fs_mode & ISGID) &&
1196 		!groupmember(ip->i_e2fs_gid, cnp->cn_cred) &&
1197 	    suser_ucred(cnp->cn_cred))
1198 		ip->i_e2fs_mode &= ~ISGID;
1199 
1200 	/*
1201 	 * Make sure inode goes to disk before directory entry.
1202 	 */
1203 	if ((error = ext2fs_update(ip, 1)) != 0)
1204 		goto bad;
1205 	error = ext2fs_direnter(ip, dvp, cnp);
1206 	if (error != 0)
1207 		goto bad;
1208 	if ((cnp->cn_flags & SAVESTART) == 0)
1209 		pool_put(&namei_pool, cnp->cn_pnbuf);
1210 	vput(dvp);
1211 	*vpp = tvp;
1212 	return (0);
1213 
1214 bad:
1215 	/*
1216 	 * Write error occurred trying to update the inode
1217 	 * or the directory so must deallocate the inode.
1218 	 */
1219 	pool_put(&namei_pool, cnp->cn_pnbuf);
1220 	vput(dvp);
1221 	ip->i_e2fs_nlink = 0;
1222 	ip->i_flag |= IN_CHANGE;
1223 	tvp->v_type = VNON;
1224 	vput(tvp);
1225 	return (error);
1226 }
1227 
1228 /*
1229  * Synch an open file.
1230  */
1231 /* ARGSUSED */
1232 int
1233 ext2fs_fsync(void *v)
1234 {
1235 	struct vop_fsync_args *ap = v;
1236 	struct vnode *vp = ap->a_vp;
1237 
1238 	vflushbuf(vp, ap->a_waitfor == MNT_WAIT);
1239 	return (ext2fs_update(VTOI(ap->a_vp), ap->a_waitfor == MNT_WAIT));
1240 }
1241 
1242 /*
1243  * Reclaim an inode so that it can be used for other purposes.
1244  */
1245 int
1246 ext2fs_reclaim(void *v)
1247 {
1248 	struct vop_reclaim_args *ap = v;
1249 	struct vnode *vp = ap->a_vp;
1250 	struct inode *ip;
1251 #ifdef DIAGNOSTIC
1252 	extern int prtactive;
1253 
1254 	if (prtactive && vp->v_usecount != 0)
1255 		vprint("ext2fs_reclaim: pushing active", vp);
1256 #endif
1257 
1258 	/*
1259 	 * Remove the inode from its hash chain.
1260 	 */
1261 	ip = VTOI(vp);
1262 	ufs_ihashrem(ip);
1263 
1264 	/*
1265 	 * Purge old data structures associated with the inode.
1266 	 */
1267 	cache_purge(vp);
1268 	if (ip->i_devvp)
1269 		vrele(ip->i_devvp);
1270 
1271 	if (ip->i_e2din != NULL)
1272 		pool_put(&ext2fs_dinode_pool, ip->i_e2din);
1273 
1274 	pool_put(&ext2fs_inode_pool, ip);
1275 
1276 	vp->v_data = NULL;
1277 
1278 	return (0);
1279 }
1280 
1281 /* Global vfs data structures for ext2fs. */
1282 struct vops ext2fs_vops = {
1283         .vop_lookup     = ext2fs_lookup,
1284         .vop_create     = ext2fs_create,
1285         .vop_mknod      = ext2fs_mknod,
1286         .vop_open       = ext2fs_open,
1287         .vop_close      = ufs_close,
1288         .vop_access     = ext2fs_access,
1289         .vop_getattr    = ext2fs_getattr,
1290         .vop_setattr    = ext2fs_setattr,
1291         .vop_read       = ext2fs_read,
1292         .vop_write      = ext2fs_write,
1293         .vop_ioctl      = ufs_ioctl,
1294         .vop_poll       = ufs_poll,
1295         .vop_kqfilter   = vop_generic_kqfilter,
1296         .vop_fsync      = ext2fs_fsync,
1297         .vop_remove     = ext2fs_remove,
1298         .vop_link       = ext2fs_link,
1299         .vop_rename     = ext2fs_rename,
1300         .vop_mkdir      = ext2fs_mkdir,
1301         .vop_rmdir      = ext2fs_rmdir,
1302         .vop_symlink    = ext2fs_symlink,
1303         .vop_readdir    = ext2fs_readdir,
1304         .vop_readlink   = ext2fs_readlink,
1305         .vop_abortop    = vop_generic_abortop,
1306         .vop_inactive   = ext2fs_inactive,
1307         .vop_reclaim    = ext2fs_reclaim,
1308         .vop_lock       = ufs_lock,
1309         .vop_unlock     = ufs_unlock,
1310         .vop_bmap       = ext2fs_bmap,
1311         .vop_strategy   = ufs_strategy,
1312         .vop_print      = ufs_print,
1313         .vop_islocked   = ufs_islocked,
1314         .vop_pathconf   = ext2fs_pathconf,
1315         .vop_advlock    = ext2fs_advlock,
1316         .vop_bwrite     = vop_generic_bwrite
1317 };
1318 
1319 struct vops ext2fs_specvops = {
1320         .vop_close      = ufsspec_close,
1321         .vop_access     = ext2fs_access,
1322         .vop_getattr    = ext2fs_getattr,
1323         .vop_setattr    = ext2fs_setattr,
1324         .vop_read       = ufsspec_read,
1325         .vop_write      = ufsspec_write,
1326         .vop_fsync      = ext2fs_fsync,
1327         .vop_inactive   = ext2fs_inactive,
1328         .vop_reclaim    = ext2fs_reclaim,
1329         .vop_lock       = ufs_lock,
1330         .vop_unlock     = ufs_unlock,
1331         .vop_print      = ufs_print,
1332         .vop_islocked   = ufs_islocked,
1333 
1334         /* XXX: Keep in sync with spec_vops. */
1335 	.vop_lookup	= vop_generic_lookup,
1336 	.vop_create	= spec_badop,
1337 	.vop_mknod	= spec_badop,
1338 	.vop_open	= spec_open,
1339 	.vop_ioctl	= spec_ioctl,
1340 	.vop_poll	= spec_poll,
1341 	.vop_kqfilter	= spec_kqfilter,
1342 	.vop_revoke	= vop_generic_revoke,
1343 	.vop_remove	= spec_badop,
1344 	.vop_link	= spec_badop,
1345 	.vop_rename	= spec_badop,
1346 	.vop_mkdir	= spec_badop,
1347 	.vop_rmdir	= spec_badop,
1348 	.vop_symlink	= spec_badop,
1349 	.vop_readdir	= spec_badop,
1350 	.vop_readlink	= spec_badop,
1351 	.vop_abortop	= spec_badop,
1352 	.vop_bmap	= vop_generic_bmap,
1353 	.vop_strategy	= spec_strategy,
1354 	.vop_pathconf	= spec_pathconf,
1355 	.vop_advlock	= spec_advlock,
1356 	.vop_bwrite	= vop_generic_bwrite,
1357 };
1358 
1359 #ifdef FIFO
1360 struct vops ext2fs_fifovops = {
1361         .vop_close      = ufsfifo_close,
1362         .vop_access     = ufsfifo_close,
1363         .vop_getattr    = ext2fs_getattr,
1364         .vop_setattr    = ext2fs_setattr,
1365         .vop_read       = ufsfifo_read,
1366         .vop_write      = ufsfifo_write,
1367         .vop_fsync      = ext2fs_fsync,
1368         .vop_inactive   = ext2fs_inactive,
1369         .vop_reclaim    = ext2fsfifo_reclaim,
1370         .vop_lock       = ufs_lock,
1371         .vop_unlock     = ufs_unlock,
1372         .vop_print      = ufs_print,
1373         .vop_islocked   = ufs_islocked,
1374         .vop_bwrite     = vop_generic_bwrite,
1375 
1376         /* XXX: Keep in sync with fifo_vops */
1377 	.vop_lookup	= vop_generic_lookup,
1378 	.vop_create	= fifo_badop,
1379 	.vop_mknod	= fifo_badop,
1380 	.vop_open	= fifo_open,
1381 	.vop_ioctl	= fifo_ioctl,
1382 	.vop_poll	= fifo_poll,
1383 	.vop_kqfilter	= fifo_kqfilter,
1384 	.vop_revoke	= vop_generic_revoke,
1385 	.vop_remove	= fifo_badop,
1386 	.vop_link	= fifo_badop,
1387 	.vop_rename	= fifo_badop,
1388 	.vop_mkdir	= fifo_badop,
1389 	.vop_rmdir	= fifo_badop,
1390 	.vop_symlink	= fifo_badop,
1391 	.vop_readdir	= fifo_badop,
1392 	.vop_readlink	= fifo_badop,
1393 	.vop_abortop	= fifo_badop,
1394 	.vop_bmap	= vop_generic_bmap,
1395 	.vop_strategy	= fifo_badop,
1396 	.vop_pathconf	= fifo_pathconf,
1397 	.vop_advlock	= fifo_advlock,
1398 };
1399 
1400 int
1401 ext2fsfifo_reclaim(void *v)
1402 {
1403 	fifo_reclaim(v);
1404 	return (ext2fs_reclaim(v));
1405 }
1406 #endif /* FIFO */
1407