2 * modified for EXT2FS support in Lites 1.1
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
8 * Copyright (c) 1982, 1986, 1989, 1993
9 * The Regents of the University of California. All rights reserved.
10 * (c) UNIX System Laboratories, Inc.
11 * All or some portions of this file are derived from material licensed
12 * to the University of California by American Telephone and Telegraph
13 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
14 * the permission of UNIX System Laboratories, Inc.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
45 * @(#)ext2_vnops.c 8.7 (Berkeley) 2/3/94
46 * $FreeBSD: src/sys/gnu/ext2fs/ext2_vnops.c,v 1.51.2.2 2003/01/02 17:26:18 bde Exp $
47 * $DragonFly: src/sys/vfs/gnu/ext2fs/ext2_vnops.c,v 1.4 2003/07/19 21:14:32 dillon Exp $
50 #include "opt_quota.h"
51 #include "opt_suiddir.h"
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/resourcevar.h>
56 #include <sys/kernel.h>
60 #include <sys/mount.h>
62 #include <sys/vnode.h>
63 #include <sys/namei.h>
66 #include <vm/vm_extern.h>
67 #include <vm/vm_zone.h>
68 #include <vm/vnode_pager.h>
70 #include <sys/signalvar.h>
71 #include <ufs/ufs/dir.h>
72 #include <ufs/ufs/quota.h>
73 #include <ufs/ufs/inode.h>
74 #include <ufs/ufs/ufsmount.h>
75 #include <ufs/ufs/ufs_extern.h>
77 #include <gnu/ext2fs/ext2_fs_sb.h>
78 #include <gnu/ext2fs/fs.h>
79 #include <gnu/ext2fs/ext2_extern.h>
80 #include <gnu/ext2fs/ext2_fs.h>
82 static int ext2_makeinode __P((int mode, struct vnode *, struct vnode **, struct componentname *));
84 static int ext2_fsync __P((struct vop_fsync_args *));
85 static int ext2_read __P((struct vop_read_args *));
86 static int ext2_write __P((struct vop_write_args *));
87 static int ext2_remove __P((struct vop_remove_args *));
88 static int ext2_link __P((struct vop_link_args *));
89 static int ext2_rename __P((struct vop_rename_args *));
90 static int ext2_mkdir __P((struct vop_mkdir_args *));
91 static int ext2_rmdir __P((struct vop_rmdir_args *));
92 static int ext2_create __P((struct vop_create_args *));
93 static int ext2_mknod __P((struct vop_mknod_args *));
94 static int ext2_symlink __P((struct vop_symlink_args *));
95 static int ext2_getpages __P((struct vop_getpages_args *));
96 static int ext2_putpages __P((struct vop_putpages_args *));
98 /* Global vfs data structures for ufs. */
99 vop_t **ext2_vnodeop_p;
100 static struct vnodeopv_entry_desc ext2_vnodeop_entries[] = {
101 { &vop_default_desc, (vop_t *) ufs_vnoperate },
102 { &vop_cachedlookup_desc, (vop_t *) ext2_lookup },
103 { &vop_fsync_desc, (vop_t *) ext2_fsync },
104 { &vop_inactive_desc, (vop_t *) ext2_inactive },
105 { &vop_lookup_desc, (vop_t *) vfs_cache_lookup },
106 { &vop_read_desc, (vop_t *) ext2_read },
107 { &vop_readdir_desc, (vop_t *) ext2_readdir },
108 { &vop_reallocblks_desc, (vop_t *) ext2_reallocblks },
109 { &vop_write_desc, (vop_t *) ext2_write },
110 { &vop_remove_desc, (vop_t *) ext2_remove },
111 { &vop_link_desc, (vop_t *) ext2_link },
112 { &vop_rename_desc, (vop_t *) ext2_rename },
113 { &vop_mkdir_desc, (vop_t *) ext2_mkdir },
114 { &vop_rmdir_desc, (vop_t *) ext2_rmdir },
115 { &vop_create_desc, (vop_t *) ext2_create },
116 { &vop_mknod_desc, (vop_t *) ext2_mknod },
117 { &vop_symlink_desc, (vop_t *) ext2_symlink },
118 { &vop_getpages_desc, (vop_t *) ext2_getpages },
119 { &vop_putpages_desc, (vop_t *) ext2_putpages },
122 static struct vnodeopv_desc ext2fs_vnodeop_opv_desc =
123 { &ext2_vnodeop_p, ext2_vnodeop_entries };
125 vop_t **ext2_specop_p;
126 static struct vnodeopv_entry_desc ext2_specop_entries[] = {
127 { &vop_default_desc, (vop_t *) ufs_vnoperatespec },
128 { &vop_fsync_desc, (vop_t *) ext2_fsync },
129 { &vop_inactive_desc, (vop_t *) ext2_inactive },
132 static struct vnodeopv_desc ext2fs_specop_opv_desc =
133 { &ext2_specop_p, ext2_specop_entries };
135 vop_t **ext2_fifoop_p;
136 static struct vnodeopv_entry_desc ext2_fifoop_entries[] = {
137 { &vop_default_desc, (vop_t *) ufs_vnoperatefifo },
138 { &vop_fsync_desc, (vop_t *) ext2_fsync },
139 { &vop_inactive_desc, (vop_t *) ext2_inactive },
142 static struct vnodeopv_desc ext2fs_fifoop_opv_desc =
143 { &ext2_fifoop_p, ext2_fifoop_entries };
145 VNODEOP_SET(ext2fs_vnodeop_opv_desc);
146 VNODEOP_SET(ext2fs_specop_opv_desc);
147 VNODEOP_SET(ext2fs_fifoop_opv_desc);
149 #include <gnu/ext2fs/ext2_readwrite.c>
152 * A virgin directory (no blushing please).
153 * Note that the type and namlen fields are reversed relative to ufs.
154 * Also, we don't use `struct odirtemplate', since it would just cause
155 * endianness problems.
157 static struct dirtemplate mastertemplate = {
158 0, 12, 1, EXT2_FT_DIR, ".",
159 0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
161 static struct dirtemplate omastertemplate = {
162 0, 12, 1, EXT2_FT_UNKNOWN, ".",
163 0, DIRBLKSIZ - 12, 2, EXT2_FT_UNKNOWN, ".."
167 * Create a regular file
171 struct vop_create_args /* {
173 struct vnode **a_vpp;
174 struct componentname *a_cnp;
181 ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
182 ap->a_dvp, ap->a_vpp, ap->a_cnp);
189 * Synch an open file.
194 struct vop_fsync_args /* {
196 struct ucred *a_cred;
201 register struct vnode *vp = ap->a_vp;
202 register struct buf *bp;
207 * XXX why is all this fs specific?
211 * Flush all dirty buffers associated with a vnode.
213 ext2_discard_prealloc(VTOI(vp));
217 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
218 nbp = TAILQ_NEXT(bp, b_vnbufs);
219 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
221 if ((bp->b_flags & B_DELWRI) == 0)
222 panic("ext2_fsync: not dirty");
226 * Wait for I/O associated with indirect blocks to complete,
227 * since there is no way to quickly wait for them below.
229 if (bp->b_vp == vp || ap->a_waitfor == MNT_NOWAIT)
235 if (ap->a_waitfor == MNT_WAIT) {
236 while (vp->v_numoutput) {
237 vp->v_flag |= VBWAIT;
238 tsleep(&vp->v_numoutput, 0, "e2fsyn", 0);
241 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
242 vprint("ext2_fsync: dirty", vp);
248 return (UFS_UPDATE(ap->a_vp, ap->a_waitfor == MNT_WAIT));
257 struct vop_mknod_args /* {
259 struct vnode **a_vpp;
260 struct componentname *a_cnp;
264 struct vattr *vap = ap->a_vap;
265 struct vnode **vpp = ap->a_vpp;
270 error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
271 ap->a_dvp, vpp, ap->a_cnp);
275 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
276 if (vap->va_rdev != VNOVAL) {
278 * Want to be able to use this to make badblock
279 * inodes, so don't truncate the dev number.
281 ip->i_rdev = vap->va_rdev;
284 * Remove inode, then reload it through VFS_VGET so it is
285 * checked to see if it is an alias of an existing entry in
289 (*vpp)->v_type = VNON;
290 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
292 error = VFS_VGET(ap->a_dvp->v_mount, ino, vpp);
302 struct vop_remove_args /* {
305 struct componentname *a_cnp;
309 struct vnode *vp = ap->a_vp;
310 struct vnode *dvp = ap->a_dvp;
314 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
315 (VTOI(dvp)->i_flags & APPEND)) {
319 error = ext2_dirremove(dvp, ap->a_cnp);
322 ip->i_flag |= IN_CHANGE;
333 struct vop_link_args /* {
334 struct vnode *a_tdvp;
336 struct componentname *a_cnp;
339 struct vnode *vp = ap->a_vp;
340 struct vnode *tdvp = ap->a_tdvp;
341 struct componentname *cnp = ap->a_cnp;
342 struct proc *p = cnp->cn_proc;
347 if ((cnp->cn_flags & HASBUF) == 0)
348 panic("ufs_link: no name");
350 if (tdvp->v_mount != vp->v_mount) {
354 if (tdvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE, p))) {
358 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
362 if (ip->i_flags & (IMMUTABLE | APPEND)) {
367 ip->i_flag |= IN_CHANGE;
368 error = UFS_UPDATE(vp, 1);
370 error = ext2_direnter(ip, tdvp, cnp);
373 ip->i_flag |= IN_CHANGE;
377 VOP_UNLOCK(vp, 0, p);
383 * Rename system call.
384 * See comments in sys/ufs/ufs/ufs_vnops.c
388 struct vop_rename_args /* {
389 struct vnode *a_fdvp;
391 struct componentname *a_fcnp;
392 struct vnode *a_tdvp;
394 struct componentname *a_tcnp;
397 struct vnode *tvp = ap->a_tvp;
398 register struct vnode *tdvp = ap->a_tdvp;
399 struct vnode *fvp = ap->a_fvp;
400 struct vnode *fdvp = ap->a_fdvp;
401 struct componentname *tcnp = ap->a_tcnp;
402 struct componentname *fcnp = ap->a_fcnp;
403 struct proc *p = fcnp->cn_proc;
404 struct inode *ip, *xp, *dp;
405 struct dirtemplate dirbuf;
406 int doingdirectory = 0, oldparent = 0, newparent = 0;
411 if ((tcnp->cn_flags & HASBUF) == 0 ||
412 (fcnp->cn_flags & HASBUF) == 0)
413 panic("ufs_rename: no name");
416 * Check for cross-device rename.
418 if ((fvp->v_mount != tdvp->v_mount) ||
419 (tvp && (fvp->v_mount != tvp->v_mount))) {
433 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
434 (VTOI(tdvp)->i_flags & APPEND))) {
440 * Renaming a file to itself has no effect. The upper layers should
441 * not call us in that case. Temporarily just warn if they do.
444 printf("ext2_rename: fvp == tvp (can't happen)\n");
449 if ((error = vn_lock(fvp, LK_EXCLUSIVE, p)) != 0)
453 if (ip->i_nlink >= LINK_MAX) {
454 VOP_UNLOCK(fvp, 0, p);
458 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
459 || (dp->i_flags & APPEND)) {
460 VOP_UNLOCK(fvp, 0, p);
464 if ((ip->i_mode & IFMT) == IFDIR) {
466 * Avoid ".", "..", and aliases of "." for obvious reasons.
468 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
469 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
470 (ip->i_flag & IN_RENAME)) {
471 VOP_UNLOCK(fvp, 0, p);
475 ip->i_flag |= IN_RENAME;
476 oldparent = dp->i_number;
482 * When the target exists, both the directory
483 * and target vnodes are returned locked.
491 * 1) Bump link count while we're moving stuff
492 * around. If we crash somewhere before
493 * completing our work, the link count
494 * may be wrong, but correctable.
497 ip->i_flag |= IN_CHANGE;
498 if ((error = UFS_UPDATE(fvp, 1)) != 0) {
499 VOP_UNLOCK(fvp, 0, p);
504 * If ".." must be changed (ie the directory gets a new
505 * parent) then the source directory must not be in the
506 * directory heirarchy above the target, as this would
507 * orphan everything below the source directory. Also
508 * the user must have write permission in the source so
509 * as to be able to change "..". We must repeat the call
510 * to namei, as the parent directory is unlocked by the
511 * call to checkpath().
513 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
514 VOP_UNLOCK(fvp, 0, p);
515 if (oldparent != dp->i_number)
516 newparent = dp->i_number;
517 if (doingdirectory && newparent) {
518 if (error) /* write access check above */
522 error = ext2_checkpath(ip, dp, tcnp->cn_cred);
526 error = relookup(tdvp, &tvp, tcnp);
536 * 2) If target doesn't exist, link the target
537 * to the source and unlink the source.
538 * Otherwise, rewrite the target directory
539 * entry to reference the source inode and
540 * expunge the original entry's existence.
543 if (dp->i_dev != ip->i_dev)
544 panic("ufs_rename: EXDEV");
546 * Account for ".." in new directory.
547 * When source and destination have the same
548 * parent we don't fool with the link count.
550 if (doingdirectory && newparent) {
551 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
556 dp->i_flag |= IN_CHANGE;
557 error = UFS_UPDATE(tdvp, 1);
561 error = ext2_direnter(ip, tdvp, tcnp);
563 if (doingdirectory && newparent) {
565 dp->i_flag |= IN_CHANGE;
566 (void)UFS_UPDATE(tdvp, 1);
572 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
573 panic("ufs_rename: EXDEV");
575 * Short circuit rename(foo, foo).
577 if (xp->i_number == ip->i_number)
578 panic("ufs_rename: same file");
580 * If the parent directory is "sticky", then the user must
581 * own the parent directory, or the destination of the rename,
582 * otherwise the destination may not be changed (except by
583 * root). This implements append-only directories.
585 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
586 tcnp->cn_cred->cr_uid != dp->i_uid &&
587 xp->i_uid != tcnp->cn_cred->cr_uid) {
592 * Target must be empty if a directory and have no links
593 * to it. Also, ensure source and target are compatible
594 * (both directories, or both not directories).
596 if ((xp->i_mode&IFMT) == IFDIR) {
597 if (! ext2_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
602 if (!doingdirectory) {
607 } else if (doingdirectory) {
611 error = ext2_dirrewrite(dp, ip, tcnp);
615 * If the target directory is in the same
616 * directory as the source directory,
617 * decrement the link count on the parent
618 * of the target directory.
620 if (doingdirectory && !newparent) {
622 dp->i_flag |= IN_CHANGE;
626 * Adjust the link count of the target to
627 * reflect the dirrewrite above. If this is
628 * a directory it is empty and there are
629 * no links to it, so we can squash the inode and
630 * any space associated with it. We disallowed
631 * renaming over top of a directory with links to
632 * it above, as the remaining link would point to
633 * a directory without "." or ".." entries.
636 if (doingdirectory) {
637 if (--xp->i_nlink != 0)
638 panic("ufs_rename: linked directory");
639 error = UFS_TRUNCATE(tvp, (off_t)0, IO_SYNC,
640 tcnp->cn_cred, tcnp->cn_proc);
642 xp->i_flag |= IN_CHANGE;
648 * 3) Unlink the source.
650 fcnp->cn_flags &= ~MODMASK;
651 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
653 error = relookup(fdvp, &fvp, fcnp);
661 * From name has disappeared.
664 panic("ufs_rename: lost dir entry");
669 * Ensure that the directory entry still exists and has not
670 * changed while the new name has been entered. If the source is
671 * a file then the entry may have been unlinked or renamed. In
672 * either case there is no further work to be done. If the source
673 * is a directory then it cannot have been rmdir'ed; its link
674 * count of three would cause a rmdir to fail with ENOTEMPTY.
675 * The IN_RENAME flag ensures that it cannot be moved by another
680 panic("ufs_rename: lost dir entry");
683 * If the source is a directory with a
684 * new parent, the link count of the old
685 * parent directory must be decremented
686 * and ".." set to point to the new parent.
688 if (doingdirectory && newparent) {
690 dp->i_flag |= IN_CHANGE;
691 error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
692 sizeof (struct dirtemplate), (off_t)0,
693 UIO_SYSSPACE, IO_NODELOCKED,
694 tcnp->cn_cred, (int *)0, (struct proc *)0);
696 /* Like ufs little-endian: */
697 namlen = dirbuf.dotdot_type;
699 dirbuf.dotdot_name[0] != '.' ||
700 dirbuf.dotdot_name[1] != '.') {
701 ufs_dirbad(xp, (doff_t)12,
702 "rename: mangled dir");
704 dirbuf.dotdot_ino = newparent;
705 (void) vn_rdwr(UIO_WRITE, fvp,
707 sizeof (struct dirtemplate),
708 (off_t)0, UIO_SYSSPACE,
709 IO_NODELOCKED|IO_SYNC,
710 tcnp->cn_cred, (int *)0,
716 error = ext2_dirremove(fdvp, fcnp);
719 xp->i_flag |= IN_CHANGE;
721 xp->i_flag &= ~IN_RENAME;
736 ip->i_flag &= ~IN_RENAME;
737 if (vn_lock(fvp, LK_EXCLUSIVE, p) == 0) {
739 ip->i_flag |= IN_CHANGE;
740 ip->i_flag &= ~IN_RENAME;
752 struct vop_mkdir_args /* {
754 struct vnode **a_vpp;
755 struct componentname *a_cnp;
759 register struct vnode *dvp = ap->a_dvp;
760 register struct vattr *vap = ap->a_vap;
761 register struct componentname *cnp = ap->a_cnp;
762 register struct inode *ip, *dp;
764 struct dirtemplate dirtemplate, *dtp;
768 if ((cnp->cn_flags & HASBUF) == 0)
769 panic("ufs_mkdir: no name");
772 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
776 dmode = vap->va_mode & 0777;
779 * Must simulate part of ext2_makeinode here to acquire the inode,
780 * but not have it entered in the parent directory. The entry is
781 * made later after writing "." and ".." entries.
783 error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
787 ip->i_gid = dp->i_gid;
791 struct ucred ucred, *ucp;
795 * if we are hacking owners here, (only do this where told to)
796 * and we are not giving it TOO root, (would subvert quotas)
797 * then go ahead and give it to the other user.
798 * The new directory also inherits the SUID bit.
799 * If user's UID and dir UID are the same,
800 * 'give it away' so that the SUID is still forced on.
802 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
803 (dp->i_mode & ISUID) && dp->i_uid) {
805 ip->i_uid = dp->i_uid;
807 if (dp->i_uid != cnp->cn_cred->cr_uid) {
809 * make sure the correct user gets charged
811 * Make a dummy credential for the victim.
812 * XXX This seems to never be accessed out of
813 * our context so a stack variable is ok.
816 ucred.cr_uid = ip->i_uid;
817 ucred.cr_ngroups = 1;
818 ucred.cr_groups[0] = dp->i_gid;
823 ip->i_uid = cnp->cn_cred->cr_uid;
826 if ((error = getinoquota(ip)) ||
827 (error = chkiq(ip, 1, ucp, 0))) {
828 UFS_VFREE(tvp, ip->i_number, dmode);
835 ip->i_uid = cnp->cn_cred->cr_uid;
837 if ((error = getinoquota(ip)) ||
838 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
839 UFS_VFREE(tvp, ip->i_number, dmode);
845 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
847 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
849 if (cnp->cn_flags & ISWHITEOUT)
850 ip->i_flags |= UF_OPAQUE;
851 error = UFS_UPDATE(tvp, 1);
854 * Bump link count in parent directory
855 * to reflect work done below. Should
856 * be done before reference is created
857 * so reparation is possible if we crash.
860 dp->i_flag |= IN_CHANGE;
861 error = UFS_UPDATE(dvp, 1);
865 /* Initialize directory with "." and ".." from static template. */
866 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs->s_es,
867 EXT2_FEATURE_INCOMPAT_FILETYPE))
868 dtp = &mastertemplate;
870 dtp = &omastertemplate;
872 dirtemplate.dot_ino = ip->i_number;
873 dirtemplate.dotdot_ino = dp->i_number;
874 /* note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE
875 * so let's just redefine it - for this function only
878 #define DIRBLKSIZ VTOI(dvp)->i_e2fs->s_blocksize
879 dirtemplate.dotdot_reclen = DIRBLKSIZ - 12;
880 error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
881 sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
882 IO_NODELOCKED|IO_SYNC, cnp->cn_cred, (int *)0, (struct proc *)0);
885 dp->i_flag |= IN_CHANGE;
888 if (DIRBLKSIZ > VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
889 panic("ufs_mkdir: blksize"); /* XXX should grow with balloc() */
891 ip->i_size = DIRBLKSIZ;
892 ip->i_flag |= IN_CHANGE;
895 /* Directory set up, now install its entry in the parent directory. */
896 error = ext2_direnter(ip, dvp, cnp);
899 dp->i_flag |= IN_CHANGE;
903 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
904 * for us because we set the link count to 0.
908 ip->i_flag |= IN_CHANGE;
915 #define DIRBLKSIZ DEV_BSIZE
923 struct vop_rmdir_args /* {
926 struct componentname *a_cnp;
929 struct vnode *vp = ap->a_vp;
930 struct vnode *dvp = ap->a_dvp;
931 struct componentname *cnp = ap->a_cnp;
932 struct proc *p = cnp->cn_proc;
933 struct inode *ip, *dp;
940 * Verify the directory is empty (and valid).
941 * (Rmdir ".." won't be valid since
942 * ".." will contain a reference to
943 * the current directory and thus be
947 if (ip->i_nlink != 2 || !ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
951 if ((dp->i_flags & APPEND)
952 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
957 * Delete reference to directory before purging
958 * inode. If we crash in between, the directory
959 * will be reattached to lost+found,
961 error = ext2_dirremove(dvp, cnp);
965 dp->i_flag |= IN_CHANGE;
967 VOP_UNLOCK(dvp, 0, p);
969 * Truncate inode. The only stuff left
970 * in the directory is "." and "..". The
971 * "." reference is inconsequential since
972 * we're quashing it. The ".." reference
973 * has already been adjusted above. We've
974 * removed the "." reference and the reference
975 * in the parent directory, but there may be
976 * other hard links so decrement by 2 and
977 * worry about them later.
980 error = UFS_TRUNCATE(vp, (off_t)0, IO_SYNC, cnp->cn_cred, p);
981 cache_purge(ITOV(ip));
982 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
988 * symlink -- make a symbolic link
992 struct vop_symlink_args /* {
994 struct vnode **a_vpp;
995 struct componentname *a_cnp;
1000 register struct vnode *vp, **vpp = ap->a_vpp;
1001 register struct inode *ip;
1004 error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1009 len = strlen(ap->a_target);
1010 if (len < vp->v_mount->mnt_maxsymlinklen) {
1012 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1014 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1016 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1017 UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred, (int *)0,
1025 * Allocate a new inode.
1028 ext2_makeinode(mode, dvp, vpp, cnp)
1032 struct componentname *cnp;
1034 register struct inode *ip, *pdir;
1040 if ((cnp->cn_flags & HASBUF) == 0)
1041 panic("ext2_makeinode: no name");
1044 if ((mode & IFMT) == 0)
1047 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
1052 ip->i_gid = pdir->i_gid;
1056 struct ucred ucred, *ucp;
1061 * not the owner of the directory,
1062 * and we are hacking owners here, (only do this where told to)
1063 * and we are not giving it TOO root, (would subvert quotas)
1064 * then go ahead and give it to the other user.
1065 * Note that this drops off the execute bits for security.
1067 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1068 (pdir->i_mode & ISUID) &&
1069 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1070 ip->i_uid = pdir->i_uid;
1074 * make sure the correct user gets charged
1076 * Quickly knock up a dummy credential for the victim.
1077 * XXX This seems to never be accessed out of our
1078 * context so a stack variable is ok.
1081 ucred.cr_uid = ip->i_uid;
1082 ucred.cr_ngroups = 1;
1083 ucred.cr_groups[0] = pdir->i_gid;
1087 ip->i_uid = cnp->cn_cred->cr_uid;
1091 if ((error = getinoquota(ip)) ||
1092 (error = chkiq(ip, 1, ucp, 0))) {
1093 UFS_VFREE(tvp, ip->i_number, mode);
1100 ip->i_uid = cnp->cn_cred->cr_uid;
1102 if ((error = getinoquota(ip)) ||
1103 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1104 UFS_VFREE(tvp, ip->i_number, mode);
1110 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1112 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
1114 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
1115 suser_cred(cnp->cn_cred, PRISON_ROOT))
1116 ip->i_mode &= ~ISGID;
1118 if (cnp->cn_flags & ISWHITEOUT)
1119 ip->i_flags |= UF_OPAQUE;
1122 * Make sure inode goes to disk before directory entry.
1124 error = UFS_UPDATE(tvp, 1);
1127 error = ext2_direnter(ip, dvp, cnp);
1136 * Write error occurred trying to update the inode
1137 * or the directory so must deallocate the inode.
1140 ip->i_flag |= IN_CHANGE;
1148 * XXX By default, wimp out... note that a_offset is ignored (and always
1153 struct vop_getpages_args *ap;
1155 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
1162 * XXX By default, wimp out... note that a_offset is ignored (and always
1167 struct vop_putpages_args *ap;
1169 return (vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
1170 ap->a_sync, ap->a_rtvals));