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