ext2_vnops.c revision 272531
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: stable/10/sys/fs/ext2fs/ext2_vnops.c 272531 2014-10-04 17:46:04Z pfg $
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/filio.h>
52#include <sys/stat.h>
53#include <sys/bio.h>
54#include <sys/buf.h>
55#include <sys/endian.h>
56#include <sys/priv.h>
57#include <sys/rwlock.h>
58#include <sys/mount.h>
59#include <sys/unistd.h>
60#include <sys/time.h>
61#include <sys/vnode.h>
62#include <sys/namei.h>
63#include <sys/lockf.h>
64#include <sys/event.h>
65#include <sys/conf.h>
66#include <sys/file.h>
67
68#include <vm/vm.h>
69#include <vm/vm_param.h>
70#include <vm/vm_extern.h>
71#include <vm/vm_object.h>
72#include <vm/vm_page.h>
73#include <vm/vm_pager.h>
74#include <vm/vnode_pager.h>
75
76#include "opt_directio.h"
77
78#include <ufs/ufs/dir.h>
79
80#include <fs/ext2fs/fs.h>
81#include <fs/ext2fs/inode.h>
82#include <fs/ext2fs/ext2_extern.h>
83#include <fs/ext2fs/ext2fs.h>
84#include <fs/ext2fs/ext2_dinode.h>
85#include <fs/ext2fs/ext2_dir.h>
86#include <fs/ext2fs/ext2_mount.h>
87
88static int ext2_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
89static void ext2_itimes_locked(struct vnode *);
90static int ext4_ext_read(struct vop_read_args *);
91static int ext2_ind_read(struct vop_read_args *);
92
93static vop_access_t	ext2_access;
94static int ext2_chmod(struct vnode *, int, struct ucred *, struct thread *);
95static int ext2_chown(struct vnode *, uid_t, gid_t, struct ucred *,
96    struct thread *);
97static vop_close_t	ext2_close;
98static vop_create_t	ext2_create;
99static vop_fsync_t	ext2_fsync;
100static vop_getpages_t	ext2_getpages;
101static vop_getattr_t	ext2_getattr;
102static vop_ioctl_t	ext2_ioctl;
103static vop_link_t	ext2_link;
104static vop_mkdir_t	ext2_mkdir;
105static vop_mknod_t	ext2_mknod;
106static vop_open_t	ext2_open;
107static vop_pathconf_t	ext2_pathconf;
108static vop_print_t	ext2_print;
109static vop_read_t	ext2_read;
110static vop_readlink_t	ext2_readlink;
111static vop_remove_t	ext2_remove;
112static vop_rename_t	ext2_rename;
113static vop_rmdir_t	ext2_rmdir;
114static vop_setattr_t	ext2_setattr;
115static vop_strategy_t	ext2_strategy;
116static vop_symlink_t	ext2_symlink;
117static vop_write_t	ext2_write;
118static vop_vptofh_t	ext2_vptofh;
119static vop_close_t	ext2fifo_close;
120static vop_kqfilter_t	ext2fifo_kqfilter;
121
122/* Global vfs data structures for ext2. */
123struct vop_vector ext2_vnodeops = {
124	.vop_default =		&default_vnodeops,
125	.vop_access =		ext2_access,
126	.vop_bmap =		ext2_bmap,
127	.vop_cachedlookup =	ext2_lookup,
128	.vop_close =		ext2_close,
129	.vop_create =		ext2_create,
130	.vop_fsync =		ext2_fsync,
131	.vop_getpages =		ext2_getpages,
132	.vop_getattr =		ext2_getattr,
133	.vop_inactive =		ext2_inactive,
134	.vop_ioctl =		ext2_ioctl,
135	.vop_link =		ext2_link,
136	.vop_lookup =		vfs_cache_lookup,
137	.vop_mkdir =		ext2_mkdir,
138	.vop_mknod =		ext2_mknod,
139	.vop_open =		ext2_open,
140	.vop_pathconf =		ext2_pathconf,
141	.vop_poll =		vop_stdpoll,
142	.vop_print =		ext2_print,
143	.vop_read =		ext2_read,
144	.vop_readdir =		ext2_readdir,
145	.vop_readlink =		ext2_readlink,
146	.vop_reallocblks =	ext2_reallocblks,
147	.vop_reclaim =		ext2_reclaim,
148	.vop_remove =		ext2_remove,
149	.vop_rename =		ext2_rename,
150	.vop_rmdir =		ext2_rmdir,
151	.vop_setattr =		ext2_setattr,
152	.vop_strategy =		ext2_strategy,
153	.vop_symlink =		ext2_symlink,
154	.vop_write =		ext2_write,
155	.vop_vptofh =		ext2_vptofh,
156};
157
158struct vop_vector ext2_fifoops = {
159	.vop_default =		&fifo_specops,
160	.vop_access =		ext2_access,
161	.vop_close =		ext2fifo_close,
162	.vop_fsync =		ext2_fsync,
163	.vop_getattr =		ext2_getattr,
164	.vop_inactive =		ext2_inactive,
165	.vop_kqfilter =		ext2fifo_kqfilter,
166	.vop_print =		ext2_print,
167	.vop_read =		VOP_PANIC,
168	.vop_reclaim =		ext2_reclaim,
169	.vop_setattr =		ext2_setattr,
170	.vop_write =		VOP_PANIC,
171	.vop_vptofh =		ext2_vptofh,
172};
173
174/*
175 * A virgin directory (no blushing please).
176 * Note that the type and namlen fields are reversed relative to ext2.
177 * Also, we don't use `struct odirtemplate', since it would just cause
178 * endianness problems.
179 */
180static struct dirtemplate mastertemplate = {
181	0, 12, 1, EXT2_FT_DIR, ".",
182	0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
183};
184static struct dirtemplate omastertemplate = {
185	0, 12, 1, EXT2_FT_UNKNOWN, ".",
186	0, DIRBLKSIZ - 12, 2, EXT2_FT_UNKNOWN, ".."
187};
188
189static void
190ext2_itimes_locked(struct vnode *vp)
191{
192	struct inode *ip;
193	struct timespec ts;
194
195	ASSERT_VI_LOCKED(vp, __func__);
196
197	ip = VTOI(vp);
198	if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
199		return;
200	if ((vp->v_type == VBLK || vp->v_type == VCHR))
201		ip->i_flag |= IN_LAZYMOD;
202	else
203		ip->i_flag |= IN_MODIFIED;
204	if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
205		vfs_timestamp(&ts);
206		if (ip->i_flag & IN_ACCESS) {
207			ip->i_atime = ts.tv_sec;
208			ip->i_atimensec = ts.tv_nsec;
209		}
210		if (ip->i_flag & IN_UPDATE) {
211			ip->i_mtime = ts.tv_sec;
212			ip->i_mtimensec = ts.tv_nsec;
213			ip->i_modrev++;
214		}
215		if (ip->i_flag & IN_CHANGE) {
216			ip->i_ctime = ts.tv_sec;
217			ip->i_ctimensec = ts.tv_nsec;
218		}
219	}
220	ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
221}
222
223void
224ext2_itimes(struct vnode *vp)
225{
226
227	VI_LOCK(vp);
228	ext2_itimes_locked(vp);
229	VI_UNLOCK(vp);
230}
231
232/*
233 * Create a regular file
234 */
235static int
236ext2_create(struct vop_create_args *ap)
237{
238	int error;
239
240	error =
241	    ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
242	    ap->a_dvp, ap->a_vpp, ap->a_cnp);
243	if (error)
244		return (error);
245	return (0);
246}
247
248static int
249ext2_open(struct vop_open_args *ap)
250{
251
252	if (ap->a_vp->v_type == VBLK || ap->a_vp->v_type == VCHR)
253		return (EOPNOTSUPP);
254
255	/*
256	 * Files marked append-only must be opened for appending.
257	 */
258	if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
259	    (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
260		return (EPERM);
261
262	vnode_create_vobject(ap->a_vp, VTOI(ap->a_vp)->i_size, ap->a_td);
263
264	return (0);
265}
266
267/*
268 * Close called.
269 *
270 * Update the times on the inode.
271 */
272static int
273ext2_close(struct vop_close_args *ap)
274{
275	struct vnode *vp = ap->a_vp;
276
277	VI_LOCK(vp);
278	if (vp->v_usecount > 1)
279		ext2_itimes_locked(vp);
280	VI_UNLOCK(vp);
281	return (0);
282}
283
284static int
285ext2_access(struct vop_access_args *ap)
286{
287	struct vnode *vp = ap->a_vp;
288	struct inode *ip = VTOI(vp);
289	accmode_t accmode = ap->a_accmode;
290	int error;
291
292	if (vp->v_type == VBLK || vp->v_type == VCHR)
293		return (EOPNOTSUPP);
294
295	/*
296	 * Disallow write attempts on read-only file systems;
297	 * unless the file is a socket, fifo, or a block or
298	 * character device resident on the file system.
299	 */
300	if (accmode & VWRITE) {
301		switch (vp->v_type) {
302		case VDIR:
303		case VLNK:
304		case VREG:
305			if (vp->v_mount->mnt_flag & MNT_RDONLY)
306				return (EROFS);
307			break;
308		default:
309			break;
310		}
311	}
312
313	/* If immutable bit set, nobody gets to write it. */
314	if ((accmode & VWRITE) && (ip->i_flags & (SF_IMMUTABLE | SF_SNAPSHOT)))
315		return (EPERM);
316
317	error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
318	    ap->a_accmode, ap->a_cred, NULL);
319	return (error);
320}
321
322static int
323ext2_getattr(struct vop_getattr_args *ap)
324{
325	struct vnode *vp = ap->a_vp;
326	struct inode *ip = VTOI(vp);
327	struct vattr *vap = ap->a_vap;
328
329	ext2_itimes(vp);
330	/*
331	 * Copy from inode table
332	 */
333	vap->va_fsid = dev2udev(ip->i_devvp->v_rdev);
334	vap->va_fileid = ip->i_number;
335	vap->va_mode = ip->i_mode & ~IFMT;
336	vap->va_nlink = ip->i_nlink;
337	vap->va_uid = ip->i_uid;
338	vap->va_gid = ip->i_gid;
339	vap->va_rdev = ip->i_rdev;
340	vap->va_size = ip->i_size;
341	vap->va_atime.tv_sec = ip->i_atime;
342	vap->va_atime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_atimensec : 0;
343	vap->va_mtime.tv_sec = ip->i_mtime;
344	vap->va_mtime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_mtimensec : 0;
345	vap->va_ctime.tv_sec = ip->i_ctime;
346	vap->va_ctime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_ctimensec : 0;
347	if E2DI_HAS_XTIME(ip) {
348		vap->va_birthtime.tv_sec = ip->i_birthtime;
349		vap->va_birthtime.tv_nsec = ip->i_birthnsec;
350	}
351	vap->va_flags = ip->i_flags;
352	vap->va_gen = ip->i_gen;
353	vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
354	vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
355	vap->va_type = IFTOVT(ip->i_mode);
356	vap->va_filerev = ip->i_modrev;
357	return (0);
358}
359
360/*
361 * Set attribute vnode op. called from several syscalls
362 */
363static int
364ext2_setattr(struct vop_setattr_args *ap)
365{
366	struct vattr *vap = ap->a_vap;
367	struct vnode *vp = ap->a_vp;
368	struct inode *ip = VTOI(vp);
369	struct ucred *cred = ap->a_cred;
370	struct thread *td = curthread;
371	int error;
372
373	/*
374	 * Check for unsettable attributes.
375	 */
376	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
377	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
378	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
379	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
380		return (EINVAL);
381	}
382	if (vap->va_flags != VNOVAL) {
383		/* Disallow flags not supported by ext2fs. */
384		if(vap->va_flags & ~(SF_APPEND | SF_IMMUTABLE | UF_NODUMP))
385			return (EOPNOTSUPP);
386
387		if (vp->v_mount->mnt_flag & MNT_RDONLY)
388			return (EROFS);
389		/*
390		 * Callers may only modify the file flags on objects they
391		 * have VADMIN rights for.
392		 */
393		if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
394			return (error);
395		/*
396		 * Unprivileged processes and privileged processes in
397		 * jail() are not permitted to unset system flags, or
398		 * modify flags if any system flags are set.
399		 * Privileged non-jail processes may not modify system flags
400		 * if securelevel > 0 and any existing system flags are set.
401		 */
402		if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
403			if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) {
404				error = securelevel_gt(cred, 0);
405				if (error)
406					return (error);
407			}
408		} else {
409			if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND) ||
410			    ((vap->va_flags ^ ip->i_flags) & SF_SETTABLE))
411				return (EPERM);
412		}
413		ip->i_flags = vap->va_flags;
414		ip->i_flag |= IN_CHANGE;
415		if (ip->i_flags & (IMMUTABLE | APPEND))
416			return (0);
417	}
418	if (ip->i_flags & (IMMUTABLE | APPEND))
419		return (EPERM);
420	/*
421	 * Go through the fields and update iff not VNOVAL.
422	 */
423	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
424		if (vp->v_mount->mnt_flag & MNT_RDONLY)
425			return (EROFS);
426		if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred,
427		    td)) != 0)
428			return (error);
429	}
430	if (vap->va_size != VNOVAL) {
431		/*
432		 * Disallow write attempts on read-only file systems;
433		 * unless the file is a socket, fifo, or a block or
434		 * character device resident on the file system.
435		 */
436		switch (vp->v_type) {
437		case VDIR:
438			return (EISDIR);
439		case VLNK:
440		case VREG:
441			if (vp->v_mount->mnt_flag & MNT_RDONLY)
442				return (EROFS);
443			break;
444		default:
445			break;
446		}
447		if ((error = ext2_truncate(vp, vap->va_size, 0, cred, td)) != 0)
448			return (error);
449	}
450	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
451		if (vp->v_mount->mnt_flag & MNT_RDONLY)
452			return (EROFS);
453		/*
454		 * From utimes(2):
455		 * If times is NULL, ... The caller must be the owner of
456		 * the file, have permission to write the file, or be the
457		 * super-user.
458		 * If times is non-NULL, ... The caller must be the owner of
459		 * the file or be the super-user.
460		 */
461		if ((error = VOP_ACCESS(vp, VADMIN, cred, td)) &&
462		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
463		    (error = VOP_ACCESS(vp, VWRITE, cred, td))))
464			return (error);
465		if (vap->va_atime.tv_sec != VNOVAL)
466			ip->i_flag |= IN_ACCESS;
467		if (vap->va_mtime.tv_sec != VNOVAL)
468			ip->i_flag |= IN_CHANGE | IN_UPDATE;
469		ext2_itimes(vp);
470		if (vap->va_atime.tv_sec != VNOVAL) {
471			ip->i_atime = vap->va_atime.tv_sec;
472			ip->i_atimensec = vap->va_atime.tv_nsec;
473		}
474		if (vap->va_mtime.tv_sec != VNOVAL) {
475			ip->i_mtime = vap->va_mtime.tv_sec;
476			ip->i_mtimensec = vap->va_mtime.tv_nsec;
477		}
478		ip->i_birthtime = vap->va_birthtime.tv_sec;
479		ip->i_birthnsec = vap->va_birthtime.tv_nsec;
480		error = ext2_update(vp, 0);
481		if (error)
482			return (error);
483	}
484	error = 0;
485	if (vap->va_mode != (mode_t)VNOVAL) {
486		if (vp->v_mount->mnt_flag & MNT_RDONLY)
487			return (EROFS);
488		error = ext2_chmod(vp, (int)vap->va_mode, cred, td);
489	}
490	return (error);
491}
492
493/*
494 * Change the mode on a file.
495 * Inode must be locked before calling.
496 */
497static int
498ext2_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td)
499{
500	struct inode *ip = VTOI(vp);
501	int error;
502
503	/*
504	 * To modify the permissions on a file, must possess VADMIN
505	 * for that file.
506	 */
507	if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
508		return (error);
509	/*
510	 * Privileged processes may set the sticky bit on non-directories,
511	 * as well as set the setgid bit on a file with a group that the
512	 * process is not a member of.
513	 */
514	if (vp->v_type != VDIR && (mode & S_ISTXT)) {
515		error = priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0);
516		if (error)
517			return (EFTYPE);
518	}
519	if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
520		error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
521		if (error)
522			return (error);
523	}
524	ip->i_mode &= ~ALLPERMS;
525	ip->i_mode |= (mode & ALLPERMS);
526	ip->i_flag |= IN_CHANGE;
527	return (0);
528}
529
530/*
531 * Perform chown operation on inode ip;
532 * inode must be locked prior to call.
533 */
534static int
535ext2_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
536    struct thread *td)
537{
538	struct inode *ip = VTOI(vp);
539	uid_t ouid;
540	gid_t ogid;
541	int error = 0;
542
543	if (uid == (uid_t)VNOVAL)
544		uid = ip->i_uid;
545	if (gid == (gid_t)VNOVAL)
546		gid = ip->i_gid;
547	/*
548	 * To modify the ownership of a file, must possess VADMIN
549	 * for that file.
550	 */
551	if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
552		return (error);
553	/*
554	 * To change the owner of a file, or change the group of a file
555	 * to a group of which we are not a member, the caller must
556	 * have privilege.
557	 */
558	if (uid != ip->i_uid || (gid != ip->i_gid &&
559	    !groupmember(gid, cred))) {
560		error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0);
561		if (error)
562			return (error);
563	}
564	ogid = ip->i_gid;
565	ouid = ip->i_uid;
566	ip->i_gid = gid;
567	ip->i_uid = uid;
568	ip->i_flag |= IN_CHANGE;
569	if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
570		if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0) != 0)
571			ip->i_mode &= ~(ISUID | ISGID);
572	}
573	return (0);
574}
575
576/*
577 * Synch an open file.
578 */
579/* ARGSUSED */
580static int
581ext2_fsync(struct vop_fsync_args *ap)
582{
583	/*
584	 * Flush all dirty buffers associated with a vnode.
585	 */
586
587	vop_stdfsync(ap);
588
589	return (ext2_update(ap->a_vp, ap->a_waitfor == MNT_WAIT));
590}
591
592/*
593 * Mknod vnode call
594 */
595/* ARGSUSED */
596static int
597ext2_mknod(struct vop_mknod_args *ap)
598{
599	struct vattr *vap = ap->a_vap;
600	struct vnode **vpp = ap->a_vpp;
601	struct inode *ip;
602	ino_t ino;
603	int error;
604
605	error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
606	    ap->a_dvp, vpp, ap->a_cnp);
607	if (error)
608		return (error);
609	ip = VTOI(*vpp);
610	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
611	if (vap->va_rdev != VNOVAL) {
612		/*
613		 * Want to be able to use this to make badblock
614		 * inodes, so don't truncate the dev number.
615		 */
616		ip->i_rdev = vap->va_rdev;
617	}
618	/*
619	 * Remove inode, then reload it through VFS_VGET so it is
620	 * checked to see if it is an alias of an existing entry in
621	 * the inode cache.	 XXX I don't believe this is necessary now.
622	 */
623	(*vpp)->v_type = VNON;
624	ino = ip->i_number;	/* Save this before vgone() invalidates ip. */
625	vgone(*vpp);
626	vput(*vpp);
627	error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
628	if (error) {
629		*vpp = NULL;
630		return (error);
631	}
632	return (0);
633}
634
635static int
636ext2_remove(struct vop_remove_args *ap)
637{
638	struct inode *ip;
639	struct vnode *vp = ap->a_vp;
640	struct vnode *dvp = ap->a_dvp;
641	int error;
642
643	ip = VTOI(vp);
644	if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
645	    (VTOI(dvp)->i_flags & APPEND)) {
646		error = EPERM;
647		goto out;
648	}
649	error = ext2_dirremove(dvp, ap->a_cnp);
650	if (error == 0) {
651		ip->i_nlink--;
652		ip->i_flag |= IN_CHANGE;
653	}
654out:
655	return (error);
656}
657
658/*
659 * link vnode call
660 */
661static int
662ext2_link(struct vop_link_args *ap)
663{
664	struct vnode *vp = ap->a_vp;
665	struct vnode *tdvp = ap->a_tdvp;
666	struct componentname *cnp = ap->a_cnp;
667	struct inode *ip;
668	int error;
669
670#ifdef INVARIANTS
671	if ((cnp->cn_flags & HASBUF) == 0)
672		panic("ext2_link: no name");
673#endif
674	ip = VTOI(vp);
675	if ((nlink_t)ip->i_nlink >= EXT2_LINK_MAX) {
676		error = EMLINK;
677		goto out;
678	}
679	if (ip->i_flags & (IMMUTABLE | APPEND)) {
680		error = EPERM;
681		goto out;
682	}
683	ip->i_nlink++;
684	ip->i_flag |= IN_CHANGE;
685	error = ext2_update(vp, !DOINGASYNC(vp));
686	if (!error)
687		error = ext2_direnter(ip, tdvp, cnp);
688	if (error) {
689		ip->i_nlink--;
690		ip->i_flag |= IN_CHANGE;
691	}
692out:
693	return (error);
694}
695
696/*
697 * Rename system call.
698 * 	rename("foo", "bar");
699 * is essentially
700 *	unlink("bar");
701 *	link("foo", "bar");
702 *	unlink("foo");
703 * but ``atomically''.  Can't do full commit without saving state in the
704 * inode on disk which isn't feasible at this time.  Best we can do is
705 * always guarantee the target exists.
706 *
707 * Basic algorithm is:
708 *
709 * 1) Bump link count on source while we're linking it to the
710 *    target.  This also ensure the inode won't be deleted out
711 *    from underneath us while we work (it may be truncated by
712 *    a concurrent `trunc' or `open' for creation).
713 * 2) Link source to destination.  If destination already exists,
714 *    delete it first.
715 * 3) Unlink source reference to inode if still around. If a
716 *    directory was moved and the parent of the destination
717 *    is different from the source, patch the ".." entry in the
718 *    directory.
719 */
720static int
721ext2_rename(struct vop_rename_args *ap)
722{
723	struct vnode *tvp = ap->a_tvp;
724	struct vnode *tdvp = ap->a_tdvp;
725	struct vnode *fvp = ap->a_fvp;
726	struct vnode *fdvp = ap->a_fdvp;
727	struct componentname *tcnp = ap->a_tcnp;
728	struct componentname *fcnp = ap->a_fcnp;
729	struct inode *ip, *xp, *dp;
730	struct dirtemplate dirbuf;
731	int doingdirectory = 0, oldparent = 0, newparent = 0;
732	int error = 0;
733	u_char namlen;
734
735#ifdef INVARIANTS
736	if ((tcnp->cn_flags & HASBUF) == 0 ||
737	    (fcnp->cn_flags & HASBUF) == 0)
738		panic("ext2_rename: no name");
739#endif
740	/*
741	 * Check for cross-device rename.
742	 */
743	if ((fvp->v_mount != tdvp->v_mount) ||
744	    (tvp && (fvp->v_mount != tvp->v_mount))) {
745		error = EXDEV;
746abortit:
747		if (tdvp == tvp)
748			vrele(tdvp);
749		else
750			vput(tdvp);
751		if (tvp)
752			vput(tvp);
753		vrele(fdvp);
754		vrele(fvp);
755		return (error);
756	}
757
758	if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
759	    (VTOI(tdvp)->i_flags & APPEND))) {
760		error = EPERM;
761		goto abortit;
762	}
763
764	/*
765	 * Renaming a file to itself has no effect.  The upper layers should
766	 * not call us in that case.  Temporarily just warn if they do.
767	 */
768	if (fvp == tvp) {
769		printf("ext2_rename: fvp == tvp (can't happen)\n");
770		error = 0;
771		goto abortit;
772	}
773
774	if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
775		goto abortit;
776	dp = VTOI(fdvp);
777	ip = VTOI(fvp);
778	if (ip->i_nlink >= EXT2_LINK_MAX) {
779		VOP_UNLOCK(fvp, 0);
780		error = EMLINK;
781		goto abortit;
782	}
783	if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
784	    || (dp->i_flags & APPEND)) {
785		VOP_UNLOCK(fvp, 0);
786		error = EPERM;
787		goto abortit;
788	}
789	if ((ip->i_mode & IFMT) == IFDIR) {
790		/*
791		 * Avoid ".", "..", and aliases of "." for obvious reasons.
792		 */
793		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
794		    dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
795		    (ip->i_flag & IN_RENAME)) {
796			VOP_UNLOCK(fvp, 0);
797			error = EINVAL;
798			goto abortit;
799		}
800		ip->i_flag |= IN_RENAME;
801		oldparent = dp->i_number;
802		doingdirectory++;
803	}
804	vrele(fdvp);
805
806	/*
807	 * When the target exists, both the directory
808	 * and target vnodes are returned locked.
809	 */
810	dp = VTOI(tdvp);
811	xp = NULL;
812	if (tvp)
813		xp = VTOI(tvp);
814
815	/*
816	 * 1) Bump link count while we're moving stuff
817	 *    around.  If we crash somewhere before
818	 *    completing our work, the link count
819	 *    may be wrong, but correctable.
820	 */
821	ip->i_nlink++;
822	ip->i_flag |= IN_CHANGE;
823	if ((error = ext2_update(fvp, !DOINGASYNC(fvp))) != 0) {
824		VOP_UNLOCK(fvp, 0);
825		goto bad;
826	}
827
828	/*
829	 * If ".." must be changed (ie the directory gets a new
830	 * parent) then the source directory must not be in the
831	 * directory hierarchy above the target, as this would
832	 * orphan everything below the source directory. Also
833	 * the user must have write permission in the source so
834	 * as to be able to change "..". We must repeat the call
835	 * to namei, as the parent directory is unlocked by the
836	 * call to checkpath().
837	 */
838	error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
839	VOP_UNLOCK(fvp, 0);
840	if (oldparent != dp->i_number)
841		newparent = dp->i_number;
842	if (doingdirectory && newparent) {
843		if (error)	/* write access check above */
844			goto bad;
845		if (xp != NULL)
846			vput(tvp);
847		error = ext2_checkpath(ip, dp, tcnp->cn_cred);
848		if (error)
849			goto out;
850		VREF(tdvp);
851		error = relookup(tdvp, &tvp, tcnp);
852		if (error)
853			goto out;
854		vrele(tdvp);
855		dp = VTOI(tdvp);
856		xp = NULL;
857		if (tvp)
858			xp = VTOI(tvp);
859	}
860	/*
861	 * 2) If target doesn't exist, link the target
862	 *    to the source and unlink the source.
863	 *    Otherwise, rewrite the target directory
864	 *    entry to reference the source inode and
865	 *    expunge the original entry's existence.
866	 */
867	if (xp == NULL) {
868		if (dp->i_devvp != ip->i_devvp)
869			panic("ext2_rename: EXDEV");
870		/*
871		 * Account for ".." in new directory.
872		 * When source and destination have the same
873		 * parent we don't fool with the link count.
874		 */
875		if (doingdirectory && newparent) {
876			if ((nlink_t)dp->i_nlink >= EXT2_LINK_MAX) {
877				error = EMLINK;
878				goto bad;
879			}
880			dp->i_nlink++;
881			dp->i_flag |= IN_CHANGE;
882			error = ext2_update(tdvp, !DOINGASYNC(tdvp));
883			if (error)
884				goto bad;
885		}
886		error = ext2_direnter(ip, tdvp, tcnp);
887		if (error) {
888			if (doingdirectory && newparent) {
889				dp->i_nlink--;
890				dp->i_flag |= IN_CHANGE;
891				(void)ext2_update(tdvp, 1);
892			}
893			goto bad;
894		}
895		vput(tdvp);
896	} else {
897		if (xp->i_devvp != dp->i_devvp || xp->i_devvp != ip->i_devvp)
898		       panic("ext2_rename: EXDEV");
899		/*
900		 * Short circuit rename(foo, foo).
901		 */
902		if (xp->i_number == ip->i_number)
903			panic("ext2_rename: same file");
904		/*
905		 * If the parent directory is "sticky", then the user must
906		 * own the parent directory, or the destination of the rename,
907		 * otherwise the destination may not be changed (except by
908		 * root). This implements append-only directories.
909		 */
910		if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
911		    tcnp->cn_cred->cr_uid != dp->i_uid &&
912		    xp->i_uid != tcnp->cn_cred->cr_uid) {
913			error = EPERM;
914			goto bad;
915		}
916		/*
917		 * Target must be empty if a directory and have no links
918		 * to it. Also, ensure source and target are compatible
919		 * (both directories, or both not directories).
920		 */
921		if ((xp->i_mode&IFMT) == IFDIR) {
922			if (! ext2_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
923			    xp->i_nlink > 2) {
924				error = ENOTEMPTY;
925				goto bad;
926			}
927			if (!doingdirectory) {
928				error = ENOTDIR;
929				goto bad;
930			}
931			cache_purge(tdvp);
932		} else if (doingdirectory) {
933			error = EISDIR;
934			goto bad;
935		}
936		error = ext2_dirrewrite(dp, ip, tcnp);
937		if (error)
938			goto bad;
939		/*
940		 * If the target directory is in the same
941		 * directory as the source directory,
942		 * decrement the link count on the parent
943		 * of the target directory.
944		 */
945		if (doingdirectory && !newparent) {
946			dp->i_nlink--;
947			dp->i_flag |= IN_CHANGE;
948		}
949		vput(tdvp);
950		/*
951		 * Adjust the link count of the target to
952		 * reflect the dirrewrite above.  If this is
953		 * a directory it is empty and there are
954		 * no links to it, so we can squash the inode and
955		 * any space associated with it.  We disallowed
956		 * renaming over top of a directory with links to
957		 * it above, as the remaining link would point to
958		 * a directory without "." or ".." entries.
959		 */
960		xp->i_nlink--;
961		if (doingdirectory) {
962			if (--xp->i_nlink != 0)
963				panic("ext2_rename: linked directory");
964			error = ext2_truncate(tvp, (off_t)0, IO_SYNC,
965			    tcnp->cn_cred, tcnp->cn_thread);
966		}
967		xp->i_flag |= IN_CHANGE;
968		vput(tvp);
969		xp = NULL;
970	}
971
972	/*
973	 * 3) Unlink the source.
974	 */
975	fcnp->cn_flags &= ~MODMASK;
976	fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
977	VREF(fdvp);
978	error = relookup(fdvp, &fvp, fcnp);
979	if (error == 0)
980		vrele(fdvp);
981	if (fvp != NULL) {
982		xp = VTOI(fvp);
983		dp = VTOI(fdvp);
984	} else {
985		/*
986		 * From name has disappeared.
987		 */
988		if (doingdirectory)
989			panic("ext2_rename: lost dir entry");
990		vrele(ap->a_fvp);
991		return (0);
992	}
993	/*
994	 * Ensure that the directory entry still exists and has not
995	 * changed while the new name has been entered. If the source is
996	 * a file then the entry may have been unlinked or renamed. In
997	 * either case there is no further work to be done. If the source
998	 * is a directory then it cannot have been rmdir'ed; its link
999	 * count of three would cause a rmdir to fail with ENOTEMPTY.
1000	 * The IN_RENAME flag ensures that it cannot be moved by another
1001	 * rename.
1002	 */
1003	if (xp != ip) {
1004		if (doingdirectory)
1005			panic("ext2_rename: lost dir entry");
1006	} else {
1007		/*
1008		 * If the source is a directory with a
1009		 * new parent, the link count of the old
1010		 * parent directory must be decremented
1011		 * and ".." set to point to the new parent.
1012		 */
1013		if (doingdirectory && newparent) {
1014			dp->i_nlink--;
1015			dp->i_flag |= IN_CHANGE;
1016			error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
1017				sizeof(struct dirtemplate), (off_t)0,
1018				UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1019				tcnp->cn_cred, NOCRED, NULL, NULL);
1020			if (error == 0) {
1021				/* Like ufs little-endian: */
1022				namlen = dirbuf.dotdot_type;
1023				if (namlen != 2 ||
1024				    dirbuf.dotdot_name[0] != '.' ||
1025				    dirbuf.dotdot_name[1] != '.') {
1026					ext2_dirbad(xp, (doff_t)12,
1027					    "rename: mangled dir");
1028				} else {
1029					dirbuf.dotdot_ino = newparent;
1030					(void) vn_rdwr(UIO_WRITE, fvp,
1031					    (caddr_t)&dirbuf,
1032					    sizeof(struct dirtemplate),
1033					    (off_t)0, UIO_SYSSPACE,
1034					    IO_NODELOCKED | IO_SYNC |
1035					    IO_NOMACCHECK, tcnp->cn_cred,
1036					    NOCRED, NULL, NULL);
1037					cache_purge(fdvp);
1038				}
1039			}
1040		}
1041		error = ext2_dirremove(fdvp, fcnp);
1042		if (!error) {
1043			xp->i_nlink--;
1044			xp->i_flag |= IN_CHANGE;
1045		}
1046		xp->i_flag &= ~IN_RENAME;
1047	}
1048	if (dp)
1049		vput(fdvp);
1050	if (xp)
1051		vput(fvp);
1052	vrele(ap->a_fvp);
1053	return (error);
1054
1055bad:
1056	if (xp)
1057		vput(ITOV(xp));
1058	vput(ITOV(dp));
1059out:
1060	if (doingdirectory)
1061		ip->i_flag &= ~IN_RENAME;
1062	if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1063		ip->i_nlink--;
1064		ip->i_flag |= IN_CHANGE;
1065		ip->i_flag &= ~IN_RENAME;
1066		vput(fvp);
1067	} else
1068		vrele(fvp);
1069	return (error);
1070}
1071
1072/*
1073 * Mkdir system call
1074 */
1075static int
1076ext2_mkdir(struct vop_mkdir_args *ap)
1077{
1078	struct vnode *dvp = ap->a_dvp;
1079	struct vattr *vap = ap->a_vap;
1080	struct componentname *cnp = ap->a_cnp;
1081	struct inode *ip, *dp;
1082	struct vnode *tvp;
1083	struct dirtemplate dirtemplate, *dtp;
1084	int error, dmode;
1085
1086#ifdef INVARIANTS
1087	if ((cnp->cn_flags & HASBUF) == 0)
1088		panic("ext2_mkdir: no name");
1089#endif
1090	dp = VTOI(dvp);
1091	if ((nlink_t)dp->i_nlink >= EXT2_LINK_MAX) {
1092		error = EMLINK;
1093		goto out;
1094	}
1095	dmode = vap->va_mode & 0777;
1096	dmode |= IFDIR;
1097	/*
1098	 * Must simulate part of ext2_makeinode here to acquire the inode,
1099	 * but not have it entered in the parent directory. The entry is
1100	 * made later after writing "." and ".." entries.
1101	 */
1102	error = ext2_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1103	if (error)
1104		goto out;
1105	ip = VTOI(tvp);
1106	ip->i_gid = dp->i_gid;
1107#ifdef SUIDDIR
1108	{
1109		/*
1110		 * if we are hacking owners here, (only do this where told to)
1111		 * and we are not giving it TOO root, (would subvert quotas)
1112		 * then go ahead and give it to the other user.
1113		 * The new directory also inherits the SUID bit.
1114		 * If user's UID and dir UID are the same,
1115		 * 'give it away' so that the SUID is still forced on.
1116		 */
1117		if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1118		   (dp->i_mode & ISUID) && dp->i_uid) {
1119			dmode |= ISUID;
1120			ip->i_uid = dp->i_uid;
1121		} else {
1122			ip->i_uid = cnp->cn_cred->cr_uid;
1123		}
1124	}
1125#else
1126	ip->i_uid = cnp->cn_cred->cr_uid;
1127#endif
1128	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1129	ip->i_mode = dmode;
1130	tvp->v_type = VDIR;	/* Rest init'd in getnewvnode(). */
1131	ip->i_nlink = 2;
1132	if (cnp->cn_flags & ISWHITEOUT)
1133		ip->i_flags |= UF_OPAQUE;
1134	error = ext2_update(tvp, 1);
1135
1136	/*
1137	 * Bump link count in parent directory
1138	 * to reflect work done below.  Should
1139	 * be done before reference is created
1140	 * so reparation is possible if we crash.
1141	 */
1142	dp->i_nlink++;
1143	dp->i_flag |= IN_CHANGE;
1144	error = ext2_update(dvp, !DOINGASYNC(dvp));
1145	if (error)
1146		goto bad;
1147
1148	/* Initialize directory with "." and ".." from static template. */
1149	if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
1150	    EXT2F_INCOMPAT_FTYPE))
1151		dtp = &mastertemplate;
1152	else
1153		dtp = &omastertemplate;
1154	dirtemplate = *dtp;
1155	dirtemplate.dot_ino = ip->i_number;
1156	dirtemplate.dotdot_ino = dp->i_number;
1157	/* note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE
1158	 * so let's just redefine it - for this function only
1159	 */
1160#undef  DIRBLKSIZ
1161#define DIRBLKSIZ  VTOI(dvp)->i_e2fs->e2fs_bsize
1162	dirtemplate.dotdot_reclen = DIRBLKSIZ - 12;
1163	error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
1164	    sizeof(dirtemplate), (off_t)0, UIO_SYSSPACE,
1165	    IO_NODELOCKED | IO_SYNC | IO_NOMACCHECK, cnp->cn_cred, NOCRED,
1166	    NULL, NULL);
1167	if (error) {
1168		dp->i_nlink--;
1169		dp->i_flag |= IN_CHANGE;
1170		goto bad;
1171	}
1172	if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1173		/* XXX should grow with balloc() */
1174		panic("ext2_mkdir: blksize");
1175	else {
1176		ip->i_size = DIRBLKSIZ;
1177		ip->i_flag |= IN_CHANGE;
1178	}
1179
1180	/* Directory set up, now install its entry in the parent directory. */
1181	error = ext2_direnter(ip, dvp, cnp);
1182	if (error) {
1183		dp->i_nlink--;
1184		dp->i_flag |= IN_CHANGE;
1185	}
1186bad:
1187	/*
1188	 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1189	 * for us because we set the link count to 0.
1190	 */
1191	if (error) {
1192		ip->i_nlink = 0;
1193		ip->i_flag |= IN_CHANGE;
1194		vput(tvp);
1195	} else
1196		*ap->a_vpp = tvp;
1197out:
1198	return (error);
1199#undef  DIRBLKSIZ
1200#define DIRBLKSIZ  DEV_BSIZE
1201}
1202
1203/*
1204 * Rmdir system call.
1205 */
1206static int
1207ext2_rmdir(struct vop_rmdir_args *ap)
1208{
1209	struct vnode *vp = ap->a_vp;
1210	struct vnode *dvp = ap->a_dvp;
1211	struct componentname *cnp = ap->a_cnp;
1212	struct inode *ip, *dp;
1213	int error;
1214
1215	ip = VTOI(vp);
1216	dp = VTOI(dvp);
1217
1218	/*
1219	 * Verify the directory is empty (and valid).
1220	 * (Rmdir ".." won't be valid since
1221	 *  ".." will contain a reference to
1222	 *  the current directory and thus be
1223	 *  non-empty.)
1224	 */
1225	error = 0;
1226	if (ip->i_nlink != 2 || !ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1227		error = ENOTEMPTY;
1228		goto out;
1229	}
1230	if ((dp->i_flags & APPEND)
1231	    || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1232		error = EPERM;
1233		goto out;
1234	}
1235	/*
1236	 * Delete reference to directory before purging
1237	 * inode.  If we crash in between, the directory
1238	 * will be reattached to lost+found,
1239	 */
1240	error = ext2_dirremove(dvp, cnp);
1241	if (error)
1242		goto out;
1243	dp->i_nlink--;
1244	dp->i_flag |= IN_CHANGE;
1245	cache_purge(dvp);
1246	VOP_UNLOCK(dvp, 0);
1247	/*
1248	 * Truncate inode.  The only stuff left
1249	 * in the directory is "." and "..".  The
1250	 * "." reference is inconsequential since
1251	 * we're quashing it.  The ".." reference
1252	 * has already been adjusted above.  We've
1253	 * removed the "." reference and the reference
1254	 * in the parent directory, but there may be
1255	 * other hard links so decrement by 2 and
1256	 * worry about them later.
1257	 */
1258	ip->i_nlink -= 2;
1259	error = ext2_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred,
1260	    cnp->cn_thread);
1261	cache_purge(ITOV(ip));
1262	if (vn_lock(dvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1263		VOP_UNLOCK(vp, 0);
1264		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1265		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1266	}
1267out:
1268	return (error);
1269}
1270
1271/*
1272 * symlink -- make a symbolic link
1273 */
1274static int
1275ext2_symlink(struct vop_symlink_args *ap)
1276{
1277	struct vnode *vp, **vpp = ap->a_vpp;
1278	struct inode *ip;
1279	int len, error;
1280
1281	error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1282	    vpp, ap->a_cnp);
1283	if (error)
1284		return (error);
1285	vp = *vpp;
1286	len = strlen(ap->a_target);
1287	if (len < vp->v_mount->mnt_maxsymlinklen) {
1288		ip = VTOI(vp);
1289		bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1290		ip->i_size = len;
1291		ip->i_flag |= IN_CHANGE | IN_UPDATE;
1292	} else
1293		error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1294		    UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1295		    ap->a_cnp->cn_cred, NOCRED, NULL, NULL);
1296	if (error)
1297		vput(vp);
1298	return (error);
1299}
1300
1301/*
1302 * Return target name of a symbolic link
1303 */
1304static int
1305ext2_readlink(struct vop_readlink_args *ap)
1306{
1307	struct vnode *vp = ap->a_vp;
1308	struct inode *ip = VTOI(vp);
1309	int isize;
1310
1311	isize = ip->i_size;
1312	if (isize < vp->v_mount->mnt_maxsymlinklen) {
1313		uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1314		return (0);
1315	}
1316	return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1317}
1318
1319/*
1320 * Calculate the logical to physical mapping if not done already,
1321 * then call the device strategy routine.
1322 *
1323 * In order to be able to swap to a file, the ext2_bmaparray() operation may not
1324 * deadlock on memory.  See ext2_bmap() for details.
1325 */
1326static int
1327ext2_strategy(struct vop_strategy_args *ap)
1328{
1329	struct buf *bp = ap->a_bp;
1330	struct vnode *vp = ap->a_vp;
1331	struct inode *ip;
1332	struct bufobj *bo;
1333	daddr_t blkno;
1334	int error;
1335
1336	ip = VTOI(vp);
1337	if (vp->v_type == VBLK || vp->v_type == VCHR)
1338		panic("ext2_strategy: spec");
1339	if (bp->b_blkno == bp->b_lblkno) {
1340		error = ext2_bmaparray(vp, bp->b_lblkno, &blkno, NULL, NULL);
1341		bp->b_blkno = blkno;
1342		if (error) {
1343			bp->b_error = error;
1344			bp->b_ioflags |= BIO_ERROR;
1345			bufdone(bp);
1346			return (0);
1347		}
1348		if ((long)bp->b_blkno == -1)
1349			vfs_bio_clrbuf(bp);
1350	}
1351	if ((long)bp->b_blkno == -1) {
1352		bufdone(bp);
1353		return (0);
1354	}
1355	bp->b_iooffset = dbtob(bp->b_blkno);
1356	bo = VFSTOEXT2(vp->v_mount)->um_bo;
1357	BO_STRATEGY(bo, bp);
1358	return (0);
1359}
1360
1361/*
1362 * Print out the contents of an inode.
1363 */
1364static int
1365ext2_print(struct vop_print_args *ap)
1366{
1367	struct vnode *vp = ap->a_vp;
1368	struct inode *ip = VTOI(vp);
1369
1370	vn_printf(ip->i_devvp, "\tino %lu", (u_long)ip->i_number);
1371	if (vp->v_type == VFIFO)
1372		fifo_printinfo(vp);
1373	printf("\n");
1374	return (0);
1375}
1376
1377/*
1378 * Close wrapper for fifos.
1379 *
1380 * Update the times on the inode then do device close.
1381 */
1382static int
1383ext2fifo_close(struct vop_close_args *ap)
1384{
1385	struct vnode *vp = ap->a_vp;
1386
1387	VI_LOCK(vp);
1388	if (vp->v_usecount > 1)
1389		ext2_itimes_locked(vp);
1390	VI_UNLOCK(vp);
1391	return (fifo_specops.vop_close(ap));
1392}
1393
1394/*
1395 * Kqfilter wrapper for fifos.
1396 *
1397 * Fall through to ext2 kqfilter routines if needed
1398 */
1399static int
1400ext2fifo_kqfilter(struct vop_kqfilter_args *ap)
1401{
1402	int error;
1403
1404	error = fifo_specops.vop_kqfilter(ap);
1405	if (error)
1406		error = vfs_kqfilter(ap);
1407	return (error);
1408}
1409
1410/*
1411 * Return POSIX pathconf information applicable to ext2 filesystems.
1412 */
1413static int
1414ext2_pathconf(struct vop_pathconf_args *ap)
1415{
1416	int error = 0;
1417
1418	switch (ap->a_name) {
1419	case _PC_LINK_MAX:
1420		*ap->a_retval = EXT2_LINK_MAX;
1421		break;
1422	case _PC_NAME_MAX:
1423		*ap->a_retval = NAME_MAX;
1424		break;
1425	case _PC_PATH_MAX:
1426		*ap->a_retval = PATH_MAX;
1427		break;
1428	case _PC_PIPE_BUF:
1429		*ap->a_retval = PIPE_BUF;
1430		break;
1431	case _PC_CHOWN_RESTRICTED:
1432		*ap->a_retval = 1;
1433		break;
1434	case _PC_NO_TRUNC:
1435		*ap->a_retval = 1;
1436		break;
1437	case _PC_MIN_HOLE_SIZE:
1438		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1439		break;
1440	case _PC_ASYNC_IO:
1441		/* _PC_ASYNC_IO should have been handled by upper layers. */
1442		KASSERT(0, ("_PC_ASYNC_IO should not get here"));
1443		error = EINVAL;
1444		break;
1445	case _PC_PRIO_IO:
1446		*ap->a_retval = 0;
1447		break;
1448	case _PC_SYNC_IO:
1449		*ap->a_retval = 0;
1450		break;
1451	case _PC_ALLOC_SIZE_MIN:
1452		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
1453		break;
1454	case _PC_FILESIZEBITS:
1455		*ap->a_retval = 64;
1456		break;
1457	case _PC_REC_INCR_XFER_SIZE:
1458		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1459		break;
1460	case _PC_REC_MAX_XFER_SIZE:
1461		*ap->a_retval = -1; /* means ``unlimited'' */
1462		break;
1463	case _PC_REC_MIN_XFER_SIZE:
1464		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1465		break;
1466	case _PC_REC_XFER_ALIGN:
1467		*ap->a_retval = PAGE_SIZE;
1468		break;
1469	case _PC_SYMLINK_MAX:
1470		*ap->a_retval = MAXPATHLEN;
1471		break;
1472
1473	default:
1474		error = EINVAL;
1475		break;
1476	}
1477	return (error);
1478}
1479
1480/*
1481 * Vnode pointer to File handle
1482 */
1483/* ARGSUSED */
1484static int
1485ext2_vptofh(struct vop_vptofh_args *ap)
1486{
1487	struct inode *ip;
1488	struct ufid *ufhp;
1489
1490	ip = VTOI(ap->a_vp);
1491	ufhp = (struct ufid *)ap->a_fhp;
1492	ufhp->ufid_len = sizeof(struct ufid);
1493	ufhp->ufid_ino = ip->i_number;
1494	ufhp->ufid_gen = ip->i_gen;
1495	return (0);
1496}
1497
1498/*
1499 * Initialize the vnode associated with a new inode, handle aliased
1500 * vnodes.
1501 */
1502int
1503ext2_vinit(struct mount *mntp, struct vop_vector *fifoops, struct vnode **vpp)
1504{
1505	struct inode *ip;
1506	struct vnode *vp;
1507
1508	vp = *vpp;
1509	ip = VTOI(vp);
1510	vp->v_type = IFTOVT(ip->i_mode);
1511	if (vp->v_type == VFIFO)
1512		vp->v_op = fifoops;
1513
1514	if (ip->i_number == EXT2_ROOTINO)
1515		vp->v_vflag |= VV_ROOT;
1516	ip->i_modrev = init_va_filerev();
1517	*vpp = vp;
1518	return (0);
1519}
1520
1521/*
1522 * Allocate a new inode.
1523 */
1524static int
1525ext2_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
1526    struct componentname *cnp)
1527{
1528	struct inode *ip, *pdir;
1529	struct vnode *tvp;
1530	int error;
1531
1532	pdir = VTOI(dvp);
1533#ifdef INVARIANTS
1534	if ((cnp->cn_flags & HASBUF) == 0)
1535		panic("ext2_makeinode: no name");
1536#endif
1537	*vpp = NULL;
1538	if ((mode & IFMT) == 0)
1539		mode |= IFREG;
1540
1541	error = ext2_valloc(dvp, mode, cnp->cn_cred, &tvp);
1542	if (error) {
1543		return (error);
1544	}
1545	ip = VTOI(tvp);
1546	ip->i_gid = pdir->i_gid;
1547#ifdef SUIDDIR
1548	{
1549		/*
1550		 * if we are
1551		 * not the owner of the directory,
1552		 * and we are hacking owners here, (only do this where told to)
1553		 * and we are not giving it TOO root, (would subvert quotas)
1554		 * then go ahead and give it to the other user.
1555		 * Note that this drops off the execute bits for security.
1556		 */
1557		if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1558		     (pdir->i_mode & ISUID) &&
1559		     (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1560			ip->i_uid = pdir->i_uid;
1561			mode &= ~07111;
1562		} else {
1563			ip->i_uid = cnp->cn_cred->cr_uid;
1564		}
1565	}
1566#else
1567	ip->i_uid = cnp->cn_cred->cr_uid;
1568#endif
1569	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1570	ip->i_mode = mode;
1571	tvp->v_type = IFTOVT(mode);	/* Rest init'd in getnewvnode(). */
1572	ip->i_nlink = 1;
1573	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred)) {
1574		if (priv_check_cred(cnp->cn_cred, PRIV_VFS_RETAINSUGID, 0))
1575			ip->i_mode &= ~ISGID;
1576	}
1577
1578	if (cnp->cn_flags & ISWHITEOUT)
1579		ip->i_flags |= UF_OPAQUE;
1580
1581	/*
1582	 * Make sure inode goes to disk before directory entry.
1583	 */
1584	error = ext2_update(tvp, !DOINGASYNC(tvp));
1585	if (error)
1586		goto bad;
1587	error = ext2_direnter(ip, dvp, cnp);
1588	if (error)
1589		goto bad;
1590
1591	*vpp = tvp;
1592	return (0);
1593
1594bad:
1595	/*
1596	 * Write error occurred trying to update the inode
1597	 * or the directory so must deallocate the inode.
1598	 */
1599	ip->i_nlink = 0;
1600	ip->i_flag |= IN_CHANGE;
1601	vput(tvp);
1602	return (error);
1603}
1604
1605/*
1606 * Vnode op for reading.
1607 */
1608static int
1609ext2_read(struct vop_read_args *ap)
1610{
1611	struct vnode *vp;
1612	struct inode *ip;
1613	int error;
1614
1615	vp = ap->a_vp;
1616	ip = VTOI(vp);
1617
1618	/*EXT4_EXT_LOCK(ip);*/
1619	if (ip->i_flag & IN_E4EXTENTS)
1620		error = ext4_ext_read(ap);
1621	else
1622		error = ext2_ind_read(ap);
1623	/*EXT4_EXT_UNLOCK(ip);*/
1624	return (error);
1625}
1626
1627/*
1628 * Vnode op for reading.
1629 */
1630static int
1631ext2_ind_read(struct vop_read_args *ap)
1632{
1633	struct vnode *vp;
1634	struct inode *ip;
1635	struct uio *uio;
1636	struct m_ext2fs *fs;
1637	struct buf *bp;
1638	daddr_t lbn, nextlbn;
1639	off_t bytesinfile;
1640	long size, xfersize, blkoffset;
1641	int error, orig_resid, seqcount;
1642	int ioflag;
1643
1644	vp = ap->a_vp;
1645	uio = ap->a_uio;
1646	ioflag = ap->a_ioflag;
1647
1648	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
1649	ip = VTOI(vp);
1650
1651#ifdef INVARIANTS
1652	if (uio->uio_rw != UIO_READ)
1653		panic("%s: mode", "ext2_read");
1654
1655	if (vp->v_type == VLNK) {
1656		if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
1657			panic("%s: short symlink", "ext2_read");
1658	} else if (vp->v_type != VREG && vp->v_type != VDIR)
1659		panic("%s: type %d", "ext2_read", vp->v_type);
1660#endif
1661	orig_resid = uio->uio_resid;
1662	KASSERT(orig_resid >= 0, ("ext2_read: uio->uio_resid < 0"));
1663	if (orig_resid == 0)
1664		return (0);
1665	KASSERT(uio->uio_offset >= 0, ("ext2_read: uio->uio_offset < 0"));
1666	fs = ip->i_e2fs;
1667	if (uio->uio_offset < ip->i_size &&
1668	    uio->uio_offset >= fs->e2fs_maxfilesize)
1669	    	return (EOVERFLOW);
1670
1671	for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
1672		if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
1673			break;
1674		lbn = lblkno(fs, uio->uio_offset);
1675		nextlbn = lbn + 1;
1676		size = blksize(fs, ip, lbn);
1677		blkoffset = blkoff(fs, uio->uio_offset);
1678
1679		xfersize = fs->e2fs_fsize - blkoffset;
1680		if (uio->uio_resid < xfersize)
1681			xfersize = uio->uio_resid;
1682		if (bytesinfile < xfersize)
1683			xfersize = bytesinfile;
1684
1685		if (lblktosize(fs, nextlbn) >= ip->i_size)
1686			error = bread(vp, lbn, size, NOCRED, &bp);
1687		else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
1688			error = cluster_read(vp, ip->i_size, lbn, size,
1689			    NOCRED, blkoffset + uio->uio_resid, seqcount,
1690			    0, &bp);
1691		} else if (seqcount > 1) {
1692			u_int nextsize = blksize(fs, ip, nextlbn);
1693			error = breadn(vp, lbn,
1694			    size, &nextlbn, &nextsize, 1, NOCRED, &bp);
1695		} else
1696			error = bread(vp, lbn, size, NOCRED, &bp);
1697		if (error) {
1698			brelse(bp);
1699			bp = NULL;
1700			break;
1701		}
1702
1703		/*
1704		 * If IO_DIRECT then set B_DIRECT for the buffer.  This
1705		 * will cause us to attempt to release the buffer later on
1706		 * and will cause the buffer cache to attempt to free the
1707		 * underlying pages.
1708		 */
1709		if (ioflag & IO_DIRECT)
1710			bp->b_flags |= B_DIRECT;
1711
1712		/*
1713		 * We should only get non-zero b_resid when an I/O error
1714		 * has occurred, which should cause us to break above.
1715		 * However, if the short read did not cause an error,
1716		 * then we want to ensure that we do not uiomove bad
1717		 * or uninitialized data.
1718		 */
1719		size -= bp->b_resid;
1720		if (size < xfersize) {
1721			if (size == 0)
1722				break;
1723			xfersize = size;
1724		}
1725		error = uiomove((char *)bp->b_data + blkoffset,
1726			(int)xfersize, uio);
1727		if (error)
1728			break;
1729
1730		if (ioflag & (IO_VMIO|IO_DIRECT)) {
1731			/*
1732			 * If it's VMIO or direct I/O, then we don't
1733			 * need the buf, mark it available for
1734			 * freeing. If it's non-direct VMIO, the VM has
1735			 * the data.
1736			 */
1737			bp->b_flags |= B_RELBUF;
1738			brelse(bp);
1739		} else {
1740			/*
1741			 * Otherwise let whoever
1742			 * made the request take care of
1743			 * freeing it. We just queue
1744			 * it onto another list.
1745			 */
1746			bqrelse(bp);
1747		}
1748	}
1749
1750	/*
1751	 * This can only happen in the case of an error
1752	 * because the loop above resets bp to NULL on each iteration
1753	 * and on normal completion has not set a new value into it.
1754	 * so it must have come from a 'break' statement
1755	 */
1756	if (bp != NULL) {
1757		if (ioflag & (IO_VMIO|IO_DIRECT)) {
1758			bp->b_flags |= B_RELBUF;
1759			brelse(bp);
1760		} else {
1761			bqrelse(bp);
1762		}
1763	}
1764
1765	if ((error == 0 || uio->uio_resid != orig_resid) &&
1766	    (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
1767		ip->i_flag |= IN_ACCESS;
1768	return (error);
1769}
1770
1771static int
1772ext2_ioctl(struct vop_ioctl_args *ap)
1773{
1774
1775	switch (ap->a_command) {
1776	case FIOSEEKDATA:
1777	case FIOSEEKHOLE:
1778		return (vn_bmap_seekhole(ap->a_vp, ap->a_command,
1779		    (off_t *)ap->a_data, ap->a_cred));
1780	default:
1781		return (ENOTTY);
1782	}
1783}
1784
1785/*
1786 * this function handles ext4 extents block mapping
1787 */
1788static int
1789ext4_ext_read(struct vop_read_args *ap)
1790{
1791	struct vnode *vp;
1792	struct inode *ip;
1793	struct uio *uio;
1794	struct m_ext2fs *fs;
1795	struct buf *bp;
1796	struct ext4_extent nex, *ep;
1797	struct ext4_extent_path path;
1798	daddr_t lbn, newblk;
1799	off_t bytesinfile;
1800	int cache_type;
1801	ssize_t orig_resid;
1802	int error;
1803	long size, xfersize, blkoffset;
1804
1805	vp = ap->a_vp;
1806	ip = VTOI(vp);
1807	uio = ap->a_uio;
1808	memset(&path, 0, sizeof(path));
1809
1810	orig_resid = uio->uio_resid;
1811	KASSERT(orig_resid >= 0, ("%s: uio->uio_resid < 0", __func__));
1812	if (orig_resid == 0)
1813		return (0);
1814	KASSERT(uio->uio_offset >= 0, ("%s: uio->uio_offset < 0", __func__));
1815	fs = ip->i_e2fs;
1816	if (uio->uio_offset < ip->i_size && uio->uio_offset >= fs->e2fs_maxfilesize)
1817		return (EOVERFLOW);
1818
1819	while (uio->uio_resid > 0) {
1820		if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
1821			break;
1822		lbn = lblkno(fs, uio->uio_offset);
1823		size = blksize(fs, ip, lbn);
1824		blkoffset = blkoff(fs, uio->uio_offset);
1825
1826		xfersize = fs->e2fs_fsize - blkoffset;
1827		xfersize = MIN(xfersize, uio->uio_resid);
1828		xfersize = MIN(xfersize, bytesinfile);
1829
1830		/* get block from ext4 extent cache */
1831		cache_type = ext4_ext_in_cache(ip, lbn, &nex);
1832		switch (cache_type) {
1833		case EXT4_EXT_CACHE_NO:
1834			ext4_ext_find_extent(fs, ip, lbn, &path);
1835			ep = path.ep_ext;
1836			if (ep == NULL)
1837				return (EIO);
1838
1839			ext4_ext_put_cache(ip, ep, EXT4_EXT_CACHE_IN);
1840
1841			newblk = lbn - ep->e_blk + (ep->e_start_lo |
1842			    (daddr_t)ep->e_start_hi << 32);
1843
1844			if (path.ep_bp != NULL) {
1845				brelse(path.ep_bp);
1846				path.ep_bp = NULL;
1847			}
1848			break;
1849
1850		case EXT4_EXT_CACHE_GAP:
1851			/* block has not been allocated yet */
1852			return (0);
1853
1854		case EXT4_EXT_CACHE_IN:
1855			newblk = lbn - nex.e_blk + (nex.e_start_lo |
1856			    (daddr_t)nex.e_start_hi << 32);
1857			break;
1858
1859		default:
1860			panic("%s: invalid cache type", __func__);
1861		}
1862
1863		error = bread(ip->i_devvp, fsbtodb(fs, newblk), size, NOCRED, &bp);
1864		if (error) {
1865			brelse(bp);
1866			return (error);
1867		}
1868
1869		size -= bp->b_resid;
1870		if (size < xfersize) {
1871			if (size == 0) {
1872				bqrelse(bp);
1873				break;
1874			}
1875			xfersize = size;
1876		}
1877		error = uiomove(bp->b_data + blkoffset, (int)xfersize, uio);
1878		bqrelse(bp);
1879		if (error)
1880			return (error);
1881	}
1882
1883	return (0);
1884}
1885
1886/*
1887 * Vnode op for writing.
1888 */
1889static int
1890ext2_write(struct vop_write_args *ap)
1891{
1892	struct vnode *vp;
1893	struct uio *uio;
1894	struct inode *ip;
1895	struct m_ext2fs *fs;
1896	struct buf *bp;
1897	daddr_t lbn;
1898	off_t osize;
1899	int blkoffset, error, flags, ioflag, resid, size, seqcount, xfersize;
1900
1901	ioflag = ap->a_ioflag;
1902	uio = ap->a_uio;
1903	vp = ap->a_vp;
1904
1905	seqcount = ioflag >> IO_SEQSHIFT;
1906	ip = VTOI(vp);
1907
1908#ifdef INVARIANTS
1909	if (uio->uio_rw != UIO_WRITE)
1910		panic("%s: mode", "ext2_write");
1911#endif
1912
1913	switch (vp->v_type) {
1914	case VREG:
1915		if (ioflag & IO_APPEND)
1916			uio->uio_offset = ip->i_size;
1917		if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
1918			return (EPERM);
1919		/* FALLTHROUGH */
1920	case VLNK:
1921		break;
1922	case VDIR:
1923		/* XXX differs from ffs -- this is called from ext2_mkdir(). */
1924		if ((ioflag & IO_SYNC) == 0)
1925		panic("ext2_write: nonsync dir write");
1926		break;
1927	default:
1928		panic("ext2_write: type %p %d (%jd,%jd)", (void *)vp,
1929		    vp->v_type, (intmax_t)uio->uio_offset,
1930		    (intmax_t)uio->uio_resid);
1931	}
1932
1933	KASSERT(uio->uio_resid >= 0, ("ext2_write: uio->uio_resid < 0"));
1934	KASSERT(uio->uio_offset >= 0, ("ext2_write: uio->uio_offset < 0"));
1935	fs = ip->i_e2fs;
1936	if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->e2fs_maxfilesize)
1937		return (EFBIG);
1938	/*
1939	 * Maybe this should be above the vnode op call, but so long as
1940	 * file servers have no limits, I don't think it matters.
1941	 */
1942	if (vn_rlimit_fsize(vp, uio, uio->uio_td))
1943		return (EFBIG);
1944
1945	resid = uio->uio_resid;
1946	osize = ip->i_size;
1947	if (seqcount > BA_SEQMAX)
1948		flags = BA_SEQMAX << BA_SEQSHIFT;
1949	else
1950		flags = seqcount << BA_SEQSHIFT;
1951	if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
1952		flags |= IO_SYNC;
1953
1954	for (error = 0; uio->uio_resid > 0;) {
1955		lbn = lblkno(fs, uio->uio_offset);
1956		blkoffset = blkoff(fs, uio->uio_offset);
1957		xfersize = fs->e2fs_fsize - blkoffset;
1958		if (uio->uio_resid < xfersize)
1959			xfersize = uio->uio_resid;
1960		if (uio->uio_offset + xfersize > ip->i_size)
1961			vnode_pager_setsize(vp, uio->uio_offset + xfersize);
1962
1963		/*
1964		 * We must perform a read-before-write if the transfer size
1965		 * does not cover the entire buffer.
1966		 */
1967		if (fs->e2fs_bsize > xfersize)
1968			flags |= BA_CLRBUF;
1969		else
1970			flags &= ~BA_CLRBUF;
1971		error = ext2_balloc(ip, lbn, blkoffset + xfersize,
1972		    ap->a_cred, &bp, flags);
1973		if (error != 0)
1974			break;
1975
1976		if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
1977			bp->b_flags |= B_NOCACHE;
1978		if (uio->uio_offset + xfersize > ip->i_size)
1979			ip->i_size = uio->uio_offset + xfersize;
1980		size = blksize(fs, ip, lbn) - bp->b_resid;
1981		if (size < xfersize)
1982			xfersize = size;
1983
1984		error =
1985		    uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
1986		/*
1987		 * If the buffer is not already filled and we encounter an
1988		 * error while trying to fill it, we have to clear out any
1989		 * garbage data from the pages instantiated for the buffer.
1990		 * If we do not, a failed uiomove() during a write can leave
1991		 * the prior contents of the pages exposed to a userland mmap.
1992		 *
1993		 * Note that we need only clear buffers with a transfer size
1994		 * equal to the block size because buffers with a shorter
1995		 * transfer size were cleared above by the call to ext2_balloc()
1996		 * with the BA_CLRBUF flag set.
1997		 *
1998		 * If the source region for uiomove identically mmaps the
1999		 * buffer, uiomove() performed the NOP copy, and the buffer
2000		 * content remains valid because the page fault handler
2001		 * validated the pages.
2002		 */
2003		if (error != 0 && (bp->b_flags & B_CACHE) == 0 &&
2004		    fs->e2fs_bsize == xfersize)
2005			vfs_bio_clrbuf(bp);
2006		if (ioflag & (IO_VMIO|IO_DIRECT)) {
2007			bp->b_flags |= B_RELBUF;
2008		}
2009
2010		/*
2011		 * If IO_SYNC each buffer is written synchronously.  Otherwise
2012		 * if we have a severe page deficiency write the buffer
2013		 * asynchronously.  Otherwise try to cluster, and if that
2014		 * doesn't do it then either do an async write (if O_DIRECT),
2015		 * or a delayed write (if not).
2016		 */
2017		if (ioflag & IO_SYNC) {
2018			(void)bwrite(bp);
2019		} else if (vm_page_count_severe() ||
2020		    buf_dirty_count_severe() ||
2021		    (ioflag & IO_ASYNC)) {
2022			bp->b_flags |= B_CLUSTEROK;
2023			bawrite(bp);
2024		} else if (xfersize + blkoffset == fs->e2fs_fsize) {
2025			if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
2026				bp->b_flags |= B_CLUSTEROK;
2027				cluster_write(vp, bp, ip->i_size, seqcount, 0);
2028			} else {
2029				bawrite(bp);
2030			}
2031		} else if (ioflag & IO_DIRECT) {
2032			bp->b_flags |= B_CLUSTEROK;
2033			bawrite(bp);
2034		} else {
2035			bp->b_flags |= B_CLUSTEROK;
2036			bdwrite(bp);
2037		}
2038		if (error || xfersize == 0)
2039			break;
2040	}
2041	/*
2042	 * If we successfully wrote any data, and we are not the superuser
2043	 * we clear the setuid and setgid bits as a precaution against
2044	 * tampering.
2045	 */
2046	if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
2047	    ap->a_cred) {
2048		if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
2049			ip->i_mode &= ~(ISUID | ISGID);
2050	}
2051	if (error) {
2052		if (ioflag & IO_UNIT) {
2053			(void)ext2_truncate(vp, osize,
2054			    ioflag & IO_SYNC, ap->a_cred, uio->uio_td);
2055			uio->uio_offset -= resid - uio->uio_resid;
2056			uio->uio_resid = resid;
2057		}
2058	}
2059	if (uio->uio_resid != resid) {
2060		ip->i_flag |= IN_CHANGE | IN_UPDATE;
2061		if (ioflag & IO_SYNC)
2062			error = ext2_update(vp, 1);
2063	}
2064	return (error);
2065}
2066
2067/*
2068 * get page routine
2069 */
2070static int
2071ext2_getpages(struct vop_getpages_args *ap)
2072{
2073	int i;
2074	vm_page_t mreq;
2075	int pcount;
2076
2077	pcount = round_page(ap->a_count) / PAGE_SIZE;
2078	mreq = ap->a_m[ap->a_reqpage];
2079
2080	/*
2081	 * if ANY DEV_BSIZE blocks are valid on a large filesystem block,
2082	 * then the entire page is valid.  Since the page may be mapped,
2083	 * user programs might reference data beyond the actual end of file
2084	 * occuring within the page.  We have to zero that data.
2085	 */
2086	VM_OBJECT_WLOCK(mreq->object);
2087	if (mreq->valid) {
2088		if (mreq->valid != VM_PAGE_BITS_ALL)
2089			vm_page_zero_invalid(mreq, TRUE);
2090		for (i = 0; i < pcount; i++) {
2091			if (i != ap->a_reqpage) {
2092				vm_page_lock(ap->a_m[i]);
2093				vm_page_free(ap->a_m[i]);
2094				vm_page_unlock(ap->a_m[i]);
2095			}
2096		}
2097		VM_OBJECT_WUNLOCK(mreq->object);
2098		return VM_PAGER_OK;
2099	}
2100	VM_OBJECT_WUNLOCK(mreq->object);
2101
2102	return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
2103					    ap->a_count,
2104					    ap->a_reqpage);
2105}
2106