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