xref: /freebsd/sys/fs/ext2fs/ext2_vnops.c (revision 7aa383846770374466b1dcb2cefd71bde9acf463)
1 /*-
2  *  modified for EXT2FS support in Lites 1.1
3  *
4  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5  *  University of Utah, Department of Computer Science
6  */
7 /*-
8  * Copyright (c) 1982, 1986, 1989, 1993
9  *	The Regents of the University of California.  All rights reserved.
10  * (c) UNIX System Laboratories, Inc.
11  * All or some portions of this file are derived from material licensed
12  * to the University of California by American Telephone and Telegraph
13  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
14  * the permission of UNIX System Laboratories, Inc.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)ufs_vnops.c	8.7 (Berkeley) 2/3/94
41  *	@(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
42  * $FreeBSD$
43  */
44 
45 #include "opt_suiddir.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/fcntl.h>
51 #include <sys/stat.h>
52 #include <sys/bio.h>
53 #include <sys/buf.h>
54 #include <sys/endian.h>
55 #include <sys/priv.h>
56 #include <sys/mount.h>
57 #include <sys/unistd.h>
58 #include <sys/time.h>
59 #include <sys/vnode.h>
60 #include <sys/namei.h>
61 #include <sys/lockf.h>
62 #include <sys/event.h>
63 #include <sys/conf.h>
64 #include <sys/file.h>
65 
66 #include <vm/vm.h>
67 #include <vm/vm_extern.h>
68 #include <vm/vnode_pager.h>
69 
70 #include <fs/fifofs/fifo.h>
71 
72 #include <ufs/ufs/dir.h>
73 
74 #include <fs/ext2fs/inode.h>
75 #include <fs/ext2fs/ext2_mount.h>
76 #include <fs/ext2fs/fs.h>
77 #include <fs/ext2fs/ext2_extern.h>
78 #include <fs/ext2fs/ext2fs.h>
79 #include <fs/ext2fs/ext2_dir.h>
80 
81 static int ext2_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
82 static void ext2_itimes_locked(struct vnode *);
83 
84 static vop_access_t	ext2_access;
85 static int ext2_chmod(struct vnode *, int, struct ucred *, struct thread *);
86 static int ext2_chown(struct vnode *, uid_t, gid_t, struct ucred *,
87     struct thread *);
88 static vop_close_t	ext2_close;
89 static vop_create_t	ext2_create;
90 static vop_fsync_t	ext2_fsync;
91 static vop_getattr_t	ext2_getattr;
92 static vop_link_t	ext2_link;
93 static vop_mkdir_t	ext2_mkdir;
94 static vop_mknod_t	ext2_mknod;
95 static vop_open_t	ext2_open;
96 static vop_pathconf_t	ext2_pathconf;
97 static vop_print_t	ext2_print;
98 static vop_read_t	ext2_read;
99 static vop_readlink_t	ext2_readlink;
100 static vop_remove_t	ext2_remove;
101 static vop_rename_t	ext2_rename;
102 static vop_rmdir_t	ext2_rmdir;
103 static vop_setattr_t	ext2_setattr;
104 static vop_strategy_t	ext2_strategy;
105 static vop_symlink_t	ext2_symlink;
106 static vop_write_t	ext2_write;
107 static vop_vptofh_t	ext2_vptofh;
108 static vop_close_t	ext2fifo_close;
109 static vop_kqfilter_t	ext2fifo_kqfilter;
110 
111 /* Global vfs data structures for ext2. */
112 struct vop_vector ext2_vnodeops = {
113 	.vop_default =		&default_vnodeops,
114 	.vop_access =		ext2_access,
115 	.vop_bmap =		ext2_bmap,
116 	.vop_cachedlookup =	ext2_lookup,
117 	.vop_close =		ext2_close,
118 	.vop_create =		ext2_create,
119 	.vop_fsync =		ext2_fsync,
120 	.vop_getattr =		ext2_getattr,
121 	.vop_inactive =		ext2_inactive,
122 	.vop_link =		ext2_link,
123 	.vop_lookup =		vfs_cache_lookup,
124 	.vop_mkdir =		ext2_mkdir,
125 	.vop_mknod =		ext2_mknod,
126 	.vop_open =		ext2_open,
127 	.vop_pathconf =		ext2_pathconf,
128 	.vop_poll =		vop_stdpoll,
129 	.vop_print =		ext2_print,
130 	.vop_read =		ext2_read,
131 	.vop_readdir =		ext2_readdir,
132 	.vop_readlink =		ext2_readlink,
133 	.vop_reallocblks =	ext2_reallocblks,
134 	.vop_reclaim =		ext2_reclaim,
135 	.vop_remove =		ext2_remove,
136 	.vop_rename =		ext2_rename,
137 	.vop_rmdir =		ext2_rmdir,
138 	.vop_setattr =		ext2_setattr,
139 	.vop_strategy =		ext2_strategy,
140 	.vop_symlink =		ext2_symlink,
141 	.vop_write =		ext2_write,
142 	.vop_vptofh =		ext2_vptofh,
143 };
144 
145 struct vop_vector ext2_fifoops = {
146 	.vop_default =		&fifo_specops,
147 	.vop_access =		ext2_access,
148 	.vop_close =		ext2fifo_close,
149 	.vop_fsync =		ext2_fsync,
150 	.vop_getattr =		ext2_getattr,
151 	.vop_inactive =		ext2_inactive,
152 	.vop_kqfilter =		ext2fifo_kqfilter,
153 	.vop_print =		ext2_print,
154 	.vop_read =		VOP_PANIC,
155 	.vop_reclaim =		ext2_reclaim,
156 	.vop_setattr =		ext2_setattr,
157 	.vop_write =		VOP_PANIC,
158 	.vop_vptofh =		ext2_vptofh,
159 };
160 
161 #include <fs/ext2fs/ext2_readwrite.c>
162 
163 /*
164  * A virgin directory (no blushing please).
165  * Note that the type and namlen fields are reversed relative to ext2.
166  * Also, we don't use `struct odirtemplate', since it would just cause
167  * endianness problems.
168  */
169 static struct dirtemplate mastertemplate = {
170 	0, 12, 1, EXT2_FT_DIR, ".",
171 	0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
172 };
173 static struct dirtemplate omastertemplate = {
174 	0, 12, 1, EXT2_FT_UNKNOWN, ".",
175 	0, DIRBLKSIZ - 12, 2, EXT2_FT_UNKNOWN, ".."
176 };
177 
178 static void
179 ext2_itimes_locked(struct vnode *vp)
180 {
181 	struct inode *ip;
182 	struct timespec ts;
183 
184 	ASSERT_VI_LOCKED(vp, __func__);
185 
186 	ip = VTOI(vp);
187 	if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
188 		return;
189 	if ((vp->v_type == VBLK || vp->v_type == VCHR))
190 		ip->i_flag |= IN_LAZYMOD;
191 	else
192 		ip->i_flag |= IN_MODIFIED;
193 	if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
194 		vfs_timestamp(&ts);
195 		if (ip->i_flag & IN_ACCESS) {
196 			ip->i_atime = ts.tv_sec;
197 			ip->i_atimensec = ts.tv_nsec;
198 		}
199 		if (ip->i_flag & IN_UPDATE) {
200 			ip->i_mtime = ts.tv_sec;
201 			ip->i_mtimensec = ts.tv_nsec;
202 			ip->i_modrev++;
203 		}
204 		if (ip->i_flag & IN_CHANGE) {
205 			ip->i_ctime = ts.tv_sec;
206 			ip->i_ctimensec = ts.tv_nsec;
207 		}
208 	}
209 	ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
210 }
211 
212 void
213 ext2_itimes(struct vnode *vp)
214 {
215 
216 	VI_LOCK(vp);
217 	ext2_itimes_locked(vp);
218 	VI_UNLOCK(vp);
219 }
220 
221 /*
222  * Create a regular file
223  */
224 static int
225 ext2_create(ap)
226 	struct vop_create_args /* {
227 		struct vnode *a_dvp;
228 		struct vnode **a_vpp;
229 		struct componentname *a_cnp;
230 		struct vattr *a_vap;
231 	} */ *ap;
232 {
233 	int error;
234 
235 	error =
236 	    ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
237 	    ap->a_dvp, ap->a_vpp, ap->a_cnp);
238 	if (error)
239 		return (error);
240 	return (0);
241 }
242 
243 static int
244 ext2_open(ap)
245 	struct vop_open_args /* {
246 		struct vnode *a_vp;
247 		int  a_mode;
248 		struct ucred *a_cred;
249 		struct thread *a_td;
250 	} */ *ap;
251 {
252 
253 	if (ap->a_vp->v_type == VBLK || ap->a_vp->v_type == VCHR)
254 		return (EOPNOTSUPP);
255 
256 	/*
257 	 * Files marked append-only must be opened for appending.
258 	 */
259 	if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
260 	    (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
261 		return (EPERM);
262 
263 	vnode_create_vobject(ap->a_vp, VTOI(ap->a_vp)->i_size, ap->a_td);
264 
265 	return (0);
266 }
267 
268 /*
269  * Close called.
270  *
271  * Update the times on the inode.
272  */
273 static int
274 ext2_close(ap)
275 	struct vop_close_args /* {
276 		struct vnode *a_vp;
277 		int  a_fflag;
278 		struct ucred *a_cred;
279 		struct thread *a_td;
280 	} */ *ap;
281 {
282 	struct vnode *vp = ap->a_vp;
283 
284 	VI_LOCK(vp);
285 	if (vp->v_usecount > 1)
286 		ext2_itimes_locked(vp);
287 	VI_UNLOCK(vp);
288 	return (0);
289 }
290 
291 static int
292 ext2_access(ap)
293 	struct vop_access_args /* {
294 		struct vnode *a_vp;
295 		accmode_t a_accmode;
296 		struct ucred *a_cred;
297 		struct thread *a_td;
298 	} */ *ap;
299 {
300 	struct vnode *vp = ap->a_vp;
301 	struct inode *ip = VTOI(vp);
302 	accmode_t accmode = ap->a_accmode;
303 	int error;
304 
305 	if (vp->v_type == VBLK || vp->v_type == VCHR)
306 		return (EOPNOTSUPP);
307 
308 	/*
309 	 * Disallow write attempts on read-only file systems;
310 	 * unless the file is a socket, fifo, or a block or
311 	 * character device resident on the file system.
312 	 */
313 	if (accmode & VWRITE) {
314 		switch (vp->v_type) {
315 		case VDIR:
316 		case VLNK:
317 		case VREG:
318 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
319 				return (EROFS);
320 			break;
321 		default:
322 			break;
323 		}
324 	}
325 
326 	/* If immutable bit set, nobody gets to write it. */
327 	if ((accmode & VWRITE) && (ip->i_flags & (SF_IMMUTABLE | SF_SNAPSHOT)))
328 		return (EPERM);
329 
330 	error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
331 	    ap->a_accmode, ap->a_cred, NULL);
332 	return (error);
333 }
334 
335 static int
336 ext2_getattr(ap)
337 	struct vop_getattr_args /* {
338 		struct vnode *a_vp;
339 		struct vattr *a_vap;
340 		struct ucred *a_cred;
341 	} */ *ap;
342 {
343 	struct vnode *vp = ap->a_vp;
344 	struct inode *ip = VTOI(vp);
345 	struct vattr *vap = ap->a_vap;
346 
347 	ext2_itimes(vp);
348 	/*
349 	 * Copy from inode table
350 	 */
351 	vap->va_fsid = dev2udev(ip->i_devvp->v_rdev);
352 	vap->va_fileid = ip->i_number;
353 	vap->va_mode = ip->i_mode & ~IFMT;
354 	vap->va_nlink = ip->i_nlink;
355 	vap->va_uid = ip->i_uid;
356 	vap->va_gid = ip->i_gid;
357 	vap->va_rdev = ip->i_rdev;
358 	vap->va_size = ip->i_size;
359 	vap->va_atime.tv_sec = ip->i_atime;
360 	vap->va_atime.tv_nsec = ip->i_atimensec;
361 	vap->va_mtime.tv_sec = ip->i_mtime;
362 	vap->va_mtime.tv_nsec = ip->i_mtimensec;
363 	vap->va_ctime.tv_sec = ip->i_ctime;
364 	vap->va_ctime.tv_nsec = ip->i_ctimensec;
365 	vap->va_flags = ip->i_flags;
366 	vap->va_gen = ip->i_gen;
367 	vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
368 	vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
369 	vap->va_type = IFTOVT(ip->i_mode);
370 	vap->va_filerev = ip->i_modrev;
371 	return (0);
372 }
373 
374 /*
375  * Set attribute vnode op. called from several syscalls
376  */
377 static int
378 ext2_setattr(ap)
379 	struct vop_setattr_args /* {
380 		struct vnode *a_vp;
381 		struct vattr *a_vap;
382 		struct ucred *a_cred;
383 	} */ *ap;
384 {
385 	struct vattr *vap = ap->a_vap;
386 	struct vnode *vp = ap->a_vp;
387 	struct inode *ip = VTOI(vp);
388 	struct ucred *cred = ap->a_cred;
389 	struct thread *td = curthread;
390 	int error;
391 
392 	/*
393 	 * Check for unsettable attributes.
394 	 */
395 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
396 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
397 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
398 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
399 		return (EINVAL);
400 	}
401 	if (vap->va_flags != VNOVAL) {
402 		/* Disallow flags not supported by ext2fs. */
403 		if(vap->va_flags & ~(SF_APPEND | SF_IMMUTABLE | UF_NODUMP))
404 			return (EOPNOTSUPP);
405 
406 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
407 			return (EROFS);
408 		/*
409 		 * Callers may only modify the file flags on objects they
410 		 * have VADMIN rights for.
411 		 */
412 		if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
413 			return (error);
414 		/*
415 		 * Unprivileged processes and privileged processes in
416 		 * jail() are not permitted to unset system flags, or
417 		 * modify flags if any system flags are set.
418 		 * Privileged non-jail processes may not modify system flags
419 		 * if securelevel > 0 and any existing system flags are set.
420 		 */
421 		if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
422 			if (ip->i_flags
423 			    & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
424 				error = securelevel_gt(cred, 0);
425 				if (error)
426 					return (error);
427 			}
428 			ip->i_flags = vap->va_flags;
429 		} else {
430 			if (ip->i_flags
431 			    & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
432 			    (vap->va_flags & UF_SETTABLE) != vap->va_flags)
433 				return (EPERM);
434 			ip->i_flags &= SF_SETTABLE;
435 			ip->i_flags |= (vap->va_flags & UF_SETTABLE);
436 		}
437 		ip->i_flag |= IN_CHANGE;
438 		if (vap->va_flags & (IMMUTABLE | APPEND))
439 			return (0);
440 	}
441 	if (ip->i_flags & (IMMUTABLE | APPEND))
442 		return (EPERM);
443 	/*
444 	 * Go through the fields and update iff not VNOVAL.
445 	 */
446 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
447 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
448 			return (EROFS);
449 		if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred,
450 		    td)) != 0)
451 			return (error);
452 	}
453 	if (vap->va_size != VNOVAL) {
454 		/*
455 		 * Disallow write attempts on read-only file systems;
456 		 * unless the file is a socket, fifo, or a block or
457 		 * character device resident on the file system.
458 		 */
459 		switch (vp->v_type) {
460 		case VDIR:
461 			return (EISDIR);
462 		case VLNK:
463 		case VREG:
464 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
465 				return (EROFS);
466 			break;
467 		default:
468 			break;
469 		}
470 		if ((error = ext2_truncate(vp, vap->va_size, 0, cred, td)) != 0)
471 			return (error);
472 	}
473 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
474 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
475 			return (EROFS);
476 		/*
477 		 * From utimes(2):
478 		 * If times is NULL, ... The caller must be the owner of
479 		 * the file, have permission to write the file, or be the
480 		 * super-user.
481 		 * If times is non-NULL, ... The caller must be the owner of
482 		 * the file or be the super-user.
483 		 */
484 		if ((error = VOP_ACCESS(vp, VADMIN, cred, td)) &&
485 		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
486 		    (error = VOP_ACCESS(vp, VWRITE, cred, td))))
487 			return (error);
488 		if (vap->va_atime.tv_sec != VNOVAL)
489 			ip->i_flag |= IN_ACCESS;
490 		if (vap->va_mtime.tv_sec != VNOVAL)
491 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
492 		ext2_itimes(vp);
493 		if (vap->va_atime.tv_sec != VNOVAL) {
494 			ip->i_atime = vap->va_atime.tv_sec;
495 			ip->i_atimensec = vap->va_atime.tv_nsec;
496 		}
497 		if (vap->va_mtime.tv_sec != VNOVAL) {
498 			ip->i_mtime = vap->va_mtime.tv_sec;
499 			ip->i_mtimensec = vap->va_mtime.tv_nsec;
500 		}
501 		error = ext2_update(vp, 0);
502 		if (error)
503 			return (error);
504 	}
505 	error = 0;
506 	if (vap->va_mode != (mode_t)VNOVAL) {
507 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
508 			return (EROFS);
509 		error = ext2_chmod(vp, (int)vap->va_mode, cred, td);
510 	}
511 	return (error);
512 }
513 
514 /*
515  * Change the mode on a file.
516  * Inode must be locked before calling.
517  */
518 static int
519 ext2_chmod(vp, mode, cred, td)
520 	struct vnode *vp;
521 	int mode;
522 	struct ucred *cred;
523 	struct thread *td;
524 {
525 	struct inode *ip = VTOI(vp);
526 	int error;
527 
528 	/*
529 	 * To modify the permissions on a file, must possess VADMIN
530 	 * for that file.
531 	 */
532 	if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
533 		return (error);
534 	/*
535 	 * Privileged processes may set the sticky bit on non-directories,
536 	 * as well as set the setgid bit on a file with a group that the
537 	 * process is not a member of.
538 	 */
539 	if (vp->v_type != VDIR && (mode & S_ISTXT)) {
540 		error = priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0);
541 		if (error)
542 			return (EFTYPE);
543 	}
544 	if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
545 		error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
546 		if (error)
547 			return (error);
548 	}
549 	ip->i_mode &= ~ALLPERMS;
550 	ip->i_mode |= (mode & ALLPERMS);
551 	ip->i_flag |= IN_CHANGE;
552 	return (0);
553 }
554 
555 /*
556  * Perform chown operation on inode ip;
557  * inode must be locked prior to call.
558  */
559 static int
560 ext2_chown(vp, uid, gid, cred, td)
561 	struct vnode *vp;
562 	uid_t uid;
563 	gid_t gid;
564 	struct ucred *cred;
565 	struct thread *td;
566 {
567 	struct inode *ip = VTOI(vp);
568 	uid_t ouid;
569 	gid_t ogid;
570 	int error = 0;
571 
572 	if (uid == (uid_t)VNOVAL)
573 		uid = ip->i_uid;
574 	if (gid == (gid_t)VNOVAL)
575 		gid = ip->i_gid;
576 	/*
577 	 * To modify the ownership of a file, must possess VADMIN
578 	 * for that file.
579 	 */
580 	if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
581 		return (error);
582 	/*
583 	 * To change the owner of a file, or change the group of a file
584 	 * to a group of which we are not a member, the caller must
585 	 * have privilege.
586 	 */
587 	if (uid != ip->i_uid || (gid != ip->i_gid &&
588 	    !groupmember(gid, cred))) {
589 		error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0);
590 		if (error)
591 			return (error);
592 	}
593 	ogid = ip->i_gid;
594 	ouid = ip->i_uid;
595 	ip->i_gid = gid;
596 	ip->i_uid = uid;
597 	ip->i_flag |= IN_CHANGE;
598 	if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
599 		if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0) != 0)
600 			ip->i_mode &= ~(ISUID | ISGID);
601 	}
602 	return (0);
603 }
604 
605 /*
606  * Synch an open file.
607  */
608 /* ARGSUSED */
609 static int
610 ext2_fsync(ap)
611 	struct vop_fsync_args /* {
612 		struct vnode *a_vp;
613 		struct ucred *a_cred;
614 		int a_waitfor;
615 		struct thread *a_td;
616 	} */ *ap;
617 {
618 	/*
619 	 * Flush all dirty buffers associated with a vnode.
620 	 */
621 
622 	vop_stdfsync(ap);
623 
624 	return (ext2_update(ap->a_vp, ap->a_waitfor == MNT_WAIT));
625 }
626 
627 /*
628  * Mknod vnode call
629  */
630 /* ARGSUSED */
631 static int
632 ext2_mknod(ap)
633 	struct vop_mknod_args /* {
634 		struct vnode *a_dvp;
635 		struct vnode **a_vpp;
636 		struct componentname *a_cnp;
637 		struct vattr *a_vap;
638 	} */ *ap;
639 {
640 	struct vattr *vap = ap->a_vap;
641 	struct vnode **vpp = ap->a_vpp;
642 	struct inode *ip;
643 	ino_t ino;
644 	int error;
645 
646 	error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
647 	    ap->a_dvp, vpp, ap->a_cnp);
648 	if (error)
649 		return (error);
650 	ip = VTOI(*vpp);
651 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
652 	if (vap->va_rdev != VNOVAL) {
653 		/*
654 		 * Want to be able to use this to make badblock
655 		 * inodes, so don't truncate the dev number.
656 		 */
657 		ip->i_rdev = vap->va_rdev;
658 	}
659 	/*
660 	 * Remove inode, then reload it through VFS_VGET so it is
661 	 * checked to see if it is an alias of an existing entry in
662 	 * the inode cache.	 XXX I don't believe this is necessary now.
663 	 */
664 	(*vpp)->v_type = VNON;
665 	ino = ip->i_number;	/* Save this before vgone() invalidates ip. */
666 	vgone(*vpp);
667 	vput(*vpp);
668 	error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
669 	if (error) {
670 		*vpp = NULL;
671 		return (error);
672 	}
673 	return (0);
674 }
675 
676 static int
677 ext2_remove(ap)
678 	struct vop_remove_args /* {
679 		struct vnode *a_dvp;
680 		struct vnode *a_vp;
681 		struct componentname *a_cnp;
682 	} */ *ap;
683 {
684 	struct inode *ip;
685 	struct vnode *vp = ap->a_vp;
686 	struct vnode *dvp = ap->a_dvp;
687 	int error;
688 
689 	ip = VTOI(vp);
690 	if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
691 	    (VTOI(dvp)->i_flags & APPEND)) {
692 		error = EPERM;
693 		goto out;
694 	}
695 	error = ext2_dirremove(dvp, ap->a_cnp);
696 	if (error == 0) {
697 		ip->i_nlink--;
698 		ip->i_flag |= IN_CHANGE;
699 	}
700 out:
701 	return (error);
702 }
703 
704 /*
705  * link vnode call
706  */
707 static int
708 ext2_link(ap)
709 	struct vop_link_args /* {
710 		struct vnode *a_tdvp;
711 		struct vnode *a_vp;
712 		struct componentname *a_cnp;
713 	} */ *ap;
714 {
715 	struct vnode *vp = ap->a_vp;
716 	struct vnode *tdvp = ap->a_tdvp;
717 	struct componentname *cnp = ap->a_cnp;
718 	struct inode *ip;
719 	int error;
720 
721 #ifdef DIAGNOSTIC
722 	if ((cnp->cn_flags & HASBUF) == 0)
723 		panic("ext2_link: no name");
724 #endif
725 	if (tdvp->v_mount != vp->v_mount) {
726 		error = EXDEV;
727 		goto out;
728 	}
729 	ip = VTOI(vp);
730 	if ((nlink_t)ip->i_nlink >= LINK_MAX) {
731 		error = EMLINK;
732 		goto out;
733 	}
734 	if (ip->i_flags & (IMMUTABLE | APPEND)) {
735 		error = EPERM;
736 		goto out;
737 	}
738 	ip->i_nlink++;
739 	ip->i_flag |= IN_CHANGE;
740 	error = ext2_update(vp, 1);
741 	if (!error)
742 		error = ext2_direnter(ip, tdvp, cnp);
743 	if (error) {
744 		ip->i_nlink--;
745 		ip->i_flag |= IN_CHANGE;
746 	}
747 out:
748 	return (error);
749 }
750 
751 /*
752  * Rename system call.
753  * 	rename("foo", "bar");
754  * is essentially
755  *	unlink("bar");
756  *	link("foo", "bar");
757  *	unlink("foo");
758  * but ``atomically''.  Can't do full commit without saving state in the
759  * inode on disk which isn't feasible at this time.  Best we can do is
760  * always guarantee the target exists.
761  *
762  * Basic algorithm is:
763  *
764  * 1) Bump link count on source while we're linking it to the
765  *    target.  This also ensure the inode won't be deleted out
766  *    from underneath us while we work (it may be truncated by
767  *    a concurrent `trunc' or `open' for creation).
768  * 2) Link source to destination.  If destination already exists,
769  *    delete it first.
770  * 3) Unlink source reference to inode if still around. If a
771  *    directory was moved and the parent of the destination
772  *    is different from the source, patch the ".." entry in the
773  *    directory.
774  */
775 static int
776 ext2_rename(ap)
777 	struct vop_rename_args  /* {
778 		struct vnode *a_fdvp;
779 		struct vnode *a_fvp;
780 		struct componentname *a_fcnp;
781 		struct vnode *a_tdvp;
782 		struct vnode *a_tvp;
783 		struct componentname *a_tcnp;
784 	} */ *ap;
785 {
786 	struct vnode *tvp = ap->a_tvp;
787 	struct vnode *tdvp = ap->a_tdvp;
788 	struct vnode *fvp = ap->a_fvp;
789 	struct vnode *fdvp = ap->a_fdvp;
790 	struct componentname *tcnp = ap->a_tcnp;
791 	struct componentname *fcnp = ap->a_fcnp;
792 	struct inode *ip, *xp, *dp;
793 	struct dirtemplate dirbuf;
794 	int doingdirectory = 0, oldparent = 0, newparent = 0;
795 	int error = 0;
796 	u_char namlen;
797 
798 #ifdef DIAGNOSTIC
799 	if ((tcnp->cn_flags & HASBUF) == 0 ||
800 	    (fcnp->cn_flags & HASBUF) == 0)
801 		panic("ext2_rename: no name");
802 #endif
803 	/*
804 	 * Check for cross-device rename.
805 	 */
806 	if ((fvp->v_mount != tdvp->v_mount) ||
807 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
808 		error = EXDEV;
809 abortit:
810 		if (tdvp == tvp)
811 			vrele(tdvp);
812 		else
813 			vput(tdvp);
814 		if (tvp)
815 			vput(tvp);
816 		vrele(fdvp);
817 		vrele(fvp);
818 		return (error);
819 	}
820 
821 	if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
822 	    (VTOI(tdvp)->i_flags & APPEND))) {
823 		error = EPERM;
824 		goto abortit;
825 	}
826 
827 	/*
828 	 * Renaming a file to itself has no effect.  The upper layers should
829 	 * not call us in that case.  Temporarily just warn if they do.
830 	 */
831 	if (fvp == tvp) {
832 		printf("ext2_rename: fvp == tvp (can't happen)\n");
833 		error = 0;
834 		goto abortit;
835 	}
836 
837 	if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
838 		goto abortit;
839 	dp = VTOI(fdvp);
840 	ip = VTOI(fvp);
841  	if (ip->i_nlink >= LINK_MAX) {
842  		VOP_UNLOCK(fvp, 0);
843  		error = EMLINK;
844  		goto abortit;
845  	}
846 	if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
847 	    || (dp->i_flags & APPEND)) {
848 		VOP_UNLOCK(fvp, 0);
849 		error = EPERM;
850 		goto abortit;
851 	}
852 	if ((ip->i_mode & IFMT) == IFDIR) {
853 		/*
854 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
855 		 */
856 		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
857 		    dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
858 		    (ip->i_flag & IN_RENAME)) {
859 			VOP_UNLOCK(fvp, 0);
860 			error = EINVAL;
861 			goto abortit;
862 		}
863 		ip->i_flag |= IN_RENAME;
864 		oldparent = dp->i_number;
865 		doingdirectory++;
866 	}
867 	vrele(fdvp);
868 
869 	/*
870 	 * When the target exists, both the directory
871 	 * and target vnodes are returned locked.
872 	 */
873 	dp = VTOI(tdvp);
874 	xp = NULL;
875 	if (tvp)
876 		xp = VTOI(tvp);
877 
878 	/*
879 	 * 1) Bump link count while we're moving stuff
880 	 *    around.  If we crash somewhere before
881 	 *    completing our work, the link count
882 	 *    may be wrong, but correctable.
883 	 */
884 	ip->i_nlink++;
885 	ip->i_flag |= IN_CHANGE;
886 	if ((error = ext2_update(fvp, 1)) != 0) {
887 		VOP_UNLOCK(fvp, 0);
888 		goto bad;
889 	}
890 
891 	/*
892 	 * If ".." must be changed (ie the directory gets a new
893 	 * parent) then the source directory must not be in the
894 	 * directory hierarchy above the target, as this would
895 	 * orphan everything below the source directory. Also
896 	 * the user must have write permission in the source so
897 	 * as to be able to change "..". We must repeat the call
898 	 * to namei, as the parent directory is unlocked by the
899 	 * call to checkpath().
900 	 */
901 	error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
902 	VOP_UNLOCK(fvp, 0);
903 	if (oldparent != dp->i_number)
904 		newparent = dp->i_number;
905 	if (doingdirectory && newparent) {
906 		if (error)	/* write access check above */
907 			goto bad;
908 		if (xp != NULL)
909 			vput(tvp);
910 		error = ext2_checkpath(ip, dp, tcnp->cn_cred);
911 		if (error)
912 			goto out;
913 		VREF(tdvp);
914 		error = relookup(tdvp, &tvp, tcnp);
915 		if (error)
916 			goto out;
917 		vrele(tdvp);
918 		dp = VTOI(tdvp);
919 		xp = NULL;
920 		if (tvp)
921 			xp = VTOI(tvp);
922 	}
923 	/*
924 	 * 2) If target doesn't exist, link the target
925 	 *    to the source and unlink the source.
926 	 *    Otherwise, rewrite the target directory
927 	 *    entry to reference the source inode and
928 	 *    expunge the original entry's existence.
929 	 */
930 	if (xp == NULL) {
931 		if (dp->i_devvp != ip->i_devvp)
932 			panic("ext2_rename: EXDEV");
933 		/*
934 		 * Account for ".." in new directory.
935 		 * When source and destination have the same
936 		 * parent we don't fool with the link count.
937 		 */
938 		if (doingdirectory && newparent) {
939 			if ((nlink_t)dp->i_nlink >= LINK_MAX) {
940 				error = EMLINK;
941 				goto bad;
942 			}
943 			dp->i_nlink++;
944 			dp->i_flag |= IN_CHANGE;
945 			error = ext2_update(tdvp, 1);
946 			if (error)
947 				goto bad;
948 		}
949 		error = ext2_direnter(ip, tdvp, tcnp);
950 		if (error) {
951 			if (doingdirectory && newparent) {
952 				dp->i_nlink--;
953 				dp->i_flag |= IN_CHANGE;
954 				(void)ext2_update(tdvp, 1);
955 			}
956 			goto bad;
957 		}
958 		vput(tdvp);
959 	} else {
960 		if (xp->i_devvp != dp->i_devvp || xp->i_devvp != ip->i_devvp)
961 		       panic("ext2_rename: EXDEV");
962 		/*
963 		 * Short circuit rename(foo, foo).
964 		 */
965 		if (xp->i_number == ip->i_number)
966 			panic("ext2_rename: same file");
967 		/*
968 		 * If the parent directory is "sticky", then the user must
969 		 * own the parent directory, or the destination of the rename,
970 		 * otherwise the destination may not be changed (except by
971 		 * root). This implements append-only directories.
972 		 */
973 		if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
974 		    tcnp->cn_cred->cr_uid != dp->i_uid &&
975 		    xp->i_uid != tcnp->cn_cred->cr_uid) {
976 			error = EPERM;
977 			goto bad;
978 		}
979 		/*
980 		 * Target must be empty if a directory and have no links
981 		 * to it. Also, ensure source and target are compatible
982 		 * (both directories, or both not directories).
983 		 */
984 		if ((xp->i_mode&IFMT) == IFDIR) {
985 			if (! ext2_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
986 			    xp->i_nlink > 2) {
987 				error = ENOTEMPTY;
988 				goto bad;
989 			}
990 			if (!doingdirectory) {
991 				error = ENOTDIR;
992 				goto bad;
993 			}
994 			cache_purge(tdvp);
995 		} else if (doingdirectory) {
996 			error = EISDIR;
997 			goto bad;
998 		}
999 		error = ext2_dirrewrite(dp, ip, tcnp);
1000 		if (error)
1001 			goto bad;
1002 		/*
1003 		 * If the target directory is in the same
1004 		 * directory as the source directory,
1005 		 * decrement the link count on the parent
1006 		 * of the target directory.
1007 		 */
1008 		if (doingdirectory && !newparent) {
1009 		       dp->i_nlink--;
1010 		       dp->i_flag |= IN_CHANGE;
1011 		}
1012 		vput(tdvp);
1013 		/*
1014 		 * Adjust the link count of the target to
1015 		 * reflect the dirrewrite above.  If this is
1016 		 * a directory it is empty and there are
1017 		 * no links to it, so we can squash the inode and
1018 		 * any space associated with it.  We disallowed
1019 		 * renaming over top of a directory with links to
1020 		 * it above, as the remaining link would point to
1021 		 * a directory without "." or ".." entries.
1022 		 */
1023 		xp->i_nlink--;
1024 		if (doingdirectory) {
1025 			if (--xp->i_nlink != 0)
1026 				panic("ext2_rename: linked directory");
1027 			error = ext2_truncate(tvp, (off_t)0, IO_SYNC,
1028 			    tcnp->cn_cred, tcnp->cn_thread);
1029 		}
1030 		xp->i_flag |= IN_CHANGE;
1031 		vput(tvp);
1032 		xp = NULL;
1033 	}
1034 
1035 	/*
1036 	 * 3) Unlink the source.
1037 	 */
1038 	fcnp->cn_flags &= ~MODMASK;
1039 	fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1040 	VREF(fdvp);
1041 	error = relookup(fdvp, &fvp, fcnp);
1042 	if (error == 0)
1043 		vrele(fdvp);
1044 	if (fvp != NULL) {
1045 		xp = VTOI(fvp);
1046 		dp = VTOI(fdvp);
1047 	} else {
1048 		/*
1049 		 * From name has disappeared.
1050 		 */
1051 		if (doingdirectory)
1052 			panic("ext2_rename: lost dir entry");
1053 		vrele(ap->a_fvp);
1054 		return (0);
1055 	}
1056 	/*
1057 	 * Ensure that the directory entry still exists and has not
1058 	 * changed while the new name has been entered. If the source is
1059 	 * a file then the entry may have been unlinked or renamed. In
1060 	 * either case there is no further work to be done. If the source
1061 	 * is a directory then it cannot have been rmdir'ed; its link
1062 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
1063 	 * The IN_RENAME flag ensures that it cannot be moved by another
1064 	 * rename.
1065 	 */
1066 	if (xp != ip) {
1067 		if (doingdirectory)
1068 			panic("ext2_rename: lost dir entry");
1069 	} else {
1070 		/*
1071 		 * If the source is a directory with a
1072 		 * new parent, the link count of the old
1073 		 * parent directory must be decremented
1074 		 * and ".." set to point to the new parent.
1075 		 */
1076 		if (doingdirectory && newparent) {
1077 			dp->i_nlink--;
1078 			dp->i_flag |= IN_CHANGE;
1079 			error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
1080 				sizeof (struct dirtemplate), (off_t)0,
1081 				UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1082 				tcnp->cn_cred, NOCRED, NULL, NULL);
1083 			if (error == 0) {
1084 				/* Like ufs little-endian: */
1085 				namlen = dirbuf.dotdot_type;
1086 				if (namlen != 2 ||
1087 				    dirbuf.dotdot_name[0] != '.' ||
1088 				    dirbuf.dotdot_name[1] != '.') {
1089 					ext2_dirbad(xp, (doff_t)12,
1090 					    "rename: mangled dir");
1091 				} else {
1092 					dirbuf.dotdot_ino = newparent;
1093 					(void) vn_rdwr(UIO_WRITE, fvp,
1094 					    (caddr_t)&dirbuf,
1095 					    sizeof (struct dirtemplate),
1096 					    (off_t)0, UIO_SYSSPACE,
1097 					    IO_NODELOCKED | IO_SYNC |
1098 					    IO_NOMACCHECK, tcnp->cn_cred,
1099 					    NOCRED, NULL, NULL);
1100 					cache_purge(fdvp);
1101 				}
1102 			}
1103 		}
1104 		error = ext2_dirremove(fdvp, fcnp);
1105 		if (!error) {
1106 			xp->i_nlink--;
1107 			xp->i_flag |= IN_CHANGE;
1108 		}
1109 		xp->i_flag &= ~IN_RENAME;
1110 	}
1111 	if (dp)
1112 		vput(fdvp);
1113 	if (xp)
1114 		vput(fvp);
1115 	vrele(ap->a_fvp);
1116 	return (error);
1117 
1118 bad:
1119 	if (xp)
1120 		vput(ITOV(xp));
1121 	vput(ITOV(dp));
1122 out:
1123 	if (doingdirectory)
1124 		ip->i_flag &= ~IN_RENAME;
1125 	if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1126 		ip->i_nlink--;
1127 		ip->i_flag |= IN_CHANGE;
1128 		ip->i_flag &= ~IN_RENAME;
1129 		vput(fvp);
1130 	} else
1131 		vrele(fvp);
1132 	return (error);
1133 }
1134 
1135 /*
1136  * Mkdir system call
1137  */
1138 static int
1139 ext2_mkdir(ap)
1140 	struct vop_mkdir_args /* {
1141 		struct vnode *a_dvp;
1142 		struct vnode **a_vpp;
1143 		struct componentname *a_cnp;
1144 		struct vattr *a_vap;
1145 	} */ *ap;
1146 {
1147 	struct vnode *dvp = ap->a_dvp;
1148 	struct vattr *vap = ap->a_vap;
1149 	struct componentname *cnp = ap->a_cnp;
1150 	struct inode *ip, *dp;
1151 	struct vnode *tvp;
1152 	struct dirtemplate dirtemplate, *dtp;
1153 	int error, dmode;
1154 
1155 #ifdef DIAGNOSTIC
1156 	if ((cnp->cn_flags & HASBUF) == 0)
1157 		panic("ext2_mkdir: no name");
1158 #endif
1159 	dp = VTOI(dvp);
1160 	if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1161 		error = EMLINK;
1162 		goto out;
1163 	}
1164 	dmode = vap->va_mode & 0777;
1165 	dmode |= IFDIR;
1166 	/*
1167 	 * Must simulate part of ext2_makeinode here to acquire the inode,
1168 	 * but not have it entered in the parent directory. The entry is
1169 	 * made later after writing "." and ".." entries.
1170 	 */
1171 	error = ext2_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1172 	if (error)
1173 		goto out;
1174 	ip = VTOI(tvp);
1175 	ip->i_gid = dp->i_gid;
1176 #ifdef SUIDDIR
1177 	{
1178 		/*
1179 		 * if we are hacking owners here, (only do this where told to)
1180 		 * and we are not giving it TOO root, (would subvert quotas)
1181 		 * then go ahead and give it to the other user.
1182 		 * The new directory also inherits the SUID bit.
1183 		 * If user's UID and dir UID are the same,
1184 		 * 'give it away' so that the SUID is still forced on.
1185 		 */
1186 		if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1187 		   (dp->i_mode & ISUID) && dp->i_uid) {
1188 			dmode |= ISUID;
1189 			ip->i_uid = dp->i_uid;
1190 		} else {
1191 			ip->i_uid = cnp->cn_cred->cr_uid;
1192 		}
1193 	}
1194 #else
1195 	ip->i_uid = cnp->cn_cred->cr_uid;
1196 #endif
1197 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1198 	ip->i_mode = dmode;
1199 	tvp->v_type = VDIR;	/* Rest init'd in getnewvnode(). */
1200 	ip->i_nlink = 2;
1201 	if (cnp->cn_flags & ISWHITEOUT)
1202 		ip->i_flags |= UF_OPAQUE;
1203 	error = ext2_update(tvp, 1);
1204 
1205 	/*
1206 	 * Bump link count in parent directory
1207 	 * to reflect work done below.  Should
1208 	 * be done before reference is created
1209 	 * so reparation is possible if we crash.
1210 	 */
1211 	dp->i_nlink++;
1212 	dp->i_flag |= IN_CHANGE;
1213 	error = ext2_update(dvp, 1);
1214 	if (error)
1215 		goto bad;
1216 
1217 	/* Initialize directory with "." and ".." from static template. */
1218 	if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
1219 	    EXT2F_INCOMPAT_FTYPE))
1220 		dtp = &mastertemplate;
1221 	else
1222 		dtp = &omastertemplate;
1223 	dirtemplate = *dtp;
1224 	dirtemplate.dot_ino = ip->i_number;
1225 	dirtemplate.dotdot_ino = dp->i_number;
1226 	/* note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE
1227 	 * so let's just redefine it - for this function only
1228 	 */
1229 #undef  DIRBLKSIZ
1230 #define DIRBLKSIZ  VTOI(dvp)->i_e2fs->e2fs_bsize
1231 	dirtemplate.dotdot_reclen = DIRBLKSIZ - 12;
1232 	error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
1233 	    sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
1234 	    IO_NODELOCKED | IO_SYNC | IO_NOMACCHECK, cnp->cn_cred, NOCRED,
1235 	    NULL, NULL);
1236 	if (error) {
1237 		dp->i_nlink--;
1238 		dp->i_flag |= IN_CHANGE;
1239 		goto bad;
1240 	}
1241 	if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1242 		/* XXX should grow with balloc() */
1243 		panic("ext2_mkdir: blksize");
1244 	else {
1245 		ip->i_size = DIRBLKSIZ;
1246 		ip->i_flag |= IN_CHANGE;
1247 	}
1248 
1249 	/* Directory set up, now install its entry in the parent directory. */
1250 	error = ext2_direnter(ip, dvp, cnp);
1251 	if (error) {
1252 		dp->i_nlink--;
1253 		dp->i_flag |= IN_CHANGE;
1254 	}
1255 bad:
1256 	/*
1257 	 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1258 	 * for us because we set the link count to 0.
1259 	 */
1260 	if (error) {
1261 		ip->i_nlink = 0;
1262 		ip->i_flag |= IN_CHANGE;
1263 		vput(tvp);
1264 	} else
1265 		*ap->a_vpp = tvp;
1266 out:
1267 	return (error);
1268 #undef  DIRBLKSIZ
1269 #define DIRBLKSIZ  DEV_BSIZE
1270 }
1271 
1272 /*
1273  * Rmdir system call.
1274  */
1275 static int
1276 ext2_rmdir(ap)
1277 	struct vop_rmdir_args /* {
1278 		struct vnode *a_dvp;
1279 		struct vnode *a_vp;
1280 		struct componentname *a_cnp;
1281 	} */ *ap;
1282 {
1283 	struct vnode *vp = ap->a_vp;
1284 	struct vnode *dvp = ap->a_dvp;
1285 	struct componentname *cnp = ap->a_cnp;
1286 	struct inode *ip, *dp;
1287 	int error;
1288 
1289 	ip = VTOI(vp);
1290 	dp = VTOI(dvp);
1291 
1292 	/*
1293 	 * Verify the directory is empty (and valid).
1294 	 * (Rmdir ".." won't be valid since
1295 	 *  ".." will contain a reference to
1296 	 *  the current directory and thus be
1297 	 *  non-empty.)
1298 	 */
1299 	error = 0;
1300 	if (ip->i_nlink != 2 || !ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1301 		error = ENOTEMPTY;
1302 		goto out;
1303 	}
1304 	if ((dp->i_flags & APPEND)
1305 	    || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1306 		error = EPERM;
1307 		goto out;
1308 	}
1309 	/*
1310 	 * Delete reference to directory before purging
1311 	 * inode.  If we crash in between, the directory
1312 	 * will be reattached to lost+found,
1313 	 */
1314 	error = ext2_dirremove(dvp, cnp);
1315 	if (error)
1316 		goto out;
1317 	dp->i_nlink--;
1318 	dp->i_flag |= IN_CHANGE;
1319 	cache_purge(dvp);
1320 	VOP_UNLOCK(dvp, 0);
1321 	/*
1322 	 * Truncate inode.  The only stuff left
1323 	 * in the directory is "." and "..".  The
1324 	 * "." reference is inconsequential since
1325 	 * we're quashing it.  The ".." reference
1326 	 * has already been adjusted above.  We've
1327 	 * removed the "." reference and the reference
1328 	 * in the parent directory, but there may be
1329 	 * other hard links so decrement by 2 and
1330 	 * worry about them later.
1331 	 */
1332 	ip->i_nlink -= 2;
1333 	error = ext2_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred,
1334 	    cnp->cn_thread);
1335 	cache_purge(ITOV(ip));
1336 	vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1337 out:
1338 	return (error);
1339 }
1340 
1341 /*
1342  * symlink -- make a symbolic link
1343  */
1344 static int
1345 ext2_symlink(ap)
1346 	struct vop_symlink_args /* {
1347 		struct vnode *a_dvp;
1348 		struct vnode **a_vpp;
1349 		struct componentname *a_cnp;
1350 		struct vattr *a_vap;
1351 		char *a_target;
1352 	} */ *ap;
1353 {
1354 	struct vnode *vp, **vpp = ap->a_vpp;
1355 	struct inode *ip;
1356 	int len, error;
1357 
1358 	error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1359 	    vpp, ap->a_cnp);
1360 	if (error)
1361 		return (error);
1362 	vp = *vpp;
1363 	len = strlen(ap->a_target);
1364 	if (len < vp->v_mount->mnt_maxsymlinklen) {
1365 		ip = VTOI(vp);
1366 		bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1367 		ip->i_size = len;
1368 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
1369 	} else
1370 		error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1371 		    UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1372 		    ap->a_cnp->cn_cred, NOCRED, NULL, NULL);
1373 	if (error)
1374 		vput(vp);
1375 	return (error);
1376 }
1377 
1378 /*
1379  * Return target name of a symbolic link
1380  */
1381 static int
1382 ext2_readlink(ap)
1383 	struct vop_readlink_args /* {
1384 		struct vnode *a_vp;
1385 		struct uio *a_uio;
1386 		struct ucred *a_cred;
1387 	} */ *ap;
1388 {
1389 	struct vnode *vp = ap->a_vp;
1390 	struct inode *ip = VTOI(vp);
1391 	int isize;
1392 
1393 	isize = ip->i_size;
1394 	if (isize < vp->v_mount->mnt_maxsymlinklen) {
1395 		uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1396 		return (0);
1397 	}
1398 	return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1399 }
1400 
1401 /*
1402  * Calculate the logical to physical mapping if not done already,
1403  * then call the device strategy routine.
1404  *
1405  * In order to be able to swap to a file, the ext2_bmaparray() operation may not
1406  * deadlock on memory.  See ext2_bmap() for details.
1407  */
1408 static int
1409 ext2_strategy(ap)
1410 	struct vop_strategy_args /* {
1411 		struct vnode *a_vp;
1412 		struct buf *a_bp;
1413 	} */ *ap;
1414 {
1415 	struct buf *bp = ap->a_bp;
1416 	struct vnode *vp = ap->a_vp;
1417 	struct inode *ip;
1418 	struct bufobj *bo;
1419 	int32_t blkno;
1420 	int error;
1421 
1422 	ip = VTOI(vp);
1423 	if (vp->v_type == VBLK || vp->v_type == VCHR)
1424 		panic("ext2_strategy: spec");
1425 	if (bp->b_blkno == bp->b_lblkno) {
1426 		error = ext2_bmaparray(vp, bp->b_lblkno, &blkno, NULL, NULL);
1427 		bp->b_blkno = blkno;
1428 		if (error) {
1429 			bp->b_error = error;
1430 			bp->b_ioflags |= BIO_ERROR;
1431 			bufdone(bp);
1432 			return (0);
1433 		}
1434 		if ((long)bp->b_blkno == -1)
1435 			vfs_bio_clrbuf(bp);
1436 	}
1437 	if ((long)bp->b_blkno == -1) {
1438 		bufdone(bp);
1439 		return (0);
1440 	}
1441 	bp->b_iooffset = dbtob(bp->b_blkno);
1442 	bo = VFSTOEXT2(vp->v_mount)->um_bo;
1443 	BO_STRATEGY(bo, bp);
1444 	return (0);
1445 }
1446 
1447 /*
1448  * Print out the contents of an inode.
1449  */
1450 static int
1451 ext2_print(ap)
1452 	struct vop_print_args /* {
1453 		struct vnode *a_vp;
1454 	} */ *ap;
1455 {
1456 	struct vnode *vp = ap->a_vp;
1457 	struct inode *ip = VTOI(vp);
1458 
1459 	vn_printf(ip->i_devvp, "\tino %lu", (u_long)ip->i_number);
1460 	if (vp->v_type == VFIFO)
1461 		fifo_printinfo(vp);
1462 	printf("\n");
1463 	return (0);
1464 }
1465 
1466 /*
1467  * Close wrapper for fifos.
1468  *
1469  * Update the times on the inode then do device close.
1470  */
1471 static int
1472 ext2fifo_close(ap)
1473 	struct vop_close_args /* {
1474 		struct vnode *a_vp;
1475 		int  a_fflag;
1476 		struct ucred *a_cred;
1477 		struct thread *a_td;
1478 	} */ *ap;
1479 {
1480 	struct vnode *vp = ap->a_vp;
1481 
1482 	VI_LOCK(vp);
1483 	if (vp->v_usecount > 1)
1484 		ext2_itimes_locked(vp);
1485 	VI_UNLOCK(vp);
1486 	return (fifo_specops.vop_close(ap));
1487 }
1488 
1489 /*
1490  * Kqfilter wrapper for fifos.
1491  *
1492  * Fall through to ext2 kqfilter routines if needed
1493  */
1494 static int
1495 ext2fifo_kqfilter(ap)
1496 	struct vop_kqfilter_args *ap;
1497 {
1498 	int error;
1499 
1500 	error = fifo_specops.vop_kqfilter(ap);
1501 	if (error)
1502 		error = vfs_kqfilter(ap);
1503 	return (error);
1504 }
1505 
1506 /*
1507  * Return POSIX pathconf information applicable to ext2 filesystems.
1508  */
1509 static int
1510 ext2_pathconf(ap)
1511 	struct vop_pathconf_args /* {
1512 		struct vnode *a_vp;
1513 		int a_name;
1514 		int *a_retval;
1515 	} */ *ap;
1516 {
1517 
1518 	switch (ap->a_name) {
1519 	case _PC_LINK_MAX:
1520 		*ap->a_retval = LINK_MAX;
1521 		return (0);
1522 	case _PC_NAME_MAX:
1523 		*ap->a_retval = NAME_MAX;
1524 		return (0);
1525 	case _PC_PATH_MAX:
1526 		*ap->a_retval = PATH_MAX;
1527 		return (0);
1528 	case _PC_PIPE_BUF:
1529 		*ap->a_retval = PIPE_BUF;
1530 		return (0);
1531 	case _PC_CHOWN_RESTRICTED:
1532 		*ap->a_retval = 1;
1533 		return (0);
1534 	case _PC_NO_TRUNC:
1535 		*ap->a_retval = 1;
1536 		return (0);
1537 	default:
1538 		return (EINVAL);
1539 	}
1540 	/* NOTREACHED */
1541 }
1542 
1543 /*
1544  * Vnode pointer to File handle
1545  */
1546 /* ARGSUSED */
1547 static int
1548 ext2_vptofh(ap)
1549 	struct vop_vptofh_args /* {
1550 		struct vnode *a_vp;
1551 		struct fid *a_fhp;
1552 	} */ *ap;
1553 {
1554 	struct inode *ip;
1555 	struct ufid *ufhp;
1556 
1557 	ip = VTOI(ap->a_vp);
1558 	ufhp = (struct ufid *)ap->a_fhp;
1559 	ufhp->ufid_len = sizeof(struct ufid);
1560 	ufhp->ufid_ino = ip->i_number;
1561 	ufhp->ufid_gen = ip->i_gen;
1562 	return (0);
1563 }
1564 
1565 /*
1566  * Initialize the vnode associated with a new inode, handle aliased
1567  * vnodes.
1568  */
1569 int
1570 ext2_vinit(mntp, fifoops, vpp)
1571 	struct mount *mntp;
1572 	struct vop_vector *fifoops;
1573 	struct vnode **vpp;
1574 {
1575 	struct inode *ip;
1576 	struct vnode *vp;
1577 
1578 	vp = *vpp;
1579 	ip = VTOI(vp);
1580 	vp->v_type = IFTOVT(ip->i_mode);
1581 	if (vp->v_type == VFIFO)
1582 		vp->v_op = fifoops;
1583 
1584 	if (ip->i_number == ROOTINO)
1585 		vp->v_vflag |= VV_ROOT;
1586 	ip->i_modrev = init_va_filerev();
1587 	*vpp = vp;
1588 	return (0);
1589 }
1590 
1591 /*
1592  * Allocate a new inode.
1593  */
1594 static int
1595 ext2_makeinode(mode, dvp, vpp, cnp)
1596 	int mode;
1597 	struct vnode *dvp;
1598 	struct vnode **vpp;
1599 	struct componentname *cnp;
1600 {
1601 	struct inode *ip, *pdir;
1602 	struct vnode *tvp;
1603 	int error;
1604 
1605 	pdir = VTOI(dvp);
1606 #ifdef DIAGNOSTIC
1607 	if ((cnp->cn_flags & HASBUF) == 0)
1608 		panic("ext2_makeinode: no name");
1609 #endif
1610 	*vpp = NULL;
1611 	if ((mode & IFMT) == 0)
1612 		mode |= IFREG;
1613 
1614 	error = ext2_valloc(dvp, mode, cnp->cn_cred, &tvp);
1615 	if (error) {
1616 		return (error);
1617 	}
1618 	ip = VTOI(tvp);
1619 	ip->i_gid = pdir->i_gid;
1620 #ifdef SUIDDIR
1621 	{
1622 		/*
1623 		 * if we are
1624 		 * not the owner of the directory,
1625 		 * and we are hacking owners here, (only do this where told to)
1626 		 * and we are not giving it TOO root, (would subvert quotas)
1627 		 * then go ahead and give it to the other user.
1628 		 * Note that this drops off the execute bits for security.
1629 		 */
1630 		if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1631 		     (pdir->i_mode & ISUID) &&
1632 		     (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1633 			ip->i_uid = pdir->i_uid;
1634 			mode &= ~07111;
1635 		} else {
1636 			ip->i_uid = cnp->cn_cred->cr_uid;
1637 		}
1638 	}
1639 #else
1640 	ip->i_uid = cnp->cn_cred->cr_uid;
1641 #endif
1642 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1643 	ip->i_mode = mode;
1644 	tvp->v_type = IFTOVT(mode);	/* Rest init'd in getnewvnode(). */
1645 	ip->i_nlink = 1;
1646 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred)) {
1647 		if (priv_check_cred(cnp->cn_cred, PRIV_VFS_RETAINSUGID, 0))
1648 			ip->i_mode &= ~ISGID;
1649 	}
1650 
1651 	if (cnp->cn_flags & ISWHITEOUT)
1652 		ip->i_flags |= UF_OPAQUE;
1653 
1654 	/*
1655 	 * Make sure inode goes to disk before directory entry.
1656 	 */
1657 	error = ext2_update(tvp, 1);
1658 	if (error)
1659 		goto bad;
1660 	error = ext2_direnter(ip, dvp, cnp);
1661 	if (error)
1662 		goto bad;
1663 
1664 	*vpp = tvp;
1665 	return (0);
1666 
1667 bad:
1668 	/*
1669 	 * Write error occurred trying to update the inode
1670 	 * or the directory so must deallocate the inode.
1671 	 */
1672 	ip->i_nlink = 0;
1673 	ip->i_flag |= IN_CHANGE;
1674 	vput(tvp);
1675 	return (error);
1676 }
1677