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