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