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