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