]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/fs/ext2fs/ext2_vnops.c
MFC r271467, r271468:
[FreeBSD/stable/9.git] / sys / fs / 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  * 4. 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.7 (Berkeley) 2/3/94
41  *      @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
42  * $FreeBSD$
43  */
44
45 #include "opt_suiddir.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/fcntl.h>
51 #include <sys/filio.h>
52 #include <sys/stat.h>
53 #include <sys/bio.h>
54 #include <sys/buf.h>
55 #include <sys/endian.h>
56 #include <sys/priv.h>
57 #include <sys/rwlock.h>
58 #include <sys/mount.h>
59 #include <sys/unistd.h>
60 #include <sys/time.h>
61 #include <sys/vnode.h>
62 #include <sys/namei.h>
63 #include <sys/lockf.h>
64 #include <sys/event.h>
65 #include <sys/conf.h>
66 #include <sys/file.h>
67
68 #include <vm/vm.h>
69 #include <vm/vm_param.h>
70 #include <vm/vm_extern.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_pager.h>
74 #include <vm/vnode_pager.h>
75
76 #include "opt_directio.h"
77
78 #include <fs/fifofs/fifo.h>
79
80 #include <ufs/ufs/dir.h>
81
82 #include <fs/ext2fs/fs.h>
83 #include <fs/ext2fs/inode.h>
84 #include <fs/ext2fs/ext2_extern.h>
85 #include <fs/ext2fs/ext2fs.h>
86 #include <fs/ext2fs/ext2_dinode.h>
87 #include <fs/ext2fs/ext2_dir.h>
88 #include <fs/ext2fs/ext2_mount.h>
89
90 static int ext2_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
91 static void ext2_itimes_locked(struct vnode *);
92 static int ext4_ext_read(struct vop_read_args *);
93 static int ext2_ind_read(struct vop_read_args *);
94
95 static vop_access_t     ext2_access;
96 static int ext2_chmod(struct vnode *, int, struct ucred *, struct thread *);
97 static int ext2_chown(struct vnode *, uid_t, gid_t, struct ucred *,
98     struct thread *);
99 static vop_close_t      ext2_close;
100 static vop_create_t     ext2_create;
101 static vop_fsync_t      ext2_fsync;
102 static vop_getpages_t   ext2_getpages;
103 static vop_getattr_t    ext2_getattr;
104 static vop_ioctl_t      ext2_ioctl;
105 static vop_link_t       ext2_link;
106 static vop_mkdir_t      ext2_mkdir;
107 static vop_mknod_t      ext2_mknod;
108 static vop_open_t       ext2_open;
109 static vop_pathconf_t   ext2_pathconf;
110 static vop_print_t      ext2_print;
111 static vop_read_t       ext2_read;
112 static vop_readlink_t   ext2_readlink;
113 static vop_remove_t     ext2_remove;
114 static vop_rename_t     ext2_rename;
115 static vop_rmdir_t      ext2_rmdir;
116 static vop_setattr_t    ext2_setattr;
117 static vop_strategy_t   ext2_strategy;
118 static vop_symlink_t    ext2_symlink;
119 static vop_write_t      ext2_write;
120 static vop_vptofh_t     ext2_vptofh;
121 static vop_close_t      ext2fifo_close;
122 static vop_kqfilter_t   ext2fifo_kqfilter;
123
124 /* Global vfs data structures for ext2. */
125 struct vop_vector ext2_vnodeops = {
126         .vop_default =          &default_vnodeops,
127         .vop_access =           ext2_access,
128         .vop_bmap =             ext2_bmap,
129         .vop_cachedlookup =     ext2_lookup,
130         .vop_close =            ext2_close,
131         .vop_create =           ext2_create,
132         .vop_fsync =            ext2_fsync,
133         .vop_getpages =         ext2_getpages,
134         .vop_getattr =          ext2_getattr,
135         .vop_inactive =         ext2_inactive,
136         .vop_ioctl =            ext2_ioctl,
137         .vop_link =             ext2_link,
138         .vop_lookup =           vfs_cache_lookup,
139         .vop_mkdir =            ext2_mkdir,
140         .vop_mknod =            ext2_mknod,
141         .vop_open =             ext2_open,
142         .vop_pathconf =         ext2_pathconf,
143         .vop_poll =             vop_stdpoll,
144         .vop_print =            ext2_print,
145         .vop_read =             ext2_read,
146         .vop_readdir =          ext2_readdir,
147         .vop_readlink =         ext2_readlink,
148         .vop_reallocblks =      ext2_reallocblks,
149         .vop_reclaim =          ext2_reclaim,
150         .vop_remove =           ext2_remove,
151         .vop_rename =           ext2_rename,
152         .vop_rmdir =            ext2_rmdir,
153         .vop_setattr =          ext2_setattr,
154         .vop_strategy =         ext2_strategy,
155         .vop_symlink =          ext2_symlink,
156         .vop_write =            ext2_write,
157         .vop_vptofh =           ext2_vptofh,
158 };
159
160 struct vop_vector ext2_fifoops = {
161         .vop_default =          &fifo_specops,
162         .vop_access =           ext2_access,
163         .vop_close =            ext2fifo_close,
164         .vop_fsync =            ext2_fsync,
165         .vop_getattr =          ext2_getattr,
166         .vop_inactive =         ext2_inactive,
167         .vop_kqfilter =         ext2fifo_kqfilter,
168         .vop_print =            ext2_print,
169         .vop_read =             VOP_PANIC,
170         .vop_reclaim =          ext2_reclaim,
171         .vop_setattr =          ext2_setattr,
172         .vop_write =            VOP_PANIC,
173         .vop_vptofh =           ext2_vptofh,
174 };
175
176 /*
177  * A virgin directory (no blushing please).
178  * Note that the type and namlen fields are reversed relative to ext2.
179  * Also, we don't use `struct odirtemplate', since it would just cause
180  * endianness problems.
181  */
182 static struct dirtemplate mastertemplate = {
183         0, 12, 1, EXT2_FT_DIR, ".",
184         0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
185 };
186 static struct dirtemplate omastertemplate = {
187         0, 12, 1, EXT2_FT_UNKNOWN, ".",
188         0, DIRBLKSIZ - 12, 2, EXT2_FT_UNKNOWN, ".."
189 };
190
191 static void
192 ext2_itimes_locked(struct vnode *vp)
193 {
194         struct inode *ip;
195         struct timespec ts;
196
197         ASSERT_VI_LOCKED(vp, __func__); 
198
199         ip = VTOI(vp);
200         if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
201                 return;
202         if ((vp->v_type == VBLK || vp->v_type == VCHR))
203                 ip->i_flag |= IN_LAZYMOD;
204         else
205                 ip->i_flag |= IN_MODIFIED;
206         if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
207                 vfs_timestamp(&ts);
208                 if (ip->i_flag & IN_ACCESS) {
209                         ip->i_atime = ts.tv_sec;
210                         ip->i_atimensec = ts.tv_nsec;
211                 }
212                 if (ip->i_flag & IN_UPDATE) {
213                         ip->i_mtime = ts.tv_sec;
214                         ip->i_mtimensec = ts.tv_nsec;
215                         ip->i_modrev++;
216                 }
217                 if (ip->i_flag & IN_CHANGE) {
218                         ip->i_ctime = ts.tv_sec;
219                         ip->i_ctimensec = ts.tv_nsec;
220                 }
221         }
222         ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
223 }
224
225 void
226 ext2_itimes(struct vnode *vp)
227 {
228
229         VI_LOCK(vp);
230         ext2_itimes_locked(vp);
231         VI_UNLOCK(vp);
232 }
233
234 /*
235  * Create a regular file
236  */
237 static int
238 ext2_create(struct vop_create_args *ap)
239 {
240         int error;
241
242         error =
243             ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
244             ap->a_dvp, ap->a_vpp, ap->a_cnp);
245         if (error)
246                 return (error);
247         return (0);
248 }
249
250 static int
251 ext2_open(struct vop_open_args *ap)
252 {
253
254         if (ap->a_vp->v_type == VBLK || ap->a_vp->v_type == VCHR)
255                 return (EOPNOTSUPP);
256
257         /*
258          * Files marked append-only must be opened for appending.
259          */
260         if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
261             (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
262                 return (EPERM);
263
264         vnode_create_vobject(ap->a_vp, VTOI(ap->a_vp)->i_size, ap->a_td);
265
266         return (0);
267 }
268
269 /*
270  * Close called.
271  *
272  * Update the times on the inode.
273  */
274 static int
275 ext2_close(struct vop_close_args *ap)
276 {
277         struct vnode *vp = ap->a_vp;
278
279         VI_LOCK(vp);
280         if (vp->v_usecount > 1)
281                 ext2_itimes_locked(vp);
282         VI_UNLOCK(vp);
283         return (0);
284 }
285
286 static int
287 ext2_access(struct vop_access_args *ap)
288 {
289         struct vnode *vp = ap->a_vp;
290         struct inode *ip = VTOI(vp);
291         accmode_t accmode = ap->a_accmode;
292         int error;
293
294         if (vp->v_type == VBLK || vp->v_type == VCHR)
295                 return (EOPNOTSUPP);
296
297         /*
298          * Disallow write attempts on read-only file systems;
299          * unless the file is a socket, fifo, or a block or
300          * character device resident on the file system.
301          */
302         if (accmode & VWRITE) {
303                 switch (vp->v_type) {
304                 case VDIR:
305                 case VLNK:
306                 case VREG:
307                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
308                                 return (EROFS);
309                         break;
310                 default:
311                         break;
312                 }
313         }
314
315         /* If immutable bit set, nobody gets to write it. */
316         if ((accmode & VWRITE) && (ip->i_flags & (SF_IMMUTABLE | SF_SNAPSHOT)))
317                 return (EPERM);
318
319         error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
320             ap->a_accmode, ap->a_cred, NULL);
321         return (error);
322 }
323
324 static int
325 ext2_getattr(struct vop_getattr_args *ap)
326 {
327         struct vnode *vp = ap->a_vp;
328         struct inode *ip = VTOI(vp);
329         struct vattr *vap = ap->a_vap;
330
331         ext2_itimes(vp);
332         /*
333          * Copy from inode table
334          */
335         vap->va_fsid = dev2udev(ip->i_devvp->v_rdev);
336         vap->va_fileid = ip->i_number;
337         vap->va_mode = ip->i_mode & ~IFMT;
338         vap->va_nlink = ip->i_nlink;
339         vap->va_uid = ip->i_uid;
340         vap->va_gid = ip->i_gid;
341         vap->va_rdev = ip->i_rdev;
342         vap->va_size = ip->i_size;
343         vap->va_atime.tv_sec = ip->i_atime;
344         vap->va_atime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_atimensec : 0;
345         vap->va_mtime.tv_sec = ip->i_mtime;
346         vap->va_mtime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_mtimensec : 0;
347         vap->va_ctime.tv_sec = ip->i_ctime;
348         vap->va_ctime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_ctimensec : 0;
349         if E2DI_HAS_XTIME(ip) {
350                 vap->va_birthtime.tv_sec = ip->i_birthtime;
351                 vap->va_birthtime.tv_nsec = ip->i_birthnsec;
352         }
353         vap->va_flags = ip->i_flags;
354         vap->va_gen = ip->i_gen;
355         vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
356         vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
357         vap->va_type = IFTOVT(ip->i_mode);
358         vap->va_filerev = ip->i_modrev;
359         return (0);
360 }
361
362 /*
363  * Set attribute vnode op. called from several syscalls
364  */
365 static int
366 ext2_setattr(struct vop_setattr_args *ap)
367 {
368         struct vattr *vap = ap->a_vap;
369         struct vnode *vp = ap->a_vp;
370         struct inode *ip = VTOI(vp);
371         struct ucred *cred = ap->a_cred;
372         struct thread *td = curthread;
373         int error;
374
375         /*
376          * Check for unsettable attributes.
377          */
378         if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
379             (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
380             (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
381             ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
382                 return (EINVAL);
383         }
384         if (vap->va_flags != VNOVAL) {
385                 /* Disallow flags not supported by ext2fs. */
386                 if(vap->va_flags & ~(SF_APPEND | SF_IMMUTABLE | UF_NODUMP))
387                         return (EOPNOTSUPP);
388
389                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
390                         return (EROFS);
391                 /*
392                  * Callers may only modify the file flags on objects they
393                  * have VADMIN rights for.
394                  */
395                 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
396                         return (error);
397                 /*
398                  * Unprivileged processes and privileged processes in
399                  * jail() are not permitted to unset system flags, or
400                  * modify flags if any system flags are set.
401                  * Privileged non-jail processes may not modify system flags
402                  * if securelevel > 0 and any existing system flags are set.
403                  */
404                 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
405                         if (ip->i_flags &
406                             (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
407                                 error = securelevel_gt(cred, 0);
408                                 if (error)
409                                         return (error);
410                         }
411                         ip->i_flags = vap->va_flags;
412                 } else {
413                         if (ip->i_flags &
414                             (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
415                             (vap->va_flags & UF_SETTABLE) != vap->va_flags)
416                                 return (EPERM);
417                         ip->i_flags &= SF_SETTABLE;
418                         ip->i_flags |= (vap->va_flags & UF_SETTABLE);
419                 }
420                 ip->i_flag |= IN_CHANGE;
421                 if (ip->i_flags & (IMMUTABLE | APPEND))
422                         return (0);
423         }
424         if (ip->i_flags & (IMMUTABLE | APPEND))
425                 return (EPERM);
426         /*
427          * Go through the fields and update iff not VNOVAL.
428          */
429         if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
430                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
431                         return (EROFS);
432                 if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred,
433                     td)) != 0)
434                         return (error);
435         }
436         if (vap->va_size != VNOVAL) {
437                 /*
438                  * Disallow write attempts on read-only file systems;
439                  * unless the file is a socket, fifo, or a block or
440                  * character device resident on the file system.
441                  */
442                 switch (vp->v_type) {
443                 case VDIR:
444                         return (EISDIR);
445                 case VLNK:
446                 case VREG:
447                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
448                                 return (EROFS);
449                         break;
450                 default:
451                         break;
452                 }
453                 if ((error = ext2_truncate(vp, vap->va_size, 0, cred, td)) != 0)
454                         return (error);
455         }
456         if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
457                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
458                         return (EROFS);
459                 /*
460                  * From utimes(2):
461                  * If times is NULL, ... The caller must be the owner of
462                  * the file, have permission to write the file, or be the
463                  * super-user.
464                  * If times is non-NULL, ... The caller must be the owner of
465                  * the file or be the super-user.
466                  */
467                 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)) &&
468                     ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
469                     (error = VOP_ACCESS(vp, VWRITE, cred, td))))
470                         return (error);
471                 if (vap->va_atime.tv_sec != VNOVAL)
472                         ip->i_flag |= IN_ACCESS;
473                 if (vap->va_mtime.tv_sec != VNOVAL)
474                         ip->i_flag |= IN_CHANGE | IN_UPDATE;
475                 ext2_itimes(vp);
476                 if (vap->va_atime.tv_sec != VNOVAL) {
477                         ip->i_atime = vap->va_atime.tv_sec;
478                         ip->i_atimensec = vap->va_atime.tv_nsec;
479                 }
480                 if (vap->va_mtime.tv_sec != VNOVAL) {
481                         ip->i_mtime = vap->va_mtime.tv_sec;
482                         ip->i_mtimensec = vap->va_mtime.tv_nsec;
483                 }
484                 ip->i_birthtime = vap->va_birthtime.tv_sec;
485                 ip->i_birthnsec = vap->va_birthtime.tv_nsec;
486                 error = ext2_update(vp, 0);
487                 if (error)
488                         return (error);
489         }
490         error = 0;
491         if (vap->va_mode != (mode_t)VNOVAL) {
492                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
493                         return (EROFS);
494                 error = ext2_chmod(vp, (int)vap->va_mode, cred, td);
495         }
496         return (error);
497 }
498
499 /*
500  * Change the mode on a file.
501  * Inode must be locked before calling.
502  */
503 static int
504 ext2_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td)
505 {
506         struct inode *ip = VTOI(vp);
507         int error;
508
509         /*
510          * To modify the permissions on a file, must possess VADMIN
511          * for that file.
512          */
513         if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
514                 return (error);
515         /*
516          * Privileged processes may set the sticky bit on non-directories,
517          * as well as set the setgid bit on a file with a group that the
518          * process is not a member of.
519          */
520         if (vp->v_type != VDIR && (mode & S_ISTXT)) {
521                 error = priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0);
522                 if (error)
523                         return (EFTYPE);
524         }
525         if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
526                 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
527                 if (error)
528                         return (error);
529         }
530         ip->i_mode &= ~ALLPERMS;
531         ip->i_mode |= (mode & ALLPERMS);
532         ip->i_flag |= IN_CHANGE;
533         return (0);
534 }
535
536 /*
537  * Perform chown operation on inode ip;
538  * inode must be locked prior to call.
539  */
540 static int
541 ext2_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
542     struct thread *td)
543 {
544         struct inode *ip = VTOI(vp);
545         uid_t ouid;
546         gid_t ogid;
547         int error = 0;
548
549         if (uid == (uid_t)VNOVAL)
550                 uid = ip->i_uid;
551         if (gid == (gid_t)VNOVAL)
552                 gid = ip->i_gid;
553         /*
554          * To modify the ownership of a file, must possess VADMIN
555          * for that file.
556          */
557         if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
558                 return (error);
559         /*
560          * To change the owner of a file, or change the group of a file
561          * to a group of which we are not a member, the caller must
562          * have privilege.
563          */
564         if (uid != ip->i_uid || (gid != ip->i_gid &&
565             !groupmember(gid, cred))) {
566                 error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0);
567                 if (error)
568                         return (error);
569         }
570         ogid = ip->i_gid;
571         ouid = ip->i_uid;
572         ip->i_gid = gid;
573         ip->i_uid = uid;
574         ip->i_flag |= IN_CHANGE;
575         if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
576                 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0) != 0)
577                         ip->i_mode &= ~(ISUID | ISGID);
578         }
579         return (0);
580 }
581
582 /*
583  * Synch an open file.
584  */
585 /* ARGSUSED */
586 static int
587 ext2_fsync(struct vop_fsync_args *ap)
588 {
589         /*
590          * Flush all dirty buffers associated with a vnode.
591          */
592
593         vop_stdfsync(ap);
594
595         return (ext2_update(ap->a_vp, ap->a_waitfor == MNT_WAIT));
596 }
597
598 /*
599  * Mknod vnode call
600  */
601 /* ARGSUSED */
602 static int
603 ext2_mknod(struct vop_mknod_args *ap)
604 {
605         struct vattr *vap = ap->a_vap;
606         struct vnode **vpp = ap->a_vpp;
607         struct inode *ip;
608         ino_t ino;
609         int error;
610
611         error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
612             ap->a_dvp, vpp, ap->a_cnp);
613         if (error)
614                 return (error);
615         ip = VTOI(*vpp);
616         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
617         if (vap->va_rdev != VNOVAL) {
618                 /*
619                  * Want to be able to use this to make badblock
620                  * inodes, so don't truncate the dev number.
621                  */
622                 ip->i_rdev = vap->va_rdev;
623         }
624         /*
625          * Remove inode, then reload it through VFS_VGET so it is
626          * checked to see if it is an alias of an existing entry in
627          * the inode cache.      XXX I don't believe this is necessary now.
628          */
629         (*vpp)->v_type = VNON;
630         ino = ip->i_number;     /* Save this before vgone() invalidates ip. */
631         vgone(*vpp);
632         vput(*vpp);
633         error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
634         if (error) {
635                 *vpp = NULL;
636                 return (error);
637         }
638         return (0);
639 }
640
641 static int
642 ext2_remove(struct vop_remove_args *ap)
643 {
644         struct inode *ip;
645         struct vnode *vp = ap->a_vp;
646         struct vnode *dvp = ap->a_dvp;
647         int error;
648
649         ip = VTOI(vp);
650         if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
651             (VTOI(dvp)->i_flags & APPEND)) {
652                 error = EPERM;
653                 goto out;
654         }
655         error = ext2_dirremove(dvp, ap->a_cnp);
656         if (error == 0) {
657                 ip->i_nlink--;
658                 ip->i_flag |= IN_CHANGE;
659         }
660 out:
661         return (error);
662 }
663
664 /*
665  * link vnode call
666  */
667 static int
668 ext2_link(struct vop_link_args *ap)
669 {
670         struct vnode *vp = ap->a_vp;
671         struct vnode *tdvp = ap->a_tdvp;
672         struct componentname *cnp = ap->a_cnp;
673         struct inode *ip;
674         int error;
675
676 #ifdef INVARIANTS
677         if ((cnp->cn_flags & HASBUF) == 0)
678                 panic("ext2_link: no name");
679 #endif
680         if (tdvp->v_mount != vp->v_mount) {
681                 error = EXDEV;
682                 goto out;
683         }
684         ip = VTOI(vp);
685         if ((nlink_t)ip->i_nlink >= EXT2_LINK_MAX) {
686                 error = EMLINK;
687                 goto out;
688         }
689         if (ip->i_flags & (IMMUTABLE | APPEND)) {
690                 error = EPERM;
691                 goto out;
692         }
693         ip->i_nlink++;
694         ip->i_flag |= IN_CHANGE;
695         error = ext2_update(vp, !DOINGASYNC(vp));
696         if (!error)
697                 error = ext2_direnter(ip, tdvp, cnp);
698         if (error) {
699                 ip->i_nlink--;
700                 ip->i_flag |= IN_CHANGE;
701         }
702 out:
703         return (error);
704 }
705
706 /*
707  * Rename system call.
708  *      rename("foo", "bar");
709  * is essentially
710  *      unlink("bar");
711  *      link("foo", "bar");
712  *      unlink("foo");
713  * but ``atomically''.  Can't do full commit without saving state in the
714  * inode on disk which isn't feasible at this time.  Best we can do is
715  * always guarantee the target exists.
716  *
717  * Basic algorithm is:
718  *
719  * 1) Bump link count on source while we're linking it to the
720  *    target.  This also ensure the inode won't be deleted out
721  *    from underneath us while we work (it may be truncated by
722  *    a concurrent `trunc' or `open' for creation).
723  * 2) Link source to destination.  If destination already exists,
724  *    delete it first.
725  * 3) Unlink source reference to inode if still around. If a
726  *    directory was moved and the parent of the destination
727  *    is different from the source, patch the ".." entry in the
728  *    directory.
729  */
730 static int
731 ext2_rename(struct vop_rename_args *ap)
732 {
733         struct vnode *tvp = ap->a_tvp;
734         struct vnode *tdvp = ap->a_tdvp;
735         struct vnode *fvp = ap->a_fvp;
736         struct vnode *fdvp = ap->a_fdvp;
737         struct componentname *tcnp = ap->a_tcnp;
738         struct componentname *fcnp = ap->a_fcnp;
739         struct inode *ip, *xp, *dp;
740         struct dirtemplate dirbuf;
741         int doingdirectory = 0, oldparent = 0, newparent = 0;
742         int error = 0;
743         u_char namlen;
744
745 #ifdef INVARIANTS
746         if ((tcnp->cn_flags & HASBUF) == 0 ||
747             (fcnp->cn_flags & HASBUF) == 0)
748                 panic("ext2_rename: no name");
749 #endif
750         /*
751          * Check for cross-device rename.
752          */
753         if ((fvp->v_mount != tdvp->v_mount) ||
754             (tvp && (fvp->v_mount != tvp->v_mount))) {
755                 error = EXDEV;
756 abortit:
757                 if (tdvp == tvp)
758                         vrele(tdvp);
759                 else
760                         vput(tdvp);
761                 if (tvp)
762                         vput(tvp);
763                 vrele(fdvp);
764                 vrele(fvp);
765                 return (error);
766         }
767
768         if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
769             (VTOI(tdvp)->i_flags & APPEND))) {
770                 error = EPERM;
771                 goto abortit;
772         }
773
774         /*
775          * Renaming a file to itself has no effect.  The upper layers should
776          * not call us in that case.  Temporarily just warn if they do.
777          */
778         if (fvp == tvp) {
779                 printf("ext2_rename: fvp == tvp (can't happen)\n");
780                 error = 0;
781                 goto abortit;
782         }
783
784         if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
785                 goto abortit;
786         dp = VTOI(fdvp);
787         ip = VTOI(fvp);
788         if (ip->i_nlink >= EXT2_LINK_MAX) {
789                 VOP_UNLOCK(fvp, 0);
790                 error = EMLINK;
791                 goto abortit;
792         }
793         if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
794             || (dp->i_flags & APPEND)) {
795                 VOP_UNLOCK(fvp, 0);
796                 error = EPERM;
797                 goto abortit;
798         }
799         if ((ip->i_mode & IFMT) == IFDIR) {
800                 /*
801                  * Avoid ".", "..", and aliases of "." for obvious reasons.
802                  */
803                 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
804                     dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
805                     (ip->i_flag & IN_RENAME)) {
806                         VOP_UNLOCK(fvp, 0);
807                         error = EINVAL;
808                         goto abortit;
809                 }
810                 ip->i_flag |= IN_RENAME;
811                 oldparent = dp->i_number;
812                 doingdirectory++;
813         }
814         vrele(fdvp);
815
816         /*
817          * When the target exists, both the directory
818          * and target vnodes are returned locked.
819          */
820         dp = VTOI(tdvp);
821         xp = NULL;
822         if (tvp)
823                 xp = VTOI(tvp);
824
825         /*
826          * 1) Bump link count while we're moving stuff
827          *    around.  If we crash somewhere before
828          *    completing our work, the link count
829          *    may be wrong, but correctable.
830          */
831         ip->i_nlink++;
832         ip->i_flag |= IN_CHANGE;
833         if ((error = ext2_update(fvp, !DOINGASYNC(fvp))) != 0) {
834                 VOP_UNLOCK(fvp, 0);
835                 goto bad;
836         }
837
838         /*
839          * If ".." must be changed (ie the directory gets a new
840          * parent) then the source directory must not be in the
841          * directory hierarchy above the target, as this would
842          * orphan everything below the source directory. Also
843          * the user must have write permission in the source so
844          * as to be able to change "..". We must repeat the call
845          * to namei, as the parent directory is unlocked by the
846          * call to checkpath().
847          */
848         error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
849         VOP_UNLOCK(fvp, 0);
850         if (oldparent != dp->i_number)
851                 newparent = dp->i_number;
852         if (doingdirectory && newparent) {
853                 if (error)      /* write access check above */
854                         goto bad;
855                 if (xp != NULL)
856                         vput(tvp);
857                 error = ext2_checkpath(ip, dp, tcnp->cn_cred);
858                 if (error)
859                         goto out;
860                 VREF(tdvp);
861                 error = relookup(tdvp, &tvp, tcnp);
862                 if (error)
863                         goto out;
864                 vrele(tdvp);
865                 dp = VTOI(tdvp);
866                 xp = NULL;
867                 if (tvp)
868                         xp = VTOI(tvp);
869         }
870         /*
871          * 2) If target doesn't exist, link the target
872          *    to the source and unlink the source.
873          *    Otherwise, rewrite the target directory
874          *    entry to reference the source inode and
875          *    expunge the original entry's existence.
876          */
877         if (xp == NULL) {
878                 if (dp->i_devvp != ip->i_devvp)
879                         panic("ext2_rename: EXDEV");
880                 /*
881                  * Account for ".." in new directory.
882                  * When source and destination have the same
883                  * parent we don't fool with the link count.
884                  */
885                 if (doingdirectory && newparent) {
886                         if ((nlink_t)dp->i_nlink >= EXT2_LINK_MAX) {
887                                 error = EMLINK;
888                                 goto bad;
889                         }
890                         dp->i_nlink++;
891                         dp->i_flag |= IN_CHANGE;
892                         error = ext2_update(tdvp, !DOINGASYNC(tdvp));
893                         if (error)
894                                 goto bad;
895                 }
896                 error = ext2_direnter(ip, tdvp, tcnp);
897                 if (error) {
898                         if (doingdirectory && newparent) {
899                                 dp->i_nlink--;
900                                 dp->i_flag |= IN_CHANGE;
901                                 (void)ext2_update(tdvp, 1);
902                         }
903                         goto bad;
904                 }
905                 vput(tdvp);
906         } else {
907                 if (xp->i_devvp != dp->i_devvp || xp->i_devvp != ip->i_devvp)
908                        panic("ext2_rename: EXDEV");
909                 /*
910                  * Short circuit rename(foo, foo).
911                  */
912                 if (xp->i_number == ip->i_number)
913                         panic("ext2_rename: same file");
914                 /*
915                  * If the parent directory is "sticky", then the user must
916                  * own the parent directory, or the destination of the rename,
917                  * otherwise the destination may not be changed (except by
918                  * root). This implements append-only directories.
919                  */
920                 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
921                     tcnp->cn_cred->cr_uid != dp->i_uid &&
922                     xp->i_uid != tcnp->cn_cred->cr_uid) {
923                         error = EPERM;
924                         goto bad;
925                 }
926                 /*
927                  * Target must be empty if a directory and have no links
928                  * to it. Also, ensure source and target are compatible
929                  * (both directories, or both not directories).
930                  */
931                 if ((xp->i_mode&IFMT) == IFDIR) {
932                         if (! ext2_dirempty(xp, dp->i_number, tcnp->cn_cred) || 
933                             xp->i_nlink > 2) {
934                                 error = ENOTEMPTY;
935                                 goto bad;
936                         }
937                         if (!doingdirectory) {
938                                 error = ENOTDIR;
939                                 goto bad;
940                         }
941                         cache_purge(tdvp);
942                 } else if (doingdirectory) {
943                         error = EISDIR;
944                         goto bad;
945                 }
946                 error = ext2_dirrewrite(dp, ip, tcnp);
947                 if (error)
948                         goto bad;
949                 /*
950                  * If the target directory is in the same
951                  * directory as the source directory,
952                  * decrement the link count on the parent
953                  * of the target directory.
954                  */
955                 if (doingdirectory && !newparent) {
956                         dp->i_nlink--;
957                         dp->i_flag |= IN_CHANGE;
958                 }
959                 vput(tdvp);
960                 /*
961                  * Adjust the link count of the target to
962                  * reflect the dirrewrite above.  If this is
963                  * a directory it is empty and there are
964                  * no links to it, so we can squash the inode and
965                  * any space associated with it.  We disallowed
966                  * renaming over top of a directory with links to
967                  * it above, as the remaining link would point to
968                  * a directory without "." or ".." entries.
969                  */
970                 xp->i_nlink--;
971                 if (doingdirectory) {
972                         if (--xp->i_nlink != 0)
973                                 panic("ext2_rename: linked directory");
974                         error = ext2_truncate(tvp, (off_t)0, IO_SYNC,
975                             tcnp->cn_cred, tcnp->cn_thread);
976                 }
977                 xp->i_flag |= IN_CHANGE;
978                 vput(tvp);
979                 xp = NULL;
980         }
981
982         /*
983          * 3) Unlink the source.
984          */
985         fcnp->cn_flags &= ~MODMASK;
986         fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
987         VREF(fdvp);
988         error = relookup(fdvp, &fvp, fcnp);
989         if (error == 0)
990                 vrele(fdvp);
991         if (fvp != NULL) {
992                 xp = VTOI(fvp);
993                 dp = VTOI(fdvp);
994         } else {
995                 /*
996                  * From name has disappeared.
997                  */
998                 if (doingdirectory)
999                         panic("ext2_rename: lost dir entry");
1000                 vrele(ap->a_fvp);
1001                 return (0);
1002         }
1003         /*
1004          * Ensure that the directory entry still exists and has not
1005          * changed while the new name has been entered. If the source is
1006          * a file then the entry may have been unlinked or renamed. In
1007          * either case there is no further work to be done. If the source
1008          * is a directory then it cannot have been rmdir'ed; its link
1009          * count of three would cause a rmdir to fail with ENOTEMPTY.
1010          * The IN_RENAME flag ensures that it cannot be moved by another
1011          * rename.
1012          */
1013         if (xp != ip) {
1014                 if (doingdirectory)
1015                         panic("ext2_rename: lost dir entry");
1016         } else {
1017                 /*
1018                  * If the source is a directory with a
1019                  * new parent, the link count of the old
1020                  * parent directory must be decremented
1021                  * and ".." set to point to the new parent.
1022                  */
1023                 if (doingdirectory && newparent) {
1024                         dp->i_nlink--;
1025                         dp->i_flag |= IN_CHANGE;
1026                         error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
1027                                 sizeof(struct dirtemplate), (off_t)0,
1028                                 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1029                                 tcnp->cn_cred, NOCRED, NULL, NULL);
1030                         if (error == 0) {
1031                                 /* Like ufs little-endian: */
1032                                 namlen = dirbuf.dotdot_type;
1033                                 if (namlen != 2 ||
1034                                     dirbuf.dotdot_name[0] != '.' ||
1035                                     dirbuf.dotdot_name[1] != '.') {
1036                                         ext2_dirbad(xp, (doff_t)12,
1037                                             "rename: mangled dir");
1038                                 } else {
1039                                         dirbuf.dotdot_ino = newparent;
1040                                         (void) vn_rdwr(UIO_WRITE, fvp,
1041                                             (caddr_t)&dirbuf,
1042                                             sizeof(struct dirtemplate),
1043                                             (off_t)0, UIO_SYSSPACE,
1044                                             IO_NODELOCKED | IO_SYNC |
1045                                             IO_NOMACCHECK, tcnp->cn_cred,
1046                                             NOCRED, NULL, NULL);
1047                                         cache_purge(fdvp);
1048                                 }
1049                         }
1050                 }
1051                 error = ext2_dirremove(fdvp, fcnp);
1052                 if (!error) {
1053                         xp->i_nlink--;
1054                         xp->i_flag |= IN_CHANGE;
1055                 }
1056                 xp->i_flag &= ~IN_RENAME;
1057         }
1058         if (dp)
1059                 vput(fdvp);
1060         if (xp)
1061                 vput(fvp);
1062         vrele(ap->a_fvp);
1063         return (error);
1064
1065 bad:
1066         if (xp)
1067                 vput(ITOV(xp));
1068         vput(ITOV(dp));
1069 out:
1070         if (doingdirectory)
1071                 ip->i_flag &= ~IN_RENAME;
1072         if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1073                 ip->i_nlink--;
1074                 ip->i_flag |= IN_CHANGE;
1075                 ip->i_flag &= ~IN_RENAME;
1076                 vput(fvp);
1077         } else
1078                 vrele(fvp);
1079         return (error);
1080 }
1081
1082 /*
1083  * Mkdir system call
1084  */
1085 static int
1086 ext2_mkdir(struct vop_mkdir_args *ap)
1087 {
1088         struct vnode *dvp = ap->a_dvp;
1089         struct vattr *vap = ap->a_vap;
1090         struct componentname *cnp = ap->a_cnp;
1091         struct inode *ip, *dp;
1092         struct vnode *tvp;
1093         struct dirtemplate dirtemplate, *dtp;
1094         int error, dmode;
1095
1096 #ifdef INVARIANTS
1097         if ((cnp->cn_flags & HASBUF) == 0)
1098                 panic("ext2_mkdir: no name");
1099 #endif
1100         dp = VTOI(dvp);
1101         if ((nlink_t)dp->i_nlink >= EXT2_LINK_MAX) {
1102                 error = EMLINK;
1103                 goto out;
1104         }
1105         dmode = vap->va_mode & 0777;
1106         dmode |= IFDIR;
1107         /*
1108          * Must simulate part of ext2_makeinode here to acquire the inode,
1109          * but not have it entered in the parent directory. The entry is
1110          * made later after writing "." and ".." entries.
1111          */
1112         error = ext2_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1113         if (error)
1114                 goto out;
1115         ip = VTOI(tvp);
1116         ip->i_gid = dp->i_gid;
1117 #ifdef SUIDDIR
1118         {
1119                 /*
1120                  * if we are hacking owners here, (only do this where told to)
1121                  * and we are not giving it TOO root, (would subvert quotas)
1122                  * then go ahead and give it to the other user.
1123                  * The new directory also inherits the SUID bit. 
1124                  * If user's UID and dir UID are the same,
1125                  * 'give it away' so that the SUID is still forced on.
1126                  */
1127                 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1128                    (dp->i_mode & ISUID) && dp->i_uid) {
1129                         dmode |= ISUID;
1130                         ip->i_uid = dp->i_uid;
1131                 } else {
1132                         ip->i_uid = cnp->cn_cred->cr_uid;
1133                 }
1134         }
1135 #else
1136         ip->i_uid = cnp->cn_cred->cr_uid;
1137 #endif
1138         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1139         ip->i_mode = dmode;
1140         tvp->v_type = VDIR;     /* Rest init'd in getnewvnode(). */
1141         ip->i_nlink = 2;
1142         if (cnp->cn_flags & ISWHITEOUT)
1143                 ip->i_flags |= UF_OPAQUE;
1144         error = ext2_update(tvp, 1);
1145
1146         /*
1147          * Bump link count in parent directory
1148          * to reflect work done below.  Should
1149          * be done before reference is created
1150          * so reparation is possible if we crash.
1151          */
1152         dp->i_nlink++;
1153         dp->i_flag |= IN_CHANGE;
1154         error = ext2_update(dvp, !DOINGASYNC(dvp));
1155         if (error)
1156                 goto bad;
1157
1158         /* Initialize directory with "." and ".." from static template. */
1159         if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
1160             EXT2F_INCOMPAT_FTYPE))
1161                 dtp = &mastertemplate;
1162         else
1163                 dtp = &omastertemplate;
1164         dirtemplate = *dtp;
1165         dirtemplate.dot_ino = ip->i_number;
1166         dirtemplate.dotdot_ino = dp->i_number;
1167         /* note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE 
1168          * so let's just redefine it - for this function only
1169          */
1170 #undef  DIRBLKSIZ 
1171 #define DIRBLKSIZ  VTOI(dvp)->i_e2fs->e2fs_bsize
1172         dirtemplate.dotdot_reclen = DIRBLKSIZ - 12;
1173         error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
1174             sizeof(dirtemplate), (off_t)0, UIO_SYSSPACE,
1175             IO_NODELOCKED | IO_SYNC | IO_NOMACCHECK, cnp->cn_cred, NOCRED,
1176             NULL, NULL);
1177         if (error) {
1178                 dp->i_nlink--;
1179                 dp->i_flag |= IN_CHANGE;
1180                 goto bad;
1181         }
1182         if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1183                 /* XXX should grow with balloc() */
1184                 panic("ext2_mkdir: blksize");
1185         else {
1186                 ip->i_size = DIRBLKSIZ;
1187                 ip->i_flag |= IN_CHANGE;
1188         }
1189
1190         /* Directory set up, now install its entry in the parent directory. */
1191         error = ext2_direnter(ip, dvp, cnp);
1192         if (error) {
1193                 dp->i_nlink--;
1194                 dp->i_flag |= IN_CHANGE;
1195         }
1196 bad:
1197         /*
1198          * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1199          * for us because we set the link count to 0.
1200          */
1201         if (error) {
1202                 ip->i_nlink = 0;
1203                 ip->i_flag |= IN_CHANGE;
1204                 vput(tvp);
1205         } else
1206                 *ap->a_vpp = tvp;
1207 out:
1208         return (error);
1209 #undef  DIRBLKSIZ
1210 #define DIRBLKSIZ  DEV_BSIZE
1211 }
1212
1213 /*
1214  * Rmdir system call.
1215  */
1216 static int
1217 ext2_rmdir(struct vop_rmdir_args *ap)
1218 {
1219         struct vnode *vp = ap->a_vp;
1220         struct vnode *dvp = ap->a_dvp;
1221         struct componentname *cnp = ap->a_cnp;
1222         struct inode *ip, *dp;
1223         int error;
1224
1225         ip = VTOI(vp);
1226         dp = VTOI(dvp);
1227
1228         /*
1229          * Verify the directory is empty (and valid).
1230          * (Rmdir ".." won't be valid since
1231          *  ".." will contain a reference to
1232          *  the current directory and thus be
1233          *  non-empty.)
1234          */
1235         error = 0;
1236         if (ip->i_nlink != 2 || !ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1237                 error = ENOTEMPTY;
1238                 goto out;
1239         }
1240         if ((dp->i_flags & APPEND)
1241             || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1242                 error = EPERM;
1243                 goto out;
1244         }
1245         /*
1246          * Delete reference to directory before purging
1247          * inode.  If we crash in between, the directory
1248          * will be reattached to lost+found,
1249          */
1250         error = ext2_dirremove(dvp, cnp);
1251         if (error)
1252                 goto out;
1253         dp->i_nlink--;
1254         dp->i_flag |= IN_CHANGE;
1255         cache_purge(dvp);
1256         VOP_UNLOCK(dvp, 0);
1257         /*
1258          * Truncate inode.  The only stuff left
1259          * in the directory is "." and "..".  The
1260          * "." reference is inconsequential since
1261          * we're quashing it.  The ".." reference
1262          * has already been adjusted above.  We've
1263          * removed the "." reference and the reference
1264          * in the parent directory, but there may be
1265          * other hard links so decrement by 2 and
1266          * worry about them later.
1267          */
1268         ip->i_nlink -= 2;
1269         error = ext2_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred,
1270             cnp->cn_thread);
1271         cache_purge(ITOV(ip));
1272         if (vn_lock(dvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1273                 VOP_UNLOCK(vp, 0);
1274                 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1275                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1276         }
1277 out:
1278         return (error);
1279 }
1280
1281 /*
1282  * symlink -- make a symbolic link
1283  */
1284 static int
1285 ext2_symlink(struct vop_symlink_args *ap)
1286 {
1287         struct vnode *vp, **vpp = ap->a_vpp;
1288         struct inode *ip;
1289         int len, error;
1290
1291         error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1292             vpp, ap->a_cnp);
1293         if (error)
1294                 return (error);
1295         vp = *vpp;
1296         len = strlen(ap->a_target);
1297         if (len < vp->v_mount->mnt_maxsymlinklen) {
1298                 ip = VTOI(vp);
1299                 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1300                 ip->i_size = len;
1301                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1302         } else
1303                 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1304                     UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1305                     ap->a_cnp->cn_cred, NOCRED, NULL, NULL);
1306         if (error)
1307                 vput(vp);
1308         return (error);
1309 }
1310
1311 /*
1312  * Return target name of a symbolic link
1313  */
1314 static int
1315 ext2_readlink(struct vop_readlink_args *ap)
1316 {
1317         struct vnode *vp = ap->a_vp;
1318         struct inode *ip = VTOI(vp);
1319         int isize;
1320
1321         isize = ip->i_size;
1322         if (isize < vp->v_mount->mnt_maxsymlinklen) {
1323                 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1324                 return (0);
1325         }
1326         return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1327 }
1328
1329 /*
1330  * Calculate the logical to physical mapping if not done already,
1331  * then call the device strategy routine.
1332  *
1333  * In order to be able to swap to a file, the ext2_bmaparray() operation may not
1334  * deadlock on memory.  See ext2_bmap() for details.
1335  */
1336 static int
1337 ext2_strategy(struct vop_strategy_args *ap)
1338 {
1339         struct buf *bp = ap->a_bp;
1340         struct vnode *vp = ap->a_vp;
1341         struct inode *ip;
1342         struct bufobj *bo;
1343         daddr_t blkno;
1344         int error;
1345
1346         ip = VTOI(vp);
1347         if (vp->v_type == VBLK || vp->v_type == VCHR)
1348                 panic("ext2_strategy: spec");
1349         if (bp->b_blkno == bp->b_lblkno) {
1350                 error = ext2_bmaparray(vp, bp->b_lblkno, &blkno, NULL, NULL);
1351                 bp->b_blkno = blkno;
1352                 if (error) {
1353                         bp->b_error = error;
1354                         bp->b_ioflags |= BIO_ERROR;
1355                         bufdone(bp);
1356                         return (0);
1357                 }
1358                 if ((long)bp->b_blkno == -1)
1359                         vfs_bio_clrbuf(bp);
1360         }
1361         if ((long)bp->b_blkno == -1) {
1362                 bufdone(bp);
1363                 return (0);
1364         }
1365         bp->b_iooffset = dbtob(bp->b_blkno);
1366         bo = VFSTOEXT2(vp->v_mount)->um_bo;
1367         BO_STRATEGY(bo, bp);
1368         return (0);
1369 }
1370
1371 /*
1372  * Print out the contents of an inode.
1373  */
1374 static int
1375 ext2_print(struct vop_print_args *ap)
1376 {
1377         struct vnode *vp = ap->a_vp;
1378         struct inode *ip = VTOI(vp);
1379
1380         vn_printf(ip->i_devvp, "\tino %lu", (u_long)ip->i_number);
1381         if (vp->v_type == VFIFO)
1382                 fifo_printinfo(vp);
1383         printf("\n");
1384         return (0);
1385 }
1386
1387 /*
1388  * Close wrapper for fifos.
1389  *
1390  * Update the times on the inode then do device close.
1391  */
1392 static int
1393 ext2fifo_close(struct vop_close_args *ap)
1394 {
1395         struct vnode *vp = ap->a_vp;
1396
1397         VI_LOCK(vp);
1398         if (vp->v_usecount > 1)
1399                 ext2_itimes_locked(vp);
1400         VI_UNLOCK(vp);
1401         return (fifo_specops.vop_close(ap));
1402 }
1403
1404 /*
1405  * Kqfilter wrapper for fifos.
1406  *
1407  * Fall through to ext2 kqfilter routines if needed 
1408  */
1409 static int
1410 ext2fifo_kqfilter(struct vop_kqfilter_args *ap)
1411 {
1412         int error;
1413
1414         error = fifo_specops.vop_kqfilter(ap);
1415         if (error)
1416                 error = vfs_kqfilter(ap);
1417         return (error);
1418 }
1419
1420 /*
1421  * Return POSIX pathconf information applicable to ext2 filesystems.
1422  */
1423 static int
1424 ext2_pathconf(struct vop_pathconf_args *ap)
1425 {
1426         int error = 0;
1427
1428         switch (ap->a_name) {
1429         case _PC_LINK_MAX:
1430                 *ap->a_retval = EXT2_LINK_MAX;
1431                 break;
1432         case _PC_NAME_MAX:
1433                 *ap->a_retval = NAME_MAX;
1434                 break;
1435         case _PC_PATH_MAX:
1436                 *ap->a_retval = PATH_MAX;
1437                 break;
1438         case _PC_PIPE_BUF:
1439                 *ap->a_retval = PIPE_BUF;
1440                 break;
1441         case _PC_CHOWN_RESTRICTED:
1442                 *ap->a_retval = 1;
1443                 break;
1444         case _PC_NO_TRUNC:
1445                 *ap->a_retval = 1;
1446                 break;
1447         case _PC_MIN_HOLE_SIZE:
1448                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1449                 break;
1450         case _PC_ASYNC_IO:
1451                 /* _PC_ASYNC_IO should have been handled by upper layers. */
1452                 KASSERT(0, ("_PC_ASYNC_IO should not get here"));
1453                 error = EINVAL;
1454                 break;
1455         case _PC_PRIO_IO:
1456                 *ap->a_retval = 0;
1457                 break;
1458         case _PC_SYNC_IO:
1459                 *ap->a_retval = 0;
1460                 break;
1461         case _PC_ALLOC_SIZE_MIN:
1462                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
1463                 break;
1464         case _PC_FILESIZEBITS:
1465                 *ap->a_retval = 64;
1466                 break;
1467         case _PC_REC_INCR_XFER_SIZE:
1468                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1469                 break;
1470         case _PC_REC_MAX_XFER_SIZE:
1471                 *ap->a_retval = -1; /* means ``unlimited'' */
1472                 break;
1473         case _PC_REC_MIN_XFER_SIZE:
1474                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1475                 break;
1476         case _PC_REC_XFER_ALIGN:
1477                 *ap->a_retval = PAGE_SIZE;
1478                 break;
1479         case _PC_SYMLINK_MAX:
1480                 *ap->a_retval = MAXPATHLEN;
1481                 break;
1482
1483         default:
1484                 error = EINVAL;
1485                 break;
1486         }
1487         return (error);
1488 }
1489
1490 /*
1491  * Vnode pointer to File handle
1492  */
1493 /* ARGSUSED */
1494 static int
1495 ext2_vptofh(struct vop_vptofh_args *ap)
1496 {
1497         struct inode *ip;
1498         struct ufid *ufhp;
1499
1500         ip = VTOI(ap->a_vp);
1501         ufhp = (struct ufid *)ap->a_fhp;
1502         ufhp->ufid_len = sizeof(struct ufid);
1503         ufhp->ufid_ino = ip->i_number;
1504         ufhp->ufid_gen = ip->i_gen;
1505         return (0);
1506 }
1507
1508 /*
1509  * Initialize the vnode associated with a new inode, handle aliased
1510  * vnodes.
1511  */
1512 int
1513 ext2_vinit(struct mount *mntp, struct vop_vector *fifoops, struct vnode **vpp)
1514 {
1515         struct inode *ip;
1516         struct vnode *vp;
1517
1518         vp = *vpp;
1519         ip = VTOI(vp);
1520         vp->v_type = IFTOVT(ip->i_mode);
1521         if (vp->v_type == VFIFO)
1522                 vp->v_op = fifoops;
1523
1524         if (ip->i_number == EXT2_ROOTINO)
1525                 vp->v_vflag |= VV_ROOT;
1526         ip->i_modrev = init_va_filerev();
1527         *vpp = vp;
1528         return (0);
1529 }
1530
1531 /*
1532  * Allocate a new inode.
1533  */
1534 static int
1535 ext2_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
1536     struct componentname *cnp)
1537 {
1538         struct inode *ip, *pdir;
1539         struct vnode *tvp;
1540         int error;
1541
1542         pdir = VTOI(dvp);
1543 #ifdef INVARIANTS
1544         if ((cnp->cn_flags & HASBUF) == 0)
1545                 panic("ext2_makeinode: no name");
1546 #endif
1547         *vpp = NULL;
1548         if ((mode & IFMT) == 0)
1549                 mode |= IFREG;
1550
1551         error = ext2_valloc(dvp, mode, cnp->cn_cred, &tvp);
1552         if (error) {
1553                 return (error);
1554         }
1555         ip = VTOI(tvp);
1556         ip->i_gid = pdir->i_gid;
1557 #ifdef SUIDDIR
1558         {
1559                 /*
1560                  * if we are
1561                  * not the owner of the directory,
1562                  * and we are hacking owners here, (only do this where told to)
1563                  * and we are not giving it TOO root, (would subvert quotas)
1564                  * then go ahead and give it to the other user.
1565                  * Note that this drops off the execute bits for security.
1566                  */
1567                 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1568                      (pdir->i_mode & ISUID) &&
1569                      (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1570                         ip->i_uid = pdir->i_uid;
1571                         mode &= ~07111;
1572                 } else {
1573                         ip->i_uid = cnp->cn_cred->cr_uid;
1574                 }
1575         }
1576 #else
1577         ip->i_uid = cnp->cn_cred->cr_uid;
1578 #endif
1579         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1580         ip->i_mode = mode;
1581         tvp->v_type = IFTOVT(mode);     /* Rest init'd in getnewvnode(). */
1582         ip->i_nlink = 1;
1583         if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred)) {
1584                 if (priv_check_cred(cnp->cn_cred, PRIV_VFS_RETAINSUGID, 0))
1585                         ip->i_mode &= ~ISGID;
1586         }
1587
1588         if (cnp->cn_flags & ISWHITEOUT)
1589                 ip->i_flags |= UF_OPAQUE;
1590
1591         /*
1592          * Make sure inode goes to disk before directory entry.
1593          */
1594         error = ext2_update(tvp, !DOINGASYNC(tvp));
1595         if (error)
1596                 goto bad;
1597         error = ext2_direnter(ip, dvp, cnp);
1598         if (error)
1599                 goto bad;
1600
1601         *vpp = tvp;
1602         return (0);
1603
1604 bad:
1605         /*
1606          * Write error occurred trying to update the inode
1607          * or the directory so must deallocate the inode.
1608          */
1609         ip->i_nlink = 0;
1610         ip->i_flag |= IN_CHANGE;
1611         vput(tvp);
1612         return (error);
1613 }
1614
1615 /*
1616  * Vnode op for reading.
1617  */
1618 static int
1619 ext2_read(struct vop_read_args *ap)
1620 {
1621         struct vnode *vp;
1622         struct inode *ip;
1623         int error;
1624
1625         vp = ap->a_vp;
1626         ip = VTOI(vp);
1627
1628         /*EXT4_EXT_LOCK(ip);*/
1629         if (ip->i_flag & IN_E4EXTENTS)
1630                 error = ext4_ext_read(ap);
1631         else
1632                 error = ext2_ind_read(ap);
1633         /*EXT4_EXT_UNLOCK(ip);*/
1634         return (error);
1635 }
1636
1637 /*
1638  * Vnode op for reading.
1639  */
1640 static int
1641 ext2_ind_read(struct vop_read_args *ap)
1642 {
1643         struct vnode *vp;
1644         struct inode *ip;
1645         struct uio *uio;
1646         struct m_ext2fs *fs;
1647         struct buf *bp;
1648         daddr_t lbn, nextlbn;
1649         off_t bytesinfile;
1650         long size, xfersize, blkoffset;
1651         int error, orig_resid, seqcount;
1652         int ioflag;
1653
1654         vp = ap->a_vp;
1655         uio = ap->a_uio;
1656         ioflag = ap->a_ioflag;
1657
1658         seqcount = ap->a_ioflag >> IO_SEQSHIFT;
1659         ip = VTOI(vp);
1660
1661 #ifdef INVARIANTS
1662         if (uio->uio_rw != UIO_READ)
1663                 panic("%s: mode", "ext2_read");
1664
1665         if (vp->v_type == VLNK) {
1666                 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
1667                         panic("%s: short symlink", "ext2_read");
1668         } else if (vp->v_type != VREG && vp->v_type != VDIR)
1669                 panic("%s: type %d", "ext2_read", vp->v_type);
1670 #endif
1671         orig_resid = uio->uio_resid;
1672         KASSERT(orig_resid >= 0, ("ext2_read: uio->uio_resid < 0"));
1673         if (orig_resid == 0)
1674                 return (0);
1675         KASSERT(uio->uio_offset >= 0, ("ext2_read: uio->uio_offset < 0"));
1676         fs = ip->i_e2fs;
1677         if (uio->uio_offset < ip->i_size &&
1678             uio->uio_offset >= fs->e2fs_maxfilesize)
1679                 return (EOVERFLOW);
1680
1681         for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
1682                 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
1683                         break;
1684                 lbn = lblkno(fs, uio->uio_offset);
1685                 nextlbn = lbn + 1;
1686                 size = blksize(fs, ip, lbn);
1687                 blkoffset = blkoff(fs, uio->uio_offset);
1688
1689                 xfersize = fs->e2fs_fsize - blkoffset;
1690                 if (uio->uio_resid < xfersize)
1691                         xfersize = uio->uio_resid;
1692                 if (bytesinfile < xfersize)
1693                         xfersize = bytesinfile;
1694
1695                 if (lblktosize(fs, nextlbn) >= ip->i_size)
1696                         error = bread(vp, lbn, size, NOCRED, &bp);
1697                 else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0)
1698                         error = cluster_read(vp, ip->i_size, lbn, size,
1699                             NOCRED, blkoffset + uio->uio_resid, seqcount, &bp);
1700                 else if (seqcount > 1) {
1701                         u_int nextsize = blksize(fs, ip, nextlbn);
1702                         error = breadn(vp, lbn,
1703                             size, &nextlbn, &nextsize, 1, NOCRED, &bp);
1704                 } else
1705                         error = bread(vp, lbn, size, NOCRED, &bp);
1706                 if (error) {
1707                         brelse(bp);
1708                         bp = NULL;
1709                         break;
1710                 }
1711
1712                 /*
1713                  * If IO_DIRECT then set B_DIRECT for the buffer.  This
1714                  * will cause us to attempt to release the buffer later on
1715                  * and will cause the buffer cache to attempt to free the
1716                  * underlying pages.
1717                  */
1718                 if (ioflag & IO_DIRECT)
1719                         bp->b_flags |= B_DIRECT;
1720
1721                 /*
1722                  * We should only get non-zero b_resid when an I/O error
1723                  * has occurred, which should cause us to break above.
1724                  * However, if the short read did not cause an error,
1725                  * then we want to ensure that we do not uiomove bad
1726                  * or uninitialized data.
1727                  */
1728                 size -= bp->b_resid;
1729                 if (size < xfersize) {
1730                         if (size == 0)
1731                                 break;
1732                         xfersize = size;
1733                 }
1734                 error = uiomove((char *)bp->b_data + blkoffset,
1735                         (int)xfersize, uio);
1736                 if (error)
1737                         break;
1738
1739                 if (ioflag & (IO_VMIO|IO_DIRECT)) {
1740                         /*
1741                          * If it's VMIO or direct I/O, then we don't
1742                          * need the buf, mark it available for
1743                          * freeing. If it's non-direct VMIO, the VM has
1744                          * the data.
1745                          */
1746                         bp->b_flags |= B_RELBUF;
1747                         brelse(bp);
1748                 } else {
1749                         /*
1750                          * Otherwise let whoever
1751                          * made the request take care of
1752                          * freeing it. We just queue
1753                          * it onto another list.
1754                          */
1755                         bqrelse(bp);
1756                 }
1757         }
1758
1759         /* 
1760          * This can only happen in the case of an error
1761          * because the loop above resets bp to NULL on each iteration
1762          * and on normal completion has not set a new value into it.
1763          * so it must have come from a 'break' statement
1764          */
1765         if (bp != NULL) {
1766                 if (ioflag & (IO_VMIO|IO_DIRECT)) {
1767                         bp->b_flags |= B_RELBUF;
1768                         brelse(bp);
1769                 } else {
1770                         bqrelse(bp);
1771                 }
1772         }
1773
1774         if ((error == 0 || uio->uio_resid != orig_resid) &&
1775             (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
1776                 ip->i_flag |= IN_ACCESS;
1777         return (error);
1778 }
1779
1780 static int
1781 ext2_ioctl(struct vop_ioctl_args *ap)
1782 {
1783
1784         switch (ap->a_command) {
1785         case FIOSEEKDATA:
1786         case FIOSEEKHOLE:
1787                 return (vn_bmap_seekhole(ap->a_vp, ap->a_command,
1788                     (off_t *)ap->a_data, ap->a_cred));
1789         default:
1790                 return (ENOTTY);
1791         }
1792 }
1793
1794 /*
1795  * this function handles ext4 extents block mapping
1796  */
1797 static int
1798 ext4_ext_read(struct vop_read_args *ap)
1799 {
1800         struct vnode *vp;
1801         struct inode *ip;
1802         struct uio *uio;
1803         struct m_ext2fs *fs;
1804         struct buf *bp;
1805         struct ext4_extent nex, *ep;
1806         struct ext4_extent_path path;
1807         daddr_t lbn, newblk;
1808         off_t bytesinfile;
1809         int cache_type;
1810         ssize_t orig_resid;
1811         int error;
1812         long size, xfersize, blkoffset;
1813
1814         vp = ap->a_vp;
1815         ip = VTOI(vp);
1816         uio = ap->a_uio;
1817         memset(&path, 0, sizeof(path));
1818
1819         orig_resid = uio->uio_resid;
1820         KASSERT(orig_resid >= 0, ("%s: uio->uio_resid < 0", __func__));
1821         if (orig_resid == 0)
1822                 return (0);
1823         KASSERT(uio->uio_offset >= 0, ("%s: uio->uio_offset < 0", __func__));
1824         fs = ip->i_e2fs;
1825         if (uio->uio_offset < ip->i_size && uio->uio_offset >= fs->e2fs_maxfilesize)
1826                 return (EOVERFLOW);
1827
1828         while (uio->uio_resid > 0) {
1829                 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
1830                         break;
1831                 lbn = lblkno(fs, uio->uio_offset);
1832                 size = blksize(fs, ip, lbn);
1833                 blkoffset = blkoff(fs, uio->uio_offset);
1834
1835                 xfersize = fs->e2fs_fsize - blkoffset;
1836                 xfersize = MIN(xfersize, uio->uio_resid);
1837                 xfersize = MIN(xfersize, bytesinfile);
1838
1839                 /* get block from ext4 extent cache */
1840                 cache_type = ext4_ext_in_cache(ip, lbn, &nex);
1841                 switch (cache_type) {
1842                 case EXT4_EXT_CACHE_NO:
1843                         ext4_ext_find_extent(fs, ip, lbn, &path);
1844                         ep = path.ep_ext;
1845                         if (ep == NULL)
1846                                 return (EIO);
1847
1848                         ext4_ext_put_cache(ip, ep, EXT4_EXT_CACHE_IN);
1849
1850                         newblk = lbn - ep->e_blk + (ep->e_start_lo |
1851                             (daddr_t)ep->e_start_hi << 32);
1852
1853                         if (path.ep_bp != NULL) {
1854                                 brelse(path.ep_bp);
1855                                 path.ep_bp = NULL;
1856                         }
1857                         break;
1858
1859                 case EXT4_EXT_CACHE_GAP:
1860                         /* block has not been allocated yet */
1861                         return (0);
1862
1863                 case EXT4_EXT_CACHE_IN:
1864                         newblk = lbn - nex.e_blk + (nex.e_start_lo |
1865                             (daddr_t)nex.e_start_hi << 32);
1866                         break;
1867
1868                 default:
1869                         panic("%s: invalid cache type", __func__);
1870                 }
1871
1872                 error = bread(ip->i_devvp, fsbtodb(fs, newblk), size, NOCRED, &bp);
1873                 if (error) {
1874                         brelse(bp);
1875                         return (error);
1876                 }
1877
1878                 size -= bp->b_resid;
1879                 if (size < xfersize) {
1880                         if (size == 0) {
1881                                 bqrelse(bp);
1882                                 break;
1883                         }
1884                         xfersize = size;
1885                 }
1886                 error = uiomove(bp->b_data + blkoffset, (int)xfersize, uio);
1887                 bqrelse(bp);
1888                 if (error)
1889                         return (error);
1890         }
1891
1892         return (0);
1893 }
1894
1895 /*
1896  * Vnode op for writing.
1897  */
1898 static int
1899 ext2_write(struct vop_write_args *ap)
1900 {
1901         struct vnode *vp;
1902         struct uio *uio;
1903         struct inode *ip;
1904         struct m_ext2fs *fs;
1905         struct buf *bp;
1906         daddr_t lbn;
1907         off_t osize;
1908         int blkoffset, error, flags, ioflag, resid, size, seqcount, xfersize;
1909
1910         ioflag = ap->a_ioflag;
1911         uio = ap->a_uio;
1912         vp = ap->a_vp;
1913
1914         seqcount = ioflag >> IO_SEQSHIFT;
1915         ip = VTOI(vp);
1916
1917 #ifdef INVARIANTS
1918         if (uio->uio_rw != UIO_WRITE)
1919                 panic("%s: mode", "ext2_write");
1920 #endif
1921
1922         switch (vp->v_type) {
1923         case VREG:
1924                 if (ioflag & IO_APPEND)
1925                         uio->uio_offset = ip->i_size;
1926                 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
1927                         return (EPERM);
1928                 /* FALLTHROUGH */
1929         case VLNK:
1930                 break;
1931         case VDIR:
1932                 /* XXX differs from ffs -- this is called from ext2_mkdir(). */
1933                 if ((ioflag & IO_SYNC) == 0)
1934                 panic("ext2_write: nonsync dir write");
1935                 break;
1936         default:
1937                 panic("ext2_write: type %p %d (%jd,%jd)", (void *)vp,
1938                     vp->v_type, (intmax_t)uio->uio_offset,
1939                     (intmax_t)uio->uio_resid);
1940         }
1941
1942         KASSERT(uio->uio_resid >= 0, ("ext2_write: uio->uio_resid < 0"));
1943         KASSERT(uio->uio_offset >= 0, ("ext2_write: uio->uio_offset < 0"));
1944         fs = ip->i_e2fs;
1945         if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->e2fs_maxfilesize)
1946                 return (EFBIG);
1947         /*
1948          * Maybe this should be above the vnode op call, but so long as
1949          * file servers have no limits, I don't think it matters.
1950          */
1951         if (vn_rlimit_fsize(vp, uio, uio->uio_td))
1952                 return (EFBIG);
1953
1954         resid = uio->uio_resid;
1955         osize = ip->i_size;
1956         if (seqcount > BA_SEQMAX)
1957                 flags = BA_SEQMAX << BA_SEQSHIFT;
1958         else
1959                 flags = seqcount << BA_SEQSHIFT;
1960         if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
1961                 flags |= IO_SYNC;
1962
1963         for (error = 0; uio->uio_resid > 0;) {
1964                 lbn = lblkno(fs, uio->uio_offset);
1965                 blkoffset = blkoff(fs, uio->uio_offset);
1966                 xfersize = fs->e2fs_fsize - blkoffset;
1967                 if (uio->uio_resid < xfersize)
1968                         xfersize = uio->uio_resid;
1969                 if (uio->uio_offset + xfersize > ip->i_size)
1970                         vnode_pager_setsize(vp, uio->uio_offset + xfersize);
1971
1972                 /*
1973                  * We must perform a read-before-write if the transfer size
1974                  * does not cover the entire buffer.
1975                  */
1976                 if (fs->e2fs_bsize > xfersize)
1977                         flags |= BA_CLRBUF;
1978                 else
1979                         flags &= ~BA_CLRBUF;
1980                 error = ext2_balloc(ip, lbn, blkoffset + xfersize,
1981                     ap->a_cred, &bp, flags);
1982                 if (error != 0)
1983                         break;
1984
1985                 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
1986                         bp->b_flags |= B_NOCACHE;
1987                 if (uio->uio_offset + xfersize > ip->i_size)
1988                         ip->i_size = uio->uio_offset + xfersize;
1989                 size = blksize(fs, ip, lbn) - bp->b_resid;
1990                 if (size < xfersize)
1991                         xfersize = size;
1992
1993                 error =
1994                     uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
1995                 /*
1996                  * If the buffer is not already filled and we encounter an
1997                  * error while trying to fill it, we have to clear out any
1998                  * garbage data from the pages instantiated for the buffer.
1999                  * If we do not, a failed uiomove() during a write can leave
2000                  * the prior contents of the pages exposed to a userland mmap.
2001                  *
2002                  * Note that we need only clear buffers with a transfer size
2003                  * equal to the block size because buffers with a shorter
2004                  * transfer size were cleared above by the call to ext2_balloc()
2005                  * with the BA_CLRBUF flag set.
2006                  *
2007                  * If the source region for uiomove identically mmaps the
2008                  * buffer, uiomove() performed the NOP copy, and the buffer
2009                  * content remains valid because the page fault handler
2010                  * validated the pages.
2011                  */
2012                 if (error != 0 && (bp->b_flags & B_CACHE) == 0 &&
2013                     fs->e2fs_bsize == xfersize)
2014                         vfs_bio_clrbuf(bp);
2015                 if (ioflag & (IO_VMIO|IO_DIRECT)) {
2016                         bp->b_flags |= B_RELBUF;
2017                 }
2018
2019                 /*
2020                  * If IO_SYNC each buffer is written synchronously.  Otherwise
2021                  * if we have a severe page deficiency write the buffer
2022                  * asynchronously.  Otherwise try to cluster, and if that
2023                  * doesn't do it then either do an async write (if O_DIRECT),
2024                  * or a delayed write (if not).
2025                  */
2026                 if (ioflag & IO_SYNC) {
2027                         (void)bwrite(bp);
2028                 } else if (vm_page_count_severe() ||
2029                     buf_dirty_count_severe() ||
2030                     (ioflag & IO_ASYNC)) {
2031                         bp->b_flags |= B_CLUSTEROK;
2032                         bawrite(bp);
2033                 } else if (xfersize + blkoffset == fs->e2fs_fsize) {
2034                         if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
2035                                 bp->b_flags |= B_CLUSTEROK;
2036                                 cluster_write(vp, bp, ip->i_size, seqcount);
2037                         } else {
2038                                 bawrite(bp);
2039                         }
2040                 } else if (ioflag & IO_DIRECT) {
2041                         bp->b_flags |= B_CLUSTEROK;
2042                         bawrite(bp);
2043                 } else {
2044                         bp->b_flags |= B_CLUSTEROK;
2045                         bdwrite(bp);
2046                 }
2047                 if (error || xfersize == 0)
2048                         break;
2049         }
2050         /*
2051          * If we successfully wrote any data, and we are not the superuser
2052          * we clear the setuid and setgid bits as a precaution against
2053          * tampering.
2054          */
2055         if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
2056             ap->a_cred) {
2057                 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
2058                         ip->i_mode &= ~(ISUID | ISGID);
2059         }
2060         if (error) {
2061                 if (ioflag & IO_UNIT) {
2062                         (void)ext2_truncate(vp, osize,
2063                             ioflag & IO_SYNC, ap->a_cred, uio->uio_td);
2064                         uio->uio_offset -= resid - uio->uio_resid;
2065                         uio->uio_resid = resid;
2066                 }
2067         }
2068         if (uio->uio_resid != resid) {
2069                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
2070                 if (ioflag & IO_SYNC)
2071                         error = ext2_update(vp, 1);
2072         }
2073         return (error);
2074 }
2075
2076 /*
2077  * get page routine
2078  */
2079 static int
2080 ext2_getpages(struct vop_getpages_args *ap)
2081 {
2082         int i;
2083         vm_page_t mreq;
2084         int pcount;
2085
2086         pcount = round_page(ap->a_count) / PAGE_SIZE;
2087         mreq = ap->a_m[ap->a_reqpage];
2088
2089         /*
2090          * if ANY DEV_BSIZE blocks are valid on a large filesystem block,
2091          * then the entire page is valid.  Since the page may be mapped,
2092          * user programs might reference data beyond the actual end of file
2093          * occuring within the page.  We have to zero that data.
2094          */
2095         VM_OBJECT_WLOCK(mreq->object);
2096         if (mreq->valid) {
2097                 if (mreq->valid != VM_PAGE_BITS_ALL)
2098                         vm_page_zero_invalid(mreq, TRUE);
2099                 for (i = 0; i < pcount; i++) {
2100                         if (i != ap->a_reqpage) {
2101                                 vm_page_lock(ap->a_m[i]);
2102                                 vm_page_free(ap->a_m[i]);
2103                                 vm_page_unlock(ap->a_m[i]);
2104                         }
2105                 }
2106                 VM_OBJECT_WUNLOCK(mreq->object);
2107                 return VM_PAGER_OK;
2108         }
2109         VM_OBJECT_WUNLOCK(mreq->object);
2110
2111         return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
2112                                             ap->a_count,
2113                                             ap->a_reqpage);
2114 }