xref: /openbsd-src/sys/ufs/ext2fs/ext2fs_vnops.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: ext2fs_vnops.c,v 1.51 2008/06/09 23:38:37 millert 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 
59 #include <uvm/uvm_extern.h>
60 
61 #include <miscfs/fifofs/fifo.h>
62 #include <miscfs/specfs/specdev.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 = 0;
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) 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 = 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) 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, (size_t *)0, (struct proc *)0);
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, (size_t *)0,
845 					    (struct proc *)0);
846 					cache_purge(fdvp);
847 				}
848 			}
849 		}
850 		error = ext2fs_dirremove(fdvp, fcnp);
851 		if (!error) {
852 			xp->i_e2fs_nlink--;
853 			xp->i_flag |= IN_CHANGE;
854 		}
855 		xp->i_flag &= ~IN_RENAME;
856 	}
857 	if (dp)
858 		vput(fdvp);
859 	if (xp)
860 		vput(fvp);
861 	vrele(ap->a_fvp);
862 	return (error);
863 
864 bad:
865 	if (xp)
866 		vput(ITOV(xp));
867 	vput(ITOV(dp));
868 out:
869 	if (doingdirectory)
870 		ip->i_flag &= ~IN_RENAME;
871 	if (vn_lock(fvp, LK_EXCLUSIVE, p) == 0) {
872 		ip->i_e2fs_nlink--;
873 		ip->i_flag |= IN_CHANGE;
874 		vput(fvp);
875 	} else
876 		vrele(fvp);
877 	return (error);
878 }
879 
880 /*
881  * Mkdir system call
882  */
883 int
884 ext2fs_mkdir(void *v)
885 {
886 	struct vop_mkdir_args *ap = v;
887 	struct vnode *dvp = ap->a_dvp;
888 	struct vattr *vap = ap->a_vap;
889 	struct componentname *cnp = ap->a_cnp;
890 	struct inode *ip, *dp;
891 	struct vnode *tvp;
892 	struct ext2fs_dirtemplate dirtemplate;
893 	mode_t dmode;
894 	int error;
895 
896 #ifdef DIAGNOSTIC
897 	if ((cnp->cn_flags & HASBUF) == 0)
898 		panic("ext2fs_mkdir: no name");
899 #endif
900 	dp = VTOI(dvp);
901 	if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) {
902 		error = EMLINK;
903 		goto out;
904 	}
905 	dmode = vap->va_mode & ACCESSPERMS;
906 	dmode |= IFDIR;
907 	/*
908 	 * Must simulate part of ext2fs_makeinode here to acquire the inode,
909 	 * but not have it entered in the parent directory. The entry is
910 	 * made later after writing "." and ".." entries.
911 	 */
912 	if ((error = ext2fs_inode_alloc(dp, dmode, cnp->cn_cred, &tvp)) != 0)
913 		goto out;
914 	ip = VTOI(tvp);
915 	ip->i_e2fs_uid = cnp->cn_cred->cr_uid;
916 	ip->i_e2fs_gid = dp->i_e2fs_gid;
917 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
918 	ip->i_e2fs_mode = dmode;
919 	tvp->v_type = VDIR;	/* Rest init'd in getnewvnode(). */
920 	ip->i_e2fs_nlink = 2;
921 	error = ext2fs_update(ip, NULL, NULL, 1);
922 
923 	/*
924 	 * Bump link count in parent directory
925 	 * to reflect work done below.  Should
926 	 * be done before reference is created
927 	 * so reparation is possible if we crash.
928 	 */
929 	dp->i_e2fs_nlink++;
930 	dp->i_flag |= IN_CHANGE;
931 	if ((error = ext2fs_update(dp, NULL, NULL, 1)) != 0)
932 		goto bad;
933 
934 	/* Initialize directory with "." and ".." from static template. */
935 	bzero(&dirtemplate, sizeof(dirtemplate));
936 	dirtemplate.dot_ino = h2fs32(ip->i_number);
937 	dirtemplate.dot_reclen = h2fs16(12);
938 	dirtemplate.dot_namlen = 1;
939 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
940 	    (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
941 		dirtemplate.dot_type = EXT2_FT_DIR;
942 	}
943 	dirtemplate.dot_name[0] = '.';
944 	dirtemplate.dotdot_ino = h2fs32(dp->i_number);
945 	dirtemplate.dotdot_reclen = h2fs16(VTOI(dvp)->i_e2fs->e2fs_bsize - 12);
946 	dirtemplate.dotdot_namlen = 2;
947 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
948 	    (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
949 		dirtemplate.dotdot_type = EXT2_FT_DIR;
950 	}
951 	dirtemplate.dotdot_name[0] = dirtemplate.dotdot_name[1] = '.';
952 	error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
953 	    sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
954 	    IO_NODELOCKED|IO_SYNC, cnp->cn_cred, (size_t *)0, (struct proc *)0);
955 	if (error) {
956 		dp->i_e2fs_nlink--;
957 		dp->i_flag |= IN_CHANGE;
958 		goto bad;
959 	}
960 	if (VTOI(dvp)->i_e2fs->e2fs_bsize >
961 							VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
962 		panic("ext2fs_mkdir: blksize"); /* XXX should grow with balloc() */
963 	else {
964 		error = ext2fs_setsize(ip, VTOI(dvp)->i_e2fs->e2fs_bsize);
965   	        if (error) {
966   	        	dp->i_e2fs_nlink--;
967   	        	dp->i_flag |= IN_CHANGE;
968   	        	goto bad;
969   	        }
970 		ip->i_flag |= IN_CHANGE;
971 	}
972 
973 	/* Directory set up, now install its entry in the parent directory. */
974 	error = ext2fs_direnter(ip, dvp, cnp);
975 	if (error != 0) {
976 		dp->i_e2fs_nlink--;
977 		dp->i_flag |= IN_CHANGE;
978 	}
979 bad:
980 	/*
981 	 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
982 	 * for us because we set the link count to 0.
983 	 */
984 	if (error) {
985 		ip->i_e2fs_nlink = 0;
986 		ip->i_flag |= IN_CHANGE;
987 		vput(tvp);
988 	} else
989 		*ap->a_vpp = tvp;
990 out:
991 	pool_put(&namei_pool, cnp->cn_pnbuf);
992 	vput(dvp);
993 	return (error);
994 }
995 
996 /*
997  * Rmdir system call.
998  */
999 int
1000 ext2fs_rmdir(void *v)
1001 {
1002 	struct vop_rmdir_args *ap = v;
1003 	struct vnode *vp = ap->a_vp;
1004 	struct vnode *dvp = ap->a_dvp;
1005 	struct componentname *cnp = ap->a_cnp;
1006 	struct inode *ip, *dp;
1007 	int error;
1008 
1009 	ip = VTOI(vp);
1010 	dp = VTOI(dvp);
1011 	/*
1012 	 * No rmdir "." please.
1013 	 */
1014 	if (dp == ip) {
1015 		vrele(dvp);
1016 		vput(vp);
1017 		return (EINVAL);
1018 	}
1019 	/*
1020 	 * Verify the directory is empty (and valid).
1021 	 * (Rmdir ".." won't be valid since
1022 	 *  ".." will contain a reference to
1023 	 *  the current directory and thus be
1024 	 *  non-empty.)
1025 	 */
1026 	error = 0;
1027 	if (ip->i_e2fs_nlink != 2 ||
1028 	    !ext2fs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1029 		error = ENOTEMPTY;
1030 		goto out;
1031 	}
1032 	if ((dp->i_e2fs_flags & EXT2_APPEND) ||
1033 				 (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND))) {
1034 		error = EPERM;
1035 		goto out;
1036 	}
1037 	/*
1038 	 * Delete reference to directory before purging
1039 	 * inode.  If we crash in between, the directory
1040 	 * will be reattached to lost+found,
1041 	 */
1042 	error = ext2fs_dirremove(dvp, cnp);
1043 	if (error != 0)
1044 		goto out;
1045 	dp->i_e2fs_nlink--;
1046 	dp->i_flag |= IN_CHANGE;
1047 	cache_purge(dvp);
1048 	vput(dvp);
1049 	dvp = NULL;
1050 	/*
1051 	 * Truncate inode.  The only stuff left
1052 	 * in the directory is "." and "..".  The
1053 	 * "." reference is inconsequential since
1054 	 * we're quashing it.  The ".." reference
1055 	 * has already been adjusted above.  We've
1056 	 * removed the "." reference and the reference
1057 	 * in the parent directory, but there may be
1058 	 * other hard links so decrement by 2 and
1059 	 * worry about them later.
1060 	 */
1061 	ip->i_e2fs_nlink -= 2;
1062 	error = ext2fs_truncate(ip, (off_t)0, IO_SYNC, cnp->cn_cred);
1063 	cache_purge(ITOV(ip));
1064 out:
1065 	if (dvp)
1066 		vput(dvp);
1067 	vput(vp);
1068 	return (error);
1069 }
1070 
1071 /*
1072  * symlink -- make a symbolic link
1073  */
1074 int
1075 ext2fs_symlink(void *v)
1076 {
1077 	struct vop_symlink_args *ap = v;
1078 	struct vnode *vp, **vpp = ap->a_vpp;
1079 	struct inode *ip;
1080 	int len, error;
1081 
1082 	error = ext2fs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1083 			      vpp, ap->a_cnp);
1084 	if (error)
1085 		return (error);
1086 	vp = *vpp;
1087 	len = strlen(ap->a_target);
1088 	if (len < vp->v_mount->mnt_maxsymlinklen) {
1089 		ip = VTOI(vp);
1090 		bcopy(ap->a_target, (char *)ip->i_e2din->e2di_shortlink, len);
1091 		error = ext2fs_setsize(ip, len);
1092 		if (error)
1093 			goto bad;
1094 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
1095 	} else
1096 		error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1097 		    UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred, NULL,
1098 		    (struct proc *)0);
1099 bad:
1100 	vput(vp);
1101 	return (error);
1102 }
1103 
1104 /*
1105  * Return target name of a symbolic link
1106  */
1107 int
1108 ext2fs_readlink(void *v)
1109 {
1110 	struct vop_readlink_args *ap = v;
1111 	struct vnode *vp = ap->a_vp;
1112 	struct inode *ip = VTOI(vp);
1113 	int isize;
1114 
1115 	isize = ext2fs_size(ip);
1116 	if (isize < vp->v_mount->mnt_maxsymlinklen ||
1117 	    (vp->v_mount->mnt_maxsymlinklen == 0 && ip->i_e2fs_nblock == 0)) {
1118 		uiomove((char *)ip->i_e2din->e2di_shortlink, isize, ap->a_uio);
1119 		return (0);
1120 	}
1121 	return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1122 }
1123 
1124 /*
1125  * Advisory record locking support
1126  */
1127 int
1128 ext2fs_advlock(void *v)
1129 {
1130 	struct vop_advlock_args *ap = v;
1131 	struct inode *ip = VTOI(ap->a_vp);
1132 
1133 	return (lf_advlock(&ip->i_lockf, ext2fs_size(ip), ap->a_id, ap->a_op,
1134 	    ap->a_fl, ap->a_flags));
1135 }
1136 
1137 /*
1138  * Allocate a new inode.
1139  */
1140 int
1141 ext2fs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
1142     struct componentname *cnp)
1143 {
1144 	struct inode *ip, *pdir;
1145 	struct vnode *tvp;
1146 	int error;
1147 
1148 	pdir = VTOI(dvp);
1149 #ifdef DIAGNOSTIC
1150 	if ((cnp->cn_flags & HASBUF) == 0)
1151 		panic("ext2fs_makeinode: no name");
1152 #endif
1153 	*vpp = NULL;
1154 	if ((mode & IFMT) == 0)
1155 		mode |= IFREG;
1156 
1157 	if ((error = ext2fs_inode_alloc(pdir, mode, cnp->cn_cred, &tvp))
1158 	    != 0) {
1159 		pool_put(&namei_pool, cnp->cn_pnbuf);
1160 		vput(dvp);
1161 		return (error);
1162 	}
1163 	ip = VTOI(tvp);
1164 	ip->i_e2fs_gid = pdir->i_e2fs_gid;
1165 	ip->i_e2fs_uid = cnp->cn_cred->cr_uid;
1166 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1167 	ip->i_e2fs_mode = mode;
1168 	tvp->v_type = IFTOVT(mode);	/* Rest init'd in getnewvnode(). */
1169 	ip->i_e2fs_nlink = 1;
1170 	if ((ip->i_e2fs_mode & ISGID) &&
1171 		!groupmember(ip->i_e2fs_gid, cnp->cn_cred) &&
1172 	    suser_ucred(cnp->cn_cred))
1173 		ip->i_e2fs_mode &= ~ISGID;
1174 
1175 	/*
1176 	 * Make sure inode goes to disk before directory entry.
1177 	 */
1178 	if ((error = ext2fs_update(ip, NULL, NULL, 1)) != 0)
1179 		goto bad;
1180 	error = ext2fs_direnter(ip, dvp, cnp);
1181 	if (error != 0)
1182 		goto bad;
1183 	if ((cnp->cn_flags & SAVESTART) == 0)
1184 		pool_put(&namei_pool, cnp->cn_pnbuf);
1185 	vput(dvp);
1186 	*vpp = tvp;
1187 	return (0);
1188 
1189 bad:
1190 	/*
1191 	 * Write error occurred trying to update the inode
1192 	 * or the directory so must deallocate the inode.
1193 	 */
1194 	pool_put(&namei_pool, cnp->cn_pnbuf);
1195 	vput(dvp);
1196 	ip->i_e2fs_nlink = 0;
1197 	ip->i_flag |= IN_CHANGE;
1198 	tvp->v_type = VNON;
1199 	vput(tvp);
1200 	return (error);
1201 }
1202 
1203 /*
1204  * Synch an open file.
1205  */
1206 /* ARGSUSED */
1207 int
1208 ext2fs_fsync(void *v)
1209 {
1210 	struct vop_fsync_args *ap = v;
1211 	struct vnode *vp = ap->a_vp;
1212 
1213 	vflushbuf(vp, ap->a_waitfor == MNT_WAIT);
1214 	return (ext2fs_update(VTOI(ap->a_vp), NULL, NULL,
1215 		    ap->a_waitfor == MNT_WAIT));
1216 }
1217 
1218 /*
1219  * Reclaim an inode so that it can be used for other purposes.
1220  */
1221 int
1222 ext2fs_reclaim(void *v)
1223 {
1224 	struct vop_reclaim_args *ap = v;
1225 	struct vnode *vp = ap->a_vp;
1226 	struct inode *ip;
1227 #ifdef DIAGNOSTIC
1228 	extern int prtactive;
1229 
1230 	if (prtactive && vp->v_usecount != 0)
1231 		vprint("ext2fs_reclaim: pushing active", vp);
1232 #endif
1233 
1234 	/*
1235 	 * Remove the inode from its hash chain.
1236 	 */
1237 	ip = VTOI(vp);
1238 	ufs_ihashrem(ip);
1239 
1240 	/*
1241 	 * Purge old data structures associated with the inode.
1242 	 */
1243 	cache_purge(vp);
1244 	if (ip->i_devvp)
1245 		vrele(ip->i_devvp);
1246 
1247 	if (ip->i_e2din != NULL)
1248 		pool_put(&ext2fs_dinode_pool, ip->i_e2din);
1249 
1250 	pool_put(&ext2fs_inode_pool, ip);
1251 
1252 	vp->v_data = NULL;
1253 
1254 	return (0);
1255 }
1256 
1257 /* Global vfs data structures for ext2fs. */
1258 int (**ext2fs_vnodeop_p)(void *);
1259 struct vnodeopv_entry_desc ext2fs_vnodeop_entries[] = {
1260 	{ &vop_default_desc, eopnotsupp },
1261 	{ &vop_lookup_desc, ext2fs_lookup },	/* lookup */
1262 	{ &vop_create_desc, ext2fs_create },	/* create */
1263 	{ &vop_mknod_desc, ext2fs_mknod },		/* mknod */
1264 	{ &vop_open_desc, ext2fs_open },		/* open */
1265 	{ &vop_close_desc, ufs_close },			/* close */
1266 	{ &vop_access_desc, ext2fs_access },	/* access */
1267 	{ &vop_getattr_desc, ext2fs_getattr },	/* getattr */
1268 	{ &vop_setattr_desc, ext2fs_setattr },	/* setattr */
1269 	{ &vop_read_desc, ext2fs_read },		/* read */
1270 	{ &vop_write_desc, ext2fs_write },		/* write */
1271 	{ &vop_ioctl_desc, ufs_ioctl },			/* ioctl */
1272 	{ &vop_poll_desc, ufs_poll },		/* poll */
1273 	{ &vop_kqfilter_desc, vop_generic_kqfilter },	/* kqfilter */
1274 	{ &vop_fsync_desc, ext2fs_fsync },		/* fsync */
1275 	{ &vop_remove_desc, ext2fs_remove },	/* remove */
1276 	{ &vop_link_desc, ext2fs_link },		/* link */
1277 	{ &vop_rename_desc, ext2fs_rename },	/* rename */
1278 	{ &vop_mkdir_desc, ext2fs_mkdir },		/* mkdir */
1279 	{ &vop_rmdir_desc, ext2fs_rmdir },		/* rmdir */
1280 	{ &vop_symlink_desc, ext2fs_symlink },	/* symlink */
1281 	{ &vop_readdir_desc, ext2fs_readdir },	/* readdir */
1282 	{ &vop_readlink_desc, ext2fs_readlink },/* readlink */
1283 	{ &vop_abortop_desc, vop_generic_abortop },		/* abortop */
1284 	{ &vop_inactive_desc, ext2fs_inactive },/* inactive */
1285 	{ &vop_reclaim_desc, ext2fs_reclaim },	/* reclaim */
1286 	{ &vop_lock_desc, ufs_lock },			/* lock */
1287 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
1288 	{ &vop_bmap_desc, ext2fs_bmap },		/* bmap */
1289 	{ &vop_strategy_desc, ufs_strategy },	/* strategy */
1290 	{ &vop_print_desc, ufs_print },			/* print */
1291 	{ &vop_islocked_desc, ufs_islocked },	/* islocked */
1292 	{ &vop_pathconf_desc, ufs_pathconf },	/* pathconf */
1293 	{ &vop_advlock_desc, ext2fs_advlock },	/* advlock */
1294 	{ &vop_bwrite_desc, vop_generic_bwrite },		/* bwrite */
1295 	{ NULL, NULL }
1296 };
1297 struct vnodeopv_desc ext2fs_vnodeop_opv_desc =
1298 	{ &ext2fs_vnodeop_p, ext2fs_vnodeop_entries };
1299 
1300 int (**ext2fs_specop_p)(void *);
1301 struct vnodeopv_entry_desc ext2fs_specop_entries[] = {
1302 	{ &vop_default_desc, spec_vnoperate },
1303 	{ &vop_close_desc, ufsspec_close },		/* close */
1304 	{ &vop_access_desc, ext2fs_access },	/* access */
1305 	{ &vop_getattr_desc, ext2fs_getattr },	/* getattr */
1306 	{ &vop_setattr_desc, ext2fs_setattr },	/* setattr */
1307 	{ &vop_read_desc, ufsspec_read },		/* read */
1308 	{ &vop_write_desc, ufsspec_write },		/* write */
1309 	{ &vop_fsync_desc, ext2fs_fsync },		/* fsync */
1310 	{ &vop_inactive_desc, ext2fs_inactive },/* inactive */
1311 	{ &vop_reclaim_desc, ext2fs_reclaim },	/* reclaim */
1312 	{ &vop_lock_desc, ufs_lock },			/* lock */
1313 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
1314 	{ &vop_print_desc, ufs_print },			/* print */
1315 	{ &vop_islocked_desc, ufs_islocked },	/* islocked */
1316 	{ NULL, NULL }
1317 };
1318 struct vnodeopv_desc ext2fs_specop_opv_desc =
1319 	{ &ext2fs_specop_p, ext2fs_specop_entries };
1320 
1321 #ifdef FIFO
1322 int (**ext2fs_fifoop_p)(void *);
1323 struct vnodeopv_entry_desc ext2fs_fifoop_entries[] = {
1324 	{ &vop_default_desc, fifo_vnoperate },
1325 	{ &vop_close_desc, ufsfifo_close },		/* close */
1326 	{ &vop_access_desc, ext2fs_access },	/* access */
1327 	{ &vop_getattr_desc, ext2fs_getattr },	/* getattr */
1328 	{ &vop_setattr_desc, ext2fs_setattr },	/* setattr */
1329 	{ &vop_read_desc, ufsfifo_read },		/* read */
1330 	{ &vop_write_desc, ufsfifo_write },		/* write */
1331 	{ &vop_fsync_desc, ext2fs_fsync },		/* fsync */
1332 	{ &vop_inactive_desc, ext2fs_inactive },/* inactive */
1333 	{ &vop_reclaim_desc, ext2fsfifo_reclaim },	/* reclaim */
1334 	{ &vop_lock_desc, ufs_lock },			/* lock */
1335 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
1336 	{ &vop_print_desc, ufs_print },			/* print */
1337 	{ &vop_islocked_desc, ufs_islocked },	/* islocked */
1338 	{ &vop_bwrite_desc, vop_generic_bwrite },		/* bwrite */
1339 	{ NULL, NULL }
1340 };
1341 struct vnodeopv_desc ext2fs_fifoop_opv_desc =
1342 	{ &ext2fs_fifoop_p, ext2fs_fifoop_entries };
1343 
1344 int
1345 ext2fsfifo_reclaim(void *v)
1346 {
1347 	fifo_reclaim(v);
1348 	return (ext2fs_reclaim(v));
1349 }
1350 #endif /* FIFO */
1351