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