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