kernel - Finish implementing PG_RAM / pipelined mmap operation
[dragonfly.git] / sys / vfs / gnu / ext2fs / ext2_vnops.c
1 /*
2  *  modified for EXT2FS support in Lites 1.1
3  *
4  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5  *  University of Utah, Department of Computer Science
6  */
7 /*
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.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
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.
31  *
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
42  * SUCH DAMAGE.
43  *
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.43 2008/06/19 23:27:39 dillon Exp $
48  */
49
50 #include "opt_quota.h"
51 #include "opt_suiddir.h"
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/resourcevar.h>
56 #include <sys/kernel.h>
57 #include <sys/stat.h>
58 #include <sys/fcntl.h>
59 #include <sys/buf.h>
60 #include <sys/stat.h>
61 #include <sys/proc.h>
62 #include <sys/priv.h>
63 #include <sys/mount.h>
64 #include <sys/time.h>
65 #include <sys/vnode.h>
66 #include <sys/dirent.h>
67 #include <sys/lockf.h>
68 #include <sys/event.h>
69 #include <sys/conf.h>
70 #include <sys/file.h>
71 #include <sys/jail.h>
72 #include <sys/namei.h>
73 #include <sys/signalvar.h>
74 #include <sys/unistd.h>
75
76 #include <vm/vm.h>
77 #include <vm/vm_extern.h>
78 #include <vm/vm_zone.h>
79 #include <vm/vnode_pager.h>
80
81 #include <sys/buf2.h>
82 #include <sys/thread2.h>
83
84 #include <vfs/fifofs/fifo.h>
85
86 #include "dir.h"
87 #include "quota.h"
88 #include "inode.h"
89 #include "ext2mount.h"
90 #include "ext2_fs_sb.h"
91 #include "fs.h"
92 #include "ext2_extern.h"
93 #include "ext2_fs.h"
94
95 static int ext2_access (struct vop_access_args *);
96 static int ext2_advlock (struct vop_advlock_args *);
97 static int ext2_chmod (struct vnode *, int, struct ucred *);
98 static int ext2_chown (struct vnode *, uid_t, gid_t, struct ucred *);
99 static int ext2_close (struct vop_close_args *);
100 static int ext2_getattr (struct vop_getattr_args *);
101 static int ext2_makeinode (int mode, struct vnode *, struct vnode **, struct componentname *);
102 static int ext2_mmap (struct vop_mmap_args *);
103 static int ext2_open (struct vop_open_args *);
104 static int ext2_pathconf (struct vop_pathconf_args *);
105 static int ext2_print (struct vop_print_args *);
106 static int ext2_readlink (struct vop_readlink_args *);
107 static int ext2_setattr (struct vop_setattr_args *);
108 static int ext2_strategy (struct vop_strategy_args *);
109 static int ext2_whiteout (struct vop_old_whiteout_args *);
110 static int filt_ext2read (struct knote *kn, long hint);
111 static int filt_ext2write (struct knote *kn, long hint);
112 static int filt_ext2vnode (struct knote *kn, long hint);
113 static void filt_ext2detach (struct knote *kn);
114 static int ext2_kqfilter (struct vop_kqfilter_args *ap);
115 static int ext2fifo_close (struct vop_close_args *);
116 static int ext2fifo_kqfilter (struct vop_kqfilter_args *);
117 static int ext2fifo_read (struct vop_read_args *);
118 static int ext2fifo_write (struct vop_write_args *);
119
120 static int ext2_fsync (struct vop_fsync_args *);
121 static int ext2_read (struct vop_read_args *);
122 static int ext2_write (struct vop_write_args *);
123 static int ext2_remove (struct vop_old_remove_args *);
124 static int ext2_link (struct vop_old_link_args *);
125 static int ext2_rename (struct vop_old_rename_args *);
126 static int ext2_mkdir (struct vop_old_mkdir_args *);
127 static int ext2_rmdir (struct vop_old_rmdir_args *);
128 static int ext2_create (struct vop_old_create_args *);
129 static int ext2_mknod (struct vop_old_mknod_args *);
130 static int ext2_symlink (struct vop_old_symlink_args *);
131 static int ext2_getpages (struct vop_getpages_args *);
132
133 #include "ext2_readwrite.c"
134
135 union _qcvt {
136         int64_t qcvt;
137         int32_t val[2];
138 };
139 #define SETHIGH(q, h) { \
140         union _qcvt tmp; \
141         tmp.qcvt = (q); \
142         tmp.val[_QUAD_HIGHWORD] = (h); \
143         (q) = tmp.qcvt; \
144 }
145 #define SETLOW(q, l) { \
146         union _qcvt tmp; \
147         tmp.qcvt = (q); \
148         tmp.val[_QUAD_LOWWORD] = (l); \
149         (q) = tmp.qcvt; \
150 }
151 #define VN_KNOTE(vp, b) \
152         KNOTE(&vp->v_pollinfo.vpi_selinfo.si_note, (b))
153
154 #define OFSFMT(vp)              ((vp)->v_mount->mnt_maxsymlinklen <= 0)
155
156 /*
157  * A virgin directory (no blushing please).
158  * Note that the type and namlen fields are reversed relative to ufs.
159  * Also, we don't use `struct odirtemplate', since it would just cause
160  * endianness problems.
161  */
162 static struct dirtemplate ext2_mastertemplate = {
163         0, 12, 1, EXT2_FT_DIR, ".",
164         0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
165 };
166 static struct dirtemplate ext2_omastertemplate = {
167         0, 12, 1, EXT2_FT_UNKNOWN, ".",
168         0, DIRBLKSIZ - 12, 2, EXT2_FT_UNKNOWN, ".."
169 };
170
171 /*
172  * Create a regular file
173  *
174  * ext2_create(struct vnode *a_dvp, struct vnode **a_vpp,
175  *             struct componentname *a_cnp, struct vattr *a_vap)
176  */
177 static int
178 ext2_create(struct vop_old_create_args *ap)
179 {
180         int error;
181
182         error =
183             ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
184             ap->a_dvp, ap->a_vpp, ap->a_cnp);
185         if (error)
186                 return (error);
187         return (0);
188 }
189
190 /*
191  * Synch an open file.
192  *
193  * ext2_fsync(struct vnode *a_vp, struct ucred *a_cred, int a_waitfor,
194  *            struct proc *a_p)
195  */
196 /* ARGSUSED */
197
198 static int ext2_fsync_bp(struct buf *bp, void *data);
199
200 struct ext2_fsync_bp_info {
201         struct vnode *vp;
202         int waitfor;
203 };
204
205 static int
206 ext2_fsync(struct vop_fsync_args *ap)
207 {
208         struct ext2_fsync_bp_info info;
209         struct vnode *vp = ap->a_vp;
210         lwkt_tokref vlock;
211         int count;
212
213         /* 
214          * XXX why is all this fs specific?
215          */
216
217         /*
218          * Flush all dirty buffers associated with a vnode.
219          */
220         ext2_discard_prealloc(VTOI(vp));
221
222         lwkt_gettoken(&vlock, &vp->v_token);
223         info.vp = vp;
224 loop:
225         info.waitfor = ap->a_waitfor;
226         count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, 
227                         ext2_fsync_bp, &info);
228         if (count)
229                 goto loop;
230
231         if (ap->a_waitfor == MNT_WAIT) {
232                 bio_track_wait(&vp->v_track_write, 0, 0);
233 #if DIAGNOSTIC
234                 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
235                         vprint("ext2_fsync: dirty", vp);
236                         goto loop;
237                 }
238 #endif
239         }
240         lwkt_reltoken(&vlock);
241         return (EXT2_UPDATE(ap->a_vp, ap->a_waitfor == MNT_WAIT));
242 }
243
244 static int
245 ext2_fsync_bp(struct buf *bp, void *data)
246 {
247         struct ext2_fsync_bp_info *info = data;
248
249         if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
250                 return(0);
251         if ((bp->b_flags & B_DELWRI) == 0)
252                 panic("ext2_fsync: not dirty");
253         bremfree(bp);
254
255         /*
256          * Wait for I/O associated with indirect blocks to complete,
257          * since there is no way to quickly wait for them below.
258          */
259         if (bp->b_vp == info->vp || info->waitfor == MNT_NOWAIT)
260                 bawrite(bp);
261         else
262                 bwrite(bp);
263         return(1);
264 }
265
266 /*
267  * Mknod vnode call
268  *
269  * ext2_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
270  *            struct componentname *a_cnp, struct vattr *a_vap)
271  */
272 /* ARGSUSED */
273 static int
274 ext2_mknod(struct vop_old_mknod_args *ap)
275 {
276         struct vattr *vap = ap->a_vap;
277         struct vnode **vpp = ap->a_vpp;
278         struct inode *ip;
279         ino_t ino;
280         int error;
281
282         if (vap->va_rmajor != VNOVAL &&
283             makeudev(vap->va_rmajor, vap->va_rminor) == NOUDEV) {
284                 return (EINVAL);
285         }
286
287         error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
288             ap->a_dvp, vpp, ap->a_cnp);
289         if (error)
290                 return (error);
291         ip = VTOI(*vpp);
292         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
293         if (vap->va_rmajor != VNOVAL) {
294                 /*
295                  * Want to be able to use this to make badblock
296                  * inodes, so don't truncate the dev number.
297                  */
298                 ip->i_rdev = makeudev(vap->va_rmajor, vap->va_rminor);
299         }
300         /*
301          * Remove inode, then reload it through VFS_VGET so it is
302          * checked to see if it is an alias of an existing entry in
303          * the inode cache.
304          */
305         (*vpp)->v_type = VNON;
306         ino = ip->i_number;     /* Save this before vgone() invalidates ip. */
307         vgone_vxlocked(*vpp);
308         vput(*vpp);
309         error = VFS_VGET(ap->a_dvp->v_mount, NULL, ino, vpp);
310         if (error) {
311                 *vpp = NULL;
312                 return (error);
313         }
314         return (0);
315 }
316
317 /*
318  * ext2_remove(struct vnode *a_dvp, struct vnode *a_vp,
319  *             struct componentname *a_cnp)
320  */
321 static int
322 ext2_remove(struct vop_old_remove_args *ap)
323 {
324         struct inode *ip;
325         struct vnode *vp = ap->a_vp;
326         struct vnode *dvp = ap->a_dvp;
327         int error;
328
329         ip = VTOI(vp);
330         if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
331             (VTOI(dvp)->i_flags & APPEND)) {
332                 error = EPERM;
333                 goto out;
334         }
335         error = ext2_dirremove(dvp, ap->a_cnp);
336         if (error == 0) {
337                 ip->i_nlink--;
338                 ip->i_flag |= IN_CHANGE;
339         }
340 out:
341         return (error);
342 }
343
344 /*
345  * link vnode call
346  *
347  * ext2_link(struct vnode *a_tdvp, struct vnode *a_vp,
348  *           struct componentname *a_cnp)
349  */
350 static int
351 ext2_link(struct vop_old_link_args *ap)
352 {
353         struct vnode *vp = ap->a_vp;
354         struct vnode *tdvp = ap->a_tdvp;
355         struct componentname *cnp = ap->a_cnp;
356         struct inode *ip;
357         int error;
358
359         if (tdvp->v_mount != vp->v_mount) {
360                 error = EXDEV;
361                 goto out2;
362         }
363         if (tdvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE))) {
364                 goto out2;
365         }
366         ip = VTOI(vp);
367         if ((nlink_t)ip->i_nlink >= LINK_MAX) {
368                 error = EMLINK;
369                 goto out1;
370         }
371         if (ip->i_flags & (IMMUTABLE | APPEND)) {
372                 error = EPERM;
373                 goto out1;
374         }
375         ip->i_nlink++;
376         ip->i_flag |= IN_CHANGE;
377         error = EXT2_UPDATE(vp, 1);
378         if (!error)
379                 error = ext2_direnter(ip, tdvp, cnp);
380         if (error) {
381                 ip->i_nlink--;
382                 ip->i_flag |= IN_CHANGE;
383         }
384 out1:
385         if (tdvp != vp)
386                 vn_unlock(vp);
387 out2:
388         return (error);
389 }
390
391 /*
392  * Rename system call.  fdvp, fvp are ref'd.  tvp, tdvp are ref'd and locked.
393  * all vp's are released and must be in an unlocked state on return.
394  *
395  * ext2_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
396  *              struct componentname *a_fcnp, struct vnode *a_tdvp,
397  *              struct vnode *a_tvp, struct componentname *a_tcnp)
398  */
399 static int
400 ext2_rename(struct vop_old_rename_args *ap)
401 {
402         struct vnode *tvp = ap->a_tvp;
403         struct vnode *tdvp = ap->a_tdvp;
404         struct vnode *fvp = ap->a_fvp;
405         struct vnode *fdvp = ap->a_fdvp;
406         struct componentname *tcnp = ap->a_tcnp;
407         struct componentname *fcnp = ap->a_fcnp;
408         struct inode *ip, *xp, *dp;
409         struct dirtemplate dirbuf;
410         int doingdirectory = 0, oldparent = 0, newparent = 0;
411         int error = 0;
412         u_char namlen;
413
414         /*
415          * Check for cross-device rename.
416          */
417         if ((fvp->v_mount != tdvp->v_mount) ||
418             (tvp && (fvp->v_mount != tvp->v_mount)) ||
419             tvp == tdvp) {
420                 error = EXDEV;
421 abortit:
422                 if (tdvp == tvp)
423                         vrele(tdvp);
424                 else
425                         vput(tdvp);
426                 if (tvp)
427                         vput(tvp);
428                 vrele(fdvp);
429                 vrele(fvp);
430                 return (error);
431         }
432
433         if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
434             (VTOI(tdvp)->i_flags & APPEND))) {
435                 error = EPERM;
436                 goto abortit;
437         }
438
439         /*
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.
442          */
443         if (fvp == tvp) {
444                 error = 0;
445                 goto abortit;
446         }
447
448         if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
449                 goto abortit;
450
451         /*
452          * fvp, tvp, tdvp locked.  fdvp not locked but note that fdvp may
453          * be equal to tdvp.
454          */
455         dp = VTOI(fdvp);
456         ip = VTOI(fvp);
457         if (ip->i_nlink >= LINK_MAX) {
458                 vn_unlock(fvp);
459                 error = EMLINK;
460                 goto abortit;
461         }
462         if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
463             || (dp->i_flags & APPEND)) {
464                 vn_unlock(fvp);
465                 error = EPERM;
466                 goto abortit;
467         }
468         if ((ip->i_mode & IFMT) == IFDIR) {
469                 /*
470                  * Avoid ".", "..", and aliases of "." for obvious reasons.
471                  */
472                 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
473                     dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & CNP_ISDOTDOT ||
474                     (ip->i_flag & IN_RENAME)) {
475                         vn_unlock(fvp);
476                         error = EINVAL;
477                         goto abortit;
478                 }
479                 ip->i_flag |= IN_RENAME;
480                 oldparent = dp->i_number;
481                 doingdirectory++;
482         }
483
484         /*
485          * tvp is non-NULL if the target exists.   fvp is still locked but
486          * we will unlock it soon.  The 'bad' goto target requires dp and
487          * xp to be correctly assigned.
488          */
489         dp = VTOI(tdvp);
490         if (tvp)
491                 xp = VTOI(tvp);
492         else
493                 xp = NULL;
494
495         /*
496          * 1) Bump link count while we're moving stuff
497          *    around.  If we crash somewhere before
498          *    completing our work, the link count
499          *    may be wrong, but correctable.
500          */
501         ip->i_nlink++;
502         ip->i_flag |= IN_CHANGE;
503         if ((error = EXT2_UPDATE(fvp, 1)) != 0) {
504                 vn_unlock(fvp);
505                 goto bad;
506         }
507
508         /*
509          * If ".." must be changed (ie the directory gets a new
510          * parent) then the source directory must not be in the
511          * directory heirarchy above the target, as this would
512          * orphan everything below the source directory. Also
513          * the user must have write permission in the source so
514          * as to be able to change "..". We must repeat the call
515          * to namei, as the parent directory is unlocked by the
516          * call to checkpath().
517          */
518         error = VOP_EACCESS(fvp, VWRITE, tcnp->cn_cred);
519         vn_unlock(fvp);
520
521         /*
522          * tvp (if not NULL) and tdvp are locked.  fvp and fdvp are not.
523          * dp and xp are set according to tdvp and tvp.
524          */
525         if (oldparent != dp->i_number)
526                 newparent = dp->i_number;
527         if (doingdirectory && newparent) {
528                 if (error)      /* write access check above */
529                         goto bad;
530
531                 /*
532                  * Prepare for relookup, get rid of xp
533                  */
534                 if (xp != NULL) {
535                         vput(tvp);
536                         xp = NULL;
537                 }
538
539                 /*
540                  * checkpath vput()'s tdvp (VTOI(dp)) on return no matter what,
541                  * get an extra ref so we wind up with just an unlocked, ref'd
542                  * tdvp.  The 'out' target skips xp and tdvp cleanups.  Our
543                  * tdvp is now unlocked so we have to clean it up ourselves.
544                  */
545                 vref(tdvp);
546                 error = ext2_checkpath(ip, dp, tcnp->cn_cred);
547                 tcnp->cn_flags |= CNP_PDIRUNLOCK;
548                 if (error) {
549                         vrele(tdvp);
550                         goto out;
551                 }
552                 /*
553                  * relookup no longer messes with the ref count.  An unlocked
554                  * tdvp must be passed and if no error occurs a locked tdvp
555                  * will be returned.  We have to use the out target again.
556                  */
557                 error = relookup(tdvp, &tvp, tcnp);
558                 if (error) {
559                         if (tcnp->cn_flags & CNP_PDIRUNLOCK)
560                                 vrele(tdvp);
561                         else
562                                 vput(tdvp);
563                         goto out;
564                 }
565
566                 /*
567                  * tdvp is locked at this point.  in the RENAME case tvp may
568                  * be NULL without an error, assign xp accordingly.  The
569                  * 'bad' target can be used again after this.
570                  */
571                 dp = VTOI(tdvp);
572                 if (tvp)
573                         xp = VTOI(tvp);
574         }
575         /*
576          * 2) If target doesn't exist, link the target
577          *    to the source and unlink the source.
578          *    Otherwise, rewrite the target directory
579          *    entry to reference the source inode and
580          *    expunge the original entry's existence.
581          *
582          * tdvp and tvp are cleaned up by this code.  tvp is only good if
583          * xp is not NULL.
584          */
585         if (xp == NULL) {
586                 if (dp->i_dev != ip->i_dev)
587                         panic("ext2_rename: EXDEV");
588                 /*
589                  * Account for ".." in new directory.
590                  * When source and destination have the same
591                  * parent we don't fool with the link count.
592                  */
593                 if (doingdirectory && newparent) {
594                         if ((nlink_t)dp->i_nlink >= LINK_MAX) {
595                                 error = EMLINK;
596                                 goto bad;
597                         }
598                         dp->i_nlink++;
599                         dp->i_flag |= IN_CHANGE;
600                         error = EXT2_UPDATE(tdvp, 1);
601                         if (error)
602                                 goto bad;
603                 }
604                 error = ext2_direnter(ip, tdvp, tcnp);
605                 if (error) {
606                         if (doingdirectory && newparent) {
607                                 dp->i_nlink--;
608                                 dp->i_flag |= IN_CHANGE;
609                                 EXT2_UPDATE(tdvp, 1);
610                         }
611                         goto bad;
612                 }
613
614                 /*
615                  * manual cleanup, we can't use the bad or out target after
616                  * this.
617                  */
618                 vput(tdvp);
619         } else {
620                 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
621                         panic("ext2_rename: EXDEV");
622                 /*
623                  * Short circuit rename(foo, foo).
624                  */
625                 if (xp->i_number == ip->i_number)
626                         panic("ext2_rename: same file");
627                 /*
628                  * If the parent directory is "sticky", then the user must
629                  * own the parent directory, or the destination of the rename,
630                  * otherwise the destination may not be changed (except by
631                  * root). This implements append-only directories.
632                  */
633                 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
634                     tcnp->cn_cred->cr_uid != dp->i_uid &&
635                     xp->i_uid != tcnp->cn_cred->cr_uid) {
636                         error = EPERM;
637                         goto bad;
638                 }
639                 /*
640                  * Target must be empty if a directory and have no links
641                  * to it. Also, ensure source and target are compatible
642                  * (both directories, or both not directories).
643                  */
644                 if ((xp->i_mode&IFMT) == IFDIR) {
645                         if (! ext2_dirempty(xp, dp->i_number, tcnp->cn_cred) || 
646                             xp->i_nlink > 2) {
647                                 error = ENOTEMPTY;
648                                 goto bad;
649                         }
650                         if (!doingdirectory) {
651                                 error = ENOTDIR;
652                                 goto bad;
653                         }
654                 } else if (doingdirectory) {
655                         error = EISDIR;
656                         goto bad;
657                 }
658                 error = ext2_dirrewrite(dp, ip, tcnp);
659                 if (error)
660                         goto bad;
661                 /*
662                  * If the target directory is in the same
663                  * directory as the source directory,
664                  * decrement the link count on the parent
665                  * of the target directory.
666                  */
667                  if (doingdirectory && !newparent) {
668                         dp->i_nlink--;
669                         dp->i_flag |= IN_CHANGE;
670                 }
671
672                 /*
673                  * manual cleanup, we can't use the bad or out target after
674                  * this.
675                  */
676                 vput(tdvp);
677
678                 /*
679                  * Adjust the link count of the target to
680                  * reflect the dirrewrite above.  If this is
681                  * a directory it is empty and there are
682                  * no links to it, so we can squash the inode and
683                  * any space associated with it.  We disallowed
684                  * renaming over top of a directory with links to
685                  * it above, as the remaining link would point to
686                  * a directory without "." or ".." entries.
687                  */
688                 xp->i_nlink--;
689                 if (doingdirectory) {
690                         if (--xp->i_nlink != 0)
691                                 panic("ext2_rename: linked directory");
692                         error = EXT2_TRUNCATE(tvp, (off_t)0, IO_SYNC,
693                                               tcnp->cn_cred);
694                 }
695                 xp->i_flag |= IN_CHANGE;
696                 vput(tvp);
697                 xp = NULL;
698         }
699
700         /*
701          * tvp and tdvp have been cleaned up.  The bad and out targets may
702          * not be used.  fvp and fdvp are ref'd but not locked.  ip
703          * still represents the old fvp and ip->i_flag may still have IN_RENAME
704          * set (if doingdirectory).
705          */
706
707         /*
708          * 3) Unlink the source.
709          *
710          * fdvp is locked and ref'd. ap->a_fvp holds the old lookup unlocked
711          * and ref'd, fvp will hold the new lookup locked and ref'd.
712          *
713          * After the relookup ap->a_fvp must be released as part of our
714          * cleanup, not just fdvp and fvp.  And, on success, fdvp and
715          * fvp will be locked so the bad and out targets cannot be used.
716          */
717         fcnp->cn_flags &= ~CNP_MODMASK;
718         fcnp->cn_flags |= CNP_LOCKPARENT;
719         KKASSERT(fcnp->cn_flags & CNP_PDIRUNLOCK);
720         error = relookup(fdvp, &fvp, fcnp);
721         if (error) {
722                 /*
723                  * From name has disappeared.
724                  */
725                 if (doingdirectory)
726                         panic("ext2_rename: lost dir entry");
727                 /* ip->i_flag only sets IN_RENAME if doingdirectory */
728                 vrele(ap->a_fvp);
729                 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
730                         vrele(fdvp);
731                 else
732                         vput(fdvp);
733                 return (0);
734         }
735         KKASSERT((fcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
736
737         /*
738          * This case shouldn't occur
739          */
740         if (fvp == NULL) {
741                 /*
742                  * From name has disappeared.
743                  */
744                 if (doingdirectory)
745                         panic("ext2_rename: lost dir entry");
746                 /* ip->i_flag only sets IN_RENAME if doingdirectory */
747                 vrele(ap->a_fvp);
748                 vput(fvp);
749                 vput(fdvp);
750                 return (0);
751         }
752
753         /*
754          * fvp and fdvp are both ref'd and locked.
755          */
756         xp = VTOI(fvp);
757         dp = VTOI(fdvp);
758
759         /*
760          * Ensure that the directory entry still exists and has not
761          * changed while the new name has been entered. If the source is
762          * a file then the entry may have been unlinked or renamed. In
763          * either case there is no further work to be done. If the source
764          * is a directory then it cannot have been rmdir'ed; its link
765          * count of three would cause a rmdir to fail with ENOTEMPTY.
766          * The IN_RENAME flag ensures that it cannot be moved by another
767          * rename.
768          */
769         if (xp != ip) {
770                 if (doingdirectory)
771                         panic("ext2_rename: lost dir entry");
772                 /* ip->i_flag only sets IN_RENAME if doingdirectory */
773         } else {
774                 /*
775                  * If the source is a directory with a
776                  * new parent, the link count of the old
777                  * parent directory must be decremented
778                  * and ".." set to point to the new parent.
779                  */
780                 if (doingdirectory && newparent) {
781                         dp->i_nlink--;
782                         dp->i_flag |= IN_CHANGE;
783                         error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
784                                         sizeof (struct dirtemplate), (off_t)0,
785                                         UIO_SYSSPACE, IO_NODELOCKED,
786                                         tcnp->cn_cred, NULL);
787                         if (error == 0) {
788                                 /* Like ext2 little-endian: */
789                                 namlen = dirbuf.dotdot_type;
790                                 if (namlen != 2 ||
791                                     dirbuf.dotdot_name[0] != '.' ||
792                                     dirbuf.dotdot_name[1] != '.') {
793                                         ext2_dirbad(xp, (doff_t)12,
794                                             "rename: mangled dir");
795                                 } else {
796                                         dirbuf.dotdot_ino = newparent;
797                                         vn_rdwr(UIO_WRITE, fvp,
798                                                 (caddr_t)&dirbuf,
799                                                 sizeof (struct dirtemplate),
800                                                 (off_t)0, UIO_SYSSPACE,
801                                                 IO_NODELOCKED|IO_SYNC,
802                                                 tcnp->cn_cred, NULL);
803                                 }
804                         }
805                 }
806                 error = ext2_dirremove(fdvp, fcnp);
807                 if (!error) {
808                         xp->i_nlink--;
809                         xp->i_flag |= IN_CHANGE;
810                 }
811                 xp->i_flag &= ~IN_RENAME;
812         }
813         vput(fdvp);
814         vput(fvp);
815         vrele(ap->a_fvp);
816         return (error);
817
818 bad:
819         if (xp)
820                 vput(ITOV(xp));
821         if (dp)
822                 vput(ITOV(dp));
823 out:
824         if (doingdirectory)
825                 ip->i_flag &= ~IN_RENAME;
826         if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
827                 ip->i_nlink--;
828                 ip->i_flag |= IN_CHANGE;
829                 ip->i_flag &= ~IN_RENAME;
830                 vput(fvp);
831         } else {
832                 vrele(fvp);
833         }
834         return (error);
835 }
836
837 /*
838  * Mkdir system call
839  *
840  * ext2_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
841  *            struct componentname *a_cnp, struct vattr *a_vap)
842  */
843 static int
844 ext2_mkdir(struct vop_old_mkdir_args *ap)
845 {
846         struct vnode *dvp = ap->a_dvp;
847         struct vattr *vap = ap->a_vap;
848         struct componentname *cnp = ap->a_cnp;
849         struct inode *ip, *dp;
850         struct vnode *tvp;
851         struct dirtemplate dirtemplate, *dtp;
852         int error, dmode;
853
854         dp = VTOI(dvp);
855         if ((nlink_t)dp->i_nlink >= LINK_MAX) {
856                 error = EMLINK;
857                 goto out;
858         }
859         dmode = vap->va_mode & 0777;
860         dmode |= IFDIR;
861         /*
862          * Must simulate part of ext2_makeinode here to acquire the inode,
863          * but not have it entered in the parent directory. The entry is
864          * made later after writing "." and ".." entries.
865          */
866         error = EXT2_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
867         if (error)
868                 goto out;
869         ip = VTOI(tvp);
870         ip->i_gid = dp->i_gid;
871 #ifdef SUIDDIR
872         {
873 #ifdef QUOTA
874                 struct ucred ucred, *ucp;
875                 ucp = cnp->cn_cred;
876 #endif
877                 /*
878                  * if we are hacking owners here, (only do this where told to)
879                  * and we are not giving it TOO root, (would subvert quotas)
880                  * then go ahead and give it to the other user.
881                  * The new directory also inherits the SUID bit. 
882                  * If user's UID and dir UID are the same,
883                  * 'give it away' so that the SUID is still forced on.
884                  */
885                 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
886                    (dp->i_mode & ISUID) && dp->i_uid) {
887                         dmode |= ISUID;
888                         ip->i_uid = dp->i_uid;
889 #ifdef QUOTA
890                         if (dp->i_uid != cnp->cn_cred->cr_uid) {
891                                 /*
892                                  * make sure the correct user gets charged
893                                  * for the space.
894                                  * Make a dummy credential for the victim.
895                                  * XXX This seems to never be accessed out of
896                                  * our context so a stack variable is ok.
897                                  */
898                                 ucred.cr_ref = 1;
899                                 ucred.cr_uid = ip->i_uid;
900                                 ucred.cr_ngroups = 1;
901                                 ucred.cr_groups[0] = dp->i_gid;
902                                 ucp = &ucred;
903                         }
904 #endif
905                 } else {
906                         ip->i_uid = cnp->cn_cred->cr_uid;
907                 }
908 #ifdef QUOTA
909                 if ((error = ext2_getinoquota(ip)) ||
910                 (error = ext2_chkiq(ip, 1, ucp, 0))) {
911                         EXT2_VFREE(tvp, ip->i_number, dmode);
912                         vput(tvp);
913                         return (error);
914                 }
915 #endif
916         }
917 #else
918         ip->i_uid = cnp->cn_cred->cr_uid;
919 #ifdef QUOTA
920         if ((error = ext2_getinoquota(ip)) ||
921             (error = ext2_chkiq(ip, 1, cnp->cn_cred, 0))) {
922                 EXT2_VFREE(tvp, ip->i_number, dmode);
923                 vput(tvp);
924                 return (error);
925         }
926 #endif
927 #endif
928         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
929         ip->i_mode = dmode;
930         tvp->v_type = VDIR;     /* Rest init'd in getnewvnode(). */
931         ip->i_nlink = 2;
932         if (cnp->cn_flags & CNP_ISWHITEOUT)
933                 ip->i_flags |= UF_OPAQUE;
934         error = EXT2_UPDATE(tvp, 1);
935
936         /*
937          * The vnode must have a VM object in order to issue buffer cache
938          * ops on it.
939          */
940         vinitvmio(tvp, 0);
941
942         /*
943          * Bump link count in parent directory
944          * to reflect work done below.  Should
945          * be done before reference is created
946          * so reparation is possible if we crash.
947          */
948         dp->i_nlink++;
949         dp->i_flag |= IN_CHANGE;
950         error = EXT2_UPDATE(dvp, 1);
951         if (error)
952                 goto bad;
953
954         /* Initialize directory with "." and ".." from static template. */
955         if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs->s_es,
956             EXT2_FEATURE_INCOMPAT_FILETYPE))
957                 dtp = &ext2_mastertemplate;
958         else
959                 dtp = &ext2_omastertemplate;
960         dirtemplate = *dtp;
961         dirtemplate.dot_ino = ip->i_number;
962         dirtemplate.dotdot_ino = dp->i_number;
963         /* note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE 
964          * so let's just redefine it - for this function only
965          */
966 #undef  DIRBLKSIZ 
967 #define DIRBLKSIZ  VTOI(dvp)->i_e2fs->s_blocksize
968         dirtemplate.dotdot_reclen = DIRBLKSIZ - 12;
969         error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
970                         sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
971                         IO_NODELOCKED|IO_SYNC, cnp->cn_cred, NULL);
972         if (error) {
973                 dp->i_nlink--;
974                 dp->i_flag |= IN_CHANGE;
975                 goto bad;
976         }
977         if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
978                 panic("ext2_mkdir: blksize"); /* XXX should grow with balloc() */
979         else {
980                 ip->i_size = DIRBLKSIZ;
981                 ip->i_flag |= IN_CHANGE;
982         }
983
984         /* Directory set up, now install its entry in the parent directory. */
985         error = ext2_direnter(ip, dvp, cnp);
986         if (error) {
987                 dp->i_nlink--;
988                 dp->i_flag |= IN_CHANGE;
989         }
990 bad:
991         /*
992          * No need to do an explicit VOP_TRUNCATE here, vrele will do this
993          * for us because we set the link count to 0.
994          */
995         if (error) {
996                 ip->i_nlink = 0;
997                 ip->i_flag |= IN_CHANGE;
998                 vput(tvp);
999         } else
1000                 *ap->a_vpp = tvp;
1001 out:
1002         return (error);
1003 #undef  DIRBLKSIZ
1004 #define DIRBLKSIZ  DEV_BSIZE
1005 }
1006
1007 /*
1008  * Rmdir system call.
1009  *
1010  * ext2_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1011  *            struct componentname *a_cnp)
1012  */
1013 static int
1014 ext2_rmdir(struct vop_old_rmdir_args *ap)
1015 {
1016         struct vnode *vp = ap->a_vp;
1017         struct vnode *dvp = ap->a_dvp;
1018         struct componentname *cnp = ap->a_cnp;
1019         struct inode *ip, *dp;
1020         int error;
1021
1022         ip = VTOI(vp);
1023         dp = VTOI(dvp);
1024
1025         /*
1026          * Verify the directory is empty (and valid).
1027          * (Rmdir ".." won't be valid since
1028          *  ".." will contain a reference to
1029          *  the current directory and thus be
1030          *  non-empty.)
1031          */
1032         error = 0;
1033         if (ip->i_nlink != 2 || !ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1034                 error = ENOTEMPTY;
1035                 goto out;
1036         }
1037         if ((dp->i_flags & APPEND)
1038             || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1039                 error = EPERM;
1040                 goto out;
1041         }
1042         /*
1043          * Delete reference to directory before purging
1044          * inode.  If we crash in between, the directory
1045          * will be reattached to lost+found,
1046          */
1047         error = ext2_dirremove(dvp, cnp);
1048         if (error)
1049                 goto out;
1050         dp->i_nlink--;
1051         dp->i_flag |= IN_CHANGE;
1052         vn_unlock(dvp);
1053         /*
1054          * Truncate inode.  The only stuff left
1055          * in the directory is "." and "..".  The
1056          * "." reference is inconsequential since
1057          * we're quashing it.  The ".." reference
1058          * has already been adjusted above.  We've
1059          * removed the "." reference and the reference
1060          * in the parent directory, but there may be
1061          * other hard links so decrement by 2 and
1062          * worry about them later.
1063          */
1064         ip->i_nlink -= 2;
1065         error = EXT2_TRUNCATE(vp, (off_t)0, IO_SYNC, cnp->cn_cred);
1066         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1067 out:
1068         return (error);
1069 }
1070
1071 /*
1072  * symlink -- make a symbolic link
1073  *
1074  * ext2_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1075  *              struct componentname *a_cnp, struct vattr *a_vap,
1076  *              char *a_target)
1077  */
1078 static int
1079 ext2_symlink(struct vop_old_symlink_args *ap)
1080 {
1081         struct vnode *vp, **vpp = ap->a_vpp;
1082         struct inode *ip;
1083         int len, error;
1084
1085         error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1086             vpp, ap->a_cnp);
1087         if (error)
1088                 return (error);
1089         vp = *vpp;
1090         len = strlen(ap->a_target);
1091         if (len < vp->v_mount->mnt_maxsymlinklen) {
1092                 ip = VTOI(vp);
1093                 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1094                 ip->i_size = len;
1095                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1096         } else {
1097                 /*
1098                  * Make sure we have a VM object in order to use
1099                  * the buffer cache.
1100                  */
1101                 if (vp->v_object == NULL)
1102                         vinitvmio(vp, 0);
1103
1104                 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1105                                 UIO_SYSSPACE, IO_NODELOCKED, 
1106                                 ap->a_cnp->cn_cred, NULL);
1107
1108                 if (error)
1109                         vput(vp);
1110         }
1111
1112         return (error);
1113 }
1114
1115 /*
1116  * Allocate a new inode.
1117  */
1118 static int
1119 ext2_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
1120                struct componentname *cnp)
1121 {
1122         struct inode *ip, *pdir;
1123         struct vnode *tvp;
1124         int error;
1125
1126         pdir = VTOI(dvp);
1127         *vpp = NULL;
1128         if ((mode & IFMT) == 0)
1129                 mode |= IFREG;
1130
1131         error = EXT2_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
1132         if (error) {
1133                 return (error);
1134         }
1135         ip = VTOI(tvp);
1136         ip->i_gid = pdir->i_gid;
1137 #ifdef SUIDDIR
1138         {
1139 #ifdef QUOTA
1140                 struct ucred ucred, *ucp;
1141                 ucp = cnp->cn_cred;
1142 #endif
1143                 /*
1144                  * if we are
1145                  * not the owner of the directory,
1146                  * and we are hacking owners here, (only do this where told to)
1147                  * and we are not giving it TOO root, (would subvert quotas)
1148                  * then go ahead and give it to the other user.
1149                  * Note that this drops off the execute bits for security.
1150                  */
1151                 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1152                      (pdir->i_mode & ISUID) &&
1153                      (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1154                         ip->i_uid = pdir->i_uid;
1155                         mode &= ~07111;
1156 #ifdef QUOTA
1157                         /*
1158                          * make sure the correct user gets charged
1159                          * for the space.
1160                          * Quickly knock up a dummy credential for the victim.
1161                          * XXX This seems to never be accessed out of our
1162                          * context so a stack variable is ok.
1163                          */
1164                         ucred.cr_ref = 1;
1165                         ucred.cr_uid = ip->i_uid;
1166                         ucred.cr_ngroups = 1;
1167                         ucred.cr_groups[0] = pdir->i_gid;
1168                         ucp = &ucred;
1169 #endif
1170                 } else {
1171                         ip->i_uid = cnp->cn_cred->cr_uid;
1172                 }
1173         
1174 #ifdef QUOTA
1175                 if ((error = ext2_getinoquota(ip)) ||
1176                 (error = ext2_chkiq(ip, 1, ucp, 0))) {
1177                         EXT2_VFREE(tvp, ip->i_number, mode);
1178                         vput(tvp);
1179                         return (error);
1180                 }
1181 #endif
1182         }
1183 #else
1184         ip->i_uid = cnp->cn_cred->cr_uid;
1185 #ifdef QUOTA
1186         if ((error = ext2_getinoquota(ip)) ||
1187             (error = ext2_chkiq(ip, 1, cnp->cn_cred, 0))) {
1188                 EXT2_VFREE(tvp, ip->i_number, mode);
1189                 vput(tvp);
1190                 return (error);
1191         }
1192 #endif
1193 #endif
1194         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1195         ip->i_mode = mode;
1196         tvp->v_type = IFTOVT(mode);     /* Rest init'd in getnewvnode(). */
1197         ip->i_nlink = 1;
1198         if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
1199             priv_check_cred(cnp->cn_cred, PRIV_VFS_SETGID, 0))
1200                 ip->i_mode &= ~ISGID;
1201
1202         if (cnp->cn_flags & CNP_ISWHITEOUT)
1203                 ip->i_flags |= UF_OPAQUE;
1204
1205         /*
1206          * Regular files and directories need VM objects.  Softlinks do 
1207          * not (not immediately anyway).
1208          */
1209         if (tvp->v_type == VREG || tvp->v_type == VDIR)
1210                 vinitvmio(tvp, 0);
1211
1212         /*
1213          * Make sure inode goes to disk before directory entry.
1214          */
1215         error = EXT2_UPDATE(tvp, 1);
1216         if (error)
1217                 goto bad;
1218         error = ext2_direnter(ip, dvp, cnp);
1219         if (error)
1220                 goto bad;
1221
1222         *vpp = tvp;
1223         return (0);
1224
1225 bad:
1226         /*
1227          * Write error occurred trying to update the inode
1228          * or the directory so must deallocate the inode.
1229          */
1230         ip->i_nlink = 0;
1231         ip->i_flag |= IN_CHANGE;
1232         vput(tvp);
1233         return (error);
1234 }
1235
1236 /*
1237  * get page routine
1238  *
1239  * XXX By default, wimp out... note that a_offset is ignored (and always
1240  * XXX has been).
1241  */
1242 static int
1243 ext2_getpages(struct vop_getpages_args *ap)
1244 {
1245         int error;
1246
1247         error = vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
1248                                              ap->a_reqpage, ap->a_seqaccess);
1249         return(error);
1250 }
1251
1252 void
1253 ext2_itimes(struct vnode *vp)
1254 {
1255         struct inode *ip;
1256         struct timespec ts;
1257
1258         ip = VTOI(vp);
1259         if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
1260                 return;
1261         if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
1262                 ip->i_flag |= IN_LAZYMOD;
1263         else
1264                 ip->i_flag |= IN_MODIFIED;
1265         if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1266                 vfs_timestamp(&ts);
1267                 if (ip->i_flag & IN_ACCESS) {
1268                         ip->i_atime = ts.tv_sec;
1269                         ip->i_atimensec = ts.tv_nsec;
1270                 }
1271                 if (ip->i_flag & IN_UPDATE) {
1272                         ip->i_mtime = ts.tv_sec;
1273                         ip->i_mtimensec = ts.tv_nsec;
1274                         ip->i_modrev++;
1275                 }
1276                 if (ip->i_flag & IN_CHANGE) {
1277                         ip->i_ctime = ts.tv_sec;
1278                         ip->i_ctimensec = ts.tv_nsec;
1279                 }
1280         }
1281         ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
1282 }
1283
1284 /*
1285  * Open called.
1286  *
1287  * Nothing to do.
1288  *
1289  * ext2_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
1290  *           struct file *a_fp)
1291  */
1292 /* ARGSUSED */
1293 static
1294 int
1295 ext2_open(struct vop_open_args *ap)
1296 {
1297         struct vnode *vp = ap->a_vp;
1298
1299         /*
1300          * Files marked append-only must be opened for appending.
1301          */
1302         if ((VTOI(vp)->i_flags & APPEND) &&
1303             (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) {
1304                 return (EPERM);
1305         }
1306         return (vop_stdopen(ap));
1307 }
1308
1309 /*
1310  * Close called.
1311  *
1312  * Update the times on the inode.
1313  *
1314  * ext2_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred)
1315  */
1316 /* ARGSUSED */
1317 static
1318 int
1319 ext2_close(struct vop_close_args *ap)
1320 {
1321         struct vnode *vp = ap->a_vp;
1322
1323         if (vp->v_sysref.refcnt > 1)
1324                 ext2_itimes(vp);
1325         return (vop_stdclose(ap));
1326 }
1327
1328 /*
1329  * ext2_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
1330  */
1331 static
1332 int
1333 ext2_access(struct vop_access_args *ap)
1334 {
1335         struct vnode *vp = ap->a_vp;
1336         struct inode *ip = VTOI(vp);
1337         int error;
1338
1339 #ifdef QUOTA
1340         if (ap->a_mode & VWRITE) {
1341                 switch (vp->v_type) {
1342                 case VDIR:
1343                 case VLNK:
1344                 case VREG:
1345                         if ((error = ext2_getinoquota(ip)) != 0)
1346                                 return (error);
1347                         break;
1348                 default:
1349                         break;
1350                 }
1351         }
1352 #endif
1353
1354         error = vop_helper_access(ap, ip->i_uid, ip->i_gid, ip->i_mode,
1355                                   ip->i_flags);
1356         return (error);
1357 }
1358
1359 /*
1360  * ext2_getattr(struct vnode *a_vp, struct vattr *a_vap)
1361  */
1362 /* ARGSUSED */
1363 static
1364 int
1365 ext2_getattr(struct vop_getattr_args *ap)
1366 {
1367         struct vnode *vp = ap->a_vp;
1368         struct inode *ip = VTOI(vp);
1369         struct vattr *vap = ap->a_vap;
1370
1371         ext2_itimes(vp);
1372         /*
1373          * Copy from inode table
1374          */
1375         vap->va_fsid = dev2udev(ip->i_dev);
1376         vap->va_fileid = ip->i_number;
1377         vap->va_mode = ip->i_mode & ~IFMT;
1378         vap->va_nlink = VFSTOEXT2(vp->v_mount)->um_i_effnlink_valid ?
1379             ip->i_effnlink : ip->i_nlink;
1380         vap->va_uid = ip->i_uid;
1381         vap->va_gid = ip->i_gid;
1382         vap->va_rmajor = umajor(ip->i_rdev);
1383         vap->va_rminor = uminor(ip->i_rdev);
1384         vap->va_size = ip->i_din.di_size;
1385         vap->va_atime.tv_sec = ip->i_atime;
1386         vap->va_atime.tv_nsec = ip->i_atimensec;
1387         vap->va_mtime.tv_sec = ip->i_mtime;
1388         vap->va_mtime.tv_nsec = ip->i_mtimensec;
1389         vap->va_ctime.tv_sec = ip->i_ctime;
1390         vap->va_ctime.tv_nsec = ip->i_ctimensec;
1391         vap->va_flags = ip->i_flags;
1392         vap->va_gen = ip->i_gen;
1393         vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
1394         vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
1395         vap->va_type = IFTOVT(ip->i_mode);
1396         vap->va_filerev = ip->i_modrev;
1397         return (0);
1398 }
1399
1400 /*
1401  * Set attribute vnode op. called from several syscalls
1402  *
1403  * ext2_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
1404  */
1405 static
1406 int
1407 ext2_setattr(struct vop_setattr_args *ap)
1408 {
1409         struct vattr *vap = ap->a_vap;
1410         struct vnode *vp = ap->a_vp;
1411         struct inode *ip = VTOI(vp);
1412         struct ucred *cred = ap->a_cred;
1413         int error;
1414
1415         /*
1416          * Check for unsettable attributes.
1417          */
1418         if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
1419             (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
1420             (vap->va_blocksize != VNOVAL) || (vap->va_rmajor != VNOVAL) ||
1421             ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
1422                 return (EINVAL);
1423         }
1424         if (vap->va_flags != VNOVAL) {
1425                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1426                         return (EROFS);
1427                 if (cred->cr_uid != ip->i_uid &&
1428                     (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)))
1429                         return (error);
1430                 /*
1431                  * Note that a root chflags becomes a user chflags when
1432                  * we are jailed, unless the jail.chflags_allowed sysctl
1433                  * is set.
1434                  */
1435                 if (cred->cr_uid == 0 && 
1436                     (!jailed(cred) || jail_chflags_allowed)) {
1437                         if ((ip->i_flags
1438                             & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) &&
1439                             securelevel > 0)
1440                                 return (EPERM);
1441                         ip->i_flags = vap->va_flags;
1442                 } else {
1443                         if (ip->i_flags
1444                             & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
1445                             (vap->va_flags & UF_SETTABLE) != vap->va_flags)
1446                                 return (EPERM);
1447                         ip->i_flags &= SF_SETTABLE;
1448                         ip->i_flags |= (vap->va_flags & UF_SETTABLE);
1449                 }
1450                 ip->i_flag |= IN_CHANGE;
1451                 if (vap->va_flags & (IMMUTABLE | APPEND))
1452                         return (0);
1453         }
1454         if (ip->i_flags & (IMMUTABLE | APPEND))
1455                 return (EPERM);
1456         /*
1457          * Go through the fields and update iff not VNOVAL.
1458          */
1459         if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
1460                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1461                         return (EROFS);
1462                 if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred)) != 0)
1463                         return (error);
1464         }
1465         if (vap->va_size != VNOVAL) {
1466                 /*
1467                  * Disallow write attempts on read-only filesystems;
1468                  * unless the file is a socket, fifo, or a block or
1469                  * character device resident on the filesystem.
1470                  */
1471                 switch (vp->v_type) {
1472                 case VDIR:
1473                         return (EISDIR);
1474                 case VLNK:
1475                 case VREG:
1476                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
1477                                 return (EROFS);
1478                         break;
1479                 default:
1480                         break;
1481                 }
1482                 if ((error = EXT2_TRUNCATE(vp, vap->va_size, 0, cred)) != 0)
1483                         return (error);
1484         }
1485         ip = VTOI(vp);
1486         if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1487                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1488                         return (EROFS);
1489                 if (cred->cr_uid != ip->i_uid &&
1490                     (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)) &&
1491                     ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
1492                     (error = VOP_EACCESS(vp, VWRITE, cred))))
1493                         return (error);
1494                 if (vap->va_atime.tv_sec != VNOVAL)
1495                         ip->i_flag |= IN_ACCESS;
1496                 if (vap->va_mtime.tv_sec != VNOVAL)
1497                         ip->i_flag |= IN_CHANGE | IN_UPDATE;
1498                 ext2_itimes(vp);
1499                 if (vap->va_atime.tv_sec != VNOVAL) {
1500                         ip->i_atime = vap->va_atime.tv_sec;
1501                         ip->i_atimensec = vap->va_atime.tv_nsec;
1502                 }
1503                 if (vap->va_mtime.tv_sec != VNOVAL) {
1504                         ip->i_mtime = vap->va_mtime.tv_sec;
1505                         ip->i_mtimensec = vap->va_mtime.tv_nsec;
1506                 }
1507                 error = EXT2_UPDATE(vp, 0);
1508                 if (error)
1509                         return (error);
1510         }
1511         error = 0;
1512         if (vap->va_mode != (mode_t)VNOVAL) {
1513                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1514                         return (EROFS);
1515                 error = ext2_chmod(vp, (int)vap->va_mode, cred);
1516         }
1517         VN_KNOTE(vp, NOTE_ATTRIB);
1518         return (error);
1519 }
1520
1521 /*
1522  * Change the mode on a file.
1523  * Inode must be locked before calling.
1524  */
1525 static int
1526 ext2_chmod(struct vnode *vp, int mode, struct ucred *cred)
1527 {
1528         struct inode *ip = VTOI(vp);
1529         int error;
1530
1531         if (cred->cr_uid != ip->i_uid) {
1532             error = priv_check_cred(cred, PRIV_VFS_CHMOD, 0);
1533             if (error)
1534                 return (error);
1535         }
1536         if (cred->cr_uid) {
1537                 if (vp->v_type != VDIR && (mode & S_ISTXT))
1538                         return (EFTYPE);
1539                 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
1540                         return (EPERM);
1541         }
1542         ip->i_mode &= ~ALLPERMS;
1543         ip->i_mode |= (mode & ALLPERMS);
1544         ip->i_flag |= IN_CHANGE;
1545         return (0);
1546 }
1547
1548 /*
1549  * Perform chown operation on inode ip;
1550  * inode must be locked prior to call.
1551  */
1552 static int
1553 ext2_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred)
1554 {
1555         struct inode *ip = VTOI(vp);
1556         uid_t ouid;
1557         gid_t ogid;
1558         int error = 0;
1559 #ifdef QUOTA
1560         int i;
1561         long change;
1562 #endif
1563
1564         if (uid == (uid_t)VNOVAL)
1565                 uid = ip->i_uid;
1566         if (gid == (gid_t)VNOVAL)
1567                 gid = ip->i_gid;
1568         /*
1569          * If we don't own the file, are trying to change the owner
1570          * of the file, or are not a member of the target group,
1571          * the caller must be superuser or the call fails.
1572          */
1573         if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
1574             (gid != ip->i_gid && !(cred->cr_gid == gid ||
1575             groupmember((gid_t)gid, cred)))) &&
1576             (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
1577                 return (error);
1578         ogid = ip->i_gid;
1579         ouid = ip->i_uid;
1580 #ifdef QUOTA
1581         if ((error = ext2_getinoquota(ip)) != 0)
1582                 return (error);
1583         if (ouid == uid) {
1584                 ext2_dqrele(vp, ip->i_dquot[USRQUOTA]);
1585                 ip->i_dquot[USRQUOTA] = NODQUOT;
1586         }
1587         if (ogid == gid) {
1588                 ext2_dqrele(vp, ip->i_dquot[GRPQUOTA]);
1589                 ip->i_dquot[GRPQUOTA] = NODQUOT;
1590         }
1591         change = ip->i_blocks;
1592         (void) ext2_chkdq(ip, -change, cred, CHOWN);
1593         (void) ext2_chkiq(ip, -1, cred, CHOWN);
1594         for (i = 0; i < MAXQUOTAS; i++) {
1595                 ext2_dqrele(vp, ip->i_dquot[i]);
1596                 ip->i_dquot[i] = NODQUOT;
1597         }
1598 #endif
1599         ip->i_gid = gid;
1600         ip->i_uid = uid;
1601 #ifdef QUOTA
1602         if ((error = ext2_getinoquota(ip)) == 0) {
1603                 if (ouid == uid) {
1604                         ext2_dqrele(vp, ip->i_dquot[USRQUOTA]);
1605                         ip->i_dquot[USRQUOTA] = NODQUOT;
1606                 }
1607                 if (ogid == gid) {
1608                         ext2_dqrele(vp, ip->i_dquot[GRPQUOTA]);
1609                         ip->i_dquot[GRPQUOTA] = NODQUOT;
1610                 }
1611                 if ((error = ext2_chkdq(ip, change, cred, CHOWN)) == 0) {
1612                         if ((error = ext2_chkiq(ip, 1, cred, CHOWN)) == 0)
1613                                 goto good;
1614                         else
1615                                 (void)ext2_chkdq(ip, -change, cred, CHOWN|FORCE);
1616                 }
1617                 for (i = 0; i < MAXQUOTAS; i++) {
1618                         ext2_dqrele(vp, ip->i_dquot[i]);
1619                         ip->i_dquot[i] = NODQUOT;
1620                 }
1621         }
1622         ip->i_gid = ogid;
1623         ip->i_uid = ouid;
1624         if (ext2_getinoquota(ip) == 0) {
1625                 if (ouid == uid) {
1626                         ext2_dqrele(vp, ip->i_dquot[USRQUOTA]);
1627                         ip->i_dquot[USRQUOTA] = NODQUOT;
1628                 }
1629                 if (ogid == gid) {
1630                         ext2_dqrele(vp, ip->i_dquot[GRPQUOTA]);
1631                         ip->i_dquot[GRPQUOTA] = NODQUOT;
1632                 }
1633                 (void) ext2_chkdq(ip, change, cred, FORCE|CHOWN);
1634                 (void) ext2_chkiq(ip, 1, cred, FORCE|CHOWN);
1635                 (void) ext2_getinoquota(ip);
1636         }
1637         return (error);
1638 good:
1639         if (ext2_getinoquota(ip))
1640                 panic("ext2_chown: lost quota");
1641 #endif /* QUOTA */
1642         ip->i_flag |= IN_CHANGE;
1643         if (cred->cr_uid != 0 && (ouid != uid || ogid != gid))
1644                 ip->i_mode &= ~(ISUID | ISGID);
1645         return (0);
1646 }
1647
1648 /*
1649  * Mmap a file
1650  *
1651  * NB Currently unsupported.
1652  *
1653  * ext2_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
1654  */
1655 /* ARGSUSED */
1656 static
1657 int
1658 ext2_mmap(struct vop_mmap_args *ap)
1659 {
1660         return (EINVAL);
1661 }
1662
1663 /*
1664  * whiteout vnode call
1665  *
1666  * ext2_whiteout(struct vnode *a_dvp, struct componentname *a_cnp, int a_flags)
1667  */
1668 static
1669 int
1670 ext2_whiteout(struct vop_old_whiteout_args *ap)
1671 {
1672         return (EOPNOTSUPP);
1673 }
1674
1675 /*
1676  * Return target name of a symbolic link
1677  *
1678  * ext2_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1679  */
1680 static
1681 int
1682 ext2_readlink(struct vop_readlink_args *ap)
1683 {
1684         struct vnode *vp = ap->a_vp;
1685         struct inode *ip = VTOI(vp);
1686         int isize;
1687
1688         isize = ip->i_size;
1689         if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1690             (ip->i_din.di_blocks == 0)) {   /* XXX - for old fastlink support */
1691                 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1692                 return (0);
1693         }
1694         return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1695 }
1696
1697 /*
1698  * Calculate the logical to physical mapping if not done already,
1699  * then call the device strategy routine.
1700  *
1701  * In order to be able to swap to a file, the VOP_BMAP operation may not
1702  * deadlock on memory.  See ext2_bmap() for details.
1703  *
1704  * ext2_strategy(struct vnode *a_vp, struct bio *a_bio)
1705  */
1706 static
1707 int
1708 ext2_strategy(struct vop_strategy_args *ap)
1709 {
1710         struct bio *bio = ap->a_bio;
1711         struct bio *nbio;
1712         struct buf *bp = bio->bio_buf;
1713         struct vnode *vp = ap->a_vp;
1714         struct inode *ip;
1715         int error;
1716
1717         ip = VTOI(vp);
1718         if (vp->v_type == VBLK || vp->v_type == VCHR)
1719                 panic("ext2_strategy: spec");
1720         nbio = push_bio(bio);
1721         if (nbio->bio_offset == NOOFFSET) {
1722                 error = VOP_BMAP(vp, bio->bio_offset, &nbio->bio_offset,
1723                                  NULL, NULL, bp->b_cmd);
1724                 if (error) {
1725                         bp->b_error = error;
1726                         bp->b_flags |= B_ERROR;
1727                         /* I/O was never started on nbio, must biodone(bio) */
1728                         biodone(bio);
1729                         return (error);
1730                 }
1731                 if (nbio->bio_offset == NOOFFSET)
1732                         vfs_bio_clrbuf(bp);
1733         }
1734         if (nbio->bio_offset == NOOFFSET) {
1735                 /* I/O was never started on nbio, must biodone(bio) */
1736                 biodone(bio);
1737                 return (0);
1738         }
1739         vn_strategy(ip->i_devvp, nbio);
1740         return (0);
1741 }
1742
1743 /*
1744  * Print out the contents of an inode.
1745  *
1746  * ext2_print(struct vnode *a_vp)
1747  */
1748 static
1749 int
1750 ext2_print(struct vop_print_args *ap)
1751 {
1752         struct vnode *vp = ap->a_vp;
1753         struct inode *ip = VTOI(vp);
1754
1755         kprintf("tag VT_EXT2FS, ino %lu, on dev %s (%d, %d)",
1756             (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev),
1757             minor(ip->i_dev));
1758         if (vp->v_type == VFIFO)
1759                 fifo_printinfo(vp);
1760         lockmgr_printinfo(&vp->v_lock);
1761         kprintf("\n");
1762         return (0);
1763 }
1764
1765 /*
1766  * Read wrapper for fifos.
1767  *
1768  * ext2fifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1769  *              struct ucred *a_cred)
1770  */
1771 static
1772 int
1773 ext2fifo_read(struct vop_read_args *ap)
1774 {
1775         int error, resid;
1776         struct inode *ip;
1777         struct uio *uio;
1778
1779         uio = ap->a_uio;
1780         resid = uio->uio_resid;
1781         error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1782         ip = VTOI(ap->a_vp);
1783         if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL &&
1784             (uio->uio_resid != resid || (error == 0 && resid != 0)))
1785                 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1786         return (error);
1787 }
1788
1789 /*
1790  * Write wrapper for fifos.
1791  *
1792  * ext2fifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1793  *               struct ucred *a_cred)
1794  */
1795 static
1796 int
1797 ext2fifo_write(struct vop_write_args *ap)
1798 {
1799         int error, resid;
1800         struct inode *ip;
1801         struct uio *uio;
1802
1803         uio = ap->a_uio;
1804         resid = uio->uio_resid;
1805         error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1806         ip = VTOI(ap->a_vp);
1807         if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1808                 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1809         return (error);
1810 }
1811
1812 /*
1813  * Close wrapper for fifos.
1814  *
1815  * Update the times on the inode then do device close.
1816  *
1817  * ext2fifo_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred)
1818  */
1819 static
1820 int
1821 ext2fifo_close(struct vop_close_args *ap)
1822 {
1823         struct vnode *vp = ap->a_vp;
1824
1825         if (vp->v_sysref.refcnt > 1)
1826                 ext2_itimes(vp);
1827         return (VOCALL(&fifo_vnode_vops, &ap->a_head));
1828 }
1829
1830 /*
1831  * Kqfilter wrapper for fifos.
1832  *
1833  * Fall through to ext2 kqfilter routines if needed 
1834  */
1835 static
1836 int
1837 ext2fifo_kqfilter(struct vop_kqfilter_args *ap)
1838 {
1839         int error;
1840
1841         error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1842         if (error)
1843                 error = ext2_kqfilter(ap);
1844         return (error);
1845 }
1846
1847 /*
1848  * Return POSIX pathconf information applicable to ext2 filesystems.
1849  *
1850  * ext2_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
1851  */
1852 static
1853 int
1854 ext2_pathconf(struct vop_pathconf_args *ap)
1855 {
1856         switch (ap->a_name) {
1857         case _PC_LINK_MAX:
1858                 *ap->a_retval = LINK_MAX;
1859                 return (0);
1860         case _PC_NAME_MAX:
1861                 *ap->a_retval = NAME_MAX;
1862                 return (0);
1863         case _PC_PATH_MAX:
1864                 *ap->a_retval = PATH_MAX;
1865                 return (0);
1866         case _PC_PIPE_BUF:
1867                 *ap->a_retval = PIPE_BUF;
1868                 return (0);
1869         case _PC_CHOWN_RESTRICTED:
1870                 *ap->a_retval = 1;
1871                 return (0);
1872         case _PC_NO_TRUNC:
1873                 *ap->a_retval = 1;
1874                 return (0);
1875         default:
1876                 return (EINVAL);
1877         }
1878         /* NOTREACHED */
1879 }
1880
1881 /*
1882  * Advisory record locking support
1883  *
1884  * ext2_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
1885  *             int a_flags)
1886  */
1887 static
1888 int
1889 ext2_advlock(struct vop_advlock_args *ap)
1890 {
1891         struct inode *ip = VTOI(ap->a_vp);
1892
1893         return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
1894 }
1895
1896 /*
1897  * Initialize the vnode associated with a new inode, handle aliased
1898  * vnodes.
1899  */
1900 int
1901 ext2_vinit(struct mount *mntp, struct vnode **vpp)
1902 {
1903         struct inode *ip;
1904         struct vnode *vp;
1905         struct timeval tv;
1906
1907         vp = *vpp;
1908         ip = VTOI(vp);
1909
1910         switch(vp->v_type = IFTOVT(ip->i_mode)) {
1911         case VCHR:
1912         case VBLK:
1913                 vp->v_ops = &mntp->mnt_vn_spec_ops;
1914                 addaliasu(vp, umajor(ip->i_rdev), uminor(ip->i_rdev));
1915                 break;
1916         case VFIFO:
1917                 vp->v_ops = &mntp->mnt_vn_fifo_ops;
1918                 break;
1919         case VDIR:
1920         case VREG:
1921                 vinitvmio(vp, ip->i_size);
1922                 break;
1923         case VLNK:
1924                 if ((ip->i_size >= vp->v_mount->mnt_maxsymlinklen) &&
1925                     ip->i_din.di_blocks != 0
1926                 ) {
1927                         vinitvmio(vp, ip->i_size);
1928                 }
1929                 break;
1930         default:
1931                 break;
1932
1933         }
1934
1935         if (ip->i_number == ROOTINO)
1936                 vp->v_flag |= VROOT;
1937         /*
1938          * Initialize modrev times
1939          */
1940         getmicrouptime(&tv);
1941         SETHIGH(ip->i_modrev, tv.tv_sec);
1942         SETLOW(ip->i_modrev, tv.tv_usec * 4294);
1943         *vpp = vp;
1944         return (0);
1945 }
1946
1947 static struct filterops ext2read_filtops = 
1948         { 1, NULL, filt_ext2detach, filt_ext2read };
1949 static struct filterops ext2write_filtops = 
1950         { 1, NULL, filt_ext2detach, filt_ext2write };
1951 static struct filterops ext2vnode_filtops = 
1952         { 1, NULL, filt_ext2detach, filt_ext2vnode };
1953
1954 /*
1955  * ext2_kqfilter(struct vnode *a_vp, struct knote *a_kn)
1956  */
1957 static int
1958 ext2_kqfilter(struct vop_kqfilter_args *ap)
1959 {
1960         struct vnode *vp = ap->a_vp;
1961         struct knote *kn = ap->a_kn;
1962         lwkt_tokref vlock;
1963
1964         switch (kn->kn_filter) {
1965         case EVFILT_READ:
1966                 kn->kn_fop = &ext2read_filtops;
1967                 break;
1968         case EVFILT_WRITE:
1969                 kn->kn_fop = &ext2write_filtops;
1970                 break;
1971         case EVFILT_VNODE:
1972                 kn->kn_fop = &ext2vnode_filtops;
1973                 break;
1974         default:
1975                 return (1);
1976         }
1977
1978         kn->kn_hook = (caddr_t)vp;
1979
1980         lwkt_gettoken(&vlock, &vp->v_token);
1981         SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
1982         lwkt_reltoken(&vlock);
1983
1984         return (0);
1985 }
1986
1987 static void
1988 filt_ext2detach(struct knote *kn)
1989 {
1990         struct vnode *vp = (struct vnode *)kn->kn_hook;
1991         lwkt_tokref vlock;
1992
1993         lwkt_gettoken(&vlock, &vp->v_token);
1994         SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
1995             kn, knote, kn_selnext);
1996         lwkt_reltoken(&vlock);
1997 }
1998
1999 /*ARGSUSED*/
2000 static int
2001 filt_ext2read(struct knote *kn, long hint)
2002 {
2003         struct vnode *vp = (struct vnode *)kn->kn_hook;
2004         struct inode *ip = VTOI(vp);
2005
2006         /*
2007          * filesystem is gone, so set the EOF flag and schedule 
2008          * the knote for deletion.
2009          */
2010         if (hint == NOTE_REVOKE) {
2011                 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2012                 return (1);
2013         }
2014
2015         kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
2016         return (kn->kn_data != 0);
2017 }
2018
2019 /*ARGSUSED*/
2020 static int
2021 filt_ext2write(struct knote *kn, long hint)
2022 {
2023         /*
2024          * filesystem is gone, so set the EOF flag and schedule 
2025          * the knote for deletion.
2026          */
2027         if (hint == NOTE_REVOKE)
2028                 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2029
2030         kn->kn_data = 0;
2031         return (1);
2032 }
2033
2034 static int
2035 filt_ext2vnode(struct knote *kn, long hint)
2036 {
2037         if (kn->kn_sfflags & hint)
2038                 kn->kn_fflags |= hint;
2039         if (hint == NOTE_REVOKE) {
2040                 kn->kn_flags |= EV_EOF;
2041                 return (1);
2042         }
2043         return (kn->kn_fflags != 0);
2044 }
2045
2046 struct vop_ops ext2_vnode_vops = {
2047         .vop_default =          vop_defaultop,
2048         .vop_fsync =            ext2_fsync,
2049         .vop_read =             ext2_read,
2050         .vop_reallocblks =      ext2_reallocblks,
2051         .vop_write =            ext2_write,
2052         .vop_access =           ext2_access,
2053         .vop_advlock =          ext2_advlock,
2054         .vop_bmap =             ext2_bmap,
2055         .vop_old_lookup =       ext2_lookup,
2056         .vop_close =            ext2_close,
2057         .vop_old_create =       ext2_create,
2058         .vop_getattr =          ext2_getattr,
2059         .vop_inactive =         ext2_inactive,
2060         .vop_old_link =         ext2_link,
2061         .vop_old_mkdir =        ext2_mkdir,
2062         .vop_old_mknod =        ext2_mknod,
2063         .vop_mmap =             ext2_mmap,
2064         .vop_open =             ext2_open,
2065         .vop_pathconf =         ext2_pathconf,
2066         .vop_poll =             vop_stdpoll,
2067         .vop_kqfilter =         ext2_kqfilter,
2068         .vop_print =            ext2_print,
2069         .vop_readdir =          ext2_readdir,
2070         .vop_readlink =         ext2_readlink,
2071         .vop_reclaim =          ext2_reclaim,
2072         .vop_old_remove =       ext2_remove,
2073         .vop_old_rename =       ext2_rename,
2074         .vop_old_rmdir =        ext2_rmdir,
2075         .vop_setattr =          ext2_setattr,
2076         .vop_strategy =         ext2_strategy,
2077         .vop_old_symlink =      ext2_symlink,
2078         .vop_old_whiteout =     ext2_whiteout,
2079         .vop_getpages =         ext2_getpages,
2080         .vop_putpages =         vop_stdputpages
2081 };
2082
2083 struct vop_ops ext2_spec_vops = {
2084         .vop_default =          vop_defaultop,
2085         .vop_fsync =            ext2_fsync,
2086         .vop_access =           ext2_access,
2087         .vop_close =            ext2_close,
2088         .vop_getattr =          ext2_getattr,
2089         .vop_inactive =         ext2_inactive,
2090         .vop_print =            ext2_print,
2091         .vop_read =             vop_stdnoread,
2092         .vop_reclaim =          ext2_reclaim,
2093         .vop_setattr =          ext2_setattr,
2094         .vop_write =            vop_stdnowrite
2095 };
2096
2097 struct vop_ops ext2_fifo_vops = {
2098         .vop_default =          ext2_vnoperatefifo,
2099         .vop_fsync =            ext2_fsync,
2100         .vop_access =           ext2_access,
2101         .vop_close =            ext2fifo_close,
2102         .vop_getattr =          ext2_getattr,
2103         .vop_inactive =         ext2_inactive,
2104         .vop_kqfilter =         ext2fifo_kqfilter,
2105         .vop_print =            ext2_print,
2106         .vop_read =             ext2fifo_read,
2107         .vop_reclaim =          ext2_reclaim,
2108         .vop_setattr =          ext2_setattr,
2109         .vop_write =            ext2fifo_write
2110 };
2111
2112 VNODEOP_SET(ext2_vnode_vops);
2113 VNODEOP_SET(ext2_spec_vops);
2114 VNODEOP_SET(ext2_fifo_vops);
2115
2116 /*
2117  * ext2_vnoperate()
2118  */
2119 int
2120 ext2_vnoperate(struct vop_generic_args *ap)
2121 {
2122         return (VOCALL(&ext2_vnode_vops, ap));
2123 }
2124
2125 /*
2126  * ext2_vnoperatefifo()
2127  */
2128 int
2129 ext2_vnoperatefifo(struct vop_generic_args *ap)
2130 {
2131         return (VOCALL(&ext2_fifo_vops, ap));
2132 }
2133