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