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