xref: /dragonfly/sys/vfs/ext2fs/ext2_vnops.c (revision 2b3f93ea)
1cfe60390STomohiro Kusumi /*-
2cfe60390STomohiro Kusumi  *  modified for EXT2FS support in Lites 1.1
3cfe60390STomohiro Kusumi  *
4cfe60390STomohiro Kusumi  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5cfe60390STomohiro Kusumi  *  University of Utah, Department of Computer Science
6cfe60390STomohiro Kusumi  */
7cfe60390STomohiro Kusumi /*-
8cfe60390STomohiro Kusumi  * SPDX-License-Identifier: BSD-3-Clause
9cfe60390STomohiro Kusumi  *
10cfe60390STomohiro Kusumi  * Copyright (c) 1982, 1986, 1989, 1993
11cfe60390STomohiro Kusumi  *	The Regents of the University of California.  All rights reserved.
12cfe60390STomohiro Kusumi  * (c) UNIX System Laboratories, Inc.
13cfe60390STomohiro Kusumi  * All or some portions of this file are derived from material licensed
14cfe60390STomohiro Kusumi  * to the University of California by American Telephone and Telegraph
15cfe60390STomohiro Kusumi  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16cfe60390STomohiro Kusumi  * the permission of UNIX System Laboratories, Inc.
17cfe60390STomohiro Kusumi  *
18cfe60390STomohiro Kusumi  * Redistribution and use in source and binary forms, with or without
19cfe60390STomohiro Kusumi  * modification, are permitted provided that the following conditions
20cfe60390STomohiro Kusumi  * are met:
21cfe60390STomohiro Kusumi  * 1. Redistributions of source code must retain the above copyright
22cfe60390STomohiro Kusumi  *    notice, this list of conditions and the following disclaimer.
23cfe60390STomohiro Kusumi  * 2. Redistributions in binary form must reproduce the above copyright
24cfe60390STomohiro Kusumi  *    notice, this list of conditions and the following disclaimer in the
25cfe60390STomohiro Kusumi  *    documentation and/or other materials provided with the distribution.
26cfe60390STomohiro Kusumi  * 3. Neither the name of the University nor the names of its contributors
27cfe60390STomohiro Kusumi  *    may be used to endorse or promote products derived from this software
28cfe60390STomohiro Kusumi  *    without specific prior written permission.
29cfe60390STomohiro Kusumi  *
30cfe60390STomohiro Kusumi  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31cfe60390STomohiro Kusumi  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32cfe60390STomohiro Kusumi  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33cfe60390STomohiro Kusumi  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34cfe60390STomohiro Kusumi  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35cfe60390STomohiro Kusumi  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36cfe60390STomohiro Kusumi  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37cfe60390STomohiro Kusumi  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38cfe60390STomohiro Kusumi  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39cfe60390STomohiro Kusumi  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40cfe60390STomohiro Kusumi  * SUCH DAMAGE.
41cfe60390STomohiro Kusumi  *
42cfe60390STomohiro Kusumi  *	@(#)ufs_vnops.c	8.7 (Berkeley) 2/3/94
43cfe60390STomohiro Kusumi  *	@(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
44cfe60390STomohiro Kusumi  * $FreeBSD$
45cfe60390STomohiro Kusumi  */
46cfe60390STomohiro Kusumi 
479eacd38aSSascha Wildner #include "opt_suiddir.h"
489eacd38aSSascha Wildner 
49cfe60390STomohiro Kusumi #include <sys/param.h>
50cfe60390STomohiro Kusumi #include <sys/systm.h>
51cfe60390STomohiro Kusumi #include <sys/kernel.h>
52cfe60390STomohiro Kusumi #include <sys/fcntl.h>
53cfe60390STomohiro Kusumi #include <sys/filio.h>
54cfe60390STomohiro Kusumi #include <sys/limits.h>
55cfe60390STomohiro Kusumi #include <sys/stat.h>
56cfe60390STomohiro Kusumi #include <sys/bio.h>
57cfe60390STomohiro Kusumi #include <sys/buf2.h>
58cfe60390STomohiro Kusumi #include <sys/endian.h>
59*2b3f93eaSMatthew Dillon #include <sys/caps.h>
60cfe60390STomohiro Kusumi #include <sys/mount.h>
61cfe60390STomohiro Kusumi #include <sys/unistd.h>
62cfe60390STomohiro Kusumi #include <sys/time.h>
63cfe60390STomohiro Kusumi #include <sys/vnode.h>
64cfe60390STomohiro Kusumi #include <sys/namei.h>
65cfe60390STomohiro Kusumi #include <sys/lockf.h>
66cfe60390STomohiro Kusumi #include <sys/event.h>
67cfe60390STomohiro Kusumi #include <sys/conf.h>
68cfe60390STomohiro Kusumi #include <sys/file.h>
69cfe60390STomohiro Kusumi #include <sys/vmmeter.h>
70cfe60390STomohiro Kusumi #include <sys/vfsops.h>
71cfe60390STomohiro Kusumi #include <sys/malloc.h>
72cfe60390STomohiro Kusumi #include <sys/uio.h>
73cfe60390STomohiro Kusumi #include <sys/jail.h>
74cfe60390STomohiro Kusumi 
75cfe60390STomohiro Kusumi #include <vm/vm.h>
76cfe60390STomohiro Kusumi #include <vm/vm_param.h>
77cfe60390STomohiro Kusumi #include <vm/vm_extern.h>
78cfe60390STomohiro Kusumi #include <vm/vm_object.h>
79cfe60390STomohiro Kusumi #include <vm/vm_page2.h>
80cfe60390STomohiro Kusumi #include <vm/vm_pager.h>
81cfe60390STomohiro Kusumi #include <vm/vnode_pager.h>
82cfe60390STomohiro Kusumi 
83cfe60390STomohiro Kusumi #include <vfs/ufs/dir.h>
84cfe60390STomohiro Kusumi #include <vfs/fifofs/fifo.h>
85cfe60390STomohiro Kusumi 
86cfe60390STomohiro Kusumi #include <vfs/ext2fs/fs.h>
87cfe60390STomohiro Kusumi #include <vfs/ext2fs/inode.h>
88cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2fs.h>
89cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_extern.h>
90cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_dinode.h>
91cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_dir.h>
92cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_mount.h>
93cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_extents.h>
94cfe60390STomohiro Kusumi 
95cfe60390STomohiro Kusumi SDT_PROVIDER_DECLARE(ext2fs);
96cfe60390STomohiro Kusumi /*
97cfe60390STomohiro Kusumi  * ext2fs trace probe:
98cfe60390STomohiro Kusumi  * arg0: verbosity. Higher numbers give more verbose messages
99cfe60390STomohiro Kusumi  * arg1: Textual message
100cfe60390STomohiro Kusumi  */
101cfe60390STomohiro Kusumi SDT_PROBE_DEFINE2(ext2fs, , vnops, trace, "int", "char*");
102cfe60390STomohiro Kusumi 
103cfe60390STomohiro Kusumi static int ext2_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
104cfe60390STomohiro Kusumi 
105cfe60390STomohiro Kusumi static int ext2_chmod(struct vnode *, int, struct ucred *, struct thread *);
106cfe60390STomohiro Kusumi static int ext2_chown(struct vnode *, uid_t, gid_t, struct ucred *,
107cfe60390STomohiro Kusumi     struct thread *);
108cfe60390STomohiro Kusumi 
109cfe60390STomohiro Kusumi /*
110cfe60390STomohiro Kusumi  * A virgin directory (no blushing please).
111cfe60390STomohiro Kusumi  * Note that the type and namlen fields are reversed relative to ext2.
112cfe60390STomohiro Kusumi  * Also, we don't use `struct odirtemplate', since it would just cause
113cfe60390STomohiro Kusumi  * endianness problems.
114cfe60390STomohiro Kusumi  */
115cfe60390STomohiro Kusumi static struct dirtemplate mastertemplate = {
116cfe60390STomohiro Kusumi 	0, htole16(12), 1, EXT2_FT_DIR, ".",
117cfe60390STomohiro Kusumi 	0, htole16(DIRBLKSIZ - 12), 2, EXT2_FT_DIR, ".."
118cfe60390STomohiro Kusumi };
119cfe60390STomohiro Kusumi static struct dirtemplate omastertemplate = {
120cfe60390STomohiro Kusumi 	0, htole16(12), 1, EXT2_FT_UNKNOWN, ".",
121cfe60390STomohiro Kusumi 	0, htole16(DIRBLKSIZ - 12), 2, EXT2_FT_UNKNOWN, ".."
122cfe60390STomohiro Kusumi };
123cfe60390STomohiro Kusumi 
124cfe60390STomohiro Kusumi void
ext2_itimes(struct vnode * vp)125cfe60390STomohiro Kusumi ext2_itimes(struct vnode *vp)
126cfe60390STomohiro Kusumi {
127cfe60390STomohiro Kusumi 	struct inode *ip;
128cfe60390STomohiro Kusumi 	struct timespec ts;
129cfe60390STomohiro Kusumi 
130cfe60390STomohiro Kusumi 	ip = VTOI(vp);
131cfe60390STomohiro Kusumi 	if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
132cfe60390STomohiro Kusumi 		return;
133cfe60390STomohiro Kusumi 	if ((vp->v_type == VBLK || vp->v_type == VCHR))
134cfe60390STomohiro Kusumi 		ip->i_flag |= IN_LAZYMOD;
135cfe60390STomohiro Kusumi 	else
136cfe60390STomohiro Kusumi 		ip->i_flag |= IN_MODIFIED;
137cfe60390STomohiro Kusumi 	if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
138cfe60390STomohiro Kusumi 		vfs_timestamp(&ts);
139cfe60390STomohiro Kusumi 		if (ip->i_flag & IN_ACCESS) {
140cfe60390STomohiro Kusumi 			ip->i_atime = ts.tv_sec;
141cfe60390STomohiro Kusumi 			ip->i_atimensec = ts.tv_nsec;
142cfe60390STomohiro Kusumi 		}
143cfe60390STomohiro Kusumi 		if (ip->i_flag & IN_UPDATE) {
144cfe60390STomohiro Kusumi 			ip->i_mtime = ts.tv_sec;
145cfe60390STomohiro Kusumi 			ip->i_mtimensec = ts.tv_nsec;
146cfe60390STomohiro Kusumi 			ip->i_modrev++;
147cfe60390STomohiro Kusumi 		}
148cfe60390STomohiro Kusumi 		if (ip->i_flag & IN_CHANGE) {
149cfe60390STomohiro Kusumi 			ip->i_ctime = ts.tv_sec;
150cfe60390STomohiro Kusumi 			ip->i_ctimensec = ts.tv_nsec;
151cfe60390STomohiro Kusumi 		}
152cfe60390STomohiro Kusumi 	}
153cfe60390STomohiro Kusumi 	ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
154cfe60390STomohiro Kusumi }
155cfe60390STomohiro Kusumi 
156cfe60390STomohiro Kusumi /*
157cfe60390STomohiro Kusumi  * Create a regular file
158cfe60390STomohiro Kusumi  */
159cfe60390STomohiro Kusumi static int
ext2_create(struct vop_old_create_args * ap)160cfe60390STomohiro Kusumi ext2_create(struct vop_old_create_args *ap)
161cfe60390STomohiro Kusumi {
162cfe60390STomohiro Kusumi 	int error;
163cfe60390STomohiro Kusumi 
164cfe60390STomohiro Kusumi 	error =
165cfe60390STomohiro Kusumi 	    ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
166cfe60390STomohiro Kusumi 	    ap->a_dvp, ap->a_vpp, ap->a_cnp);
167cfe60390STomohiro Kusumi 	if (error != 0)
168cfe60390STomohiro Kusumi 		return (error);
169cfe60390STomohiro Kusumi 	return (0);
170cfe60390STomohiro Kusumi }
171cfe60390STomohiro Kusumi 
172cfe60390STomohiro Kusumi static int
ext2_open(struct vop_open_args * ap)173cfe60390STomohiro Kusumi ext2_open(struct vop_open_args *ap)
174cfe60390STomohiro Kusumi {
175cfe60390STomohiro Kusumi 
176cfe60390STomohiro Kusumi 	if (ap->a_vp->v_type == VBLK || ap->a_vp->v_type == VCHR)
177cfe60390STomohiro Kusumi 		return (EOPNOTSUPP);
178cfe60390STomohiro Kusumi 
179cfe60390STomohiro Kusumi 	/*
180cfe60390STomohiro Kusumi 	 * Files marked append-only must be opened for appending.
181cfe60390STomohiro Kusumi 	 */
182cfe60390STomohiro Kusumi 	if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
183cfe60390STomohiro Kusumi 	    (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
184cfe60390STomohiro Kusumi 		return (EPERM);
185cfe60390STomohiro Kusumi 
186cfe60390STomohiro Kusumi 	return (vop_stdopen(ap));
187cfe60390STomohiro Kusumi }
188cfe60390STomohiro Kusumi 
189cfe60390STomohiro Kusumi /*
190cfe60390STomohiro Kusumi  * Close called.
191cfe60390STomohiro Kusumi  *
192cfe60390STomohiro Kusumi  * Update the times on the inode.
193cfe60390STomohiro Kusumi  */
194cfe60390STomohiro Kusumi static int
ext2_close(struct vop_close_args * ap)195cfe60390STomohiro Kusumi ext2_close(struct vop_close_args *ap)
196cfe60390STomohiro Kusumi {
197cfe60390STomohiro Kusumi 	struct vnode *vp = ap->a_vp;
198cfe60390STomohiro Kusumi 
199cfe60390STomohiro Kusumi 	if (VREFCNT(vp) > 1)
200cfe60390STomohiro Kusumi 		ext2_itimes(vp);
201cfe60390STomohiro Kusumi 	return (vop_stdclose(ap));
202cfe60390STomohiro Kusumi }
203cfe60390STomohiro Kusumi 
204cfe60390STomohiro Kusumi static int
ext2_access(struct vop_access_args * ap)205cfe60390STomohiro Kusumi ext2_access(struct vop_access_args *ap)
206cfe60390STomohiro Kusumi {
207cfe60390STomohiro Kusumi 	struct vnode *vp = ap->a_vp;
208cfe60390STomohiro Kusumi 	struct inode *ip = VTOI(vp);
209cfe60390STomohiro Kusumi 	int error;
210cfe60390STomohiro Kusumi 
211cfe60390STomohiro Kusumi 	if (vp->v_type == VBLK || vp->v_type == VCHR)
212cfe60390STomohiro Kusumi 		return (EOPNOTSUPP);
213cfe60390STomohiro Kusumi 
214cfe60390STomohiro Kusumi 	error = vop_helper_access(ap, ip->i_uid, ip->i_gid, ip->i_mode,
215cfe60390STomohiro Kusumi 	    ip->i_flags);
216cfe60390STomohiro Kusumi 	return (error);
217cfe60390STomohiro Kusumi }
218cfe60390STomohiro Kusumi 
219cfe60390STomohiro Kusumi static int
ext2_getattr(struct vop_getattr_args * ap)220cfe60390STomohiro Kusumi ext2_getattr(struct vop_getattr_args *ap)
221cfe60390STomohiro Kusumi {
222cfe60390STomohiro Kusumi 	struct vnode *vp = ap->a_vp;
223cfe60390STomohiro Kusumi 	struct inode *ip = VTOI(vp);
224cfe60390STomohiro Kusumi 	struct vattr *vap = ap->a_vap;
225cfe60390STomohiro Kusumi 
226cfe60390STomohiro Kusumi 	ext2_itimes(vp);
227cfe60390STomohiro Kusumi 	/*
228cfe60390STomohiro Kusumi 	 * Copy from inode table
229cfe60390STomohiro Kusumi 	 */
230cfe60390STomohiro Kusumi 	vap->va_fsid = devid_from_dev(ip->i_dev);
231cfe60390STomohiro Kusumi 	vap->va_fileid = ip->i_number;
232cfe60390STomohiro Kusumi 	vap->va_mode = ip->i_mode & ~IFMT;
233cfe60390STomohiro Kusumi 	vap->va_nlink = ip->i_nlink;
234cfe60390STomohiro Kusumi 	vap->va_uid = ip->i_uid;
235cfe60390STomohiro Kusumi 	vap->va_gid = ip->i_gid;
236cfe60390STomohiro Kusumi 	vap->va_size = ip->i_size;
237cfe60390STomohiro Kusumi 	vap->va_atime.tv_sec = ip->i_atime;
238cfe60390STomohiro Kusumi 	vap->va_atime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_atimensec : 0;
239cfe60390STomohiro Kusumi 	vap->va_mtime.tv_sec = ip->i_mtime;
240cfe60390STomohiro Kusumi 	vap->va_mtime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_mtimensec : 0;
241cfe60390STomohiro Kusumi 	vap->va_ctime.tv_sec = ip->i_ctime;
242cfe60390STomohiro Kusumi 	vap->va_ctime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_ctimensec : 0;
243cfe60390STomohiro Kusumi 	vap->va_flags = ip->i_flags;
244cfe60390STomohiro Kusumi 	vap->va_gen = ip->i_gen;
245cfe60390STomohiro Kusumi 	vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
246cfe60390STomohiro Kusumi 	vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
247cfe60390STomohiro Kusumi 	vap->va_type = IFTOVT(ip->i_mode);
248cfe60390STomohiro Kusumi 	vap->va_filerev = ip->i_modrev;
249cfe60390STomohiro Kusumi 	return (0);
250cfe60390STomohiro Kusumi }
251cfe60390STomohiro Kusumi 
252cfe60390STomohiro Kusumi /*
253cfe60390STomohiro Kusumi  * Set attribute vnode op. called from several syscalls
254cfe60390STomohiro Kusumi  */
255cfe60390STomohiro Kusumi static int
ext2_setattr(struct vop_setattr_args * ap)256cfe60390STomohiro Kusumi ext2_setattr(struct vop_setattr_args *ap)
257cfe60390STomohiro Kusumi {
258cfe60390STomohiro Kusumi 	struct vattr *vap = ap->a_vap;
259cfe60390STomohiro Kusumi 	struct vnode *vp = ap->a_vp;
260cfe60390STomohiro Kusumi 	struct inode *ip = VTOI(vp);
261cfe60390STomohiro Kusumi 	struct ucred *cred = ap->a_cred;
262cfe60390STomohiro Kusumi 	struct thread *td = curthread;
263cfe60390STomohiro Kusumi 	int error;
264cfe60390STomohiro Kusumi 
265cfe60390STomohiro Kusumi 	/*
266cfe60390STomohiro Kusumi 	 * Check for unsettable attributes.
267cfe60390STomohiro Kusumi 	 */
268cfe60390STomohiro Kusumi 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
269cfe60390STomohiro Kusumi 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
270cfe60390STomohiro Kusumi 	    (vap->va_blocksize != VNOVAL) || (vap->va_rmajor != VNOVAL) ||
271cfe60390STomohiro Kusumi 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
272cfe60390STomohiro Kusumi 		return (EINVAL);
273cfe60390STomohiro Kusumi 	}
274cfe60390STomohiro Kusumi 	if (vap->va_flags != VNOVAL) {
275cfe60390STomohiro Kusumi 		/* Disallow flags not supported by ext2fs. */
276cfe60390STomohiro Kusumi 		if (vap->va_flags & ~(SF_APPEND | SF_IMMUTABLE | UF_NODUMP))
277cfe60390STomohiro Kusumi 			return (EOPNOTSUPP);
278cfe60390STomohiro Kusumi 
279cfe60390STomohiro Kusumi 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
280cfe60390STomohiro Kusumi 			return (EROFS);
281cfe60390STomohiro Kusumi 		if (cred->cr_uid != ip->i_uid &&
282*2b3f93eaSMatthew Dillon 		    (error = caps_priv_check(cred, SYSCAP_NOVFS_SETATTR)))
283*2b3f93eaSMatthew Dillon 		{
284cfe60390STomohiro Kusumi 			return (error);
285*2b3f93eaSMatthew Dillon 		}
286*2b3f93eaSMatthew Dillon 
287cfe60390STomohiro Kusumi 		/*
288cfe60390STomohiro Kusumi 		 * Note that a root chflags becomes a user chflags when
289cfe60390STomohiro Kusumi 		 * we are jailed, unless the jail vfs_chflags sysctl
290cfe60390STomohiro Kusumi 		 * is set.
291cfe60390STomohiro Kusumi 		 */
292cfe60390STomohiro Kusumi 		if (cred->cr_uid == 0 &&
293cfe60390STomohiro Kusumi 		    (!jailed(cred) || PRISON_CAP_ISSET(cred->cr_prison->pr_caps,
294cfe60390STomohiro Kusumi 		    PRISON_CAP_VFS_CHFLAGS))) {
295cfe60390STomohiro Kusumi 			if ((ip->i_flags
296cfe60390STomohiro Kusumi 			    & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) &&
297cfe60390STomohiro Kusumi 			    securelevel > 0)
298cfe60390STomohiro Kusumi 				return (EPERM);
299cfe60390STomohiro Kusumi 			ip->i_flags = vap->va_flags;
300cfe60390STomohiro Kusumi 		} else {
301cfe60390STomohiro Kusumi 			if (ip->i_flags
302cfe60390STomohiro Kusumi 			    & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
303cfe60390STomohiro Kusumi 			    (vap->va_flags & UF_SETTABLE) != vap->va_flags)
304cfe60390STomohiro Kusumi 				return (EPERM);
305cfe60390STomohiro Kusumi 			ip->i_flags &= SF_SETTABLE;
306cfe60390STomohiro Kusumi 			ip->i_flags |= (vap->va_flags & UF_SETTABLE);
307cfe60390STomohiro Kusumi 		}
308cfe60390STomohiro Kusumi 		ip->i_flag |= IN_CHANGE;
309cfe60390STomohiro Kusumi 		if (vap->va_flags & (IMMUTABLE | APPEND))
310cfe60390STomohiro Kusumi 			return (0);
311cfe60390STomohiro Kusumi 	}
312cfe60390STomohiro Kusumi 	if (ip->i_flags & (IMMUTABLE | APPEND))
313cfe60390STomohiro Kusumi 		return (EPERM);
314cfe60390STomohiro Kusumi 	/*
315cfe60390STomohiro Kusumi 	 * Go through the fields and update iff not VNOVAL.
316cfe60390STomohiro Kusumi 	 */
317cfe60390STomohiro Kusumi 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
318cfe60390STomohiro Kusumi 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
319cfe60390STomohiro Kusumi 			return (EROFS);
320cfe60390STomohiro Kusumi 		if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred,
321cfe60390STomohiro Kusumi 		    td)) != 0)
322cfe60390STomohiro Kusumi 			return (error);
323cfe60390STomohiro Kusumi 	}
324cfe60390STomohiro Kusumi 	if (vap->va_size != VNOVAL) {
325cfe60390STomohiro Kusumi 		/*
326cfe60390STomohiro Kusumi 		 * Disallow write attempts on read-only file systems;
327cfe60390STomohiro Kusumi 		 * unless the file is a socket, fifo, or a block or
328cfe60390STomohiro Kusumi 		 * character device resident on the file system.
329cfe60390STomohiro Kusumi 		 */
330cfe60390STomohiro Kusumi 		switch (vp->v_type) {
331cfe60390STomohiro Kusumi 		case VDIR:
332cfe60390STomohiro Kusumi 			return (EISDIR);
333cfe60390STomohiro Kusumi 		case VLNK:
334cfe60390STomohiro Kusumi 		case VREG:
335cfe60390STomohiro Kusumi 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
336cfe60390STomohiro Kusumi 				return (EROFS);
337cfe60390STomohiro Kusumi 			break;
338cfe60390STomohiro Kusumi 		default:
339cfe60390STomohiro Kusumi 			break;
340cfe60390STomohiro Kusumi 		}
341cfe60390STomohiro Kusumi 		if ((error = ext2_truncate(vp, vap->va_size, 0, cred)) != 0)
342cfe60390STomohiro Kusumi 			return (error);
343cfe60390STomohiro Kusumi 	}
344cfe60390STomohiro Kusumi 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
345cfe60390STomohiro Kusumi 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
346cfe60390STomohiro Kusumi 			return (EROFS);
347cfe60390STomohiro Kusumi 		if (cred->cr_uid != ip->i_uid &&
348*2b3f93eaSMatthew Dillon 		    (error = caps_priv_check(cred, SYSCAP_NOVFS_SETATTR)) &&
349cfe60390STomohiro Kusumi 		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
350cfe60390STomohiro Kusumi 		    (error = VOP_EACCESS(vp, VWRITE, cred))))
351*2b3f93eaSMatthew Dillon 		{
352cfe60390STomohiro Kusumi 			return (error);
353*2b3f93eaSMatthew Dillon 		}
354cfe60390STomohiro Kusumi 		ip->i_flag |= IN_CHANGE | IN_MODIFIED;
355cfe60390STomohiro Kusumi 		if (vap->va_atime.tv_sec != VNOVAL) {
356cfe60390STomohiro Kusumi 			ip->i_flag &= ~IN_ACCESS;
357cfe60390STomohiro Kusumi 			ip->i_atime = vap->va_atime.tv_sec;
358cfe60390STomohiro Kusumi 			ip->i_atimensec = vap->va_atime.tv_nsec;
359cfe60390STomohiro Kusumi 		}
360cfe60390STomohiro Kusumi 		if (vap->va_mtime.tv_sec != VNOVAL) {
361cfe60390STomohiro Kusumi 			ip->i_flag &= ~IN_UPDATE;
362cfe60390STomohiro Kusumi 			ip->i_mtime = vap->va_mtime.tv_sec;
363cfe60390STomohiro Kusumi 			ip->i_mtimensec = vap->va_mtime.tv_nsec;
364cfe60390STomohiro Kusumi 		}
365cfe60390STomohiro Kusumi 		error = ext2_update(vp, 0);
366cfe60390STomohiro Kusumi 		if (error)
367cfe60390STomohiro Kusumi 			return (error);
368cfe60390STomohiro Kusumi 	}
369cfe60390STomohiro Kusumi 	error = 0;
370cfe60390STomohiro Kusumi 	if (vap->va_mode != (mode_t)VNOVAL) {
371cfe60390STomohiro Kusumi 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
372cfe60390STomohiro Kusumi 			return (EROFS);
373cfe60390STomohiro Kusumi 		error = ext2_chmod(vp, (int)vap->va_mode, cred, td);
374cfe60390STomohiro Kusumi 	}
375cfe60390STomohiro Kusumi 	return (error);
376cfe60390STomohiro Kusumi }
377cfe60390STomohiro Kusumi 
378cfe60390STomohiro Kusumi /*
379cfe60390STomohiro Kusumi  * Change the mode on a file.
380cfe60390STomohiro Kusumi  * Inode must be locked before calling.
381cfe60390STomohiro Kusumi  */
382cfe60390STomohiro Kusumi static int
ext2_chmod(struct vnode * vp,int mode,struct ucred * cred,struct thread * td)383cfe60390STomohiro Kusumi ext2_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td)
384cfe60390STomohiro Kusumi {
385cfe60390STomohiro Kusumi 	struct inode *ip = VTOI(vp);
386cfe60390STomohiro Kusumi 	int error;
387cfe60390STomohiro Kusumi 
388cfe60390STomohiro Kusumi 	if (cred->cr_uid != ip->i_uid) {
389*2b3f93eaSMatthew Dillon 		error = caps_priv_check(cred, SYSCAP_NOVFS_CHMOD);
390cfe60390STomohiro Kusumi 		if (error)
391cfe60390STomohiro Kusumi 			return (error);
392cfe60390STomohiro Kusumi 	}
393cfe60390STomohiro Kusumi 	if (cred->cr_uid) {
394cfe60390STomohiro Kusumi 		if (vp->v_type != VDIR && (mode & S_ISTXT))
395cfe60390STomohiro Kusumi 			return (EFTYPE);
396cfe60390STomohiro Kusumi 		if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
397cfe60390STomohiro Kusumi 			return (EPERM);
398cfe60390STomohiro Kusumi 	}
399cfe60390STomohiro Kusumi 	ip->i_mode &= ~ALLPERMS;
400cfe60390STomohiro Kusumi 	ip->i_mode |= (mode & ALLPERMS);
401cfe60390STomohiro Kusumi 	ip->i_flag |= IN_CHANGE;
402cfe60390STomohiro Kusumi 	return (0);
403cfe60390STomohiro Kusumi }
404cfe60390STomohiro Kusumi 
405cfe60390STomohiro Kusumi /*
406cfe60390STomohiro Kusumi  * Perform chown operation on inode ip;
407cfe60390STomohiro Kusumi  * inode must be locked prior to call.
408cfe60390STomohiro Kusumi  */
409cfe60390STomohiro Kusumi static int
ext2_chown(struct vnode * vp,uid_t uid,gid_t gid,struct ucred * cred,struct thread * td)410cfe60390STomohiro Kusumi ext2_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
411cfe60390STomohiro Kusumi     struct thread *td)
412cfe60390STomohiro Kusumi {
413cfe60390STomohiro Kusumi 	struct inode *ip = VTOI(vp);
414cfe60390STomohiro Kusumi 	uid_t ouid;
415cfe60390STomohiro Kusumi 	gid_t ogid;
416cfe60390STomohiro Kusumi 	int error = 0;
417cfe60390STomohiro Kusumi 
418cfe60390STomohiro Kusumi 	if (uid == (uid_t)VNOVAL)
419cfe60390STomohiro Kusumi 		uid = ip->i_uid;
420cfe60390STomohiro Kusumi 	if (gid == (gid_t)VNOVAL)
421cfe60390STomohiro Kusumi 		gid = ip->i_gid;
422cfe60390STomohiro Kusumi 	/*
423cfe60390STomohiro Kusumi 	 * If we don't own the file, are trying to change the owner
424cfe60390STomohiro Kusumi 	 * of the file, or are not a member of the target group,
425cfe60390STomohiro Kusumi 	 * the caller must be superuser or the call fails.
426cfe60390STomohiro Kusumi 	 */
427cfe60390STomohiro Kusumi 	if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
428cfe60390STomohiro Kusumi 	    (gid != ip->i_gid && !(cred->cr_gid == gid ||
429cfe60390STomohiro Kusumi 	    groupmember(gid, cred)))) &&
430*2b3f93eaSMatthew Dillon 	    (error = caps_priv_check(cred, SYSCAP_NOVFS_CHOWN)))
431*2b3f93eaSMatthew Dillon 	{
432cfe60390STomohiro Kusumi 		return (error);
433*2b3f93eaSMatthew Dillon 	}
434*2b3f93eaSMatthew Dillon 
435cfe60390STomohiro Kusumi 	ogid = ip->i_gid;
436cfe60390STomohiro Kusumi 	ouid = ip->i_uid;
437cfe60390STomohiro Kusumi 	ip->i_gid = gid;
438cfe60390STomohiro Kusumi 	ip->i_uid = uid;
439cfe60390STomohiro Kusumi 	ip->i_flag |= IN_CHANGE;
440cfe60390STomohiro Kusumi 	if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
441*2b3f93eaSMatthew Dillon 		if (caps_priv_check(cred, SYSCAP_NOVFS_RETAINSUGID) != 0)
442cfe60390STomohiro Kusumi 			ip->i_mode &= ~(ISUID | ISGID);
443cfe60390STomohiro Kusumi 	}
444cfe60390STomohiro Kusumi 	return (0);
445cfe60390STomohiro Kusumi }
446cfe60390STomohiro Kusumi 
447cfe60390STomohiro Kusumi struct ext2_fsync_bp_info {
448cfe60390STomohiro Kusumi 	struct vnode *vp;
449cfe60390STomohiro Kusumi 	int waitfor;
450cfe60390STomohiro Kusumi };
451cfe60390STomohiro Kusumi 
452cfe60390STomohiro Kusumi static int
ext2_fsync_bp(struct buf * bp,void * data)453cfe60390STomohiro Kusumi ext2_fsync_bp(struct buf *bp, void *data)
454cfe60390STomohiro Kusumi {
455cfe60390STomohiro Kusumi 	struct ext2_fsync_bp_info *info = data;
456cfe60390STomohiro Kusumi 
457cfe60390STomohiro Kusumi 	if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
458cfe60390STomohiro Kusumi 		return (0);
459cfe60390STomohiro Kusumi 	if ((bp->b_flags & B_DELWRI) == 0)
460cfe60390STomohiro Kusumi 		panic("ext2_fsync: not dirty");
461cfe60390STomohiro Kusumi 	bremfree(bp);
462cfe60390STomohiro Kusumi 
463cfe60390STomohiro Kusumi 	/*
464cfe60390STomohiro Kusumi 	 * Wait for I/O associated with indirect blocks to complete,
465cfe60390STomohiro Kusumi 	 * since there is no way to quickly wait for them below.
466cfe60390STomohiro Kusumi 	 */
467cfe60390STomohiro Kusumi 	if (bp->b_vp == info->vp || (info->waitfor & MNT_NOWAIT))
468cfe60390STomohiro Kusumi 		bawrite(bp);
469cfe60390STomohiro Kusumi 	else
470cfe60390STomohiro Kusumi 		bwrite(bp);
471cfe60390STomohiro Kusumi 	return (1);
472cfe60390STomohiro Kusumi }
473cfe60390STomohiro Kusumi 
474cfe60390STomohiro Kusumi /*
475cfe60390STomohiro Kusumi  * Synch an open file.
476cfe60390STomohiro Kusumi  */
477cfe60390STomohiro Kusumi /* ARGSUSED */
478cfe60390STomohiro Kusumi static int
ext2_fsync(struct vop_fsync_args * ap)479cfe60390STomohiro Kusumi ext2_fsync(struct vop_fsync_args *ap)
480cfe60390STomohiro Kusumi {
481cfe60390STomohiro Kusumi 	struct ext2_fsync_bp_info info;
482cfe60390STomohiro Kusumi 	struct vnode *vp = ap->a_vp;
483cfe60390STomohiro Kusumi 	int count;
484cfe60390STomohiro Kusumi 
485cfe60390STomohiro Kusumi 	/*
486cfe60390STomohiro Kusumi 	 * XXX why is all this fs specific?
487cfe60390STomohiro Kusumi 	 */
488cfe60390STomohiro Kusumi 
489cfe60390STomohiro Kusumi 	/*
490cfe60390STomohiro Kusumi 	 * Flush all dirty buffers associated with a vnode.
491cfe60390STomohiro Kusumi 	 */
492cfe60390STomohiro Kusumi 	lwkt_gettoken(&vp->v_token);
493cfe60390STomohiro Kusumi 	info.vp = vp;
494cfe60390STomohiro Kusumi loop:
495cfe60390STomohiro Kusumi 	info.waitfor = ap->a_waitfor;
496cfe60390STomohiro Kusumi 	count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, ext2_fsync_bp,
497cfe60390STomohiro Kusumi 	    &info);
498cfe60390STomohiro Kusumi 	if (count)
499cfe60390STomohiro Kusumi 		goto loop;
500cfe60390STomohiro Kusumi 
501cfe60390STomohiro Kusumi 	if (ap->a_waitfor == MNT_WAIT) {
502cfe60390STomohiro Kusumi 		bio_track_wait(&vp->v_track_write, 0, 0);
503cfe60390STomohiro Kusumi #ifdef DIAGNOSTIC
504cfe60390STomohiro Kusumi 		if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
505cfe60390STomohiro Kusumi 			vprint("ext2_fsync: dirty", vp);
506cfe60390STomohiro Kusumi 			goto loop;
507cfe60390STomohiro Kusumi 		}
508cfe60390STomohiro Kusumi #endif
509cfe60390STomohiro Kusumi 	}
510cfe60390STomohiro Kusumi 	lwkt_reltoken(&vp->v_token);
511cfe60390STomohiro Kusumi 
512cfe60390STomohiro Kusumi 	return (ext2_update(ap->a_vp, ap->a_waitfor == MNT_WAIT));
513cfe60390STomohiro Kusumi }
514cfe60390STomohiro Kusumi 
515cfe60390STomohiro Kusumi /*
516cfe60390STomohiro Kusumi  * Mknod vnode call
517cfe60390STomohiro Kusumi  */
518cfe60390STomohiro Kusumi /* ARGSUSED */
519cfe60390STomohiro Kusumi static int
ext2_mknod(struct vop_old_mknod_args * ap)520cfe60390STomohiro Kusumi ext2_mknod(struct vop_old_mknod_args *ap)
521cfe60390STomohiro Kusumi {
522cfe60390STomohiro Kusumi 	struct vattr *vap = ap->a_vap;
523cfe60390STomohiro Kusumi 	struct vnode **vpp = ap->a_vpp;
524cfe60390STomohiro Kusumi 	struct inode *ip;
525cfe60390STomohiro Kusumi 	ino_t ino;
526cfe60390STomohiro Kusumi 	int error;
527cfe60390STomohiro Kusumi 
528cfe60390STomohiro Kusumi 	if (vap->va_rmajor != VNOVAL &&
529cfe60390STomohiro Kusumi 	    makeudev(vap->va_rmajor, vap->va_rminor) == NOUDEV) {
530cfe60390STomohiro Kusumi 		return (EINVAL);
531cfe60390STomohiro Kusumi 	}
532cfe60390STomohiro Kusumi 
533cfe60390STomohiro Kusumi 	error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
534cfe60390STomohiro Kusumi 	    ap->a_dvp, vpp, ap->a_cnp);
535cfe60390STomohiro Kusumi 	if (error)
536cfe60390STomohiro Kusumi 		return (error);
537cfe60390STomohiro Kusumi 	ip = VTOI(*vpp);
538cfe60390STomohiro Kusumi 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
539cfe60390STomohiro Kusumi 	if (vap->va_rmajor != VNOVAL) {
540cfe60390STomohiro Kusumi 		/*
541cfe60390STomohiro Kusumi 		 * Want to be able to use this to make badblock
542cfe60390STomohiro Kusumi 		 * inodes, so don't truncate the dev number.
543cfe60390STomohiro Kusumi 		 */
544cfe60390STomohiro Kusumi 		ip->i_rdev = makeudev(vap->va_rmajor, vap->va_rminor);
545cfe60390STomohiro Kusumi 	}
546cfe60390STomohiro Kusumi 	/*
547cfe60390STomohiro Kusumi 	 * Remove inode, then reload it through VFS_VGET so it is
548cfe60390STomohiro Kusumi 	 * checked to see if it is an alias of an existing entry in
549cfe60390STomohiro Kusumi 	 * the inode cache.	 XXX I don't believe this is necessary now.
550cfe60390STomohiro Kusumi 	 */
551cfe60390STomohiro Kusumi 	(*vpp)->v_type = VNON;
552cfe60390STomohiro Kusumi 	ino = ip->i_number;	/* Save this before vgone() invalidates ip. */
553cfe60390STomohiro Kusumi 	vgone_vxlocked(*vpp);
554cfe60390STomohiro Kusumi 	vput(*vpp);
555cfe60390STomohiro Kusumi 	error = VFS_VGET(ap->a_dvp->v_mount, NULL, ino, vpp);
556cfe60390STomohiro Kusumi 	if (error) {
557cfe60390STomohiro Kusumi 		*vpp = NULL;
558cfe60390STomohiro Kusumi 		return (error);
559cfe60390STomohiro Kusumi 	}
560cfe60390STomohiro Kusumi 	return (0);
561cfe60390STomohiro Kusumi }
562cfe60390STomohiro Kusumi 
563cfe60390STomohiro Kusumi static int
ext2_remove(struct vop_old_remove_args * ap)564cfe60390STomohiro Kusumi ext2_remove(struct vop_old_remove_args *ap)
565cfe60390STomohiro Kusumi {
566cfe60390STomohiro Kusumi 	struct inode *ip;
567cfe60390STomohiro Kusumi 	struct vnode *vp = ap->a_vp;
568cfe60390STomohiro Kusumi 	struct vnode *dvp = ap->a_dvp;
569cfe60390STomohiro Kusumi 	int error;
570cfe60390STomohiro Kusumi 
571cfe60390STomohiro Kusumi 	ip = VTOI(vp);
572cfe60390STomohiro Kusumi 	if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
573cfe60390STomohiro Kusumi 	    (VTOI(dvp)->i_flags & APPEND)) {
574cfe60390STomohiro Kusumi 		error = EPERM;
575cfe60390STomohiro Kusumi 		goto out;
576cfe60390STomohiro Kusumi 	}
577cfe60390STomohiro Kusumi 	error = ext2_dirremove(dvp, ap->a_cnp);
578cfe60390STomohiro Kusumi 	if (error == 0) {
579cfe60390STomohiro Kusumi 		ip->i_nlink--;
580cfe60390STomohiro Kusumi 		ip->i_flag |= IN_CHANGE;
581cfe60390STomohiro Kusumi 	}
582cfe60390STomohiro Kusumi out:
583cfe60390STomohiro Kusumi 	return (error);
584cfe60390STomohiro Kusumi }
585cfe60390STomohiro Kusumi 
586cfe60390STomohiro Kusumi /*
587cfe60390STomohiro Kusumi  * link vnode call
588cfe60390STomohiro Kusumi  */
589cfe60390STomohiro Kusumi static int
ext2_link(struct vop_old_link_args * ap)590cfe60390STomohiro Kusumi ext2_link(struct vop_old_link_args *ap)
591cfe60390STomohiro Kusumi {
592cfe60390STomohiro Kusumi 	struct vnode *vp = ap->a_vp;
593cfe60390STomohiro Kusumi 	struct vnode *tdvp = ap->a_tdvp;
594cfe60390STomohiro Kusumi 	struct componentname *cnp = ap->a_cnp;
595cfe60390STomohiro Kusumi 	struct inode *ip;
596cfe60390STomohiro Kusumi 	int error;
597cfe60390STomohiro Kusumi 
598cfe60390STomohiro Kusumi 	if (tdvp->v_mount != vp->v_mount) {
599cfe60390STomohiro Kusumi 		error = EXDEV;
600cfe60390STomohiro Kusumi 		goto out2;
601cfe60390STomohiro Kusumi 	}
602cfe60390STomohiro Kusumi 	if (tdvp != vp) {
603cfe60390STomohiro Kusumi 		error = vn_lock(vp, LK_EXCLUSIVE | LK_FAILRECLAIM);
604cfe60390STomohiro Kusumi 		if (error)
605cfe60390STomohiro Kusumi 			goto out2;
606cfe60390STomohiro Kusumi 	}
607cfe60390STomohiro Kusumi 	ip = VTOI(vp);
608cfe60390STomohiro Kusumi 	if ((nlink_t)ip->i_nlink >= EXT4_LINK_MAX) {
609cfe60390STomohiro Kusumi 		error = EMLINK;
610cfe60390STomohiro Kusumi 		goto out;
611cfe60390STomohiro Kusumi 	}
612cfe60390STomohiro Kusumi 	if (ip->i_flags & (IMMUTABLE | APPEND)) {
613cfe60390STomohiro Kusumi 		error = EPERM;
614cfe60390STomohiro Kusumi 		goto out;
615cfe60390STomohiro Kusumi 	}
616cfe60390STomohiro Kusumi 	ip->i_nlink++;
617cfe60390STomohiro Kusumi 	ip->i_flag |= IN_CHANGE;
618cfe60390STomohiro Kusumi 	error = ext2_update(vp, !DOINGASYNC(vp));
619cfe60390STomohiro Kusumi 	if (!error)
620cfe60390STomohiro Kusumi 		error = ext2_direnter(ip, tdvp, cnp);
621cfe60390STomohiro Kusumi 	if (error) {
622cfe60390STomohiro Kusumi 		ip->i_nlink--;
623cfe60390STomohiro Kusumi 		ip->i_flag |= IN_CHANGE;
624cfe60390STomohiro Kusumi 	}
625cfe60390STomohiro Kusumi out:
626cfe60390STomohiro Kusumi 	if (tdvp != vp)
627cfe60390STomohiro Kusumi 		vn_unlock(vp);
628cfe60390STomohiro Kusumi out2:
629cfe60390STomohiro Kusumi 	return (error);
630cfe60390STomohiro Kusumi }
631cfe60390STomohiro Kusumi 
632cfe60390STomohiro Kusumi static int
ext2_inc_nlink(struct inode * ip)633cfe60390STomohiro Kusumi ext2_inc_nlink(struct inode *ip)
634cfe60390STomohiro Kusumi {
635cfe60390STomohiro Kusumi 
636cfe60390STomohiro Kusumi 	ip->i_nlink++;
637cfe60390STomohiro Kusumi 
638cfe60390STomohiro Kusumi 	if (S_ISDIR(ip->i_mode) &&
639cfe60390STomohiro Kusumi 	    EXT2_HAS_RO_COMPAT_FEATURE(ip->i_e2fs, EXT2F_ROCOMPAT_DIR_NLINK) &&
640cfe60390STomohiro Kusumi 	    ip->i_nlink > 1) {
641cfe60390STomohiro Kusumi 		if (ip->i_nlink >= EXT4_LINK_MAX || ip->i_nlink == 2)
642cfe60390STomohiro Kusumi 			ip->i_nlink = 1;
643cfe60390STomohiro Kusumi 	} else if (ip->i_nlink > EXT4_LINK_MAX) {
644cfe60390STomohiro Kusumi 		ip->i_nlink--;
645cfe60390STomohiro Kusumi 		return (EMLINK);
646cfe60390STomohiro Kusumi 	}
647cfe60390STomohiro Kusumi 
648cfe60390STomohiro Kusumi 	return (0);
649cfe60390STomohiro Kusumi }
650cfe60390STomohiro Kusumi 
651cfe60390STomohiro Kusumi static void
ext2_dec_nlink(struct inode * ip)652cfe60390STomohiro Kusumi ext2_dec_nlink(struct inode *ip)
653cfe60390STomohiro Kusumi {
654cfe60390STomohiro Kusumi 
655cfe60390STomohiro Kusumi 	if (!S_ISDIR(ip->i_mode) || ip->i_nlink > 2)
656cfe60390STomohiro Kusumi 		ip->i_nlink--;
657cfe60390STomohiro Kusumi }
658cfe60390STomohiro Kusumi 
659cfe60390STomohiro Kusumi /*
660cfe60390STomohiro Kusumi  * Rename system call.
661cfe60390STomohiro Kusumi  * 	rename("foo", "bar");
662cfe60390STomohiro Kusumi  * is essentially
663cfe60390STomohiro Kusumi  *	unlink("bar");
664cfe60390STomohiro Kusumi  *	link("foo", "bar");
665cfe60390STomohiro Kusumi  *	unlink("foo");
666cfe60390STomohiro Kusumi  * but ``atomically''.  Can't do full commit without saving state in the
667cfe60390STomohiro Kusumi  * inode on disk which isn't feasible at this time.  Best we can do is
668cfe60390STomohiro Kusumi  * always guarantee the target exists.
669cfe60390STomohiro Kusumi  *
670cfe60390STomohiro Kusumi  * Basic algorithm is:
671cfe60390STomohiro Kusumi  *
672cfe60390STomohiro Kusumi  * 1) Bump link count on source while we're linking it to the
673cfe60390STomohiro Kusumi  *    target.  This also ensure the inode won't be deleted out
674cfe60390STomohiro Kusumi  *    from underneath us while we work (it may be truncated by
675cfe60390STomohiro Kusumi  *    a concurrent `trunc' or `open' for creation).
676cfe60390STomohiro Kusumi  * 2) Link source to destination.  If destination already exists,
677cfe60390STomohiro Kusumi  *    delete it first.
678cfe60390STomohiro Kusumi  * 3) Unlink source reference to inode if still around. If a
679cfe60390STomohiro Kusumi  *    directory was moved and the parent of the destination
680cfe60390STomohiro Kusumi  *    is different from the source, patch the ".." entry in the
681cfe60390STomohiro Kusumi  *    directory.
682cfe60390STomohiro Kusumi  */
683cfe60390STomohiro Kusumi static int
ext2_rename(struct vop_old_rename_args * ap)684cfe60390STomohiro Kusumi ext2_rename(struct vop_old_rename_args *ap)
685cfe60390STomohiro Kusumi {
686cfe60390STomohiro Kusumi 	struct vnode *tvp = ap->a_tvp;
687cfe60390STomohiro Kusumi 	struct vnode *tdvp = ap->a_tdvp;
688cfe60390STomohiro Kusumi 	struct vnode *fvp = ap->a_fvp;
689cfe60390STomohiro Kusumi 	struct vnode *fdvp = ap->a_fdvp;
690cfe60390STomohiro Kusumi 	struct componentname *tcnp = ap->a_tcnp;
691cfe60390STomohiro Kusumi 	struct componentname *fcnp = ap->a_fcnp;
692cfe60390STomohiro Kusumi 	struct inode *ip, *xp, *dp;
693cfe60390STomohiro Kusumi 	struct dirtemplate *dirbuf;
694cfe60390STomohiro Kusumi 	int doingdirectory = 0, oldparent = 0, newparent = 0;
695cfe60390STomohiro Kusumi 	int error = 0;
696cfe60390STomohiro Kusumi 	u_char namlen;
697cfe60390STomohiro Kusumi 
698cfe60390STomohiro Kusumi 	/*
699cfe60390STomohiro Kusumi 	 * Check for cross-device rename.
700cfe60390STomohiro Kusumi 	 */
701cfe60390STomohiro Kusumi 	if ((fvp->v_mount != tdvp->v_mount) ||
702cfe60390STomohiro Kusumi 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
703cfe60390STomohiro Kusumi 		error = EXDEV;
704cfe60390STomohiro Kusumi abortit:
705cfe60390STomohiro Kusumi 		if (tdvp == tvp)
706cfe60390STomohiro Kusumi 			vrele(tdvp);
707cfe60390STomohiro Kusumi 		else
708cfe60390STomohiro Kusumi 			vput(tdvp);
709cfe60390STomohiro Kusumi 		if (tvp)
710cfe60390STomohiro Kusumi 			vput(tvp);
711cfe60390STomohiro Kusumi 		vrele(fdvp);
712cfe60390STomohiro Kusumi 		vrele(fvp);
713cfe60390STomohiro Kusumi 		return (error);
714cfe60390STomohiro Kusumi 	}
715cfe60390STomohiro Kusumi 
716cfe60390STomohiro Kusumi 	if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
717cfe60390STomohiro Kusumi 	    (VTOI(tdvp)->i_flags & APPEND))) {
718cfe60390STomohiro Kusumi 		error = EPERM;
719cfe60390STomohiro Kusumi 		goto abortit;
720cfe60390STomohiro Kusumi 	}
721cfe60390STomohiro Kusumi 
722cfe60390STomohiro Kusumi 	/*
723cfe60390STomohiro Kusumi 	 * Renaming a file to itself has no effect.  The upper layers should
724cfe60390STomohiro Kusumi 	 * not call us in that case.  Temporarily just warn if they do.
725cfe60390STomohiro Kusumi 	 */
726cfe60390STomohiro Kusumi 	if (fvp == tvp) {
727cfe60390STomohiro Kusumi 		SDT_PROBE2(ext2fs, , vnops, trace, 1,
728cfe60390STomohiro Kusumi 		    "rename: fvp == tvp (can't happen)");
729cfe60390STomohiro Kusumi 		error = 0;
730cfe60390STomohiro Kusumi 		goto abortit;
731cfe60390STomohiro Kusumi 	}
732cfe60390STomohiro Kusumi 
733cfe60390STomohiro Kusumi 	if ((error = vn_lock(fvp, LK_EXCLUSIVE | LK_FAILRECLAIM)) != 0)
734cfe60390STomohiro Kusumi 		goto abortit;
735cfe60390STomohiro Kusumi 	dp = VTOI(fdvp);
736cfe60390STomohiro Kusumi 	ip = VTOI(fvp);
737cfe60390STomohiro Kusumi 	if (ip->i_nlink >= EXT4_LINK_MAX &&
738cfe60390STomohiro Kusumi 	    !EXT2_HAS_RO_COMPAT_FEATURE(ip->i_e2fs, EXT2F_ROCOMPAT_DIR_NLINK)) {
739cfe60390STomohiro Kusumi 		vn_unlock(fvp);
740cfe60390STomohiro Kusumi 		error = EMLINK;
741cfe60390STomohiro Kusumi 		goto abortit;
742cfe60390STomohiro Kusumi 	}
743cfe60390STomohiro Kusumi 	if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
744cfe60390STomohiro Kusumi 	    || (dp->i_flags & APPEND)) {
745cfe60390STomohiro Kusumi 		vn_unlock(fvp);
746cfe60390STomohiro Kusumi 		error = EPERM;
747cfe60390STomohiro Kusumi 		goto abortit;
748cfe60390STomohiro Kusumi 	}
749cfe60390STomohiro Kusumi 	if ((ip->i_mode & IFMT) == IFDIR) {
750cfe60390STomohiro Kusumi 		/*
751cfe60390STomohiro Kusumi 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
752cfe60390STomohiro Kusumi 		 */
753cfe60390STomohiro Kusumi 		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
754cfe60390STomohiro Kusumi 		    dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & CNP_ISDOTDOT ||
755cfe60390STomohiro Kusumi 		    (ip->i_flag & IN_RENAME)) {
756cfe60390STomohiro Kusumi 			vn_unlock(fvp);
757cfe60390STomohiro Kusumi 			error = EINVAL;
758cfe60390STomohiro Kusumi 			goto abortit;
759cfe60390STomohiro Kusumi 		}
760cfe60390STomohiro Kusumi 		ip->i_flag |= IN_RENAME;
761cfe60390STomohiro Kusumi 		oldparent = dp->i_number;
762cfe60390STomohiro Kusumi 		doingdirectory++;
763cfe60390STomohiro Kusumi 	}
764cfe60390STomohiro Kusumi 	//vrele(fdvp); XXX
765cfe60390STomohiro Kusumi 
766cfe60390STomohiro Kusumi 	/*
767cfe60390STomohiro Kusumi 	 * When the target exists, both the directory
768cfe60390STomohiro Kusumi 	 * and target vnodes are returned locked.
769cfe60390STomohiro Kusumi 	 */
770cfe60390STomohiro Kusumi 	dp = VTOI(tdvp);
771cfe60390STomohiro Kusumi 	xp = NULL;
772cfe60390STomohiro Kusumi 	if (tvp)
773cfe60390STomohiro Kusumi 		xp = VTOI(tvp);
774cfe60390STomohiro Kusumi 
775cfe60390STomohiro Kusumi 	/*
776cfe60390STomohiro Kusumi 	 * 1) Bump link count while we're moving stuff
777cfe60390STomohiro Kusumi 	 *    around.  If we crash somewhere before
778cfe60390STomohiro Kusumi 	 *    completing our work, the link count
779cfe60390STomohiro Kusumi 	 *    may be wrong, but correctable.
780cfe60390STomohiro Kusumi 	 */
781cfe60390STomohiro Kusumi 	ext2_inc_nlink(ip);
782cfe60390STomohiro Kusumi 	ip->i_flag |= IN_CHANGE;
783cfe60390STomohiro Kusumi 	if ((error = ext2_update(fvp, !DOINGASYNC(fvp))) != 0) {
784cfe60390STomohiro Kusumi 		vn_unlock(fvp);
785cfe60390STomohiro Kusumi 		goto bad;
786cfe60390STomohiro Kusumi 	}
787cfe60390STomohiro Kusumi 
788cfe60390STomohiro Kusumi 	/*
789cfe60390STomohiro Kusumi 	 * If ".." must be changed (ie the directory gets a new
790cfe60390STomohiro Kusumi 	 * parent) then the source directory must not be in the
791cfe60390STomohiro Kusumi 	 * directory hierarchy above the target, as this would
792cfe60390STomohiro Kusumi 	 * orphan everything below the source directory. Also
793cfe60390STomohiro Kusumi 	 * the user must have write permission in the source so
794cfe60390STomohiro Kusumi 	 * as to be able to change "..". We must repeat the call
795cfe60390STomohiro Kusumi 	 * to namei, as the parent directory is unlocked by the
796cfe60390STomohiro Kusumi 	 * call to checkpath().
797cfe60390STomohiro Kusumi 	 */
798cfe60390STomohiro Kusumi 	error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred);
799cfe60390STomohiro Kusumi 	vn_unlock(fvp);
800cfe60390STomohiro Kusumi 
801cfe60390STomohiro Kusumi 	/*
802cfe60390STomohiro Kusumi 	 * tvp (if not NULL) and tdvp are locked.  fvp and fdvp are not.
803cfe60390STomohiro Kusumi 	 * dp and xp are set according to tdvp and tvp.
804cfe60390STomohiro Kusumi 	 */
805cfe60390STomohiro Kusumi 	if (oldparent != dp->i_number)
806cfe60390STomohiro Kusumi 		newparent = dp->i_number;
807cfe60390STomohiro Kusumi 	if (doingdirectory && newparent) {
808cfe60390STomohiro Kusumi 		if (error)	/* write access check above */
809cfe60390STomohiro Kusumi 			goto bad;
810cfe60390STomohiro Kusumi 
811cfe60390STomohiro Kusumi 		/*
812cfe60390STomohiro Kusumi 		 * Prepare for relookup, get rid of xp
813cfe60390STomohiro Kusumi 		 */
814cfe60390STomohiro Kusumi 		if (xp != NULL) {
815cfe60390STomohiro Kusumi 			vput(tvp);
816cfe60390STomohiro Kusumi 			xp = NULL;
817cfe60390STomohiro Kusumi 		}
818cfe60390STomohiro Kusumi 
819cfe60390STomohiro Kusumi 		/*
820cfe60390STomohiro Kusumi 		 * checkpath vput()'s tdvp (VTOI(dp)) on return no matter what,
821cfe60390STomohiro Kusumi 		 * get an extra ref so we wind up with just an unlocked, ref'd
822cfe60390STomohiro Kusumi 		 * tdvp.  The 'out' target skips xp and tdvp cleanups.  Our
823cfe60390STomohiro Kusumi 		 * tdvp is now unlocked so we have to clean it up ourselves.
824cfe60390STomohiro Kusumi 		 */
825cfe60390STomohiro Kusumi 		vref(tdvp);
826cfe60390STomohiro Kusumi 		error = ext2_checkpath(ip, dp, tcnp->cn_cred);
827cfe60390STomohiro Kusumi 		tcnp->cn_flags |= CNP_PDIRUNLOCK;
828cfe60390STomohiro Kusumi 		if (error) {
829cfe60390STomohiro Kusumi 			vrele(tdvp);
830cfe60390STomohiro Kusumi 			goto out;
831cfe60390STomohiro Kusumi 		}
832cfe60390STomohiro Kusumi 		/*
833cfe60390STomohiro Kusumi 		 * relookup no longer messes with the ref count.  An unlocked
834cfe60390STomohiro Kusumi 		 * tdvp must be passed and if no error occurs a locked tdvp
835cfe60390STomohiro Kusumi 		 * will be returned.  We have to use the out target again.
836cfe60390STomohiro Kusumi 		 */
837cfe60390STomohiro Kusumi 		error = relookup(tdvp, &tvp, tcnp);
838cfe60390STomohiro Kusumi 		if (error) {
839cfe60390STomohiro Kusumi 			if (tcnp->cn_flags & CNP_PDIRUNLOCK)
840cfe60390STomohiro Kusumi 				vrele(tdvp);
841cfe60390STomohiro Kusumi 			else
842cfe60390STomohiro Kusumi 				vput(tdvp);
843cfe60390STomohiro Kusumi 			goto out;
844cfe60390STomohiro Kusumi 		}
845cfe60390STomohiro Kusumi 
846cfe60390STomohiro Kusumi 		/*
847cfe60390STomohiro Kusumi 		 * tdvp is locked at this point.  in the RENAME case tvp may
848cfe60390STomohiro Kusumi 		 * be NULL without an error, assign xp accordingly.  The
849cfe60390STomohiro Kusumi 		 * 'bad' target can be used again after this.
850cfe60390STomohiro Kusumi 		 */
851cfe60390STomohiro Kusumi 		dp = VTOI(tdvp);
852cfe60390STomohiro Kusumi 		if (tvp)
853cfe60390STomohiro Kusumi 			xp = VTOI(tvp);
854cfe60390STomohiro Kusumi 	}
855cfe60390STomohiro Kusumi 
856cfe60390STomohiro Kusumi 	/*
857cfe60390STomohiro Kusumi 	 * 2) If target doesn't exist, link the target
858cfe60390STomohiro Kusumi 	 *    to the source and unlink the source.
859cfe60390STomohiro Kusumi 	 *    Otherwise, rewrite the target directory
860cfe60390STomohiro Kusumi 	 *    entry to reference the source inode and
861cfe60390STomohiro Kusumi 	 *    expunge the original entry's existence.
862cfe60390STomohiro Kusumi 	 */
863cfe60390STomohiro Kusumi 	if (xp == NULL) {
864cfe60390STomohiro Kusumi 		if (dp->i_devvp != ip->i_devvp)
865cfe60390STomohiro Kusumi 			panic("ext2_rename: EXDEV");
866cfe60390STomohiro Kusumi 		/*
867cfe60390STomohiro Kusumi 		 * Account for ".." in new directory.
868cfe60390STomohiro Kusumi 		 * When source and destination have the same
869cfe60390STomohiro Kusumi 		 * parent we don't fool with the link count.
870cfe60390STomohiro Kusumi 		 */
871cfe60390STomohiro Kusumi 		if (doingdirectory && newparent) {
872cfe60390STomohiro Kusumi 			if ((nlink_t)dp->i_nlink >= LINK_MAX) {
873cfe60390STomohiro Kusumi 				error = EMLINK;
874cfe60390STomohiro Kusumi 				goto bad;
875cfe60390STomohiro Kusumi 			}
876cfe60390STomohiro Kusumi 			error = ext2_inc_nlink(dp);
877cfe60390STomohiro Kusumi 			if (error)
878cfe60390STomohiro Kusumi 				goto bad;
879cfe60390STomohiro Kusumi 
880cfe60390STomohiro Kusumi 			dp->i_flag |= IN_CHANGE;
881cfe60390STomohiro Kusumi 			error = ext2_update(tdvp, !DOINGASYNC(tdvp));
882cfe60390STomohiro Kusumi 			if (error)
883cfe60390STomohiro Kusumi 				goto bad;
884cfe60390STomohiro Kusumi 		}
885cfe60390STomohiro Kusumi 		error = ext2_direnter(ip, tdvp, tcnp);
886cfe60390STomohiro Kusumi 		if (error) {
887cfe60390STomohiro Kusumi 			if (doingdirectory && newparent) {
888cfe60390STomohiro Kusumi 				ext2_dec_nlink(dp);
889cfe60390STomohiro Kusumi 				dp->i_flag |= IN_CHANGE;
890cfe60390STomohiro Kusumi 				(void)ext2_update(tdvp, 1);
891cfe60390STomohiro Kusumi 			}
892cfe60390STomohiro Kusumi 			goto bad;
893cfe60390STomohiro Kusumi 		}
894cfe60390STomohiro Kusumi 		vput(tdvp);
895cfe60390STomohiro Kusumi 	} else {
896cfe60390STomohiro Kusumi 		if (xp->i_devvp != dp->i_devvp || xp->i_devvp != ip->i_devvp)
897cfe60390STomohiro Kusumi 			panic("ext2_rename: EXDEV");
898cfe60390STomohiro Kusumi 		/*
899cfe60390STomohiro Kusumi 		 * Short circuit rename(foo, foo).
900cfe60390STomohiro Kusumi 		 */
901cfe60390STomohiro Kusumi 		if (xp->i_number == ip->i_number)
902cfe60390STomohiro Kusumi 			panic("ext2_rename: same file");
903cfe60390STomohiro Kusumi 		/*
904cfe60390STomohiro Kusumi 		 * If the parent directory is "sticky", then the user must
905cfe60390STomohiro Kusumi 		 * own the parent directory, or the destination of the rename,
906cfe60390STomohiro Kusumi 		 * otherwise the destination may not be changed (except by
907cfe60390STomohiro Kusumi 		 * root). This implements append-only directories.
908cfe60390STomohiro Kusumi 		 */
909cfe60390STomohiro Kusumi 		if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
910cfe60390STomohiro Kusumi 		    tcnp->cn_cred->cr_uid != dp->i_uid &&
911cfe60390STomohiro Kusumi 		    xp->i_uid != tcnp->cn_cred->cr_uid) {
912cfe60390STomohiro Kusumi 			error = EPERM;
913cfe60390STomohiro Kusumi 			goto bad;
914cfe60390STomohiro Kusumi 		}
915cfe60390STomohiro Kusumi 		/*
916cfe60390STomohiro Kusumi 		 * Target must be empty if a directory and have no links
917cfe60390STomohiro Kusumi 		 * to it. Also, ensure source and target are compatible
918cfe60390STomohiro Kusumi 		 * (both directories, or both not directories).
919cfe60390STomohiro Kusumi 		 */
920cfe60390STomohiro Kusumi 		if ((xp->i_mode & IFMT) == IFDIR) {
921cfe60390STomohiro Kusumi 			if (!ext2_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
922cfe60390STomohiro Kusumi 				error = ENOTEMPTY;
923cfe60390STomohiro Kusumi 				goto bad;
924cfe60390STomohiro Kusumi 			}
925cfe60390STomohiro Kusumi 			if (!doingdirectory) {
926cfe60390STomohiro Kusumi 				error = ENOTDIR;
927cfe60390STomohiro Kusumi 				goto bad;
928cfe60390STomohiro Kusumi 			}
929cfe60390STomohiro Kusumi 		} else if (doingdirectory) {
930cfe60390STomohiro Kusumi 			error = EISDIR;
931cfe60390STomohiro Kusumi 			goto bad;
932cfe60390STomohiro Kusumi 		}
933cfe60390STomohiro Kusumi 		error = ext2_dirrewrite(dp, ip, tcnp);
934cfe60390STomohiro Kusumi 		if (error)
935cfe60390STomohiro Kusumi 			goto bad;
936cfe60390STomohiro Kusumi 		/*
937cfe60390STomohiro Kusumi 		 * If the target directory is in the same
938cfe60390STomohiro Kusumi 		 * directory as the source directory,
939cfe60390STomohiro Kusumi 		 * decrement the link count on the parent
940cfe60390STomohiro Kusumi 		 * of the target directory.
941cfe60390STomohiro Kusumi 		 */
942cfe60390STomohiro Kusumi 		if (doingdirectory && !newparent) {
943cfe60390STomohiro Kusumi 			ext2_dec_nlink(dp);
944cfe60390STomohiro Kusumi 			dp->i_flag |= IN_CHANGE;
945cfe60390STomohiro Kusumi 		}
946cfe60390STomohiro Kusumi 		vput(tdvp);
947cfe60390STomohiro Kusumi 		/*
948cfe60390STomohiro Kusumi 		 * Adjust the link count of the target to
949cfe60390STomohiro Kusumi 		 * reflect the dirrewrite above.  If this is
950cfe60390STomohiro Kusumi 		 * a directory it is empty and there are
951cfe60390STomohiro Kusumi 		 * no links to it, so we can squash the inode and
952cfe60390STomohiro Kusumi 		 * any space associated with it.  We disallowed
953cfe60390STomohiro Kusumi 		 * renaming over top of a directory with links to
954cfe60390STomohiro Kusumi 		 * it above, as the remaining link would point to
955cfe60390STomohiro Kusumi 		 * a directory without "." or ".." entries.
956cfe60390STomohiro Kusumi 		 */
957cfe60390STomohiro Kusumi 		ext2_dec_nlink(xp);
958cfe60390STomohiro Kusumi 		if (doingdirectory) {
959cfe60390STomohiro Kusumi 			if (xp->i_nlink > 2)
960cfe60390STomohiro Kusumi 				panic("ext2_rename: linked directory");
961cfe60390STomohiro Kusumi 			error = ext2_truncate(tvp, (off_t)0, IO_SYNC,
962cfe60390STomohiro Kusumi 			    tcnp->cn_cred);
963cfe60390STomohiro Kusumi 			xp->i_nlink = 0;
964cfe60390STomohiro Kusumi 		}
965cfe60390STomohiro Kusumi 		xp->i_flag |= IN_CHANGE;
966cfe60390STomohiro Kusumi 		vput(tvp);
967cfe60390STomohiro Kusumi 		xp = NULL;
968cfe60390STomohiro Kusumi 	}
969cfe60390STomohiro Kusumi 
970cfe60390STomohiro Kusumi 	/*
971cfe60390STomohiro Kusumi 	 * 3) Unlink the source.
972cfe60390STomohiro Kusumi 	 */
973cfe60390STomohiro Kusumi 	fcnp->cn_flags &= ~CNP_MODMASK;
974cfe60390STomohiro Kusumi 	fcnp->cn_flags |= CNP_LOCKPARENT;
975cfe60390STomohiro Kusumi 	//vref(fdvp); XXX
976cfe60390STomohiro Kusumi 	error = relookup(fdvp, &fvp, fcnp);
977cfe60390STomohiro Kusumi 	if (error) {
978cfe60390STomohiro Kusumi 		/*
979cfe60390STomohiro Kusumi 		 * From name has disappeared.
980cfe60390STomohiro Kusumi 		 */
981cfe60390STomohiro Kusumi 		if (doingdirectory)
982cfe60390STomohiro Kusumi 			panic("ext2_rename: lost dir entry");
983cfe60390STomohiro Kusumi 		/* ip->i_flag only sets IN_RENAME if doingdirectory */
984cfe60390STomohiro Kusumi 		vrele(ap->a_fvp);
985cfe60390STomohiro Kusumi 		if (fcnp->cn_flags & CNP_PDIRUNLOCK)
986cfe60390STomohiro Kusumi 			vrele(fdvp);
987cfe60390STomohiro Kusumi 		else
988cfe60390STomohiro Kusumi 			vput(fdvp);
989cfe60390STomohiro Kusumi 		return (0);
990cfe60390STomohiro Kusumi 	}
991cfe60390STomohiro Kusumi 	KKASSERT((fcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
992cfe60390STomohiro Kusumi 
993cfe60390STomohiro Kusumi 	/*
994cfe60390STomohiro Kusumi 	 * This case shouldn't occur
995cfe60390STomohiro Kusumi 	 */
996cfe60390STomohiro Kusumi 	if (fvp == NULL) {
997cfe60390STomohiro Kusumi 		/*
998cfe60390STomohiro Kusumi 		 * From name has disappeared.
999cfe60390STomohiro Kusumi 		 */
1000cfe60390STomohiro Kusumi 		if (doingdirectory)
1001cfe60390STomohiro Kusumi 			panic("ext2_rename: lost dir entry");
1002cfe60390STomohiro Kusumi 		/* ip->i_flag only sets IN_RENAME if doingdirectory */
1003cfe60390STomohiro Kusumi 		vrele(ap->a_fvp);
1004cfe60390STomohiro Kusumi 		vput(fvp);
1005cfe60390STomohiro Kusumi 		vput(fdvp);
1006cfe60390STomohiro Kusumi 		return (0);
1007cfe60390STomohiro Kusumi 	}
1008cfe60390STomohiro Kusumi 
1009cfe60390STomohiro Kusumi 	/*
1010cfe60390STomohiro Kusumi 	 * fvp and fdvp are both ref'd and locked.
1011cfe60390STomohiro Kusumi 	 */
1012cfe60390STomohiro Kusumi 	xp = VTOI(fvp);
1013cfe60390STomohiro Kusumi 	dp = VTOI(fdvp);
1014cfe60390STomohiro Kusumi 
1015cfe60390STomohiro Kusumi 	/*
1016cfe60390STomohiro Kusumi 	 * Ensure that the directory entry still exists and has not
1017cfe60390STomohiro Kusumi 	 * changed while the new name has been entered. If the source is
1018cfe60390STomohiro Kusumi 	 * a file then the entry may have been unlinked or renamed. In
1019cfe60390STomohiro Kusumi 	 * either case there is no further work to be done. If the source
1020cfe60390STomohiro Kusumi 	 * is a directory then it cannot have been rmdir'ed; its link
1021cfe60390STomohiro Kusumi 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
1022cfe60390STomohiro Kusumi 	 * The IN_RENAME flag ensures that it cannot be moved by another
1023cfe60390STomohiro Kusumi 	 * rename.
1024cfe60390STomohiro Kusumi 	 */
1025cfe60390STomohiro Kusumi 	if (xp != ip) {
1026cfe60390STomohiro Kusumi 		/*
1027cfe60390STomohiro Kusumi 		 * From name resolves to a different inode.  IN_RENAME is
1028cfe60390STomohiro Kusumi 		 * not sufficient protection against timing window races
1029cfe60390STomohiro Kusumi 		 * so we can't panic here.
1030cfe60390STomohiro Kusumi 		 */
1031cfe60390STomohiro Kusumi 	} else {
1032cfe60390STomohiro Kusumi 		/*
1033cfe60390STomohiro Kusumi 		 * If the source is a directory with a
1034cfe60390STomohiro Kusumi 		 * new parent, the link count of the old
1035cfe60390STomohiro Kusumi 		 * parent directory must be decremented
1036cfe60390STomohiro Kusumi 		 * and ".." set to point to the new parent.
1037cfe60390STomohiro Kusumi 		 */
1038cfe60390STomohiro Kusumi 		if (doingdirectory && newparent) {
1039cfe60390STomohiro Kusumi 			ext2_dec_nlink(dp);
1040cfe60390STomohiro Kusumi 			dp->i_flag |= IN_CHANGE;
1041cfe60390STomohiro Kusumi 			dirbuf = malloc(dp->i_e2fs->e2fs_bsize, M_TEMP, M_WAITOK | M_ZERO);
1042cfe60390STomohiro Kusumi 			error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
1043cfe60390STomohiro Kusumi 			    sizeof (struct dirtemplate), (off_t)0,
1044cfe60390STomohiro Kusumi 			    UIO_SYSSPACE, IO_NODELOCKED,
1045cfe60390STomohiro Kusumi 			    tcnp->cn_cred, NULL);
1046cfe60390STomohiro Kusumi 			if (error == 0) {
1047cfe60390STomohiro Kusumi 				/* Like ufs little-endian: */
1048cfe60390STomohiro Kusumi 				namlen = dirbuf->dotdot_type;
1049cfe60390STomohiro Kusumi 				if (namlen != 2 ||
1050cfe60390STomohiro Kusumi 				    dirbuf->dotdot_name[0] != '.' ||
1051cfe60390STomohiro Kusumi 				    dirbuf->dotdot_name[1] != '.') {
1052cfe60390STomohiro Kusumi 					ext2_dirbad(xp, (doff_t)12,
1053cfe60390STomohiro Kusumi 					    "rename: mangled dir");
1054cfe60390STomohiro Kusumi 				} else {
1055cfe60390STomohiro Kusumi 					dirbuf->dotdot_ino = htole32(newparent);
1056cfe60390STomohiro Kusumi 					/*
1057cfe60390STomohiro Kusumi 					 * dirblock 0 could be htree root,
1058cfe60390STomohiro Kusumi 					 * try both csum update functions.
1059cfe60390STomohiro Kusumi 					 */
1060cfe60390STomohiro Kusumi 					ext2_dirent_csum_set(ip,
1061cfe60390STomohiro Kusumi 					    (struct ext2fs_direct_2 *)dirbuf);
1062cfe60390STomohiro Kusumi 					ext2_dx_csum_set(ip,
1063cfe60390STomohiro Kusumi 					    (struct ext2fs_direct_2 *)dirbuf);
1064cfe60390STomohiro Kusumi 					vn_rdwr(UIO_WRITE, fvp,
1065cfe60390STomohiro Kusumi 					    (caddr_t)&dirbuf,
1066cfe60390STomohiro Kusumi 					    sizeof (struct dirtemplate),
1067cfe60390STomohiro Kusumi 					    (off_t)0, UIO_SYSSPACE,
1068cfe60390STomohiro Kusumi 					    IO_NODELOCKED | IO_SYNC,
1069cfe60390STomohiro Kusumi 					    tcnp->cn_cred, NULL);
1070cfe60390STomohiro Kusumi 				}
1071cfe60390STomohiro Kusumi 			}
1072cfe60390STomohiro Kusumi 			free(dirbuf, M_TEMP);
1073cfe60390STomohiro Kusumi 		}
1074cfe60390STomohiro Kusumi 		error = ext2_dirremove(fdvp, fcnp);
1075cfe60390STomohiro Kusumi 		if (!error) {
1076cfe60390STomohiro Kusumi 			ext2_dec_nlink(xp);
1077cfe60390STomohiro Kusumi 			xp->i_flag |= IN_CHANGE;
1078cfe60390STomohiro Kusumi 		}
1079cfe60390STomohiro Kusumi 		xp->i_flag &= ~IN_RENAME;
1080cfe60390STomohiro Kusumi 	}
1081cfe60390STomohiro Kusumi 	if (dp)
1082cfe60390STomohiro Kusumi 		vput(fdvp);
1083cfe60390STomohiro Kusumi 	if (xp)
1084cfe60390STomohiro Kusumi 		vput(fvp);
1085cfe60390STomohiro Kusumi 	vrele(ap->a_fvp);
1086cfe60390STomohiro Kusumi 	return (error);
1087cfe60390STomohiro Kusumi 
1088cfe60390STomohiro Kusumi bad:
1089cfe60390STomohiro Kusumi 	if (xp)
1090cfe60390STomohiro Kusumi 		vput(ITOV(xp));
1091cfe60390STomohiro Kusumi 	vput(ITOV(dp));
1092cfe60390STomohiro Kusumi out:
1093cfe60390STomohiro Kusumi 	if (doingdirectory)
1094cfe60390STomohiro Kusumi 		ip->i_flag &= ~IN_RENAME;
1095cfe60390STomohiro Kusumi 	if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1096cfe60390STomohiro Kusumi 		ext2_dec_nlink(ip);
1097cfe60390STomohiro Kusumi 		ip->i_flag |= IN_CHANGE;
1098cfe60390STomohiro Kusumi 		ip->i_flag &= ~IN_RENAME;
1099cfe60390STomohiro Kusumi 		vput(fvp);
1100cfe60390STomohiro Kusumi 	} else
1101cfe60390STomohiro Kusumi 		vrele(fvp);
1102cfe60390STomohiro Kusumi 	return (error);
1103cfe60390STomohiro Kusumi }
1104cfe60390STomohiro Kusumi 
1105cfe60390STomohiro Kusumi /*
1106cfe60390STomohiro Kusumi  * Mkdir system call
1107cfe60390STomohiro Kusumi  */
1108cfe60390STomohiro Kusumi static int
ext2_mkdir(struct vop_old_mkdir_args * ap)1109cfe60390STomohiro Kusumi ext2_mkdir(struct vop_old_mkdir_args *ap)
1110cfe60390STomohiro Kusumi {
1111cfe60390STomohiro Kusumi 	struct m_ext2fs *fs;
1112cfe60390STomohiro Kusumi 	struct vnode *dvp = ap->a_dvp;
1113cfe60390STomohiro Kusumi 	struct vattr *vap = ap->a_vap;
1114cfe60390STomohiro Kusumi 	struct componentname *cnp = ap->a_cnp;
1115cfe60390STomohiro Kusumi 	struct inode *ip, *dp;
1116cfe60390STomohiro Kusumi 	struct vnode *tvp;
1117cfe60390STomohiro Kusumi 	struct dirtemplate dirtemplate, *dtp;
1118cfe60390STomohiro Kusumi 	char *buf = NULL;
1119cfe60390STomohiro Kusumi 	int error, dmode;
1120cfe60390STomohiro Kusumi 
1121cfe60390STomohiro Kusumi 	dp = VTOI(dvp);
1122cfe60390STomohiro Kusumi 	if ((nlink_t)dp->i_nlink >= EXT4_LINK_MAX &&
1123cfe60390STomohiro Kusumi 	    !EXT2_HAS_RO_COMPAT_FEATURE(dp->i_e2fs, EXT2F_ROCOMPAT_DIR_NLINK)) {
1124cfe60390STomohiro Kusumi 		error = EMLINK;
1125cfe60390STomohiro Kusumi 		goto out;
1126cfe60390STomohiro Kusumi 	}
1127cfe60390STomohiro Kusumi 	dmode = vap->va_mode & 0777;
1128cfe60390STomohiro Kusumi 	dmode |= IFDIR;
1129cfe60390STomohiro Kusumi 	/*
1130cfe60390STomohiro Kusumi 	 * Must simulate part of ext2_makeinode here to acquire the inode,
1131cfe60390STomohiro Kusumi 	 * but not have it entered in the parent directory. The entry is
1132cfe60390STomohiro Kusumi 	 * made later after writing "." and ".." entries.
1133cfe60390STomohiro Kusumi 	 */
1134cfe60390STomohiro Kusumi 	error = ext2_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1135cfe60390STomohiro Kusumi 	if (error)
1136cfe60390STomohiro Kusumi 		goto out;
1137cfe60390STomohiro Kusumi 	ip = VTOI(tvp);
1138cfe60390STomohiro Kusumi 	fs = ip->i_e2fs;
1139cfe60390STomohiro Kusumi 	ip->i_gid = dp->i_gid;
1140cfe60390STomohiro Kusumi #ifdef SUIDDIR
1141cfe60390STomohiro Kusumi 	{
1142cfe60390STomohiro Kusumi 		/*
1143cfe60390STomohiro Kusumi 		 * if we are hacking owners here, (only do this where told to)
1144cfe60390STomohiro Kusumi 		 * and we are not giving it TOO root, (would subvert quotas)
1145cfe60390STomohiro Kusumi 		 * then go ahead and give it to the other user.
1146cfe60390STomohiro Kusumi 		 * The new directory also inherits the SUID bit.
1147cfe60390STomohiro Kusumi 		 * If user's UID and dir UID are the same,
1148cfe60390STomohiro Kusumi 		 * 'give it away' so that the SUID is still forced on.
1149cfe60390STomohiro Kusumi 		 */
1150cfe60390STomohiro Kusumi 		if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1151cfe60390STomohiro Kusumi 		    (dp->i_mode & ISUID) && dp->i_uid) {
1152cfe60390STomohiro Kusumi 			dmode |= ISUID;
1153cfe60390STomohiro Kusumi 			ip->i_uid = dp->i_uid;
1154cfe60390STomohiro Kusumi 		} else {
1155cfe60390STomohiro Kusumi 			ip->i_uid = cnp->cn_cred->cr_uid;
1156cfe60390STomohiro Kusumi 		}
1157cfe60390STomohiro Kusumi 	}
1158cfe60390STomohiro Kusumi #else
1159cfe60390STomohiro Kusumi 	ip->i_uid = cnp->cn_cred->cr_uid;
1160cfe60390STomohiro Kusumi #endif
1161cfe60390STomohiro Kusumi 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1162cfe60390STomohiro Kusumi 	ip->i_mode = dmode;
1163cfe60390STomohiro Kusumi 	tvp->v_type = VDIR;	/* Rest init'd in getnewvnode(). */
1164cfe60390STomohiro Kusumi 	ip->i_nlink = 2;
1165cfe60390STomohiro Kusumi 	if (cnp->cn_flags & CNP_ISWHITEOUT)
1166cfe60390STomohiro Kusumi 		ip->i_flags |= UF_OPAQUE;
1167cfe60390STomohiro Kusumi 	error = ext2_update(tvp, 1);
1168cfe60390STomohiro Kusumi 
1169cfe60390STomohiro Kusumi 	/*
1170cfe60390STomohiro Kusumi 	 * The vnode must have a VM object in order to issue buffer cache
1171cfe60390STomohiro Kusumi 	 * ops on it.
1172cfe60390STomohiro Kusumi 	 */
1173cfe60390STomohiro Kusumi 	vinitvmio(tvp, 0, PAGE_SIZE, -1);
1174cfe60390STomohiro Kusumi 
1175cfe60390STomohiro Kusumi 	/*
1176cfe60390STomohiro Kusumi 	 * Bump link count in parent directory
1177cfe60390STomohiro Kusumi 	 * to reflect work done below.  Should
1178cfe60390STomohiro Kusumi 	 * be done before reference is created
1179cfe60390STomohiro Kusumi 	 * so reparation is possible if we crash.
1180cfe60390STomohiro Kusumi 	 */
1181cfe60390STomohiro Kusumi 	ext2_inc_nlink(dp);
1182cfe60390STomohiro Kusumi 	dp->i_flag |= IN_CHANGE;
1183cfe60390STomohiro Kusumi 	error = ext2_update(dvp, !DOINGASYNC(dvp));
1184cfe60390STomohiro Kusumi 	if (error)
1185cfe60390STomohiro Kusumi 		goto bad;
1186cfe60390STomohiro Kusumi 
1187cfe60390STomohiro Kusumi 	/* Initialize directory with "." and ".." from static template. */
1188cfe60390STomohiro Kusumi 	if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
1189cfe60390STomohiro Kusumi 	    EXT2F_INCOMPAT_FTYPE))
1190cfe60390STomohiro Kusumi 		dtp = &mastertemplate;
1191cfe60390STomohiro Kusumi 	else
1192cfe60390STomohiro Kusumi 		dtp = &omastertemplate;
1193cfe60390STomohiro Kusumi 	dirtemplate = *dtp;
1194cfe60390STomohiro Kusumi 	dirtemplate.dot_ino = htole32(ip->i_number);
1195cfe60390STomohiro Kusumi 	dirtemplate.dotdot_ino = htole32(dp->i_number);
1196cfe60390STomohiro Kusumi 	/*
1197cfe60390STomohiro Kusumi 	 * note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE so let's
1198cfe60390STomohiro Kusumi 	 * just redefine it - for this function only
1199cfe60390STomohiro Kusumi 	 */
1200cfe60390STomohiro Kusumi #undef  DIRBLKSIZ
1201cfe60390STomohiro Kusumi #define DIRBLKSIZ  VTOI(dvp)->i_e2fs->e2fs_bsize
1202cfe60390STomohiro Kusumi 	dirtemplate.dotdot_reclen = htole16(DIRBLKSIZ - 12);
1203cfe60390STomohiro Kusumi 	buf = malloc(DIRBLKSIZ, M_TEMP, M_WAITOK | M_ZERO);
1204cfe60390STomohiro Kusumi 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
1205cfe60390STomohiro Kusumi 		dirtemplate.dotdot_reclen =
1206cfe60390STomohiro Kusumi 		    htole16(le16toh(dirtemplate.dotdot_reclen) -
1207cfe60390STomohiro Kusumi 		    sizeof(struct ext2fs_direct_tail));
1208cfe60390STomohiro Kusumi 		ext2_init_dirent_tail(EXT2_DIRENT_TAIL(buf, DIRBLKSIZ));
1209cfe60390STomohiro Kusumi 	}
1210cfe60390STomohiro Kusumi 	memcpy(buf, &dirtemplate, sizeof(dirtemplate));
1211cfe60390STomohiro Kusumi 	ext2_dirent_csum_set(ip, (struct ext2fs_direct_2 *)buf);
1212cfe60390STomohiro Kusumi 	error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)buf,
1213cfe60390STomohiro Kusumi 	    DIRBLKSIZ, (off_t)0, UIO_SYSSPACE,
1214cfe60390STomohiro Kusumi 	    IO_NODELOCKED | IO_SYNC, cnp->cn_cred, NULL);
1215cfe60390STomohiro Kusumi 	if (error) {
1216cfe60390STomohiro Kusumi 		ext2_dec_nlink(dp);
1217cfe60390STomohiro Kusumi 		dp->i_flag |= IN_CHANGE;
1218cfe60390STomohiro Kusumi 		goto bad;
1219cfe60390STomohiro Kusumi 	}
1220cfe60390STomohiro Kusumi 	if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1221cfe60390STomohiro Kusumi 		/* XXX should grow with balloc() */
1222cfe60390STomohiro Kusumi 		panic("ext2_mkdir: blksize");
1223cfe60390STomohiro Kusumi 	else {
1224cfe60390STomohiro Kusumi 		ip->i_size = DIRBLKSIZ;
1225cfe60390STomohiro Kusumi 		ip->i_flag |= IN_CHANGE;
1226cfe60390STomohiro Kusumi 	}
1227cfe60390STomohiro Kusumi 
1228cfe60390STomohiro Kusumi 	/* Directory set up, now install its entry in the parent directory. */
1229cfe60390STomohiro Kusumi 	error = ext2_direnter(ip, dvp, cnp);
1230cfe60390STomohiro Kusumi 	if (error) {
1231cfe60390STomohiro Kusumi 		ext2_dec_nlink(dp);
1232cfe60390STomohiro Kusumi 		dp->i_flag |= IN_CHANGE;
1233cfe60390STomohiro Kusumi 	}
1234cfe60390STomohiro Kusumi bad:
1235cfe60390STomohiro Kusumi 	/*
1236cfe60390STomohiro Kusumi 	 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1237cfe60390STomohiro Kusumi 	 * for us because we set the link count to 0.
1238cfe60390STomohiro Kusumi 	 */
1239cfe60390STomohiro Kusumi 	if (error) {
1240cfe60390STomohiro Kusumi 		ip->i_nlink = 0;
1241cfe60390STomohiro Kusumi 		ip->i_flag |= IN_CHANGE;
1242cfe60390STomohiro Kusumi 		vput(tvp);
1243cfe60390STomohiro Kusumi 	} else
1244cfe60390STomohiro Kusumi 		*ap->a_vpp = tvp;
1245cfe60390STomohiro Kusumi out:
1246cfe60390STomohiro Kusumi 	free(buf, M_TEMP);
1247cfe60390STomohiro Kusumi 	return (error);
1248cfe60390STomohiro Kusumi #undef  DIRBLKSIZ
1249cfe60390STomohiro Kusumi #define DIRBLKSIZ  DEV_BSIZE
1250cfe60390STomohiro Kusumi }
1251cfe60390STomohiro Kusumi 
1252cfe60390STomohiro Kusumi /*
1253cfe60390STomohiro Kusumi  * Rmdir system call.
1254cfe60390STomohiro Kusumi  */
1255cfe60390STomohiro Kusumi static int
ext2_rmdir(struct vop_old_rmdir_args * ap)1256cfe60390STomohiro Kusumi ext2_rmdir(struct vop_old_rmdir_args *ap)
1257cfe60390STomohiro Kusumi {
1258cfe60390STomohiro Kusumi 	struct vnode *vp = ap->a_vp;
1259cfe60390STomohiro Kusumi 	struct vnode *dvp = ap->a_dvp;
1260cfe60390STomohiro Kusumi 	struct componentname *cnp = ap->a_cnp;
1261cfe60390STomohiro Kusumi 	struct inode *ip, *dp;
1262cfe60390STomohiro Kusumi 	int error;
1263cfe60390STomohiro Kusumi 
1264cfe60390STomohiro Kusumi 	ip = VTOI(vp);
1265cfe60390STomohiro Kusumi 	dp = VTOI(dvp);
1266cfe60390STomohiro Kusumi 
1267cfe60390STomohiro Kusumi 	/*
1268cfe60390STomohiro Kusumi 	 * Verify the directory is empty (and valid).
1269cfe60390STomohiro Kusumi 	 * (Rmdir ".." won't be valid since
1270cfe60390STomohiro Kusumi 	 *  ".." will contain a reference to
1271cfe60390STomohiro Kusumi 	 *  the current directory and thus be
1272cfe60390STomohiro Kusumi 	 *  non-empty.)
1273cfe60390STomohiro Kusumi 	 */
1274cfe60390STomohiro Kusumi 	if (!ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1275cfe60390STomohiro Kusumi 		error = ENOTEMPTY;
1276cfe60390STomohiro Kusumi 		goto out;
1277cfe60390STomohiro Kusumi 	}
1278cfe60390STomohiro Kusumi 	if ((dp->i_flags & APPEND)
1279cfe60390STomohiro Kusumi 	    || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1280cfe60390STomohiro Kusumi 		error = EPERM;
1281cfe60390STomohiro Kusumi 		goto out;
1282cfe60390STomohiro Kusumi 	}
1283cfe60390STomohiro Kusumi 	/*
1284cfe60390STomohiro Kusumi 	 * Delete reference to directory before purging
1285cfe60390STomohiro Kusumi 	 * inode.  If we crash in between, the directory
1286cfe60390STomohiro Kusumi 	 * will be reattached to lost+found,
1287cfe60390STomohiro Kusumi 	 */
1288cfe60390STomohiro Kusumi 	error = ext2_dirremove(dvp, cnp);
1289cfe60390STomohiro Kusumi 	if (error)
1290cfe60390STomohiro Kusumi 		goto out;
1291cfe60390STomohiro Kusumi 	ext2_dec_nlink(dp);
1292cfe60390STomohiro Kusumi 	dp->i_flag |= IN_CHANGE;
1293cfe60390STomohiro Kusumi 	vn_unlock(dvp);
1294cfe60390STomohiro Kusumi 	/*
1295cfe60390STomohiro Kusumi 	 * Truncate inode.  The only stuff left
1296cfe60390STomohiro Kusumi 	 * in the directory is "." and "..".
1297cfe60390STomohiro Kusumi 	 */
1298cfe60390STomohiro Kusumi 	ip->i_nlink = 0;
1299cfe60390STomohiro Kusumi 	error = ext2_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred);
1300cfe60390STomohiro Kusumi 	vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1301cfe60390STomohiro Kusumi out:
1302cfe60390STomohiro Kusumi 	return (error);
1303cfe60390STomohiro Kusumi }
1304cfe60390STomohiro Kusumi 
1305cfe60390STomohiro Kusumi /*
1306cfe60390STomohiro Kusumi  * symlink -- make a symbolic link
1307cfe60390STomohiro Kusumi  */
1308cfe60390STomohiro Kusumi static int
ext2_symlink(struct vop_old_symlink_args * ap)1309cfe60390STomohiro Kusumi ext2_symlink(struct vop_old_symlink_args *ap)
1310cfe60390STomohiro Kusumi {
1311cfe60390STomohiro Kusumi 	struct vnode *vp, **vpp = ap->a_vpp;
1312cfe60390STomohiro Kusumi 	struct inode *ip;
1313cfe60390STomohiro Kusumi 	int len, error;
1314cfe60390STomohiro Kusumi 
1315cfe60390STomohiro Kusumi 	error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1316cfe60390STomohiro Kusumi 	    vpp, ap->a_cnp);
1317cfe60390STomohiro Kusumi 	if (error)
1318cfe60390STomohiro Kusumi 		return (error);
1319cfe60390STomohiro Kusumi 	vp = *vpp;
1320cfe60390STomohiro Kusumi 	len = strlen(ap->a_target);
1321cfe60390STomohiro Kusumi 	if (len < vp->v_mount->mnt_maxsymlinklen) {
1322cfe60390STomohiro Kusumi 		ip = VTOI(vp);
1323cfe60390STomohiro Kusumi 		bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1324cfe60390STomohiro Kusumi 		ip->i_size = len;
1325cfe60390STomohiro Kusumi 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
1326cfe60390STomohiro Kusumi 	} else {
1327cfe60390STomohiro Kusumi 		/*
1328cfe60390STomohiro Kusumi 		 * Make sure we have a VM object in order to use
1329cfe60390STomohiro Kusumi 		 * the buffer cache.
1330cfe60390STomohiro Kusumi 		 */
1331cfe60390STomohiro Kusumi 		if (vp->v_object == NULL)
1332cfe60390STomohiro Kusumi 			vinitvmio(vp, 0, PAGE_SIZE, -1);
1333cfe60390STomohiro Kusumi 
1334cfe60390STomohiro Kusumi 		error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1335cfe60390STomohiro Kusumi 		    UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred, NULL);
1336cfe60390STomohiro Kusumi 	}
1337cfe60390STomohiro Kusumi 	if (error)
1338cfe60390STomohiro Kusumi 		vput(vp);
1339cfe60390STomohiro Kusumi 	return (error);
1340cfe60390STomohiro Kusumi }
1341cfe60390STomohiro Kusumi 
1342cfe60390STomohiro Kusumi /*
1343cfe60390STomohiro Kusumi  * Return target name of a symbolic link
1344cfe60390STomohiro Kusumi  */
1345cfe60390STomohiro Kusumi static int
ext2_readlink(struct vop_readlink_args * ap)1346cfe60390STomohiro Kusumi ext2_readlink(struct vop_readlink_args *ap)
1347cfe60390STomohiro Kusumi {
1348cfe60390STomohiro Kusumi 	struct vnode *vp = ap->a_vp;
1349cfe60390STomohiro Kusumi 	struct inode *ip = VTOI(vp);
1350cfe60390STomohiro Kusumi 	int isize;
1351cfe60390STomohiro Kusumi 
1352cfe60390STomohiro Kusumi 	isize = ip->i_size;
1353cfe60390STomohiro Kusumi 	if (isize < vp->v_mount->mnt_maxsymlinklen) {
1354cfe60390STomohiro Kusumi 		uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1355cfe60390STomohiro Kusumi 		return (0);
1356cfe60390STomohiro Kusumi 	}
1357cfe60390STomohiro Kusumi 	return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1358cfe60390STomohiro Kusumi }
1359cfe60390STomohiro Kusumi 
1360cfe60390STomohiro Kusumi /*
1361cfe60390STomohiro Kusumi  * Calculate the logical to physical mapping if not done already,
1362cfe60390STomohiro Kusumi  * then call the device strategy routine.
1363cfe60390STomohiro Kusumi  *
1364cfe60390STomohiro Kusumi  * In order to be able to swap to a file, the ext2_bmaparray() operation may not
1365cfe60390STomohiro Kusumi  * deadlock on memory.  See ext2_bmap() for details.
1366cfe60390STomohiro Kusumi  */
1367cfe60390STomohiro Kusumi static int
ext2_strategy(struct vop_strategy_args * ap)1368cfe60390STomohiro Kusumi ext2_strategy(struct vop_strategy_args *ap)
1369cfe60390STomohiro Kusumi {
1370cfe60390STomohiro Kusumi 	struct bio *bio = ap->a_bio;
1371cfe60390STomohiro Kusumi 	struct bio *nbio;
1372cfe60390STomohiro Kusumi 	struct buf *bp = bio->bio_buf;
1373cfe60390STomohiro Kusumi 	struct vnode *vp = ap->a_vp;
1374cfe60390STomohiro Kusumi 	struct inode *ip;
1375cfe60390STomohiro Kusumi 	int error;
1376cfe60390STomohiro Kusumi 
1377cfe60390STomohiro Kusumi 	ip = VTOI(vp);
1378cfe60390STomohiro Kusumi 	if (vp->v_type == VBLK || vp->v_type == VCHR)
1379cfe60390STomohiro Kusumi 		panic("ext2_strategy: spec");
1380cfe60390STomohiro Kusumi 	nbio = push_bio(bio);
1381cfe60390STomohiro Kusumi 	if (nbio->bio_offset == NOOFFSET) {
1382cfe60390STomohiro Kusumi 		error = VOP_BMAP(vp, bio->bio_offset, &nbio->bio_offset, NULL,
1383cfe60390STomohiro Kusumi 		    NULL, bp->b_cmd);
1384cfe60390STomohiro Kusumi 		if (error) {
1385cfe60390STomohiro Kusumi 			bp->b_error = error;
1386cfe60390STomohiro Kusumi 			bp->b_flags |= B_ERROR;
1387cfe60390STomohiro Kusumi 			/* I/O was never started on nbio, must biodone(bio) */
1388cfe60390STomohiro Kusumi 			biodone(bio);
1389cfe60390STomohiro Kusumi 			return (error);
1390cfe60390STomohiro Kusumi 		}
1391cfe60390STomohiro Kusumi 		if (nbio->bio_offset == NOOFFSET)
1392cfe60390STomohiro Kusumi 			vfs_bio_clrbuf(bp);
1393cfe60390STomohiro Kusumi 	}
1394cfe60390STomohiro Kusumi 	if (nbio->bio_offset == NOOFFSET) {
1395cfe60390STomohiro Kusumi 		/* I/O was never started on nbio, must biodone(bio) */
1396cfe60390STomohiro Kusumi 		biodone(bio);
1397cfe60390STomohiro Kusumi 		return (0);
1398cfe60390STomohiro Kusumi 	}
1399cfe60390STomohiro Kusumi 	vn_strategy(ip->i_devvp, nbio);
1400cfe60390STomohiro Kusumi 	return (0);
1401cfe60390STomohiro Kusumi }
1402cfe60390STomohiro Kusumi 
1403cfe60390STomohiro Kusumi /*
1404cfe60390STomohiro Kusumi  * Print out the contents of an inode.
1405cfe60390STomohiro Kusumi  */
1406cfe60390STomohiro Kusumi static int
ext2_print(struct vop_print_args * ap)1407cfe60390STomohiro Kusumi ext2_print(struct vop_print_args *ap)
1408cfe60390STomohiro Kusumi {
1409cfe60390STomohiro Kusumi 	struct vnode *vp = ap->a_vp;
1410cfe60390STomohiro Kusumi 	struct inode *ip = VTOI(vp);
1411cfe60390STomohiro Kusumi 
1412cfe60390STomohiro Kusumi 	printf("tag VT_EXT2FS, ino %lu, on dev %s (%d, %d)",
1413cfe60390STomohiro Kusumi 	    (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev),
1414cfe60390STomohiro Kusumi 	    minor(ip->i_dev));
1415cfe60390STomohiro Kusumi 	if (vp->v_type == VFIFO)
1416cfe60390STomohiro Kusumi 		fifo_printinfo(vp);
1417cfe60390STomohiro Kusumi 	lockmgr_printinfo(&vp->v_lock);
1418cfe60390STomohiro Kusumi 	printf("\n");
1419cfe60390STomohiro Kusumi 	return (0);
1420cfe60390STomohiro Kusumi }
1421cfe60390STomohiro Kusumi 
1422cfe60390STomohiro Kusumi /*
1423cfe60390STomohiro Kusumi  * Read wrapper for fifos.
1424cfe60390STomohiro Kusumi  */
1425cfe60390STomohiro Kusumi static
1426cfe60390STomohiro Kusumi int
ext2fifo_read(struct vop_read_args * ap)1427cfe60390STomohiro Kusumi ext2fifo_read(struct vop_read_args *ap)
1428cfe60390STomohiro Kusumi {
1429cfe60390STomohiro Kusumi 	int error, resid;
1430cfe60390STomohiro Kusumi 	struct inode *ip;
1431cfe60390STomohiro Kusumi 	struct uio *uio;
1432cfe60390STomohiro Kusumi 
1433cfe60390STomohiro Kusumi 	uio = ap->a_uio;
1434cfe60390STomohiro Kusumi 	resid = uio->uio_resid;
1435cfe60390STomohiro Kusumi 	error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1436cfe60390STomohiro Kusumi 	ip = VTOI(ap->a_vp);
1437cfe60390STomohiro Kusumi 	if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL &&
1438cfe60390STomohiro Kusumi 	    (uio->uio_resid != resid || (error == 0 && resid != 0)))
1439cfe60390STomohiro Kusumi 		VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1440cfe60390STomohiro Kusumi 	return (error);
1441cfe60390STomohiro Kusumi }
1442cfe60390STomohiro Kusumi 
1443cfe60390STomohiro Kusumi /*
1444cfe60390STomohiro Kusumi  * Write wrapper for fifos.
1445cfe60390STomohiro Kusumi  */
1446cfe60390STomohiro Kusumi static
1447cfe60390STomohiro Kusumi int
ext2fifo_write(struct vop_write_args * ap)1448cfe60390STomohiro Kusumi ext2fifo_write(struct vop_write_args *ap)
1449cfe60390STomohiro Kusumi {
1450cfe60390STomohiro Kusumi 	int error, resid;
1451cfe60390STomohiro Kusumi 	struct inode *ip;
1452cfe60390STomohiro Kusumi 	struct uio *uio;
1453cfe60390STomohiro Kusumi 
1454cfe60390STomohiro Kusumi 	uio = ap->a_uio;
1455cfe60390STomohiro Kusumi 	resid = uio->uio_resid;
1456cfe60390STomohiro Kusumi 	error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1457cfe60390STomohiro Kusumi 	ip = VTOI(ap->a_vp);
1458cfe60390STomohiro Kusumi 	if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1459cfe60390STomohiro Kusumi 		VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1460cfe60390STomohiro Kusumi 	return (error);
1461cfe60390STomohiro Kusumi }
1462cfe60390STomohiro Kusumi 
1463cfe60390STomohiro Kusumi /*
1464cfe60390STomohiro Kusumi  * Close wrapper for fifos.
1465cfe60390STomohiro Kusumi  *
1466cfe60390STomohiro Kusumi  * Update the times on the inode then do device close.
1467cfe60390STomohiro Kusumi  */
1468cfe60390STomohiro Kusumi static int
ext2fifo_close(struct vop_close_args * ap)1469cfe60390STomohiro Kusumi ext2fifo_close(struct vop_close_args *ap)
1470cfe60390STomohiro Kusumi {
1471cfe60390STomohiro Kusumi 	struct vnode *vp = ap->a_vp;
1472cfe60390STomohiro Kusumi 
1473cfe60390STomohiro Kusumi 	if (VREFCNT(vp) > 1)
1474cfe60390STomohiro Kusumi 		ext2_itimes(vp);
1475cfe60390STomohiro Kusumi 	return (VOCALL(&fifo_vnode_vops, &ap->a_head));
1476cfe60390STomohiro Kusumi }
1477cfe60390STomohiro Kusumi 
1478cfe60390STomohiro Kusumi static void
filt_ext2detach(struct knote * kn)1479cfe60390STomohiro Kusumi filt_ext2detach(struct knote *kn)
1480cfe60390STomohiro Kusumi {
1481cfe60390STomohiro Kusumi 	struct vnode *vp = (struct vnode *)kn->kn_hook;
1482cfe60390STomohiro Kusumi 
1483cfe60390STomohiro Kusumi 	lwkt_gettoken(&vp->v_token);
1484cfe60390STomohiro Kusumi 	knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1485cfe60390STomohiro Kusumi 	lwkt_reltoken(&vp->v_token);
1486cfe60390STomohiro Kusumi }
1487cfe60390STomohiro Kusumi 
1488cfe60390STomohiro Kusumi /*ARGSUSED*/
1489cfe60390STomohiro Kusumi static int
filt_ext2read(struct knote * kn,long hint)1490cfe60390STomohiro Kusumi filt_ext2read(struct knote *kn, long hint)
1491cfe60390STomohiro Kusumi {
1492cfe60390STomohiro Kusumi 	struct vnode *vp = (struct vnode *)kn->kn_hook;
1493cfe60390STomohiro Kusumi 	struct inode *ip = VTOI(vp);
1494cfe60390STomohiro Kusumi 	off_t off;
1495cfe60390STomohiro Kusumi 
1496cfe60390STomohiro Kusumi 	/*
1497cfe60390STomohiro Kusumi 	 * filesystem is gone, so set the EOF flag and schedule
1498cfe60390STomohiro Kusumi 	 * the knote for deletion.
1499cfe60390STomohiro Kusumi 	 */
1500cfe60390STomohiro Kusumi 	if (hint == NOTE_REVOKE) {
1501cfe60390STomohiro Kusumi 		kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
1502cfe60390STomohiro Kusumi 		return (1);
1503cfe60390STomohiro Kusumi 	}
1504cfe60390STomohiro Kusumi         off = ip->i_size - kn->kn_fp->f_offset;
1505cfe60390STomohiro Kusumi 	kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
1506cfe60390STomohiro Kusumi 	if (kn->kn_sfflags & NOTE_OLDAPI)
1507cfe60390STomohiro Kusumi 		return (1);
1508cfe60390STomohiro Kusumi         return (kn->kn_data != 0);
1509cfe60390STomohiro Kusumi }
1510cfe60390STomohiro Kusumi 
1511cfe60390STomohiro Kusumi /*ARGSUSED*/
1512cfe60390STomohiro Kusumi static int
filt_ext2write(struct knote * kn,long hint)1513cfe60390STomohiro Kusumi filt_ext2write(struct knote *kn, long hint)
1514cfe60390STomohiro Kusumi {
1515cfe60390STomohiro Kusumi 	/*
1516cfe60390STomohiro Kusumi 	 * filesystem is gone, so set the EOF flag and schedule
1517cfe60390STomohiro Kusumi 	 * the knote for deletion.
1518cfe60390STomohiro Kusumi 	 */
1519cfe60390STomohiro Kusumi 	if (hint == NOTE_REVOKE)
1520cfe60390STomohiro Kusumi 		kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
1521cfe60390STomohiro Kusumi 
1522cfe60390STomohiro Kusumi         kn->kn_data = 0;
1523cfe60390STomohiro Kusumi         return (1);
1524cfe60390STomohiro Kusumi }
1525cfe60390STomohiro Kusumi 
1526cfe60390STomohiro Kusumi static int
filt_ext2vnode(struct knote * kn,long hint)1527cfe60390STomohiro Kusumi filt_ext2vnode(struct knote *kn, long hint)
1528cfe60390STomohiro Kusumi {
1529cfe60390STomohiro Kusumi 	if (kn->kn_sfflags & hint)
1530cfe60390STomohiro Kusumi 		kn->kn_fflags |= hint;
1531cfe60390STomohiro Kusumi 	if (hint == NOTE_REVOKE) {
1532cfe60390STomohiro Kusumi 		kn->kn_flags |= (EV_EOF | EV_NODATA);
1533cfe60390STomohiro Kusumi 		return (1);
1534cfe60390STomohiro Kusumi 	}
1535cfe60390STomohiro Kusumi 	return (kn->kn_fflags != 0);
1536cfe60390STomohiro Kusumi }
1537cfe60390STomohiro Kusumi 
1538cfe60390STomohiro Kusumi static struct filterops ext2read_filtops =
15394a24abd0STomohiro Kusumi 	{ FILTEROP_ISFD | FILTEROP_MPSAFE, NULL, filt_ext2detach, filt_ext2read };
1540cfe60390STomohiro Kusumi static struct filterops ext2write_filtops =
15414a24abd0STomohiro Kusumi 	{ FILTEROP_ISFD | FILTEROP_MPSAFE, NULL, filt_ext2detach, filt_ext2write };
1542cfe60390STomohiro Kusumi static struct filterops ext2vnode_filtops =
15434a24abd0STomohiro Kusumi 	{ FILTEROP_ISFD | FILTEROP_MPSAFE, NULL, filt_ext2detach, filt_ext2vnode };
1544cfe60390STomohiro Kusumi 
1545cfe60390STomohiro Kusumi static int
ext2_kqfilter(struct vop_kqfilter_args * ap)1546cfe60390STomohiro Kusumi ext2_kqfilter(struct vop_kqfilter_args *ap)
1547cfe60390STomohiro Kusumi {
1548cfe60390STomohiro Kusumi 	struct vnode *vp = ap->a_vp;
1549cfe60390STomohiro Kusumi 	struct knote *kn = ap->a_kn;
1550cfe60390STomohiro Kusumi 
1551cfe60390STomohiro Kusumi 	switch (kn->kn_filter) {
1552cfe60390STomohiro Kusumi 	case EVFILT_READ:
1553cfe60390STomohiro Kusumi 		kn->kn_fop = &ext2read_filtops;
1554cfe60390STomohiro Kusumi 		break;
1555cfe60390STomohiro Kusumi 	case EVFILT_WRITE:
1556cfe60390STomohiro Kusumi 		kn->kn_fop = &ext2write_filtops;
1557cfe60390STomohiro Kusumi 		break;
1558cfe60390STomohiro Kusumi 	case EVFILT_VNODE:
1559cfe60390STomohiro Kusumi 		kn->kn_fop = &ext2vnode_filtops;
1560cfe60390STomohiro Kusumi 		break;
1561cfe60390STomohiro Kusumi 	default:
1562cfe60390STomohiro Kusumi 		return (EOPNOTSUPP);
1563cfe60390STomohiro Kusumi 	}
1564cfe60390STomohiro Kusumi 
1565cfe60390STomohiro Kusumi 	kn->kn_hook = (caddr_t)vp;
1566cfe60390STomohiro Kusumi 
1567cfe60390STomohiro Kusumi 	/* XXX: kq token actually protects the list */
1568cfe60390STomohiro Kusumi 	lwkt_gettoken(&vp->v_token);
1569cfe60390STomohiro Kusumi 	knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1570cfe60390STomohiro Kusumi 	lwkt_reltoken(&vp->v_token);
1571cfe60390STomohiro Kusumi 
1572cfe60390STomohiro Kusumi 	return (0);
1573cfe60390STomohiro Kusumi }
1574cfe60390STomohiro Kusumi 
1575cfe60390STomohiro Kusumi /*
1576cfe60390STomohiro Kusumi  * Kqfilter wrapper for fifos.
1577cfe60390STomohiro Kusumi  *
1578cfe60390STomohiro Kusumi  * Fall through to ext2 kqfilter routines if needed
1579cfe60390STomohiro Kusumi  */
1580cfe60390STomohiro Kusumi static int
ext2fifo_kqfilter(struct vop_kqfilter_args * ap)1581cfe60390STomohiro Kusumi ext2fifo_kqfilter(struct vop_kqfilter_args *ap)
1582cfe60390STomohiro Kusumi {
1583cfe60390STomohiro Kusumi 	int error;
1584cfe60390STomohiro Kusumi 
1585cfe60390STomohiro Kusumi 	error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1586cfe60390STomohiro Kusumi 	if (error)
1587cfe60390STomohiro Kusumi 		error = ext2_kqfilter(ap);
1588cfe60390STomohiro Kusumi 	return (error);
1589cfe60390STomohiro Kusumi }
1590cfe60390STomohiro Kusumi 
1591cfe60390STomohiro Kusumi /*
1592cfe60390STomohiro Kusumi  * Return POSIX pathconf information applicable to ext2 filesystems.
1593cfe60390STomohiro Kusumi  */
1594cfe60390STomohiro Kusumi static int
ext2_pathconf(struct vop_pathconf_args * ap)1595cfe60390STomohiro Kusumi ext2_pathconf(struct vop_pathconf_args *ap)
1596cfe60390STomohiro Kusumi {
1597cfe60390STomohiro Kusumi 	int error = 0;
1598cfe60390STomohiro Kusumi 
1599cfe60390STomohiro Kusumi 	switch (ap->a_name) {
1600cfe60390STomohiro Kusumi 	case _PC_LINK_MAX:
1601cfe60390STomohiro Kusumi 		if (EXT2_HAS_RO_COMPAT_FEATURE(VTOI(ap->a_vp)->i_e2fs,
1602cfe60390STomohiro Kusumi 		    EXT2F_ROCOMPAT_DIR_NLINK))
1603cfe60390STomohiro Kusumi 			*ap->a_retval = INT_MAX;
1604cfe60390STomohiro Kusumi 		else
1605cfe60390STomohiro Kusumi 			*ap->a_retval = EXT4_LINK_MAX;
1606cfe60390STomohiro Kusumi 		break;
1607cfe60390STomohiro Kusumi 	case _PC_NAME_MAX:
1608cfe60390STomohiro Kusumi 		*ap->a_retval = NAME_MAX;
1609cfe60390STomohiro Kusumi 		break;
1610cfe60390STomohiro Kusumi 	case _PC_PATH_MAX:
1611cfe60390STomohiro Kusumi 		*ap->a_retval = PATH_MAX;
1612cfe60390STomohiro Kusumi 		break;
1613cfe60390STomohiro Kusumi 	case _PC_PIPE_BUF:
1614cfe60390STomohiro Kusumi 		if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
1615cfe60390STomohiro Kusumi 			*ap->a_retval = PIPE_BUF;
1616cfe60390STomohiro Kusumi 		else
1617cfe60390STomohiro Kusumi 			error = EINVAL;
1618cfe60390STomohiro Kusumi 		break;
1619cfe60390STomohiro Kusumi 	case _PC_CHOWN_RESTRICTED:
1620cfe60390STomohiro Kusumi 		*ap->a_retval = 1;
1621cfe60390STomohiro Kusumi 		break;
1622cfe60390STomohiro Kusumi 	case _PC_NO_TRUNC:
1623cfe60390STomohiro Kusumi 		*ap->a_retval = 1;
1624cfe60390STomohiro Kusumi 		break;
1625cfe60390STomohiro Kusumi 	case _PC_MIN_HOLE_SIZE:
1626cfe60390STomohiro Kusumi 		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1627cfe60390STomohiro Kusumi 		break;
1628cfe60390STomohiro Kusumi 	case _PC_PRIO_IO:
1629cfe60390STomohiro Kusumi 		*ap->a_retval = 0;
1630cfe60390STomohiro Kusumi 		break;
1631cfe60390STomohiro Kusumi 	case _PC_SYNC_IO:
1632cfe60390STomohiro Kusumi 		*ap->a_retval = 0;
1633cfe60390STomohiro Kusumi 		break;
1634cfe60390STomohiro Kusumi 	case _PC_ALLOC_SIZE_MIN:
1635cfe60390STomohiro Kusumi 		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
1636cfe60390STomohiro Kusumi 		break;
1637cfe60390STomohiro Kusumi 	case _PC_FILESIZEBITS:
1638cfe60390STomohiro Kusumi 		*ap->a_retval = 64;
1639cfe60390STomohiro Kusumi 		break;
1640cfe60390STomohiro Kusumi 	case _PC_REC_INCR_XFER_SIZE:
1641cfe60390STomohiro Kusumi 		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1642cfe60390STomohiro Kusumi 		break;
1643cfe60390STomohiro Kusumi 	case _PC_REC_MAX_XFER_SIZE:
1644cfe60390STomohiro Kusumi 		*ap->a_retval = -1;	/* means ``unlimited'' */
1645cfe60390STomohiro Kusumi 		break;
1646cfe60390STomohiro Kusumi 	case _PC_REC_MIN_XFER_SIZE:
1647cfe60390STomohiro Kusumi 		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1648cfe60390STomohiro Kusumi 		break;
1649cfe60390STomohiro Kusumi 	case _PC_REC_XFER_ALIGN:
1650cfe60390STomohiro Kusumi 		*ap->a_retval = PAGE_SIZE;
1651cfe60390STomohiro Kusumi 		break;
1652cfe60390STomohiro Kusumi 	case _PC_SYMLINK_MAX:
1653cfe60390STomohiro Kusumi 		*ap->a_retval = MAXPATHLEN;
1654cfe60390STomohiro Kusumi 		break;
1655cfe60390STomohiro Kusumi 
1656cfe60390STomohiro Kusumi 	default:
1657cfe60390STomohiro Kusumi 		error = vop_stdpathconf(ap);
1658cfe60390STomohiro Kusumi 		break;
1659cfe60390STomohiro Kusumi 	}
1660cfe60390STomohiro Kusumi 	return (error);
1661cfe60390STomohiro Kusumi }
1662cfe60390STomohiro Kusumi 
1663cfe60390STomohiro Kusumi /*
1664cfe60390STomohiro Kusumi  * Initialize the vnode associated with a new inode, handle aliased vnodes.
1665cfe60390STomohiro Kusumi  */
1666cfe60390STomohiro Kusumi int
ext2_vinit(struct mount * mntp,struct vnode ** vpp)1667cfe60390STomohiro Kusumi ext2_vinit(struct mount *mntp, struct vnode **vpp)
1668cfe60390STomohiro Kusumi {
1669cfe60390STomohiro Kusumi 	struct inode *ip;
1670cfe60390STomohiro Kusumi 	struct vnode *vp;
1671cfe60390STomohiro Kusumi 
1672cfe60390STomohiro Kusumi 	vp = *vpp;
1673cfe60390STomohiro Kusumi 	ip = VTOI(vp);
1674cfe60390STomohiro Kusumi 
1675cfe60390STomohiro Kusumi 	switch (vp->v_type = IFTOVT(ip->i_mode)) {
1676cfe60390STomohiro Kusumi 	case VCHR:
1677cfe60390STomohiro Kusumi 	case VBLK:
1678cfe60390STomohiro Kusumi 		vp->v_ops = &mntp->mnt_vn_spec_ops;
1679cfe60390STomohiro Kusumi 		addaliasu(vp, umajor(ip->i_rdev), uminor(ip->i_rdev));
1680cfe60390STomohiro Kusumi 		break;
1681cfe60390STomohiro Kusumi 	case VFIFO:
1682cfe60390STomohiro Kusumi 		vp->v_ops = &mntp->mnt_vn_fifo_ops;
1683cfe60390STomohiro Kusumi 		break;
1684cfe60390STomohiro Kusumi 	case VDIR:
1685cfe60390STomohiro Kusumi 	case VREG:
1686cfe60390STomohiro Kusumi 		vinitvmio(vp, ip->i_size, PAGE_SIZE, -1); /* XXX */
1687cfe60390STomohiro Kusumi 		break;
1688cfe60390STomohiro Kusumi 	case VLNK:
1689cfe60390STomohiro Kusumi 		if ((ip->i_size >= vp->v_mount->mnt_maxsymlinklen) &&
1690cfe60390STomohiro Kusumi 		    ip->i_blocks != 0) {
1691cfe60390STomohiro Kusumi 			vinitvmio(vp, ip->i_size, PAGE_SIZE, -1);
1692cfe60390STomohiro Kusumi 		}
1693cfe60390STomohiro Kusumi 		break;
1694cfe60390STomohiro Kusumi 	default:
1695cfe60390STomohiro Kusumi 		break;
1696cfe60390STomohiro Kusumi 	}
1697cfe60390STomohiro Kusumi 
1698cfe60390STomohiro Kusumi 	/*
1699cfe60390STomohiro Kusumi 	 * Only unallocated inodes should be of type VNON.
1700cfe60390STomohiro Kusumi 	 */
1701cfe60390STomohiro Kusumi 	if (ip->i_mode != 0 && vp->v_type == VNON)
1702cfe60390STomohiro Kusumi 		return (EINVAL);
1703cfe60390STomohiro Kusumi 
1704cfe60390STomohiro Kusumi 	if (ip->i_number == EXT2_ROOTINO)
1705cfe60390STomohiro Kusumi 		vp->v_flag |= VROOT;
1706cfe60390STomohiro Kusumi 	/*
1707cfe60390STomohiro Kusumi 	 * Initialize modrev times.
1708cfe60390STomohiro Kusumi 	 */
1709cfe60390STomohiro Kusumi 	ip->i_modrev = init_va_filerev();
1710cfe60390STomohiro Kusumi 	*vpp = vp;
1711cfe60390STomohiro Kusumi 	return (0);
1712cfe60390STomohiro Kusumi }
1713cfe60390STomohiro Kusumi 
1714cfe60390STomohiro Kusumi /*
1715cfe60390STomohiro Kusumi  * Allocate a new inode.
1716cfe60390STomohiro Kusumi  */
1717cfe60390STomohiro Kusumi static int
ext2_makeinode(int mode,struct vnode * dvp,struct vnode ** vpp,struct componentname * cnp)1718cfe60390STomohiro Kusumi ext2_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
1719cfe60390STomohiro Kusumi     struct componentname *cnp)
1720cfe60390STomohiro Kusumi {
1721cfe60390STomohiro Kusumi 	struct inode *ip, *pdir;
1722cfe60390STomohiro Kusumi 	struct vnode *tvp;
1723cfe60390STomohiro Kusumi 	int error;
1724cfe60390STomohiro Kusumi 
1725cfe60390STomohiro Kusumi 	pdir = VTOI(dvp);
1726cfe60390STomohiro Kusumi 	*vpp = NULL;
1727cfe60390STomohiro Kusumi 	if ((mode & IFMT) == 0)
1728cfe60390STomohiro Kusumi 		mode |= IFREG;
1729cfe60390STomohiro Kusumi 
1730cfe60390STomohiro Kusumi 	error = ext2_valloc(dvp, mode, cnp->cn_cred, &tvp);
1731cfe60390STomohiro Kusumi 	if (error) {
1732cfe60390STomohiro Kusumi 		return (error);
1733cfe60390STomohiro Kusumi 	}
1734cfe60390STomohiro Kusumi 	ip = VTOI(tvp);
1735cfe60390STomohiro Kusumi 	ip->i_gid = pdir->i_gid;
1736cfe60390STomohiro Kusumi #ifdef SUIDDIR
1737cfe60390STomohiro Kusumi 	{
1738cfe60390STomohiro Kusumi 		/*
1739cfe60390STomohiro Kusumi 		 * if we are
1740cfe60390STomohiro Kusumi 		 * not the owner of the directory,
1741cfe60390STomohiro Kusumi 		 * and we are hacking owners here, (only do this where told to)
1742cfe60390STomohiro Kusumi 		 * and we are not giving it TOO root, (would subvert quotas)
1743cfe60390STomohiro Kusumi 		 * then go ahead and give it to the other user.
1744cfe60390STomohiro Kusumi 		 * Note that this drops off the execute bits for security.
1745cfe60390STomohiro Kusumi 		 */
1746cfe60390STomohiro Kusumi 		if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1747cfe60390STomohiro Kusumi 		    (pdir->i_mode & ISUID) &&
1748cfe60390STomohiro Kusumi 		    (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1749cfe60390STomohiro Kusumi 			ip->i_uid = pdir->i_uid;
1750cfe60390STomohiro Kusumi 			mode &= ~07111;
1751cfe60390STomohiro Kusumi 		} else {
1752cfe60390STomohiro Kusumi 			ip->i_uid = cnp->cn_cred->cr_uid;
1753cfe60390STomohiro Kusumi 		}
1754cfe60390STomohiro Kusumi 	}
1755cfe60390STomohiro Kusumi #else
1756cfe60390STomohiro Kusumi 	ip->i_uid = cnp->cn_cred->cr_uid;
1757cfe60390STomohiro Kusumi #endif
1758cfe60390STomohiro Kusumi 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1759cfe60390STomohiro Kusumi 	ip->i_mode = mode;
1760cfe60390STomohiro Kusumi 	tvp->v_type = IFTOVT(mode);	/* Rest init'd in getnewvnode(). */
1761cfe60390STomohiro Kusumi 	ip->i_nlink = 1;
1762cfe60390STomohiro Kusumi 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred)) {
1763*2b3f93eaSMatthew Dillon 		if (caps_priv_check(cnp->cn_cred, SYSCAP_NOVFS_RETAINSUGID))
1764cfe60390STomohiro Kusumi 			ip->i_mode &= ~ISGID;
1765cfe60390STomohiro Kusumi 	}
1766cfe60390STomohiro Kusumi 
1767cfe60390STomohiro Kusumi 	if (cnp->cn_flags & CNP_ISWHITEOUT)
1768cfe60390STomohiro Kusumi 		ip->i_flags |= UF_OPAQUE;
1769cfe60390STomohiro Kusumi 
1770cfe60390STomohiro Kusumi 	/*
1771cfe60390STomohiro Kusumi 	 * Regular files and directories need VM objects.  Softlinks do
1772cfe60390STomohiro Kusumi 	 * not (not immediately anyway).
1773cfe60390STomohiro Kusumi 	 */
1774cfe60390STomohiro Kusumi 	if (tvp->v_type == VREG || tvp->v_type == VDIR)
1775cfe60390STomohiro Kusumi 		vinitvmio(tvp, 0, PAGE_SIZE, -1);
1776cfe60390STomohiro Kusumi 
1777cfe60390STomohiro Kusumi 	/*
1778cfe60390STomohiro Kusumi 	 * Make sure inode goes to disk before directory entry.
1779cfe60390STomohiro Kusumi 	 */
1780cfe60390STomohiro Kusumi 	error = ext2_update(tvp, !DOINGASYNC(tvp));
1781cfe60390STomohiro Kusumi 	if (error)
1782cfe60390STomohiro Kusumi 		goto bad;
1783cfe60390STomohiro Kusumi 
1784cfe60390STomohiro Kusumi 	error = ext2_direnter(ip, dvp, cnp);
1785cfe60390STomohiro Kusumi 	if (error)
1786cfe60390STomohiro Kusumi 		goto bad;
1787cfe60390STomohiro Kusumi 
1788cfe60390STomohiro Kusumi 	*vpp = tvp;
1789cfe60390STomohiro Kusumi 	return (0);
1790cfe60390STomohiro Kusumi 
1791cfe60390STomohiro Kusumi bad:
1792cfe60390STomohiro Kusumi 	/*
1793cfe60390STomohiro Kusumi 	 * Write error occurred trying to update the inode
1794cfe60390STomohiro Kusumi 	 * or the directory so must deallocate the inode.
1795cfe60390STomohiro Kusumi 	 */
1796cfe60390STomohiro Kusumi 	ip->i_nlink = 0;
1797cfe60390STomohiro Kusumi 	ip->i_flag |= IN_CHANGE;
1798cfe60390STomohiro Kusumi 	vput(tvp);
1799cfe60390STomohiro Kusumi 	return (error);
1800cfe60390STomohiro Kusumi }
1801cfe60390STomohiro Kusumi 
1802cfe60390STomohiro Kusumi /*
1803cfe60390STomohiro Kusumi  * Vnode op for reading.
1804cfe60390STomohiro Kusumi  */
1805cfe60390STomohiro Kusumi static int
ext2_read(struct vop_read_args * ap)1806cfe60390STomohiro Kusumi ext2_read(struct vop_read_args *ap)
1807cfe60390STomohiro Kusumi {
1808cfe60390STomohiro Kusumi 	struct vnode *vp;
1809cfe60390STomohiro Kusumi 	struct inode *ip;
1810cfe60390STomohiro Kusumi 	struct uio *uio;
1811cfe60390STomohiro Kusumi 	struct m_ext2fs *fs;
1812cfe60390STomohiro Kusumi 	struct buf *bp;
1813cfe60390STomohiro Kusumi 	daddr_t lbn;
1814cfe60390STomohiro Kusumi 	off_t nextlbn;
1815cfe60390STomohiro Kusumi 	off_t nextloffset;
1816cfe60390STomohiro Kusumi 	off_t bytesinfile;
1817cfe60390STomohiro Kusumi 	long size, xfersize, blkoffset;
1818cfe60390STomohiro Kusumi 	int error, orig_resid, seqcount;
1819cfe60390STomohiro Kusumi 	int ioflag;
1820cfe60390STomohiro Kusumi 
1821cfe60390STomohiro Kusumi 	vp = ap->a_vp;
1822cfe60390STomohiro Kusumi 	uio = ap->a_uio;
1823cfe60390STomohiro Kusumi 	ioflag = ap->a_ioflag;
1824cfe60390STomohiro Kusumi 
1825cfe60390STomohiro Kusumi 	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
1826cfe60390STomohiro Kusumi 	ip = VTOI(vp);
1827cfe60390STomohiro Kusumi 
1828cfe60390STomohiro Kusumi #ifdef INVARIANTS
1829cfe60390STomohiro Kusumi 	if (uio->uio_rw != UIO_READ)
1830cfe60390STomohiro Kusumi 		panic("%s: mode", "ext2_read");
1831cfe60390STomohiro Kusumi 
1832cfe60390STomohiro Kusumi 	if (vp->v_type == VLNK) {
1833cfe60390STomohiro Kusumi 		if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
1834cfe60390STomohiro Kusumi 			panic("%s: short symlink", "ext2_read");
1835cfe60390STomohiro Kusumi 	} else if (vp->v_type != VREG && vp->v_type != VDIR)
1836cfe60390STomohiro Kusumi 		panic("%s: type %d", "ext2_read", vp->v_type);
1837cfe60390STomohiro Kusumi #endif
1838cfe60390STomohiro Kusumi 	orig_resid = uio->uio_resid;
1839cfe60390STomohiro Kusumi 	KASSERT(orig_resid >= 0, ("ext2_read: uio->uio_resid < 0"));
1840cfe60390STomohiro Kusumi 	if (orig_resid == 0)
1841cfe60390STomohiro Kusumi 		return (0);
1842cfe60390STomohiro Kusumi 	KASSERT(uio->uio_offset >= 0, ("ext2_read: uio->uio_offset < 0"));
1843cfe60390STomohiro Kusumi 	fs = ip->i_e2fs;
1844cfe60390STomohiro Kusumi 	if (uio->uio_offset < ip->i_size &&
1845cfe60390STomohiro Kusumi 	    uio->uio_offset >= fs->e2fs_maxfilesize)
1846cfe60390STomohiro Kusumi 		return (EOVERFLOW);
1847cfe60390STomohiro Kusumi 
1848cfe60390STomohiro Kusumi 	for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
1849cfe60390STomohiro Kusumi 		if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
1850cfe60390STomohiro Kusumi 			break;
1851cfe60390STomohiro Kusumi 		lbn = lblkno(fs, uio->uio_offset);
1852cfe60390STomohiro Kusumi 		nextlbn = lbn + 1;
1853cfe60390STomohiro Kusumi 		nextloffset = lblktodoff(fs, nextlbn);
1854cfe60390STomohiro Kusumi 		size = blksize(fs, ip, lbn);
1855cfe60390STomohiro Kusumi 		blkoffset = blkoff(fs, uio->uio_offset);
1856cfe60390STomohiro Kusumi 
1857cfe60390STomohiro Kusumi 		xfersize = fs->e2fs_fsize - blkoffset;
1858cfe60390STomohiro Kusumi 		if (uio->uio_resid < xfersize)
1859cfe60390STomohiro Kusumi 			xfersize = uio->uio_resid;
1860cfe60390STomohiro Kusumi 		if (bytesinfile < xfersize)
1861cfe60390STomohiro Kusumi 			xfersize = bytesinfile;
1862cfe60390STomohiro Kusumi 
1863cfe60390STomohiro Kusumi 		if (nextloffset >= ip->i_size)
1864e5b38eb5STomohiro Kusumi 			error = bread(vp, lblktodoff(fs, lbn), size, &bp);
1865cfe60390STomohiro Kusumi 		else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
1866e5b38eb5STomohiro Kusumi 			error = cluster_read(vp, (off_t)ip->i_size,
1867cfe60390STomohiro Kusumi 			    lblktodoff(fs, lbn), size, uio->uio_resid,
1868cfe60390STomohiro Kusumi 			    (ap->a_ioflag >> IO_SEQSHIFT) * MAXBSIZE, &bp);
1869cfe60390STomohiro Kusumi 		} else if (seqcount > 1) {
1870cfe60390STomohiro Kusumi 			u_int nextsize = blksize(fs, ip, nextlbn);
1871cfe60390STomohiro Kusumi 
1872e5b38eb5STomohiro Kusumi 			error = breadn(vp, lblktodoff(fs, lbn), size,
1873e5b38eb5STomohiro Kusumi 			    &nextloffset, &nextsize, 1, &bp);
1874cfe60390STomohiro Kusumi 		} else
1875e5b38eb5STomohiro Kusumi 			error = bread(vp, lblktodoff(fs, lbn), size, &bp);
1876cfe60390STomohiro Kusumi 		if (error) {
1877e5b38eb5STomohiro Kusumi 			brelse(bp);
1878cfe60390STomohiro Kusumi 			bp = NULL;
1879cfe60390STomohiro Kusumi 			break;
1880cfe60390STomohiro Kusumi 		}
1881cfe60390STomohiro Kusumi 
1882cfe60390STomohiro Kusumi 		/*
1883cfe60390STomohiro Kusumi 		 * We should only get non-zero b_resid when an I/O error
1884cfe60390STomohiro Kusumi 		 * has occurred, which should cause us to break above.
1885cfe60390STomohiro Kusumi 		 * However, if the short read did not cause an error,
1886cfe60390STomohiro Kusumi 		 * then we want to ensure that we do not uiomove bad
1887cfe60390STomohiro Kusumi 		 * or uninitialized data.
1888cfe60390STomohiro Kusumi 		 */
1889cfe60390STomohiro Kusumi 		size -= bp->b_resid;
1890cfe60390STomohiro Kusumi 		if (size < xfersize) {
1891cfe60390STomohiro Kusumi 			if (size == 0)
1892cfe60390STomohiro Kusumi 				break;
1893cfe60390STomohiro Kusumi 			xfersize = size;
1894cfe60390STomohiro Kusumi 		}
1895cfe60390STomohiro Kusumi 		error = uiomove((char *)bp->b_data + blkoffset,
1896cfe60390STomohiro Kusumi 		    (int)xfersize, uio);
1897cfe60390STomohiro Kusumi 		if (error)
1898cfe60390STomohiro Kusumi 			break;
1899e5b38eb5STomohiro Kusumi 		bqrelse(bp);
1900cfe60390STomohiro Kusumi 	}
1901cfe60390STomohiro Kusumi 
1902cfe60390STomohiro Kusumi 	/*
1903cfe60390STomohiro Kusumi 	 * This can only happen in the case of an error because the loop
1904cfe60390STomohiro Kusumi 	 * above resets bp to NULL on each iteration and on normal
1905cfe60390STomohiro Kusumi 	 * completion has not set a new value into it. so it must have come
1906cfe60390STomohiro Kusumi 	 * from a 'break' statement
1907cfe60390STomohiro Kusumi 	 */
1908cfe60390STomohiro Kusumi 	if (bp != NULL)
1909e5b38eb5STomohiro Kusumi 		bqrelse(bp);
1910cfe60390STomohiro Kusumi 
1911cfe60390STomohiro Kusumi 	if ((error == 0 || uio->uio_resid != orig_resid) &&
1912cfe60390STomohiro Kusumi 	    (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
1913cfe60390STomohiro Kusumi 		ip->i_flag |= IN_ACCESS;
1914cfe60390STomohiro Kusumi 	return (error);
1915cfe60390STomohiro Kusumi }
1916cfe60390STomohiro Kusumi 
1917cfe60390STomohiro Kusumi /*
1918cfe60390STomohiro Kusumi  * Vnode op for writing.
1919cfe60390STomohiro Kusumi  */
1920cfe60390STomohiro Kusumi static int
ext2_write(struct vop_write_args * ap)1921cfe60390STomohiro Kusumi ext2_write(struct vop_write_args *ap)
1922cfe60390STomohiro Kusumi {
1923cfe60390STomohiro Kusumi 	struct vnode *vp;
1924cfe60390STomohiro Kusumi 	struct uio *uio;
1925cfe60390STomohiro Kusumi 	struct inode *ip;
1926cfe60390STomohiro Kusumi 	struct m_ext2fs *fs;
1927cfe60390STomohiro Kusumi 	struct buf *bp;
1928cfe60390STomohiro Kusumi 	struct thread *td;
1929cfe60390STomohiro Kusumi 	daddr_t lbn;
1930cfe60390STomohiro Kusumi 	off_t osize;
1931cfe60390STomohiro Kusumi 	int blkoffset, error, flags, ioflag, resid, size, seqcount, xfersize;
1932cfe60390STomohiro Kusumi 
1933cfe60390STomohiro Kusumi 	ioflag = ap->a_ioflag;
1934cfe60390STomohiro Kusumi 	uio = ap->a_uio;
1935cfe60390STomohiro Kusumi 	vp = ap->a_vp;
1936cfe60390STomohiro Kusumi 
1937cfe60390STomohiro Kusumi 	seqcount = ioflag >> IO_SEQSHIFT;
1938cfe60390STomohiro Kusumi 	ip = VTOI(vp);
1939cfe60390STomohiro Kusumi 
1940cfe60390STomohiro Kusumi #ifdef INVARIANTS
1941cfe60390STomohiro Kusumi 	if (uio->uio_rw != UIO_WRITE)
1942cfe60390STomohiro Kusumi 		panic("%s: mode", "ext2_write");
1943cfe60390STomohiro Kusumi #endif
1944cfe60390STomohiro Kusumi 
1945cfe60390STomohiro Kusumi 	switch (vp->v_type) {
1946cfe60390STomohiro Kusumi 	case VREG:
1947cfe60390STomohiro Kusumi 		if (ioflag & IO_APPEND)
1948cfe60390STomohiro Kusumi 			uio->uio_offset = ip->i_size;
1949cfe60390STomohiro Kusumi 		if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
1950cfe60390STomohiro Kusumi 			return (EPERM);
1951cfe60390STomohiro Kusumi 		/* FALLTHROUGH */
1952cfe60390STomohiro Kusumi 	case VLNK:
1953cfe60390STomohiro Kusumi 		break;
1954cfe60390STomohiro Kusumi 	case VDIR:
1955cfe60390STomohiro Kusumi 		/* XXX differs from ffs -- this is called from ext2_mkdir(). */
1956cfe60390STomohiro Kusumi 		if ((ioflag & IO_SYNC) == 0)
1957cfe60390STomohiro Kusumi 			panic("ext2_write: nonsync dir write");
1958cfe60390STomohiro Kusumi 		break;
1959cfe60390STomohiro Kusumi 	default:
1960cfe60390STomohiro Kusumi 		panic("ext2_write: type %p %d (%jd,%jd)", (void *)vp,
1961cfe60390STomohiro Kusumi 		    vp->v_type, (intmax_t)uio->uio_offset,
1962cfe60390STomohiro Kusumi 		    (intmax_t)uio->uio_resid);
1963cfe60390STomohiro Kusumi 	}
1964cfe60390STomohiro Kusumi 
1965cfe60390STomohiro Kusumi 	KASSERT(uio->uio_resid >= 0, ("ext2_write: uio->uio_resid < 0"));
1966cfe60390STomohiro Kusumi 	KASSERT(uio->uio_offset >= 0, ("ext2_write: uio->uio_offset < 0"));
1967cfe60390STomohiro Kusumi 	fs = ip->i_e2fs;
1968cfe60390STomohiro Kusumi 	if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->e2fs_maxfilesize)
1969cfe60390STomohiro Kusumi 		return (EFBIG);
1970cfe60390STomohiro Kusumi 	/*
1971cfe60390STomohiro Kusumi 	 * Maybe this should be above the vnode op call, but so long as
1972cfe60390STomohiro Kusumi 	 * file servers have no limits, I don't think it matters.
1973cfe60390STomohiro Kusumi 	 */
1974cfe60390STomohiro Kusumi 	td = uio->uio_td;
1975cfe60390STomohiro Kusumi 	if (vp->v_type == VREG && td && td->td_proc &&
1976cfe60390STomohiro Kusumi 	    uio->uio_offset + uio->uio_resid >
1977cfe60390STomohiro Kusumi 	    td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
1978cfe60390STomohiro Kusumi 		lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
1979cfe60390STomohiro Kusumi 		return (EFBIG);
1980cfe60390STomohiro Kusumi 	}
1981cfe60390STomohiro Kusumi 
1982cfe60390STomohiro Kusumi 	resid = uio->uio_resid;
1983cfe60390STomohiro Kusumi 	osize = ip->i_size;
1984cfe60390STomohiro Kusumi 	if (seqcount > BA_SEQMAX)
1985cfe60390STomohiro Kusumi 		flags = BA_SEQMAX << BA_SEQSHIFT;
1986cfe60390STomohiro Kusumi 	else
1987cfe60390STomohiro Kusumi 		flags = seqcount << BA_SEQSHIFT;
1988cfe60390STomohiro Kusumi 	if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
1989cfe60390STomohiro Kusumi 		flags |= IO_SYNC;
1990cfe60390STomohiro Kusumi 
1991cfe60390STomohiro Kusumi 	for (error = 0; uio->uio_resid > 0;) {
1992cfe60390STomohiro Kusumi 		lbn = lblkno(fs, uio->uio_offset);
1993cfe60390STomohiro Kusumi 		blkoffset = blkoff(fs, uio->uio_offset);
1994cfe60390STomohiro Kusumi 		xfersize = fs->e2fs_fsize - blkoffset;
1995cfe60390STomohiro Kusumi 		if (uio->uio_resid < xfersize)
1996cfe60390STomohiro Kusumi 			xfersize = uio->uio_resid;
1997cfe60390STomohiro Kusumi 		if (uio->uio_offset + xfersize > ip->i_size)
1998cfe60390STomohiro Kusumi 			vnode_pager_setsize(vp, uio->uio_offset + xfersize);
1999cfe60390STomohiro Kusumi 
2000cfe60390STomohiro Kusumi 		/*
2001cfe60390STomohiro Kusumi 		 * We must perform a read-before-write if the transfer size
2002cfe60390STomohiro Kusumi 		 * does not cover the entire buffer.
2003cfe60390STomohiro Kusumi 		 */
2004cfe60390STomohiro Kusumi 		if (fs->e2fs_bsize > xfersize)
2005cfe60390STomohiro Kusumi 			flags |= BA_CLRBUF;
2006cfe60390STomohiro Kusumi 		else
2007cfe60390STomohiro Kusumi 			flags &= ~BA_CLRBUF;
2008cfe60390STomohiro Kusumi 		error = ext2_balloc(ip, lbn, blkoffset + xfersize,
2009cfe60390STomohiro Kusumi 		    ap->a_cred, &bp, flags);
2010cfe60390STomohiro Kusumi 		if (error != 0)
2011cfe60390STomohiro Kusumi 			break;
2012cfe60390STomohiro Kusumi 
2013cfe60390STomohiro Kusumi 		if ((ioflag & (IO_SYNC | IO_INVAL)) == (IO_SYNC | IO_INVAL))
2014cfe60390STomohiro Kusumi 			bp->b_flags |= B_NOCACHE;
2015cfe60390STomohiro Kusumi 		if (uio->uio_offset + xfersize > ip->i_size)
2016cfe60390STomohiro Kusumi 			ip->i_size = uio->uio_offset + xfersize;
2017cfe60390STomohiro Kusumi 		size = blksize(fs, ip, lbn) - bp->b_resid;
2018cfe60390STomohiro Kusumi 		if (size < xfersize)
2019cfe60390STomohiro Kusumi 			xfersize = size;
2020cfe60390STomohiro Kusumi 
2021cfe60390STomohiro Kusumi 		error =
2022cfe60390STomohiro Kusumi 		    uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
2023cfe60390STomohiro Kusumi 		if ((ioflag & IO_VMIO) &&
2024cfe60390STomohiro Kusumi 		    LIST_FIRST(&bp->b_dep) == NULL) /* in ext2fs? */
2025cfe60390STomohiro Kusumi 			bp->b_flags |= B_RELBUF;
2026cfe60390STomohiro Kusumi 		/*
2027cfe60390STomohiro Kusumi 		 * If the buffer is not already filled and we encounter an
2028cfe60390STomohiro Kusumi 		 * error while trying to fill it, we have to clear out any
2029cfe60390STomohiro Kusumi 		 * garbage data from the pages instantiated for the buffer.
2030cfe60390STomohiro Kusumi 		 * If we do not, a failed uiomove() during a write can leave
2031cfe60390STomohiro Kusumi 		 * the prior contents of the pages exposed to a userland mmap.
2032cfe60390STomohiro Kusumi 		 *
2033cfe60390STomohiro Kusumi 		 * Note that we need only clear buffers with a transfer size
2034cfe60390STomohiro Kusumi 		 * equal to the block size because buffers with a shorter
2035cfe60390STomohiro Kusumi 		 * transfer size were cleared above by the call to ext2_balloc()
2036cfe60390STomohiro Kusumi 		 * with the BA_CLRBUF flag set.
2037cfe60390STomohiro Kusumi 		 *
2038cfe60390STomohiro Kusumi 		 * If the source region for uiomove identically mmaps the
2039cfe60390STomohiro Kusumi 		 * buffer, uiomove() performed the NOP copy, and the buffer
2040cfe60390STomohiro Kusumi 		 * content remains valid because the page fault handler
2041cfe60390STomohiro Kusumi 		 * validated the pages.
2042cfe60390STomohiro Kusumi 		 */
2043cfe60390STomohiro Kusumi 		if (error != 0 && (bp->b_flags & B_CACHE) == 0 &&
2044cfe60390STomohiro Kusumi 		    fs->e2fs_bsize == xfersize)
2045cfe60390STomohiro Kusumi 			vfs_bio_clrbuf(bp);
2046cfe60390STomohiro Kusumi 
2047cfe60390STomohiro Kusumi 		/*
2048cfe60390STomohiro Kusumi 		 * If IO_SYNC each buffer is written synchronously.  Otherwise
2049cfe60390STomohiro Kusumi 		 * if we have a severe page deficiency write the buffer
2050cfe60390STomohiro Kusumi 		 * asynchronously.  Otherwise try to cluster, and if that
2051cfe60390STomohiro Kusumi 		 * doesn't do it then either do an async write (if O_DIRECT),
2052cfe60390STomohiro Kusumi 		 * or a delayed write (if not).
2053cfe60390STomohiro Kusumi 		 */
2054cfe60390STomohiro Kusumi 		if (ioflag & IO_SYNC) {
2055cfe60390STomohiro Kusumi 			(void)bwrite(bp);
2056e91e64c7SMatthew Dillon 		} else if (vm_paging_severe() ||
2057cfe60390STomohiro Kusumi 			   buf_dirty_count_severe() ||
2058e91e64c7SMatthew Dillon 			   (ioflag & IO_ASYNC))
2059e91e64c7SMatthew Dillon 		{
2060cfe60390STomohiro Kusumi 			bp->b_flags |= B_CLUSTEROK;
2061cfe60390STomohiro Kusumi 			bawrite(bp);
2062cfe60390STomohiro Kusumi 		} else if (xfersize + blkoffset == fs->e2fs_fsize) {
2063cfe60390STomohiro Kusumi 			if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
2064cfe60390STomohiro Kusumi 				bp->b_flags |= B_CLUSTEROK;
2065cfe60390STomohiro Kusumi 				cluster_write(bp, (off_t)ip->i_size,
2066cfe60390STomohiro Kusumi 				    vp->v_mount->mnt_stat.f_iosize, seqcount);
2067cfe60390STomohiro Kusumi 			} else {
2068cfe60390STomohiro Kusumi 				bawrite(bp);
2069cfe60390STomohiro Kusumi 			}
2070cfe60390STomohiro Kusumi 		} else if (ioflag & IO_DIRECT) {
2071cfe60390STomohiro Kusumi 			bp->b_flags |= B_CLUSTEROK;
2072cfe60390STomohiro Kusumi 			bawrite(bp);
2073cfe60390STomohiro Kusumi 		} else {
2074cfe60390STomohiro Kusumi 			bp->b_flags |= B_CLUSTEROK;
2075cfe60390STomohiro Kusumi 			bdwrite(bp);
2076cfe60390STomohiro Kusumi 		}
2077cfe60390STomohiro Kusumi 		if (error || xfersize == 0)
2078cfe60390STomohiro Kusumi 			break;
2079cfe60390STomohiro Kusumi 	}
2080cfe60390STomohiro Kusumi 	/*
2081cfe60390STomohiro Kusumi 	 * If we successfully wrote any data, and we are not the superuser
2082cfe60390STomohiro Kusumi 	 * we clear the setuid and setgid bits as a precaution against
2083cfe60390STomohiro Kusumi 	 * tampering.
2084cfe60390STomohiro Kusumi 	 */
2085cfe60390STomohiro Kusumi 	if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
2086cfe60390STomohiro Kusumi 	    ap->a_cred) {
2087*2b3f93eaSMatthew Dillon 		if (caps_priv_check(ap->a_cred, SYSCAP_NOVFS_RETAINSUGID))
2088cfe60390STomohiro Kusumi 			ip->i_mode &= ~(ISUID | ISGID);
2089cfe60390STomohiro Kusumi 	}
2090cfe60390STomohiro Kusumi 	if (error) {
2091cfe60390STomohiro Kusumi 		if (ioflag & IO_UNIT) {
2092cfe60390STomohiro Kusumi 			(void)ext2_truncate(vp, osize, ioflag & IO_SYNC,
2093cfe60390STomohiro Kusumi 			    ap->a_cred);
2094cfe60390STomohiro Kusumi 			uio->uio_offset -= resid - uio->uio_resid;
2095cfe60390STomohiro Kusumi 			uio->uio_resid = resid;
2096cfe60390STomohiro Kusumi 		}
2097cfe60390STomohiro Kusumi 	}
2098cfe60390STomohiro Kusumi 	if (uio->uio_resid != resid) {
2099cfe60390STomohiro Kusumi 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
2100cfe60390STomohiro Kusumi 		if (ioflag & IO_SYNC)
2101cfe60390STomohiro Kusumi 			error = ext2_update(vp, 1);
2102cfe60390STomohiro Kusumi 	}
2103cfe60390STomohiro Kusumi 	return (error);
2104cfe60390STomohiro Kusumi }
2105cfe60390STomohiro Kusumi 
2106cfe60390STomohiro Kusumi /* Global vfs data structures for ext2. */
2107cfe60390STomohiro Kusumi struct vop_ops ext2_vnodeops = {
2108cfe60390STomohiro Kusumi 	.vop_default =		vop_defaultop,
2109cfe60390STomohiro Kusumi 	.vop_access =		ext2_access,
2110cfe60390STomohiro Kusumi 	.vop_bmap =		ext2_bmap,
2111cfe60390STomohiro Kusumi 	.vop_old_lookup =	ext2_lookup,
2112cfe60390STomohiro Kusumi 	.vop_close =		ext2_close,
2113cfe60390STomohiro Kusumi 	.vop_old_create =	ext2_create,
2114cfe60390STomohiro Kusumi 	.vop_fsync =		ext2_fsync,
2115cfe60390STomohiro Kusumi 	.vop_getpages =		vop_stdgetpages,
2116cfe60390STomohiro Kusumi 	.vop_putpages =		vop_stdputpages,
2117cfe60390STomohiro Kusumi 	.vop_getattr =		ext2_getattr,
2118cfe60390STomohiro Kusumi 	.vop_inactive =		ext2_inactive,
2119cfe60390STomohiro Kusumi 	.vop_old_link =		ext2_link,
2120cfe60390STomohiro Kusumi 	.vop_old_lookup =	ext2_lookup,
2121cfe60390STomohiro Kusumi 	.vop_old_mkdir =	ext2_mkdir,
2122cfe60390STomohiro Kusumi 	.vop_old_mknod =	ext2_mknod,
2123cfe60390STomohiro Kusumi 	.vop_open =		ext2_open,
2124cfe60390STomohiro Kusumi 	.vop_pathconf =		ext2_pathconf,
2125cfe60390STomohiro Kusumi 	.vop_print =		ext2_print,
2126cfe60390STomohiro Kusumi 	.vop_read =		ext2_read,
2127cfe60390STomohiro Kusumi 	.vop_readdir =		ext2_readdir,
2128cfe60390STomohiro Kusumi 	.vop_readlink =		ext2_readlink,
2129cfe60390STomohiro Kusumi 	.vop_reallocblks =	ext2_reallocblks,
2130cfe60390STomohiro Kusumi 	.vop_reclaim =		ext2_reclaim,
2131cfe60390STomohiro Kusumi 	.vop_old_remove =	ext2_remove,
2132cfe60390STomohiro Kusumi 	.vop_old_rename =	ext2_rename,
2133cfe60390STomohiro Kusumi 	.vop_old_rmdir =	ext2_rmdir,
2134cfe60390STomohiro Kusumi 	.vop_setattr =		ext2_setattr,
2135cfe60390STomohiro Kusumi 	.vop_strategy =		ext2_strategy,
2136cfe60390STomohiro Kusumi 	.vop_old_symlink =	ext2_symlink,
2137cfe60390STomohiro Kusumi 	.vop_write =		ext2_write,
2138cfe60390STomohiro Kusumi };
2139cfe60390STomohiro Kusumi 
2140cfe60390STomohiro Kusumi struct vop_ops ext2_specops = {
2141cfe60390STomohiro Kusumi 	.vop_default =		vop_defaultop,
2142cfe60390STomohiro Kusumi 	.vop_access =		ext2_access,
2143cfe60390STomohiro Kusumi 	.vop_close =		ext2_close,
2144cfe60390STomohiro Kusumi 	.vop_fsync =		ext2_fsync,
2145cfe60390STomohiro Kusumi 	.vop_getattr =		ext2_getattr,
2146cfe60390STomohiro Kusumi 	.vop_inactive =		ext2_inactive,
2147cfe60390STomohiro Kusumi 	.vop_pathconf =		ext2_pathconf,
2148cfe60390STomohiro Kusumi 	.vop_print =		ext2_print,
2149cfe60390STomohiro Kusumi 	.vop_read =		vop_stdnoread,
2150cfe60390STomohiro Kusumi 	.vop_reclaim =		ext2_reclaim,
2151cfe60390STomohiro Kusumi 	.vop_setattr =		ext2_setattr,
2152cfe60390STomohiro Kusumi 	.vop_write =		vop_stdnowrite
2153cfe60390STomohiro Kusumi };
2154cfe60390STomohiro Kusumi 
2155cfe60390STomohiro Kusumi struct vop_ops ext2_fifoops = {
2156cfe60390STomohiro Kusumi 	.vop_default =		fifo_vnoperate,
2157cfe60390STomohiro Kusumi 	.vop_access =		ext2_access,
2158cfe60390STomohiro Kusumi 	.vop_close =		ext2fifo_close,
2159cfe60390STomohiro Kusumi 	.vop_fsync =		ext2_fsync,
2160cfe60390STomohiro Kusumi 	.vop_getattr =		ext2_getattr,
2161cfe60390STomohiro Kusumi 	.vop_inactive =		ext2_inactive,
2162cfe60390STomohiro Kusumi 	.vop_kqfilter =		ext2fifo_kqfilter,
2163cfe60390STomohiro Kusumi 	.vop_pathconf =		ext2_pathconf,
2164cfe60390STomohiro Kusumi 	.vop_print =		ext2_print,
2165cfe60390STomohiro Kusumi 	.vop_read =		ext2fifo_read,
2166cfe60390STomohiro Kusumi 	.vop_reclaim =		ext2_reclaim,
2167cfe60390STomohiro Kusumi 	.vop_setattr =		ext2_setattr,
2168cfe60390STomohiro Kusumi 	.vop_write =		ext2fifo_write
2169cfe60390STomohiro Kusumi };
2170cfe60390STomohiro Kusumi 
2171cfe60390STomohiro Kusumi VNODEOP_SET(ext2_vnodeops);
2172cfe60390STomohiro Kusumi VNODEOP_SET(ext2_specops);
2173cfe60390STomohiro Kusumi VNODEOP_SET(ext2_fifoops);
2174