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