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>
57 #include <sys/endian.h>
59 #include <sys/rwlock.h>
60 #include <sys/mount.h>
61 #include <sys/unistd.h>
63 #include <sys/vnode.h>
64 #include <sys/namei.h>
65 #include <sys/lockf.h>
66 #include <sys/event.h>
69 #include <sys/extattr.h>
70 #include <sys/vmmeter.h>
73 #include <vm/vm_param.h>
74 #include <vm/vm_extern.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_pager.h>
78 #include <vm/vnode_pager.h>
80 #include "opt_directio.h"
82 #include <ufs/ufs/dir.h>
84 #include <fs/ext2fs/fs.h>
85 #include <fs/ext2fs/inode.h>
86 #include <fs/ext2fs/ext2_acl.h>
87 #include <fs/ext2fs/ext2_extern.h>
88 #include <fs/ext2fs/ext2fs.h>
89 #include <fs/ext2fs/ext2_dinode.h>
90 #include <fs/ext2fs/ext2_dir.h>
91 #include <fs/ext2fs/ext2_mount.h>
92 #include <fs/ext2fs/ext2_extattr.h>
94 static int ext2_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
95 static void ext2_itimes_locked(struct vnode *);
97 static vop_access_t ext2_access;
98 static int ext2_chmod(struct vnode *, int, struct ucred *, struct thread *);
99 static int ext2_chown(struct vnode *, uid_t, gid_t, struct ucred *,
101 static vop_close_t ext2_close;
102 static vop_create_t ext2_create;
103 static vop_fsync_t ext2_fsync;
104 static vop_getattr_t ext2_getattr;
105 static vop_ioctl_t ext2_ioctl;
106 static vop_link_t ext2_link;
107 static vop_mkdir_t ext2_mkdir;
108 static vop_mknod_t ext2_mknod;
109 static vop_open_t ext2_open;
110 static vop_pathconf_t ext2_pathconf;
111 static vop_print_t ext2_print;
112 static vop_read_t ext2_read;
113 static vop_readlink_t ext2_readlink;
114 static vop_remove_t ext2_remove;
115 static vop_rename_t ext2_rename;
116 static vop_rmdir_t ext2_rmdir;
117 static vop_setattr_t ext2_setattr;
118 static vop_strategy_t ext2_strategy;
119 static vop_symlink_t ext2_symlink;
120 static vop_write_t ext2_write;
121 static vop_deleteextattr_t ext2_deleteextattr;
122 static vop_getextattr_t ext2_getextattr;
123 static vop_listextattr_t ext2_listextattr;
124 static vop_setextattr_t ext2_setextattr;
125 static vop_vptofh_t ext2_vptofh;
126 static vop_close_t ext2fifo_close;
127 static vop_kqfilter_t ext2fifo_kqfilter;
129 /* Global vfs data structures for ext2. */
130 struct vop_vector ext2_vnodeops = {
131 .vop_default = &default_vnodeops,
132 .vop_access = ext2_access,
133 .vop_bmap = ext2_bmap,
134 .vop_cachedlookup = ext2_lookup,
135 .vop_close = ext2_close,
136 .vop_create = ext2_create,
137 .vop_fsync = ext2_fsync,
138 .vop_getpages = vnode_pager_local_getpages,
139 .vop_getpages_async = vnode_pager_local_getpages_async,
140 .vop_getattr = ext2_getattr,
141 .vop_inactive = ext2_inactive,
142 .vop_ioctl = ext2_ioctl,
143 .vop_link = ext2_link,
144 .vop_lookup = vfs_cache_lookup,
145 .vop_mkdir = ext2_mkdir,
146 .vop_mknod = ext2_mknod,
147 .vop_open = ext2_open,
148 .vop_pathconf = ext2_pathconf,
149 .vop_poll = vop_stdpoll,
150 .vop_print = ext2_print,
151 .vop_read = ext2_read,
152 .vop_readdir = ext2_readdir,
153 .vop_readlink = ext2_readlink,
154 .vop_reallocblks = ext2_reallocblks,
155 .vop_reclaim = ext2_reclaim,
156 .vop_remove = ext2_remove,
157 .vop_rename = ext2_rename,
158 .vop_rmdir = ext2_rmdir,
159 .vop_setattr = ext2_setattr,
160 .vop_strategy = ext2_strategy,
161 .vop_symlink = ext2_symlink,
162 .vop_write = ext2_write,
163 .vop_deleteextattr = ext2_deleteextattr,
164 .vop_getextattr = ext2_getextattr,
165 .vop_listextattr = ext2_listextattr,
166 .vop_setextattr = ext2_setextattr,
168 .vop_getacl = ext2_getacl,
169 .vop_setacl = ext2_setacl,
170 .vop_aclcheck = ext2_aclcheck,
172 .vop_vptofh = ext2_vptofh,
175 struct vop_vector ext2_fifoops = {
176 .vop_default = &fifo_specops,
177 .vop_access = ext2_access,
178 .vop_close = ext2fifo_close,
179 .vop_fsync = ext2_fsync,
180 .vop_getattr = ext2_getattr,
181 .vop_inactive = ext2_inactive,
182 .vop_kqfilter = ext2fifo_kqfilter,
183 .vop_print = ext2_print,
184 .vop_read = VOP_PANIC,
185 .vop_reclaim = ext2_reclaim,
186 .vop_setattr = ext2_setattr,
187 .vop_write = VOP_PANIC,
188 .vop_vptofh = ext2_vptofh,
192 * A virgin directory (no blushing please).
193 * Note that the type and namlen fields are reversed relative to ext2.
194 * Also, we don't use `struct odirtemplate', since it would just cause
195 * endianness problems.
197 static struct dirtemplate mastertemplate = {
198 0, 12, 1, EXT2_FT_DIR, ".",
199 0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
201 static struct dirtemplate omastertemplate = {
202 0, 12, 1, EXT2_FT_UNKNOWN, ".",
203 0, DIRBLKSIZ - 12, 2, EXT2_FT_UNKNOWN, ".."
207 ext2_itimes_locked(struct vnode *vp)
212 ASSERT_VI_LOCKED(vp, __func__);
215 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
217 if ((vp->v_type == VBLK || vp->v_type == VCHR))
218 ip->i_flag |= IN_LAZYMOD;
220 ip->i_flag |= IN_MODIFIED;
221 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
223 if (ip->i_flag & IN_ACCESS) {
224 ip->i_atime = ts.tv_sec;
225 ip->i_atimensec = ts.tv_nsec;
227 if (ip->i_flag & IN_UPDATE) {
228 ip->i_mtime = ts.tv_sec;
229 ip->i_mtimensec = ts.tv_nsec;
232 if (ip->i_flag & IN_CHANGE) {
233 ip->i_ctime = ts.tv_sec;
234 ip->i_ctimensec = ts.tv_nsec;
237 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
241 ext2_itimes(struct vnode *vp)
245 ext2_itimes_locked(vp);
250 * Create a regular file
253 ext2_create(struct vop_create_args *ap)
258 ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
259 ap->a_dvp, ap->a_vpp, ap->a_cnp);
262 if ((ap->a_cnp->cn_flags & MAKEENTRY) != 0)
263 cache_enter(ap->a_dvp, *ap->a_vpp, ap->a_cnp);
268 ext2_open(struct vop_open_args *ap)
271 if (ap->a_vp->v_type == VBLK || ap->a_vp->v_type == VCHR)
275 * Files marked append-only must be opened for appending.
277 if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
278 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
281 vnode_create_vobject(ap->a_vp, VTOI(ap->a_vp)->i_size, ap->a_td);
289 * Update the times on the inode.
292 ext2_close(struct vop_close_args *ap)
294 struct vnode *vp = ap->a_vp;
297 if (vp->v_usecount > 1)
298 ext2_itimes_locked(vp);
304 ext2_access(struct vop_access_args *ap)
306 struct vnode *vp = ap->a_vp;
307 struct inode *ip = VTOI(vp);
308 accmode_t accmode = ap->a_accmode;
311 if (vp->v_type == VBLK || vp->v_type == VCHR)
315 * Disallow write attempts on read-only file systems;
316 * unless the file is a socket, fifo, or a block or
317 * character device resident on the file system.
319 if (accmode & VWRITE) {
320 switch (vp->v_type) {
324 if (vp->v_mount->mnt_flag & MNT_RDONLY)
332 /* If immutable bit set, nobody gets to write it. */
333 if ((accmode & VWRITE) && (ip->i_flags & (SF_IMMUTABLE | SF_SNAPSHOT)))
336 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
337 ap->a_accmode, ap->a_cred, NULL);
342 ext2_getattr(struct vop_getattr_args *ap)
344 struct vnode *vp = ap->a_vp;
345 struct inode *ip = VTOI(vp);
346 struct vattr *vap = ap->a_vap;
350 * Copy from inode table
352 vap->va_fsid = dev2udev(ip->i_devvp->v_rdev);
353 vap->va_fileid = ip->i_number;
354 vap->va_mode = ip->i_mode & ~IFMT;
355 vap->va_nlink = ip->i_nlink;
356 vap->va_uid = ip->i_uid;
357 vap->va_gid = ip->i_gid;
358 vap->va_rdev = ip->i_rdev;
359 vap->va_size = ip->i_size;
360 vap->va_atime.tv_sec = ip->i_atime;
361 vap->va_atime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_atimensec : 0;
362 vap->va_mtime.tv_sec = ip->i_mtime;
363 vap->va_mtime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_mtimensec : 0;
364 vap->va_ctime.tv_sec = ip->i_ctime;
365 vap->va_ctime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_ctimensec : 0;
366 if E2DI_HAS_XTIME(ip) {
367 vap->va_birthtime.tv_sec = ip->i_birthtime;
368 vap->va_birthtime.tv_nsec = ip->i_birthnsec;
370 vap->va_flags = ip->i_flags;
371 vap->va_gen = ip->i_gen;
372 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
373 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
374 vap->va_type = IFTOVT(ip->i_mode);
375 vap->va_filerev = ip->i_modrev;
380 * Set attribute vnode op. called from several syscalls
383 ext2_setattr(struct vop_setattr_args *ap)
385 struct vattr *vap = ap->a_vap;
386 struct vnode *vp = ap->a_vp;
387 struct inode *ip = VTOI(vp);
388 struct ucred *cred = ap->a_cred;
389 struct thread *td = curthread;
393 * Check for unsettable attributes.
395 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
396 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
397 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
398 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
401 if (vap->va_flags != VNOVAL) {
402 /* Disallow flags not supported by ext2fs. */
403 if (vap->va_flags & ~(SF_APPEND | SF_IMMUTABLE | UF_NODUMP))
406 if (vp->v_mount->mnt_flag & MNT_RDONLY)
409 * Callers may only modify the file flags on objects they
410 * have VADMIN rights for.
412 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
415 * Unprivileged processes and privileged processes in
416 * jail() are not permitted to unset system flags, or
417 * modify flags if any system flags are set.
418 * Privileged non-jail processes may not modify system flags
419 * if securelevel > 0 and any existing system flags are set.
421 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
422 if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) {
423 error = securelevel_gt(cred, 0);
428 if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND) ||
429 ((vap->va_flags ^ ip->i_flags) & SF_SETTABLE))
432 ip->i_flags = vap->va_flags;
433 ip->i_flag |= IN_CHANGE;
434 if (ip->i_flags & (IMMUTABLE | APPEND))
437 if (ip->i_flags & (IMMUTABLE | APPEND))
440 * Go through the fields and update iff not VNOVAL.
442 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
443 if (vp->v_mount->mnt_flag & MNT_RDONLY)
445 if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred,
449 if (vap->va_size != VNOVAL) {
451 * Disallow write attempts on read-only file systems;
452 * unless the file is a socket, fifo, or a block or
453 * character device resident on the file system.
455 switch (vp->v_type) {
460 if (vp->v_mount->mnt_flag & MNT_RDONLY)
466 if ((error = ext2_truncate(vp, vap->va_size, 0, cred, td)) != 0)
469 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
470 if (vp->v_mount->mnt_flag & MNT_RDONLY)
474 * If times is NULL, ... The caller must be the owner of
475 * the file, have permission to write the file, or be the
477 * If times is non-NULL, ... The caller must be the owner of
478 * the file or be the super-user.
480 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)) &&
481 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
482 (error = VOP_ACCESS(vp, VWRITE, cred, td))))
484 ip->i_flag |= IN_CHANGE | IN_MODIFIED;
485 if (vap->va_atime.tv_sec != VNOVAL) {
486 ip->i_flag &= ~IN_ACCESS;
487 ip->i_atime = vap->va_atime.tv_sec;
488 ip->i_atimensec = vap->va_atime.tv_nsec;
490 if (vap->va_mtime.tv_sec != VNOVAL) {
491 ip->i_flag &= ~IN_UPDATE;
492 ip->i_mtime = vap->va_mtime.tv_sec;
493 ip->i_mtimensec = vap->va_mtime.tv_nsec;
495 ip->i_birthtime = vap->va_birthtime.tv_sec;
496 ip->i_birthnsec = vap->va_birthtime.tv_nsec;
497 error = ext2_update(vp, 0);
502 if (vap->va_mode != (mode_t)VNOVAL) {
503 if (vp->v_mount->mnt_flag & MNT_RDONLY)
505 error = ext2_chmod(vp, (int)vap->va_mode, cred, td);
511 * Change the mode on a file.
512 * Inode must be locked before calling.
515 ext2_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td)
517 struct inode *ip = VTOI(vp);
521 * To modify the permissions on a file, must possess VADMIN
524 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
527 * Privileged processes may set the sticky bit on non-directories,
528 * as well as set the setgid bit on a file with a group that the
529 * process is not a member of.
531 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
532 error = priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0);
536 if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
537 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
541 ip->i_mode &= ~ALLPERMS;
542 ip->i_mode |= (mode & ALLPERMS);
543 ip->i_flag |= IN_CHANGE;
548 * Perform chown operation on inode ip;
549 * inode must be locked prior to call.
552 ext2_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
555 struct inode *ip = VTOI(vp);
560 if (uid == (uid_t)VNOVAL)
562 if (gid == (gid_t)VNOVAL)
565 * To modify the ownership of a file, must possess VADMIN
568 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
571 * To change the owner of a file, or change the group of a file
572 * to a group of which we are not a member, the caller must
575 if (uid != ip->i_uid || (gid != ip->i_gid &&
576 !groupmember(gid, cred))) {
577 error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0);
585 ip->i_flag |= IN_CHANGE;
586 if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
587 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0) != 0)
588 ip->i_mode &= ~(ISUID | ISGID);
594 * Synch an open file.
598 ext2_fsync(struct vop_fsync_args *ap)
601 * Flush all dirty buffers associated with a vnode.
606 return (ext2_update(ap->a_vp, ap->a_waitfor == MNT_WAIT));
614 ext2_mknod(struct vop_mknod_args *ap)
616 struct vattr *vap = ap->a_vap;
617 struct vnode **vpp = ap->a_vpp;
622 error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
623 ap->a_dvp, vpp, ap->a_cnp);
627 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
628 if (vap->va_rdev != VNOVAL) {
630 * Want to be able to use this to make badblock
631 * inodes, so don't truncate the dev number.
633 if (!(ip->i_flag & IN_E4EXTENTS))
634 ip->i_rdev = vap->va_rdev;
637 * Remove inode, then reload it through VFS_VGET so it is
638 * checked to see if it is an alias of an existing entry in
639 * the inode cache. XXX I don't believe this is necessary now.
641 (*vpp)->v_type = VNON;
642 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
645 error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
654 ext2_remove(struct vop_remove_args *ap)
657 struct vnode *vp = ap->a_vp;
658 struct vnode *dvp = ap->a_dvp;
662 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
663 (VTOI(dvp)->i_flags & APPEND)) {
667 error = ext2_dirremove(dvp, ap->a_cnp);
670 ip->i_flag |= IN_CHANGE;
676 static unsigned short
677 ext2_max_nlink(struct inode *ip)
683 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_DIR_NLINK))
684 return (EXT4_LINK_MAX);
686 return (EXT2_LINK_MAX);
693 ext2_link(struct vop_link_args *ap)
695 struct vnode *vp = ap->a_vp;
696 struct vnode *tdvp = ap->a_tdvp;
697 struct componentname *cnp = ap->a_cnp;
702 if ((cnp->cn_flags & HASBUF) == 0)
703 panic("ext2_link: no name");
706 if ((nlink_t)ip->i_nlink >= ext2_max_nlink(ip)) {
710 if (ip->i_flags & (IMMUTABLE | APPEND)) {
715 ip->i_flag |= IN_CHANGE;
716 error = ext2_update(vp, !DOINGASYNC(vp));
718 error = ext2_direnter(ip, tdvp, cnp);
721 ip->i_flag |= IN_CHANGE;
728 ext2_inc_nlink(struct inode *ip)
733 if (ext2_htree_has_idx(ip) && ip->i_nlink > 1) {
734 if (ip->i_nlink >= ext2_max_nlink(ip) || ip->i_nlink == 2)
736 } else if (ip->i_nlink > ext2_max_nlink(ip)) {
745 ext2_dec_nlink(struct inode *ip)
748 if (!S_ISDIR(ip->i_mode) || ip->i_nlink > 2)
753 * Rename system call.
754 * rename("foo", "bar");
757 * link("foo", "bar");
759 * but ``atomically''. Can't do full commit without saving state in the
760 * inode on disk which isn't feasible at this time. Best we can do is
761 * always guarantee the target exists.
763 * Basic algorithm is:
765 * 1) Bump link count on source while we're linking it to the
766 * target. This also ensure the inode won't be deleted out
767 * from underneath us while we work (it may be truncated by
768 * a concurrent `trunc' or `open' for creation).
769 * 2) Link source to destination. If destination already exists,
771 * 3) Unlink source reference to inode if still around. If a
772 * directory was moved and the parent of the destination
773 * is different from the source, patch the ".." entry in the
777 ext2_rename(struct vop_rename_args *ap)
779 struct vnode *tvp = ap->a_tvp;
780 struct vnode *tdvp = ap->a_tdvp;
781 struct vnode *fvp = ap->a_fvp;
782 struct vnode *fdvp = ap->a_fdvp;
783 struct componentname *tcnp = ap->a_tcnp;
784 struct componentname *fcnp = ap->a_fcnp;
785 struct inode *ip, *xp, *dp;
786 struct dirtemplate dirbuf;
787 int doingdirectory = 0, oldparent = 0, newparent = 0;
792 if ((tcnp->cn_flags & HASBUF) == 0 ||
793 (fcnp->cn_flags & HASBUF) == 0)
794 panic("ext2_rename: no name");
797 * Check for cross-device rename.
799 if ((fvp->v_mount != tdvp->v_mount) ||
800 (tvp && (fvp->v_mount != tvp->v_mount))) {
814 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
815 (VTOI(tdvp)->i_flags & APPEND))) {
821 * Renaming a file to itself has no effect. The upper layers should
822 * not call us in that case. Temporarily just warn if they do.
825 printf("ext2_rename: fvp == tvp (can't happen)\n");
830 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
834 if (ip->i_nlink >= ext2_max_nlink(ip) && !ext2_htree_has_idx(ip)) {
839 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
840 || (dp->i_flags & APPEND)) {
845 if ((ip->i_mode & IFMT) == IFDIR) {
847 * Avoid ".", "..", and aliases of "." for obvious reasons.
849 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
850 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
851 (ip->i_flag & IN_RENAME)) {
856 ip->i_flag |= IN_RENAME;
857 oldparent = dp->i_number;
863 * When the target exists, both the directory
864 * and target vnodes are returned locked.
872 * 1) Bump link count while we're moving stuff
873 * around. If we crash somewhere before
874 * completing our work, the link count
875 * may be wrong, but correctable.
878 ip->i_flag |= IN_CHANGE;
879 if ((error = ext2_update(fvp, !DOINGASYNC(fvp))) != 0) {
885 * If ".." must be changed (ie the directory gets a new
886 * parent) then the source directory must not be in the
887 * directory hierarchy above the target, as this would
888 * orphan everything below the source directory. Also
889 * the user must have write permission in the source so
890 * as to be able to change "..". We must repeat the call
891 * to namei, as the parent directory is unlocked by the
892 * call to checkpath().
894 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
896 if (oldparent != dp->i_number)
897 newparent = dp->i_number;
898 if (doingdirectory && newparent) {
899 if (error) /* write access check above */
903 error = ext2_checkpath(ip, dp, tcnp->cn_cred);
907 error = relookup(tdvp, &tvp, tcnp);
917 * 2) If target doesn't exist, link the target
918 * to the source and unlink the source.
919 * Otherwise, rewrite the target directory
920 * entry to reference the source inode and
921 * expunge the original entry's existence.
924 if (dp->i_devvp != ip->i_devvp)
925 panic("ext2_rename: EXDEV");
927 * Account for ".." in new directory.
928 * When source and destination have the same
929 * parent we don't fool with the link count.
931 if (doingdirectory && newparent) {
932 error = ext2_inc_nlink(dp);
936 dp->i_flag |= IN_CHANGE;
937 error = ext2_update(tdvp, !DOINGASYNC(tdvp));
941 error = ext2_direnter(ip, tdvp, tcnp);
943 if (doingdirectory && newparent) {
945 dp->i_flag |= IN_CHANGE;
946 (void)ext2_update(tdvp, 1);
952 if (xp->i_devvp != dp->i_devvp || xp->i_devvp != ip->i_devvp)
953 panic("ext2_rename: EXDEV");
955 * Short circuit rename(foo, foo).
957 if (xp->i_number == ip->i_number)
958 panic("ext2_rename: same file");
960 * If the parent directory is "sticky", then the user must
961 * own the parent directory, or the destination of the rename,
962 * otherwise the destination may not be changed (except by
963 * root). This implements append-only directories.
965 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
966 tcnp->cn_cred->cr_uid != dp->i_uid &&
967 xp->i_uid != tcnp->cn_cred->cr_uid) {
972 * Target must be empty if a directory and have no links
973 * to it. Also, ensure source and target are compatible
974 * (both directories, or both not directories).
976 if ((xp->i_mode & IFMT) == IFDIR) {
977 if (!ext2_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
981 if (!doingdirectory) {
986 } else if (doingdirectory) {
990 error = ext2_dirrewrite(dp, ip, tcnp);
994 * If the target directory is in the same
995 * directory as the source directory,
996 * decrement the link count on the parent
997 * of the target directory.
999 if (doingdirectory && !newparent) {
1001 dp->i_flag |= IN_CHANGE;
1005 * Adjust the link count of the target to
1006 * reflect the dirrewrite above. If this is
1007 * a directory it is empty and there are
1008 * no links to it, so we can squash the inode and
1009 * any space associated with it. We disallowed
1010 * renaming over top of a directory with links to
1011 * it above, as the remaining link would point to
1012 * a directory without "." or ".." entries.
1015 if (doingdirectory) {
1016 if (--xp->i_nlink != 0)
1017 panic("ext2_rename: linked directory");
1018 error = ext2_truncate(tvp, (off_t)0, IO_SYNC,
1019 tcnp->cn_cred, tcnp->cn_thread);
1021 xp->i_flag |= IN_CHANGE;
1027 * 3) Unlink the source.
1029 fcnp->cn_flags &= ~MODMASK;
1030 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1032 error = relookup(fdvp, &fvp, fcnp);
1040 * From name has disappeared. IN_RENAME is not sufficient
1041 * to protect against directory races due to timing windows,
1042 * so we can't panic here.
1048 * Ensure that the directory entry still exists and has not
1049 * changed while the new name has been entered. If the source is
1050 * a file then the entry may have been unlinked or renamed. In
1051 * either case there is no further work to be done. If the source
1052 * is a directory then it cannot have been rmdir'ed; its link
1053 * count of three would cause a rmdir to fail with ENOTEMPTY.
1054 * The IN_RENAME flag ensures that it cannot be moved by another
1059 * From name resolves to a different inode. IN_RENAME is
1060 * not sufficient protection against timing window races
1061 * so we can't panic here.
1065 * If the source is a directory with a
1066 * new parent, the link count of the old
1067 * parent directory must be decremented
1068 * and ".." set to point to the new parent.
1070 if (doingdirectory && newparent) {
1072 dp->i_flag |= IN_CHANGE;
1073 error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
1074 sizeof(struct dirtemplate), (off_t)0,
1075 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1076 tcnp->cn_cred, NOCRED, NULL, NULL);
1078 /* Like ufs little-endian: */
1079 namlen = dirbuf.dotdot_type;
1081 dirbuf.dotdot_name[0] != '.' ||
1082 dirbuf.dotdot_name[1] != '.') {
1083 ext2_dirbad(xp, (doff_t)12,
1084 "rename: mangled dir");
1086 dirbuf.dotdot_ino = newparent;
1087 (void)vn_rdwr(UIO_WRITE, fvp,
1089 sizeof(struct dirtemplate),
1090 (off_t)0, UIO_SYSSPACE,
1091 IO_NODELOCKED | IO_SYNC |
1092 IO_NOMACCHECK, tcnp->cn_cred,
1093 NOCRED, NULL, NULL);
1098 error = ext2_dirremove(fdvp, fcnp);
1101 xp->i_flag |= IN_CHANGE;
1103 xp->i_flag &= ~IN_RENAME;
1118 ip->i_flag &= ~IN_RENAME;
1119 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1121 ip->i_flag |= IN_CHANGE;
1122 ip->i_flag &= ~IN_RENAME;
1131 ext2_do_posix1e_acl_inheritance_dir(struct vnode *dvp, struct vnode *tvp,
1132 mode_t dmode, struct ucred *cred, struct thread *td)
1135 struct inode *ip = VTOI(tvp);
1136 struct acl *dacl, *acl;
1138 acl = acl_alloc(M_WAITOK);
1139 dacl = acl_alloc(M_WAITOK);
1142 * Retrieve default ACL from parent, if any.
1144 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cred, td);
1148 * Retrieved a default ACL, so merge mode and ACL if
1149 * necessary. If the ACL is empty, fall through to
1150 * the "not defined or available" case.
1152 if (acl->acl_cnt != 0) {
1153 dmode = acl_posix1e_newfilemode(dmode, acl);
1156 ext2_sync_acl_from_inode(ip, acl);
1163 * Just use the mode as-is.
1173 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cred, td);
1175 error = VOP_SETACL(tvp, ACL_TYPE_DEFAULT, dacl, cred, td);
1182 * XXX: This should not happen, as EOPNOTSUPP above
1183 * was supposed to free acl.
1186 printf("ext2_mkdir: VOP_GETACL() but no VOP_SETACL()\n");
1202 ext2_do_posix1e_acl_inheritance_file(struct vnode *dvp, struct vnode *tvp,
1203 mode_t mode, struct ucred *cred, struct thread *td)
1206 struct inode *ip = VTOI(tvp);
1209 acl = acl_alloc(M_WAITOK);
1212 * Retrieve default ACL for parent, if any.
1214 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cred, td);
1218 * Retrieved a default ACL, so merge mode and ACL if
1221 if (acl->acl_cnt != 0) {
1223 * Two possible ways for default ACL to not
1224 * be present. First, the EA can be
1225 * undefined, or second, the default ACL can
1226 * be blank. If it's blank, fall through to
1227 * the it's not defined case.
1229 mode = acl_posix1e_newfilemode(mode, acl);
1231 ext2_sync_acl_from_inode(ip, acl);
1238 * Just use the mode as-is.
1248 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cred, td);
1255 * XXX: This should not happen, as EOPNOTSUPP above was
1256 * supposed to free acl.
1258 printf("ufs_do_posix1e_acl_inheritance_file: VOP_GETACL() "
1259 "but no VOP_SETACL()\n");
1260 /* panic("ufs_do_posix1e_acl_inheritance_file: VOP_GETACL() "
1261 "but no VOP_SETACL()"); */
1274 #endif /* UFS_ACL */
1280 ext2_mkdir(struct vop_mkdir_args *ap)
1282 struct vnode *dvp = ap->a_dvp;
1283 struct vattr *vap = ap->a_vap;
1284 struct componentname *cnp = ap->a_cnp;
1285 struct inode *ip, *dp;
1287 struct dirtemplate dirtemplate, *dtp;
1291 if ((cnp->cn_flags & HASBUF) == 0)
1292 panic("ext2_mkdir: no name");
1295 if ((nlink_t)dp->i_nlink >= ext2_max_nlink(dp) &&
1296 !ext2_htree_has_idx(dp)) {
1300 dmode = vap->va_mode & 0777;
1303 * Must simulate part of ext2_makeinode here to acquire the inode,
1304 * but not have it entered in the parent directory. The entry is
1305 * made later after writing "." and ".." entries.
1307 error = ext2_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1311 ip->i_gid = dp->i_gid;
1315 * if we are hacking owners here, (only do this where told to)
1316 * and we are not giving it TOO root, (would subvert quotas)
1317 * then go ahead and give it to the other user.
1318 * The new directory also inherits the SUID bit.
1319 * If user's UID and dir UID are the same,
1320 * 'give it away' so that the SUID is still forced on.
1322 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1323 (dp->i_mode & ISUID) && dp->i_uid) {
1325 ip->i_uid = dp->i_uid;
1327 ip->i_uid = cnp->cn_cred->cr_uid;
1331 ip->i_uid = cnp->cn_cred->cr_uid;
1333 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1335 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1337 if (cnp->cn_flags & ISWHITEOUT)
1338 ip->i_flags |= UF_OPAQUE;
1339 error = ext2_update(tvp, 1);
1342 * Bump link count in parent directory
1343 * to reflect work done below. Should
1344 * be done before reference is created
1345 * so reparation is possible if we crash.
1348 dp->i_flag |= IN_CHANGE;
1349 error = ext2_update(dvp, !DOINGASYNC(dvp));
1353 /* Initialize directory with "." and ".." from static template. */
1354 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
1355 EXT2F_INCOMPAT_FTYPE))
1356 dtp = &mastertemplate;
1358 dtp = &omastertemplate;
1360 dirtemplate.dot_ino = ip->i_number;
1361 dirtemplate.dotdot_ino = dp->i_number;
1363 * note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE so let's
1364 * just redefine it - for this function only
1367 #define DIRBLKSIZ VTOI(dvp)->i_e2fs->e2fs_bsize
1368 dirtemplate.dotdot_reclen = DIRBLKSIZ - 12;
1369 error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
1370 sizeof(dirtemplate), (off_t)0, UIO_SYSSPACE,
1371 IO_NODELOCKED | IO_SYNC | IO_NOMACCHECK, cnp->cn_cred, NOCRED,
1375 dp->i_flag |= IN_CHANGE;
1378 if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1379 /* XXX should grow with balloc() */
1380 panic("ext2_mkdir: blksize");
1382 ip->i_size = DIRBLKSIZ;
1383 ip->i_flag |= IN_CHANGE;
1387 if (dvp->v_mount->mnt_flag & MNT_ACLS) {
1388 error = ext2_do_posix1e_acl_inheritance_dir(dvp, tvp, dmode,
1389 cnp->cn_cred, cnp->cn_thread);
1394 #endif /* UFS_ACL */
1396 /* Directory set up, now install its entry in the parent directory. */
1397 error = ext2_direnter(ip, dvp, cnp);
1400 dp->i_flag |= IN_CHANGE;
1404 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1405 * for us because we set the link count to 0.
1409 ip->i_flag |= IN_CHANGE;
1416 #define DIRBLKSIZ DEV_BSIZE
1420 * Rmdir system call.
1423 ext2_rmdir(struct vop_rmdir_args *ap)
1425 struct vnode *vp = ap->a_vp;
1426 struct vnode *dvp = ap->a_dvp;
1427 struct componentname *cnp = ap->a_cnp;
1428 struct inode *ip, *dp;
1435 * Verify the directory is empty (and valid).
1436 * (Rmdir ".." won't be valid since
1437 * ".." will contain a reference to
1438 * the current directory and thus be
1441 if (!ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1445 if ((dp->i_flags & APPEND)
1446 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1451 * Delete reference to directory before purging
1452 * inode. If we crash in between, the directory
1453 * will be reattached to lost+found,
1455 error = ext2_dirremove(dvp, cnp);
1459 dp->i_flag |= IN_CHANGE;
1463 * Truncate inode. The only stuff left
1464 * in the directory is "." and "..".
1467 error = ext2_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred,
1469 cache_purge(ITOV(ip));
1470 if (vn_lock(dvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1472 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1473 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1480 * symlink -- make a symbolic link
1483 ext2_symlink(struct vop_symlink_args *ap)
1485 struct vnode *vp, **vpp = ap->a_vpp;
1489 error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1494 len = strlen(ap->a_target);
1495 if (len < vp->v_mount->mnt_maxsymlinklen) {
1497 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1499 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1501 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1502 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1503 ap->a_cnp->cn_cred, NOCRED, NULL, NULL);
1510 * Return target name of a symbolic link
1513 ext2_readlink(struct vop_readlink_args *ap)
1515 struct vnode *vp = ap->a_vp;
1516 struct inode *ip = VTOI(vp);
1520 if (isize < vp->v_mount->mnt_maxsymlinklen) {
1521 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1524 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1528 * Calculate the logical to physical mapping if not done already,
1529 * then call the device strategy routine.
1531 * In order to be able to swap to a file, the ext2_bmaparray() operation may not
1532 * deadlock on memory. See ext2_bmap() for details.
1535 ext2_strategy(struct vop_strategy_args *ap)
1537 struct buf *bp = ap->a_bp;
1538 struct vnode *vp = ap->a_vp;
1543 if (vp->v_type == VBLK || vp->v_type == VCHR)
1544 panic("ext2_strategy: spec");
1545 if (bp->b_blkno == bp->b_lblkno) {
1547 if (VTOI(ap->a_vp)->i_flag & IN_E4EXTENTS)
1548 error = ext4_bmapext(vp, bp->b_lblkno, &blkno, NULL, NULL);
1550 error = ext2_bmaparray(vp, bp->b_lblkno, &blkno, NULL, NULL);
1552 bp->b_blkno = blkno;
1554 bp->b_error = error;
1555 bp->b_ioflags |= BIO_ERROR;
1559 if ((long)bp->b_blkno == -1)
1562 if ((long)bp->b_blkno == -1) {
1566 bp->b_iooffset = dbtob(bp->b_blkno);
1567 bo = VFSTOEXT2(vp->v_mount)->um_bo;
1568 BO_STRATEGY(bo, bp);
1573 * Print out the contents of an inode.
1576 ext2_print(struct vop_print_args *ap)
1578 struct vnode *vp = ap->a_vp;
1579 struct inode *ip = VTOI(vp);
1581 vn_printf(ip->i_devvp, "\tino %ju", (uintmax_t)ip->i_number);
1582 if (vp->v_type == VFIFO)
1589 * Close wrapper for fifos.
1591 * Update the times on the inode then do device close.
1594 ext2fifo_close(struct vop_close_args *ap)
1596 struct vnode *vp = ap->a_vp;
1599 if (vp->v_usecount > 1)
1600 ext2_itimes_locked(vp);
1602 return (fifo_specops.vop_close(ap));
1606 * Kqfilter wrapper for fifos.
1608 * Fall through to ext2 kqfilter routines if needed
1611 ext2fifo_kqfilter(struct vop_kqfilter_args *ap)
1615 error = fifo_specops.vop_kqfilter(ap);
1617 error = vfs_kqfilter(ap);
1622 * Return POSIX pathconf information applicable to ext2 filesystems.
1625 ext2_pathconf(struct vop_pathconf_args *ap)
1629 switch (ap->a_name) {
1631 if (ext2_htree_has_idx(VTOI(ap->a_vp)))
1632 *ap->a_retval = INT_MAX;
1634 *ap->a_retval = ext2_max_nlink(VTOI(ap->a_vp));
1641 case _PC_ACL_EXTENDED:
1642 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
1647 case _PC_ACL_PATH_MAX:
1648 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
1649 *ap->a_retval = ACL_MAX_ENTRIES;
1653 #endif /* UFS_ACL */
1655 case _PC_MIN_HOLE_SIZE:
1656 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1664 case _PC_ALLOC_SIZE_MIN:
1665 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
1667 case _PC_FILESIZEBITS:
1670 case _PC_REC_INCR_XFER_SIZE:
1671 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1673 case _PC_REC_MAX_XFER_SIZE:
1674 *ap->a_retval = -1; /* means ``unlimited'' */
1676 case _PC_REC_MIN_XFER_SIZE:
1677 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1679 case _PC_REC_XFER_ALIGN:
1680 *ap->a_retval = PAGE_SIZE;
1682 case _PC_SYMLINK_MAX:
1683 *ap->a_retval = MAXPATHLEN;
1687 error = vop_stdpathconf(ap);
1694 * Vnode operation to remove a named attribute.
1697 ext2_deleteextattr(struct vop_deleteextattr_args *ap)
1700 struct m_ext2fs *fs;
1703 ip = VTOI(ap->a_vp);
1706 if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1707 return (EOPNOTSUPP);
1709 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1710 return (EOPNOTSUPP);
1712 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1713 ap->a_cred, ap->a_td, VWRITE);
1719 if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1720 error = ext2_extattr_inode_delete(ip, ap->a_attrnamespace, ap->a_name);
1721 if (error != ENOATTR)
1726 error = ext2_extattr_block_delete(ip, ap->a_attrnamespace, ap->a_name);
1732 * Vnode operation to retrieve a named extended attribute.
1735 ext2_getextattr(struct vop_getextattr_args *ap)
1738 struct m_ext2fs *fs;
1741 ip = VTOI(ap->a_vp);
1744 if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1745 return (EOPNOTSUPP);
1747 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1748 return (EOPNOTSUPP);
1750 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1751 ap->a_cred, ap->a_td, VREAD);
1755 if (ap->a_size != NULL)
1760 if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1761 error = ext2_extattr_inode_get(ip, ap->a_attrnamespace,
1762 ap->a_name, ap->a_uio, ap->a_size);
1763 if (error != ENOATTR)
1768 error = ext2_extattr_block_get(ip, ap->a_attrnamespace,
1769 ap->a_name, ap->a_uio, ap->a_size);
1775 * Vnode operation to retrieve extended attributes on a vnode.
1778 ext2_listextattr(struct vop_listextattr_args *ap)
1781 struct m_ext2fs *fs;
1784 ip = VTOI(ap->a_vp);
1787 if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1788 return (EOPNOTSUPP);
1790 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1791 return (EOPNOTSUPP);
1793 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1794 ap->a_cred, ap->a_td, VREAD);
1798 if (ap->a_size != NULL)
1801 if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1802 error = ext2_extattr_inode_list(ip, ap->a_attrnamespace,
1803 ap->a_uio, ap->a_size);
1809 error = ext2_extattr_block_list(ip, ap->a_attrnamespace,
1810 ap->a_uio, ap->a_size);
1816 * Vnode operation to set a named attribute.
1819 ext2_setextattr(struct vop_setextattr_args *ap)
1822 struct m_ext2fs *fs;
1825 ip = VTOI(ap->a_vp);
1828 if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1829 return (EOPNOTSUPP);
1831 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1832 return (EOPNOTSUPP);
1834 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1835 ap->a_cred, ap->a_td, VWRITE);
1839 error = ext2_extattr_valid_attrname(ap->a_attrnamespace, ap->a_name);
1843 if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1844 error = ext2_extattr_inode_set(ip, ap->a_attrnamespace,
1845 ap->a_name, ap->a_uio);
1846 if (error != ENOSPC)
1850 error = ext2_extattr_block_set(ip, ap->a_attrnamespace,
1851 ap->a_name, ap->a_uio);
1857 * Vnode pointer to File handle
1861 ext2_vptofh(struct vop_vptofh_args *ap)
1866 ip = VTOI(ap->a_vp);
1867 ufhp = (struct ufid *)ap->a_fhp;
1868 ufhp->ufid_len = sizeof(struct ufid);
1869 ufhp->ufid_ino = ip->i_number;
1870 ufhp->ufid_gen = ip->i_gen;
1875 * Initialize the vnode associated with a new inode, handle aliased
1879 ext2_vinit(struct mount *mntp, struct vop_vector *fifoops, struct vnode **vpp)
1886 vp->v_type = IFTOVT(ip->i_mode);
1887 if (vp->v_type == VFIFO)
1890 if (ip->i_number == EXT2_ROOTINO)
1891 vp->v_vflag |= VV_ROOT;
1892 ip->i_modrev = init_va_filerev();
1898 * Allocate a new inode.
1901 ext2_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
1902 struct componentname *cnp)
1904 struct inode *ip, *pdir;
1910 if ((cnp->cn_flags & HASBUF) == 0)
1911 panic("ext2_makeinode: no name");
1914 if ((mode & IFMT) == 0)
1917 error = ext2_valloc(dvp, mode, cnp->cn_cred, &tvp);
1922 ip->i_gid = pdir->i_gid;
1927 * not the owner of the directory,
1928 * and we are hacking owners here, (only do this where told to)
1929 * and we are not giving it TOO root, (would subvert quotas)
1930 * then go ahead and give it to the other user.
1931 * Note that this drops off the execute bits for security.
1933 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1934 (pdir->i_mode & ISUID) &&
1935 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1936 ip->i_uid = pdir->i_uid;
1939 ip->i_uid = cnp->cn_cred->cr_uid;
1943 ip->i_uid = cnp->cn_cred->cr_uid;
1945 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1947 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
1949 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred)) {
1950 if (priv_check_cred(cnp->cn_cred, PRIV_VFS_RETAINSUGID, 0))
1951 ip->i_mode &= ~ISGID;
1954 if (cnp->cn_flags & ISWHITEOUT)
1955 ip->i_flags |= UF_OPAQUE;
1958 * Make sure inode goes to disk before directory entry.
1960 error = ext2_update(tvp, !DOINGASYNC(tvp));
1965 if (dvp->v_mount->mnt_flag & MNT_ACLS) {
1966 error = ext2_do_posix1e_acl_inheritance_file(dvp, tvp, mode,
1967 cnp->cn_cred, cnp->cn_thread);
1971 #endif /* UFS_ACL */
1973 error = ext2_direnter(ip, dvp, cnp);
1982 * Write error occurred trying to update the inode
1983 * or the directory so must deallocate the inode.
1986 ip->i_flag |= IN_CHANGE;
1992 * Vnode op for reading.
1995 ext2_read(struct vop_read_args *ap)
2000 struct m_ext2fs *fs;
2002 daddr_t lbn, nextlbn;
2004 long size, xfersize, blkoffset;
2005 int error, orig_resid, seqcount;
2010 ioflag = ap->a_ioflag;
2012 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
2016 if (uio->uio_rw != UIO_READ)
2017 panic("%s: mode", "ext2_read");
2019 if (vp->v_type == VLNK) {
2020 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
2021 panic("%s: short symlink", "ext2_read");
2022 } else if (vp->v_type != VREG && vp->v_type != VDIR)
2023 panic("%s: type %d", "ext2_read", vp->v_type);
2025 orig_resid = uio->uio_resid;
2026 KASSERT(orig_resid >= 0, ("ext2_read: uio->uio_resid < 0"));
2027 if (orig_resid == 0)
2029 KASSERT(uio->uio_offset >= 0, ("ext2_read: uio->uio_offset < 0"));
2031 if (uio->uio_offset < ip->i_size &&
2032 uio->uio_offset >= fs->e2fs_maxfilesize)
2035 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
2036 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
2038 lbn = lblkno(fs, uio->uio_offset);
2040 size = blksize(fs, ip, lbn);
2041 blkoffset = blkoff(fs, uio->uio_offset);
2043 xfersize = fs->e2fs_fsize - blkoffset;
2044 if (uio->uio_resid < xfersize)
2045 xfersize = uio->uio_resid;
2046 if (bytesinfile < xfersize)
2047 xfersize = bytesinfile;
2049 if (lblktosize(fs, nextlbn) >= ip->i_size)
2050 error = bread(vp, lbn, size, NOCRED, &bp);
2051 else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
2052 error = cluster_read(vp, ip->i_size, lbn, size,
2053 NOCRED, blkoffset + uio->uio_resid, seqcount,
2055 } else if (seqcount > 1) {
2056 u_int nextsize = blksize(fs, ip, nextlbn);
2058 error = breadn(vp, lbn,
2059 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
2061 error = bread(vp, lbn, size, NOCRED, &bp);
2069 * We should only get non-zero b_resid when an I/O error
2070 * has occurred, which should cause us to break above.
2071 * However, if the short read did not cause an error,
2072 * then we want to ensure that we do not uiomove bad
2073 * or uninitialized data.
2075 size -= bp->b_resid;
2076 if (size < xfersize) {
2081 error = uiomove((char *)bp->b_data + blkoffset,
2082 (int)xfersize, uio);
2085 vfs_bio_brelse(bp, ioflag);
2089 * This can only happen in the case of an error because the loop
2090 * above resets bp to NULL on each iteration and on normal
2091 * completion has not set a new value into it. so it must have come
2092 * from a 'break' statement
2095 vfs_bio_brelse(bp, ioflag);
2097 if ((error == 0 || uio->uio_resid != orig_resid) &&
2098 (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
2099 ip->i_flag |= IN_ACCESS;
2104 ext2_ioctl(struct vop_ioctl_args *ap)
2107 switch (ap->a_command) {
2110 return (vn_bmap_seekhole(ap->a_vp, ap->a_command,
2111 (off_t *)ap->a_data, ap->a_cred));
2118 * Vnode op for writing.
2121 ext2_write(struct vop_write_args *ap)
2126 struct m_ext2fs *fs;
2130 int blkoffset, error, flags, ioflag, resid, size, seqcount, xfersize;
2132 ioflag = ap->a_ioflag;
2136 seqcount = ioflag >> IO_SEQSHIFT;
2140 if (uio->uio_rw != UIO_WRITE)
2141 panic("%s: mode", "ext2_write");
2144 switch (vp->v_type) {
2146 if (ioflag & IO_APPEND)
2147 uio->uio_offset = ip->i_size;
2148 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
2154 /* XXX differs from ffs -- this is called from ext2_mkdir(). */
2155 if ((ioflag & IO_SYNC) == 0)
2156 panic("ext2_write: nonsync dir write");
2159 panic("ext2_write: type %p %d (%jd,%jd)", (void *)vp,
2160 vp->v_type, (intmax_t)uio->uio_offset,
2161 (intmax_t)uio->uio_resid);
2164 KASSERT(uio->uio_resid >= 0, ("ext2_write: uio->uio_resid < 0"));
2165 KASSERT(uio->uio_offset >= 0, ("ext2_write: uio->uio_offset < 0"));
2167 if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->e2fs_maxfilesize)
2170 * Maybe this should be above the vnode op call, but so long as
2171 * file servers have no limits, I don't think it matters.
2173 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
2176 resid = uio->uio_resid;
2178 if (seqcount > BA_SEQMAX)
2179 flags = BA_SEQMAX << BA_SEQSHIFT;
2181 flags = seqcount << BA_SEQSHIFT;
2182 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
2185 for (error = 0; uio->uio_resid > 0;) {
2186 lbn = lblkno(fs, uio->uio_offset);
2187 blkoffset = blkoff(fs, uio->uio_offset);
2188 xfersize = fs->e2fs_fsize - blkoffset;
2189 if (uio->uio_resid < xfersize)
2190 xfersize = uio->uio_resid;
2191 if (uio->uio_offset + xfersize > ip->i_size)
2192 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
2195 * We must perform a read-before-write if the transfer size
2196 * does not cover the entire buffer.
2198 if (fs->e2fs_bsize > xfersize)
2201 flags &= ~BA_CLRBUF;
2202 error = ext2_balloc(ip, lbn, blkoffset + xfersize,
2203 ap->a_cred, &bp, flags);
2207 if ((ioflag & (IO_SYNC | IO_INVAL)) == (IO_SYNC | IO_INVAL))
2208 bp->b_flags |= B_NOCACHE;
2209 if (uio->uio_offset + xfersize > ip->i_size)
2210 ip->i_size = uio->uio_offset + xfersize;
2211 size = blksize(fs, ip, lbn) - bp->b_resid;
2212 if (size < xfersize)
2216 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
2218 * If the buffer is not already filled and we encounter an
2219 * error while trying to fill it, we have to clear out any
2220 * garbage data from the pages instantiated for the buffer.
2221 * If we do not, a failed uiomove() during a write can leave
2222 * the prior contents of the pages exposed to a userland mmap.
2224 * Note that we need only clear buffers with a transfer size
2225 * equal to the block size because buffers with a shorter
2226 * transfer size were cleared above by the call to ext2_balloc()
2227 * with the BA_CLRBUF flag set.
2229 * If the source region for uiomove identically mmaps the
2230 * buffer, uiomove() performed the NOP copy, and the buffer
2231 * content remains valid because the page fault handler
2232 * validated the pages.
2234 if (error != 0 && (bp->b_flags & B_CACHE) == 0 &&
2235 fs->e2fs_bsize == xfersize)
2238 vfs_bio_set_flags(bp, ioflag);
2241 * If IO_SYNC each buffer is written synchronously. Otherwise
2242 * if we have a severe page deficiency write the buffer
2243 * asynchronously. Otherwise try to cluster, and if that
2244 * doesn't do it then either do an async write (if O_DIRECT),
2245 * or a delayed write (if not).
2247 if (ioflag & IO_SYNC) {
2249 } else if (vm_page_count_severe() ||
2250 buf_dirty_count_severe() ||
2251 (ioflag & IO_ASYNC)) {
2252 bp->b_flags |= B_CLUSTEROK;
2254 } else if (xfersize + blkoffset == fs->e2fs_fsize) {
2255 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
2256 bp->b_flags |= B_CLUSTEROK;
2257 cluster_write(vp, bp, ip->i_size, seqcount, 0);
2261 } else if (ioflag & IO_DIRECT) {
2262 bp->b_flags |= B_CLUSTEROK;
2265 bp->b_flags |= B_CLUSTEROK;
2268 if (error || xfersize == 0)
2272 * If we successfully wrote any data, and we are not the superuser
2273 * we clear the setuid and setgid bits as a precaution against
2276 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
2278 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
2279 ip->i_mode &= ~(ISUID | ISGID);
2282 if (ioflag & IO_UNIT) {
2283 (void)ext2_truncate(vp, osize,
2284 ioflag & IO_SYNC, ap->a_cred, uio->uio_td);
2285 uio->uio_offset -= resid - uio->uio_resid;
2286 uio->uio_resid = resid;
2289 if (uio->uio_resid != resid) {
2290 ip->i_flag |= IN_CHANGE | IN_UPDATE;
2291 if (ioflag & IO_SYNC)
2292 error = ext2_update(vp, 1);