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