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