]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/gnu/fs/ext2fs/ext2_vnops.c
Merge ntpd & friends 4.2.4p5 from vendor/ntp/dist into head. Next commit
[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 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 inode *ip, *xp, *dp;
769         struct dirtemplate dirbuf;
770         int doingdirectory = 0, oldparent = 0, newparent = 0;
771         int error = 0;
772         u_char namlen;
773
774 #ifdef DIAGNOSTIC
775         if ((tcnp->cn_flags & HASBUF) == 0 ||
776             (fcnp->cn_flags & HASBUF) == 0)
777                 panic("ext2_rename: no name");
778 #endif
779         /*
780          * Check for cross-device rename.
781          */
782         if ((fvp->v_mount != tdvp->v_mount) ||
783             (tvp && (fvp->v_mount != tvp->v_mount))) {
784                 error = EXDEV;
785 abortit:
786                 if (tdvp == tvp)
787                         vrele(tdvp);
788                 else
789                         vput(tdvp);
790                 if (tvp)
791                         vput(tvp);
792                 vrele(fdvp);
793                 vrele(fvp);
794                 return (error);
795         }
796
797         if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
798             (VTOI(tdvp)->i_flags & APPEND))) {
799                 error = EPERM;
800                 goto abortit;
801         }
802
803         /*
804          * Renaming a file to itself has no effect.  The upper layers should
805          * not call us in that case.  Temporarily just warn if they do.
806          */
807         if (fvp == tvp) {
808                 printf("ext2_rename: fvp == tvp (can't happen)\n");
809                 error = 0;
810                 goto abortit;
811         }
812
813         if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
814                 goto abortit;
815         dp = VTOI(fdvp);
816         ip = VTOI(fvp);
817         if (ip->i_nlink >= LINK_MAX) {
818                 VOP_UNLOCK(fvp, 0);
819                 error = EMLINK;
820                 goto abortit;
821         }
822         if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
823             || (dp->i_flags & APPEND)) {
824                 VOP_UNLOCK(fvp, 0);
825                 error = EPERM;
826                 goto abortit;
827         }
828         if ((ip->i_mode & IFMT) == IFDIR) {
829                 /*
830                  * Avoid ".", "..", and aliases of "." for obvious reasons.
831                  */
832                 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
833                     dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
834                     (ip->i_flag & IN_RENAME)) {
835                         VOP_UNLOCK(fvp, 0);
836                         error = EINVAL;
837                         goto abortit;
838                 }
839                 ip->i_flag |= IN_RENAME;
840                 oldparent = dp->i_number;
841                 doingdirectory++;
842         }
843         vrele(fdvp);
844
845         /*
846          * When the target exists, both the directory
847          * and target vnodes are returned locked.
848          */
849         dp = VTOI(tdvp);
850         xp = NULL;
851         if (tvp)
852                 xp = VTOI(tvp);
853
854         /*
855          * 1) Bump link count while we're moving stuff
856          *    around.  If we crash somewhere before
857          *    completing our work, the link count
858          *    may be wrong, but correctable.
859          */
860         ip->i_nlink++;
861         ip->i_flag |= IN_CHANGE;
862         if ((error = ext2_update(fvp, 1)) != 0) {
863                 VOP_UNLOCK(fvp, 0);
864                 goto bad;
865         }
866
867         /*
868          * If ".." must be changed (ie the directory gets a new
869          * parent) then the source directory must not be in the
870          * directory heirarchy above the target, as this would
871          * orphan everything below the source directory. Also
872          * the user must have write permission in the source so
873          * as to be able to change "..". We must repeat the call
874          * to namei, as the parent directory is unlocked by the
875          * call to checkpath().
876          */
877         error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
878         VOP_UNLOCK(fvp, 0);
879         if (oldparent != dp->i_number)
880                 newparent = dp->i_number;
881         if (doingdirectory && newparent) {
882                 if (error)      /* write access check above */
883                         goto bad;
884                 if (xp != NULL)
885                         vput(tvp);
886                 error = ext2_checkpath(ip, dp, tcnp->cn_cred);
887                 if (error)
888                         goto out;
889                 VREF(tdvp);
890                 error = relookup(tdvp, &tvp, tcnp);
891                 if (error)
892                         goto out;
893                 vrele(tdvp);
894                 dp = VTOI(tdvp);
895                 xp = NULL;
896                 if (tvp)
897                         xp = VTOI(tvp);
898         }
899         /*
900          * 2) If target doesn't exist, link the target
901          *    to the source and unlink the source.
902          *    Otherwise, rewrite the target directory
903          *    entry to reference the source inode and
904          *    expunge the original entry's existence.
905          */
906         if (xp == NULL) {
907                 if (dp->i_devvp != ip->i_devvp)
908                         panic("ext2_rename: EXDEV");
909                 /*
910                  * Account for ".." in new directory.
911                  * When source and destination have the same
912                  * parent we don't fool with the link count.
913                  */
914                 if (doingdirectory && newparent) {
915                         if ((nlink_t)dp->i_nlink >= LINK_MAX) {
916                                 error = EMLINK;
917                                 goto bad;
918                         }
919                         dp->i_nlink++;
920                         dp->i_flag |= IN_CHANGE;
921                         error = ext2_update(tdvp, 1);
922                         if (error)
923                                 goto bad;
924                 }
925                 error = ext2_direnter(ip, tdvp, tcnp);
926                 if (error) {
927                         if (doingdirectory && newparent) {
928                                 dp->i_nlink--;
929                                 dp->i_flag |= IN_CHANGE;
930                                 (void)ext2_update(tdvp, 1);
931                         }
932                         goto bad;
933                 }
934                 vput(tdvp);
935         } else {
936                 if (xp->i_devvp != dp->i_devvp || xp->i_devvp != ip->i_devvp)
937                        panic("ext2_rename: EXDEV");
938                 /*
939                  * Short circuit rename(foo, foo).
940                  */
941                 if (xp->i_number == ip->i_number)
942                         panic("ext2_rename: same file");
943                 /*
944                  * If the parent directory is "sticky", then the user must
945                  * own the parent directory, or the destination of the rename,
946                  * otherwise the destination may not be changed (except by
947                  * root). This implements append-only directories.
948                  */
949                 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
950                     tcnp->cn_cred->cr_uid != dp->i_uid &&
951                     xp->i_uid != tcnp->cn_cred->cr_uid) {
952                         error = EPERM;
953                         goto bad;
954                 }
955                 /*
956                  * Target must be empty if a directory and have no links
957                  * to it. Also, ensure source and target are compatible
958                  * (both directories, or both not directories).
959                  */
960                 if ((xp->i_mode&IFMT) == IFDIR) {
961                         if (! ext2_dirempty(xp, dp->i_number, tcnp->cn_cred) || 
962                             xp->i_nlink > 2) {
963                                 error = ENOTEMPTY;
964                                 goto bad;
965                         }
966                         if (!doingdirectory) {
967                                 error = ENOTDIR;
968                                 goto bad;
969                         }
970                         cache_purge(tdvp);
971                 } else if (doingdirectory) {
972                         error = EISDIR;
973                         goto bad;
974                 }
975                 error = ext2_dirrewrite(dp, ip, tcnp);
976                 if (error)
977                         goto bad;
978                 /*
979                  * If the target directory is in the same
980                  * directory as the source directory,
981                  * decrement the link count on the parent
982                  * of the target directory.
983                  */
984                 if (doingdirectory && !newparent) {
985                        dp->i_nlink--;
986                        dp->i_flag |= IN_CHANGE;
987                 }
988                 vput(tdvp);
989                 /*
990                  * Adjust the link count of the target to
991                  * reflect the dirrewrite above.  If this is
992                  * a directory it is empty and there are
993                  * no links to it, so we can squash the inode and
994                  * any space associated with it.  We disallowed
995                  * renaming over top of a directory with links to
996                  * it above, as the remaining link would point to
997                  * a directory without "." or ".." entries.
998                  */
999                 xp->i_nlink--;
1000                 if (doingdirectory) {
1001                         if (--xp->i_nlink != 0)
1002                                 panic("ext2_rename: linked directory");
1003                         error = ext2_truncate(tvp, (off_t)0, IO_SYNC,
1004                             tcnp->cn_cred, tcnp->cn_thread);
1005                 }
1006                 xp->i_flag |= IN_CHANGE;
1007                 vput(tvp);
1008                 xp = NULL;
1009         }
1010
1011         /*
1012          * 3) Unlink the source.
1013          */
1014         fcnp->cn_flags &= ~MODMASK;
1015         fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1016         VREF(fdvp);
1017         error = relookup(fdvp, &fvp, fcnp);
1018         if (error == 0)
1019                 vrele(fdvp);
1020         if (fvp != NULL) {
1021                 xp = VTOI(fvp);
1022                 dp = VTOI(fdvp);
1023         } else {
1024                 /*
1025                  * From name has disappeared.
1026                  */
1027                 if (doingdirectory)
1028                         panic("ext2_rename: lost dir entry");
1029                 vrele(ap->a_fvp);
1030                 return (0);
1031         }
1032         /*
1033          * Ensure that the directory entry still exists and has not
1034          * changed while the new name has been entered. If the source is
1035          * a file then the entry may have been unlinked or renamed. In
1036          * either case there is no further work to be done. If the source
1037          * is a directory then it cannot have been rmdir'ed; its link
1038          * count of three would cause a rmdir to fail with ENOTEMPTY.
1039          * The IN_RENAME flag ensures that it cannot be moved by another
1040          * rename.
1041          */
1042         if (xp != ip) {
1043                 if (doingdirectory)
1044                         panic("ext2_rename: lost dir entry");
1045         } else {
1046                 /*
1047                  * If the source is a directory with a
1048                  * new parent, the link count of the old
1049                  * parent directory must be decremented
1050                  * and ".." set to point to the new parent.
1051                  */
1052                 if (doingdirectory && newparent) {
1053                         dp->i_nlink--;
1054                         dp->i_flag |= IN_CHANGE;
1055                         error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
1056                                 sizeof (struct dirtemplate), (off_t)0,
1057                                 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1058                                 tcnp->cn_cred, NOCRED, (int *)0,
1059                                 (struct thread *)0);
1060                         if (error == 0) {
1061                                 /* Like ufs little-endian: */
1062                                 namlen = dirbuf.dotdot_type;
1063                                 if (namlen != 2 ||
1064                                     dirbuf.dotdot_name[0] != '.' ||
1065                                     dirbuf.dotdot_name[1] != '.') {
1066                                         ext2_dirbad(xp, (doff_t)12,
1067                                             "rename: mangled dir");
1068                                 } else {
1069                                         dirbuf.dotdot_ino = newparent;
1070                                         (void) vn_rdwr(UIO_WRITE, fvp,
1071                                             (caddr_t)&dirbuf,
1072                                             sizeof (struct dirtemplate),
1073                                             (off_t)0, UIO_SYSSPACE,
1074                                             IO_NODELOCKED | IO_SYNC |
1075                                             IO_NOMACCHECK, tcnp->cn_cred,
1076                                             NOCRED, (int *)0,
1077                                             (struct thread *)0);
1078                                         cache_purge(fdvp);
1079                                 }
1080                         }
1081                 }
1082                 error = ext2_dirremove(fdvp, fcnp);
1083                 if (!error) {
1084                         xp->i_nlink--;
1085                         xp->i_flag |= IN_CHANGE;
1086                 }
1087                 xp->i_flag &= ~IN_RENAME;
1088         }
1089         if (dp)
1090                 vput(fdvp);
1091         if (xp)
1092                 vput(fvp);
1093         vrele(ap->a_fvp);
1094         return (error);
1095
1096 bad:
1097         if (xp)
1098                 vput(ITOV(xp));
1099         vput(ITOV(dp));
1100 out:
1101         if (doingdirectory)
1102                 ip->i_flag &= ~IN_RENAME;
1103         if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1104                 ip->i_nlink--;
1105                 ip->i_flag |= IN_CHANGE;
1106                 ip->i_flag &= ~IN_RENAME;
1107                 vput(fvp);
1108         } else
1109                 vrele(fvp);
1110         return (error);
1111 }
1112
1113 /*
1114  * Mkdir system call
1115  */
1116 static int
1117 ext2_mkdir(ap)
1118         struct vop_mkdir_args /* {
1119                 struct vnode *a_dvp;
1120                 struct vnode **a_vpp;
1121                 struct componentname *a_cnp;
1122                 struct vattr *a_vap;
1123         } */ *ap;
1124 {
1125         struct vnode *dvp = ap->a_dvp;
1126         struct vattr *vap = ap->a_vap;
1127         struct componentname *cnp = ap->a_cnp;
1128         struct inode *ip, *dp;
1129         struct vnode *tvp;
1130         struct dirtemplate dirtemplate, *dtp;
1131         int error, dmode;
1132
1133 #ifdef DIAGNOSTIC
1134         if ((cnp->cn_flags & HASBUF) == 0)
1135                 panic("ext2_mkdir: no name");
1136 #endif
1137         dp = VTOI(dvp);
1138         if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1139                 error = EMLINK;
1140                 goto out;
1141         }
1142         dmode = vap->va_mode & 0777;
1143         dmode |= IFDIR;
1144         /*
1145          * Must simulate part of ext2_makeinode here to acquire the inode,
1146          * but not have it entered in the parent directory. The entry is
1147          * made later after writing "." and ".." entries.
1148          */
1149         error = ext2_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1150         if (error)
1151                 goto out;
1152         ip = VTOI(tvp);
1153         ip->i_gid = dp->i_gid;
1154 #ifdef SUIDDIR
1155         {
1156                 /*
1157                  * if we are hacking owners here, (only do this where told to)
1158                  * and we are not giving it TOO root, (would subvert quotas)
1159                  * then go ahead and give it to the other user.
1160                  * The new directory also inherits the SUID bit. 
1161                  * If user's UID and dir UID are the same,
1162                  * 'give it away' so that the SUID is still forced on.
1163                  */
1164                 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1165                    (dp->i_mode & ISUID) && dp->i_uid) {
1166                         dmode |= ISUID;
1167                         ip->i_uid = dp->i_uid;
1168                 } else {
1169                         ip->i_uid = cnp->cn_cred->cr_uid;
1170                 }
1171         }
1172 #else
1173         ip->i_uid = cnp->cn_cred->cr_uid;
1174 #endif
1175         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1176         ip->i_mode = dmode;
1177         tvp->v_type = VDIR;     /* Rest init'd in getnewvnode(). */
1178         ip->i_nlink = 2;
1179         if (cnp->cn_flags & ISWHITEOUT)
1180                 ip->i_flags |= UF_OPAQUE;
1181         error = ext2_update(tvp, 1);
1182
1183         /*
1184          * Bump link count in parent directory
1185          * to reflect work done below.  Should
1186          * be done before reference is created
1187          * so reparation is possible if we crash.
1188          */
1189         dp->i_nlink++;
1190         dp->i_flag |= IN_CHANGE;
1191         error = ext2_update(dvp, 1);
1192         if (error)
1193                 goto bad;
1194
1195         /* Initialize directory with "." and ".." from static template. */
1196         if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs->s_es,
1197             EXT2_FEATURE_INCOMPAT_FILETYPE))
1198                 dtp = &mastertemplate;
1199         else
1200                 dtp = &omastertemplate;
1201         dirtemplate = *dtp;
1202         dirtemplate.dot_ino = ip->i_number;
1203         dirtemplate.dotdot_ino = dp->i_number;
1204         /* note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE 
1205          * so let's just redefine it - for this function only
1206          */
1207 #undef  DIRBLKSIZ 
1208 #define DIRBLKSIZ  VTOI(dvp)->i_e2fs->s_blocksize
1209         dirtemplate.dotdot_reclen = DIRBLKSIZ - 12;
1210         error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
1211             sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
1212             IO_NODELOCKED | IO_SYNC | IO_NOMACCHECK, cnp->cn_cred, NOCRED,
1213             (int *)0, (struct thread *)0);
1214         if (error) {
1215                 dp->i_nlink--;
1216                 dp->i_flag |= IN_CHANGE;
1217                 goto bad;
1218         }
1219         if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1220                 /* XXX should grow with balloc() */
1221                 panic("ext2_mkdir: blksize");
1222         else {
1223                 ip->i_size = DIRBLKSIZ;
1224                 ip->i_flag |= IN_CHANGE;
1225         }
1226
1227         /* Directory set up, now install its entry in the parent directory. */
1228         error = ext2_direnter(ip, dvp, cnp);
1229         if (error) {
1230                 dp->i_nlink--;
1231                 dp->i_flag |= IN_CHANGE;
1232         }
1233 bad:
1234         /*
1235          * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1236          * for us because we set the link count to 0.
1237          */
1238         if (error) {
1239                 ip->i_nlink = 0;
1240                 ip->i_flag |= IN_CHANGE;
1241                 vput(tvp);
1242         } else
1243                 *ap->a_vpp = tvp;
1244 out:
1245         return (error);
1246 #undef  DIRBLKSIZ
1247 #define DIRBLKSIZ  DEV_BSIZE
1248 }
1249
1250 /*
1251  * Rmdir system call.
1252  */
1253 static int
1254 ext2_rmdir(ap)
1255         struct vop_rmdir_args /* {
1256                 struct vnode *a_dvp;
1257                 struct vnode *a_vp;
1258                 struct componentname *a_cnp;
1259         } */ *ap;
1260 {
1261         struct vnode *vp = ap->a_vp;
1262         struct vnode *dvp = ap->a_dvp;
1263         struct componentname *cnp = ap->a_cnp;
1264         struct inode *ip, *dp;
1265         int error;
1266
1267         ip = VTOI(vp);
1268         dp = VTOI(dvp);
1269
1270         /*
1271          * Verify the directory is empty (and valid).
1272          * (Rmdir ".." won't be valid since
1273          *  ".." will contain a reference to
1274          *  the current directory and thus be
1275          *  non-empty.)
1276          */
1277         error = 0;
1278         if (ip->i_nlink != 2 || !ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1279                 error = ENOTEMPTY;
1280                 goto out;
1281         }
1282         if ((dp->i_flags & APPEND)
1283             || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1284                 error = EPERM;
1285                 goto out;
1286         }
1287         /*
1288          * Delete reference to directory before purging
1289          * inode.  If we crash in between, the directory
1290          * will be reattached to lost+found,
1291          */
1292         error = ext2_dirremove(dvp, cnp);
1293         if (error)
1294                 goto out;
1295         dp->i_nlink--;
1296         dp->i_flag |= IN_CHANGE;
1297         cache_purge(dvp);
1298         VOP_UNLOCK(dvp, 0);
1299         /*
1300          * Truncate inode.  The only stuff left
1301          * in the directory is "." and "..".  The
1302          * "." reference is inconsequential since
1303          * we're quashing it.  The ".." reference
1304          * has already been adjusted above.  We've
1305          * removed the "." reference and the reference
1306          * in the parent directory, but there may be
1307          * other hard links so decrement by 2 and
1308          * worry about them later.
1309          */
1310         ip->i_nlink -= 2;
1311         error = ext2_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred,
1312             cnp->cn_thread);
1313         cache_purge(ITOV(ip));
1314         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1315 out:
1316         return (error);
1317 }
1318
1319 /*
1320  * symlink -- make a symbolic link
1321  */
1322 static int
1323 ext2_symlink(ap)
1324         struct vop_symlink_args /* {
1325                 struct vnode *a_dvp;
1326                 struct vnode **a_vpp;
1327                 struct componentname *a_cnp;
1328                 struct vattr *a_vap;
1329                 char *a_target;
1330         } */ *ap;
1331 {
1332         struct vnode *vp, **vpp = ap->a_vpp;
1333         struct inode *ip;
1334         int len, error;
1335
1336         error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1337             vpp, ap->a_cnp);
1338         if (error)
1339                 return (error);
1340         vp = *vpp;
1341         len = strlen(ap->a_target);
1342         if (len < vp->v_mount->mnt_maxsymlinklen) {
1343                 ip = VTOI(vp);
1344                 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1345                 ip->i_size = len;
1346                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1347         } else
1348                 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1349                     UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1350                     ap->a_cnp->cn_cred, NOCRED, (int *)0, (struct thread *)0);
1351         if (error)
1352                 vput(vp);
1353         return (error);
1354 }
1355
1356 /*
1357  * Return target name of a symbolic link
1358  */
1359 static int
1360 ext2_readlink(ap)
1361         struct vop_readlink_args /* {
1362                 struct vnode *a_vp;
1363                 struct uio *a_uio;
1364                 struct ucred *a_cred;
1365         } */ *ap;
1366 {
1367         struct vnode *vp = ap->a_vp;
1368         struct inode *ip = VTOI(vp);
1369         int isize;
1370
1371         isize = ip->i_size;
1372         if (isize < vp->v_mount->mnt_maxsymlinklen) {
1373                 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1374                 return (0);
1375         }
1376         return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1377 }
1378
1379 /*
1380  * Calculate the logical to physical mapping if not done already,
1381  * then call the device strategy routine.
1382  *
1383  * In order to be able to swap to a file, the ext2_bmaparray() operation may not
1384  * deadlock on memory.  See ext2_bmap() for details.
1385  */
1386 static int
1387 ext2_strategy(ap)
1388         struct vop_strategy_args /* {
1389                 struct vnode *a_vp;
1390                 struct buf *a_bp;
1391         } */ *ap;
1392 {
1393         struct buf *bp = ap->a_bp;
1394         struct vnode *vp = ap->a_vp;
1395         struct inode *ip;
1396         struct bufobj *bo;
1397         int32_t blkno;
1398         int error;
1399
1400         ip = VTOI(vp);
1401         if (vp->v_type == VBLK || vp->v_type == VCHR)
1402                 panic("ext2_strategy: spec");
1403         if (bp->b_blkno == bp->b_lblkno) {
1404                 error = ext2_bmaparray(vp, bp->b_lblkno, &blkno, NULL, NULL);
1405                 bp->b_blkno = blkno;
1406                 if (error) {
1407                         bp->b_error = error;
1408                         bp->b_ioflags |= BIO_ERROR;
1409                         bufdone(bp);
1410                         return (error);
1411                 }
1412                 if ((long)bp->b_blkno == -1)
1413                         vfs_bio_clrbuf(bp);
1414         }
1415         if ((long)bp->b_blkno == -1) {
1416                 bufdone(bp);
1417                 return (0);
1418         }
1419         bp->b_iooffset = dbtob(bp->b_blkno);
1420         bo = VFSTOEXT2(vp->v_mount)->um_bo;
1421         BO_STRATEGY(bo, bp);
1422         return (0);
1423 }
1424
1425 /*
1426  * Print out the contents of an inode.
1427  */
1428 static int
1429 ext2_print(ap)
1430         struct vop_print_args /* {
1431                 struct vnode *a_vp;
1432         } */ *ap;
1433 {
1434         struct vnode *vp = ap->a_vp;
1435         struct inode *ip = VTOI(vp);
1436
1437         vn_printf(ip->i_devvp, "\tino %lu", (u_long)ip->i_number);
1438         if (vp->v_type == VFIFO)
1439                 fifo_printinfo(vp);
1440         printf("\n");
1441         return (0);
1442 }
1443
1444 /*
1445  * Close wrapper for fifos.
1446  *
1447  * Update the times on the inode then do device close.
1448  */
1449 static int
1450 ext2fifo_close(ap)
1451         struct vop_close_args /* {
1452                 struct vnode *a_vp;
1453                 int  a_fflag;
1454                 struct ucred *a_cred;
1455                 struct thread *a_td;
1456         } */ *ap;
1457 {
1458         struct vnode *vp = ap->a_vp;
1459
1460         VI_LOCK(vp);
1461         if (vp->v_usecount > 1)
1462                 ext2_itimes(vp);
1463         VI_UNLOCK(vp);
1464         return (fifo_specops.vop_close(ap));
1465 }
1466
1467 /*
1468  * Kqfilter wrapper for fifos.
1469  *
1470  * Fall through to ext2 kqfilter routines if needed 
1471  */
1472 static int
1473 ext2fifo_kqfilter(ap)
1474         struct vop_kqfilter_args *ap;
1475 {
1476         int error;
1477
1478         error = fifo_specops.vop_kqfilter(ap);
1479         if (error)
1480                 error = ext2_kqfilter(ap);
1481         return (error);
1482 }
1483
1484 /*
1485  * Return POSIX pathconf information applicable to ext2 filesystems.
1486  */
1487 static int
1488 ext2_pathconf(ap)
1489         struct vop_pathconf_args /* {
1490                 struct vnode *a_vp;
1491                 int a_name;
1492                 int *a_retval;
1493         } */ *ap;
1494 {
1495
1496         switch (ap->a_name) {
1497         case _PC_LINK_MAX:
1498                 *ap->a_retval = LINK_MAX;
1499                 return (0);
1500         case _PC_NAME_MAX:
1501                 *ap->a_retval = NAME_MAX;
1502                 return (0);
1503         case _PC_PATH_MAX:
1504                 *ap->a_retval = PATH_MAX;
1505                 return (0);
1506         case _PC_PIPE_BUF:
1507                 *ap->a_retval = PIPE_BUF;
1508                 return (0);
1509         case _PC_CHOWN_RESTRICTED:
1510                 *ap->a_retval = 1;
1511                 return (0);
1512         case _PC_NO_TRUNC:
1513                 *ap->a_retval = 1;
1514                 return (0);
1515         default:
1516                 return (EINVAL);
1517         }
1518         /* NOTREACHED */
1519 }
1520
1521 /*
1522  * Vnode pointer to File handle
1523  */
1524 /* ARGSUSED */
1525 static int
1526 ext2_vptofh(ap)
1527         struct vop_vptofh_args /* {
1528                 struct vnode *a_vp;
1529                 struct fid *a_fhp;
1530         } */ *ap;
1531 {
1532         struct inode *ip;
1533         struct ufid *ufhp;
1534
1535         ip = VTOI(ap->a_vp);
1536         ufhp = (struct ufid *)ap->a_fhp;
1537         ufhp->ufid_len = sizeof(struct ufid);
1538         ufhp->ufid_ino = ip->i_number;
1539         ufhp->ufid_gen = ip->i_gen;
1540         return (0);
1541 }
1542
1543 /*
1544  * Initialize the vnode associated with a new inode, handle aliased
1545  * vnodes.
1546  */
1547 int
1548 ext2_vinit(mntp, fifoops, vpp)
1549         struct mount *mntp;
1550         struct vop_vector *fifoops;
1551         struct vnode **vpp;
1552 {
1553         struct inode *ip;
1554         struct vnode *vp;
1555
1556         vp = *vpp;
1557         ip = VTOI(vp);
1558         vp->v_type = IFTOVT(ip->i_mode);
1559         if (vp->v_type == VFIFO)
1560                 vp->v_op = fifoops;
1561
1562         if (ip->i_number == ROOTINO)
1563                 vp->v_vflag |= VV_ROOT;
1564         ip->i_modrev = init_va_filerev();
1565         *vpp = vp;
1566         return (0);
1567 }
1568
1569 /*
1570  * Allocate a new inode.
1571  */
1572 static int
1573 ext2_makeinode(mode, dvp, vpp, cnp)
1574         int mode;
1575         struct vnode *dvp;
1576         struct vnode **vpp;
1577         struct componentname *cnp;
1578 {
1579         struct inode *ip, *pdir;
1580         struct vnode *tvp;
1581         int error;
1582
1583         pdir = VTOI(dvp);
1584 #ifdef DIAGNOSTIC
1585         if ((cnp->cn_flags & HASBUF) == 0)
1586                 panic("ext2_makeinode: no name");
1587 #endif
1588         *vpp = NULL;
1589         if ((mode & IFMT) == 0)
1590                 mode |= IFREG;
1591
1592         error = ext2_valloc(dvp, mode, cnp->cn_cred, &tvp);
1593         if (error) {
1594                 return (error);
1595         }
1596         ip = VTOI(tvp);
1597         ip->i_gid = pdir->i_gid;
1598 #ifdef SUIDDIR
1599         {
1600                 /*
1601                  * if we are
1602                  * not the owner of the directory,
1603                  * and we are hacking owners here, (only do this where told to)
1604                  * and we are not giving it TOO root, (would subvert quotas)
1605                  * then go ahead and give it to the other user.
1606                  * Note that this drops off the execute bits for security.
1607                  */
1608                 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1609                      (pdir->i_mode & ISUID) &&
1610                      (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1611                         ip->i_uid = pdir->i_uid;
1612                         mode &= ~07111;
1613                 } else {
1614                         ip->i_uid = cnp->cn_cred->cr_uid;
1615                 }
1616         }
1617 #else
1618         ip->i_uid = cnp->cn_cred->cr_uid;
1619 #endif
1620         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1621         ip->i_mode = mode;
1622         tvp->v_type = IFTOVT(mode);     /* Rest init'd in getnewvnode(). */
1623         ip->i_nlink = 1;
1624         if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred)) {
1625                 if (priv_check_cred(cnp->cn_cred, PRIV_VFS_RETAINSUGID, 0))
1626                         ip->i_mode &= ~ISGID;
1627         }
1628
1629         if (cnp->cn_flags & ISWHITEOUT)
1630                 ip->i_flags |= UF_OPAQUE;
1631
1632         /*
1633          * Make sure inode goes to disk before directory entry.
1634          */
1635         error = ext2_update(tvp, 1);
1636         if (error)
1637                 goto bad;
1638         error = ext2_direnter(ip, dvp, cnp);
1639         if (error)
1640                 goto bad;
1641
1642         *vpp = tvp;
1643         return (0);
1644
1645 bad:
1646         /*
1647          * Write error occurred trying to update the inode
1648          * or the directory so must deallocate the inode.
1649          */
1650         ip->i_nlink = 0;
1651         ip->i_flag |= IN_CHANGE;
1652         vput(tvp);
1653         return (error);
1654 }
1655
1656 static struct filterops ext2read_filtops = 
1657         { 1, NULL, filt_ext2detach, filt_ext2read };
1658 static struct filterops ext2write_filtops = 
1659         { 1, NULL, filt_ext2detach, filt_ext2write };
1660 static struct filterops ext2vnode_filtops = 
1661         { 1, NULL, filt_ext2detach, filt_ext2vnode };
1662
1663 static int
1664 ext2_kqfilter(ap)
1665         struct vop_kqfilter_args /* {
1666                 struct vnode *a_vp;
1667                 struct knote *a_kn;
1668         } */ *ap;
1669 {
1670         struct vnode *vp = ap->a_vp;
1671         struct knote *kn = ap->a_kn;
1672
1673         switch (kn->kn_filter) {
1674         case EVFILT_READ:
1675                 kn->kn_fop = &ext2read_filtops;
1676                 break;
1677         case EVFILT_WRITE:
1678                 kn->kn_fop = &ext2write_filtops;
1679                 break;
1680         case EVFILT_VNODE:
1681                 kn->kn_fop = &ext2vnode_filtops;
1682                 break;
1683         default:
1684                 return (1);
1685         }
1686
1687         kn->kn_hook = (caddr_t)vp;
1688
1689         if (vp->v_pollinfo == NULL)
1690                 v_addpollinfo(vp);
1691         if (vp->v_pollinfo == NULL)
1692                 return ENOMEM;
1693         knlist_add(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
1694
1695         return (0);
1696 }
1697
1698 static void
1699 filt_ext2detach(struct knote *kn)
1700 {
1701         struct vnode *vp = (struct vnode *)kn->kn_hook;
1702
1703         KASSERT(vp->v_pollinfo != NULL, ("Mising v_pollinfo"));
1704         knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
1705 }
1706
1707 /*ARGSUSED*/
1708 static int
1709 filt_ext2read(struct knote *kn, long hint)
1710 {
1711         struct vnode *vp = (struct vnode *)kn->kn_hook;
1712         struct inode *ip = VTOI(vp);
1713
1714         /*
1715          * filesystem is gone, so set the EOF flag and schedule 
1716          * the knote for deletion.
1717          */
1718         if (hint == NOTE_REVOKE) {
1719                 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1720                 return (1);
1721         }
1722
1723         kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
1724         return (kn->kn_data != 0);
1725 }
1726
1727 /*ARGSUSED*/
1728 static int
1729 filt_ext2write(struct knote *kn, long hint)
1730 {
1731
1732         /*
1733          * filesystem is gone, so set the EOF flag and schedule 
1734          * the knote for deletion.
1735          */
1736         if (hint == NOTE_REVOKE)
1737                 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1738
1739         kn->kn_data = 0;
1740         return (1);
1741 }
1742
1743 static int
1744 filt_ext2vnode(struct knote *kn, long hint)
1745 {
1746
1747         if (kn->kn_sfflags & hint)
1748                 kn->kn_fflags |= hint;
1749         if (hint == NOTE_REVOKE) {
1750                 kn->kn_flags |= EV_EOF;
1751                 return (1);
1752         }
1753         return (kn->kn_fflags != 0);
1754 }