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