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/resourcevar.h>
50 #include <sys/kernel.h>
51 #include <sys/fcntl.h>
57 #include <sys/mount.h>
58 #include <sys/unistd.h>
60 #include <sys/vnode.h>
61 #include <sys/namei.h>
62 #include <sys/lockf.h>
63 #include <sys/event.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vnode_pager.h>
71 #include <fs/fifofs/fifo.h>
73 #include <sys/signalvar.h>
74 #include <ufs/ufs/dir.h>
76 #include <gnu/fs/ext2fs/inode.h>
77 #include <gnu/fs/ext2fs/ext2_mount.h>
78 #include <gnu/fs/ext2fs/ext2_fs_sb.h>
79 #include <gnu/fs/ext2fs/fs.h>
80 #include <gnu/fs/ext2fs/ext2_extern.h>
81 #include <gnu/fs/ext2fs/ext2_fs.h>
83 static int ext2_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
85 static vop_access_t ext2_access;
86 static vop_advlock_t ext2_advlock;
87 static int ext2_chmod(struct vnode *, int, struct ucred *, struct thread *);
88 static int ext2_chown(struct vnode *, uid_t, gid_t, struct ucred *,
90 static vop_close_t ext2_close;
91 static vop_create_t ext2_create;
92 static vop_fsync_t ext2_fsync;
93 static vop_getattr_t ext2_getattr;
94 static vop_kqfilter_t ext2_kqfilter;
95 static vop_link_t ext2_link;
96 static vop_mkdir_t ext2_mkdir;
97 static vop_mknod_t ext2_mknod;
98 static vop_open_t ext2_open;
99 static vop_pathconf_t ext2_pathconf;
100 static vop_print_t ext2_print;
101 static vop_read_t ext2_read;
102 static vop_readlink_t ext2_readlink;
103 static vop_remove_t ext2_remove;
104 static vop_rename_t ext2_rename;
105 static vop_rmdir_t ext2_rmdir;
106 static vop_setattr_t ext2_setattr;
107 static vop_strategy_t ext2_strategy;
108 static vop_symlink_t ext2_symlink;
109 static vop_write_t ext2_write;
110 static vop_vptofh_t ext2_vptofh;
111 static vop_close_t ext2fifo_close;
112 static vop_kqfilter_t ext2fifo_kqfilter;
113 static int filt_ext2read(struct knote *kn, long hint);
114 static int filt_ext2write(struct knote *kn, long hint);
115 static int filt_ext2vnode(struct knote *kn, long hint);
116 static void filt_ext2detach(struct knote *kn);
118 /* Global vfs data structures for ext2. */
119 struct vop_vector ext2_vnodeops = {
120 .vop_default = &default_vnodeops,
121 .vop_access = ext2_access,
122 .vop_advlock = ext2_advlock,
123 .vop_bmap = ext2_bmap,
124 .vop_cachedlookup = ext2_lookup,
125 .vop_close = ext2_close,
126 .vop_create = ext2_create,
127 .vop_fsync = ext2_fsync,
128 .vop_getattr = ext2_getattr,
129 .vop_inactive = ext2_inactive,
130 .vop_link = ext2_link,
131 .vop_lookup = vfs_cache_lookup,
132 .vop_mkdir = ext2_mkdir,
133 .vop_mknod = ext2_mknod,
134 .vop_open = ext2_open,
135 .vop_pathconf = ext2_pathconf,
136 .vop_poll = vop_stdpoll,
137 .vop_kqfilter = ext2_kqfilter,
138 .vop_print = ext2_print,
139 .vop_read = ext2_read,
140 .vop_readdir = ext2_readdir,
141 .vop_readlink = ext2_readlink,
142 .vop_reallocblks = ext2_reallocblks,
143 .vop_reclaim = ext2_reclaim,
144 .vop_remove = ext2_remove,
145 .vop_rename = ext2_rename,
146 .vop_rmdir = ext2_rmdir,
147 .vop_setattr = ext2_setattr,
148 .vop_strategy = ext2_strategy,
149 .vop_symlink = ext2_symlink,
150 .vop_write = ext2_write,
151 .vop_vptofh = ext2_vptofh,
154 struct vop_vector ext2_fifoops = {
155 .vop_default = &fifo_specops,
156 .vop_access = ext2_access,
157 .vop_close = ext2fifo_close,
158 .vop_fsync = ext2_fsync,
159 .vop_getattr = ext2_getattr,
160 .vop_inactive = ext2_inactive,
161 .vop_kqfilter = ext2fifo_kqfilter,
162 .vop_print = ext2_print,
163 .vop_read = VOP_PANIC,
164 .vop_reclaim = ext2_reclaim,
165 .vop_setattr = ext2_setattr,
166 .vop_write = VOP_PANIC,
167 .vop_vptofh = ext2_vptofh,
170 #include <gnu/fs/ext2fs/ext2_readwrite.c>
173 * A virgin directory (no blushing please).
174 * Note that the type and namlen fields are reversed relative to ext2.
175 * Also, we don't use `struct odirtemplate', since it would just cause
176 * endianness problems.
178 static struct dirtemplate mastertemplate = {
179 0, 12, 1, EXT2_FT_DIR, ".",
180 0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
182 static struct dirtemplate omastertemplate = {
183 0, 12, 1, EXT2_FT_UNKNOWN, ".",
184 0, DIRBLKSIZ - 12, 2, EXT2_FT_UNKNOWN, ".."
195 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
197 if ((vp->v_type == VBLK || vp->v_type == VCHR))
198 ip->i_flag |= IN_LAZYMOD;
200 ip->i_flag |= IN_MODIFIED;
201 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
203 if (ip->i_flag & IN_ACCESS) {
204 ip->i_atime = ts.tv_sec;
205 ip->i_atimensec = ts.tv_nsec;
207 if (ip->i_flag & IN_UPDATE) {
208 ip->i_mtime = ts.tv_sec;
209 ip->i_mtimensec = ts.tv_nsec;
212 if (ip->i_flag & IN_CHANGE) {
213 ip->i_ctime = ts.tv_sec;
214 ip->i_ctimensec = ts.tv_nsec;
217 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
221 * Create a regular file
225 struct vop_create_args /* {
227 struct vnode **a_vpp;
228 struct componentname *a_cnp;
235 ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
236 ap->a_dvp, ap->a_vpp, ap->a_cnp);
244 struct vop_open_args /* {
247 struct ucred *a_cred;
252 if (ap->a_vp->v_type == VBLK || ap->a_vp->v_type == VCHR)
256 * Files marked append-only must be opened for appending.
258 if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
259 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
262 vnode_create_vobject(ap->a_vp, VTOI(ap->a_vp)->i_size, ap->a_td);
270 * Update the times on the inode.
274 struct vop_close_args /* {
277 struct ucred *a_cred;
281 struct vnode *vp = ap->a_vp;
284 if (vp->v_usecount > 1)
292 struct vop_access_args /* {
295 struct ucred *a_cred;
299 struct vnode *vp = ap->a_vp;
300 struct inode *ip = VTOI(vp);
301 mode_t mode = ap->a_mode;
304 if (vp->v_type == VBLK || vp->v_type == VCHR)
308 * Disallow write attempts on read-only file systems;
309 * unless the file is a socket, fifo, or a block or
310 * character device resident on the file system.
313 switch (vp->v_type) {
317 if (vp->v_mount->mnt_flag & MNT_RDONLY)
325 /* If immutable bit set, nobody gets to write it. */
326 if ((mode & VWRITE) && (ip->i_flags & (IMMUTABLE | SF_SNAPSHOT)))
329 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
330 ap->a_mode, ap->a_cred, NULL);
336 struct vop_getattr_args /* {
339 struct ucred *a_cred;
343 struct vnode *vp = ap->a_vp;
344 struct inode *ip = VTOI(vp);
345 struct vattr *vap = ap->a_vap;
349 * Copy from inode table
351 vap->va_fsid = dev2udev(ip->i_devvp->v_rdev);
352 vap->va_fileid = ip->i_number;
353 vap->va_mode = ip->i_mode & ~IFMT;
354 vap->va_nlink = ip->i_nlink;
355 vap->va_uid = ip->i_uid;
356 vap->va_gid = ip->i_gid;
357 vap->va_rdev = ip->i_rdev;
358 vap->va_size = ip->i_size;
359 vap->va_atime.tv_sec = ip->i_atime;
360 vap->va_atime.tv_nsec = ip->i_atimensec;
361 vap->va_mtime.tv_sec = ip->i_mtime;
362 vap->va_mtime.tv_nsec = ip->i_mtimensec;
363 vap->va_ctime.tv_sec = ip->i_ctime;
364 vap->va_ctime.tv_nsec = ip->i_ctimensec;
365 vap->va_flags = ip->i_flags;
366 vap->va_gen = ip->i_gen;
367 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
368 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
369 vap->va_type = IFTOVT(ip->i_mode);
370 vap->va_filerev = ip->i_modrev;
375 * Set attribute vnode op. called from several syscalls
379 struct vop_setattr_args /* {
382 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 = ap->a_td;
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 if (vp->v_mount->mnt_flag & MNT_RDONLY)
406 * Callers may only modify the file flags on objects they
407 * have VADMIN rights for.
409 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
412 * Unprivileged processes and privileged processes in
413 * jail() are not permitted to unset system flags, or
414 * modify flags if any system flags are set.
415 * Privileged non-jail processes may not modify system flags
416 * if securelevel > 0 and any existing system flags are set.
418 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
420 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
421 error = securelevel_gt(cred, 0);
425 ip->i_flags = vap->va_flags;
428 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
429 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
431 ip->i_flags &= SF_SETTABLE;
432 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
434 ip->i_flag |= IN_CHANGE;
435 if (vap->va_flags & (IMMUTABLE | APPEND))
438 if (ip->i_flags & (IMMUTABLE | APPEND))
441 * Go through the fields and update iff not VNOVAL.
443 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
444 if (vp->v_mount->mnt_flag & MNT_RDONLY)
446 if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred,
450 if (vap->va_size != VNOVAL) {
452 * Disallow write attempts on read-only file systems;
453 * unless the file is a socket, fifo, or a block or
454 * character device resident on the file system.
456 switch (vp->v_type) {
461 if (vp->v_mount->mnt_flag & MNT_RDONLY)
467 if ((error = ext2_truncate(vp, vap->va_size, 0, cred, td)) != 0)
470 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
471 if (vp->v_mount->mnt_flag & MNT_RDONLY)
475 * If times is NULL, ... The caller must be the owner of
476 * the file, have permission to write the file, or be the
478 * If times is non-NULL, ... The caller must be the owner of
479 * the file or be the super-user.
481 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)) &&
482 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
483 (error = VOP_ACCESS(vp, VWRITE, cred, td))))
485 if (vap->va_atime.tv_sec != VNOVAL)
486 ip->i_flag |= IN_ACCESS;
487 if (vap->va_mtime.tv_sec != VNOVAL)
488 ip->i_flag |= IN_CHANGE | IN_UPDATE;
490 if (vap->va_atime.tv_sec != VNOVAL) {
491 ip->i_atime = vap->va_atime.tv_sec;
492 ip->i_atimensec = vap->va_atime.tv_nsec;
494 if (vap->va_mtime.tv_sec != VNOVAL) {
495 ip->i_mtime = vap->va_mtime.tv_sec;
496 ip->i_mtimensec = vap->va_mtime.tv_nsec;
498 error = ext2_update(vp, 0);
503 if (vap->va_mode != (mode_t)VNOVAL) {
504 if (vp->v_mount->mnt_flag & MNT_RDONLY)
506 error = ext2_chmod(vp, (int)vap->va_mode, cred, td);
512 * Change the mode on a file.
513 * Inode must be locked before calling.
516 ext2_chmod(vp, mode, cred, td)
522 struct inode *ip = VTOI(vp);
526 * To modify the permissions on a file, must possess VADMIN
529 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
532 * Privileged processes may set the sticky bit on non-directories,
533 * as well as set the setgid bit on a file with a group that the
534 * process is not a member of.
536 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
537 error = priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0);
541 if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
542 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
546 ip->i_mode &= ~ALLPERMS;
547 ip->i_mode |= (mode & ALLPERMS);
548 ip->i_flag |= IN_CHANGE;
553 * Perform chown operation on inode ip;
554 * inode must be locked prior to call.
557 ext2_chown(vp, uid, gid, cred, td)
564 struct inode *ip = VTOI(vp);
569 if (uid == (uid_t)VNOVAL)
571 if (gid == (gid_t)VNOVAL)
574 * To modify the ownership of a file, must possess VADMIN
577 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
580 * To change the owner of a file, or change the group of a file
581 * to a group of which we are not a member, the caller must
584 if (uid != ip->i_uid || (gid != ip->i_gid &&
585 !groupmember(gid, cred))) {
586 error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0);
594 ip->i_flag |= IN_CHANGE;
595 if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
596 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0) != 0)
597 ip->i_mode &= ~(ISUID | ISGID);
603 * Synch an open file.
608 struct vop_fsync_args /* {
610 struct ucred *a_cred;
616 * Flush all dirty buffers associated with a vnode.
618 ext2_discard_prealloc(VTOI(ap->a_vp));
622 return (ext2_update(ap->a_vp, ap->a_waitfor == MNT_WAIT));
631 struct vop_mknod_args /* {
633 struct vnode **a_vpp;
634 struct componentname *a_cnp;
638 struct vattr *vap = ap->a_vap;
639 struct vnode **vpp = ap->a_vpp;
644 error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
645 ap->a_dvp, vpp, ap->a_cnp);
649 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
650 if (vap->va_rdev != VNOVAL) {
652 * Want to be able to use this to make badblock
653 * inodes, so don't truncate the dev number.
655 ip->i_rdev = vap->va_rdev;
658 * Remove inode, then reload it through VFS_VGET so it is
659 * checked to see if it is an alias of an existing entry in
660 * the inode cache. XXX I don't believe this is necessary now.
662 (*vpp)->v_type = VNON;
663 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
666 error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
676 struct vop_remove_args /* {
679 struct componentname *a_cnp;
683 struct vnode *vp = ap->a_vp;
684 struct vnode *dvp = ap->a_dvp;
688 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
689 (VTOI(dvp)->i_flags & APPEND)) {
693 error = ext2_dirremove(dvp, ap->a_cnp);
696 ip->i_flag |= IN_CHANGE;
707 struct vop_link_args /* {
708 struct vnode *a_tdvp;
710 struct componentname *a_cnp;
713 struct vnode *vp = ap->a_vp;
714 struct vnode *tdvp = ap->a_tdvp;
715 struct componentname *cnp = ap->a_cnp;
720 if ((cnp->cn_flags & HASBUF) == 0)
721 panic("ext2_link: no name");
723 if (tdvp->v_mount != vp->v_mount) {
728 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
732 if (ip->i_flags & (IMMUTABLE | APPEND)) {
737 ip->i_flag |= IN_CHANGE;
738 error = ext2_update(vp, 1);
740 error = ext2_direnter(ip, tdvp, cnp);
743 ip->i_flag |= IN_CHANGE;
750 * Rename system call.
751 * See comments in sys/ufs/ufs/ufs_vnops.c
755 struct vop_rename_args /* {
756 struct vnode *a_fdvp;
758 struct componentname *a_fcnp;
759 struct vnode *a_tdvp;
761 struct componentname *a_tcnp;
764 struct vnode *tvp = ap->a_tvp;
765 struct vnode *tdvp = ap->a_tdvp;
766 struct vnode *fvp = ap->a_fvp;
767 struct vnode *fdvp = ap->a_fdvp;
768 struct componentname *tcnp = ap->a_tcnp;
769 struct componentname *fcnp = ap->a_fcnp;
770 struct thread *td = fcnp->cn_thread;
771 struct inode *ip, *xp, *dp;
772 struct dirtemplate dirbuf;
773 int doingdirectory = 0, oldparent = 0, newparent = 0;
778 if ((tcnp->cn_flags & HASBUF) == 0 ||
779 (fcnp->cn_flags & HASBUF) == 0)
780 panic("ext2_rename: no name");
783 * Check for cross-device rename.
785 if ((fvp->v_mount != tdvp->v_mount) ||
786 (tvp && (fvp->v_mount != tvp->v_mount))) {
800 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
801 (VTOI(tdvp)->i_flags & APPEND))) {
807 * Renaming a file to itself has no effect. The upper layers should
808 * not call us in that case. Temporarily just warn if they do.
811 printf("ext2_rename: fvp == tvp (can't happen)\n");
816 if ((error = vn_lock(fvp, LK_EXCLUSIVE, td)) != 0)
820 if (ip->i_nlink >= LINK_MAX) {
821 VOP_UNLOCK(fvp, 0, td);
825 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
826 || (dp->i_flags & APPEND)) {
827 VOP_UNLOCK(fvp, 0, td);
831 if ((ip->i_mode & IFMT) == IFDIR) {
833 * Avoid ".", "..", and aliases of "." for obvious reasons.
835 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
836 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
837 (ip->i_flag & IN_RENAME)) {
838 VOP_UNLOCK(fvp, 0, td);
842 ip->i_flag |= IN_RENAME;
843 oldparent = dp->i_number;
849 * When the target exists, both the directory
850 * and target vnodes are returned locked.
858 * 1) Bump link count while we're moving stuff
859 * around. If we crash somewhere before
860 * completing our work, the link count
861 * may be wrong, but correctable.
864 ip->i_flag |= IN_CHANGE;
865 if ((error = ext2_update(fvp, 1)) != 0) {
866 VOP_UNLOCK(fvp, 0, td);
871 * If ".." must be changed (ie the directory gets a new
872 * parent) then the source directory must not be in the
873 * directory heirarchy above the target, as this would
874 * orphan everything below the source directory. Also
875 * the user must have write permission in the source so
876 * as to be able to change "..". We must repeat the call
877 * to namei, as the parent directory is unlocked by the
878 * call to checkpath().
880 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
881 VOP_UNLOCK(fvp, 0, td);
882 if (oldparent != dp->i_number)
883 newparent = dp->i_number;
884 if (doingdirectory && newparent) {
885 if (error) /* write access check above */
889 error = ext2_checkpath(ip, dp, tcnp->cn_cred);
893 error = relookup(tdvp, &tvp, tcnp);
903 * 2) If target doesn't exist, link the target
904 * to the source and unlink the source.
905 * Otherwise, rewrite the target directory
906 * entry to reference the source inode and
907 * expunge the original entry's existence.
910 if (dp->i_devvp != ip->i_devvp)
911 panic("ext2_rename: EXDEV");
913 * Account for ".." in new directory.
914 * When source and destination have the same
915 * parent we don't fool with the link count.
917 if (doingdirectory && newparent) {
918 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
923 dp->i_flag |= IN_CHANGE;
924 error = ext2_update(tdvp, 1);
928 error = ext2_direnter(ip, tdvp, tcnp);
930 if (doingdirectory && newparent) {
932 dp->i_flag |= IN_CHANGE;
933 (void)ext2_update(tdvp, 1);
939 if (xp->i_devvp != dp->i_devvp || xp->i_devvp != ip->i_devvp)
940 panic("ext2_rename: EXDEV");
942 * Short circuit rename(foo, foo).
944 if (xp->i_number == ip->i_number)
945 panic("ext2_rename: same file");
947 * If the parent directory is "sticky", then the user must
948 * own the parent directory, or the destination of the rename,
949 * otherwise the destination may not be changed (except by
950 * root). This implements append-only directories.
952 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
953 tcnp->cn_cred->cr_uid != dp->i_uid &&
954 xp->i_uid != tcnp->cn_cred->cr_uid) {
959 * Target must be empty if a directory and have no links
960 * to it. Also, ensure source and target are compatible
961 * (both directories, or both not directories).
963 if ((xp->i_mode&IFMT) == IFDIR) {
964 if (! ext2_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
969 if (!doingdirectory) {
974 } else if (doingdirectory) {
978 error = ext2_dirrewrite(dp, ip, tcnp);
982 * If the target directory is in the same
983 * directory as the source directory,
984 * decrement the link count on the parent
985 * of the target directory.
987 if (doingdirectory && !newparent) {
989 dp->i_flag |= IN_CHANGE;
993 * Adjust the link count of the target to
994 * reflect the dirrewrite above. If this is
995 * a directory it is empty and there are
996 * no links to it, so we can squash the inode and
997 * any space associated with it. We disallowed
998 * renaming over top of a directory with links to
999 * it above, as the remaining link would point to
1000 * a directory without "." or ".." entries.
1003 if (doingdirectory) {
1004 if (--xp->i_nlink != 0)
1005 panic("ext2_rename: linked directory");
1006 error = ext2_truncate(tvp, (off_t)0, IO_SYNC,
1007 tcnp->cn_cred, tcnp->cn_thread);
1009 xp->i_flag |= IN_CHANGE;
1015 * 3) Unlink the source.
1017 fcnp->cn_flags &= ~MODMASK;
1018 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1020 error = relookup(fdvp, &fvp, fcnp);
1028 * From name has disappeared.
1031 panic("ext2_rename: lost dir entry");
1036 * Ensure that the directory entry still exists and has not
1037 * changed while the new name has been entered. If the source is
1038 * a file then the entry may have been unlinked or renamed. In
1039 * either case there is no further work to be done. If the source
1040 * is a directory then it cannot have been rmdir'ed; its link
1041 * count of three would cause a rmdir to fail with ENOTEMPTY.
1042 * The IN_RENAME flag ensures that it cannot be moved by another
1047 panic("ext2_rename: lost dir entry");
1050 * If the source is a directory with a
1051 * new parent, the link count of the old
1052 * parent directory must be decremented
1053 * and ".." set to point to the new parent.
1055 if (doingdirectory && newparent) {
1057 dp->i_flag |= IN_CHANGE;
1058 error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
1059 sizeof (struct dirtemplate), (off_t)0,
1060 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1061 tcnp->cn_cred, NOCRED, (int *)0,
1062 (struct thread *)0);
1064 /* Like ufs little-endian: */
1065 namlen = dirbuf.dotdot_type;
1067 dirbuf.dotdot_name[0] != '.' ||
1068 dirbuf.dotdot_name[1] != '.') {
1069 ext2_dirbad(xp, (doff_t)12,
1070 "rename: mangled dir");
1072 dirbuf.dotdot_ino = newparent;
1073 (void) vn_rdwr(UIO_WRITE, fvp,
1075 sizeof (struct dirtemplate),
1076 (off_t)0, UIO_SYSSPACE,
1077 IO_NODELOCKED | IO_SYNC |
1078 IO_NOMACCHECK, tcnp->cn_cred,
1080 (struct thread *)0);
1085 error = ext2_dirremove(fdvp, fcnp);
1088 xp->i_flag |= IN_CHANGE;
1090 xp->i_flag &= ~IN_RENAME;
1105 ip->i_flag &= ~IN_RENAME;
1106 if (vn_lock(fvp, LK_EXCLUSIVE, td) == 0) {
1108 ip->i_flag |= IN_CHANGE;
1109 ip->i_flag &= ~IN_RENAME;
1121 struct vop_mkdir_args /* {
1122 struct vnode *a_dvp;
1123 struct vnode **a_vpp;
1124 struct componentname *a_cnp;
1125 struct vattr *a_vap;
1128 struct vnode *dvp = ap->a_dvp;
1129 struct vattr *vap = ap->a_vap;
1130 struct componentname *cnp = ap->a_cnp;
1131 struct inode *ip, *dp;
1133 struct dirtemplate dirtemplate, *dtp;
1137 if ((cnp->cn_flags & HASBUF) == 0)
1138 panic("ext2_mkdir: no name");
1141 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1145 dmode = vap->va_mode & 0777;
1148 * Must simulate part of ext2_makeinode here to acquire the inode,
1149 * but not have it entered in the parent directory. The entry is
1150 * made later after writing "." and ".." entries.
1152 error = ext2_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1156 ip->i_gid = dp->i_gid;
1160 * if we are hacking owners here, (only do this where told to)
1161 * and we are not giving it TOO root, (would subvert quotas)
1162 * then go ahead and give it to the other user.
1163 * The new directory also inherits the SUID bit.
1164 * If user's UID and dir UID are the same,
1165 * 'give it away' so that the SUID is still forced on.
1167 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1168 (dp->i_mode & ISUID) && dp->i_uid) {
1170 ip->i_uid = dp->i_uid;
1172 ip->i_uid = cnp->cn_cred->cr_uid;
1176 ip->i_uid = cnp->cn_cred->cr_uid;
1178 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1180 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1182 if (cnp->cn_flags & ISWHITEOUT)
1183 ip->i_flags |= UF_OPAQUE;
1184 error = ext2_update(tvp, 1);
1187 * Bump link count in parent directory
1188 * to reflect work done below. Should
1189 * be done before reference is created
1190 * so reparation is possible if we crash.
1193 dp->i_flag |= IN_CHANGE;
1194 error = ext2_update(dvp, 1);
1198 /* Initialize directory with "." and ".." from static template. */
1199 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs->s_es,
1200 EXT2_FEATURE_INCOMPAT_FILETYPE))
1201 dtp = &mastertemplate;
1203 dtp = &omastertemplate;
1205 dirtemplate.dot_ino = ip->i_number;
1206 dirtemplate.dotdot_ino = dp->i_number;
1207 /* note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE
1208 * so let's just redefine it - for this function only
1211 #define DIRBLKSIZ VTOI(dvp)->i_e2fs->s_blocksize
1212 dirtemplate.dotdot_reclen = DIRBLKSIZ - 12;
1213 error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
1214 sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
1215 IO_NODELOCKED | IO_SYNC | IO_NOMACCHECK, cnp->cn_cred, NOCRED,
1216 (int *)0, (struct thread *)0);
1219 dp->i_flag |= IN_CHANGE;
1222 if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1223 /* XXX should grow with balloc() */
1224 panic("ext2_mkdir: blksize");
1226 ip->i_size = DIRBLKSIZ;
1227 ip->i_flag |= IN_CHANGE;
1230 /* Directory set up, now install its entry in the parent directory. */
1231 error = ext2_direnter(ip, dvp, cnp);
1234 dp->i_flag |= IN_CHANGE;
1238 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1239 * for us because we set the link count to 0.
1243 ip->i_flag |= IN_CHANGE;
1250 #define DIRBLKSIZ DEV_BSIZE
1254 * Rmdir system call.
1258 struct vop_rmdir_args /* {
1259 struct vnode *a_dvp;
1261 struct componentname *a_cnp;
1264 struct vnode *vp = ap->a_vp;
1265 struct vnode *dvp = ap->a_dvp;
1266 struct componentname *cnp = ap->a_cnp;
1267 struct thread *td = cnp->cn_thread;
1268 struct inode *ip, *dp;
1275 * Verify the directory is empty (and valid).
1276 * (Rmdir ".." won't be valid since
1277 * ".." will contain a reference to
1278 * the current directory and thus be
1282 if (ip->i_nlink != 2 || !ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1286 if ((dp->i_flags & APPEND)
1287 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1292 * Delete reference to directory before purging
1293 * inode. If we crash in between, the directory
1294 * will be reattached to lost+found,
1296 error = ext2_dirremove(dvp, cnp);
1300 dp->i_flag |= IN_CHANGE;
1302 VOP_UNLOCK(dvp, 0, td);
1304 * Truncate inode. The only stuff left
1305 * in the directory is "." and "..". The
1306 * "." reference is inconsequential since
1307 * we're quashing it. The ".." reference
1308 * has already been adjusted above. We've
1309 * removed the "." reference and the reference
1310 * in the parent directory, but there may be
1311 * other hard links so decrement by 2 and
1312 * worry about them later.
1315 error = ext2_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred, td);
1316 cache_purge(ITOV(ip));
1317 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
1323 * symlink -- make a symbolic link
1327 struct vop_symlink_args /* {
1328 struct vnode *a_dvp;
1329 struct vnode **a_vpp;
1330 struct componentname *a_cnp;
1331 struct vattr *a_vap;
1335 struct vnode *vp, **vpp = ap->a_vpp;
1339 error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1344 len = strlen(ap->a_target);
1345 if (len < vp->v_mount->mnt_maxsymlinklen) {
1347 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1349 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1351 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1352 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1353 ap->a_cnp->cn_cred, NOCRED, (int *)0, (struct thread *)0);
1360 * Return target name of a symbolic link
1364 struct vop_readlink_args /* {
1367 struct ucred *a_cred;
1370 struct vnode *vp = ap->a_vp;
1371 struct inode *ip = VTOI(vp);
1375 if (isize < vp->v_mount->mnt_maxsymlinklen) {
1376 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1379 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1383 * Calculate the logical to physical mapping if not done already,
1384 * then call the device strategy routine.
1386 * In order to be able to swap to a file, the ext2_bmaparray() operation may not
1387 * deadlock on memory. See ext2_bmap() for details.
1391 struct vop_strategy_args /* {
1396 struct buf *bp = ap->a_bp;
1397 struct vnode *vp = ap->a_vp;
1404 if (vp->v_type == VBLK || vp->v_type == VCHR)
1405 panic("ext2_strategy: spec");
1406 if (bp->b_blkno == bp->b_lblkno) {
1407 error = ext2_bmaparray(vp, bp->b_lblkno, &blkno, NULL, NULL);
1408 bp->b_blkno = blkno;
1410 bp->b_error = error;
1411 bp->b_ioflags |= BIO_ERROR;
1415 if ((long)bp->b_blkno == -1)
1418 if ((long)bp->b_blkno == -1) {
1422 bp->b_iooffset = dbtob(bp->b_blkno);
1423 bo = VFSTOEXT2(vp->v_mount)->um_bo;
1424 BO_STRATEGY(bo, bp);
1429 * Print out the contents of an inode.
1433 struct vop_print_args /* {
1437 struct vnode *vp = ap->a_vp;
1438 struct inode *ip = VTOI(vp);
1440 vn_printf(ip->i_devvp, "\tino %lu", (u_long)ip->i_number);
1441 if (vp->v_type == VFIFO)
1448 * Close wrapper for fifos.
1450 * Update the times on the inode then do device close.
1454 struct vop_close_args /* {
1457 struct ucred *a_cred;
1458 struct thread *a_td;
1461 struct vnode *vp = ap->a_vp;
1464 if (vp->v_usecount > 1)
1467 return (fifo_specops.vop_close(ap));
1471 * Kqfilter wrapper for fifos.
1473 * Fall through to ext2 kqfilter routines if needed
1476 ext2fifo_kqfilter(ap)
1477 struct vop_kqfilter_args *ap;
1481 error = fifo_specops.vop_kqfilter(ap);
1483 error = ext2_kqfilter(ap);
1488 * Return POSIX pathconf information applicable to ext2 filesystems.
1492 struct vop_pathconf_args /* {
1499 switch (ap->a_name) {
1501 *ap->a_retval = LINK_MAX;
1504 *ap->a_retval = NAME_MAX;
1507 *ap->a_retval = PATH_MAX;
1510 *ap->a_retval = PIPE_BUF;
1512 case _PC_CHOWN_RESTRICTED:
1525 * Advisory record locking support
1529 struct vop_advlock_args /* {
1537 struct inode *ip = VTOI(ap->a_vp);
1539 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
1543 * Vnode pointer to File handle
1548 struct vop_vptofh_args /* {
1556 ip = VTOI(ap->a_vp);
1557 ufhp = (struct ufid *)ap->a_fhp;
1558 ufhp->ufid_len = sizeof(struct ufid);
1559 ufhp->ufid_ino = ip->i_number;
1560 ufhp->ufid_gen = ip->i_gen;
1565 * Initialize the vnode associated with a new inode, handle aliased
1569 ext2_vinit(mntp, fifoops, vpp)
1571 struct vop_vector *fifoops;
1579 vp->v_type = IFTOVT(ip->i_mode);
1580 if (vp->v_type == VFIFO)
1583 if (ip->i_number == ROOTINO)
1584 vp->v_vflag |= VV_ROOT;
1585 ip->i_modrev = init_va_filerev();
1591 * Allocate a new inode.
1594 ext2_makeinode(mode, dvp, vpp, cnp)
1598 struct componentname *cnp;
1600 struct inode *ip, *pdir;
1606 if ((cnp->cn_flags & HASBUF) == 0)
1607 panic("ext2_makeinode: no name");
1610 if ((mode & IFMT) == 0)
1613 error = ext2_valloc(dvp, mode, cnp->cn_cred, &tvp);
1618 ip->i_gid = pdir->i_gid;
1623 * not the owner of the directory,
1624 * and we are hacking owners here, (only do this where told to)
1625 * and we are not giving it TOO root, (would subvert quotas)
1626 * then go ahead and give it to the other user.
1627 * Note that this drops off the execute bits for security.
1629 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1630 (pdir->i_mode & ISUID) &&
1631 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1632 ip->i_uid = pdir->i_uid;
1635 ip->i_uid = cnp->cn_cred->cr_uid;
1639 ip->i_uid = cnp->cn_cred->cr_uid;
1641 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1643 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
1645 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred)) {
1646 if (priv_check_cred(cnp->cn_cred, PRIV_VFS_RETAINSUGID, 0))
1647 ip->i_mode &= ~ISGID;
1650 if (cnp->cn_flags & ISWHITEOUT)
1651 ip->i_flags |= UF_OPAQUE;
1654 * Make sure inode goes to disk before directory entry.
1656 error = ext2_update(tvp, 1);
1659 error = ext2_direnter(ip, dvp, cnp);
1668 * Write error occurred trying to update the inode
1669 * or the directory so must deallocate the inode.
1672 ip->i_flag |= IN_CHANGE;
1677 static struct filterops ext2read_filtops =
1678 { 1, NULL, filt_ext2detach, filt_ext2read };
1679 static struct filterops ext2write_filtops =
1680 { 1, NULL, filt_ext2detach, filt_ext2write };
1681 static struct filterops ext2vnode_filtops =
1682 { 1, NULL, filt_ext2detach, filt_ext2vnode };
1686 struct vop_kqfilter_args /* {
1691 struct vnode *vp = ap->a_vp;
1692 struct knote *kn = ap->a_kn;
1694 switch (kn->kn_filter) {
1696 kn->kn_fop = &ext2read_filtops;
1699 kn->kn_fop = &ext2write_filtops;
1702 kn->kn_fop = &ext2vnode_filtops;
1708 kn->kn_hook = (caddr_t)vp;
1710 if (vp->v_pollinfo == NULL)
1712 if (vp->v_pollinfo == NULL)
1714 knlist_add(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
1720 filt_ext2detach(struct knote *kn)
1722 struct vnode *vp = (struct vnode *)kn->kn_hook;
1724 KASSERT(vp->v_pollinfo != NULL, ("Mising v_pollinfo"));
1725 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
1730 filt_ext2read(struct knote *kn, long hint)
1732 struct vnode *vp = (struct vnode *)kn->kn_hook;
1733 struct inode *ip = VTOI(vp);
1736 * filesystem is gone, so set the EOF flag and schedule
1737 * the knote for deletion.
1739 if (hint == NOTE_REVOKE) {
1740 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1744 kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
1745 return (kn->kn_data != 0);
1750 filt_ext2write(struct knote *kn, long hint)
1754 * filesystem is gone, so set the EOF flag and schedule
1755 * the knote for deletion.
1757 if (hint == NOTE_REVOKE)
1758 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1765 filt_ext2vnode(struct knote *kn, long hint)
1768 if (kn->kn_sfflags & hint)
1769 kn->kn_fflags |= hint;
1770 if (hint == NOTE_REVOKE) {
1771 kn->kn_flags |= EV_EOF;
1774 return (kn->kn_fflags != 0);