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 * Copyright (c) 1982, 1986, 1989, 1993
9 * The Regents of the University of California. All rights reserved.
10 * (c) UNIX System Laboratories, Inc.
11 * All or some portions of this file are derived from material licensed
12 * to the University of California by American Telephone and Telegraph
13 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
14 * the permission of UNIX System Laboratories, Inc.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * @(#)ufs_vnops.c 8.7 (Berkeley) 2/3/94
41 * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
45 #include "opt_suiddir.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/fcntl.h>
51 #include <sys/filio.h>
55 #include <sys/endian.h>
57 #include <sys/rwlock.h>
58 #include <sys/mount.h>
59 #include <sys/unistd.h>
61 #include <sys/vnode.h>
62 #include <sys/namei.h>
63 #include <sys/lockf.h>
64 #include <sys/event.h>
67 #include <sys/extattr.h>
68 #include <sys/vmmeter.h>
71 #include <vm/vm_param.h>
72 #include <vm/vm_extern.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
75 #include <vm/vm_pager.h>
76 #include <vm/vnode_pager.h>
78 #include "opt_directio.h"
80 #include <ufs/ufs/dir.h>
82 #include <fs/ext2fs/fs.h>
83 #include <fs/ext2fs/inode.h>
84 #include <fs/ext2fs/ext2_extern.h>
85 #include <fs/ext2fs/ext2fs.h>
86 #include <fs/ext2fs/ext2_dinode.h>
87 #include <fs/ext2fs/ext2_dir.h>
88 #include <fs/ext2fs/ext2_mount.h>
89 #include <fs/ext2fs/ext2_extattr.h>
91 static int ext2_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
92 static void ext2_itimes_locked(struct vnode *);
93 static int ext4_ext_read(struct vop_read_args *);
94 static int ext2_ind_read(struct vop_read_args *);
96 static vop_access_t ext2_access;
97 static int ext2_chmod(struct vnode *, int, struct ucred *, struct thread *);
98 static int ext2_chown(struct vnode *, uid_t, gid_t, struct ucred *,
100 static vop_close_t ext2_close;
101 static vop_create_t ext2_create;
102 static vop_fsync_t ext2_fsync;
103 static vop_getattr_t ext2_getattr;
104 static vop_ioctl_t ext2_ioctl;
105 static vop_link_t ext2_link;
106 static vop_mkdir_t ext2_mkdir;
107 static vop_mknod_t ext2_mknod;
108 static vop_open_t ext2_open;
109 static vop_pathconf_t ext2_pathconf;
110 static vop_print_t ext2_print;
111 static vop_read_t ext2_read;
112 static vop_readlink_t ext2_readlink;
113 static vop_remove_t ext2_remove;
114 static vop_rename_t ext2_rename;
115 static vop_rmdir_t ext2_rmdir;
116 static vop_setattr_t ext2_setattr;
117 static vop_strategy_t ext2_strategy;
118 static vop_symlink_t ext2_symlink;
119 static vop_write_t ext2_write;
120 static vop_deleteextattr_t ext2_deleteextattr;
121 static vop_getextattr_t ext2_getextattr;
122 static vop_listextattr_t ext2_listextattr;
123 static vop_setextattr_t ext2_setextattr;
124 static vop_vptofh_t ext2_vptofh;
125 static vop_close_t ext2fifo_close;
126 static vop_kqfilter_t ext2fifo_kqfilter;
128 /* Global vfs data structures for ext2. */
129 struct vop_vector ext2_vnodeops = {
130 .vop_default = &default_vnodeops,
131 .vop_access = ext2_access,
132 .vop_bmap = ext2_bmap,
133 .vop_cachedlookup = ext2_lookup,
134 .vop_close = ext2_close,
135 .vop_create = ext2_create,
136 .vop_fsync = ext2_fsync,
137 .vop_getpages = vnode_pager_local_getpages,
138 .vop_getpages_async = vnode_pager_local_getpages_async,
139 .vop_getattr = ext2_getattr,
140 .vop_inactive = ext2_inactive,
141 .vop_ioctl = ext2_ioctl,
142 .vop_link = ext2_link,
143 .vop_lookup = vfs_cache_lookup,
144 .vop_mkdir = ext2_mkdir,
145 .vop_mknod = ext2_mknod,
146 .vop_open = ext2_open,
147 .vop_pathconf = ext2_pathconf,
148 .vop_poll = vop_stdpoll,
149 .vop_print = ext2_print,
150 .vop_read = ext2_read,
151 .vop_readdir = ext2_readdir,
152 .vop_readlink = ext2_readlink,
153 .vop_reallocblks = ext2_reallocblks,
154 .vop_reclaim = ext2_reclaim,
155 .vop_remove = ext2_remove,
156 .vop_rename = ext2_rename,
157 .vop_rmdir = ext2_rmdir,
158 .vop_setattr = ext2_setattr,
159 .vop_strategy = ext2_strategy,
160 .vop_symlink = ext2_symlink,
161 .vop_write = ext2_write,
162 .vop_deleteextattr = ext2_deleteextattr,
163 .vop_getextattr = ext2_getextattr,
164 .vop_listextattr = ext2_listextattr,
165 .vop_setextattr = ext2_setextattr,
166 .vop_vptofh = ext2_vptofh,
169 struct vop_vector ext2_fifoops = {
170 .vop_default = &fifo_specops,
171 .vop_access = ext2_access,
172 .vop_close = ext2fifo_close,
173 .vop_fsync = ext2_fsync,
174 .vop_getattr = ext2_getattr,
175 .vop_inactive = ext2_inactive,
176 .vop_kqfilter = ext2fifo_kqfilter,
177 .vop_print = ext2_print,
178 .vop_read = VOP_PANIC,
179 .vop_reclaim = ext2_reclaim,
180 .vop_setattr = ext2_setattr,
181 .vop_write = VOP_PANIC,
182 .vop_vptofh = ext2_vptofh,
186 * A virgin directory (no blushing please).
187 * Note that the type and namlen fields are reversed relative to ext2.
188 * Also, we don't use `struct odirtemplate', since it would just cause
189 * endianness problems.
191 static struct dirtemplate mastertemplate = {
192 0, 12, 1, EXT2_FT_DIR, ".",
193 0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
195 static struct dirtemplate omastertemplate = {
196 0, 12, 1, EXT2_FT_UNKNOWN, ".",
197 0, DIRBLKSIZ - 12, 2, EXT2_FT_UNKNOWN, ".."
201 ext2_itimes_locked(struct vnode *vp)
206 ASSERT_VI_LOCKED(vp, __func__);
209 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
211 if ((vp->v_type == VBLK || vp->v_type == VCHR))
212 ip->i_flag |= IN_LAZYMOD;
214 ip->i_flag |= IN_MODIFIED;
215 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
217 if (ip->i_flag & IN_ACCESS) {
218 ip->i_atime = ts.tv_sec;
219 ip->i_atimensec = ts.tv_nsec;
221 if (ip->i_flag & IN_UPDATE) {
222 ip->i_mtime = ts.tv_sec;
223 ip->i_mtimensec = ts.tv_nsec;
226 if (ip->i_flag & IN_CHANGE) {
227 ip->i_ctime = ts.tv_sec;
228 ip->i_ctimensec = ts.tv_nsec;
231 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
235 ext2_itimes(struct vnode *vp)
239 ext2_itimes_locked(vp);
244 * Create a regular file
247 ext2_create(struct vop_create_args *ap)
252 ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
253 ap->a_dvp, ap->a_vpp, ap->a_cnp);
256 if ((ap->a_cnp->cn_flags & MAKEENTRY) != 0)
257 cache_enter(ap->a_dvp, *ap->a_vpp, ap->a_cnp);
262 ext2_open(struct vop_open_args *ap)
265 if (ap->a_vp->v_type == VBLK || ap->a_vp->v_type == VCHR)
269 * Files marked append-only must be opened for appending.
271 if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
272 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
275 vnode_create_vobject(ap->a_vp, VTOI(ap->a_vp)->i_size, ap->a_td);
283 * Update the times on the inode.
286 ext2_close(struct vop_close_args *ap)
288 struct vnode *vp = ap->a_vp;
291 if (vp->v_usecount > 1)
292 ext2_itimes_locked(vp);
298 ext2_access(struct vop_access_args *ap)
300 struct vnode *vp = ap->a_vp;
301 struct inode *ip = VTOI(vp);
302 accmode_t accmode = ap->a_accmode;
305 if (vp->v_type == VBLK || vp->v_type == VCHR)
309 * Disallow write attempts on read-only file systems;
310 * unless the file is a socket, fifo, or a block or
311 * character device resident on the file system.
313 if (accmode & VWRITE) {
314 switch (vp->v_type) {
318 if (vp->v_mount->mnt_flag & MNT_RDONLY)
326 /* If immutable bit set, nobody gets to write it. */
327 if ((accmode & VWRITE) && (ip->i_flags & (SF_IMMUTABLE | SF_SNAPSHOT)))
330 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
331 ap->a_accmode, ap->a_cred, NULL);
336 ext2_getattr(struct vop_getattr_args *ap)
338 struct vnode *vp = ap->a_vp;
339 struct inode *ip = VTOI(vp);
340 struct vattr *vap = ap->a_vap;
344 * Copy from inode table
346 vap->va_fsid = dev2udev(ip->i_devvp->v_rdev);
347 vap->va_fileid = ip->i_number;
348 vap->va_mode = ip->i_mode & ~IFMT;
349 vap->va_nlink = ip->i_nlink;
350 vap->va_uid = ip->i_uid;
351 vap->va_gid = ip->i_gid;
352 vap->va_rdev = ip->i_rdev;
353 vap->va_size = ip->i_size;
354 vap->va_atime.tv_sec = ip->i_atime;
355 vap->va_atime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_atimensec : 0;
356 vap->va_mtime.tv_sec = ip->i_mtime;
357 vap->va_mtime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_mtimensec : 0;
358 vap->va_ctime.tv_sec = ip->i_ctime;
359 vap->va_ctime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_ctimensec : 0;
360 if E2DI_HAS_XTIME(ip) {
361 vap->va_birthtime.tv_sec = ip->i_birthtime;
362 vap->va_birthtime.tv_nsec = ip->i_birthnsec;
364 vap->va_flags = ip->i_flags;
365 vap->va_gen = ip->i_gen;
366 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
367 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
368 vap->va_type = IFTOVT(ip->i_mode);
369 vap->va_filerev = ip->i_modrev;
374 * Set attribute vnode op. called from several syscalls
377 ext2_setattr(struct vop_setattr_args *ap)
379 struct vattr *vap = ap->a_vap;
380 struct vnode *vp = ap->a_vp;
381 struct inode *ip = VTOI(vp);
382 struct ucred *cred = ap->a_cred;
383 struct thread *td = curthread;
387 * Check for unsettable attributes.
389 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
390 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
391 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
392 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
395 if (vap->va_flags != VNOVAL) {
396 /* Disallow flags not supported by ext2fs. */
397 if (vap->va_flags & ~(SF_APPEND | SF_IMMUTABLE | UF_NODUMP))
400 if (vp->v_mount->mnt_flag & MNT_RDONLY)
403 * Callers may only modify the file flags on objects they
404 * have VADMIN rights for.
406 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
409 * Unprivileged processes and privileged processes in
410 * jail() are not permitted to unset system flags, or
411 * modify flags if any system flags are set.
412 * Privileged non-jail processes may not modify system flags
413 * if securelevel > 0 and any existing system flags are set.
415 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
416 if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) {
417 error = securelevel_gt(cred, 0);
422 if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND) ||
423 ((vap->va_flags ^ ip->i_flags) & SF_SETTABLE))
426 ip->i_flags = vap->va_flags;
427 ip->i_flag |= IN_CHANGE;
428 if (ip->i_flags & (IMMUTABLE | APPEND))
431 if (ip->i_flags & (IMMUTABLE | APPEND))
434 * Go through the fields and update iff not VNOVAL.
436 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
437 if (vp->v_mount->mnt_flag & MNT_RDONLY)
439 if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred,
443 if (vap->va_size != VNOVAL) {
445 * Disallow write attempts on read-only file systems;
446 * unless the file is a socket, fifo, or a block or
447 * character device resident on the file system.
449 switch (vp->v_type) {
454 if (vp->v_mount->mnt_flag & MNT_RDONLY)
460 if ((error = ext2_truncate(vp, vap->va_size, 0, cred, td)) != 0)
463 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
464 if (vp->v_mount->mnt_flag & MNT_RDONLY)
468 * If times is NULL, ... The caller must be the owner of
469 * the file, have permission to write the file, or be the
471 * If times is non-NULL, ... The caller must be the owner of
472 * the file or be the super-user.
474 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)) &&
475 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
476 (error = VOP_ACCESS(vp, VWRITE, cred, td))))
478 ip->i_flag |= IN_CHANGE | IN_MODIFIED;
479 if (vap->va_atime.tv_sec != VNOVAL) {
480 ip->i_flag &= ~IN_ACCESS;
481 ip->i_atime = vap->va_atime.tv_sec;
482 ip->i_atimensec = vap->va_atime.tv_nsec;
484 if (vap->va_mtime.tv_sec != VNOVAL) {
485 ip->i_flag &= ~IN_UPDATE;
486 ip->i_mtime = vap->va_mtime.tv_sec;
487 ip->i_mtimensec = vap->va_mtime.tv_nsec;
489 ip->i_birthtime = vap->va_birthtime.tv_sec;
490 ip->i_birthnsec = vap->va_birthtime.tv_nsec;
491 error = ext2_update(vp, 0);
496 if (vap->va_mode != (mode_t)VNOVAL) {
497 if (vp->v_mount->mnt_flag & MNT_RDONLY)
499 error = ext2_chmod(vp, (int)vap->va_mode, cred, td);
505 * Change the mode on a file.
506 * Inode must be locked before calling.
509 ext2_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td)
511 struct inode *ip = VTOI(vp);
515 * To modify the permissions on a file, must possess VADMIN
518 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
521 * Privileged processes may set the sticky bit on non-directories,
522 * as well as set the setgid bit on a file with a group that the
523 * process is not a member of.
525 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
526 error = priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0);
530 if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
531 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
535 ip->i_mode &= ~ALLPERMS;
536 ip->i_mode |= (mode & ALLPERMS);
537 ip->i_flag |= IN_CHANGE;
542 * Perform chown operation on inode ip;
543 * inode must be locked prior to call.
546 ext2_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
549 struct inode *ip = VTOI(vp);
554 if (uid == (uid_t)VNOVAL)
556 if (gid == (gid_t)VNOVAL)
559 * To modify the ownership of a file, must possess VADMIN
562 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
565 * To change the owner of a file, or change the group of a file
566 * to a group of which we are not a member, the caller must
569 if (uid != ip->i_uid || (gid != ip->i_gid &&
570 !groupmember(gid, cred))) {
571 error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0);
579 ip->i_flag |= IN_CHANGE;
580 if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
581 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0) != 0)
582 ip->i_mode &= ~(ISUID | ISGID);
588 * Synch an open file.
592 ext2_fsync(struct vop_fsync_args *ap)
595 * Flush all dirty buffers associated with a vnode.
600 return (ext2_update(ap->a_vp, ap->a_waitfor == MNT_WAIT));
608 ext2_mknod(struct vop_mknod_args *ap)
610 struct vattr *vap = ap->a_vap;
611 struct vnode **vpp = ap->a_vpp;
616 error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
617 ap->a_dvp, vpp, ap->a_cnp);
621 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
622 if (vap->va_rdev != VNOVAL) {
624 * Want to be able to use this to make badblock
625 * inodes, so don't truncate the dev number.
627 ip->i_rdev = vap->va_rdev;
630 * Remove inode, then reload it through VFS_VGET so it is
631 * checked to see if it is an alias of an existing entry in
632 * the inode cache. XXX I don't believe this is necessary now.
634 (*vpp)->v_type = VNON;
635 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
638 error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
647 ext2_remove(struct vop_remove_args *ap)
650 struct vnode *vp = ap->a_vp;
651 struct vnode *dvp = ap->a_dvp;
655 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
656 (VTOI(dvp)->i_flags & APPEND)) {
660 error = ext2_dirremove(dvp, ap->a_cnp);
663 ip->i_flag |= IN_CHANGE;
673 ext2_link(struct vop_link_args *ap)
675 struct vnode *vp = ap->a_vp;
676 struct vnode *tdvp = ap->a_tdvp;
677 struct componentname *cnp = ap->a_cnp;
682 if ((cnp->cn_flags & HASBUF) == 0)
683 panic("ext2_link: no name");
686 if ((nlink_t)ip->i_nlink >= EXT2_LINK_MAX) {
690 if (ip->i_flags & (IMMUTABLE | APPEND)) {
695 ip->i_flag |= IN_CHANGE;
696 error = ext2_update(vp, !DOINGASYNC(vp));
698 error = ext2_direnter(ip, tdvp, cnp);
701 ip->i_flag |= IN_CHANGE;
708 * Rename system call.
709 * rename("foo", "bar");
712 * link("foo", "bar");
714 * but ``atomically''. Can't do full commit without saving state in the
715 * inode on disk which isn't feasible at this time. Best we can do is
716 * always guarantee the target exists.
718 * Basic algorithm is:
720 * 1) Bump link count on source while we're linking it to the
721 * target. This also ensure the inode won't be deleted out
722 * from underneath us while we work (it may be truncated by
723 * a concurrent `trunc' or `open' for creation).
724 * 2) Link source to destination. If destination already exists,
726 * 3) Unlink source reference to inode if still around. If a
727 * directory was moved and the parent of the destination
728 * is different from the source, patch the ".." entry in the
732 ext2_rename(struct vop_rename_args *ap)
734 struct vnode *tvp = ap->a_tvp;
735 struct vnode *tdvp = ap->a_tdvp;
736 struct vnode *fvp = ap->a_fvp;
737 struct vnode *fdvp = ap->a_fdvp;
738 struct componentname *tcnp = ap->a_tcnp;
739 struct componentname *fcnp = ap->a_fcnp;
740 struct inode *ip, *xp, *dp;
741 struct dirtemplate dirbuf;
742 int doingdirectory = 0, oldparent = 0, newparent = 0;
747 if ((tcnp->cn_flags & HASBUF) == 0 ||
748 (fcnp->cn_flags & HASBUF) == 0)
749 panic("ext2_rename: no name");
752 * Check for cross-device rename.
754 if ((fvp->v_mount != tdvp->v_mount) ||
755 (tvp && (fvp->v_mount != tvp->v_mount))) {
769 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
770 (VTOI(tdvp)->i_flags & APPEND))) {
776 * Renaming a file to itself has no effect. The upper layers should
777 * not call us in that case. Temporarily just warn if they do.
780 printf("ext2_rename: fvp == tvp (can't happen)\n");
785 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
789 if (ip->i_nlink >= EXT2_LINK_MAX) {
794 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
795 || (dp->i_flags & APPEND)) {
800 if ((ip->i_mode & IFMT) == IFDIR) {
802 * Avoid ".", "..", and aliases of "." for obvious reasons.
804 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
805 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
806 (ip->i_flag & IN_RENAME)) {
811 ip->i_flag |= IN_RENAME;
812 oldparent = dp->i_number;
818 * When the target exists, both the directory
819 * and target vnodes are returned locked.
827 * 1) Bump link count while we're moving stuff
828 * around. If we crash somewhere before
829 * completing our work, the link count
830 * may be wrong, but correctable.
833 ip->i_flag |= IN_CHANGE;
834 if ((error = ext2_update(fvp, !DOINGASYNC(fvp))) != 0) {
840 * If ".." must be changed (ie the directory gets a new
841 * parent) then the source directory must not be in the
842 * directory hierarchy above the target, as this would
843 * orphan everything below the source directory. Also
844 * the user must have write permission in the source so
845 * as to be able to change "..". We must repeat the call
846 * to namei, as the parent directory is unlocked by the
847 * call to checkpath().
849 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
851 if (oldparent != dp->i_number)
852 newparent = dp->i_number;
853 if (doingdirectory && newparent) {
854 if (error) /* write access check above */
858 error = ext2_checkpath(ip, dp, tcnp->cn_cred);
862 error = relookup(tdvp, &tvp, tcnp);
872 * 2) If target doesn't exist, link the target
873 * to the source and unlink the source.
874 * Otherwise, rewrite the target directory
875 * entry to reference the source inode and
876 * expunge the original entry's existence.
879 if (dp->i_devvp != ip->i_devvp)
880 panic("ext2_rename: EXDEV");
882 * Account for ".." in new directory.
883 * When source and destination have the same
884 * parent we don't fool with the link count.
886 if (doingdirectory && newparent) {
887 if ((nlink_t)dp->i_nlink >= EXT2_LINK_MAX) {
892 dp->i_flag |= IN_CHANGE;
893 error = ext2_update(tdvp, !DOINGASYNC(tdvp));
897 error = ext2_direnter(ip, tdvp, tcnp);
899 if (doingdirectory && newparent) {
901 dp->i_flag |= IN_CHANGE;
902 (void)ext2_update(tdvp, 1);
908 if (xp->i_devvp != dp->i_devvp || xp->i_devvp != ip->i_devvp)
909 panic("ext2_rename: EXDEV");
911 * Short circuit rename(foo, foo).
913 if (xp->i_number == ip->i_number)
914 panic("ext2_rename: same file");
916 * If the parent directory is "sticky", then the user must
917 * own the parent directory, or the destination of the rename,
918 * otherwise the destination may not be changed (except by
919 * root). This implements append-only directories.
921 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
922 tcnp->cn_cred->cr_uid != dp->i_uid &&
923 xp->i_uid != tcnp->cn_cred->cr_uid) {
928 * Target must be empty if a directory and have no links
929 * to it. Also, ensure source and target are compatible
930 * (both directories, or both not directories).
932 if ((xp->i_mode & IFMT) == IFDIR) {
933 if (!ext2_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
938 if (!doingdirectory) {
943 } else if (doingdirectory) {
947 error = ext2_dirrewrite(dp, ip, tcnp);
951 * If the target directory is in the same
952 * directory as the source directory,
953 * decrement the link count on the parent
954 * of the target directory.
956 if (doingdirectory && !newparent) {
958 dp->i_flag |= IN_CHANGE;
962 * Adjust the link count of the target to
963 * reflect the dirrewrite above. If this is
964 * a directory it is empty and there are
965 * no links to it, so we can squash the inode and
966 * any space associated with it. We disallowed
967 * renaming over top of a directory with links to
968 * it above, as the remaining link would point to
969 * a directory without "." or ".." entries.
972 if (doingdirectory) {
973 if (--xp->i_nlink != 0)
974 panic("ext2_rename: linked directory");
975 error = ext2_truncate(tvp, (off_t)0, IO_SYNC,
976 tcnp->cn_cred, tcnp->cn_thread);
978 xp->i_flag |= IN_CHANGE;
984 * 3) Unlink the source.
986 fcnp->cn_flags &= ~MODMASK;
987 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
989 error = relookup(fdvp, &fvp, fcnp);
997 * From name has disappeared. IN_RENAME is not sufficient
998 * to protect against directory races due to timing windows,
999 * so we can't panic here.
1005 * Ensure that the directory entry still exists and has not
1006 * changed while the new name has been entered. If the source is
1007 * a file then the entry may have been unlinked or renamed. In
1008 * either case there is no further work to be done. If the source
1009 * is a directory then it cannot have been rmdir'ed; its link
1010 * count of three would cause a rmdir to fail with ENOTEMPTY.
1011 * The IN_RENAME flag ensures that it cannot be moved by another
1016 * From name resolves to a different inode. IN_RENAME is
1017 * not sufficient protection against timing window races
1018 * so we can't panic here.
1022 * If the source is a directory with a
1023 * new parent, the link count of the old
1024 * parent directory must be decremented
1025 * and ".." set to point to the new parent.
1027 if (doingdirectory && newparent) {
1029 dp->i_flag |= IN_CHANGE;
1030 error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
1031 sizeof(struct dirtemplate), (off_t)0,
1032 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1033 tcnp->cn_cred, NOCRED, NULL, NULL);
1035 /* Like ufs little-endian: */
1036 namlen = dirbuf.dotdot_type;
1038 dirbuf.dotdot_name[0] != '.' ||
1039 dirbuf.dotdot_name[1] != '.') {
1040 ext2_dirbad(xp, (doff_t)12,
1041 "rename: mangled dir");
1043 dirbuf.dotdot_ino = newparent;
1044 (void)vn_rdwr(UIO_WRITE, fvp,
1046 sizeof(struct dirtemplate),
1047 (off_t)0, UIO_SYSSPACE,
1048 IO_NODELOCKED | IO_SYNC |
1049 IO_NOMACCHECK, tcnp->cn_cred,
1050 NOCRED, NULL, NULL);
1055 error = ext2_dirremove(fdvp, fcnp);
1058 xp->i_flag |= IN_CHANGE;
1060 xp->i_flag &= ~IN_RENAME;
1075 ip->i_flag &= ~IN_RENAME;
1076 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1078 ip->i_flag |= IN_CHANGE;
1079 ip->i_flag &= ~IN_RENAME;
1090 ext2_mkdir(struct vop_mkdir_args *ap)
1092 struct vnode *dvp = ap->a_dvp;
1093 struct vattr *vap = ap->a_vap;
1094 struct componentname *cnp = ap->a_cnp;
1095 struct inode *ip, *dp;
1097 struct dirtemplate dirtemplate, *dtp;
1101 if ((cnp->cn_flags & HASBUF) == 0)
1102 panic("ext2_mkdir: no name");
1105 if ((nlink_t)dp->i_nlink >= EXT2_LINK_MAX) {
1109 dmode = vap->va_mode & 0777;
1112 * Must simulate part of ext2_makeinode here to acquire the inode,
1113 * but not have it entered in the parent directory. The entry is
1114 * made later after writing "." and ".." entries.
1116 error = ext2_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1120 ip->i_gid = dp->i_gid;
1124 * if we are hacking owners here, (only do this where told to)
1125 * and we are not giving it TOO root, (would subvert quotas)
1126 * then go ahead and give it to the other user.
1127 * The new directory also inherits the SUID bit.
1128 * If user's UID and dir UID are the same,
1129 * 'give it away' so that the SUID is still forced on.
1131 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1132 (dp->i_mode & ISUID) && dp->i_uid) {
1134 ip->i_uid = dp->i_uid;
1136 ip->i_uid = cnp->cn_cred->cr_uid;
1140 ip->i_uid = cnp->cn_cred->cr_uid;
1142 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1144 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1146 if (cnp->cn_flags & ISWHITEOUT)
1147 ip->i_flags |= UF_OPAQUE;
1148 error = ext2_update(tvp, 1);
1151 * Bump link count in parent directory
1152 * to reflect work done below. Should
1153 * be done before reference is created
1154 * so reparation is possible if we crash.
1157 dp->i_flag |= IN_CHANGE;
1158 error = ext2_update(dvp, !DOINGASYNC(dvp));
1162 /* Initialize directory with "." and ".." from static template. */
1163 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
1164 EXT2F_INCOMPAT_FTYPE))
1165 dtp = &mastertemplate;
1167 dtp = &omastertemplate;
1169 dirtemplate.dot_ino = ip->i_number;
1170 dirtemplate.dotdot_ino = dp->i_number;
1172 * note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE so let's
1173 * just redefine it - for this function only
1176 #define DIRBLKSIZ VTOI(dvp)->i_e2fs->e2fs_bsize
1177 dirtemplate.dotdot_reclen = DIRBLKSIZ - 12;
1178 error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
1179 sizeof(dirtemplate), (off_t)0, UIO_SYSSPACE,
1180 IO_NODELOCKED | IO_SYNC | IO_NOMACCHECK, cnp->cn_cred, NOCRED,
1184 dp->i_flag |= IN_CHANGE;
1187 if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1188 /* XXX should grow with balloc() */
1189 panic("ext2_mkdir: blksize");
1191 ip->i_size = DIRBLKSIZ;
1192 ip->i_flag |= IN_CHANGE;
1195 /* Directory set up, now install its entry in the parent directory. */
1196 error = ext2_direnter(ip, dvp, cnp);
1199 dp->i_flag |= IN_CHANGE;
1203 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1204 * for us because we set the link count to 0.
1208 ip->i_flag |= IN_CHANGE;
1215 #define DIRBLKSIZ DEV_BSIZE
1219 * Rmdir system call.
1222 ext2_rmdir(struct vop_rmdir_args *ap)
1224 struct vnode *vp = ap->a_vp;
1225 struct vnode *dvp = ap->a_dvp;
1226 struct componentname *cnp = ap->a_cnp;
1227 struct inode *ip, *dp;
1234 * Verify the directory is empty (and valid).
1235 * (Rmdir ".." won't be valid since
1236 * ".." will contain a reference to
1237 * the current directory and thus be
1240 if (ip->i_nlink != 2 || !ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1244 if ((dp->i_flags & APPEND)
1245 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1250 * Delete reference to directory before purging
1251 * inode. If we crash in between, the directory
1252 * will be reattached to lost+found,
1254 error = ext2_dirremove(dvp, cnp);
1258 dp->i_flag |= IN_CHANGE;
1262 * Truncate inode. The only stuff left
1263 * in the directory is "." and "..". The
1264 * "." reference is inconsequential since
1265 * we're quashing it. The ".." reference
1266 * has already been adjusted above. We've
1267 * removed the "." reference and the reference
1268 * in the parent directory, but there may be
1269 * other hard links so decrement by 2 and
1270 * worry about them later.
1273 error = ext2_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred,
1275 cache_purge(ITOV(ip));
1276 if (vn_lock(dvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1278 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1279 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1286 * symlink -- make a symbolic link
1289 ext2_symlink(struct vop_symlink_args *ap)
1291 struct vnode *vp, **vpp = ap->a_vpp;
1295 error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1300 len = strlen(ap->a_target);
1301 if (len < vp->v_mount->mnt_maxsymlinklen) {
1303 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1305 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1307 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1308 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1309 ap->a_cnp->cn_cred, NOCRED, NULL, NULL);
1316 * Return target name of a symbolic link
1319 ext2_readlink(struct vop_readlink_args *ap)
1321 struct vnode *vp = ap->a_vp;
1322 struct inode *ip = VTOI(vp);
1326 if (isize < vp->v_mount->mnt_maxsymlinklen) {
1327 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1330 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1334 * Calculate the logical to physical mapping if not done already,
1335 * then call the device strategy routine.
1337 * In order to be able to swap to a file, the ext2_bmaparray() operation may not
1338 * deadlock on memory. See ext2_bmap() for details.
1341 ext2_strategy(struct vop_strategy_args *ap)
1343 struct buf *bp = ap->a_bp;
1344 struct vnode *vp = ap->a_vp;
1349 if (vp->v_type == VBLK || vp->v_type == VCHR)
1350 panic("ext2_strategy: spec");
1351 if (bp->b_blkno == bp->b_lblkno) {
1352 error = ext2_bmaparray(vp, bp->b_lblkno, &blkno, NULL, NULL);
1353 bp->b_blkno = blkno;
1355 bp->b_error = error;
1356 bp->b_ioflags |= BIO_ERROR;
1360 if ((long)bp->b_blkno == -1)
1363 if ((long)bp->b_blkno == -1) {
1367 bp->b_iooffset = dbtob(bp->b_blkno);
1368 bo = VFSTOEXT2(vp->v_mount)->um_bo;
1369 BO_STRATEGY(bo, bp);
1374 * Print out the contents of an inode.
1377 ext2_print(struct vop_print_args *ap)
1379 struct vnode *vp = ap->a_vp;
1380 struct inode *ip = VTOI(vp);
1382 vn_printf(ip->i_devvp, "\tino %ju", (uintmax_t)ip->i_number);
1383 if (vp->v_type == VFIFO)
1390 * Close wrapper for fifos.
1392 * Update the times on the inode then do device close.
1395 ext2fifo_close(struct vop_close_args *ap)
1397 struct vnode *vp = ap->a_vp;
1400 if (vp->v_usecount > 1)
1401 ext2_itimes_locked(vp);
1403 return (fifo_specops.vop_close(ap));
1407 * Kqfilter wrapper for fifos.
1409 * Fall through to ext2 kqfilter routines if needed
1412 ext2fifo_kqfilter(struct vop_kqfilter_args *ap)
1416 error = fifo_specops.vop_kqfilter(ap);
1418 error = vfs_kqfilter(ap);
1423 * Return POSIX pathconf information applicable to ext2 filesystems.
1426 ext2_pathconf(struct vop_pathconf_args *ap)
1430 switch (ap->a_name) {
1432 *ap->a_retval = EXT2_LINK_MAX;
1435 *ap->a_retval = NAME_MAX;
1438 *ap->a_retval = PATH_MAX;
1441 *ap->a_retval = PIPE_BUF;
1443 case _PC_CHOWN_RESTRICTED:
1449 case _PC_MIN_HOLE_SIZE:
1450 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1453 /* _PC_ASYNC_IO should have been handled by upper layers. */
1454 KASSERT(0, ("_PC_ASYNC_IO should not get here"));
1463 case _PC_ALLOC_SIZE_MIN:
1464 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
1466 case _PC_FILESIZEBITS:
1469 case _PC_REC_INCR_XFER_SIZE:
1470 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1472 case _PC_REC_MAX_XFER_SIZE:
1473 *ap->a_retval = -1; /* means ``unlimited'' */
1475 case _PC_REC_MIN_XFER_SIZE:
1476 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1478 case _PC_REC_XFER_ALIGN:
1479 *ap->a_retval = PAGE_SIZE;
1481 case _PC_SYMLINK_MAX:
1482 *ap->a_retval = MAXPATHLEN;
1493 * Vnode operation to remove a named attribute.
1496 ext2_deleteextattr(struct vop_deleteextattr_args *ap)
1499 struct m_ext2fs *fs;
1502 ip = VTOI(ap->a_vp);
1505 if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1506 return (EOPNOTSUPP);
1508 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1509 return (EOPNOTSUPP);
1511 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1512 ap->a_cred, ap->a_td, VWRITE);
1516 if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1517 error = ext2_extattr_inode_delete(ip, ap->a_attrnamespace, ap->a_name);
1518 if (error != ENOATTR)
1523 error = ext2_extattr_block_delete(ip, ap->a_attrnamespace, ap->a_name);
1529 * Vnode operation to retrieve a named extended attribute.
1532 ext2_getextattr(struct vop_getextattr_args *ap)
1535 struct m_ext2fs *fs;
1538 ip = VTOI(ap->a_vp);
1541 if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1542 return (EOPNOTSUPP);
1544 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1545 return (EOPNOTSUPP);
1547 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1548 ap->a_cred, ap->a_td, VREAD);
1552 if (ap->a_size != NULL)
1555 if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1556 error = ext2_extattr_inode_get(ip, ap->a_attrnamespace,
1557 ap->a_name, ap->a_uio, ap->a_size);
1558 if (error != ENOATTR)
1563 error = ext2_extattr_block_get(ip, ap->a_attrnamespace,
1564 ap->a_name, ap->a_uio, ap->a_size);
1570 * Vnode operation to retrieve extended attributes on a vnode.
1573 ext2_listextattr(struct vop_listextattr_args *ap)
1576 struct m_ext2fs *fs;
1579 ip = VTOI(ap->a_vp);
1582 if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1583 return (EOPNOTSUPP);
1585 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1586 return (EOPNOTSUPP);
1588 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1589 ap->a_cred, ap->a_td, VREAD);
1593 if (ap->a_size != NULL)
1596 if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1597 error = ext2_extattr_inode_list(ip, ap->a_attrnamespace,
1598 ap->a_uio, ap->a_size);
1604 error = ext2_extattr_block_list(ip, ap->a_attrnamespace,
1605 ap->a_uio, ap->a_size);
1611 * Vnode operation to set a named attribute.
1614 ext2_setextattr(struct vop_setextattr_args *ap)
1617 struct m_ext2fs *fs;
1620 ip = VTOI(ap->a_vp);
1623 if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1624 return (EOPNOTSUPP);
1626 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1627 return (EOPNOTSUPP);
1629 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1630 ap->a_cred, ap->a_td, VWRITE);
1634 error = ext2_extattr_valid_attrname(ap->a_attrnamespace, ap->a_name);
1638 if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1639 error = ext2_extattr_inode_set(ip, ap->a_attrnamespace,
1640 ap->a_name, ap->a_uio);
1641 if (error != ENOSPC)
1645 error = ext2_extattr_block_set(ip, ap->a_attrnamespace,
1646 ap->a_name, ap->a_uio);
1652 * Vnode pointer to File handle
1656 ext2_vptofh(struct vop_vptofh_args *ap)
1661 ip = VTOI(ap->a_vp);
1662 ufhp = (struct ufid *)ap->a_fhp;
1663 ufhp->ufid_len = sizeof(struct ufid);
1664 ufhp->ufid_ino = ip->i_number;
1665 ufhp->ufid_gen = ip->i_gen;
1670 * Initialize the vnode associated with a new inode, handle aliased
1674 ext2_vinit(struct mount *mntp, struct vop_vector *fifoops, struct vnode **vpp)
1681 vp->v_type = IFTOVT(ip->i_mode);
1682 if (vp->v_type == VFIFO)
1685 if (ip->i_number == EXT2_ROOTINO)
1686 vp->v_vflag |= VV_ROOT;
1687 ip->i_modrev = init_va_filerev();
1693 * Allocate a new inode.
1696 ext2_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
1697 struct componentname *cnp)
1699 struct inode *ip, *pdir;
1705 if ((cnp->cn_flags & HASBUF) == 0)
1706 panic("ext2_makeinode: no name");
1709 if ((mode & IFMT) == 0)
1712 error = ext2_valloc(dvp, mode, cnp->cn_cred, &tvp);
1717 ip->i_gid = pdir->i_gid;
1722 * not the owner of the directory,
1723 * and we are hacking owners here, (only do this where told to)
1724 * and we are not giving it TOO root, (would subvert quotas)
1725 * then go ahead and give it to the other user.
1726 * Note that this drops off the execute bits for security.
1728 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1729 (pdir->i_mode & ISUID) &&
1730 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1731 ip->i_uid = pdir->i_uid;
1734 ip->i_uid = cnp->cn_cred->cr_uid;
1738 ip->i_uid = cnp->cn_cred->cr_uid;
1740 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1742 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
1744 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred)) {
1745 if (priv_check_cred(cnp->cn_cred, PRIV_VFS_RETAINSUGID, 0))
1746 ip->i_mode &= ~ISGID;
1749 if (cnp->cn_flags & ISWHITEOUT)
1750 ip->i_flags |= UF_OPAQUE;
1753 * Make sure inode goes to disk before directory entry.
1755 error = ext2_update(tvp, !DOINGASYNC(tvp));
1758 error = ext2_direnter(ip, dvp, cnp);
1767 * Write error occurred trying to update the inode
1768 * or the directory so must deallocate the inode.
1771 ip->i_flag |= IN_CHANGE;
1777 * Vnode op for reading.
1780 ext2_read(struct vop_read_args *ap)
1789 /* EXT4_EXT_LOCK(ip); */
1790 if (ip->i_flag & IN_E4EXTENTS)
1791 error = ext4_ext_read(ap);
1793 error = ext2_ind_read(ap);
1794 /* EXT4_EXT_UNLOCK(ip); */
1799 * Vnode op for reading.
1802 ext2_ind_read(struct vop_read_args *ap)
1807 struct m_ext2fs *fs;
1809 daddr_t lbn, nextlbn;
1811 long size, xfersize, blkoffset;
1812 int error, orig_resid, seqcount;
1817 ioflag = ap->a_ioflag;
1819 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
1823 if (uio->uio_rw != UIO_READ)
1824 panic("%s: mode", "ext2_read");
1826 if (vp->v_type == VLNK) {
1827 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
1828 panic("%s: short symlink", "ext2_read");
1829 } else if (vp->v_type != VREG && vp->v_type != VDIR)
1830 panic("%s: type %d", "ext2_read", vp->v_type);
1832 orig_resid = uio->uio_resid;
1833 KASSERT(orig_resid >= 0, ("ext2_read: uio->uio_resid < 0"));
1834 if (orig_resid == 0)
1836 KASSERT(uio->uio_offset >= 0, ("ext2_read: uio->uio_offset < 0"));
1838 if (uio->uio_offset < ip->i_size &&
1839 uio->uio_offset >= fs->e2fs_maxfilesize)
1842 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
1843 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
1845 lbn = lblkno(fs, uio->uio_offset);
1847 size = blksize(fs, ip, lbn);
1848 blkoffset = blkoff(fs, uio->uio_offset);
1850 xfersize = fs->e2fs_fsize - blkoffset;
1851 if (uio->uio_resid < xfersize)
1852 xfersize = uio->uio_resid;
1853 if (bytesinfile < xfersize)
1854 xfersize = bytesinfile;
1856 if (lblktosize(fs, nextlbn) >= ip->i_size)
1857 error = bread(vp, lbn, size, NOCRED, &bp);
1858 else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
1859 error = cluster_read(vp, ip->i_size, lbn, size,
1860 NOCRED, blkoffset + uio->uio_resid, seqcount,
1862 } else if (seqcount > 1) {
1863 u_int nextsize = blksize(fs, ip, nextlbn);
1865 error = breadn(vp, lbn,
1866 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
1868 error = bread(vp, lbn, size, NOCRED, &bp);
1876 * We should only get non-zero b_resid when an I/O error
1877 * has occurred, which should cause us to break above.
1878 * However, if the short read did not cause an error,
1879 * then we want to ensure that we do not uiomove bad
1880 * or uninitialized data.
1882 size -= bp->b_resid;
1883 if (size < xfersize) {
1888 error = uiomove((char *)bp->b_data + blkoffset,
1889 (int)xfersize, uio);
1892 vfs_bio_brelse(bp, ioflag);
1896 * This can only happen in the case of an error because the loop
1897 * above resets bp to NULL on each iteration and on normal
1898 * completion has not set a new value into it. so it must have come
1899 * from a 'break' statement
1902 vfs_bio_brelse(bp, ioflag);
1904 if ((error == 0 || uio->uio_resid != orig_resid) &&
1905 (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
1906 ip->i_flag |= IN_ACCESS;
1911 ext2_ioctl(struct vop_ioctl_args *ap)
1914 switch (ap->a_command) {
1917 return (vn_bmap_seekhole(ap->a_vp, ap->a_command,
1918 (off_t *)ap->a_data, ap->a_cred));
1925 * this function handles ext4 extents block mapping
1928 ext4_ext_read(struct vop_read_args *ap)
1930 static unsigned char zeroes[EXT2_MAX_BLOCK_SIZE];
1934 struct m_ext2fs *fs;
1936 struct ext4_extent nex, *ep;
1937 struct ext4_extent_path path;
1938 daddr_t lbn, newblk;
1943 long size, xfersize, blkoffset;
1948 memset(&path, 0, sizeof(path));
1950 orig_resid = uio->uio_resid;
1951 KASSERT(orig_resid >= 0, ("%s: uio->uio_resid < 0", __func__));
1952 if (orig_resid == 0)
1954 KASSERT(uio->uio_offset >= 0, ("%s: uio->uio_offset < 0", __func__));
1956 if (uio->uio_offset < ip->i_size && uio->uio_offset >= fs->e2fs_maxfilesize)
1959 while (uio->uio_resid > 0) {
1960 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
1962 lbn = lblkno(fs, uio->uio_offset);
1963 size = blksize(fs, ip, lbn);
1964 blkoffset = blkoff(fs, uio->uio_offset);
1966 xfersize = fs->e2fs_fsize - blkoffset;
1967 xfersize = MIN(xfersize, uio->uio_resid);
1968 xfersize = MIN(xfersize, bytesinfile);
1970 /* get block from ext4 extent cache */
1971 cache_type = ext4_ext_in_cache(ip, lbn, &nex);
1972 switch (cache_type) {
1973 case EXT4_EXT_CACHE_NO:
1974 ext4_ext_find_extent(fs, ip, lbn, &path);
1975 if (path.ep_is_sparse)
1976 ep = &path.ep_sparse_ext;
1982 ext4_ext_put_cache(ip, ep,
1983 path.ep_is_sparse ? EXT4_EXT_CACHE_GAP : EXT4_EXT_CACHE_IN);
1985 newblk = lbn - ep->e_blk + (ep->e_start_lo |
1986 (daddr_t)ep->e_start_hi << 32);
1988 if (path.ep_bp != NULL) {
1994 case EXT4_EXT_CACHE_GAP:
1995 /* block has not been allocated yet */
1998 case EXT4_EXT_CACHE_IN:
1999 newblk = lbn - nex.e_blk + (nex.e_start_lo |
2000 (daddr_t)nex.e_start_hi << 32);
2004 panic("%s: invalid cache type", __func__);
2007 if (cache_type == EXT4_EXT_CACHE_GAP ||
2008 (cache_type == EXT4_EXT_CACHE_NO && path.ep_is_sparse)) {
2009 if (xfersize > sizeof(zeroes))
2010 xfersize = sizeof(zeroes);
2011 error = uiomove(zeroes, xfersize, uio);
2015 error = bread(ip->i_devvp, fsbtodb(fs, newblk), size,
2022 size -= bp->b_resid;
2023 if (size < xfersize) {
2030 error = uiomove(bp->b_data + blkoffset, xfersize, uio);
2041 * Vnode op for writing.
2044 ext2_write(struct vop_write_args *ap)
2049 struct m_ext2fs *fs;
2053 int blkoffset, error, flags, ioflag, resid, size, seqcount, xfersize;
2055 ioflag = ap->a_ioflag;
2059 seqcount = ioflag >> IO_SEQSHIFT;
2063 if (uio->uio_rw != UIO_WRITE)
2064 panic("%s: mode", "ext2_write");
2067 switch (vp->v_type) {
2069 if (ioflag & IO_APPEND)
2070 uio->uio_offset = ip->i_size;
2071 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
2077 /* XXX differs from ffs -- this is called from ext2_mkdir(). */
2078 if ((ioflag & IO_SYNC) == 0)
2079 panic("ext2_write: nonsync dir write");
2082 panic("ext2_write: type %p %d (%jd,%jd)", (void *)vp,
2083 vp->v_type, (intmax_t)uio->uio_offset,
2084 (intmax_t)uio->uio_resid);
2087 KASSERT(uio->uio_resid >= 0, ("ext2_write: uio->uio_resid < 0"));
2088 KASSERT(uio->uio_offset >= 0, ("ext2_write: uio->uio_offset < 0"));
2090 if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->e2fs_maxfilesize)
2093 * Maybe this should be above the vnode op call, but so long as
2094 * file servers have no limits, I don't think it matters.
2096 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
2099 resid = uio->uio_resid;
2101 if (seqcount > BA_SEQMAX)
2102 flags = BA_SEQMAX << BA_SEQSHIFT;
2104 flags = seqcount << BA_SEQSHIFT;
2105 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
2108 for (error = 0; uio->uio_resid > 0;) {
2109 lbn = lblkno(fs, uio->uio_offset);
2110 blkoffset = blkoff(fs, uio->uio_offset);
2111 xfersize = fs->e2fs_fsize - blkoffset;
2112 if (uio->uio_resid < xfersize)
2113 xfersize = uio->uio_resid;
2114 if (uio->uio_offset + xfersize > ip->i_size)
2115 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
2118 * We must perform a read-before-write if the transfer size
2119 * does not cover the entire buffer.
2121 if (fs->e2fs_bsize > xfersize)
2124 flags &= ~BA_CLRBUF;
2125 error = ext2_balloc(ip, lbn, blkoffset + xfersize,
2126 ap->a_cred, &bp, flags);
2130 if ((ioflag & (IO_SYNC | IO_INVAL)) == (IO_SYNC | IO_INVAL))
2131 bp->b_flags |= B_NOCACHE;
2132 if (uio->uio_offset + xfersize > ip->i_size)
2133 ip->i_size = uio->uio_offset + xfersize;
2134 size = blksize(fs, ip, lbn) - bp->b_resid;
2135 if (size < xfersize)
2139 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
2141 * If the buffer is not already filled and we encounter an
2142 * error while trying to fill it, we have to clear out any
2143 * garbage data from the pages instantiated for the buffer.
2144 * If we do not, a failed uiomove() during a write can leave
2145 * the prior contents of the pages exposed to a userland mmap.
2147 * Note that we need only clear buffers with a transfer size
2148 * equal to the block size because buffers with a shorter
2149 * transfer size were cleared above by the call to ext2_balloc()
2150 * with the BA_CLRBUF flag set.
2152 * If the source region for uiomove identically mmaps the
2153 * buffer, uiomove() performed the NOP copy, and the buffer
2154 * content remains valid because the page fault handler
2155 * validated the pages.
2157 if (error != 0 && (bp->b_flags & B_CACHE) == 0 &&
2158 fs->e2fs_bsize == xfersize)
2161 vfs_bio_set_flags(bp, ioflag);
2164 * If IO_SYNC each buffer is written synchronously. Otherwise
2165 * if we have a severe page deficiency write the buffer
2166 * asynchronously. Otherwise try to cluster, and if that
2167 * doesn't do it then either do an async write (if O_DIRECT),
2168 * or a delayed write (if not).
2170 if (ioflag & IO_SYNC) {
2172 } else if (vm_page_count_severe() ||
2173 buf_dirty_count_severe() ||
2174 (ioflag & IO_ASYNC)) {
2175 bp->b_flags |= B_CLUSTEROK;
2177 } else if (xfersize + blkoffset == fs->e2fs_fsize) {
2178 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
2179 bp->b_flags |= B_CLUSTEROK;
2180 cluster_write(vp, bp, ip->i_size, seqcount, 0);
2184 } else if (ioflag & IO_DIRECT) {
2185 bp->b_flags |= B_CLUSTEROK;
2188 bp->b_flags |= B_CLUSTEROK;
2191 if (error || xfersize == 0)
2195 * If we successfully wrote any data, and we are not the superuser
2196 * we clear the setuid and setgid bits as a precaution against
2199 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
2201 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
2202 ip->i_mode &= ~(ISUID | ISGID);
2205 if (ioflag & IO_UNIT) {
2206 (void)ext2_truncate(vp, osize,
2207 ioflag & IO_SYNC, ap->a_cred, uio->uio_td);
2208 uio->uio_offset -= resid - uio->uio_resid;
2209 uio->uio_resid = resid;
2212 if (uio->uio_resid != resid) {
2213 ip->i_flag |= IN_CHANGE | IN_UPDATE;
2214 if (ioflag & IO_SYNC)
2215 error = ext2_update(vp, 1);