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 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
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>
54 #include <sys/endian.h>
56 #include <sys/mount.h>
57 #include <sys/unistd.h>
59 #include <sys/vnode.h>
60 #include <sys/namei.h>
61 #include <sys/lockf.h>
62 #include <sys/event.h>
67 #include <vm/vm_extern.h>
68 #include <vm/vnode_pager.h>
70 #include <fs/fifofs/fifo.h>
72 #include <ufs/ufs/dir.h>
74 #include <fs/ext2fs/fs.h>
75 #include <fs/ext2fs/inode.h>
76 #include <fs/ext2fs/ext2_extern.h>
77 #include <fs/ext2fs/ext2fs.h>
78 #include <fs/ext2fs/ext2_dinode.h>
79 #include <fs/ext2fs/ext2_dir.h>
80 #include <fs/ext2fs/ext2_mount.h>
82 static int ext2_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
83 static void ext2_itimes_locked(struct vnode *);
85 static vop_access_t ext2_access;
86 static int ext2_chmod(struct vnode *, int, struct ucred *, struct thread *);
87 static int ext2_chown(struct vnode *, uid_t, gid_t, struct ucred *,
89 static vop_close_t ext2_close;
90 static vop_create_t ext2_create;
91 static vop_fsync_t ext2_fsync;
92 static vop_getattr_t ext2_getattr;
93 static vop_link_t ext2_link;
94 static vop_mkdir_t ext2_mkdir;
95 static vop_mknod_t ext2_mknod;
96 static vop_open_t ext2_open;
97 static vop_pathconf_t ext2_pathconf;
98 static vop_print_t ext2_print;
99 static vop_read_t ext2_read;
100 static vop_readlink_t ext2_readlink;
101 static vop_remove_t ext2_remove;
102 static vop_rename_t ext2_rename;
103 static vop_rmdir_t ext2_rmdir;
104 static vop_setattr_t ext2_setattr;
105 static vop_strategy_t ext2_strategy;
106 static vop_symlink_t ext2_symlink;
107 static vop_write_t ext2_write;
108 static vop_vptofh_t ext2_vptofh;
109 static vop_close_t ext2fifo_close;
110 static vop_kqfilter_t ext2fifo_kqfilter;
112 /* Global vfs data structures for ext2. */
113 struct vop_vector ext2_vnodeops = {
114 .vop_default = &default_vnodeops,
115 .vop_access = ext2_access,
116 .vop_bmap = ext2_bmap,
117 .vop_cachedlookup = ext2_lookup,
118 .vop_close = ext2_close,
119 .vop_create = ext2_create,
120 .vop_fsync = ext2_fsync,
121 .vop_getattr = ext2_getattr,
122 .vop_inactive = ext2_inactive,
123 .vop_link = ext2_link,
124 .vop_lookup = vfs_cache_lookup,
125 .vop_mkdir = ext2_mkdir,
126 .vop_mknod = ext2_mknod,
127 .vop_open = ext2_open,
128 .vop_pathconf = ext2_pathconf,
129 .vop_poll = vop_stdpoll,
130 .vop_print = ext2_print,
131 .vop_read = ext2_read,
132 .vop_readdir = ext2_readdir,
133 .vop_readlink = ext2_readlink,
134 .vop_reallocblks = ext2_reallocblks,
135 .vop_reclaim = ext2_reclaim,
136 .vop_remove = ext2_remove,
137 .vop_rename = ext2_rename,
138 .vop_rmdir = ext2_rmdir,
139 .vop_setattr = ext2_setattr,
140 .vop_strategy = ext2_strategy,
141 .vop_symlink = ext2_symlink,
142 .vop_write = ext2_write,
143 .vop_vptofh = ext2_vptofh,
146 struct vop_vector ext2_fifoops = {
147 .vop_default = &fifo_specops,
148 .vop_access = ext2_access,
149 .vop_close = ext2fifo_close,
150 .vop_fsync = ext2_fsync,
151 .vop_getattr = ext2_getattr,
152 .vop_inactive = ext2_inactive,
153 .vop_kqfilter = ext2fifo_kqfilter,
154 .vop_print = ext2_print,
155 .vop_read = VOP_PANIC,
156 .vop_reclaim = ext2_reclaim,
157 .vop_setattr = ext2_setattr,
158 .vop_write = VOP_PANIC,
159 .vop_vptofh = ext2_vptofh,
162 #include <fs/ext2fs/ext2_readwrite.c>
165 * A virgin directory (no blushing please).
166 * Note that the type and namlen fields are reversed relative to ext2.
167 * Also, we don't use `struct odirtemplate', since it would just cause
168 * endianness problems.
170 static struct dirtemplate mastertemplate = {
171 0, 12, 1, EXT2_FT_DIR, ".",
172 0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
174 static struct dirtemplate omastertemplate = {
175 0, 12, 1, EXT2_FT_UNKNOWN, ".",
176 0, DIRBLKSIZ - 12, 2, EXT2_FT_UNKNOWN, ".."
180 ext2_itimes_locked(struct vnode *vp)
185 ASSERT_VI_LOCKED(vp, __func__);
188 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
190 if ((vp->v_type == VBLK || vp->v_type == VCHR))
191 ip->i_flag |= IN_LAZYMOD;
193 ip->i_flag |= IN_MODIFIED;
194 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
196 if (ip->i_flag & IN_ACCESS) {
197 ip->i_atime = ts.tv_sec;
198 ip->i_atimensec = ts.tv_nsec;
200 if (ip->i_flag & IN_UPDATE) {
201 ip->i_mtime = ts.tv_sec;
202 ip->i_mtimensec = ts.tv_nsec;
205 if (ip->i_flag & IN_CHANGE) {
206 ip->i_ctime = ts.tv_sec;
207 ip->i_ctimensec = ts.tv_nsec;
210 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
214 ext2_itimes(struct vnode *vp)
218 ext2_itimes_locked(vp);
223 * Create a regular file
227 struct vop_create_args /* {
229 struct vnode **a_vpp;
230 struct componentname *a_cnp;
237 ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
238 ap->a_dvp, ap->a_vpp, ap->a_cnp);
246 struct vop_open_args /* {
249 struct ucred *a_cred;
254 if (ap->a_vp->v_type == VBLK || ap->a_vp->v_type == VCHR)
258 * Files marked append-only must be opened for appending.
260 if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
261 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
264 vnode_create_vobject(ap->a_vp, VTOI(ap->a_vp)->i_size, ap->a_td);
272 * Update the times on the inode.
276 struct vop_close_args /* {
279 struct ucred *a_cred;
283 struct vnode *vp = ap->a_vp;
286 if (vp->v_usecount > 1)
287 ext2_itimes_locked(vp);
294 struct vop_access_args /* {
297 struct ucred *a_cred;
301 struct vnode *vp = ap->a_vp;
302 struct inode *ip = VTOI(vp);
303 accmode_t accmode = ap->a_accmode;
306 if (vp->v_type == VBLK || vp->v_type == VCHR)
310 * Disallow write attempts on read-only file systems;
311 * unless the file is a socket, fifo, or a block or
312 * character device resident on the file system.
314 if (accmode & VWRITE) {
315 switch (vp->v_type) {
319 if (vp->v_mount->mnt_flag & MNT_RDONLY)
327 /* If immutable bit set, nobody gets to write it. */
328 if ((accmode & VWRITE) && (ip->i_flags & (SF_IMMUTABLE | SF_SNAPSHOT)))
331 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
332 ap->a_accmode, ap->a_cred, NULL);
338 struct vop_getattr_args /* {
341 struct ucred *a_cred;
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 = ip->i_atimensec;
362 vap->va_mtime.tv_sec = ip->i_mtime;
363 vap->va_mtime.tv_nsec = ip->i_mtimensec;
364 vap->va_ctime.tv_sec = ip->i_ctime;
365 vap->va_ctime.tv_nsec = ip->i_ctimensec;
366 vap->va_flags = ip->i_flags;
367 vap->va_gen = ip->i_gen;
368 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
369 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
370 vap->va_type = IFTOVT(ip->i_mode);
371 vap->va_filerev = ip->i_modrev;
376 * Set attribute vnode op. called from several syscalls
380 struct vop_setattr_args /* {
383 struct ucred *a_cred;
386 struct vattr *vap = ap->a_vap;
387 struct vnode *vp = ap->a_vp;
388 struct inode *ip = VTOI(vp);
389 struct ucred *cred = ap->a_cred;
390 struct thread *td = curthread;
394 * Check for unsettable attributes.
396 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
397 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
398 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
399 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
402 if (vap->va_flags != VNOVAL) {
403 /* Disallow flags not supported by ext2fs. */
404 if(vap->va_flags & ~(SF_APPEND | SF_IMMUTABLE | UF_NODUMP))
407 if (vp->v_mount->mnt_flag & MNT_RDONLY)
410 * Callers may only modify the file flags on objects they
411 * have VADMIN rights for.
413 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
416 * Unprivileged processes and privileged processes in
417 * jail() are not permitted to unset system flags, or
418 * modify flags if any system flags are set.
419 * Privileged non-jail processes may not modify system flags
420 * if securelevel > 0 and any existing system flags are set.
422 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
424 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
425 error = securelevel_gt(cred, 0);
429 ip->i_flags = vap->va_flags;
432 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
433 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
435 ip->i_flags &= SF_SETTABLE;
436 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
438 ip->i_flag |= IN_CHANGE;
439 if (vap->va_flags & (IMMUTABLE | APPEND))
442 if (ip->i_flags & (IMMUTABLE | APPEND))
445 * Go through the fields and update iff not VNOVAL.
447 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
448 if (vp->v_mount->mnt_flag & MNT_RDONLY)
450 if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred,
454 if (vap->va_size != VNOVAL) {
456 * Disallow write attempts on read-only file systems;
457 * unless the file is a socket, fifo, or a block or
458 * character device resident on the file system.
460 switch (vp->v_type) {
465 if (vp->v_mount->mnt_flag & MNT_RDONLY)
471 if ((error = ext2_truncate(vp, vap->va_size, 0, cred, td)) != 0)
474 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
475 if (vp->v_mount->mnt_flag & MNT_RDONLY)
479 * If times is NULL, ... The caller must be the owner of
480 * the file, have permission to write the file, or be the
482 * If times is non-NULL, ... The caller must be the owner of
483 * the file or be the super-user.
485 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)) &&
486 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
487 (error = VOP_ACCESS(vp, VWRITE, cred, td))))
489 if (vap->va_atime.tv_sec != VNOVAL)
490 ip->i_flag |= IN_ACCESS;
491 if (vap->va_mtime.tv_sec != VNOVAL)
492 ip->i_flag |= IN_CHANGE | IN_UPDATE;
494 if (vap->va_atime.tv_sec != VNOVAL) {
495 ip->i_atime = vap->va_atime.tv_sec;
496 ip->i_atimensec = vap->va_atime.tv_nsec;
498 if (vap->va_mtime.tv_sec != VNOVAL) {
499 ip->i_mtime = vap->va_mtime.tv_sec;
500 ip->i_mtimensec = vap->va_mtime.tv_nsec;
502 error = ext2_update(vp, 0);
507 if (vap->va_mode != (mode_t)VNOVAL) {
508 if (vp->v_mount->mnt_flag & MNT_RDONLY)
510 error = ext2_chmod(vp, (int)vap->va_mode, cred, td);
516 * Change the mode on a file.
517 * Inode must be locked before calling.
520 ext2_chmod(vp, mode, cred, td)
526 struct inode *ip = VTOI(vp);
530 * To modify the permissions on a file, must possess VADMIN
533 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
536 * Privileged processes may set the sticky bit on non-directories,
537 * as well as set the setgid bit on a file with a group that the
538 * process is not a member of.
540 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
541 error = priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0);
545 if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
546 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
550 ip->i_mode &= ~ALLPERMS;
551 ip->i_mode |= (mode & ALLPERMS);
552 ip->i_flag |= IN_CHANGE;
557 * Perform chown operation on inode ip;
558 * inode must be locked prior to call.
561 ext2_chown(vp, uid, gid, cred, td)
568 struct inode *ip = VTOI(vp);
573 if (uid == (uid_t)VNOVAL)
575 if (gid == (gid_t)VNOVAL)
578 * To modify the ownership of a file, must possess VADMIN
581 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
584 * To change the owner of a file, or change the group of a file
585 * to a group of which we are not a member, the caller must
588 if (uid != ip->i_uid || (gid != ip->i_gid &&
589 !groupmember(gid, cred))) {
590 error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0);
598 ip->i_flag |= IN_CHANGE;
599 if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
600 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0) != 0)
601 ip->i_mode &= ~(ISUID | ISGID);
607 * Synch an open file.
612 struct vop_fsync_args /* {
614 struct ucred *a_cred;
620 * Flush all dirty buffers associated with a vnode.
625 return (ext2_update(ap->a_vp, ap->a_waitfor == MNT_WAIT));
634 struct vop_mknod_args /* {
636 struct vnode **a_vpp;
637 struct componentname *a_cnp;
641 struct vattr *vap = ap->a_vap;
642 struct vnode **vpp = ap->a_vpp;
647 error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
648 ap->a_dvp, vpp, ap->a_cnp);
652 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
653 if (vap->va_rdev != VNOVAL) {
655 * Want to be able to use this to make badblock
656 * inodes, so don't truncate the dev number.
658 ip->i_rdev = vap->va_rdev;
661 * Remove inode, then reload it through VFS_VGET so it is
662 * checked to see if it is an alias of an existing entry in
663 * the inode cache. XXX I don't believe this is necessary now.
665 (*vpp)->v_type = VNON;
666 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
669 error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
679 struct vop_remove_args /* {
682 struct componentname *a_cnp;
686 struct vnode *vp = ap->a_vp;
687 struct vnode *dvp = ap->a_dvp;
691 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
692 (VTOI(dvp)->i_flags & APPEND)) {
696 error = ext2_dirremove(dvp, ap->a_cnp);
699 ip->i_flag |= IN_CHANGE;
710 struct vop_link_args /* {
711 struct vnode *a_tdvp;
713 struct componentname *a_cnp;
716 struct vnode *vp = ap->a_vp;
717 struct vnode *tdvp = ap->a_tdvp;
718 struct componentname *cnp = ap->a_cnp;
723 if ((cnp->cn_flags & HASBUF) == 0)
724 panic("ext2_link: no name");
726 if (tdvp->v_mount != vp->v_mount) {
731 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
735 if (ip->i_flags & (IMMUTABLE | APPEND)) {
740 ip->i_flag |= IN_CHANGE;
741 error = ext2_update(vp, !DOINGASYNC(vp));
743 error = ext2_direnter(ip, tdvp, cnp);
746 ip->i_flag |= IN_CHANGE;
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
778 struct vop_rename_args /* {
779 struct vnode *a_fdvp;
781 struct componentname *a_fcnp;
782 struct vnode *a_tdvp;
784 struct componentname *a_tcnp;
787 struct vnode *tvp = ap->a_tvp;
788 struct vnode *tdvp = ap->a_tdvp;
789 struct vnode *fvp = ap->a_fvp;
790 struct vnode *fdvp = ap->a_fdvp;
791 struct componentname *tcnp = ap->a_tcnp;
792 struct componentname *fcnp = ap->a_fcnp;
793 struct inode *ip, *xp, *dp;
794 struct dirtemplate dirbuf;
795 int doingdirectory = 0, oldparent = 0, newparent = 0;
800 if ((tcnp->cn_flags & HASBUF) == 0 ||
801 (fcnp->cn_flags & HASBUF) == 0)
802 panic("ext2_rename: no name");
805 * Check for cross-device rename.
807 if ((fvp->v_mount != tdvp->v_mount) ||
808 (tvp && (fvp->v_mount != tvp->v_mount))) {
822 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
823 (VTOI(tdvp)->i_flags & APPEND))) {
829 * Renaming a file to itself has no effect. The upper layers should
830 * not call us in that case. Temporarily just warn if they do.
833 printf("ext2_rename: fvp == tvp (can't happen)\n");
838 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
842 if (ip->i_nlink >= LINK_MAX) {
847 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
848 || (dp->i_flags & APPEND)) {
853 if ((ip->i_mode & IFMT) == IFDIR) {
855 * Avoid ".", "..", and aliases of "." for obvious reasons.
857 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
858 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
859 (ip->i_flag & IN_RENAME)) {
864 ip->i_flag |= IN_RENAME;
865 oldparent = dp->i_number;
871 * When the target exists, both the directory
872 * and target vnodes are returned locked.
880 * 1) Bump link count while we're moving stuff
881 * around. If we crash somewhere before
882 * completing our work, the link count
883 * may be wrong, but correctable.
886 ip->i_flag |= IN_CHANGE;
887 if ((error = ext2_update(fvp, !DOINGASYNC(fvp))) != 0) {
893 * If ".." must be changed (ie the directory gets a new
894 * parent) then the source directory must not be in the
895 * directory hierarchy above the target, as this would
896 * orphan everything below the source directory. Also
897 * the user must have write permission in the source so
898 * as to be able to change "..". We must repeat the call
899 * to namei, as the parent directory is unlocked by the
900 * call to checkpath().
902 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
904 if (oldparent != dp->i_number)
905 newparent = dp->i_number;
906 if (doingdirectory && newparent) {
907 if (error) /* write access check above */
911 error = ext2_checkpath(ip, dp, tcnp->cn_cred);
915 error = relookup(tdvp, &tvp, tcnp);
925 * 2) If target doesn't exist, link the target
926 * to the source and unlink the source.
927 * Otherwise, rewrite the target directory
928 * entry to reference the source inode and
929 * expunge the original entry's existence.
932 if (dp->i_devvp != ip->i_devvp)
933 panic("ext2_rename: EXDEV");
935 * Account for ".." in new directory.
936 * When source and destination have the same
937 * parent we don't fool with the link count.
939 if (doingdirectory && newparent) {
940 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
945 dp->i_flag |= IN_CHANGE;
946 error = ext2_update(tdvp, !DOINGASYNC(tdvp));
950 error = ext2_direnter(ip, tdvp, tcnp);
952 if (doingdirectory && newparent) {
954 dp->i_flag |= IN_CHANGE;
955 (void)ext2_update(tdvp, 1);
961 if (xp->i_devvp != dp->i_devvp || xp->i_devvp != ip->i_devvp)
962 panic("ext2_rename: EXDEV");
964 * Short circuit rename(foo, foo).
966 if (xp->i_number == ip->i_number)
967 panic("ext2_rename: same file");
969 * If the parent directory is "sticky", then the user must
970 * own the parent directory, or the destination of the rename,
971 * otherwise the destination may not be changed (except by
972 * root). This implements append-only directories.
974 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
975 tcnp->cn_cred->cr_uid != dp->i_uid &&
976 xp->i_uid != tcnp->cn_cred->cr_uid) {
981 * Target must be empty if a directory and have no links
982 * to it. Also, ensure source and target are compatible
983 * (both directories, or both not directories).
985 if ((xp->i_mode&IFMT) == IFDIR) {
986 if (! ext2_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
991 if (!doingdirectory) {
996 } else if (doingdirectory) {
1000 error = ext2_dirrewrite(dp, ip, tcnp);
1004 * If the target directory is in the same
1005 * directory as the source directory,
1006 * decrement the link count on the parent
1007 * of the target directory.
1009 if (doingdirectory && !newparent) {
1011 dp->i_flag |= IN_CHANGE;
1015 * Adjust the link count of the target to
1016 * reflect the dirrewrite above. If this is
1017 * a directory it is empty and there are
1018 * no links to it, so we can squash the inode and
1019 * any space associated with it. We disallowed
1020 * renaming over top of a directory with links to
1021 * it above, as the remaining link would point to
1022 * a directory without "." or ".." entries.
1025 if (doingdirectory) {
1026 if (--xp->i_nlink != 0)
1027 panic("ext2_rename: linked directory");
1028 error = ext2_truncate(tvp, (off_t)0, IO_SYNC,
1029 tcnp->cn_cred, tcnp->cn_thread);
1031 xp->i_flag |= IN_CHANGE;
1037 * 3) Unlink the source.
1039 fcnp->cn_flags &= ~MODMASK;
1040 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1042 error = relookup(fdvp, &fvp, fcnp);
1050 * From name has disappeared.
1053 panic("ext2_rename: lost dir entry");
1058 * Ensure that the directory entry still exists and has not
1059 * changed while the new name has been entered. If the source is
1060 * a file then the entry may have been unlinked or renamed. In
1061 * either case there is no further work to be done. If the source
1062 * is a directory then it cannot have been rmdir'ed; its link
1063 * count of three would cause a rmdir to fail with ENOTEMPTY.
1064 * The IN_RENAME flag ensures that it cannot be moved by another
1069 panic("ext2_rename: lost dir entry");
1072 * If the source is a directory with a
1073 * new parent, the link count of the old
1074 * parent directory must be decremented
1075 * and ".." set to point to the new parent.
1077 if (doingdirectory && newparent) {
1079 dp->i_flag |= IN_CHANGE;
1080 error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
1081 sizeof (struct dirtemplate), (off_t)0,
1082 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1083 tcnp->cn_cred, NOCRED, NULL, NULL);
1085 /* Like ufs little-endian: */
1086 namlen = dirbuf.dotdot_type;
1088 dirbuf.dotdot_name[0] != '.' ||
1089 dirbuf.dotdot_name[1] != '.') {
1090 ext2_dirbad(xp, (doff_t)12,
1091 "rename: mangled dir");
1093 dirbuf.dotdot_ino = newparent;
1094 (void) vn_rdwr(UIO_WRITE, fvp,
1096 sizeof (struct dirtemplate),
1097 (off_t)0, UIO_SYSSPACE,
1098 IO_NODELOCKED | IO_SYNC |
1099 IO_NOMACCHECK, tcnp->cn_cred,
1100 NOCRED, NULL, NULL);
1105 error = ext2_dirremove(fdvp, fcnp);
1108 xp->i_flag |= IN_CHANGE;
1110 xp->i_flag &= ~IN_RENAME;
1125 ip->i_flag &= ~IN_RENAME;
1126 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1128 ip->i_flag |= IN_CHANGE;
1129 ip->i_flag &= ~IN_RENAME;
1141 struct vop_mkdir_args /* {
1142 struct vnode *a_dvp;
1143 struct vnode **a_vpp;
1144 struct componentname *a_cnp;
1145 struct vattr *a_vap;
1148 struct vnode *dvp = ap->a_dvp;
1149 struct vattr *vap = ap->a_vap;
1150 struct componentname *cnp = ap->a_cnp;
1151 struct inode *ip, *dp;
1153 struct dirtemplate dirtemplate, *dtp;
1157 if ((cnp->cn_flags & HASBUF) == 0)
1158 panic("ext2_mkdir: no name");
1161 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1165 dmode = vap->va_mode & 0777;
1168 * Must simulate part of ext2_makeinode here to acquire the inode,
1169 * but not have it entered in the parent directory. The entry is
1170 * made later after writing "." and ".." entries.
1172 error = ext2_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1176 ip->i_gid = dp->i_gid;
1180 * if we are hacking owners here, (only do this where told to)
1181 * and we are not giving it TOO root, (would subvert quotas)
1182 * then go ahead and give it to the other user.
1183 * The new directory also inherits the SUID bit.
1184 * If user's UID and dir UID are the same,
1185 * 'give it away' so that the SUID is still forced on.
1187 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1188 (dp->i_mode & ISUID) && dp->i_uid) {
1190 ip->i_uid = dp->i_uid;
1192 ip->i_uid = cnp->cn_cred->cr_uid;
1196 ip->i_uid = cnp->cn_cred->cr_uid;
1198 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1200 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1202 if (cnp->cn_flags & ISWHITEOUT)
1203 ip->i_flags |= UF_OPAQUE;
1204 error = ext2_update(tvp, 1);
1207 * Bump link count in parent directory
1208 * to reflect work done below. Should
1209 * be done before reference is created
1210 * so reparation is possible if we crash.
1213 dp->i_flag |= IN_CHANGE;
1214 error = ext2_update(dvp, !DOINGASYNC(dvp));
1218 /* Initialize directory with "." and ".." from static template. */
1219 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
1220 EXT2F_INCOMPAT_FTYPE))
1221 dtp = &mastertemplate;
1223 dtp = &omastertemplate;
1225 dirtemplate.dot_ino = ip->i_number;
1226 dirtemplate.dotdot_ino = dp->i_number;
1227 /* note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE
1228 * so let's just redefine it - for this function only
1231 #define DIRBLKSIZ VTOI(dvp)->i_e2fs->e2fs_bsize
1232 dirtemplate.dotdot_reclen = DIRBLKSIZ - 12;
1233 error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
1234 sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
1235 IO_NODELOCKED | IO_SYNC | IO_NOMACCHECK, cnp->cn_cred, NOCRED,
1239 dp->i_flag |= IN_CHANGE;
1242 if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1243 /* XXX should grow with balloc() */
1244 panic("ext2_mkdir: blksize");
1246 ip->i_size = DIRBLKSIZ;
1247 ip->i_flag |= IN_CHANGE;
1250 /* Directory set up, now install its entry in the parent directory. */
1251 error = ext2_direnter(ip, dvp, cnp);
1254 dp->i_flag |= IN_CHANGE;
1258 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1259 * for us because we set the link count to 0.
1263 ip->i_flag |= IN_CHANGE;
1270 #define DIRBLKSIZ DEV_BSIZE
1274 * Rmdir system call.
1278 struct vop_rmdir_args /* {
1279 struct vnode *a_dvp;
1281 struct componentname *a_cnp;
1284 struct vnode *vp = ap->a_vp;
1285 struct vnode *dvp = ap->a_dvp;
1286 struct componentname *cnp = ap->a_cnp;
1287 struct inode *ip, *dp;
1294 * Verify the directory is empty (and valid).
1295 * (Rmdir ".." won't be valid since
1296 * ".." will contain a reference to
1297 * the current directory and thus be
1301 if (ip->i_nlink != 2 || !ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1305 if ((dp->i_flags & APPEND)
1306 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1311 * Delete reference to directory before purging
1312 * inode. If we crash in between, the directory
1313 * will be reattached to lost+found,
1315 error = ext2_dirremove(dvp, cnp);
1319 dp->i_flag |= IN_CHANGE;
1323 * Truncate inode. The only stuff left
1324 * in the directory is "." and "..". The
1325 * "." reference is inconsequential since
1326 * we're quashing it. The ".." reference
1327 * has already been adjusted above. We've
1328 * removed the "." reference and the reference
1329 * in the parent directory, but there may be
1330 * other hard links so decrement by 2 and
1331 * worry about them later.
1334 error = ext2_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred,
1336 cache_purge(ITOV(ip));
1337 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1343 * symlink -- make a symbolic link
1347 struct vop_symlink_args /* {
1348 struct vnode *a_dvp;
1349 struct vnode **a_vpp;
1350 struct componentname *a_cnp;
1351 struct vattr *a_vap;
1355 struct vnode *vp, **vpp = ap->a_vpp;
1359 error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1364 len = strlen(ap->a_target);
1365 if (len < vp->v_mount->mnt_maxsymlinklen) {
1367 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1369 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1371 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1372 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1373 ap->a_cnp->cn_cred, NOCRED, NULL, NULL);
1380 * Return target name of a symbolic link
1384 struct vop_readlink_args /* {
1387 struct ucred *a_cred;
1390 struct vnode *vp = ap->a_vp;
1391 struct inode *ip = VTOI(vp);
1395 if (isize < vp->v_mount->mnt_maxsymlinklen) {
1396 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1399 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1403 * Calculate the logical to physical mapping if not done already,
1404 * then call the device strategy routine.
1406 * In order to be able to swap to a file, the ext2_bmaparray() operation may not
1407 * deadlock on memory. See ext2_bmap() for details.
1411 struct vop_strategy_args /* {
1416 struct buf *bp = ap->a_bp;
1417 struct vnode *vp = ap->a_vp;
1424 if (vp->v_type == VBLK || vp->v_type == VCHR)
1425 panic("ext2_strategy: spec");
1426 if (bp->b_blkno == bp->b_lblkno) {
1427 error = ext2_bmaparray(vp, bp->b_lblkno, &blkno, NULL, NULL);
1428 bp->b_blkno = blkno;
1430 bp->b_error = error;
1431 bp->b_ioflags |= BIO_ERROR;
1435 if ((long)bp->b_blkno == -1)
1438 if ((long)bp->b_blkno == -1) {
1442 bp->b_iooffset = dbtob(bp->b_blkno);
1443 bo = VFSTOEXT2(vp->v_mount)->um_bo;
1444 BO_STRATEGY(bo, bp);
1449 * Print out the contents of an inode.
1453 struct vop_print_args /* {
1457 struct vnode *vp = ap->a_vp;
1458 struct inode *ip = VTOI(vp);
1460 vn_printf(ip->i_devvp, "\tino %lu", (u_long)ip->i_number);
1461 if (vp->v_type == VFIFO)
1468 * Close wrapper for fifos.
1470 * Update the times on the inode then do device close.
1474 struct vop_close_args /* {
1477 struct ucred *a_cred;
1478 struct thread *a_td;
1481 struct vnode *vp = ap->a_vp;
1484 if (vp->v_usecount > 1)
1485 ext2_itimes_locked(vp);
1487 return (fifo_specops.vop_close(ap));
1491 * Kqfilter wrapper for fifos.
1493 * Fall through to ext2 kqfilter routines if needed
1496 ext2fifo_kqfilter(ap)
1497 struct vop_kqfilter_args *ap;
1501 error = fifo_specops.vop_kqfilter(ap);
1503 error = vfs_kqfilter(ap);
1508 * Return POSIX pathconf information applicable to ext2 filesystems.
1512 struct vop_pathconf_args /* {
1519 switch (ap->a_name) {
1521 *ap->a_retval = LINK_MAX;
1524 *ap->a_retval = NAME_MAX;
1527 *ap->a_retval = PATH_MAX;
1530 *ap->a_retval = PIPE_BUF;
1532 case _PC_CHOWN_RESTRICTED:
1545 * Vnode pointer to File handle
1550 struct vop_vptofh_args /* {
1558 ip = VTOI(ap->a_vp);
1559 ufhp = (struct ufid *)ap->a_fhp;
1560 ufhp->ufid_len = sizeof(struct ufid);
1561 ufhp->ufid_ino = ip->i_number;
1562 ufhp->ufid_gen = ip->i_gen;
1567 * Initialize the vnode associated with a new inode, handle aliased
1571 ext2_vinit(mntp, fifoops, vpp)
1573 struct vop_vector *fifoops;
1581 vp->v_type = IFTOVT(ip->i_mode);
1582 if (vp->v_type == VFIFO)
1585 if (ip->i_number == EXT2_ROOTINO)
1586 vp->v_vflag |= VV_ROOT;
1587 ip->i_modrev = init_va_filerev();
1593 * Allocate a new inode.
1596 ext2_makeinode(mode, dvp, vpp, cnp)
1600 struct componentname *cnp;
1602 struct inode *ip, *pdir;
1608 if ((cnp->cn_flags & HASBUF) == 0)
1609 panic("ext2_makeinode: no name");
1612 if ((mode & IFMT) == 0)
1615 error = ext2_valloc(dvp, mode, cnp->cn_cred, &tvp);
1620 ip->i_gid = pdir->i_gid;
1625 * not the owner of the directory,
1626 * and we are hacking owners here, (only do this where told to)
1627 * and we are not giving it TOO root, (would subvert quotas)
1628 * then go ahead and give it to the other user.
1629 * Note that this drops off the execute bits for security.
1631 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1632 (pdir->i_mode & ISUID) &&
1633 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1634 ip->i_uid = pdir->i_uid;
1637 ip->i_uid = cnp->cn_cred->cr_uid;
1641 ip->i_uid = cnp->cn_cred->cr_uid;
1643 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1645 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
1647 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred)) {
1648 if (priv_check_cred(cnp->cn_cred, PRIV_VFS_RETAINSUGID, 0))
1649 ip->i_mode &= ~ISGID;
1652 if (cnp->cn_flags & ISWHITEOUT)
1653 ip->i_flags |= UF_OPAQUE;
1656 * Make sure inode goes to disk before directory entry.
1658 error = ext2_update(tvp, !DOINGASYNC(tvp));
1661 error = ext2_direnter(ip, dvp, cnp);
1670 * Write error occurred trying to update the inode
1671 * or the directory so must deallocate the inode.
1674 ip->i_flag |= IN_CHANGE;