015d997f1ecf3ed908ed07e644a359e6e2b754ad
[dragonfly.git] / sys / vfs / gnu / 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  * 3. All advertising materials mentioning features or use of this software
25  *    must display the following acknowledgement:
26  *      This product includes software developed by the University of
27  *      California, Berkeley and its contributors.
28  * 4. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  *
44  *      @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
45  *      @(#)ext2_vnops.c        8.7 (Berkeley) 2/3/94
46  * $FreeBSD: src/sys/gnu/ext2fs/ext2_vnops.c,v 1.51.2.2 2003/01/02 17:26:18 bde Exp $
47  * $DragonFly: src/sys/vfs/gnu/ext2fs/ext2_vnops.c,v 1.4 2003/07/19 21:14:32 dillon Exp $
48  */
49
50 #include "opt_quota.h"
51 #include "opt_suiddir.h"
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/resourcevar.h>
56 #include <sys/kernel.h>
57 #include <sys/stat.h>
58 #include <sys/buf.h>
59 #include <sys/proc.h>
60 #include <sys/mount.h>
61 #include <sys/time.h>
62 #include <sys/vnode.h>
63 #include <sys/namei.h>
64
65 #include <vm/vm.h>
66 #include <vm/vm_extern.h>
67 #include <vm/vm_zone.h>
68 #include <vm/vnode_pager.h>
69
70 #include <sys/signalvar.h>
71 #include <ufs/ufs/dir.h>
72 #include <ufs/ufs/quota.h>
73 #include <ufs/ufs/inode.h>
74 #include <ufs/ufs/ufsmount.h>
75 #include <ufs/ufs/ufs_extern.h>
76
77 #include <gnu/ext2fs/ext2_fs_sb.h>
78 #include <gnu/ext2fs/fs.h>
79 #include <gnu/ext2fs/ext2_extern.h>
80 #include <gnu/ext2fs/ext2_fs.h>
81
82 static int ext2_makeinode __P((int mode, struct vnode *, struct vnode **, struct componentname *));
83
84 static int ext2_fsync __P((struct vop_fsync_args *));
85 static int ext2_read __P((struct vop_read_args *));
86 static int ext2_write __P((struct vop_write_args *));
87 static int ext2_remove __P((struct vop_remove_args *));
88 static int ext2_link __P((struct vop_link_args *));
89 static int ext2_rename __P((struct vop_rename_args *));
90 static int ext2_mkdir __P((struct vop_mkdir_args *));
91 static int ext2_rmdir __P((struct vop_rmdir_args *));
92 static int ext2_create __P((struct vop_create_args *));
93 static int ext2_mknod __P((struct vop_mknod_args *));
94 static int ext2_symlink __P((struct vop_symlink_args *));
95 static int ext2_getpages __P((struct vop_getpages_args *));
96 static int ext2_putpages __P((struct vop_putpages_args *));
97
98 /* Global vfs data structures for ufs. */
99 vop_t **ext2_vnodeop_p;
100 static struct vnodeopv_entry_desc ext2_vnodeop_entries[] = {
101         { &vop_default_desc,            (vop_t *) ufs_vnoperate },
102         { &vop_cachedlookup_desc,       (vop_t *) ext2_lookup },
103         { &vop_fsync_desc,              (vop_t *) ext2_fsync },
104         { &vop_inactive_desc,           (vop_t *) ext2_inactive },
105         { &vop_lookup_desc,             (vop_t *) vfs_cache_lookup },
106         { &vop_read_desc,               (vop_t *) ext2_read },
107         { &vop_readdir_desc,            (vop_t *) ext2_readdir },
108         { &vop_reallocblks_desc,        (vop_t *) ext2_reallocblks },
109         { &vop_write_desc,              (vop_t *) ext2_write },
110         { &vop_remove_desc,             (vop_t *) ext2_remove },
111         { &vop_link_desc,               (vop_t *) ext2_link },
112         { &vop_rename_desc,             (vop_t *) ext2_rename },
113         { &vop_mkdir_desc,              (vop_t *) ext2_mkdir },
114         { &vop_rmdir_desc,              (vop_t *) ext2_rmdir },
115         { &vop_create_desc,             (vop_t *) ext2_create },
116         { &vop_mknod_desc,              (vop_t *) ext2_mknod },
117         { &vop_symlink_desc,            (vop_t *) ext2_symlink },
118         { &vop_getpages_desc,           (vop_t *) ext2_getpages },
119         { &vop_putpages_desc,           (vop_t *) ext2_putpages },
120         { NULL, NULL }
121 };
122 static struct vnodeopv_desc ext2fs_vnodeop_opv_desc =
123         { &ext2_vnodeop_p, ext2_vnodeop_entries };
124
125 vop_t **ext2_specop_p;
126 static struct vnodeopv_entry_desc ext2_specop_entries[] = {
127         { &vop_default_desc,            (vop_t *) ufs_vnoperatespec },
128         { &vop_fsync_desc,              (vop_t *) ext2_fsync },
129         { &vop_inactive_desc,           (vop_t *) ext2_inactive },
130         { NULL, NULL }
131 };
132 static struct vnodeopv_desc ext2fs_specop_opv_desc =
133         { &ext2_specop_p, ext2_specop_entries };
134
135 vop_t **ext2_fifoop_p;
136 static struct vnodeopv_entry_desc ext2_fifoop_entries[] = {
137         { &vop_default_desc,            (vop_t *) ufs_vnoperatefifo },
138         { &vop_fsync_desc,              (vop_t *) ext2_fsync },
139         { &vop_inactive_desc,           (vop_t *) ext2_inactive },
140         { NULL, NULL }
141 };
142 static struct vnodeopv_desc ext2fs_fifoop_opv_desc =
143         { &ext2_fifoop_p, ext2_fifoop_entries };
144
145         VNODEOP_SET(ext2fs_vnodeop_opv_desc);
146         VNODEOP_SET(ext2fs_specop_opv_desc);
147         VNODEOP_SET(ext2fs_fifoop_opv_desc);
148
149 #include <gnu/ext2fs/ext2_readwrite.c>
150
151 /*
152  * A virgin directory (no blushing please).
153  * Note that the type and namlen fields are reversed relative to ufs.
154  * Also, we don't use `struct odirtemplate', since it would just cause
155  * endianness problems.
156  */
157 static struct dirtemplate mastertemplate = {
158         0, 12, 1, EXT2_FT_DIR, ".",
159         0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
160 };
161 static struct dirtemplate omastertemplate = {
162         0, 12, 1, EXT2_FT_UNKNOWN, ".",
163         0, DIRBLKSIZ - 12, 2, EXT2_FT_UNKNOWN, ".."
164 };
165
166 /*
167  * Create a regular file
168  */
169 static int
170 ext2_create(ap)
171         struct vop_create_args /* {
172                 struct vnode *a_dvp;
173                 struct vnode **a_vpp;
174                 struct componentname *a_cnp;
175                 struct vattr *a_vap;
176         } */ *ap;
177 {
178         int error;
179
180         error =
181             ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
182             ap->a_dvp, ap->a_vpp, ap->a_cnp);
183         if (error)
184                 return (error);
185         return (0);
186 }
187
188 /*
189  * Synch an open file.
190  */
191 /* ARGSUSED */
192 static int
193 ext2_fsync(ap)
194         struct vop_fsync_args /* {
195                 struct vnode *a_vp;
196                 struct ucred *a_cred;
197                 int a_waitfor;
198                 struct proc *a_p;
199         } */ *ap;
200 {
201         register struct vnode *vp = ap->a_vp;
202         register struct buf *bp;
203         struct buf *nbp;
204         int s;
205
206         /* 
207          * XXX why is all this fs specific?
208          */
209
210         /*
211          * Flush all dirty buffers associated with a vnode.
212          */
213         ext2_discard_prealloc(VTOI(vp));
214
215 loop:
216         s = splbio();
217         for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
218                 nbp = TAILQ_NEXT(bp, b_vnbufs);
219                 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
220                         continue;
221                 if ((bp->b_flags & B_DELWRI) == 0)
222                         panic("ext2_fsync: not dirty");
223                 bremfree(bp);
224                 splx(s);
225                 /*
226                  * Wait for I/O associated with indirect blocks to complete,
227                  * since there is no way to quickly wait for them below.
228                  */
229                 if (bp->b_vp == vp || ap->a_waitfor == MNT_NOWAIT)
230                         (void) bawrite(bp);
231                 else
232                         (void) bwrite(bp);
233                 goto loop;
234         }
235         if (ap->a_waitfor == MNT_WAIT) {
236                 while (vp->v_numoutput) {
237                         vp->v_flag |= VBWAIT;
238                         tsleep(&vp->v_numoutput, 0, "e2fsyn", 0);
239                 }
240 #if DIAGNOSTIC
241                 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
242                         vprint("ext2_fsync: dirty", vp);
243                         goto loop;
244                 }
245 #endif
246         }
247         splx(s);
248         return (UFS_UPDATE(ap->a_vp, ap->a_waitfor == MNT_WAIT));
249 }
250
251 /*
252  * Mknod vnode call
253  */
254 /* ARGSUSED */
255 static int
256 ext2_mknod(ap)
257         struct vop_mknod_args /* {
258                 struct vnode *a_dvp;
259                 struct vnode **a_vpp;
260                 struct componentname *a_cnp;
261                 struct vattr *a_vap;
262         } */ *ap;
263 {
264         struct vattr *vap = ap->a_vap;
265         struct vnode **vpp = ap->a_vpp;
266         struct inode *ip;
267         ino_t ino;
268         int error;
269
270         error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
271             ap->a_dvp, vpp, ap->a_cnp);
272         if (error)
273                 return (error);
274         ip = VTOI(*vpp);
275         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
276         if (vap->va_rdev != VNOVAL) {
277                 /*
278                  * Want to be able to use this to make badblock
279                  * inodes, so don't truncate the dev number.
280                  */
281                 ip->i_rdev = vap->va_rdev;
282         }
283         /*
284          * Remove inode, then reload it through VFS_VGET so it is
285          * checked to see if it is an alias of an existing entry in
286          * the inode cache.
287          */
288         vput(*vpp);
289         (*vpp)->v_type = VNON;
290         ino = ip->i_number;     /* Save this before vgone() invalidates ip. */
291         vgone(*vpp);
292         error = VFS_VGET(ap->a_dvp->v_mount, ino, vpp);
293         if (error) {
294                 *vpp = NULL;
295                 return (error);
296         }
297         return (0);
298 }
299
300 static int
301 ext2_remove(ap)
302         struct vop_remove_args /* {
303                 struct vnode *a_dvp;
304                 struct vnode *a_vp;
305                 struct componentname *a_cnp;
306         } */ *ap;
307 {
308         struct inode *ip;
309         struct vnode *vp = ap->a_vp;
310         struct vnode *dvp = ap->a_dvp;
311         int error;
312
313         ip = VTOI(vp);
314         if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
315             (VTOI(dvp)->i_flags & APPEND)) {
316                 error = EPERM;
317                 goto out;
318         }
319         error = ext2_dirremove(dvp, ap->a_cnp);
320         if (error == 0) {
321                 ip->i_nlink--;
322                 ip->i_flag |= IN_CHANGE;
323         }
324 out:
325         return (error);
326 }
327
328 /*
329  * link vnode call
330  */
331 static int
332 ext2_link(ap)
333         struct vop_link_args /* {
334                 struct vnode *a_tdvp;
335                 struct vnode *a_vp;
336                 struct componentname *a_cnp;
337         } */ *ap;
338 {
339         struct vnode *vp = ap->a_vp;
340         struct vnode *tdvp = ap->a_tdvp;
341         struct componentname *cnp = ap->a_cnp;
342         struct proc *p = cnp->cn_proc;
343         struct inode *ip;
344         int error;
345
346 #ifdef DIAGNOSTIC
347         if ((cnp->cn_flags & HASBUF) == 0)
348                 panic("ufs_link: no name");
349 #endif
350         if (tdvp->v_mount != vp->v_mount) {
351                 error = EXDEV;
352                 goto out2;
353         }
354         if (tdvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE, p))) {
355                 goto out2;
356         }
357         ip = VTOI(vp);
358         if ((nlink_t)ip->i_nlink >= LINK_MAX) {
359                 error = EMLINK;
360                 goto out1;
361         }
362         if (ip->i_flags & (IMMUTABLE | APPEND)) {
363                 error = EPERM;
364                 goto out1;
365         }
366         ip->i_nlink++;
367         ip->i_flag |= IN_CHANGE;
368         error = UFS_UPDATE(vp, 1);
369         if (!error)
370                 error = ext2_direnter(ip, tdvp, cnp);
371         if (error) {
372                 ip->i_nlink--;
373                 ip->i_flag |= IN_CHANGE;
374         }
375 out1:
376         if (tdvp != vp)
377                 VOP_UNLOCK(vp, 0, p);
378 out2:
379         return (error);
380 }
381
382 /*
383  * Rename system call.
384  *   See comments in sys/ufs/ufs/ufs_vnops.c
385  */
386 static int
387 ext2_rename(ap)
388         struct vop_rename_args  /* {
389                 struct vnode *a_fdvp;
390                 struct vnode *a_fvp;
391                 struct componentname *a_fcnp;
392                 struct vnode *a_tdvp;
393                 struct vnode *a_tvp;
394                 struct componentname *a_tcnp;
395         } */ *ap;
396 {
397         struct vnode *tvp = ap->a_tvp;
398         register struct vnode *tdvp = ap->a_tdvp;
399         struct vnode *fvp = ap->a_fvp;
400         struct vnode *fdvp = ap->a_fdvp;
401         struct componentname *tcnp = ap->a_tcnp;
402         struct componentname *fcnp = ap->a_fcnp;
403         struct proc *p = fcnp->cn_proc;
404         struct inode *ip, *xp, *dp;
405         struct dirtemplate dirbuf;
406         int doingdirectory = 0, oldparent = 0, newparent = 0;
407         int error = 0;
408         u_char namlen;
409
410 #ifdef DIAGNOSTIC
411         if ((tcnp->cn_flags & HASBUF) == 0 ||
412             (fcnp->cn_flags & HASBUF) == 0)
413                 panic("ufs_rename: no name");
414 #endif
415         /*
416          * Check for cross-device rename.
417          */
418         if ((fvp->v_mount != tdvp->v_mount) ||
419             (tvp && (fvp->v_mount != tvp->v_mount))) {
420                 error = EXDEV;
421 abortit:
422                 if (tdvp == tvp)
423                         vrele(tdvp);
424                 else
425                         vput(tdvp);
426                 if (tvp)
427                         vput(tvp);
428                 vrele(fdvp);
429                 vrele(fvp);
430                 return (error);
431         }
432
433         if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
434             (VTOI(tdvp)->i_flags & APPEND))) {
435                 error = EPERM;
436                 goto abortit;
437         }
438
439         /*
440          * Renaming a file to itself has no effect.  The upper layers should
441          * not call us in that case.  Temporarily just warn if they do.
442          */
443         if (fvp == tvp) {
444                 printf("ext2_rename: fvp == tvp (can't happen)\n");
445                 error = 0;
446                 goto abortit;
447         }
448
449         if ((error = vn_lock(fvp, LK_EXCLUSIVE, p)) != 0)
450                 goto abortit;
451         dp = VTOI(fdvp);
452         ip = VTOI(fvp);
453         if (ip->i_nlink >= LINK_MAX) {
454                 VOP_UNLOCK(fvp, 0, p);
455                 error = EMLINK;
456                 goto abortit;
457         }
458         if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
459             || (dp->i_flags & APPEND)) {
460                 VOP_UNLOCK(fvp, 0, p);
461                 error = EPERM;
462                 goto abortit;
463         }
464         if ((ip->i_mode & IFMT) == IFDIR) {
465                 /*
466                  * Avoid ".", "..", and aliases of "." for obvious reasons.
467                  */
468                 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
469                     dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
470                     (ip->i_flag & IN_RENAME)) {
471                         VOP_UNLOCK(fvp, 0, p);
472                         error = EINVAL;
473                         goto abortit;
474                 }
475                 ip->i_flag |= IN_RENAME;
476                 oldparent = dp->i_number;
477                 doingdirectory++;
478         }
479         vrele(fdvp);
480
481         /*
482          * When the target exists, both the directory
483          * and target vnodes are returned locked.
484          */
485         dp = VTOI(tdvp);
486         xp = NULL;
487         if (tvp)
488                 xp = VTOI(tvp);
489
490         /*
491          * 1) Bump link count while we're moving stuff
492          *    around.  If we crash somewhere before
493          *    completing our work, the link count
494          *    may be wrong, but correctable.
495          */
496         ip->i_nlink++;
497         ip->i_flag |= IN_CHANGE;
498         if ((error = UFS_UPDATE(fvp, 1)) != 0) {
499                 VOP_UNLOCK(fvp, 0, p);
500                 goto bad;
501         }
502
503         /*
504          * If ".." must be changed (ie the directory gets a new
505          * parent) then the source directory must not be in the
506          * directory heirarchy above the target, as this would
507          * orphan everything below the source directory. Also
508          * the user must have write permission in the source so
509          * as to be able to change "..". We must repeat the call
510          * to namei, as the parent directory is unlocked by the
511          * call to checkpath().
512          */
513         error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
514         VOP_UNLOCK(fvp, 0, p);
515         if (oldparent != dp->i_number)
516                 newparent = dp->i_number;
517         if (doingdirectory && newparent) {
518                 if (error)      /* write access check above */
519                         goto bad;
520                 if (xp != NULL)
521                         vput(tvp);
522                 error = ext2_checkpath(ip, dp, tcnp->cn_cred);
523                 if (error)
524                         goto out;
525                 VREF(tdvp);
526                 error = relookup(tdvp, &tvp, tcnp);
527                 if (error)
528                         goto out;
529                 vrele(tdvp);
530                 dp = VTOI(tdvp);
531                 xp = NULL;
532                 if (tvp)
533                         xp = VTOI(tvp);
534         }
535         /*
536          * 2) If target doesn't exist, link the target
537          *    to the source and unlink the source.
538          *    Otherwise, rewrite the target directory
539          *    entry to reference the source inode and
540          *    expunge the original entry's existence.
541          */
542         if (xp == NULL) {
543                 if (dp->i_dev != ip->i_dev)
544                         panic("ufs_rename: EXDEV");
545                 /*
546                  * Account for ".." in new directory.
547                  * When source and destination have the same
548                  * parent we don't fool with the link count.
549                  */
550                 if (doingdirectory && newparent) {
551                         if ((nlink_t)dp->i_nlink >= LINK_MAX) {
552                                 error = EMLINK;
553                                 goto bad;
554                         }
555                         dp->i_nlink++;
556                         dp->i_flag |= IN_CHANGE;
557                         error = UFS_UPDATE(tdvp, 1);
558                         if (error)
559                                 goto bad;
560                 }
561                 error = ext2_direnter(ip, tdvp, tcnp);
562                 if (error) {
563                         if (doingdirectory && newparent) {
564                                 dp->i_nlink--;
565                                 dp->i_flag |= IN_CHANGE;
566                                 (void)UFS_UPDATE(tdvp, 1);
567                         }
568                         goto bad;
569                 }
570                 vput(tdvp);
571         } else {
572                 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
573                         panic("ufs_rename: EXDEV");
574                 /*
575                  * Short circuit rename(foo, foo).
576                  */
577                 if (xp->i_number == ip->i_number)
578                         panic("ufs_rename: same file");
579                 /*
580                  * If the parent directory is "sticky", then the user must
581                  * own the parent directory, or the destination of the rename,
582                  * otherwise the destination may not be changed (except by
583                  * root). This implements append-only directories.
584                  */
585                 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
586                     tcnp->cn_cred->cr_uid != dp->i_uid &&
587                     xp->i_uid != tcnp->cn_cred->cr_uid) {
588                         error = EPERM;
589                         goto bad;
590                 }
591                 /*
592                  * Target must be empty if a directory and have no links
593                  * to it. Also, ensure source and target are compatible
594                  * (both directories, or both not directories).
595                  */
596                 if ((xp->i_mode&IFMT) == IFDIR) {
597                         if (! ext2_dirempty(xp, dp->i_number, tcnp->cn_cred) || 
598                             xp->i_nlink > 2) {
599                                 error = ENOTEMPTY;
600                                 goto bad;
601                         }
602                         if (!doingdirectory) {
603                                 error = ENOTDIR;
604                                 goto bad;
605                         }
606                         cache_purge(tdvp);
607                 } else if (doingdirectory) {
608                         error = EISDIR;
609                         goto bad;
610                 }
611                 error = ext2_dirrewrite(dp, ip, tcnp);
612                 if (error)
613                         goto bad;
614                 /*
615                  * If the target directory is in the same
616                  * directory as the source directory,
617                  * decrement the link count on the parent
618                  * of the target directory.
619                  */
620                  if (doingdirectory && !newparent) {
621                         dp->i_nlink--;
622                         dp->i_flag |= IN_CHANGE;
623                 }
624                 vput(tdvp);
625                 /*
626                  * Adjust the link count of the target to
627                  * reflect the dirrewrite above.  If this is
628                  * a directory it is empty and there are
629                  * no links to it, so we can squash the inode and
630                  * any space associated with it.  We disallowed
631                  * renaming over top of a directory with links to
632                  * it above, as the remaining link would point to
633                  * a directory without "." or ".." entries.
634                  */
635                 xp->i_nlink--;
636                 if (doingdirectory) {
637                         if (--xp->i_nlink != 0)
638                                 panic("ufs_rename: linked directory");
639                         error = UFS_TRUNCATE(tvp, (off_t)0, IO_SYNC,
640                             tcnp->cn_cred, tcnp->cn_proc);
641                 }
642                 xp->i_flag |= IN_CHANGE;
643                 vput(tvp);
644                 xp = NULL;
645         }
646
647         /*
648          * 3) Unlink the source.
649          */
650         fcnp->cn_flags &= ~MODMASK;
651         fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
652         VREF(fdvp);
653         error = relookup(fdvp, &fvp, fcnp);
654         if (error == 0)
655                 vrele(fdvp);
656         if (fvp != NULL) {
657                 xp = VTOI(fvp);
658                 dp = VTOI(fdvp);
659         } else {
660                 /*
661                  * From name has disappeared.
662                  */
663                 if (doingdirectory)
664                         panic("ufs_rename: lost dir entry");
665                 vrele(ap->a_fvp);
666                 return (0);
667         }
668         /*
669          * Ensure that the directory entry still exists and has not
670          * changed while the new name has been entered. If the source is
671          * a file then the entry may have been unlinked or renamed. In
672          * either case there is no further work to be done. If the source
673          * is a directory then it cannot have been rmdir'ed; its link
674          * count of three would cause a rmdir to fail with ENOTEMPTY.
675          * The IN_RENAME flag ensures that it cannot be moved by another
676          * rename.
677          */
678         if (xp != ip) {
679                 if (doingdirectory)
680                         panic("ufs_rename: lost dir entry");
681         } else {
682                 /*
683                  * If the source is a directory with a
684                  * new parent, the link count of the old
685                  * parent directory must be decremented
686                  * and ".." set to point to the new parent.
687                  */
688                 if (doingdirectory && newparent) {
689                         dp->i_nlink--;
690                         dp->i_flag |= IN_CHANGE;
691                         error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
692                                 sizeof (struct dirtemplate), (off_t)0,
693                                 UIO_SYSSPACE, IO_NODELOCKED,
694                                 tcnp->cn_cred, (int *)0, (struct proc *)0);
695                         if (error == 0) {
696                                 /* Like ufs little-endian: */
697                                 namlen = dirbuf.dotdot_type;
698                                 if (namlen != 2 ||
699                                     dirbuf.dotdot_name[0] != '.' ||
700                                     dirbuf.dotdot_name[1] != '.') {
701                                         ufs_dirbad(xp, (doff_t)12,
702                                             "rename: mangled dir");
703                                 } else {
704                                         dirbuf.dotdot_ino = newparent;
705                                         (void) vn_rdwr(UIO_WRITE, fvp,
706                                             (caddr_t)&dirbuf,
707                                             sizeof (struct dirtemplate),
708                                             (off_t)0, UIO_SYSSPACE,
709                                             IO_NODELOCKED|IO_SYNC,
710                                             tcnp->cn_cred, (int *)0,
711                                             (struct proc *)0);
712                                         cache_purge(fdvp);
713                                 }
714                         }
715                 }
716                 error = ext2_dirremove(fdvp, fcnp);
717                 if (!error) {
718                         xp->i_nlink--;
719                         xp->i_flag |= IN_CHANGE;
720                 }
721                 xp->i_flag &= ~IN_RENAME;
722         }
723         if (dp)
724                 vput(fdvp);
725         if (xp)
726                 vput(fvp);
727         vrele(ap->a_fvp);
728         return (error);
729
730 bad:
731         if (xp)
732                 vput(ITOV(xp));
733         vput(ITOV(dp));
734 out:
735         if (doingdirectory)
736                 ip->i_flag &= ~IN_RENAME;
737         if (vn_lock(fvp, LK_EXCLUSIVE, p) == 0) {
738                 ip->i_nlink--;
739                 ip->i_flag |= IN_CHANGE;
740                 ip->i_flag &= ~IN_RENAME;
741                 vput(fvp);
742         } else
743                 vrele(fvp);
744         return (error);
745 }
746
747 /*
748  * Mkdir system call
749  */
750 static int
751 ext2_mkdir(ap)
752         struct vop_mkdir_args /* {
753                 struct vnode *a_dvp;
754                 struct vnode **a_vpp;
755                 struct componentname *a_cnp;
756                 struct vattr *a_vap;
757         } */ *ap;
758 {
759         register struct vnode *dvp = ap->a_dvp;
760         register struct vattr *vap = ap->a_vap;
761         register struct componentname *cnp = ap->a_cnp;
762         register struct inode *ip, *dp;
763         struct vnode *tvp;
764         struct dirtemplate dirtemplate, *dtp;
765         int error, dmode;
766
767 #ifdef DIAGNOSTIC
768         if ((cnp->cn_flags & HASBUF) == 0)
769                 panic("ufs_mkdir: no name");
770 #endif
771         dp = VTOI(dvp);
772         if ((nlink_t)dp->i_nlink >= LINK_MAX) {
773                 error = EMLINK;
774                 goto out;
775         }
776         dmode = vap->va_mode & 0777;
777         dmode |= IFDIR;
778         /*
779          * Must simulate part of ext2_makeinode here to acquire the inode,
780          * but not have it entered in the parent directory. The entry is
781          * made later after writing "." and ".." entries.
782          */
783         error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
784         if (error)
785                 goto out;
786         ip = VTOI(tvp);
787         ip->i_gid = dp->i_gid;
788 #ifdef SUIDDIR
789         {
790 #ifdef QUOTA
791                 struct ucred ucred, *ucp;
792                 ucp = cnp->cn_cred;
793 #endif                  I
794                 /*
795                  * if we are hacking owners here, (only do this where told to)
796                  * and we are not giving it TOO root, (would subvert quotas)
797                  * then go ahead and give it to the other user.
798                  * The new directory also inherits the SUID bit. 
799                  * If user's UID and dir UID are the same,
800                  * 'give it away' so that the SUID is still forced on.
801                  */
802                 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
803                    (dp->i_mode & ISUID) && dp->i_uid) {
804                         dmode |= ISUID;
805                         ip->i_uid = dp->i_uid;
806 #ifdef QUOTA
807                         if (dp->i_uid != cnp->cn_cred->cr_uid) {
808                                 /*
809                                  * make sure the correct user gets charged
810                                  * for the space.
811                                  * Make a dummy credential for the victim.
812                                  * XXX This seems to never be accessed out of
813                                  * our context so a stack variable is ok.
814                                  */
815                                 ucred.cr_ref = 1;
816                                 ucred.cr_uid = ip->i_uid;
817                                 ucred.cr_ngroups = 1;
818                                 ucred.cr_groups[0] = dp->i_gid;
819                                 ucp = &ucred;
820                         }
821 #endif                  I
822                 } else {
823                         ip->i_uid = cnp->cn_cred->cr_uid;
824                 }
825 #ifdef QUOTA
826                 if ((error = getinoquota(ip)) ||
827                 (error = chkiq(ip, 1, ucp, 0))) {
828                         UFS_VFREE(tvp, ip->i_number, dmode);
829                         vput(tvp);
830                         return (error);
831                 }
832 #endif
833         }
834 #else
835         ip->i_uid = cnp->cn_cred->cr_uid;
836 #ifdef QUOTA
837         if ((error = getinoquota(ip)) ||
838             (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
839                 UFS_VFREE(tvp, ip->i_number, dmode);
840                 vput(tvp);
841                 return (error);
842         }
843 #endif
844 #endif
845         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
846         ip->i_mode = dmode;
847         tvp->v_type = VDIR;     /* Rest init'd in getnewvnode(). */
848         ip->i_nlink = 2;
849         if (cnp->cn_flags & ISWHITEOUT)
850                 ip->i_flags |= UF_OPAQUE;
851         error = UFS_UPDATE(tvp, 1);
852
853         /*
854          * Bump link count in parent directory
855          * to reflect work done below.  Should
856          * be done before reference is created
857          * so reparation is possible if we crash.
858          */
859         dp->i_nlink++;
860         dp->i_flag |= IN_CHANGE;
861         error = UFS_UPDATE(dvp, 1);
862         if (error)
863                 goto bad;
864
865         /* Initialize directory with "." and ".." from static template. */
866         if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs->s_es,
867             EXT2_FEATURE_INCOMPAT_FILETYPE))
868                 dtp = &mastertemplate;
869         else
870                 dtp = &omastertemplate;
871         dirtemplate = *dtp;
872         dirtemplate.dot_ino = ip->i_number;
873         dirtemplate.dotdot_ino = dp->i_number;
874         /* note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE 
875          * so let's just redefine it - for this function only
876          */
877 #undef  DIRBLKSIZ 
878 #define DIRBLKSIZ  VTOI(dvp)->i_e2fs->s_blocksize
879         dirtemplate.dotdot_reclen = DIRBLKSIZ - 12;
880         error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
881             sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
882             IO_NODELOCKED|IO_SYNC, cnp->cn_cred, (int *)0, (struct proc *)0);
883         if (error) {
884                 dp->i_nlink--;
885                 dp->i_flag |= IN_CHANGE;
886                 goto bad;
887         }
888         if (DIRBLKSIZ > VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
889                 panic("ufs_mkdir: blksize"); /* XXX should grow with balloc() */
890         else {
891                 ip->i_size = DIRBLKSIZ;
892                 ip->i_flag |= IN_CHANGE;
893         }
894
895         /* Directory set up, now install its entry in the parent directory. */
896         error = ext2_direnter(ip, dvp, cnp);
897         if (error) {
898                 dp->i_nlink--;
899                 dp->i_flag |= IN_CHANGE;
900         }
901 bad:
902         /*
903          * No need to do an explicit VOP_TRUNCATE here, vrele will do this
904          * for us because we set the link count to 0.
905          */
906         if (error) {
907                 ip->i_nlink = 0;
908                 ip->i_flag |= IN_CHANGE;
909                 vput(tvp);
910         } else
911                 *ap->a_vpp = tvp;
912 out:
913         return (error);
914 #undef  DIRBLKSIZ
915 #define DIRBLKSIZ  DEV_BSIZE
916 }
917
918 /*
919  * Rmdir system call.
920  */
921 static int
922 ext2_rmdir(ap)
923         struct vop_rmdir_args /* {
924                 struct vnode *a_dvp;
925                 struct vnode *a_vp;
926                 struct componentname *a_cnp;
927         } */ *ap;
928 {
929         struct vnode *vp = ap->a_vp;
930         struct vnode *dvp = ap->a_dvp;
931         struct componentname *cnp = ap->a_cnp;
932         struct proc *p = cnp->cn_proc;
933         struct inode *ip, *dp;
934         int error;
935
936         ip = VTOI(vp);
937         dp = VTOI(dvp);
938
939         /*
940          * Verify the directory is empty (and valid).
941          * (Rmdir ".." won't be valid since
942          *  ".." will contain a reference to
943          *  the current directory and thus be
944          *  non-empty.)
945          */
946         error = 0;
947         if (ip->i_nlink != 2 || !ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
948                 error = ENOTEMPTY;
949                 goto out;
950         }
951         if ((dp->i_flags & APPEND)
952             || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
953                 error = EPERM;
954                 goto out;
955         }
956         /*
957          * Delete reference to directory before purging
958          * inode.  If we crash in between, the directory
959          * will be reattached to lost+found,
960          */
961         error = ext2_dirremove(dvp, cnp);
962         if (error)
963                 goto out;
964         dp->i_nlink--;
965         dp->i_flag |= IN_CHANGE;
966         cache_purge(dvp);
967         VOP_UNLOCK(dvp, 0, p);
968         /*
969          * Truncate inode.  The only stuff left
970          * in the directory is "." and "..".  The
971          * "." reference is inconsequential since
972          * we're quashing it.  The ".." reference
973          * has already been adjusted above.  We've
974          * removed the "." reference and the reference
975          * in the parent directory, but there may be
976          * other hard links so decrement by 2 and
977          * worry about them later.
978          */
979         ip->i_nlink -= 2;
980         error = UFS_TRUNCATE(vp, (off_t)0, IO_SYNC, cnp->cn_cred, p);
981         cache_purge(ITOV(ip));
982         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
983 out:
984         return (error);
985 }
986
987 /*
988  * symlink -- make a symbolic link
989  */
990 static int
991 ext2_symlink(ap)
992         struct vop_symlink_args /* {
993                 struct vnode *a_dvp;
994                 struct vnode **a_vpp;
995                 struct componentname *a_cnp;
996                 struct vattr *a_vap;
997                 char *a_target;
998         } */ *ap;
999 {
1000         register struct vnode *vp, **vpp = ap->a_vpp;
1001         register struct inode *ip;
1002         int len, error;
1003
1004         error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1005             vpp, ap->a_cnp);
1006         if (error)
1007                 return (error);
1008         vp = *vpp;
1009         len = strlen(ap->a_target);
1010         if (len < vp->v_mount->mnt_maxsymlinklen) {
1011                 ip = VTOI(vp);
1012                 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1013                 ip->i_size = len;
1014                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1015         } else
1016                 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1017                     UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred, (int *)0,
1018                     (struct proc *)0);
1019         if (error)
1020                 vput(vp);
1021         return (error);
1022 }
1023
1024 /*
1025  * Allocate a new inode.
1026  */
1027 static int
1028 ext2_makeinode(mode, dvp, vpp, cnp)
1029         int mode;
1030         struct vnode *dvp;
1031         struct vnode **vpp;
1032         struct componentname *cnp;
1033 {
1034         register struct inode *ip, *pdir;
1035         struct vnode *tvp;
1036         int error;
1037
1038         pdir = VTOI(dvp);
1039 #ifdef DIAGNOSTIC
1040         if ((cnp->cn_flags & HASBUF) == 0)
1041                 panic("ext2_makeinode: no name");
1042 #endif
1043         *vpp = NULL;
1044         if ((mode & IFMT) == 0)
1045                 mode |= IFREG;
1046
1047         error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
1048         if (error) {
1049                 return (error);
1050         }
1051         ip = VTOI(tvp);
1052         ip->i_gid = pdir->i_gid;
1053 #ifdef SUIDDIR
1054         {
1055 #ifdef QUOTA
1056                 struct ucred ucred, *ucp;
1057                 ucp = cnp->cn_cred;
1058 #endif                  I
1059                 /*
1060                  * if we are
1061                  * not the owner of the directory,
1062                  * and we are hacking owners here, (only do this where told to)
1063                  * and we are not giving it TOO root, (would subvert quotas)
1064                  * then go ahead and give it to the other user.
1065                  * Note that this drops off the execute bits for security.
1066                  */
1067                 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1068                      (pdir->i_mode & ISUID) &&
1069                      (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1070                         ip->i_uid = pdir->i_uid;
1071                         mode &= ~07111;
1072 #ifdef QUOTA
1073                         /*
1074                          * make sure the correct user gets charged
1075                          * for the space.
1076                          * Quickly knock up a dummy credential for the victim.
1077                          * XXX This seems to never be accessed out of our
1078                          * context so a stack variable is ok.
1079                          */
1080                         ucred.cr_ref = 1;
1081                         ucred.cr_uid = ip->i_uid;
1082                         ucred.cr_ngroups = 1;
1083                         ucred.cr_groups[0] = pdir->i_gid;
1084                         ucp = &ucred;
1085 #endif                  I
1086                 } else {
1087                         ip->i_uid = cnp->cn_cred->cr_uid;
1088                 }
1089         
1090 #ifdef QUOTA
1091                 if ((error = getinoquota(ip)) ||
1092                 (error = chkiq(ip, 1, ucp, 0))) {
1093                         UFS_VFREE(tvp, ip->i_number, mode);
1094                         vput(tvp);
1095                         return (error);
1096                 }
1097 #endif
1098         }
1099 #else
1100         ip->i_uid = cnp->cn_cred->cr_uid;
1101 #ifdef QUOTA
1102         if ((error = getinoquota(ip)) ||
1103             (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1104                 UFS_VFREE(tvp, ip->i_number, mode);
1105                 vput(tvp);
1106                 return (error);
1107         }
1108 #endif
1109 #endif
1110         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1111         ip->i_mode = mode;
1112         tvp->v_type = IFTOVT(mode);     /* Rest init'd in getnewvnode(). */
1113         ip->i_nlink = 1;
1114         if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
1115             suser_cred(cnp->cn_cred, PRISON_ROOT))
1116                 ip->i_mode &= ~ISGID;
1117
1118         if (cnp->cn_flags & ISWHITEOUT)
1119                 ip->i_flags |= UF_OPAQUE;
1120
1121         /*
1122          * Make sure inode goes to disk before directory entry.
1123          */
1124         error = UFS_UPDATE(tvp, 1);
1125         if (error)
1126                 goto bad;
1127         error = ext2_direnter(ip, dvp, cnp);
1128         if (error)
1129                 goto bad;
1130
1131         *vpp = tvp;
1132         return (0);
1133
1134 bad:
1135         /*
1136          * Write error occurred trying to update the inode
1137          * or the directory so must deallocate the inode.
1138          */
1139         ip->i_nlink = 0;
1140         ip->i_flag |= IN_CHANGE;
1141         vput(tvp);
1142         return (error);
1143 }
1144
1145 /*
1146  * get page routine
1147  *
1148  * XXX By default, wimp out... note that a_offset is ignored (and always
1149  * XXX has been).
1150  */
1151 static int
1152 ext2_getpages(ap)
1153         struct vop_getpages_args *ap;
1154 {
1155         return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
1156                 ap->a_reqpage));
1157 }
1158
1159 /*
1160  * put page routine
1161  *
1162  * XXX By default, wimp out... note that a_offset is ignored (and always
1163  * XXX has been).
1164  */
1165 static int
1166 ext2_putpages(ap)
1167         struct vop_putpages_args *ap;
1168 {
1169         return (vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
1170                 ap->a_sync, ap->a_rtvals));
1171 }