2 * modified for EXT2FS support in Lites 1.1
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
8 * Copyright (c) 1982, 1986, 1989, 1993
9 * The Regents of the University of California. All rights reserved.
10 * (c) UNIX System Laboratories, Inc.
11 * All or some portions of this file are derived from material licensed
12 * to the University of California by American Telephone and Telegraph
13 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
14 * the permission of UNIX System Laboratories, Inc.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
41 * @(#)ext2_vnops.c 8.7 (Berkeley) 2/3/94
42 * $FreeBSD: src/sys/gnu/ext2fs/ext2_vnops.c,v 1.51.2.2 2003/01/02 17:26:18 bde Exp $
45 #include "opt_quota.h"
46 #include "opt_suiddir.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/resourcevar.h>
51 #include <sys/kernel.h>
53 #include <sys/fcntl.h>
57 #include <sys/mount.h>
59 #include <sys/vnode.h>
60 #include <sys/dirent.h>
61 #include <sys/lockf.h>
62 #include <sys/event.h>
66 #include <sys/namei.h>
67 #include <sys/signalvar.h>
68 #include <sys/unistd.h>
71 #include <vm/vm_extern.h>
72 #include <vm/vm_zone.h>
73 #include <vm/vnode_pager.h>
76 #include <sys/thread2.h>
78 #include <vfs/fifofs/fifo.h>
83 #include "ext2_mount.h"
84 #include "ext2_fs_sb.h"
86 #include "ext2_extern.h"
89 static int ext2_access (struct vop_access_args
*);
90 static int ext2_advlock (struct vop_advlock_args
*);
91 static int ext2_chmod (struct vnode
*, int, struct ucred
*);
92 static int ext2_chown (struct vnode
*, uid_t
, gid_t
, struct ucred
*);
93 static int ext2_close (struct vop_close_args
*);
94 static int ext2_getattr (struct vop_getattr_args
*);
95 static int ext2_makeinode (int mode
, struct vnode
*, struct vnode
**, struct componentname
*);
96 static int ext2_mmap (struct vop_mmap_args
*);
97 static int ext2_open (struct vop_open_args
*);
98 static int ext2_pathconf (struct vop_pathconf_args
*);
99 static int ext2_print (struct vop_print_args
*);
100 static int ext2_readlink (struct vop_readlink_args
*);
101 static int ext2_setattr (struct vop_setattr_args
*);
102 static int ext2_strategy (struct vop_strategy_args
*);
103 static int ext2_whiteout (struct vop_old_whiteout_args
*);
104 static int filt_ext2read (struct knote
*kn
, long hint
);
105 static int filt_ext2write (struct knote
*kn
, long hint
);
106 static int filt_ext2vnode (struct knote
*kn
, long hint
);
107 static void filt_ext2detach (struct knote
*kn
);
108 static int ext2_kqfilter (struct vop_kqfilter_args
*ap
);
109 static int ext2fifo_close (struct vop_close_args
*);
110 static int ext2fifo_kqfilter (struct vop_kqfilter_args
*);
111 static int ext2fifo_read (struct vop_read_args
*);
112 static int ext2fifo_write (struct vop_write_args
*);
114 static int ext2_fsync (struct vop_fsync_args
*);
115 static int ext2_read (struct vop_read_args
*);
116 static int ext2_write (struct vop_write_args
*);
117 static int ext2_remove (struct vop_old_remove_args
*);
118 static int ext2_link (struct vop_old_link_args
*);
119 static int ext2_rename (struct vop_old_rename_args
*);
120 static int ext2_mkdir (struct vop_old_mkdir_args
*);
121 static int ext2_rmdir (struct vop_old_rmdir_args
*);
122 static int ext2_create (struct vop_old_create_args
*);
123 static int ext2_mknod (struct vop_old_mknod_args
*);
124 static int ext2_symlink (struct vop_old_symlink_args
*);
126 #include "ext2_readwrite.c"
132 #define SETHIGH(q, h) { \
135 tmp.val[_QUAD_HIGHWORD] = (h); \
138 #define SETLOW(q, l) { \
141 tmp.val[_QUAD_LOWWORD] = (l); \
144 #define VN_KNOTE(vp, b) \
145 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, (b))
147 #define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
150 * A virgin directory (no blushing please).
151 * Note that the type and namlen fields are reversed relative to ufs.
152 * Also, we don't use `struct odirtemplate', since it would just cause
153 * endianness problems.
155 static struct dirtemplate ext2_mastertemplate
= {
156 0, 12, 1, EXT2_FT_DIR
, ".",
157 0, DIRBLKSIZ
- 12, 2, EXT2_FT_DIR
, ".."
159 static struct dirtemplate ext2_omastertemplate
= {
160 0, 12, 1, EXT2_FT_UNKNOWN
, ".",
161 0, DIRBLKSIZ
- 12, 2, EXT2_FT_UNKNOWN
, ".."
165 * Create a regular file
167 * ext2_create(struct vnode *a_dvp, struct vnode **a_vpp,
168 * struct componentname *a_cnp, struct vattr *a_vap)
171 ext2_create(struct vop_old_create_args
*ap
)
176 ext2_makeinode(MAKEIMODE(ap
->a_vap
->va_type
, ap
->a_vap
->va_mode
),
177 ap
->a_dvp
, ap
->a_vpp
, ap
->a_cnp
);
184 * Synch an open file.
186 * ext2_fsync(struct vnode *a_vp, struct ucred *a_cred, int a_waitfor,
191 static int ext2_fsync_bp(struct buf
*bp
, void *data
);
193 struct ext2_fsync_bp_info
{
199 ext2_fsync(struct vop_fsync_args
*ap
)
201 struct ext2_fsync_bp_info info
;
202 struct vnode
*vp
= ap
->a_vp
;
206 * XXX why is all this fs specific?
210 * Flush all dirty buffers associated with a vnode.
212 ext2_discard_prealloc(VTOI(vp
));
214 lwkt_gettoken(&vp
->v_token
);
217 info
.waitfor
= ap
->a_waitfor
;
218 count
= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, NULL
,
219 ext2_fsync_bp
, &info
);
223 if (ap
->a_waitfor
== MNT_WAIT
) {
224 bio_track_wait(&vp
->v_track_write
, 0, 0);
226 if (!RB_EMPTY(&vp
->v_rbdirty_tree
)) {
227 vprint("ext2_fsync: dirty", vp
);
232 lwkt_reltoken(&vp
->v_token
);
233 return (EXT2_UPDATE(ap
->a_vp
, ap
->a_waitfor
== MNT_WAIT
));
237 ext2_fsync_bp(struct buf
*bp
, void *data
)
239 struct ext2_fsync_bp_info
*info
= data
;
241 if (BUF_LOCK(bp
, LK_EXCLUSIVE
| LK_NOWAIT
))
243 if ((bp
->b_flags
& B_DELWRI
) == 0)
244 panic("ext2_fsync: not dirty");
248 * Wait for I/O associated with indirect blocks to complete,
249 * since there is no way to quickly wait for them below.
251 if (bp
->b_vp
== info
->vp
|| (info
->waitfor
& MNT_NOWAIT
))
261 * ext2_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
262 * struct componentname *a_cnp, struct vattr *a_vap)
266 ext2_mknod(struct vop_old_mknod_args
*ap
)
268 struct vattr
*vap
= ap
->a_vap
;
269 struct vnode
**vpp
= ap
->a_vpp
;
274 if (vap
->va_rmajor
!= VNOVAL
&&
275 makeudev(vap
->va_rmajor
, vap
->va_rminor
) == NOUDEV
) {
279 error
= ext2_makeinode(MAKEIMODE(vap
->va_type
, vap
->va_mode
),
280 ap
->a_dvp
, vpp
, ap
->a_cnp
);
284 ip
->i_flag
|= IN_ACCESS
| IN_CHANGE
| IN_UPDATE
;
285 if (vap
->va_rmajor
!= VNOVAL
) {
287 * Want to be able to use this to make badblock
288 * inodes, so don't truncate the dev number.
290 ip
->i_rdev
= makeudev(vap
->va_rmajor
, vap
->va_rminor
);
293 * Remove inode, then reload it through VFS_VGET so it is
294 * checked to see if it is an alias of an existing entry in
297 (*vpp
)->v_type
= VNON
;
298 ino
= ip
->i_number
; /* Save this before vgone() invalidates ip. */
299 vgone_vxlocked(*vpp
);
301 error
= VFS_VGET(ap
->a_dvp
->v_mount
, NULL
, ino
, vpp
);
310 * ext2_remove(struct vnode *a_dvp, struct vnode *a_vp,
311 * struct componentname *a_cnp)
314 ext2_remove(struct vop_old_remove_args
*ap
)
317 struct vnode
*vp
= ap
->a_vp
;
318 struct vnode
*dvp
= ap
->a_dvp
;
322 if ((ip
->i_flags
& (NOUNLINK
| IMMUTABLE
| APPEND
)) ||
323 (VTOI(dvp
)->i_flags
& APPEND
)) {
327 error
= ext2_dirremove(dvp
, ap
->a_cnp
);
330 ip
->i_flag
|= IN_CHANGE
;
339 * ext2_link(struct vnode *a_tdvp, struct vnode *a_vp,
340 * struct componentname *a_cnp)
343 ext2_link(struct vop_old_link_args
*ap
)
345 struct vnode
*vp
= ap
->a_vp
;
346 struct vnode
*tdvp
= ap
->a_tdvp
;
347 struct componentname
*cnp
= ap
->a_cnp
;
351 if (tdvp
->v_mount
!= vp
->v_mount
) {
356 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_FAILRECLAIM
);
361 if ((nlink_t
)ip
->i_nlink
>= LINK_MAX
) {
365 if (ip
->i_flags
& (IMMUTABLE
| APPEND
)) {
370 ip
->i_flag
|= IN_CHANGE
;
371 error
= EXT2_UPDATE(vp
, 1);
373 error
= ext2_direnter(ip
, tdvp
, cnp
);
376 ip
->i_flag
|= IN_CHANGE
;
386 * Rename system call. fdvp, fvp are ref'd. tvp, tdvp are ref'd and locked.
387 * all vp's are released and must be in an unlocked state on return.
389 * ext2_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
390 * struct componentname *a_fcnp, struct vnode *a_tdvp,
391 * struct vnode *a_tvp, struct componentname *a_tcnp)
394 ext2_rename(struct vop_old_rename_args
*ap
)
396 struct vnode
*tvp
= ap
->a_tvp
;
397 struct vnode
*tdvp
= ap
->a_tdvp
;
398 struct vnode
*fvp
= ap
->a_fvp
;
399 struct vnode
*fdvp
= ap
->a_fdvp
;
400 struct componentname
*tcnp
= ap
->a_tcnp
;
401 struct componentname
*fcnp
= ap
->a_fcnp
;
402 struct inode
*ip
, *xp
, *dp
;
403 struct dirtemplate dirbuf
;
404 int doingdirectory
= 0, oldparent
= 0, newparent
= 0;
409 * Check for cross-device rename.
411 if ((fvp
->v_mount
!= tdvp
->v_mount
) ||
412 (tvp
&& (fvp
->v_mount
!= tvp
->v_mount
)) ||
427 if (tvp
&& ((VTOI(tvp
)->i_flags
& (NOUNLINK
| IMMUTABLE
| APPEND
)) ||
428 (VTOI(tdvp
)->i_flags
& APPEND
))) {
434 * Renaming a file to itself has no effect. The upper layers should
435 * not call us in that case. Temporarily just warn if they do.
442 error
= vn_lock(fvp
, LK_EXCLUSIVE
| LK_FAILRECLAIM
);
447 * fvp, tvp, tdvp locked. fdvp not locked but note that fdvp may
452 if (ip
->i_nlink
>= LINK_MAX
) {
457 if ((ip
->i_flags
& (NOUNLINK
| IMMUTABLE
| APPEND
))
458 || (dp
->i_flags
& APPEND
)) {
463 if ((ip
->i_mode
& IFMT
) == IFDIR
) {
465 * Avoid ".", "..", and aliases of "." for obvious reasons.
467 if ((fcnp
->cn_namelen
== 1 && fcnp
->cn_nameptr
[0] == '.') ||
468 dp
== ip
|| (fcnp
->cn_flags
| tcnp
->cn_flags
) & CNP_ISDOTDOT
||
469 (ip
->i_flag
& IN_RENAME
)) {
474 ip
->i_flag
|= IN_RENAME
;
475 oldparent
= dp
->i_number
;
480 * tvp is non-NULL if the target exists. fvp is still locked but
481 * we will unlock it soon. The 'bad' goto target requires dp and
482 * xp to be correctly assigned.
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
= EXT2_UPDATE(fvp
, 1)) != 0) {
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_EACCESS(fvp
, VWRITE
, tcnp
->cn_cred
);
517 * tvp (if not NULL) and tdvp are locked. fvp and fdvp are not.
518 * dp and xp are set according to tdvp and tvp.
520 if (oldparent
!= dp
->i_number
)
521 newparent
= dp
->i_number
;
522 if (doingdirectory
&& newparent
) {
523 if (error
) /* write access check above */
527 * Prepare for relookup, get rid of xp
535 * checkpath vput()'s tdvp (VTOI(dp)) on return no matter what,
536 * get an extra ref so we wind up with just an unlocked, ref'd
537 * tdvp. The 'out' target skips xp and tdvp cleanups. Our
538 * tdvp is now unlocked so we have to clean it up ourselves.
541 error
= ext2_checkpath(ip
, dp
, tcnp
->cn_cred
);
542 tcnp
->cn_flags
|= CNP_PDIRUNLOCK
;
548 * relookup no longer messes with the ref count. An unlocked
549 * tdvp must be passed and if no error occurs a locked tdvp
550 * will be returned. We have to use the out target again.
552 error
= relookup(tdvp
, &tvp
, tcnp
);
554 if (tcnp
->cn_flags
& CNP_PDIRUNLOCK
)
562 * tdvp is locked at this point. in the RENAME case tvp may
563 * be NULL without an error, assign xp accordingly. The
564 * 'bad' target can be used again after this.
571 * 2) If target doesn't exist, link the target
572 * to the source and unlink the source.
573 * Otherwise, rewrite the target directory
574 * entry to reference the source inode and
575 * expunge the original entry's existence.
577 * tdvp and tvp are cleaned up by this code. tvp is only good if
581 if (dp
->i_dev
!= ip
->i_dev
)
582 panic("ext2_rename: EXDEV");
584 * Account for ".." in new directory.
585 * When source and destination have the same
586 * parent we don't fool with the link count.
588 if (doingdirectory
&& newparent
) {
589 if ((nlink_t
)dp
->i_nlink
>= LINK_MAX
) {
594 dp
->i_flag
|= IN_CHANGE
;
595 error
= EXT2_UPDATE(tdvp
, 1);
599 error
= ext2_direnter(ip
, tdvp
, tcnp
);
601 if (doingdirectory
&& newparent
) {
603 dp
->i_flag
|= IN_CHANGE
;
604 EXT2_UPDATE(tdvp
, 1);
610 * manual cleanup, we can't use the bad or out target after
615 if (xp
->i_dev
!= dp
->i_dev
|| xp
->i_dev
!= ip
->i_dev
)
616 panic("ext2_rename: EXDEV");
618 * Short circuit rename(foo, foo).
620 if (xp
->i_number
== ip
->i_number
)
621 panic("ext2_rename: same file");
623 * If the parent directory is "sticky", then the user must
624 * own the parent directory, or the destination of the rename,
625 * otherwise the destination may not be changed (except by
626 * root). This implements append-only directories.
628 if ((dp
->i_mode
& S_ISTXT
) && tcnp
->cn_cred
->cr_uid
!= 0 &&
629 tcnp
->cn_cred
->cr_uid
!= dp
->i_uid
&&
630 xp
->i_uid
!= tcnp
->cn_cred
->cr_uid
) {
635 * Target must be empty if a directory and have no links
636 * to it. Also, ensure source and target are compatible
637 * (both directories, or both not directories).
639 if ((xp
->i_mode
&IFMT
) == IFDIR
) {
640 if (! ext2_dirempty(xp
, dp
->i_number
, tcnp
->cn_cred
) ||
645 if (!doingdirectory
) {
649 } else if (doingdirectory
) {
653 error
= ext2_dirrewrite(dp
, ip
, tcnp
);
657 * If the target directory is in the same
658 * directory as the source directory,
659 * decrement the link count on the parent
660 * of the target directory.
662 if (doingdirectory
&& !newparent
) {
664 dp
->i_flag
|= IN_CHANGE
;
668 * manual cleanup, we can't use the bad or out target after
674 * Adjust the link count of the target to
675 * reflect the dirrewrite above. If this is
676 * a directory it is empty and there are
677 * no links to it, so we can squash the inode and
678 * any space associated with it. We disallowed
679 * renaming over top of a directory with links to
680 * it above, as the remaining link would point to
681 * a directory without "." or ".." entries.
684 if (doingdirectory
) {
685 if (--xp
->i_nlink
!= 0)
686 panic("ext2_rename: linked directory");
687 error
= EXT2_TRUNCATE(tvp
, (off_t
)0, IO_SYNC
,
690 xp
->i_flag
|= IN_CHANGE
;
696 * tvp and tdvp have been cleaned up. The bad and out targets may
697 * not be used. fvp and fdvp are ref'd but not locked. ip
698 * still represents the old fvp and ip->i_flag may still have IN_RENAME
699 * set (if doingdirectory).
703 * 3) Unlink the source.
705 * fdvp is locked and ref'd. ap->a_fvp holds the old lookup unlocked
706 * and ref'd, fvp will hold the new lookup locked and ref'd.
708 * After the relookup ap->a_fvp must be released as part of our
709 * cleanup, not just fdvp and fvp. And, on success, fdvp and
710 * fvp will be locked so the bad and out targets cannot be used.
712 fcnp
->cn_flags
&= ~CNP_MODMASK
;
713 fcnp
->cn_flags
|= CNP_LOCKPARENT
;
714 KKASSERT(fcnp
->cn_flags
& CNP_PDIRUNLOCK
);
715 error
= relookup(fdvp
, &fvp
, fcnp
);
718 * From name has disappeared.
721 panic("ext2_rename: lost dir entry");
722 /* ip->i_flag only sets IN_RENAME if doingdirectory */
724 if (fcnp
->cn_flags
& CNP_PDIRUNLOCK
)
730 KKASSERT((fcnp
->cn_flags
& CNP_PDIRUNLOCK
) == 0);
733 * This case shouldn't occur
737 * From name has disappeared.
740 panic("ext2_rename: lost dir entry");
741 /* ip->i_flag only sets IN_RENAME if doingdirectory */
749 * fvp and fdvp are both ref'd and locked.
755 * Ensure that the directory entry still exists and has not
756 * changed while the new name has been entered. If the source is
757 * a file then the entry may have been unlinked or renamed. In
758 * either case there is no further work to be done. If the source
759 * is a directory then it cannot have been rmdir'ed; its link
760 * count of three would cause a rmdir to fail with ENOTEMPTY.
761 * The IN_RENAME flag ensures that it cannot be moved by another
766 panic("ext2_rename: lost dir entry");
767 /* ip->i_flag only sets IN_RENAME if doingdirectory */
770 * If the source is a directory with a
771 * new parent, the link count of the old
772 * parent directory must be decremented
773 * and ".." set to point to the new parent.
775 if (doingdirectory
&& newparent
) {
777 dp
->i_flag
|= IN_CHANGE
;
778 error
= vn_rdwr(UIO_READ
, fvp
, (caddr_t
)&dirbuf
,
779 sizeof (struct dirtemplate
), (off_t
)0,
780 UIO_SYSSPACE
, IO_NODELOCKED
,
781 tcnp
->cn_cred
, NULL
);
783 /* Like ext2 little-endian: */
784 namlen
= dirbuf
.dotdot_type
;
786 dirbuf
.dotdot_name
[0] != '.' ||
787 dirbuf
.dotdot_name
[1] != '.') {
788 ext2_dirbad(xp
, (doff_t
)12,
789 "rename: mangled dir");
791 dirbuf
.dotdot_ino
= newparent
;
792 vn_rdwr(UIO_WRITE
, fvp
,
794 sizeof (struct dirtemplate
),
795 (off_t
)0, UIO_SYSSPACE
,
796 IO_NODELOCKED
|IO_SYNC
,
797 tcnp
->cn_cred
, NULL
);
801 error
= ext2_dirremove(fdvp
, fcnp
);
804 xp
->i_flag
|= IN_CHANGE
;
806 xp
->i_flag
&= ~IN_RENAME
;
820 ip
->i_flag
&= ~IN_RENAME
;
822 error
= vn_lock(fvp
, LK_EXCLUSIVE
);
825 ip
->i_flag
|= IN_CHANGE
;
826 ip
->i_flag
&= ~IN_RENAME
;
837 * ext2_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
838 * struct componentname *a_cnp, struct vattr *a_vap)
841 ext2_mkdir(struct vop_old_mkdir_args
*ap
)
843 struct vnode
*dvp
= ap
->a_dvp
;
844 struct vattr
*vap
= ap
->a_vap
;
845 struct componentname
*cnp
= ap
->a_cnp
;
846 struct inode
*ip
, *dp
;
848 struct dirtemplate dirtemplate
, *dtp
;
852 if ((nlink_t
)dp
->i_nlink
>= LINK_MAX
) {
856 dmode
= vap
->va_mode
& 0777;
859 * Must simulate part of ext2_makeinode here to acquire the inode,
860 * but not have it entered in the parent directory. The entry is
861 * made later after writing "." and ".." entries.
863 error
= EXT2_VALLOC(dvp
, dmode
, cnp
->cn_cred
, &tvp
);
867 ip
->i_gid
= dp
->i_gid
;
871 struct ucred ucred
, *ucp
;
875 * if we are hacking owners here, (only do this where told to)
876 * and we are not giving it TOO root, (would subvert quotas)
877 * then go ahead and give it to the other user.
878 * The new directory also inherits the SUID bit.
879 * If user's UID and dir UID are the same,
880 * 'give it away' so that the SUID is still forced on.
882 if ( (dvp
->v_mount
->mnt_flag
& MNT_SUIDDIR
) &&
883 (dp
->i_mode
& ISUID
) && dp
->i_uid
) {
885 ip
->i_uid
= dp
->i_uid
;
887 if (dp
->i_uid
!= cnp
->cn_cred
->cr_uid
) {
889 * make sure the correct user gets charged
891 * Make a dummy credential for the victim.
892 * XXX This seems to never be accessed out of
893 * our context so a stack variable is ok.
896 ucred
.cr_uid
= ip
->i_uid
;
897 ucred
.cr_ngroups
= 1;
898 ucred
.cr_groups
[0] = dp
->i_gid
;
903 ip
->i_uid
= cnp
->cn_cred
->cr_uid
;
906 if ((error
= ext2_getinoquota(ip
)) ||
907 (error
= ext2_chkiq(ip
, 1, ucp
, 0))) {
908 EXT2_VFREE(tvp
, ip
->i_number
, dmode
);
915 ip
->i_uid
= cnp
->cn_cred
->cr_uid
;
917 if ((error
= ext2_getinoquota(ip
)) ||
918 (error
= ext2_chkiq(ip
, 1, cnp
->cn_cred
, 0))) {
919 EXT2_VFREE(tvp
, ip
->i_number
, dmode
);
925 ip
->i_flag
|= IN_ACCESS
| IN_CHANGE
| IN_UPDATE
;
927 tvp
->v_type
= VDIR
; /* Rest init'd in getnewvnode(). */
929 if (cnp
->cn_flags
& CNP_ISWHITEOUT
)
930 ip
->i_flags
|= UF_OPAQUE
;
931 error
= EXT2_UPDATE(tvp
, 1);
934 * The vnode must have a VM object in order to issue buffer cache
937 vinitvmio(tvp
, 0, PAGE_SIZE
, -1);
940 * Bump link count in parent directory
941 * to reflect work done below. Should
942 * be done before reference is created
943 * so reparation is possible if we crash.
946 dp
->i_flag
|= IN_CHANGE
;
947 error
= EXT2_UPDATE(dvp
, 1);
951 /* Initialize directory with "." and ".." from static template. */
952 if (EXT2_HAS_INCOMPAT_FEATURE(ip
->i_e2fs
->s_es
,
953 EXT2_FEATURE_INCOMPAT_FILETYPE
))
954 dtp
= &ext2_mastertemplate
;
956 dtp
= &ext2_omastertemplate
;
958 dirtemplate
.dot_ino
= ip
->i_number
;
959 dirtemplate
.dotdot_ino
= dp
->i_number
;
960 /* note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE
961 * so let's just redefine it - for this function only
964 #define DIRBLKSIZ VTOI(dvp)->i_e2fs->s_blocksize
965 dirtemplate
.dotdot_reclen
= DIRBLKSIZ
- 12;
966 error
= vn_rdwr(UIO_WRITE
, tvp
, (caddr_t
)&dirtemplate
,
967 sizeof (dirtemplate
), (off_t
)0, UIO_SYSSPACE
,
968 IO_NODELOCKED
|IO_SYNC
, cnp
->cn_cred
, NULL
);
971 dp
->i_flag
|= IN_CHANGE
;
974 if (DIRBLKSIZ
> VFSTOEXT2(dvp
->v_mount
)->um_mountp
->mnt_stat
.f_bsize
)
975 panic("ext2_mkdir: blksize"); /* XXX should grow with balloc() */
977 ip
->i_size
= DIRBLKSIZ
;
978 ip
->i_flag
|= IN_CHANGE
;
981 /* Directory set up, now install its entry in the parent directory. */
982 error
= ext2_direnter(ip
, dvp
, cnp
);
985 dp
->i_flag
|= IN_CHANGE
;
989 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
990 * for us because we set the link count to 0.
994 ip
->i_flag
|= IN_CHANGE
;
1001 #define DIRBLKSIZ DEV_BSIZE
1005 * Rmdir system call.
1007 * ext2_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1008 * struct componentname *a_cnp)
1011 ext2_rmdir(struct vop_old_rmdir_args
*ap
)
1013 struct vnode
*vp
= ap
->a_vp
;
1014 struct vnode
*dvp
= ap
->a_dvp
;
1015 struct componentname
*cnp
= ap
->a_cnp
;
1016 struct inode
*ip
, *dp
;
1023 * Verify the directory is empty (and valid).
1024 * (Rmdir ".." won't be valid since
1025 * ".." will contain a reference to
1026 * the current directory and thus be
1030 if (ip
->i_nlink
!= 2 || !ext2_dirempty(ip
, dp
->i_number
, cnp
->cn_cred
)) {
1034 if ((dp
->i_flags
& APPEND
)
1035 || (ip
->i_flags
& (NOUNLINK
| IMMUTABLE
| APPEND
))) {
1040 * Delete reference to directory before purging
1041 * inode. If we crash in between, the directory
1042 * will be reattached to lost+found,
1044 error
= ext2_dirremove(dvp
, cnp
);
1048 dp
->i_flag
|= IN_CHANGE
;
1051 * Truncate inode. The only stuff left
1052 * in the directory is "." and "..". The
1053 * "." reference is inconsequential since
1054 * we're quashing it. The ".." reference
1055 * has already been adjusted above. We've
1056 * removed the "." reference and the reference
1057 * in the parent directory, but there may be
1058 * other hard links so decrement by 2 and
1059 * worry about them later.
1062 error
= EXT2_TRUNCATE(vp
, (off_t
)0, IO_SYNC
, cnp
->cn_cred
);
1063 vn_lock(dvp
, LK_EXCLUSIVE
| LK_RETRY
);
1069 * symlink -- make a symbolic link
1071 * ext2_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1072 * struct componentname *a_cnp, struct vattr *a_vap,
1076 ext2_symlink(struct vop_old_symlink_args
*ap
)
1078 struct vnode
*vp
, **vpp
= ap
->a_vpp
;
1082 error
= ext2_makeinode(IFLNK
| ap
->a_vap
->va_mode
, ap
->a_dvp
,
1087 len
= strlen(ap
->a_target
);
1088 if (len
< vp
->v_mount
->mnt_maxsymlinklen
) {
1090 bcopy(ap
->a_target
, (char *)ip
->i_shortlink
, len
);
1092 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1095 * Make sure we have a VM object in order to use
1098 if (vp
->v_object
== NULL
)
1099 vinitvmio(vp
, 0, PAGE_SIZE
, -1);
1101 error
= vn_rdwr(UIO_WRITE
, vp
, ap
->a_target
, len
, (off_t
)0,
1102 UIO_SYSSPACE
, IO_NODELOCKED
,
1103 ap
->a_cnp
->cn_cred
, NULL
);
1113 * Allocate a new inode.
1116 ext2_makeinode(int mode
, struct vnode
*dvp
, struct vnode
**vpp
,
1117 struct componentname
*cnp
)
1119 struct inode
*ip
, *pdir
;
1125 if ((mode
& IFMT
) == 0)
1128 error
= EXT2_VALLOC(dvp
, mode
, cnp
->cn_cred
, &tvp
);
1133 ip
->i_gid
= pdir
->i_gid
;
1137 struct ucred ucred
, *ucp
;
1142 * not the owner of the directory,
1143 * and we are hacking owners here, (only do this where told to)
1144 * and we are not giving it TOO root, (would subvert quotas)
1145 * then go ahead and give it to the other user.
1146 * Note that this drops off the execute bits for security.
1148 if ( (dvp
->v_mount
->mnt_flag
& MNT_SUIDDIR
) &&
1149 (pdir
->i_mode
& ISUID
) &&
1150 (pdir
->i_uid
!= cnp
->cn_cred
->cr_uid
) && pdir
->i_uid
) {
1151 ip
->i_uid
= pdir
->i_uid
;
1155 * make sure the correct user gets charged
1157 * Quickly knock up a dummy credential for the victim.
1158 * XXX This seems to never be accessed out of our
1159 * context so a stack variable is ok.
1162 ucred
.cr_uid
= ip
->i_uid
;
1163 ucred
.cr_ngroups
= 1;
1164 ucred
.cr_groups
[0] = pdir
->i_gid
;
1168 ip
->i_uid
= cnp
->cn_cred
->cr_uid
;
1172 if ((error
= ext2_getinoquota(ip
)) ||
1173 (error
= ext2_chkiq(ip
, 1, ucp
, 0))) {
1174 EXT2_VFREE(tvp
, ip
->i_number
, mode
);
1181 ip
->i_uid
= cnp
->cn_cred
->cr_uid
;
1183 if ((error
= ext2_getinoquota(ip
)) ||
1184 (error
= ext2_chkiq(ip
, 1, cnp
->cn_cred
, 0))) {
1185 EXT2_VFREE(tvp
, ip
->i_number
, mode
);
1191 ip
->i_flag
|= IN_ACCESS
| IN_CHANGE
| IN_UPDATE
;
1193 tvp
->v_type
= IFTOVT(mode
); /* Rest init'd in getnewvnode(). */
1195 if ((ip
->i_mode
& ISGID
) && !groupmember(ip
->i_gid
, cnp
->cn_cred
) &&
1196 priv_check_cred(cnp
->cn_cred
, PRIV_VFS_SETGID
, 0))
1197 ip
->i_mode
&= ~ISGID
;
1199 if (cnp
->cn_flags
& CNP_ISWHITEOUT
)
1200 ip
->i_flags
|= UF_OPAQUE
;
1203 * Regular files and directories need VM objects. Softlinks do
1204 * not (not immediately anyway).
1206 if (tvp
->v_type
== VREG
|| tvp
->v_type
== VDIR
)
1207 vinitvmio(tvp
, 0, PAGE_SIZE
, -1);
1210 * Make sure inode goes to disk before directory entry.
1212 error
= EXT2_UPDATE(tvp
, 1);
1215 error
= ext2_direnter(ip
, dvp
, cnp
);
1224 * Write error occurred trying to update the inode
1225 * or the directory so must deallocate the inode.
1228 ip
->i_flag
|= IN_CHANGE
;
1234 ext2_itimes(struct vnode
*vp
)
1240 if ((ip
->i_flag
& (IN_ACCESS
| IN_CHANGE
| IN_UPDATE
)) == 0)
1242 if ((vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) && !DOINGSOFTDEP(vp
))
1243 ip
->i_flag
|= IN_LAZYMOD
;
1245 ip
->i_flag
|= IN_MODIFIED
;
1246 if ((vp
->v_mount
->mnt_flag
& MNT_RDONLY
) == 0) {
1248 if (ip
->i_flag
& IN_ACCESS
) {
1249 ip
->i_atime
= ts
.tv_sec
;
1250 ip
->i_atimensec
= ts
.tv_nsec
;
1252 if (ip
->i_flag
& IN_UPDATE
) {
1253 ip
->i_mtime
= ts
.tv_sec
;
1254 ip
->i_mtimensec
= ts
.tv_nsec
;
1257 if (ip
->i_flag
& IN_CHANGE
) {
1258 ip
->i_ctime
= ts
.tv_sec
;
1259 ip
->i_ctimensec
= ts
.tv_nsec
;
1262 ip
->i_flag
&= ~(IN_ACCESS
| IN_CHANGE
| IN_UPDATE
);
1270 * ext2_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
1271 * struct file *a_fp)
1276 ext2_open(struct vop_open_args
*ap
)
1278 struct vnode
*vp
= ap
->a_vp
;
1281 * Files marked append-only must be opened for appending.
1283 if ((VTOI(vp
)->i_flags
& APPEND
) &&
1284 (ap
->a_mode
& (FWRITE
| O_APPEND
)) == FWRITE
) {
1287 return (vop_stdopen(ap
));
1293 * Update the times on the inode.
1295 * ext2_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred)
1300 ext2_close(struct vop_close_args
*ap
)
1302 struct vnode
*vp
= ap
->a_vp
;
1304 if (VREFCNT(vp
) > 1)
1306 return (vop_stdclose(ap
));
1310 * ext2_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
1314 ext2_access(struct vop_access_args
*ap
)
1316 struct vnode
*vp
= ap
->a_vp
;
1317 struct inode
*ip
= VTOI(vp
);
1321 if (ap
->a_mode
& VWRITE
) {
1322 switch (vp
->v_type
) {
1326 if ((error
= ext2_getinoquota(ip
)) != 0)
1335 error
= vop_helper_access(ap
, ip
->i_uid
, ip
->i_gid
, ip
->i_mode
,
1341 * ext2_getattr(struct vnode *a_vp, struct vattr *a_vap)
1346 ext2_getattr(struct vop_getattr_args
*ap
)
1348 struct vnode
*vp
= ap
->a_vp
;
1349 struct inode
*ip
= VTOI(vp
);
1350 struct vattr
*vap
= ap
->a_vap
;
1354 * Copy from inode table
1356 vap
->va_fsid
= dev2udev(ip
->i_dev
);
1357 vap
->va_fileid
= ip
->i_number
;
1358 vap
->va_mode
= ip
->i_mode
& ~IFMT
;
1359 vap
->va_nlink
= VFSTOEXT2(vp
->v_mount
)->um_i_effnlink_valid
?
1360 ip
->i_effnlink
: ip
->i_nlink
;
1361 vap
->va_uid
= ip
->i_uid
;
1362 vap
->va_gid
= ip
->i_gid
;
1363 vap
->va_rmajor
= umajor(ip
->i_rdev
);
1364 vap
->va_rminor
= uminor(ip
->i_rdev
);
1365 vap
->va_size
= ip
->i_din
.di_size
;
1366 vap
->va_atime
.tv_sec
= ip
->i_atime
;
1367 vap
->va_atime
.tv_nsec
= ip
->i_atimensec
;
1368 vap
->va_mtime
.tv_sec
= ip
->i_mtime
;
1369 vap
->va_mtime
.tv_nsec
= ip
->i_mtimensec
;
1370 vap
->va_ctime
.tv_sec
= ip
->i_ctime
;
1371 vap
->va_ctime
.tv_nsec
= ip
->i_ctimensec
;
1372 vap
->va_flags
= ip
->i_flags
;
1373 vap
->va_gen
= ip
->i_gen
;
1374 vap
->va_blocksize
= vp
->v_mount
->mnt_stat
.f_iosize
;
1375 vap
->va_bytes
= dbtob((u_quad_t
)ip
->i_blocks
);
1376 vap
->va_type
= IFTOVT(ip
->i_mode
);
1377 vap
->va_filerev
= ip
->i_modrev
;
1382 * Set attribute vnode op. called from several syscalls
1384 * ext2_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
1388 ext2_setattr(struct vop_setattr_args
*ap
)
1390 struct vattr
*vap
= ap
->a_vap
;
1391 struct vnode
*vp
= ap
->a_vp
;
1392 struct inode
*ip
= VTOI(vp
);
1393 struct ucred
*cred
= ap
->a_cred
;
1397 * Check for unsettable attributes.
1399 if ((vap
->va_type
!= VNON
) || (vap
->va_nlink
!= VNOVAL
) ||
1400 (vap
->va_fsid
!= VNOVAL
) || (vap
->va_fileid
!= VNOVAL
) ||
1401 (vap
->va_blocksize
!= VNOVAL
) || (vap
->va_rmajor
!= VNOVAL
) ||
1402 ((int)vap
->va_bytes
!= VNOVAL
) || (vap
->va_gen
!= VNOVAL
)) {
1405 if (vap
->va_flags
!= VNOVAL
) {
1406 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
1408 if (cred
->cr_uid
!= ip
->i_uid
&&
1409 (error
= priv_check_cred(cred
, PRIV_VFS_SETATTR
, 0)))
1412 * Note that a root chflags becomes a user chflags when
1413 * we are jailed, unless the jail.chflags_allowed sysctl
1416 if (cred
->cr_uid
== 0 &&
1417 (!jailed(cred
) || jail_chflags_allowed
)) {
1419 & (SF_NOUNLINK
| SF_IMMUTABLE
| SF_APPEND
)) &&
1422 ip
->i_flags
= vap
->va_flags
;
1425 & (SF_NOUNLINK
| SF_IMMUTABLE
| SF_APPEND
) ||
1426 (vap
->va_flags
& UF_SETTABLE
) != vap
->va_flags
)
1428 ip
->i_flags
&= SF_SETTABLE
;
1429 ip
->i_flags
|= (vap
->va_flags
& UF_SETTABLE
);
1431 ip
->i_flag
|= IN_CHANGE
;
1432 if (vap
->va_flags
& (IMMUTABLE
| APPEND
))
1435 if (ip
->i_flags
& (IMMUTABLE
| APPEND
))
1438 * Go through the fields and update iff not VNOVAL.
1440 if (vap
->va_uid
!= (uid_t
)VNOVAL
|| vap
->va_gid
!= (gid_t
)VNOVAL
) {
1441 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
1443 if ((error
= ext2_chown(vp
, vap
->va_uid
, vap
->va_gid
, cred
)) != 0)
1446 if (vap
->va_size
!= VNOVAL
) {
1448 * Disallow write attempts on read-only filesystems;
1449 * unless the file is a socket, fifo, or a block or
1450 * character device resident on the filesystem.
1452 switch (vp
->v_type
) {
1457 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
1463 if ((error
= EXT2_TRUNCATE(vp
, vap
->va_size
, 0, cred
)) != 0)
1467 if (vap
->va_atime
.tv_sec
!= VNOVAL
|| vap
->va_mtime
.tv_sec
!= VNOVAL
) {
1468 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
1470 if (cred
->cr_uid
!= ip
->i_uid
&&
1471 (error
= priv_check_cred(cred
, PRIV_VFS_SETATTR
, 0)) &&
1472 ((vap
->va_vaflags
& VA_UTIMES_NULL
) == 0 ||
1473 (error
= VOP_EACCESS(vp
, VWRITE
, cred
))))
1475 if (vap
->va_atime
.tv_sec
!= VNOVAL
)
1476 ip
->i_flag
|= IN_ACCESS
;
1477 if (vap
->va_mtime
.tv_sec
!= VNOVAL
)
1478 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1480 if (vap
->va_atime
.tv_sec
!= VNOVAL
) {
1481 ip
->i_atime
= vap
->va_atime
.tv_sec
;
1482 ip
->i_atimensec
= vap
->va_atime
.tv_nsec
;
1484 if (vap
->va_mtime
.tv_sec
!= VNOVAL
) {
1485 ip
->i_mtime
= vap
->va_mtime
.tv_sec
;
1486 ip
->i_mtimensec
= vap
->va_mtime
.tv_nsec
;
1488 error
= EXT2_UPDATE(vp
, 0);
1493 if (vap
->va_mode
!= (mode_t
)VNOVAL
) {
1494 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
1496 error
= ext2_chmod(vp
, (int)vap
->va_mode
, cred
);
1498 VN_KNOTE(vp
, NOTE_ATTRIB
);
1503 * Change the mode on a file.
1504 * Inode must be locked before calling.
1507 ext2_chmod(struct vnode
*vp
, int mode
, struct ucred
*cred
)
1509 struct inode
*ip
= VTOI(vp
);
1512 if (cred
->cr_uid
!= ip
->i_uid
) {
1513 error
= priv_check_cred(cred
, PRIV_VFS_CHMOD
, 0);
1518 if (vp
->v_type
!= VDIR
&& (mode
& S_ISTXT
))
1520 if (!groupmember(ip
->i_gid
, cred
) && (mode
& ISGID
))
1523 ip
->i_mode
&= ~ALLPERMS
;
1524 ip
->i_mode
|= (mode
& ALLPERMS
);
1525 ip
->i_flag
|= IN_CHANGE
;
1530 * Perform chown operation on inode ip;
1531 * inode must be locked prior to call.
1534 ext2_chown(struct vnode
*vp
, uid_t uid
, gid_t gid
, struct ucred
*cred
)
1536 struct inode
*ip
= VTOI(vp
);
1545 if (uid
== (uid_t
)VNOVAL
)
1547 if (gid
== (gid_t
)VNOVAL
)
1550 * If we don't own the file, are trying to change the owner
1551 * of the file, or are not a member of the target group,
1552 * the caller must be superuser or the call fails.
1554 if ((cred
->cr_uid
!= ip
->i_uid
|| uid
!= ip
->i_uid
||
1555 (gid
!= ip
->i_gid
&& !(cred
->cr_gid
== gid
||
1556 groupmember(gid
, cred
)))) &&
1557 (error
= priv_check_cred(cred
, PRIV_VFS_CHOWN
, 0)))
1562 if ((error
= ext2_getinoquota(ip
)) != 0)
1565 ext2_dqrele(vp
, ip
->i_dquot
[USRQUOTA
]);
1566 ip
->i_dquot
[USRQUOTA
] = NODQUOT
;
1569 ext2_dqrele(vp
, ip
->i_dquot
[GRPQUOTA
]);
1570 ip
->i_dquot
[GRPQUOTA
] = NODQUOT
;
1572 change
= ip
->i_blocks
;
1573 (void) ext2_chkdq(ip
, -change
, cred
, CHOWN
);
1574 (void) ext2_chkiq(ip
, -1, cred
, CHOWN
);
1575 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1576 ext2_dqrele(vp
, ip
->i_dquot
[i
]);
1577 ip
->i_dquot
[i
] = NODQUOT
;
1583 if ((error
= ext2_getinoquota(ip
)) == 0) {
1585 ext2_dqrele(vp
, ip
->i_dquot
[USRQUOTA
]);
1586 ip
->i_dquot
[USRQUOTA
] = NODQUOT
;
1589 ext2_dqrele(vp
, ip
->i_dquot
[GRPQUOTA
]);
1590 ip
->i_dquot
[GRPQUOTA
] = NODQUOT
;
1592 if ((error
= ext2_chkdq(ip
, change
, cred
, CHOWN
)) == 0) {
1593 if ((error
= ext2_chkiq(ip
, 1, cred
, CHOWN
)) == 0)
1596 (void)ext2_chkdq(ip
, -change
, cred
, CHOWN
|FORCE
);
1598 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1599 ext2_dqrele(vp
, ip
->i_dquot
[i
]);
1600 ip
->i_dquot
[i
] = NODQUOT
;
1605 if (ext2_getinoquota(ip
) == 0) {
1607 ext2_dqrele(vp
, ip
->i_dquot
[USRQUOTA
]);
1608 ip
->i_dquot
[USRQUOTA
] = NODQUOT
;
1611 ext2_dqrele(vp
, ip
->i_dquot
[GRPQUOTA
]);
1612 ip
->i_dquot
[GRPQUOTA
] = NODQUOT
;
1614 (void) ext2_chkdq(ip
, change
, cred
, FORCE
|CHOWN
);
1615 (void) ext2_chkiq(ip
, 1, cred
, FORCE
|CHOWN
);
1616 (void) ext2_getinoquota(ip
);
1620 if (ext2_getinoquota(ip
))
1621 panic("ext2_chown: lost quota");
1623 ip
->i_flag
|= IN_CHANGE
;
1624 if (cred
->cr_uid
!= 0 && (ouid
!= uid
|| ogid
!= gid
))
1625 ip
->i_mode
&= ~(ISUID
| ISGID
);
1632 * NB Currently unsupported.
1634 * ext2_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
1639 ext2_mmap(struct vop_mmap_args
*ap
)
1645 * whiteout vnode call
1647 * ext2_whiteout(struct vnode *a_dvp, struct componentname *a_cnp, int a_flags)
1651 ext2_whiteout(struct vop_old_whiteout_args
*ap
)
1653 return (EOPNOTSUPP
);
1657 * Return target name of a symbolic link
1659 * ext2_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1663 ext2_readlink(struct vop_readlink_args
*ap
)
1665 struct vnode
*vp
= ap
->a_vp
;
1666 struct inode
*ip
= VTOI(vp
);
1670 if ((isize
< vp
->v_mount
->mnt_maxsymlinklen
) ||
1671 (ip
->i_din
.di_blocks
== 0)) { /* XXX - for old fastlink support */
1672 uiomove((char *)ip
->i_shortlink
, isize
, ap
->a_uio
);
1675 return (VOP_READ(vp
, ap
->a_uio
, 0, ap
->a_cred
));
1679 * Calculate the logical to physical mapping if not done already,
1680 * then call the device strategy routine.
1682 * In order to be able to swap to a file, the VOP_BMAP operation may not
1683 * deadlock on memory. See ext2_bmap() for details.
1685 * ext2_strategy(struct vnode *a_vp, struct bio *a_bio)
1689 ext2_strategy(struct vop_strategy_args
*ap
)
1691 struct bio
*bio
= ap
->a_bio
;
1693 struct buf
*bp
= bio
->bio_buf
;
1694 struct vnode
*vp
= ap
->a_vp
;
1699 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
)
1700 panic("ext2_strategy: spec");
1701 nbio
= push_bio(bio
);
1702 if (nbio
->bio_offset
== NOOFFSET
) {
1703 error
= VOP_BMAP(vp
, bio
->bio_offset
, &nbio
->bio_offset
,
1704 NULL
, NULL
, bp
->b_cmd
);
1706 bp
->b_error
= error
;
1707 bp
->b_flags
|= B_ERROR
;
1708 /* I/O was never started on nbio, must biodone(bio) */
1712 if (nbio
->bio_offset
== NOOFFSET
)
1715 if (nbio
->bio_offset
== NOOFFSET
) {
1716 /* I/O was never started on nbio, must biodone(bio) */
1720 vn_strategy(ip
->i_devvp
, nbio
);
1725 * Print out the contents of an inode.
1727 * ext2_print(struct vnode *a_vp)
1731 ext2_print(struct vop_print_args
*ap
)
1733 struct vnode
*vp
= ap
->a_vp
;
1734 struct inode
*ip
= VTOI(vp
);
1736 kprintf("tag VT_EXT2FS, ino %lu, on dev %s (%d, %d)",
1737 (u_long
)ip
->i_number
, devtoname(ip
->i_dev
), major(ip
->i_dev
),
1739 if (vp
->v_type
== VFIFO
)
1741 lockmgr_printinfo(&vp
->v_lock
);
1747 * Read wrapper for fifos.
1749 * ext2fifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1750 * struct ucred *a_cred)
1754 ext2fifo_read(struct vop_read_args
*ap
)
1761 resid
= uio
->uio_resid
;
1762 error
= VOCALL(&fifo_vnode_vops
, &ap
->a_head
);
1763 ip
= VTOI(ap
->a_vp
);
1764 if ((ap
->a_vp
->v_mount
->mnt_flag
& MNT_NOATIME
) == 0 && ip
!= NULL
&&
1765 (uio
->uio_resid
!= resid
|| (error
== 0 && resid
!= 0)))
1766 VTOI(ap
->a_vp
)->i_flag
|= IN_ACCESS
;
1771 * Write wrapper for fifos.
1773 * ext2fifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1774 * struct ucred *a_cred)
1778 ext2fifo_write(struct vop_write_args
*ap
)
1785 resid
= uio
->uio_resid
;
1786 error
= VOCALL(&fifo_vnode_vops
, &ap
->a_head
);
1787 ip
= VTOI(ap
->a_vp
);
1788 if (ip
!= NULL
&& (uio
->uio_resid
!= resid
|| (error
== 0 && resid
!= 0)))
1789 VTOI(ap
->a_vp
)->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1794 * Close wrapper for fifos.
1796 * Update the times on the inode then do device close.
1798 * ext2fifo_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred)
1802 ext2fifo_close(struct vop_close_args
*ap
)
1804 struct vnode
*vp
= ap
->a_vp
;
1806 if (VREFCNT(vp
) > 1)
1808 return (VOCALL(&fifo_vnode_vops
, &ap
->a_head
));
1812 * Kqfilter wrapper for fifos.
1814 * Fall through to ext2 kqfilter routines if needed
1818 ext2fifo_kqfilter(struct vop_kqfilter_args
*ap
)
1822 error
= VOCALL(&fifo_vnode_vops
, &ap
->a_head
);
1824 error
= ext2_kqfilter(ap
);
1829 * Return POSIX pathconf information applicable to ext2 filesystems.
1831 * ext2_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
1835 ext2_pathconf(struct vop_pathconf_args
*ap
)
1837 switch (ap
->a_name
) {
1839 *ap
->a_retval
= LINK_MAX
;
1842 *ap
->a_retval
= NAME_MAX
;
1845 *ap
->a_retval
= PATH_MAX
;
1848 *ap
->a_retval
= PIPE_BUF
;
1850 case _PC_CHOWN_RESTRICTED
:
1863 * Advisory record locking support
1865 * ext2_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
1870 ext2_advlock(struct vop_advlock_args
*ap
)
1872 struct inode
*ip
= VTOI(ap
->a_vp
);
1874 return (lf_advlock(ap
, &(ip
->i_lockf
), ip
->i_size
));
1878 * Initialize the vnode associated with a new inode, handle aliased
1882 ext2_vinit(struct mount
*mntp
, struct vnode
**vpp
)
1891 switch(vp
->v_type
= IFTOVT(ip
->i_mode
)) {
1894 vp
->v_ops
= &mntp
->mnt_vn_spec_ops
;
1895 addaliasu(vp
, umajor(ip
->i_rdev
), uminor(ip
->i_rdev
));
1898 vp
->v_ops
= &mntp
->mnt_vn_fifo_ops
;
1902 vinitvmio(vp
, ip
->i_size
, PAGE_SIZE
, -1); /* XXX */
1905 if ((ip
->i_size
>= vp
->v_mount
->mnt_maxsymlinklen
) &&
1906 ip
->i_din
.di_blocks
!= 0
1908 vinitvmio(vp
, ip
->i_size
, PAGE_SIZE
, -1);
1916 if (ip
->i_number
== EXT2_ROOTINO
)
1917 vp
->v_flag
|= VROOT
;
1919 * Initialize modrev times
1921 getmicrouptime(&tv
);
1922 SETHIGH(ip
->i_modrev
, tv
.tv_sec
);
1923 SETLOW(ip
->i_modrev
, tv
.tv_usec
* 4294);
1928 static struct filterops ext2read_filtops
=
1929 { FILTEROP_ISFD
, NULL
, filt_ext2detach
, filt_ext2read
};
1930 static struct filterops ext2write_filtops
=
1931 { FILTEROP_ISFD
, NULL
, filt_ext2detach
, filt_ext2write
};
1932 static struct filterops ext2vnode_filtops
=
1933 { FILTEROP_ISFD
, NULL
, filt_ext2detach
, filt_ext2vnode
};
1936 * ext2_kqfilter(struct vnode *a_vp, struct knote *a_kn)
1939 ext2_kqfilter(struct vop_kqfilter_args
*ap
)
1941 struct vnode
*vp
= ap
->a_vp
;
1942 struct knote
*kn
= ap
->a_kn
;
1944 switch (kn
->kn_filter
) {
1946 kn
->kn_fop
= &ext2read_filtops
;
1949 kn
->kn_fop
= &ext2write_filtops
;
1952 kn
->kn_fop
= &ext2vnode_filtops
;
1955 return (EOPNOTSUPP
);
1958 kn
->kn_hook
= (caddr_t
)vp
;
1960 /* XXX: kq token actually protects the list */
1961 lwkt_gettoken(&vp
->v_token
);
1962 knote_insert(&vp
->v_pollinfo
.vpi_kqinfo
.ki_note
, kn
);
1963 lwkt_reltoken(&vp
->v_token
);
1969 filt_ext2detach(struct knote
*kn
)
1971 struct vnode
*vp
= (struct vnode
*)kn
->kn_hook
;
1973 lwkt_gettoken(&vp
->v_token
);
1974 knote_remove(&vp
->v_pollinfo
.vpi_kqinfo
.ki_note
, kn
);
1975 lwkt_reltoken(&vp
->v_token
);
1980 filt_ext2read(struct knote
*kn
, long hint
)
1982 struct vnode
*vp
= (struct vnode
*)kn
->kn_hook
;
1983 struct inode
*ip
= VTOI(vp
);
1987 * filesystem is gone, so set the EOF flag and schedule
1988 * the knote for deletion.
1990 if (hint
== NOTE_REVOKE
) {
1991 kn
->kn_flags
|= (EV_EOF
| EV_NODATA
| EV_ONESHOT
);
1994 off
= ip
->i_size
- kn
->kn_fp
->f_offset
;
1995 kn
->kn_data
= (off
< INTPTR_MAX
) ? off
: INTPTR_MAX
;
1996 if (kn
->kn_sfflags
& NOTE_OLDAPI
)
1998 return (kn
->kn_data
!= 0);
2003 filt_ext2write(struct knote
*kn
, long hint
)
2006 * filesystem is gone, so set the EOF flag and schedule
2007 * the knote for deletion.
2009 if (hint
== NOTE_REVOKE
)
2010 kn
->kn_flags
|= (EV_EOF
| EV_NODATA
| EV_ONESHOT
);
2017 filt_ext2vnode(struct knote
*kn
, long hint
)
2019 if (kn
->kn_sfflags
& hint
)
2020 kn
->kn_fflags
|= hint
;
2021 if (hint
== NOTE_REVOKE
) {
2022 kn
->kn_flags
|= (EV_EOF
| EV_NODATA
);
2025 return (kn
->kn_fflags
!= 0);
2028 struct vop_ops ext2_vnode_vops
= {
2029 .vop_default
= vop_defaultop
,
2030 .vop_fsync
= ext2_fsync
,
2031 .vop_read
= ext2_read
,
2032 .vop_reallocblks
= ext2_reallocblks
,
2033 .vop_write
= ext2_write
,
2034 .vop_access
= ext2_access
,
2035 .vop_advlock
= ext2_advlock
,
2036 .vop_bmap
= ext2_bmap
,
2037 .vop_old_lookup
= ext2_lookup
,
2038 .vop_close
= ext2_close
,
2039 .vop_old_create
= ext2_create
,
2040 .vop_getattr
= ext2_getattr
,
2041 .vop_inactive
= ext2_inactive
,
2042 .vop_old_link
= ext2_link
,
2043 .vop_old_mkdir
= ext2_mkdir
,
2044 .vop_old_mknod
= ext2_mknod
,
2045 .vop_mmap
= ext2_mmap
,
2046 .vop_open
= ext2_open
,
2047 .vop_pathconf
= ext2_pathconf
,
2048 .vop_kqfilter
= ext2_kqfilter
,
2049 .vop_print
= ext2_print
,
2050 .vop_readdir
= ext2_readdir
,
2051 .vop_readlink
= ext2_readlink
,
2052 .vop_reclaim
= ext2_reclaim
,
2053 .vop_old_remove
= ext2_remove
,
2054 .vop_old_rename
= ext2_rename
,
2055 .vop_old_rmdir
= ext2_rmdir
,
2056 .vop_setattr
= ext2_setattr
,
2057 .vop_strategy
= ext2_strategy
,
2058 .vop_old_symlink
= ext2_symlink
,
2059 .vop_old_whiteout
= ext2_whiteout
,
2060 .vop_getpages
= vop_stdgetpages
,
2061 .vop_putpages
= vop_stdputpages
2064 struct vop_ops ext2_spec_vops
= {
2065 .vop_default
= vop_defaultop
,
2066 .vop_fsync
= ext2_fsync
,
2067 .vop_access
= ext2_access
,
2068 .vop_close
= ext2_close
,
2069 .vop_getattr
= ext2_getattr
,
2070 .vop_inactive
= ext2_inactive
,
2071 .vop_print
= ext2_print
,
2072 .vop_read
= vop_stdnoread
,
2073 .vop_reclaim
= ext2_reclaim
,
2074 .vop_setattr
= ext2_setattr
,
2075 .vop_write
= vop_stdnowrite
2078 struct vop_ops ext2_fifo_vops
= {
2079 .vop_default
= ext2_vnoperatefifo
,
2080 .vop_fsync
= ext2_fsync
,
2081 .vop_access
= ext2_access
,
2082 .vop_close
= ext2fifo_close
,
2083 .vop_getattr
= ext2_getattr
,
2084 .vop_inactive
= ext2_inactive
,
2085 .vop_kqfilter
= ext2fifo_kqfilter
,
2086 .vop_print
= ext2_print
,
2087 .vop_read
= ext2fifo_read
,
2088 .vop_reclaim
= ext2_reclaim
,
2089 .vop_setattr
= ext2_setattr
,
2090 .vop_write
= ext2fifo_write
2093 VNODEOP_SET(ext2_vnode_vops
);
2094 VNODEOP_SET(ext2_spec_vops
);
2095 VNODEOP_SET(ext2_fifo_vops
);
2101 ext2_vnoperate(struct vop_generic_args
*ap
)
2103 return (VOCALL(&ext2_vnode_vops
, ap
));
2107 * ext2_vnoperatefifo()
2110 ext2_vnoperatefifo(struct vop_generic_args
*ap
)
2112 return (VOCALL(&ext2_fifo_vops
, ap
));