2 * modified for EXT2FS support in Lites 1.1
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
8 * SPDX-License-Identifier: BSD-3-Clause
10 * Copyright (c) 1982, 1986, 1989, 1993
11 * The Regents of the University of California. All rights reserved.
12 * (c) UNIX System Laboratories, Inc.
13 * All or some portions of this file are derived from material licensed
14 * to the University of California by American Telephone and Telegraph
15 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16 * the permission of UNIX System Laboratories, Inc.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * @(#)ufs_vnops.c 8.7 (Berkeley) 2/3/94
43 * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
47 #include "opt_suiddir.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/fcntl.h>
53 #include <sys/filio.h>
54 #include <sys/limits.h>
59 #include <sys/endian.h>
61 #include <sys/rwlock.h>
62 #include <sys/mount.h>
63 #include <sys/unistd.h>
65 #include <sys/vnode.h>
66 #include <sys/namei.h>
67 #include <sys/lockf.h>
68 #include <sys/event.h>
71 #include <sys/extattr.h>
72 #include <sys/vmmeter.h>
75 #include <vm/vm_param.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_page.h>
79 #include <vm/vm_pager.h>
80 #include <vm/vnode_pager.h>
82 #include "opt_directio.h"
84 #include <ufs/ufs/dir.h>
86 #include <fs/ext2fs/fs.h>
87 #include <fs/ext2fs/inode.h>
88 #include <fs/ext2fs/ext2_acl.h>
89 #include <fs/ext2fs/ext2fs.h>
90 #include <fs/ext2fs/ext2_extern.h>
91 #include <fs/ext2fs/ext2_dinode.h>
92 #include <fs/ext2fs/ext2_dir.h>
93 #include <fs/ext2fs/ext2_mount.h>
94 #include <fs/ext2fs/ext2_extattr.h>
95 #include <fs/ext2fs/ext2_extents.h>
97 SDT_PROVIDER_DECLARE(ext2fs);
100 * arg0: verbosity. Higher numbers give more verbose messages
101 * arg1: Textual message
103 SDT_PROBE_DEFINE2(ext2fs, , vnops, trace, "int", "char*");
105 static int ext2_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
106 static void ext2_itimes_locked(struct vnode *);
108 static vop_access_t ext2_access;
109 static int ext2_chmod(struct vnode *, int, struct ucred *, struct thread *);
110 static int ext2_chown(struct vnode *, uid_t, gid_t, struct ucred *,
112 static vop_close_t ext2_close;
113 static vop_create_t ext2_create;
114 static vop_fsync_t ext2_fsync;
115 static vop_getattr_t ext2_getattr;
116 static vop_ioctl_t ext2_ioctl;
117 static vop_link_t ext2_link;
118 static vop_mkdir_t ext2_mkdir;
119 static vop_mknod_t ext2_mknod;
120 static vop_open_t ext2_open;
121 static vop_pathconf_t ext2_pathconf;
122 static vop_print_t ext2_print;
123 static vop_read_t ext2_read;
124 static vop_readlink_t ext2_readlink;
125 static vop_remove_t ext2_remove;
126 static vop_rename_t ext2_rename;
127 static vop_rmdir_t ext2_rmdir;
128 static vop_setattr_t ext2_setattr;
129 static vop_strategy_t ext2_strategy;
130 static vop_symlink_t ext2_symlink;
131 static vop_write_t ext2_write;
132 static vop_deleteextattr_t ext2_deleteextattr;
133 static vop_getextattr_t ext2_getextattr;
134 static vop_listextattr_t ext2_listextattr;
135 static vop_setextattr_t ext2_setextattr;
136 static vop_vptofh_t ext2_vptofh;
137 static vop_close_t ext2fifo_close;
138 static vop_kqfilter_t ext2fifo_kqfilter;
140 /* Global vfs data structures for ext2. */
141 struct vop_vector ext2_vnodeops = {
142 .vop_default = &default_vnodeops,
143 .vop_access = ext2_access,
144 .vop_bmap = ext2_bmap,
145 .vop_cachedlookup = ext2_lookup,
146 .vop_close = ext2_close,
147 .vop_create = ext2_create,
148 .vop_fsync = ext2_fsync,
149 .vop_getpages = vnode_pager_local_getpages,
150 .vop_getpages_async = vnode_pager_local_getpages_async,
151 .vop_getattr = ext2_getattr,
152 .vop_inactive = ext2_inactive,
153 .vop_ioctl = ext2_ioctl,
154 .vop_link = ext2_link,
155 .vop_lookup = vfs_cache_lookup,
156 .vop_mkdir = ext2_mkdir,
157 .vop_mknod = ext2_mknod,
158 .vop_open = ext2_open,
159 .vop_pathconf = ext2_pathconf,
160 .vop_poll = vop_stdpoll,
161 .vop_print = ext2_print,
162 .vop_read = ext2_read,
163 .vop_readdir = ext2_readdir,
164 .vop_readlink = ext2_readlink,
165 .vop_reallocblks = ext2_reallocblks,
166 .vop_reclaim = ext2_reclaim,
167 .vop_remove = ext2_remove,
168 .vop_rename = ext2_rename,
169 .vop_rmdir = ext2_rmdir,
170 .vop_setattr = ext2_setattr,
171 .vop_strategy = ext2_strategy,
172 .vop_symlink = ext2_symlink,
173 .vop_write = ext2_write,
174 .vop_deleteextattr = ext2_deleteextattr,
175 .vop_getextattr = ext2_getextattr,
176 .vop_listextattr = ext2_listextattr,
177 .vop_setextattr = ext2_setextattr,
179 .vop_getacl = ext2_getacl,
180 .vop_setacl = ext2_setacl,
181 .vop_aclcheck = ext2_aclcheck,
183 .vop_vptofh = ext2_vptofh,
185 VFS_VOP_VECTOR_REGISTER(ext2_vnodeops);
187 struct vop_vector ext2_fifoops = {
188 .vop_default = &fifo_specops,
189 .vop_access = ext2_access,
190 .vop_close = ext2fifo_close,
191 .vop_fsync = ext2_fsync,
192 .vop_getattr = ext2_getattr,
193 .vop_inactive = ext2_inactive,
194 .vop_kqfilter = ext2fifo_kqfilter,
195 .vop_pathconf = ext2_pathconf,
196 .vop_print = ext2_print,
197 .vop_read = VOP_PANIC,
198 .vop_reclaim = ext2_reclaim,
199 .vop_setattr = ext2_setattr,
200 .vop_write = VOP_PANIC,
201 .vop_vptofh = ext2_vptofh,
203 VFS_VOP_VECTOR_REGISTER(ext2_fifoops);
206 * A virgin directory (no blushing please).
207 * Note that the type and namlen fields are reversed relative to ext2.
208 * Also, we don't use `struct odirtemplate', since it would just cause
209 * endianness problems.
211 static struct dirtemplate mastertemplate = {
212 0, htole16(12), 1, EXT2_FT_DIR, ".",
213 0, htole16(DIRBLKSIZ - 12), 2, EXT2_FT_DIR, ".."
215 static struct dirtemplate omastertemplate = {
216 0, htole16(12), 1, EXT2_FT_UNKNOWN, ".",
217 0, htole16(DIRBLKSIZ - 12), 2, EXT2_FT_UNKNOWN, ".."
221 ext2_itimes_locked(struct vnode *vp)
226 ASSERT_VI_LOCKED(vp, __func__);
229 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
231 if ((vp->v_type == VBLK || vp->v_type == VCHR))
232 ip->i_flag |= IN_LAZYMOD;
234 ip->i_flag |= IN_MODIFIED;
235 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
237 if (ip->i_flag & IN_ACCESS) {
238 ip->i_atime = ts.tv_sec;
239 ip->i_atimensec = ts.tv_nsec;
241 if (ip->i_flag & IN_UPDATE) {
242 ip->i_mtime = ts.tv_sec;
243 ip->i_mtimensec = ts.tv_nsec;
246 if (ip->i_flag & IN_CHANGE) {
247 ip->i_ctime = ts.tv_sec;
248 ip->i_ctimensec = ts.tv_nsec;
251 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
255 ext2_itimes(struct vnode *vp)
259 ext2_itimes_locked(vp);
264 * Create a regular file
267 ext2_create(struct vop_create_args *ap)
272 ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
273 ap->a_dvp, ap->a_vpp, ap->a_cnp);
276 if ((ap->a_cnp->cn_flags & MAKEENTRY) != 0)
277 cache_enter(ap->a_dvp, *ap->a_vpp, ap->a_cnp);
282 ext2_open(struct vop_open_args *ap)
285 if (ap->a_vp->v_type == VBLK || ap->a_vp->v_type == VCHR)
289 * Files marked append-only must be opened for appending.
291 if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
292 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
295 vnode_create_vobject(ap->a_vp, VTOI(ap->a_vp)->i_size, ap->a_td);
303 * Update the times on the inode.
306 ext2_close(struct vop_close_args *ap)
308 struct vnode *vp = ap->a_vp;
311 if (vp->v_usecount > 1)
312 ext2_itimes_locked(vp);
318 ext2_access(struct vop_access_args *ap)
320 struct vnode *vp = ap->a_vp;
321 struct inode *ip = VTOI(vp);
322 accmode_t accmode = ap->a_accmode;
325 if (vp->v_type == VBLK || vp->v_type == VCHR)
329 * Disallow write attempts on read-only file systems;
330 * unless the file is a socket, fifo, or a block or
331 * character device resident on the file system.
333 if (accmode & VWRITE) {
334 switch (vp->v_type) {
338 if (vp->v_mount->mnt_flag & MNT_RDONLY)
346 /* If immutable bit set, nobody gets to write it. */
347 if ((accmode & VWRITE) && (ip->i_flags & (SF_IMMUTABLE | SF_SNAPSHOT)))
350 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
351 ap->a_accmode, ap->a_cred, NULL);
356 ext2_getattr(struct vop_getattr_args *ap)
358 struct vnode *vp = ap->a_vp;
359 struct inode *ip = VTOI(vp);
360 struct vattr *vap = ap->a_vap;
364 * Copy from inode table
366 vap->va_fsid = dev2udev(ip->i_devvp->v_rdev);
367 vap->va_fileid = ip->i_number;
368 vap->va_mode = ip->i_mode & ~IFMT;
369 vap->va_nlink = ip->i_nlink;
370 vap->va_uid = ip->i_uid;
371 vap->va_gid = ip->i_gid;
372 vap->va_rdev = ip->i_rdev;
373 vap->va_size = ip->i_size;
374 vap->va_atime.tv_sec = ip->i_atime;
375 vap->va_atime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_atimensec : 0;
376 vap->va_mtime.tv_sec = ip->i_mtime;
377 vap->va_mtime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_mtimensec : 0;
378 vap->va_ctime.tv_sec = ip->i_ctime;
379 vap->va_ctime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_ctimensec : 0;
380 if E2DI_HAS_XTIME(ip) {
381 vap->va_birthtime.tv_sec = ip->i_birthtime;
382 vap->va_birthtime.tv_nsec = ip->i_birthnsec;
384 vap->va_flags = ip->i_flags;
385 vap->va_gen = ip->i_gen;
386 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
387 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
388 vap->va_type = IFTOVT(ip->i_mode);
389 vap->va_filerev = ip->i_modrev;
394 * Set attribute vnode op. called from several syscalls
397 ext2_setattr(struct vop_setattr_args *ap)
399 struct vattr *vap = ap->a_vap;
400 struct vnode *vp = ap->a_vp;
401 struct inode *ip = VTOI(vp);
402 struct ucred *cred = ap->a_cred;
403 struct thread *td = curthread;
407 * Check for unsettable attributes.
409 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
410 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
411 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
412 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
415 if (vap->va_flags != VNOVAL) {
416 /* Disallow flags not supported by ext2fs. */
417 if (vap->va_flags & ~(SF_APPEND | SF_IMMUTABLE | UF_NODUMP))
420 if (vp->v_mount->mnt_flag & MNT_RDONLY)
423 * Callers may only modify the file flags on objects they
424 * have VADMIN rights for.
426 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
429 * Unprivileged processes and privileged processes in
430 * jail() are not permitted to unset system flags, or
431 * modify flags if any system flags are set.
432 * Privileged non-jail processes may not modify system flags
433 * if securelevel > 0 and any existing system flags are set.
435 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS)) {
436 if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) {
437 error = securelevel_gt(cred, 0);
442 if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND) ||
443 ((vap->va_flags ^ ip->i_flags) & SF_SETTABLE))
446 ip->i_flags = vap->va_flags;
447 ip->i_flag |= IN_CHANGE;
448 if (ip->i_flags & (IMMUTABLE | APPEND))
451 if (ip->i_flags & (IMMUTABLE | APPEND))
454 * Go through the fields and update iff not VNOVAL.
456 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
457 if (vp->v_mount->mnt_flag & MNT_RDONLY)
459 if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred,
463 if (vap->va_size != VNOVAL) {
465 * Disallow write attempts on read-only file systems;
466 * unless the file is a socket, fifo, or a block or
467 * character device resident on the file system.
469 switch (vp->v_type) {
474 if (vp->v_mount->mnt_flag & MNT_RDONLY)
480 if ((error = ext2_truncate(vp, vap->va_size, 0, cred, td)) != 0)
483 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
484 if (vp->v_mount->mnt_flag & MNT_RDONLY)
488 * If times is NULL, ... The caller must be the owner of
489 * the file, have permission to write the file, or be the
491 * If times is non-NULL, ... The caller must be the owner of
492 * the file or be the super-user.
494 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)) &&
495 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
496 (error = VOP_ACCESS(vp, VWRITE, cred, td))))
498 ip->i_flag |= IN_CHANGE | IN_MODIFIED;
499 if (vap->va_atime.tv_sec != VNOVAL) {
500 ip->i_flag &= ~IN_ACCESS;
501 ip->i_atime = vap->va_atime.tv_sec;
502 ip->i_atimensec = vap->va_atime.tv_nsec;
504 if (vap->va_mtime.tv_sec != VNOVAL) {
505 ip->i_flag &= ~IN_UPDATE;
506 ip->i_mtime = vap->va_mtime.tv_sec;
507 ip->i_mtimensec = vap->va_mtime.tv_nsec;
509 ip->i_birthtime = vap->va_birthtime.tv_sec;
510 ip->i_birthnsec = vap->va_birthtime.tv_nsec;
511 error = ext2_update(vp, 0);
516 if (vap->va_mode != (mode_t)VNOVAL) {
517 if (vp->v_mount->mnt_flag & MNT_RDONLY)
519 error = ext2_chmod(vp, (int)vap->va_mode, cred, td);
525 * Change the mode on a file.
526 * Inode must be locked before calling.
529 ext2_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td)
531 struct inode *ip = VTOI(vp);
535 * To modify the permissions on a file, must possess VADMIN
538 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
541 * Privileged processes may set the sticky bit on non-directories,
542 * as well as set the setgid bit on a file with a group that the
543 * process is not a member of.
545 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
546 error = priv_check_cred(cred, PRIV_VFS_STICKYFILE);
550 if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
551 error = priv_check_cred(cred, PRIV_VFS_SETGID);
555 ip->i_mode &= ~ALLPERMS;
556 ip->i_mode |= (mode & ALLPERMS);
557 ip->i_flag |= IN_CHANGE;
562 * Perform chown operation on inode ip;
563 * inode must be locked prior to call.
566 ext2_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
569 struct inode *ip = VTOI(vp);
574 if (uid == (uid_t)VNOVAL)
576 if (gid == (gid_t)VNOVAL)
579 * To modify the ownership of a file, must possess VADMIN
582 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
585 * To change the owner of a file, or change the group of a file
586 * to a group of which we are not a member, the caller must
589 if (uid != ip->i_uid || (gid != ip->i_gid &&
590 !groupmember(gid, cred))) {
591 error = priv_check_cred(cred, PRIV_VFS_CHOWN);
599 ip->i_flag |= IN_CHANGE;
600 if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
601 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID) != 0)
602 ip->i_mode &= ~(ISUID | ISGID);
608 * Synch an open file.
612 ext2_fsync(struct vop_fsync_args *ap)
615 * Flush all dirty buffers associated with a vnode.
620 return (ext2_update(ap->a_vp, ap->a_waitfor == MNT_WAIT));
628 ext2_mknod(struct vop_mknod_args *ap)
630 struct vattr *vap = ap->a_vap;
631 struct vnode **vpp = ap->a_vpp;
636 error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
637 ap->a_dvp, vpp, ap->a_cnp);
641 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
642 if (vap->va_rdev != VNOVAL) {
644 * Want to be able to use this to make badblock
645 * inodes, so don't truncate the dev number.
647 if (!(ip->i_flag & IN_E4EXTENTS))
648 ip->i_rdev = vap->va_rdev;
651 * Remove inode, then reload it through VFS_VGET so it is
652 * checked to see if it is an alias of an existing entry in
653 * the inode cache. XXX I don't believe this is necessary now.
655 (*vpp)->v_type = VNON;
656 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
659 error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
668 ext2_remove(struct vop_remove_args *ap)
671 struct vnode *vp = ap->a_vp;
672 struct vnode *dvp = ap->a_dvp;
676 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
677 (VTOI(dvp)->i_flags & APPEND)) {
681 error = ext2_dirremove(dvp, ap->a_cnp);
684 ip->i_flag |= IN_CHANGE;
694 ext2_link(struct vop_link_args *ap)
696 struct vnode *vp = ap->a_vp;
697 struct vnode *tdvp = ap->a_tdvp;
698 struct componentname *cnp = ap->a_cnp;
703 if ((cnp->cn_flags & HASBUF) == 0)
704 panic("ext2_link: no name");
707 if ((nlink_t)ip->i_nlink >= EXT4_LINK_MAX) {
711 if (ip->i_flags & (IMMUTABLE | APPEND)) {
716 ip->i_flag |= IN_CHANGE;
717 error = ext2_update(vp, !DOINGASYNC(vp));
719 error = ext2_direnter(ip, tdvp, cnp);
722 ip->i_flag |= IN_CHANGE;
729 ext2_inc_nlink(struct inode *ip)
734 if (S_ISDIR(ip->i_mode) &&
735 EXT2_HAS_RO_COMPAT_FEATURE(ip->i_e2fs, EXT2F_ROCOMPAT_DIR_NLINK) &&
737 if (ip->i_nlink >= EXT4_LINK_MAX || ip->i_nlink == 2)
739 } else if (ip->i_nlink > EXT4_LINK_MAX) {
748 ext2_dec_nlink(struct inode *ip)
751 if (!S_ISDIR(ip->i_mode) || ip->i_nlink > 2)
756 * Rename system call.
757 * rename("foo", "bar");
760 * link("foo", "bar");
762 * but ``atomically''. Can't do full commit without saving state in the
763 * inode on disk which isn't feasible at this time. Best we can do is
764 * always guarantee the target exists.
766 * Basic algorithm is:
768 * 1) Bump link count on source while we're linking it to the
769 * target. This also ensure the inode won't be deleted out
770 * from underneath us while we work (it may be truncated by
771 * a concurrent `trunc' or `open' for creation).
772 * 2) Link source to destination. If destination already exists,
774 * 3) Unlink source reference to inode if still around. If a
775 * directory was moved and the parent of the destination
776 * is different from the source, patch the ".." entry in the
780 ext2_rename(struct vop_rename_args *ap)
782 struct vnode *tvp = ap->a_tvp;
783 struct vnode *tdvp = ap->a_tdvp;
784 struct vnode *fvp = ap->a_fvp;
785 struct vnode *fdvp = ap->a_fdvp;
786 struct componentname *tcnp = ap->a_tcnp;
787 struct componentname *fcnp = ap->a_fcnp;
788 struct inode *ip, *xp, *dp;
789 struct dirtemplate *dirbuf;
790 int doingdirectory = 0, oldparent = 0, newparent = 0;
795 if ((tcnp->cn_flags & HASBUF) == 0 ||
796 (fcnp->cn_flags & HASBUF) == 0)
797 panic("ext2_rename: no name");
800 * Check for cross-device rename.
802 if ((fvp->v_mount != tdvp->v_mount) ||
803 (tvp && (fvp->v_mount != tvp->v_mount))) {
817 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
818 (VTOI(tdvp)->i_flags & APPEND))) {
824 * Renaming a file to itself has no effect. The upper layers should
825 * not call us in that case. Temporarily just warn if they do.
828 SDT_PROBE2(ext2fs, , vnops, trace, 1,
829 "rename: fvp == tvp (can't happen)");
834 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
838 if (ip->i_nlink >= EXT4_LINK_MAX &&
839 !EXT2_HAS_RO_COMPAT_FEATURE(ip->i_e2fs, EXT2F_ROCOMPAT_DIR_NLINK)) {
844 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
845 || (dp->i_flags & APPEND)) {
850 if ((ip->i_mode & IFMT) == IFDIR) {
852 * Avoid ".", "..", and aliases of "." for obvious reasons.
854 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
855 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
856 (ip->i_flag & IN_RENAME)) {
861 ip->i_flag |= IN_RENAME;
862 oldparent = dp->i_number;
868 * When the target exists, both the directory
869 * and target vnodes are returned locked.
877 * 1) Bump link count while we're moving stuff
878 * around. If we crash somewhere before
879 * completing our work, the link count
880 * may be wrong, but correctable.
883 ip->i_flag |= IN_CHANGE;
884 if ((error = ext2_update(fvp, !DOINGASYNC(fvp))) != 0) {
890 * If ".." must be changed (ie the directory gets a new
891 * parent) then the source directory must not be in the
892 * directory hierarchy above the target, as this would
893 * orphan everything below the source directory. Also
894 * the user must have write permission in the source so
895 * as to be able to change "..". We must repeat the call
896 * to namei, as the parent directory is unlocked by the
897 * call to checkpath().
899 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
901 if (oldparent != dp->i_number)
902 newparent = dp->i_number;
903 if (doingdirectory && newparent) {
904 if (error) /* write access check above */
908 error = ext2_checkpath(ip, dp, tcnp->cn_cred);
912 error = relookup(tdvp, &tvp, tcnp);
922 * 2) If target doesn't exist, link the target
923 * to the source and unlink the source.
924 * Otherwise, rewrite the target directory
925 * entry to reference the source inode and
926 * expunge the original entry's existence.
929 if (dp->i_devvp != ip->i_devvp)
930 panic("ext2_rename: EXDEV");
932 * Account for ".." in new directory.
933 * When source and destination have the same
934 * parent we don't fool with the link count.
936 if (doingdirectory && newparent) {
937 error = ext2_inc_nlink(dp);
941 dp->i_flag |= IN_CHANGE;
942 error = ext2_update(tdvp, !DOINGASYNC(tdvp));
946 error = ext2_direnter(ip, tdvp, tcnp);
948 if (doingdirectory && newparent) {
950 dp->i_flag |= IN_CHANGE;
951 (void)ext2_update(tdvp, 1);
957 if (xp->i_devvp != dp->i_devvp || xp->i_devvp != ip->i_devvp)
958 panic("ext2_rename: EXDEV");
960 * Short circuit rename(foo, foo).
962 if (xp->i_number == ip->i_number)
963 panic("ext2_rename: same file");
965 * If the parent directory is "sticky", then the user must
966 * own the parent directory, or the destination of the rename,
967 * otherwise the destination may not be changed (except by
968 * root). This implements append-only directories.
970 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
971 tcnp->cn_cred->cr_uid != dp->i_uid &&
972 xp->i_uid != tcnp->cn_cred->cr_uid) {
977 * Target must be empty if a directory and have no links
978 * to it. Also, ensure source and target are compatible
979 * (both directories, or both not directories).
981 if ((xp->i_mode & IFMT) == IFDIR) {
982 if (!ext2_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
986 if (!doingdirectory) {
991 } else if (doingdirectory) {
995 error = ext2_dirrewrite(dp, ip, tcnp);
999 * If the target directory is in the same
1000 * directory as the source directory,
1001 * decrement the link count on the parent
1002 * of the target directory.
1004 if (doingdirectory && !newparent) {
1006 dp->i_flag |= IN_CHANGE;
1010 * Adjust the link count of the target to
1011 * reflect the dirrewrite above. If this is
1012 * a directory it is empty and there are
1013 * no links to it, so we can squash the inode and
1014 * any space associated with it. We disallowed
1015 * renaming over top of a directory with links to
1016 * it above, as the remaining link would point to
1017 * a directory without "." or ".." entries.
1020 if (doingdirectory) {
1021 if (xp->i_nlink > 2)
1022 panic("ext2_rename: linked directory");
1023 error = ext2_truncate(tvp, (off_t)0, IO_SYNC,
1024 tcnp->cn_cred, tcnp->cn_thread);
1027 xp->i_flag |= IN_CHANGE;
1033 * 3) Unlink the source.
1035 fcnp->cn_flags &= ~MODMASK;
1036 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1038 error = relookup(fdvp, &fvp, fcnp);
1046 * From name has disappeared. IN_RENAME is not sufficient
1047 * to protect against directory races due to timing windows,
1048 * so we can't panic here.
1054 * Ensure that the directory entry still exists and has not
1055 * changed while the new name has been entered. If the source is
1056 * a file then the entry may have been unlinked or renamed. In
1057 * either case there is no further work to be done. If the source
1058 * is a directory then it cannot have been rmdir'ed; its link
1059 * count of three would cause a rmdir to fail with ENOTEMPTY.
1060 * The IN_RENAME flag ensures that it cannot be moved by another
1065 * From name resolves to a different inode. IN_RENAME is
1066 * not sufficient protection against timing window races
1067 * so we can't panic here.
1071 * If the source is a directory with a
1072 * new parent, the link count of the old
1073 * parent directory must be decremented
1074 * and ".." set to point to the new parent.
1076 if (doingdirectory && newparent) {
1078 dp->i_flag |= IN_CHANGE;
1079 dirbuf = malloc(dp->i_e2fs->e2fs_bsize, M_TEMP, M_WAITOK | M_ZERO);
1084 error = vn_rdwr(UIO_READ, fvp, (caddr_t)dirbuf,
1085 ip->i_e2fs->e2fs_bsize, (off_t)0,
1086 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1087 tcnp->cn_cred, NOCRED, NULL, NULL);
1089 /* Like ufs little-endian: */
1090 namlen = dirbuf->dotdot_type;
1092 dirbuf->dotdot_name[0] != '.' ||
1093 dirbuf->dotdot_name[1] != '.') {
1094 ext2_dirbad(xp, (doff_t)12,
1095 "rename: mangled dir");
1097 dirbuf->dotdot_ino = htole32(newparent);
1099 * dirblock 0 could be htree root,
1100 * try both csum update functions.
1102 ext2_dirent_csum_set(ip,
1103 (struct ext2fs_direct_2 *)dirbuf);
1104 ext2_dx_csum_set(ip,
1105 (struct ext2fs_direct_2 *)dirbuf);
1106 (void)vn_rdwr(UIO_WRITE, fvp,
1108 ip->i_e2fs->e2fs_bsize,
1109 (off_t)0, UIO_SYSSPACE,
1110 IO_NODELOCKED | IO_SYNC |
1111 IO_NOMACCHECK, tcnp->cn_cred,
1112 NOCRED, NULL, NULL);
1116 free(dirbuf, M_TEMP);
1118 error = ext2_dirremove(fdvp, fcnp);
1121 xp->i_flag |= IN_CHANGE;
1123 xp->i_flag &= ~IN_RENAME;
1138 ip->i_flag &= ~IN_RENAME;
1139 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1141 ip->i_flag |= IN_CHANGE;
1142 ip->i_flag &= ~IN_RENAME;
1151 ext2_do_posix1e_acl_inheritance_dir(struct vnode *dvp, struct vnode *tvp,
1152 mode_t dmode, struct ucred *cred, struct thread *td)
1155 struct inode *ip = VTOI(tvp);
1156 struct acl *dacl, *acl;
1158 acl = acl_alloc(M_WAITOK);
1159 dacl = acl_alloc(M_WAITOK);
1162 * Retrieve default ACL from parent, if any.
1164 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cred, td);
1168 * Retrieved a default ACL, so merge mode and ACL if
1169 * necessary. If the ACL is empty, fall through to
1170 * the "not defined or available" case.
1172 if (acl->acl_cnt != 0) {
1173 dmode = acl_posix1e_newfilemode(dmode, acl);
1176 ext2_sync_acl_from_inode(ip, acl);
1183 * Just use the mode as-is.
1193 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cred, td);
1195 error = VOP_SETACL(tvp, ACL_TYPE_DEFAULT, dacl, cred, td);
1202 * XXX: This should not happen, as EOPNOTSUPP above
1203 * was supposed to free acl.
1206 printf("ext2_mkdir: VOP_GETACL() but no VOP_SETACL()\n");
1222 ext2_do_posix1e_acl_inheritance_file(struct vnode *dvp, struct vnode *tvp,
1223 mode_t mode, struct ucred *cred, struct thread *td)
1226 struct inode *ip = VTOI(tvp);
1229 acl = acl_alloc(M_WAITOK);
1232 * Retrieve default ACL for parent, if any.
1234 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cred, td);
1238 * Retrieved a default ACL, so merge mode and ACL if
1241 if (acl->acl_cnt != 0) {
1243 * Two possible ways for default ACL to not
1244 * be present. First, the EA can be
1245 * undefined, or second, the default ACL can
1246 * be blank. If it's blank, fall through to
1247 * the it's not defined case.
1249 mode = acl_posix1e_newfilemode(mode, acl);
1251 ext2_sync_acl_from_inode(ip, acl);
1258 * Just use the mode as-is.
1268 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cred, td);
1275 * XXX: This should not happen, as EOPNOTSUPP above was
1276 * supposed to free acl.
1278 printf("ufs_do_posix1e_acl_inheritance_file: VOP_GETACL() "
1279 "but no VOP_SETACL()\n");
1280 /* panic("ufs_do_posix1e_acl_inheritance_file: VOP_GETACL() "
1281 "but no VOP_SETACL()"); */
1294 #endif /* UFS_ACL */
1300 ext2_mkdir(struct vop_mkdir_args *ap)
1302 struct m_ext2fs *fs;
1303 struct vnode *dvp = ap->a_dvp;
1304 struct vattr *vap = ap->a_vap;
1305 struct componentname *cnp = ap->a_cnp;
1306 struct inode *ip, *dp;
1308 struct dirtemplate dirtemplate, *dtp;
1313 if ((cnp->cn_flags & HASBUF) == 0)
1314 panic("ext2_mkdir: no name");
1317 if ((nlink_t)dp->i_nlink >= EXT4_LINK_MAX &&
1318 !EXT2_HAS_RO_COMPAT_FEATURE(dp->i_e2fs, EXT2F_ROCOMPAT_DIR_NLINK)) {
1322 dmode = vap->va_mode & 0777;
1325 * Must simulate part of ext2_makeinode here to acquire the inode,
1326 * but not have it entered in the parent directory. The entry is
1327 * made later after writing "." and ".." entries.
1329 error = ext2_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1334 ip->i_gid = dp->i_gid;
1338 * if we are hacking owners here, (only do this where told to)
1339 * and we are not giving it TOO root, (would subvert quotas)
1340 * then go ahead and give it to the other user.
1341 * The new directory also inherits the SUID bit.
1342 * If user's UID and dir UID are the same,
1343 * 'give it away' so that the SUID is still forced on.
1345 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1346 (dp->i_mode & ISUID) && dp->i_uid) {
1348 ip->i_uid = dp->i_uid;
1350 ip->i_uid = cnp->cn_cred->cr_uid;
1354 ip->i_uid = cnp->cn_cred->cr_uid;
1356 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1358 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1360 if (cnp->cn_flags & ISWHITEOUT)
1361 ip->i_flags |= UF_OPAQUE;
1362 error = ext2_update(tvp, 1);
1365 * Bump link count in parent directory
1366 * to reflect work done below. Should
1367 * be done before reference is created
1368 * so reparation is possible if we crash.
1371 dp->i_flag |= IN_CHANGE;
1372 error = ext2_update(dvp, !DOINGASYNC(dvp));
1376 /* Initialize directory with "." and ".." from static template. */
1377 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
1378 EXT2F_INCOMPAT_FTYPE))
1379 dtp = &mastertemplate;
1381 dtp = &omastertemplate;
1383 dirtemplate.dot_ino = htole32(ip->i_number);
1384 dirtemplate.dotdot_ino = htole32(dp->i_number);
1386 * note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE so let's
1387 * just redefine it - for this function only
1390 #define DIRBLKSIZ VTOI(dvp)->i_e2fs->e2fs_bsize
1391 dirtemplate.dotdot_reclen = htole16(DIRBLKSIZ - 12);
1392 buf = malloc(DIRBLKSIZ, M_TEMP, M_WAITOK | M_ZERO);
1396 dp->i_flag |= IN_CHANGE;
1399 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
1400 dirtemplate.dotdot_reclen =
1401 htole16(le16toh(dirtemplate.dotdot_reclen) -
1402 sizeof(struct ext2fs_direct_tail));
1403 ext2_init_dirent_tail(EXT2_DIRENT_TAIL(buf, DIRBLKSIZ));
1405 memcpy(buf, &dirtemplate, sizeof(dirtemplate));
1406 ext2_dirent_csum_set(ip, (struct ext2fs_direct_2 *)buf);
1407 error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)buf,
1408 DIRBLKSIZ, (off_t)0, UIO_SYSSPACE,
1409 IO_NODELOCKED | IO_SYNC | IO_NOMACCHECK, cnp->cn_cred, NOCRED,
1413 dp->i_flag |= IN_CHANGE;
1416 if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1417 /* XXX should grow with balloc() */
1418 panic("ext2_mkdir: blksize");
1420 ip->i_size = DIRBLKSIZ;
1421 ip->i_flag |= IN_CHANGE;
1425 if (dvp->v_mount->mnt_flag & MNT_ACLS) {
1426 error = ext2_do_posix1e_acl_inheritance_dir(dvp, tvp, dmode,
1427 cnp->cn_cred, cnp->cn_thread);
1432 #endif /* UFS_ACL */
1434 /* Directory set up, now install its entry in the parent directory. */
1435 error = ext2_direnter(ip, dvp, cnp);
1438 dp->i_flag |= IN_CHANGE;
1442 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1443 * for us because we set the link count to 0.
1447 ip->i_flag |= IN_CHANGE;
1455 #define DIRBLKSIZ DEV_BSIZE
1459 * Rmdir system call.
1462 ext2_rmdir(struct vop_rmdir_args *ap)
1464 struct vnode *vp = ap->a_vp;
1465 struct vnode *dvp = ap->a_dvp;
1466 struct componentname *cnp = ap->a_cnp;
1467 struct inode *ip, *dp;
1474 * Verify the directory is empty (and valid).
1475 * (Rmdir ".." won't be valid since
1476 * ".." will contain a reference to
1477 * the current directory and thus be
1480 if (!ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1484 if ((dp->i_flags & APPEND)
1485 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1490 * Delete reference to directory before purging
1491 * inode. If we crash in between, the directory
1492 * will be reattached to lost+found,
1494 error = ext2_dirremove(dvp, cnp);
1498 dp->i_flag |= IN_CHANGE;
1502 * Truncate inode. The only stuff left
1503 * in the directory is "." and "..".
1506 error = ext2_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred,
1508 cache_purge(ITOV(ip));
1509 if (vn_lock(dvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1511 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1512 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1519 * symlink -- make a symbolic link
1522 ext2_symlink(struct vop_symlink_args *ap)
1524 struct vnode *vp, **vpp = ap->a_vpp;
1528 error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1533 len = strlen(ap->a_target);
1534 if (len < vp->v_mount->mnt_maxsymlinklen) {
1536 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1538 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1540 error = vn_rdwr(UIO_WRITE, vp, __DECONST(void *, ap->a_target),
1541 len, (off_t)0, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1542 ap->a_cnp->cn_cred, NOCRED, NULL, NULL);
1549 * Return target name of a symbolic link
1552 ext2_readlink(struct vop_readlink_args *ap)
1554 struct vnode *vp = ap->a_vp;
1555 struct inode *ip = VTOI(vp);
1559 if (isize < vp->v_mount->mnt_maxsymlinklen) {
1560 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1563 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1567 * Calculate the logical to physical mapping if not done already,
1568 * then call the device strategy routine.
1570 * In order to be able to swap to a file, the ext2_bmaparray() operation may not
1571 * deadlock on memory. See ext2_bmap() for details.
1574 ext2_strategy(struct vop_strategy_args *ap)
1576 struct buf *bp = ap->a_bp;
1577 struct vnode *vp = ap->a_vp;
1582 if (vp->v_type == VBLK || vp->v_type == VCHR)
1583 panic("ext2_strategy: spec");
1584 if (bp->b_blkno == bp->b_lblkno) {
1586 if (VTOI(ap->a_vp)->i_flag & IN_E4EXTENTS)
1587 error = ext4_bmapext(vp, bp->b_lblkno, &blkno, NULL, NULL);
1589 error = ext2_bmaparray(vp, bp->b_lblkno, &blkno, NULL, NULL);
1591 bp->b_blkno = blkno;
1593 bp->b_error = error;
1594 bp->b_ioflags |= BIO_ERROR;
1598 if ((long)bp->b_blkno == -1)
1601 if ((long)bp->b_blkno == -1) {
1605 bp->b_iooffset = dbtob(bp->b_blkno);
1606 bo = VFSTOEXT2(vp->v_mount)->um_bo;
1607 BO_STRATEGY(bo, bp);
1612 * Print out the contents of an inode.
1615 ext2_print(struct vop_print_args *ap)
1617 struct vnode *vp = ap->a_vp;
1618 struct inode *ip = VTOI(vp);
1620 vn_printf(ip->i_devvp, "\tino %ju", (uintmax_t)ip->i_number);
1621 if (vp->v_type == VFIFO)
1628 * Close wrapper for fifos.
1630 * Update the times on the inode then do device close.
1633 ext2fifo_close(struct vop_close_args *ap)
1635 struct vnode *vp = ap->a_vp;
1638 if (vp->v_usecount > 1)
1639 ext2_itimes_locked(vp);
1641 return (fifo_specops.vop_close(ap));
1645 * Kqfilter wrapper for fifos.
1647 * Fall through to ext2 kqfilter routines if needed
1650 ext2fifo_kqfilter(struct vop_kqfilter_args *ap)
1654 error = fifo_specops.vop_kqfilter(ap);
1656 error = vfs_kqfilter(ap);
1661 * Return POSIX pathconf information applicable to ext2 filesystems.
1664 ext2_pathconf(struct vop_pathconf_args *ap)
1668 switch (ap->a_name) {
1670 if (EXT2_HAS_RO_COMPAT_FEATURE(VTOI(ap->a_vp)->i_e2fs,
1671 EXT2F_ROCOMPAT_DIR_NLINK))
1672 *ap->a_retval = INT_MAX;
1674 *ap->a_retval = EXT4_LINK_MAX;
1677 *ap->a_retval = NAME_MAX;
1680 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
1681 *ap->a_retval = PIPE_BUF;
1685 case _PC_CHOWN_RESTRICTED:
1693 case _PC_ACL_EXTENDED:
1694 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
1699 case _PC_ACL_PATH_MAX:
1700 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
1701 *ap->a_retval = ACL_MAX_ENTRIES;
1705 #endif /* UFS_ACL */
1707 case _PC_MIN_HOLE_SIZE:
1708 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1716 case _PC_ALLOC_SIZE_MIN:
1717 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
1719 case _PC_FILESIZEBITS:
1722 case _PC_REC_INCR_XFER_SIZE:
1723 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1725 case _PC_REC_MAX_XFER_SIZE:
1726 *ap->a_retval = -1; /* means ``unlimited'' */
1728 case _PC_REC_MIN_XFER_SIZE:
1729 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1731 case _PC_REC_XFER_ALIGN:
1732 *ap->a_retval = PAGE_SIZE;
1734 case _PC_SYMLINK_MAX:
1735 *ap->a_retval = MAXPATHLEN;
1739 error = vop_stdpathconf(ap);
1746 * Vnode operation to remove a named attribute.
1749 ext2_deleteextattr(struct vop_deleteextattr_args *ap)
1752 struct m_ext2fs *fs;
1755 ip = VTOI(ap->a_vp);
1758 if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1759 return (EOPNOTSUPP);
1761 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1762 return (EOPNOTSUPP);
1764 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1765 ap->a_cred, ap->a_td, VWRITE);
1771 if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1772 error = ext2_extattr_inode_delete(ip, ap->a_attrnamespace, ap->a_name);
1773 if (error != ENOATTR)
1778 error = ext2_extattr_block_delete(ip, ap->a_attrnamespace, ap->a_name);
1784 * Vnode operation to retrieve a named extended attribute.
1787 ext2_getextattr(struct vop_getextattr_args *ap)
1790 struct m_ext2fs *fs;
1793 ip = VTOI(ap->a_vp);
1796 if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1797 return (EOPNOTSUPP);
1799 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1800 return (EOPNOTSUPP);
1802 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1803 ap->a_cred, ap->a_td, VREAD);
1807 if (ap->a_size != NULL)
1812 if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1813 error = ext2_extattr_inode_get(ip, ap->a_attrnamespace,
1814 ap->a_name, ap->a_uio, ap->a_size);
1815 if (error != ENOATTR)
1820 error = ext2_extattr_block_get(ip, ap->a_attrnamespace,
1821 ap->a_name, ap->a_uio, ap->a_size);
1827 * Vnode operation to retrieve extended attributes on a vnode.
1830 ext2_listextattr(struct vop_listextattr_args *ap)
1833 struct m_ext2fs *fs;
1836 ip = VTOI(ap->a_vp);
1839 if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1840 return (EOPNOTSUPP);
1842 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1843 return (EOPNOTSUPP);
1845 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1846 ap->a_cred, ap->a_td, VREAD);
1850 if (ap->a_size != NULL)
1853 if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1854 error = ext2_extattr_inode_list(ip, ap->a_attrnamespace,
1855 ap->a_uio, ap->a_size);
1861 error = ext2_extattr_block_list(ip, ap->a_attrnamespace,
1862 ap->a_uio, ap->a_size);
1868 * Vnode operation to set a named attribute.
1871 ext2_setextattr(struct vop_setextattr_args *ap)
1874 struct m_ext2fs *fs;
1877 ip = VTOI(ap->a_vp);
1880 if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1881 return (EOPNOTSUPP);
1883 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1884 return (EOPNOTSUPP);
1886 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1887 ap->a_cred, ap->a_td, VWRITE);
1891 error = ext2_extattr_valid_attrname(ap->a_attrnamespace, ap->a_name);
1895 if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1896 error = ext2_extattr_inode_set(ip, ap->a_attrnamespace,
1897 ap->a_name, ap->a_uio);
1898 if (error != ENOSPC)
1902 error = ext2_extattr_block_set(ip, ap->a_attrnamespace,
1903 ap->a_name, ap->a_uio);
1909 * Vnode pointer to File handle
1913 ext2_vptofh(struct vop_vptofh_args *ap)
1918 ip = VTOI(ap->a_vp);
1919 ufhp = (struct ufid *)ap->a_fhp;
1920 ufhp->ufid_len = sizeof(struct ufid);
1921 ufhp->ufid_ino = ip->i_number;
1922 ufhp->ufid_gen = ip->i_gen;
1927 * Initialize the vnode associated with a new inode, handle aliased
1931 ext2_vinit(struct mount *mntp, struct vop_vector *fifoops, struct vnode **vpp)
1938 vp->v_type = IFTOVT(ip->i_mode);
1940 * Only unallocated inodes should be of type VNON.
1942 if (ip->i_mode != 0 && vp->v_type == VNON)
1944 if (vp->v_type == VFIFO)
1947 if (ip->i_number == EXT2_ROOTINO)
1948 vp->v_vflag |= VV_ROOT;
1949 ip->i_modrev = init_va_filerev();
1955 * Allocate a new inode.
1958 ext2_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
1959 struct componentname *cnp)
1961 struct inode *ip, *pdir;
1967 if ((cnp->cn_flags & HASBUF) == 0)
1968 panic("ext2_makeinode: no name");
1971 if ((mode & IFMT) == 0)
1974 error = ext2_valloc(dvp, mode, cnp->cn_cred, &tvp);
1979 ip->i_gid = pdir->i_gid;
1984 * not the owner of the directory,
1985 * and we are hacking owners here, (only do this where told to)
1986 * and we are not giving it TOO root, (would subvert quotas)
1987 * then go ahead and give it to the other user.
1988 * Note that this drops off the execute bits for security.
1990 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1991 (pdir->i_mode & ISUID) &&
1992 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1993 ip->i_uid = pdir->i_uid;
1996 ip->i_uid = cnp->cn_cred->cr_uid;
2000 ip->i_uid = cnp->cn_cred->cr_uid;
2002 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2004 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2006 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred)) {
2007 if (priv_check_cred(cnp->cn_cred, PRIV_VFS_RETAINSUGID))
2008 ip->i_mode &= ~ISGID;
2011 if (cnp->cn_flags & ISWHITEOUT)
2012 ip->i_flags |= UF_OPAQUE;
2015 * Make sure inode goes to disk before directory entry.
2017 error = ext2_update(tvp, !DOINGASYNC(tvp));
2022 if (dvp->v_mount->mnt_flag & MNT_ACLS) {
2023 error = ext2_do_posix1e_acl_inheritance_file(dvp, tvp, mode,
2024 cnp->cn_cred, cnp->cn_thread);
2028 #endif /* UFS_ACL */
2030 error = ext2_direnter(ip, dvp, cnp);
2039 * Write error occurred trying to update the inode
2040 * or the directory so must deallocate the inode.
2043 ip->i_flag |= IN_CHANGE;
2049 * Vnode op for reading.
2052 ext2_read(struct vop_read_args *ap)
2057 struct m_ext2fs *fs;
2059 daddr_t lbn, nextlbn;
2061 long size, xfersize, blkoffset;
2062 int error, orig_resid, seqcount;
2067 ioflag = ap->a_ioflag;
2069 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
2073 if (uio->uio_rw != UIO_READ)
2074 panic("%s: mode", "ext2_read");
2076 if (vp->v_type == VLNK) {
2077 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
2078 panic("%s: short symlink", "ext2_read");
2079 } else if (vp->v_type != VREG && vp->v_type != VDIR)
2080 panic("%s: type %d", "ext2_read", vp->v_type);
2082 orig_resid = uio->uio_resid;
2083 KASSERT(orig_resid >= 0, ("ext2_read: uio->uio_resid < 0"));
2084 if (orig_resid == 0)
2086 KASSERT(uio->uio_offset >= 0, ("ext2_read: uio->uio_offset < 0"));
2088 if (uio->uio_offset < ip->i_size &&
2089 uio->uio_offset >= fs->e2fs_maxfilesize)
2092 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
2093 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
2095 lbn = lblkno(fs, uio->uio_offset);
2097 size = blksize(fs, ip, lbn);
2098 blkoffset = blkoff(fs, uio->uio_offset);
2100 xfersize = fs->e2fs_fsize - blkoffset;
2101 if (uio->uio_resid < xfersize)
2102 xfersize = uio->uio_resid;
2103 if (bytesinfile < xfersize)
2104 xfersize = bytesinfile;
2106 if (lblktosize(fs, nextlbn) >= ip->i_size)
2107 error = bread(vp, lbn, size, NOCRED, &bp);
2108 else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
2109 error = cluster_read(vp, ip->i_size, lbn, size,
2110 NOCRED, blkoffset + uio->uio_resid, seqcount,
2112 } else if (seqcount > 1) {
2113 u_int nextsize = blksize(fs, ip, nextlbn);
2115 error = breadn(vp, lbn,
2116 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
2118 error = bread(vp, lbn, size, NOCRED, &bp);
2126 * We should only get non-zero b_resid when an I/O error
2127 * has occurred, which should cause us to break above.
2128 * However, if the short read did not cause an error,
2129 * then we want to ensure that we do not uiomove bad
2130 * or uninitialized data.
2132 size -= bp->b_resid;
2133 if (size < xfersize) {
2138 error = uiomove((char *)bp->b_data + blkoffset,
2139 (int)xfersize, uio);
2142 vfs_bio_brelse(bp, ioflag);
2146 * This can only happen in the case of an error because the loop
2147 * above resets bp to NULL on each iteration and on normal
2148 * completion has not set a new value into it. so it must have come
2149 * from a 'break' statement
2152 vfs_bio_brelse(bp, ioflag);
2154 if ((error == 0 || uio->uio_resid != orig_resid) &&
2155 (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
2156 ip->i_flag |= IN_ACCESS;
2161 ext2_ioctl(struct vop_ioctl_args *ap)
2167 switch (ap->a_command) {
2169 if (!(VTOI(vp)->i_flag & IN_E4EXTENTS)) {
2170 error = vn_lock(vp, LK_SHARED);
2172 error = ext2_bmap_seekdata(vp,
2173 (off_t *)ap->a_data);
2180 return (vn_bmap_seekhole(vp, ap->a_command,
2181 (off_t *)ap->a_data, ap->a_cred));
2188 * Vnode op for writing.
2191 ext2_write(struct vop_write_args *ap)
2196 struct m_ext2fs *fs;
2200 int blkoffset, error, flags, ioflag, resid, size, seqcount, xfersize;
2202 ioflag = ap->a_ioflag;
2206 seqcount = ioflag >> IO_SEQSHIFT;
2210 if (uio->uio_rw != UIO_WRITE)
2211 panic("%s: mode", "ext2_write");
2214 switch (vp->v_type) {
2216 if (ioflag & IO_APPEND)
2217 uio->uio_offset = ip->i_size;
2218 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
2224 /* XXX differs from ffs -- this is called from ext2_mkdir(). */
2225 if ((ioflag & IO_SYNC) == 0)
2226 panic("ext2_write: nonsync dir write");
2229 panic("ext2_write: type %p %d (%jd,%jd)", (void *)vp,
2230 vp->v_type, (intmax_t)uio->uio_offset,
2231 (intmax_t)uio->uio_resid);
2234 KASSERT(uio->uio_resid >= 0, ("ext2_write: uio->uio_resid < 0"));
2235 KASSERT(uio->uio_offset >= 0, ("ext2_write: uio->uio_offset < 0"));
2237 if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->e2fs_maxfilesize)
2240 * Maybe this should be above the vnode op call, but so long as
2241 * file servers have no limits, I don't think it matters.
2243 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
2246 resid = uio->uio_resid;
2248 if (seqcount > BA_SEQMAX)
2249 flags = BA_SEQMAX << BA_SEQSHIFT;
2251 flags = seqcount << BA_SEQSHIFT;
2252 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
2255 for (error = 0; uio->uio_resid > 0;) {
2256 lbn = lblkno(fs, uio->uio_offset);
2257 blkoffset = blkoff(fs, uio->uio_offset);
2258 xfersize = fs->e2fs_fsize - blkoffset;
2259 if (uio->uio_resid < xfersize)
2260 xfersize = uio->uio_resid;
2261 if (uio->uio_offset + xfersize > ip->i_size)
2262 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
2265 * We must perform a read-before-write if the transfer size
2266 * does not cover the entire buffer.
2268 if (fs->e2fs_bsize > xfersize)
2271 flags &= ~BA_CLRBUF;
2272 error = ext2_balloc(ip, lbn, blkoffset + xfersize,
2273 ap->a_cred, &bp, flags);
2277 if ((ioflag & (IO_SYNC | IO_INVAL)) == (IO_SYNC | IO_INVAL))
2278 bp->b_flags |= B_NOCACHE;
2279 if (uio->uio_offset + xfersize > ip->i_size)
2280 ip->i_size = uio->uio_offset + xfersize;
2281 size = blksize(fs, ip, lbn) - bp->b_resid;
2282 if (size < xfersize)
2286 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
2288 * If the buffer is not already filled and we encounter an
2289 * error while trying to fill it, we have to clear out any
2290 * garbage data from the pages instantiated for the buffer.
2291 * If we do not, a failed uiomove() during a write can leave
2292 * the prior contents of the pages exposed to a userland mmap.
2294 * Note that we need only clear buffers with a transfer size
2295 * equal to the block size because buffers with a shorter
2296 * transfer size were cleared above by the call to ext2_balloc()
2297 * with the BA_CLRBUF flag set.
2299 * If the source region for uiomove identically mmaps the
2300 * buffer, uiomove() performed the NOP copy, and the buffer
2301 * content remains valid because the page fault handler
2302 * validated the pages.
2304 if (error != 0 && (bp->b_flags & B_CACHE) == 0 &&
2305 fs->e2fs_bsize == xfersize)
2308 vfs_bio_set_flags(bp, ioflag);
2311 * If IO_SYNC each buffer is written synchronously. Otherwise
2312 * if we have a severe page deficiency write the buffer
2313 * asynchronously. Otherwise try to cluster, and if that
2314 * doesn't do it then either do an async write (if O_DIRECT),
2315 * or a delayed write (if not).
2317 if (ioflag & IO_SYNC) {
2319 } else if (vm_page_count_severe() ||
2320 buf_dirty_count_severe() ||
2321 (ioflag & IO_ASYNC)) {
2322 bp->b_flags |= B_CLUSTEROK;
2324 } else if (xfersize + blkoffset == fs->e2fs_fsize) {
2325 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
2326 bp->b_flags |= B_CLUSTEROK;
2327 cluster_write(vp, bp, ip->i_size, seqcount, 0);
2331 } else if (ioflag & IO_DIRECT) {
2332 bp->b_flags |= B_CLUSTEROK;
2335 bp->b_flags |= B_CLUSTEROK;
2338 if (error || xfersize == 0)
2342 * If we successfully wrote any data, and we are not the superuser
2343 * we clear the setuid and setgid bits as a precaution against
2346 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
2348 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID))
2349 ip->i_mode &= ~(ISUID | ISGID);
2352 if (ioflag & IO_UNIT) {
2353 (void)ext2_truncate(vp, osize,
2354 ioflag & IO_SYNC, ap->a_cred, uio->uio_td);
2355 uio->uio_offset -= resid - uio->uio_resid;
2356 uio->uio_resid = resid;
2359 if (uio->uio_resid != resid) {
2360 ip->i_flag |= IN_CHANGE | IN_UPDATE;
2361 if (ioflag & IO_SYNC)
2362 error = ext2_update(vp, 1);