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>
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 <sys/signalvar.h>
73 #include <ufs/ufs/dir.h>
75 #include <gnu/fs/ext2fs/inode.h>
76 #include <gnu/fs/ext2fs/ext2_mount.h>
77 #include <gnu/fs/ext2fs/ext2_fs_sb.h>
78 #include <gnu/fs/ext2fs/fs.h>
79 #include <gnu/fs/ext2fs/ext2_extern.h>
80 #include <gnu/fs/ext2fs/ext2_fs.h>
82 static int ext2_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
84 static vop_access_t ext2_access;
85 static vop_advlock_t ext2_advlock;
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_kqfilter_t ext2_kqfilter;
94 static vop_link_t ext2_link;
95 static vop_mkdir_t ext2_mkdir;
96 static vop_mknod_t ext2_mknod;
97 static vop_open_t ext2_open;
98 static vop_pathconf_t ext2_pathconf;
99 static vop_print_t ext2_print;
100 static vop_read_t ext2_read;
101 static vop_readlink_t ext2_readlink;
102 static vop_remove_t ext2_remove;
103 static vop_rename_t ext2_rename;
104 static vop_rmdir_t ext2_rmdir;
105 static vop_setattr_t ext2_setattr;
106 static vop_strategy_t ext2_strategy;
107 static vop_symlink_t ext2_symlink;
108 static vop_write_t ext2_write;
109 static vop_close_t ext2fifo_close;
110 static vop_kqfilter_t ext2fifo_kqfilter;
111 static int filt_ext2read(struct knote *kn, long hint);
112 static int filt_ext2write(struct knote *kn, long hint);
113 static int filt_ext2vnode(struct knote *kn, long hint);
114 static void filt_ext2detach(struct knote *kn);
116 /* Global vfs data structures for ext2. */
117 struct vop_vector ext2_vnodeops = {
118 .vop_default = &default_vnodeops,
119 .vop_access = ext2_access,
120 .vop_advlock = ext2_advlock,
121 .vop_bmap = ext2_bmap,
122 .vop_cachedlookup = ext2_lookup,
123 .vop_close = ext2_close,
124 .vop_create = ext2_create,
125 .vop_fsync = ext2_fsync,
126 .vop_getattr = ext2_getattr,
127 .vop_inactive = ext2_inactive,
128 .vop_link = ext2_link,
129 .vop_lookup = vfs_cache_lookup,
130 .vop_mkdir = ext2_mkdir,
131 .vop_mknod = ext2_mknod,
132 .vop_open = ext2_open,
133 .vop_pathconf = ext2_pathconf,
134 .vop_poll = vop_stdpoll,
135 .vop_kqfilter = ext2_kqfilter,
136 .vop_print = ext2_print,
137 .vop_read = ext2_read,
138 .vop_readdir = ext2_readdir,
139 .vop_readlink = ext2_readlink,
140 .vop_reallocblks = ext2_reallocblks,
141 .vop_reclaim = ext2_reclaim,
142 .vop_remove = ext2_remove,
143 .vop_rename = ext2_rename,
144 .vop_rmdir = ext2_rmdir,
145 .vop_setattr = ext2_setattr,
146 .vop_strategy = ext2_strategy,
147 .vop_symlink = ext2_symlink,
148 .vop_write = ext2_write,
151 struct vop_vector ext2_fifoops = {
152 .vop_default = &fifo_specops,
153 .vop_access = ext2_access,
154 .vop_close = ext2fifo_close,
155 .vop_fsync = ext2_fsync,
156 .vop_getattr = ext2_getattr,
157 .vop_inactive = ext2_inactive,
158 .vop_kqfilter = ext2fifo_kqfilter,
159 .vop_print = ext2_print,
160 .vop_read = VOP_PANIC,
161 .vop_reclaim = ext2_reclaim,
162 .vop_setattr = ext2_setattr,
163 .vop_write = VOP_PANIC,
166 #include <gnu/fs/ext2fs/ext2_readwrite.c>
169 * A virgin directory (no blushing please).
170 * Note that the type and namlen fields are reversed relative to ext2.
171 * Also, we don't use `struct odirtemplate', since it would just cause
172 * endianness problems.
174 static struct dirtemplate mastertemplate = {
175 0, 12, 1, EXT2_FT_DIR, ".",
176 0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
178 static struct dirtemplate omastertemplate = {
179 0, 12, 1, EXT2_FT_UNKNOWN, ".",
180 0, DIRBLKSIZ - 12, 2, EXT2_FT_UNKNOWN, ".."
191 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
193 if ((vp->v_type == VBLK || vp->v_type == VCHR))
194 ip->i_flag |= IN_LAZYMOD;
196 ip->i_flag |= IN_MODIFIED;
197 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
199 if (ip->i_flag & IN_ACCESS) {
200 ip->i_atime = ts.tv_sec;
201 ip->i_atimensec = ts.tv_nsec;
203 if (ip->i_flag & IN_UPDATE) {
204 ip->i_mtime = ts.tv_sec;
205 ip->i_mtimensec = ts.tv_nsec;
208 if (ip->i_flag & IN_CHANGE) {
209 ip->i_ctime = ts.tv_sec;
210 ip->i_ctimensec = ts.tv_nsec;
213 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
217 * Create a regular file
221 struct vop_create_args /* {
223 struct vnode **a_vpp;
224 struct componentname *a_cnp;
231 ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
232 ap->a_dvp, ap->a_vpp, ap->a_cnp);
240 struct vop_open_args /* {
243 struct ucred *a_cred;
248 if (ap->a_vp->v_type == VBLK || ap->a_vp->v_type == VCHR)
252 * Files marked append-only must be opened for appending.
254 if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
255 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
258 vnode_create_vobject(ap->a_vp, VTOI(ap->a_vp)->i_size, ap->a_td);
266 * Update the times on the inode.
270 struct vop_close_args /* {
273 struct ucred *a_cred;
277 struct vnode *vp = ap->a_vp;
280 if (vp->v_usecount > 1)
288 struct vop_access_args /* {
291 struct ucred *a_cred;
295 struct vnode *vp = ap->a_vp;
296 struct inode *ip = VTOI(vp);
297 mode_t mode = ap->a_mode;
300 if (vp->v_type == VBLK || vp->v_type == VCHR)
304 * Disallow write attempts on read-only file systems;
305 * unless the file is a socket, fifo, or a block or
306 * character device resident on the file system.
309 switch (vp->v_type) {
313 if (vp->v_mount->mnt_flag & MNT_RDONLY)
321 /* If immutable bit set, nobody gets to write it. */
322 if ((mode & VWRITE) && (ip->i_flags & (IMMUTABLE | SF_SNAPSHOT)))
325 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
326 ap->a_mode, ap->a_cred, NULL);
332 struct vop_getattr_args /* {
335 struct ucred *a_cred;
339 struct vnode *vp = ap->a_vp;
340 struct inode *ip = VTOI(vp);
341 struct vattr *vap = ap->a_vap;
345 * Copy from inode table
347 vap->va_fsid = dev2udev(ip->i_devvp->v_rdev);
348 vap->va_fileid = ip->i_number;
349 vap->va_mode = ip->i_mode & ~IFMT;
350 vap->va_nlink = ip->i_nlink;
351 vap->va_uid = ip->i_uid;
352 vap->va_gid = ip->i_gid;
353 vap->va_rdev = ip->i_rdev;
354 vap->va_size = ip->i_size;
355 vap->va_atime.tv_sec = ip->i_atime;
356 vap->va_atime.tv_nsec = ip->i_atimensec;
357 vap->va_mtime.tv_sec = ip->i_mtime;
358 vap->va_mtime.tv_nsec = ip->i_mtimensec;
359 vap->va_ctime.tv_sec = ip->i_ctime;
360 vap->va_ctime.tv_nsec = ip->i_ctimensec;
361 vap->va_flags = ip->i_flags;
362 vap->va_gen = ip->i_gen;
363 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
364 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
365 vap->va_type = IFTOVT(ip->i_mode);
366 vap->va_filerev = ip->i_modrev;
371 * Set attribute vnode op. called from several syscalls
375 struct vop_setattr_args /* {
378 struct ucred *a_cred;
382 struct vattr *vap = ap->a_vap;
383 struct vnode *vp = ap->a_vp;
384 struct inode *ip = VTOI(vp);
385 struct ucred *cred = ap->a_cred;
386 struct thread *td = ap->a_td;
390 * Check for unsettable attributes.
392 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
393 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
394 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
395 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
398 if (vap->va_flags != VNOVAL) {
399 if (vp->v_mount->mnt_flag & MNT_RDONLY)
402 * Callers may only modify the file flags on objects they
403 * have VADMIN rights for.
405 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
408 * Unprivileged processes and privileged processes in
409 * jail() are not permitted to unset system flags, or
410 * modify flags if any system flags are set.
411 * Privileged non-jail processes may not modify system flags
412 * if securelevel > 0 and any existing system flags are set.
414 if (!suser_cred(cred, SUSER_ALLOWJAIL)) {
416 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
417 error = securelevel_gt(cred, 0);
421 ip->i_flags = vap->va_flags;
424 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
425 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
427 ip->i_flags &= SF_SETTABLE;
428 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
430 ip->i_flag |= IN_CHANGE;
431 if (vap->va_flags & (IMMUTABLE | APPEND))
434 if (ip->i_flags & (IMMUTABLE | APPEND))
437 * Go through the fields and update iff not VNOVAL.
439 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
440 if (vp->v_mount->mnt_flag & MNT_RDONLY)
442 if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred,
446 if (vap->va_size != VNOVAL) {
448 * Disallow write attempts on read-only file systems;
449 * unless the file is a socket, fifo, or a block or
450 * character device resident on the file system.
452 switch (vp->v_type) {
457 if (vp->v_mount->mnt_flag & MNT_RDONLY)
463 if ((error = ext2_truncate(vp, vap->va_size, 0, cred, td)) != 0)
466 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
467 if (vp->v_mount->mnt_flag & MNT_RDONLY)
471 * If times is NULL, ... The caller must be the owner of
472 * the file, have permission to write the file, or be the
474 * If times is non-NULL, ... The caller must be the owner of
475 * the file or be the super-user.
477 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)) &&
478 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
479 (error = VOP_ACCESS(vp, VWRITE, cred, td))))
481 if (vap->va_atime.tv_sec != VNOVAL)
482 ip->i_flag |= IN_ACCESS;
483 if (vap->va_mtime.tv_sec != VNOVAL)
484 ip->i_flag |= IN_CHANGE | IN_UPDATE;
486 if (vap->va_atime.tv_sec != VNOVAL) {
487 ip->i_atime = vap->va_atime.tv_sec;
488 ip->i_atimensec = vap->va_atime.tv_nsec;
490 if (vap->va_mtime.tv_sec != VNOVAL) {
491 ip->i_mtime = vap->va_mtime.tv_sec;
492 ip->i_mtimensec = vap->va_mtime.tv_nsec;
494 error = ext2_update(vp, 0);
499 if (vap->va_mode != (mode_t)VNOVAL) {
500 if (vp->v_mount->mnt_flag & MNT_RDONLY)
502 error = ext2_chmod(vp, (int)vap->va_mode, cred, td);
508 * Change the mode on a file.
509 * Inode must be locked before calling.
512 ext2_chmod(vp, mode, cred, td)
518 struct inode *ip = VTOI(vp);
522 * To modify the permissions on a file, must possess VADMIN
525 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
528 * Privileged processes may set the sticky bit on non-directories,
529 * as well as set the setgid bit on a file with a group that the
530 * process is not a member of.
532 if (suser_cred(cred, SUSER_ALLOWJAIL)) {
533 if (vp->v_type != VDIR && (mode & S_ISTXT))
535 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
538 ip->i_mode &= ~ALLPERMS;
539 ip->i_mode |= (mode & ALLPERMS);
540 ip->i_flag |= IN_CHANGE;
545 * Perform chown operation on inode ip;
546 * inode must be locked prior to call.
549 ext2_chown(vp, uid, gid, cred, td)
556 struct inode *ip = VTOI(vp);
561 if (uid == (uid_t)VNOVAL)
563 if (gid == (gid_t)VNOVAL)
566 * To modify the ownership of a file, must possess VADMIN
569 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
572 * To change the owner of a file, or change the group of a file
573 * to a group of which we are not a member, the caller must
576 if ((uid != ip->i_uid ||
577 (gid != ip->i_gid && !groupmember(gid, cred))) &&
578 (error = suser_cred(cred, SUSER_ALLOWJAIL)))
584 ip->i_flag |= IN_CHANGE;
585 if (suser_cred(cred, SUSER_ALLOWJAIL) && (ouid != uid || ogid != gid))
586 ip->i_mode &= ~(ISUID | ISGID);
591 * Synch an open file.
596 struct vop_fsync_args /* {
598 struct ucred *a_cred;
604 * Flush all dirty buffers associated with a vnode.
606 ext2_discard_prealloc(VTOI(ap->a_vp));
610 return (ext2_update(ap->a_vp, ap->a_waitfor == MNT_WAIT));
619 struct vop_mknod_args /* {
621 struct vnode **a_vpp;
622 struct componentname *a_cnp;
626 struct vattr *vap = ap->a_vap;
627 struct vnode **vpp = ap->a_vpp;
632 error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
633 ap->a_dvp, vpp, ap->a_cnp);
637 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
638 if (vap->va_rdev != VNOVAL) {
640 * Want to be able to use this to make badblock
641 * inodes, so don't truncate the dev number.
643 ip->i_rdev = vap->va_rdev;
646 * Remove inode, then reload it through VFS_VGET so it is
647 * checked to see if it is an alias of an existing entry in
648 * the inode cache. XXX I don't believe this is necessary now.
650 (*vpp)->v_type = VNON;
651 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
654 error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
664 struct vop_remove_args /* {
667 struct componentname *a_cnp;
671 struct vnode *vp = ap->a_vp;
672 struct vnode *dvp = ap->a_dvp;
676 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
677 (VTOI(dvp)->i_flags & APPEND)) {
681 error = ext2_dirremove(dvp, ap->a_cnp);
684 ip->i_flag |= IN_CHANGE;
695 struct vop_link_args /* {
696 struct vnode *a_tdvp;
698 struct componentname *a_cnp;
701 struct vnode *vp = ap->a_vp;
702 struct vnode *tdvp = ap->a_tdvp;
703 struct componentname *cnp = ap->a_cnp;
708 if ((cnp->cn_flags & HASBUF) == 0)
709 panic("ext2_link: no name");
711 if (tdvp->v_mount != vp->v_mount) {
716 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
720 if (ip->i_flags & (IMMUTABLE | APPEND)) {
725 ip->i_flag |= IN_CHANGE;
726 error = ext2_update(vp, 1);
728 error = ext2_direnter(ip, tdvp, cnp);
731 ip->i_flag |= IN_CHANGE;
738 * Rename system call.
739 * See comments in sys/ufs/ufs/ufs_vnops.c
743 struct vop_rename_args /* {
744 struct vnode *a_fdvp;
746 struct componentname *a_fcnp;
747 struct vnode *a_tdvp;
749 struct componentname *a_tcnp;
752 struct vnode *tvp = ap->a_tvp;
753 struct vnode *tdvp = ap->a_tdvp;
754 struct vnode *fvp = ap->a_fvp;
755 struct vnode *fdvp = ap->a_fdvp;
756 struct componentname *tcnp = ap->a_tcnp;
757 struct componentname *fcnp = ap->a_fcnp;
758 struct thread *td = fcnp->cn_thread;
759 struct inode *ip, *xp, *dp;
760 struct dirtemplate dirbuf;
761 int doingdirectory = 0, oldparent = 0, newparent = 0;
766 if ((tcnp->cn_flags & HASBUF) == 0 ||
767 (fcnp->cn_flags & HASBUF) == 0)
768 panic("ext2_rename: no name");
771 * Check for cross-device rename.
773 if ((fvp->v_mount != tdvp->v_mount) ||
774 (tvp && (fvp->v_mount != tvp->v_mount))) {
788 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
789 (VTOI(tdvp)->i_flags & APPEND))) {
795 * Renaming a file to itself has no effect. The upper layers should
796 * not call us in that case. Temporarily just warn if they do.
799 printf("ext2_rename: fvp == tvp (can't happen)\n");
804 if ((error = vn_lock(fvp, LK_EXCLUSIVE, td)) != 0)
808 if (ip->i_nlink >= LINK_MAX) {
809 VOP_UNLOCK(fvp, 0, td);
813 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
814 || (dp->i_flags & APPEND)) {
815 VOP_UNLOCK(fvp, 0, td);
819 if ((ip->i_mode & IFMT) == IFDIR) {
821 * Avoid ".", "..", and aliases of "." for obvious reasons.
823 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
824 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
825 (ip->i_flag & IN_RENAME)) {
826 VOP_UNLOCK(fvp, 0, td);
830 ip->i_flag |= IN_RENAME;
831 oldparent = dp->i_number;
837 * When the target exists, both the directory
838 * and target vnodes are returned locked.
846 * 1) Bump link count while we're moving stuff
847 * around. If we crash somewhere before
848 * completing our work, the link count
849 * may be wrong, but correctable.
852 ip->i_flag |= IN_CHANGE;
853 if ((error = ext2_update(fvp, 1)) != 0) {
854 VOP_UNLOCK(fvp, 0, td);
859 * If ".." must be changed (ie the directory gets a new
860 * parent) then the source directory must not be in the
861 * directory heirarchy above the target, as this would
862 * orphan everything below the source directory. Also
863 * the user must have write permission in the source so
864 * as to be able to change "..". We must repeat the call
865 * to namei, as the parent directory is unlocked by the
866 * call to checkpath().
868 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
869 VOP_UNLOCK(fvp, 0, td);
870 if (oldparent != dp->i_number)
871 newparent = dp->i_number;
872 if (doingdirectory && newparent) {
873 if (error) /* write access check above */
877 error = ext2_checkpath(ip, dp, tcnp->cn_cred);
881 error = relookup(tdvp, &tvp, tcnp);
891 * 2) If target doesn't exist, link the target
892 * to the source and unlink the source.
893 * Otherwise, rewrite the target directory
894 * entry to reference the source inode and
895 * expunge the original entry's existence.
898 if (dp->i_devvp != ip->i_devvp)
899 panic("ext2_rename: EXDEV");
901 * Account for ".." in new directory.
902 * When source and destination have the same
903 * parent we don't fool with the link count.
905 if (doingdirectory && newparent) {
906 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
911 dp->i_flag |= IN_CHANGE;
912 error = ext2_update(tdvp, 1);
916 error = ext2_direnter(ip, tdvp, tcnp);
918 if (doingdirectory && newparent) {
920 dp->i_flag |= IN_CHANGE;
921 (void)ext2_update(tdvp, 1);
927 if (xp->i_devvp != dp->i_devvp || xp->i_devvp != ip->i_devvp)
928 panic("ext2_rename: EXDEV");
930 * Short circuit rename(foo, foo).
932 if (xp->i_number == ip->i_number)
933 panic("ext2_rename: same file");
935 * If the parent directory is "sticky", then the user must
936 * own the parent directory, or the destination of the rename,
937 * otherwise the destination may not be changed (except by
938 * root). This implements append-only directories.
940 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
941 tcnp->cn_cred->cr_uid != dp->i_uid &&
942 xp->i_uid != tcnp->cn_cred->cr_uid) {
947 * Target must be empty if a directory and have no links
948 * to it. Also, ensure source and target are compatible
949 * (both directories, or both not directories).
951 if ((xp->i_mode&IFMT) == IFDIR) {
952 if (! ext2_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
957 if (!doingdirectory) {
962 } else if (doingdirectory) {
966 error = ext2_dirrewrite(dp, ip, tcnp);
970 * If the target directory is in the same
971 * directory as the source directory,
972 * decrement the link count on the parent
973 * of the target directory.
975 if (doingdirectory && !newparent) {
977 dp->i_flag |= IN_CHANGE;
981 * Adjust the link count of the target to
982 * reflect the dirrewrite above. If this is
983 * a directory it is empty and there are
984 * no links to it, so we can squash the inode and
985 * any space associated with it. We disallowed
986 * renaming over top of a directory with links to
987 * it above, as the remaining link would point to
988 * a directory without "." or ".." entries.
991 if (doingdirectory) {
992 if (--xp->i_nlink != 0)
993 panic("ext2_rename: linked directory");
994 error = ext2_truncate(tvp, (off_t)0, IO_SYNC,
995 tcnp->cn_cred, tcnp->cn_thread);
997 xp->i_flag |= IN_CHANGE;
1003 * 3) Unlink the source.
1005 fcnp->cn_flags &= ~MODMASK;
1006 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1008 error = relookup(fdvp, &fvp, fcnp);
1016 * From name has disappeared.
1019 panic("ext2_rename: lost dir entry");
1024 * Ensure that the directory entry still exists and has not
1025 * changed while the new name has been entered. If the source is
1026 * a file then the entry may have been unlinked or renamed. In
1027 * either case there is no further work to be done. If the source
1028 * is a directory then it cannot have been rmdir'ed; its link
1029 * count of three would cause a rmdir to fail with ENOTEMPTY.
1030 * The IN_RENAME flag ensures that it cannot be moved by another
1035 panic("ext2_rename: lost dir entry");
1038 * If the source is a directory with a
1039 * new parent, the link count of the old
1040 * parent directory must be decremented
1041 * and ".." set to point to the new parent.
1043 if (doingdirectory && newparent) {
1045 dp->i_flag |= IN_CHANGE;
1046 error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
1047 sizeof (struct dirtemplate), (off_t)0,
1048 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1049 tcnp->cn_cred, NOCRED, (int *)0,
1050 (struct thread *)0);
1052 /* Like ufs little-endian: */
1053 namlen = dirbuf.dotdot_type;
1055 dirbuf.dotdot_name[0] != '.' ||
1056 dirbuf.dotdot_name[1] != '.') {
1057 ext2_dirbad(xp, (doff_t)12,
1058 "rename: mangled dir");
1060 dirbuf.dotdot_ino = newparent;
1061 (void) vn_rdwr(UIO_WRITE, fvp,
1063 sizeof (struct dirtemplate),
1064 (off_t)0, UIO_SYSSPACE,
1065 IO_NODELOCKED | IO_SYNC |
1066 IO_NOMACCHECK, tcnp->cn_cred,
1068 (struct thread *)0);
1073 error = ext2_dirremove(fdvp, fcnp);
1076 xp->i_flag |= IN_CHANGE;
1078 xp->i_flag &= ~IN_RENAME;
1093 ip->i_flag &= ~IN_RENAME;
1094 if (vn_lock(fvp, LK_EXCLUSIVE, td) == 0) {
1096 ip->i_flag |= IN_CHANGE;
1097 ip->i_flag &= ~IN_RENAME;
1109 struct vop_mkdir_args /* {
1110 struct vnode *a_dvp;
1111 struct vnode **a_vpp;
1112 struct componentname *a_cnp;
1113 struct vattr *a_vap;
1116 struct vnode *dvp = ap->a_dvp;
1117 struct vattr *vap = ap->a_vap;
1118 struct componentname *cnp = ap->a_cnp;
1119 struct inode *ip, *dp;
1121 struct dirtemplate dirtemplate, *dtp;
1125 if ((cnp->cn_flags & HASBUF) == 0)
1126 panic("ext2_mkdir: no name");
1129 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1133 dmode = vap->va_mode & 0777;
1136 * Must simulate part of ext2_makeinode here to acquire the inode,
1137 * but not have it entered in the parent directory. The entry is
1138 * made later after writing "." and ".." entries.
1140 error = ext2_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1144 ip->i_gid = dp->i_gid;
1148 * if we are hacking owners here, (only do this where told to)
1149 * and we are not giving it TOO root, (would subvert quotas)
1150 * then go ahead and give it to the other user.
1151 * The new directory also inherits the SUID bit.
1152 * If user's UID and dir UID are the same,
1153 * 'give it away' so that the SUID is still forced on.
1155 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1156 (dp->i_mode & ISUID) && dp->i_uid) {
1158 ip->i_uid = dp->i_uid;
1160 ip->i_uid = cnp->cn_cred->cr_uid;
1164 ip->i_uid = cnp->cn_cred->cr_uid;
1166 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1168 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1170 if (cnp->cn_flags & ISWHITEOUT)
1171 ip->i_flags |= UF_OPAQUE;
1172 error = ext2_update(tvp, 1);
1175 * Bump link count in parent directory
1176 * to reflect work done below. Should
1177 * be done before reference is created
1178 * so reparation is possible if we crash.
1181 dp->i_flag |= IN_CHANGE;
1182 error = ext2_update(dvp, 1);
1186 /* Initialize directory with "." and ".." from static template. */
1187 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs->s_es,
1188 EXT2_FEATURE_INCOMPAT_FILETYPE))
1189 dtp = &mastertemplate;
1191 dtp = &omastertemplate;
1193 dirtemplate.dot_ino = ip->i_number;
1194 dirtemplate.dotdot_ino = dp->i_number;
1195 /* note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE
1196 * so let's just redefine it - for this function only
1199 #define DIRBLKSIZ VTOI(dvp)->i_e2fs->s_blocksize
1200 dirtemplate.dotdot_reclen = DIRBLKSIZ - 12;
1201 error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
1202 sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
1203 IO_NODELOCKED | IO_SYNC | IO_NOMACCHECK, cnp->cn_cred, NOCRED,
1204 (int *)0, (struct thread *)0);
1207 dp->i_flag |= IN_CHANGE;
1210 if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1211 /* XXX should grow with balloc() */
1212 panic("ext2_mkdir: blksize");
1214 ip->i_size = DIRBLKSIZ;
1215 ip->i_flag |= IN_CHANGE;
1218 /* Directory set up, now install its entry in the parent directory. */
1219 error = ext2_direnter(ip, dvp, cnp);
1222 dp->i_flag |= IN_CHANGE;
1226 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1227 * for us because we set the link count to 0.
1231 ip->i_flag |= IN_CHANGE;
1238 #define DIRBLKSIZ DEV_BSIZE
1242 * Rmdir system call.
1246 struct vop_rmdir_args /* {
1247 struct vnode *a_dvp;
1249 struct componentname *a_cnp;
1252 struct vnode *vp = ap->a_vp;
1253 struct vnode *dvp = ap->a_dvp;
1254 struct componentname *cnp = ap->a_cnp;
1255 struct thread *td = cnp->cn_thread;
1256 struct inode *ip, *dp;
1263 * Verify the directory is empty (and valid).
1264 * (Rmdir ".." won't be valid since
1265 * ".." will contain a reference to
1266 * the current directory and thus be
1270 if (ip->i_nlink != 2 || !ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1274 if ((dp->i_flags & APPEND)
1275 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1280 * Delete reference to directory before purging
1281 * inode. If we crash in between, the directory
1282 * will be reattached to lost+found,
1284 error = ext2_dirremove(dvp, cnp);
1288 dp->i_flag |= IN_CHANGE;
1290 VOP_UNLOCK(dvp, 0, td);
1292 * Truncate inode. The only stuff left
1293 * in the directory is "." and "..". The
1294 * "." reference is inconsequential since
1295 * we're quashing it. The ".." reference
1296 * has already been adjusted above. We've
1297 * removed the "." reference and the reference
1298 * in the parent directory, but there may be
1299 * other hard links so decrement by 2 and
1300 * worry about them later.
1303 error = ext2_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred, td);
1304 cache_purge(ITOV(ip));
1305 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
1311 * symlink -- make a symbolic link
1315 struct vop_symlink_args /* {
1316 struct vnode *a_dvp;
1317 struct vnode **a_vpp;
1318 struct componentname *a_cnp;
1319 struct vattr *a_vap;
1323 struct vnode *vp, **vpp = ap->a_vpp;
1327 error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1332 len = strlen(ap->a_target);
1333 if (len < vp->v_mount->mnt_maxsymlinklen) {
1335 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1337 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1339 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1340 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1341 ap->a_cnp->cn_cred, NOCRED, (int *)0, (struct thread *)0);
1348 * Return target name of a symbolic link
1352 struct vop_readlink_args /* {
1355 struct ucred *a_cred;
1358 struct vnode *vp = ap->a_vp;
1359 struct inode *ip = VTOI(vp);
1363 if (isize < vp->v_mount->mnt_maxsymlinklen) {
1364 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1367 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1371 * Calculate the logical to physical mapping if not done already,
1372 * then call the device strategy routine.
1374 * In order to be able to swap to a file, the ext2_bmaparray() operation may not
1375 * deadlock on memory. See ext2_bmap() for details.
1379 struct vop_strategy_args /* {
1384 struct buf *bp = ap->a_bp;
1385 struct vnode *vp = ap->a_vp;
1392 if (vp->v_type == VBLK || vp->v_type == VCHR)
1393 panic("ext2_strategy: spec");
1394 if (bp->b_blkno == bp->b_lblkno) {
1395 error = ext2_bmaparray(vp, bp->b_lblkno, &blkno, NULL, NULL);
1396 bp->b_blkno = blkno;
1398 bp->b_error = error;
1399 bp->b_ioflags |= BIO_ERROR;
1403 if ((long)bp->b_blkno == -1)
1406 if ((long)bp->b_blkno == -1) {
1410 bp->b_iooffset = dbtob(bp->b_blkno);
1411 bo = VFSTOEXT2(vp->v_mount)->um_bo;
1412 BO_STRATEGY(bo, bp);
1417 * Print out the contents of an inode.
1421 struct vop_print_args /* {
1425 struct vnode *vp = ap->a_vp;
1426 struct inode *ip = VTOI(vp);
1428 vn_printf(ip->i_devvp, "\tino %lu", (u_long)ip->i_number);
1429 if (vp->v_type == VFIFO)
1436 * Close wrapper for fifos.
1438 * Update the times on the inode then do device close.
1442 struct vop_close_args /* {
1445 struct ucred *a_cred;
1446 struct thread *a_td;
1449 struct vnode *vp = ap->a_vp;
1452 if (vp->v_usecount > 1)
1455 return (fifo_specops.vop_close(ap));
1459 * Kqfilter wrapper for fifos.
1461 * Fall through to ext2 kqfilter routines if needed
1464 ext2fifo_kqfilter(ap)
1465 struct vop_kqfilter_args *ap;
1469 error = fifo_specops.vop_kqfilter(ap);
1471 error = ext2_kqfilter(ap);
1476 * Return POSIX pathconf information applicable to ext2 filesystems.
1480 struct vop_pathconf_args /* {
1487 switch (ap->a_name) {
1489 *ap->a_retval = LINK_MAX;
1492 *ap->a_retval = NAME_MAX;
1495 *ap->a_retval = PATH_MAX;
1498 *ap->a_retval = PIPE_BUF;
1500 case _PC_CHOWN_RESTRICTED:
1513 * Advisory record locking support
1517 struct vop_advlock_args /* {
1525 struct inode *ip = VTOI(ap->a_vp);
1527 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
1531 * Initialize the vnode associated with a new inode, handle aliased
1535 ext2_vinit(mntp, fifoops, vpp)
1537 struct vop_vector *fifoops;
1545 vp->v_type = IFTOVT(ip->i_mode);
1546 if (vp->v_type == VFIFO)
1549 if (ip->i_number == ROOTINO)
1550 vp->v_vflag |= VV_ROOT;
1551 ip->i_modrev = init_va_filerev();
1557 * Allocate a new inode.
1560 ext2_makeinode(mode, dvp, vpp, cnp)
1564 struct componentname *cnp;
1566 struct inode *ip, *pdir;
1572 if ((cnp->cn_flags & HASBUF) == 0)
1573 panic("ext2_makeinode: no name");
1576 if ((mode & IFMT) == 0)
1579 error = ext2_valloc(dvp, mode, cnp->cn_cred, &tvp);
1584 ip->i_gid = pdir->i_gid;
1589 * not the owner of the directory,
1590 * and we are hacking owners here, (only do this where told to)
1591 * and we are not giving it TOO root, (would subvert quotas)
1592 * then go ahead and give it to the other user.
1593 * Note that this drops off the execute bits for security.
1595 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1596 (pdir->i_mode & ISUID) &&
1597 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1598 ip->i_uid = pdir->i_uid;
1601 ip->i_uid = cnp->cn_cred->cr_uid;
1605 ip->i_uid = cnp->cn_cred->cr_uid;
1607 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1609 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
1611 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
1612 suser_cred(cnp->cn_cred, SUSER_ALLOWJAIL))
1613 ip->i_mode &= ~ISGID;
1615 if (cnp->cn_flags & ISWHITEOUT)
1616 ip->i_flags |= UF_OPAQUE;
1619 * Make sure inode goes to disk before directory entry.
1621 error = ext2_update(tvp, 1);
1624 error = ext2_direnter(ip, dvp, cnp);
1633 * Write error occurred trying to update the inode
1634 * or the directory so must deallocate the inode.
1637 ip->i_flag |= IN_CHANGE;
1642 static struct filterops ext2read_filtops =
1643 { 1, NULL, filt_ext2detach, filt_ext2read };
1644 static struct filterops ext2write_filtops =
1645 { 1, NULL, filt_ext2detach, filt_ext2write };
1646 static struct filterops ext2vnode_filtops =
1647 { 1, NULL, filt_ext2detach, filt_ext2vnode };
1651 struct vop_kqfilter_args /* {
1656 struct vnode *vp = ap->a_vp;
1657 struct knote *kn = ap->a_kn;
1659 switch (kn->kn_filter) {
1661 kn->kn_fop = &ext2read_filtops;
1664 kn->kn_fop = &ext2write_filtops;
1667 kn->kn_fop = &ext2vnode_filtops;
1673 kn->kn_hook = (caddr_t)vp;
1675 if (vp->v_pollinfo == NULL)
1677 if (vp->v_pollinfo == NULL)
1679 knlist_add(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
1685 filt_ext2detach(struct knote *kn)
1687 struct vnode *vp = (struct vnode *)kn->kn_hook;
1689 KASSERT(vp->v_pollinfo != NULL, ("Mising v_pollinfo"));
1690 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
1695 filt_ext2read(struct knote *kn, long hint)
1697 struct vnode *vp = (struct vnode *)kn->kn_hook;
1698 struct inode *ip = VTOI(vp);
1701 * filesystem is gone, so set the EOF flag and schedule
1702 * the knote for deletion.
1704 if (hint == NOTE_REVOKE) {
1705 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1709 kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
1710 return (kn->kn_data != 0);
1715 filt_ext2write(struct knote *kn, long hint)
1719 * filesystem is gone, so set the EOF flag and schedule
1720 * the knote for deletion.
1722 if (hint == NOTE_REVOKE)
1723 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1730 filt_ext2vnode(struct knote *kn, long hint)
1733 if (kn->kn_sfflags & hint)
1734 kn->kn_fflags |= hint;
1735 if (hint == NOTE_REVOKE) {
1736 kn->kn_flags |= EV_EOF;
1739 return (kn->kn_fflags != 0);