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