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