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