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