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