If softupdates or some other entity re-dirties a buffer, make sure
[dragonfly.git] / sys / vfs / ufs / ufs_vnops.c
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993, 1995
3  *      The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
39  * $FreeBSD: src/sys/ufs/ufs/ufs_vnops.c,v 1.131.2.8 2003/01/02 17:26:19 bde Exp $
40  * $DragonFly: src/sys/vfs/ufs/ufs_vnops.c,v 1.42 2006/04/07 06:38:33 dillon Exp $
41  */
42
43 #include "opt_quota.h"
44 #include "opt_suiddir.h"
45 #include "opt_ufs.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/fcntl.h>
51 #include <sys/stat.h>
52 #include <sys/buf.h>
53 #include <sys/proc.h>
54 #include <sys/namei.h>
55 #include <sys/mount.h>
56 #include <sys/unistd.h>
57 #include <sys/vnode.h>
58 #include <sys/malloc.h>
59 #include <sys/dirent.h>
60 #include <sys/lockf.h>
61 #include <sys/event.h>
62 #include <sys/conf.h>
63
64 #include <sys/file.h>           /* XXX */
65 #include <sys/jail.h>
66
67 #include <vm/vm.h>
68 #include <vm/vm_extern.h>
69
70 #include <vfs/fifofs/fifo.h>
71
72 #include "quota.h"
73 #include "inode.h"
74 #include "dir.h"
75 #include "ufsmount.h"
76 #include "ufs_extern.h"
77 #ifdef UFS_DIRHASH
78 #include "dirhash.h"
79 #endif
80
81 static int ufs_access (struct vop_access_args *);
82 static int ufs_advlock (struct vop_advlock_args *);
83 static int ufs_chmod (struct vnode *, int, struct ucred *, struct thread *);
84 static int ufs_chown (struct vnode *, uid_t, gid_t, struct ucred *, struct thread *);
85 static int ufs_close (struct vop_close_args *);
86 static int ufs_create (struct vop_old_create_args *);
87 static int ufs_getattr (struct vop_getattr_args *);
88 static int ufs_link (struct vop_old_link_args *);
89 static int ufs_makeinode (int mode, struct vnode *, struct vnode **, struct componentname *);
90 static int ufs_missingop (struct vop_generic_args *ap);
91 static int ufs_mkdir (struct vop_old_mkdir_args *);
92 static int ufs_mknod (struct vop_old_mknod_args *);
93 static int ufs_mmap (struct vop_mmap_args *);
94 static int ufs_open (struct vop_open_args *);
95 static int ufs_pathconf (struct vop_pathconf_args *);
96 static int ufs_print (struct vop_print_args *);
97 static int ufs_readdir (struct vop_readdir_args *);
98 static int ufs_readlink (struct vop_readlink_args *);
99 static int ufs_remove (struct vop_old_remove_args *);
100 static int ufs_rename (struct vop_old_rename_args *);
101 static int ufs_rmdir (struct vop_old_rmdir_args *);
102 static int ufs_setattr (struct vop_setattr_args *);
103 static int ufs_strategy (struct vop_strategy_args *);
104 static int ufs_symlink (struct vop_old_symlink_args *);
105 static int ufs_whiteout (struct vop_old_whiteout_args *);
106 static int ufsfifo_close (struct vop_close_args *);
107 static int ufsfifo_kqfilter (struct vop_kqfilter_args *);
108 static int ufsfifo_read (struct vop_read_args *);
109 static int ufsfifo_write (struct vop_write_args *);
110 static int ufsspec_close (struct vop_close_args *);
111 static int ufsspec_read (struct vop_read_args *);
112 static int ufsspec_write (struct vop_write_args *);
113 static int filt_ufsread (struct knote *kn, long hint);
114 static int filt_ufswrite (struct knote *kn, long hint);
115 static int filt_ufsvnode (struct knote *kn, long hint);
116 static void filt_ufsdetach (struct knote *kn);
117 static int ufs_kqfilter (struct vop_kqfilter_args *ap);
118
119 union _qcvt {
120         int64_t qcvt;
121         int32_t val[2];
122 };
123 #define SETHIGH(q, h) { \
124         union _qcvt tmp; \
125         tmp.qcvt = (q); \
126         tmp.val[_QUAD_HIGHWORD] = (h); \
127         (q) = tmp.qcvt; \
128 }
129 #define SETLOW(q, l) { \
130         union _qcvt tmp; \
131         tmp.qcvt = (q); \
132         tmp.val[_QUAD_LOWWORD] = (l); \
133         (q) = tmp.qcvt; \
134 }
135 #define VN_KNOTE(vp, b) \
136         KNOTE(&vp->v_pollinfo.vpi_selinfo.si_note, (b))
137
138 #define OFSFMT(vp)              ((vp)->v_mount->mnt_maxsymlinklen <= 0)
139
140 /*
141  * A virgin directory (no blushing please).
142  */
143 static struct dirtemplate mastertemplate = {
144         0, 12, DT_DIR, 1, ".",
145         0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
146 };
147 static struct odirtemplate omastertemplate = {
148         0, 12, 1, ".",
149         0, DIRBLKSIZ - 12, 2, ".."
150 };
151
152 void
153 ufs_itimes(struct vnode *vp)
154 {
155         struct inode *ip;
156         struct timespec ts;
157
158         ip = VTOI(vp);
159         if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
160                 return;
161         if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
162                 ip->i_flag |= IN_LAZYMOD;
163         else
164                 ip->i_flag |= IN_MODIFIED;
165         if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
166                 vfs_timestamp(&ts);
167                 if (ip->i_flag & IN_ACCESS) {
168                         ip->i_atime = ts.tv_sec;
169                         ip->i_atimensec = ts.tv_nsec;
170                 }
171                 if (ip->i_flag & IN_UPDATE) {
172                         ip->i_mtime = ts.tv_sec;
173                         ip->i_mtimensec = ts.tv_nsec;
174                         ip->i_modrev++;
175                 }
176                 if (ip->i_flag & IN_CHANGE) {
177                         ip->i_ctime = ts.tv_sec;
178                         ip->i_ctimensec = ts.tv_nsec;
179                 }
180         }
181         ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
182 }
183
184 /*
185  * Create a regular file
186  *
187  * ufs_create(struct vnode *a_dvp, struct vnode **a_vpp,
188  *            struct componentname *a_cnp, struct vattr *a_vap)
189  */
190 static
191 int
192 ufs_create(struct vop_old_create_args *ap)
193 {
194         int error;
195
196         error =
197             ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
198             ap->a_dvp, ap->a_vpp, ap->a_cnp);
199         if (error)
200                 return (error);
201         VN_KNOTE(ap->a_dvp, NOTE_WRITE);
202         return (0);
203 }
204
205 /*
206  * Mknod vnode call
207  *
208  * ufs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
209  *           struct componentname *a_cnp, struct vattr *a_vap)
210  */
211 /* ARGSUSED */
212 static
213 int
214 ufs_mknod(struct vop_old_mknod_args *ap)
215 {
216         struct vattr *vap = ap->a_vap;
217         struct vnode **vpp = ap->a_vpp;
218         struct inode *ip;
219         ino_t ino;
220         int error;
221
222         error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
223             ap->a_dvp, vpp, ap->a_cnp);
224         if (error)
225                 return (error);
226         VN_KNOTE(ap->a_dvp, NOTE_WRITE);
227         ip = VTOI(*vpp);
228         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
229         if (vap->va_rdev != VNOVAL) {
230                 /*
231                  * Want to be able to use this to make badblock
232                  * inodes, so don't truncate the dev number.
233                  */
234                 ip->i_rdev = vap->va_rdev;
235         }
236         /*
237          * Remove inode, then reload it through VFS_VGET so it is
238          * checked to see if it is an alias of an existing entry in
239          * the inode cache.
240          */
241         (*vpp)->v_type = VNON;
242         ino = ip->i_number;     /* Save this before vgone() invalidates ip. */
243         vgone(*vpp);
244         vput(*vpp);
245         error = VFS_VGET(ap->a_dvp->v_mount, ino, vpp);
246         if (error) {
247                 *vpp = NULL;
248                 return (error);
249         }
250         return (0);
251 }
252
253 /*
254  * Open called.
255  *
256  * Nothing to do.
257  *
258  * ufs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
259  *          struct thread *a_td)
260  */
261 /* ARGSUSED */
262 static
263 int
264 ufs_open(struct vop_open_args *ap)
265 {
266         struct vnode *vp = ap->a_vp;
267
268         /*
269          * Files marked append-only must be opened for appending.
270          */
271         if ((VTOI(vp)->i_flags & APPEND) &&
272             (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) {
273                 return (EPERM);
274         }
275
276         return (vop_stdopen(ap));
277 }
278
279 /*
280  * Close called.
281  *
282  * Update the times on the inode.
283  *
284  * ufs_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
285  *           struct thread *a_td)
286  */
287 /* ARGSUSED */
288 static
289 int
290 ufs_close(struct vop_close_args *ap)
291 {
292         struct vnode *vp = ap->a_vp;
293
294         if (vp->v_usecount > 1)
295                 ufs_itimes(vp);
296         return (vop_stdclose(ap));
297 }
298
299 /*
300  * ufs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
301  *            struct thread *a_td)
302  */
303 static
304 int
305 ufs_access(struct vop_access_args *ap)
306 {
307         struct vnode *vp = ap->a_vp;
308         struct inode *ip = VTOI(vp);
309         struct ucred *cred = ap->a_cred;
310         mode_t mask, mode = ap->a_mode;
311         gid_t *gp;
312         int i;
313 #ifdef QUOTA
314         int error;
315 #endif
316
317         /*
318          * Disallow write attempts on read-only filesystems;
319          * unless the file is a socket, fifo, or a block or
320          * character device resident on the filesystem.
321          */
322         if (mode & VWRITE) {
323                 switch (vp->v_type) {
324                 case VDIR:
325                 case VLNK:
326                 case VREG:
327                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
328                                 return (EROFS);
329 #ifdef QUOTA
330                         if ((error = ufs_getinoquota(ip)) != 0)
331                                 return (error);
332 #endif
333                         break;
334                 default:
335                         break;
336                 }
337         }
338
339         /* If immutable bit set, nobody gets to write it. */
340         if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE))
341                 return (EPERM);
342
343         /* Otherwise, user id 0 always gets access. */
344         if (cred->cr_uid == 0)
345                 return (0);
346
347         mask = 0;
348
349         /* Otherwise, check the owner. */
350         if (cred->cr_uid == ip->i_uid) {
351                 if (mode & VEXEC)
352                         mask |= S_IXUSR;
353                 if (mode & VREAD)
354                         mask |= S_IRUSR;
355                 if (mode & VWRITE)
356                         mask |= S_IWUSR;
357                 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
358         }
359
360         /* Otherwise, check the groups. */
361         for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
362                 if (ip->i_gid == *gp) {
363                         if (mode & VEXEC)
364                                 mask |= S_IXGRP;
365                         if (mode & VREAD)
366                                 mask |= S_IRGRP;
367                         if (mode & VWRITE)
368                                 mask |= S_IWGRP;
369                         return ((ip->i_mode & mask) == mask ? 0 : EACCES);
370                 }
371
372         /* Otherwise, check everyone else. */
373         if (mode & VEXEC)
374                 mask |= S_IXOTH;
375         if (mode & VREAD)
376                 mask |= S_IROTH;
377         if (mode & VWRITE)
378                 mask |= S_IWOTH;
379         return ((ip->i_mode & mask) == mask ? 0 : EACCES);
380 }
381
382 /*
383  * ufs_getattr(struct vnode *a_vp, struct vattr *a_vap,
384  *              struct thread *a_td)
385  */
386 /* ARGSUSED */
387 static
388 int
389 ufs_getattr(struct vop_getattr_args *ap)
390 {
391         struct vnode *vp = ap->a_vp;
392         struct inode *ip = VTOI(vp);
393         struct vattr *vap = ap->a_vap;
394
395         /*
396          * This may cause i_fsmid to be updated even if no change (0)
397          * is returned, but we should only write out the inode if non-zero
398          * is returned and if the mount is read-write.
399          */
400         if (cache_check_fsmid_vp(vp, &ip->i_fsmid) &&
401             (vp->v_mount->mnt_flag & MNT_RDONLY) == 0
402         ) {
403                 ip->i_flag |= IN_LAZYMOD;
404         }
405
406         ufs_itimes(vp);
407         /*
408          * Copy from inode table
409          */
410         vap->va_fsid = dev2udev(ip->i_dev);
411         vap->va_fileid = ip->i_number;
412         vap->va_mode = ip->i_mode & ~IFMT;
413         vap->va_nlink = VFSTOUFS(vp->v_mount)->um_i_effnlink_valid ?
414             ip->i_effnlink : ip->i_nlink;
415         vap->va_uid = ip->i_uid;
416         vap->va_gid = ip->i_gid;
417         vap->va_rdev = ip->i_rdev;
418         vap->va_size = ip->i_din.di_size;
419         vap->va_atime.tv_sec = ip->i_atime;
420         vap->va_atime.tv_nsec = ip->i_atimensec;
421         vap->va_mtime.tv_sec = ip->i_mtime;
422         vap->va_mtime.tv_nsec = ip->i_mtimensec;
423         vap->va_ctime.tv_sec = ip->i_ctime;
424         vap->va_ctime.tv_nsec = ip->i_ctimensec;
425         vap->va_flags = ip->i_flags;
426         vap->va_gen = ip->i_gen;
427         vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
428         vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
429         vap->va_type = IFTOVT(ip->i_mode);
430         vap->va_filerev = ip->i_modrev;
431         vap->va_fsmid = ip->i_fsmid;
432         return (0);
433 }
434
435 /*
436  * Set attribute vnode op. called from several syscalls
437  *
438  * ufs_setattr(struct vnode *a_vp, struct vattr *a_vap,
439  *              struct ucred *a_cred, struct thread *a_td)
440  */
441 static
442 int
443 ufs_setattr(struct vop_setattr_args *ap)
444 {
445         struct vattr *vap = ap->a_vap;
446         struct vnode *vp = ap->a_vp;
447         struct inode *ip = VTOI(vp);
448         struct ucred *cred = ap->a_cred;
449         int error;
450
451         /*
452          * Check for unsettable attributes.
453          */
454         if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
455             (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
456             (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
457             ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
458                 return (EINVAL);
459         }
460         if (vap->va_flags != VNOVAL) {
461                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
462                         return (EROFS);
463                 if (cred->cr_uid != ip->i_uid &&
464                     (error = suser_cred(cred, PRISON_ROOT)))
465                         return (error);
466                 /*
467                  * Note that a root chflags becomes a user chflags when
468                  * we are jailed, unless the jail.chflags_allowed sysctl
469                  * is set.
470                  */
471                 if (cred->cr_uid == 0 && 
472                     (!jailed(cred) || jail_chflags_allowed)) {
473                         if ((ip->i_flags
474                             & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) &&
475                             securelevel > 0)
476                                 return (EPERM);
477                         ip->i_flags = vap->va_flags;
478                 } else {
479                         if (ip->i_flags
480                             & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
481                             (vap->va_flags & UF_SETTABLE) != vap->va_flags)
482                                 return (EPERM);
483                         ip->i_flags &= SF_SETTABLE;
484                         ip->i_flags |= (vap->va_flags & UF_SETTABLE);
485                 }
486                 ip->i_flag |= IN_CHANGE;
487                 if (vap->va_flags & (IMMUTABLE | APPEND))
488                         return (0);
489         }
490         if (ip->i_flags & (IMMUTABLE | APPEND))
491                 return (EPERM);
492         /*
493          * Go through the fields and update iff not VNOVAL.
494          */
495         if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
496                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
497                         return (EROFS);
498                 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred, ap->a_td)) != 0)
499                         return (error);
500         }
501         if (vap->va_size != VNOVAL) {
502                 /*
503                  * Disallow write attempts on read-only filesystems;
504                  * unless the file is a socket, fifo, or a block or
505                  * character device resident on the filesystem.
506                  */
507                 switch (vp->v_type) {
508                 case VDIR:
509                         return (EISDIR);
510                 case VLNK:
511                 case VREG:
512                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
513                                 return (EROFS);
514                         break;
515                 default:
516                         break;
517                 }
518                 if ((error = UFS_TRUNCATE(vp, vap->va_size, 0, cred, ap->a_td)) != 0)
519                         return (error);
520         }
521         ip = VTOI(vp);
522         if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
523                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
524                         return (EROFS);
525                 if (cred->cr_uid != ip->i_uid &&
526                     (error = suser_cred(cred, PRISON_ROOT)) &&
527                     ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
528                     (error = VOP_ACCESS(vp, VWRITE, cred, ap->a_td))))
529                         return (error);
530                 if (vap->va_atime.tv_sec != VNOVAL)
531                         ip->i_flag |= IN_ACCESS;
532                 if (vap->va_mtime.tv_sec != VNOVAL)
533                         ip->i_flag |= IN_CHANGE | IN_UPDATE;
534                 ufs_itimes(vp);
535                 if (vap->va_atime.tv_sec != VNOVAL) {
536                         ip->i_atime = vap->va_atime.tv_sec;
537                         ip->i_atimensec = vap->va_atime.tv_nsec;
538                 }
539                 if (vap->va_mtime.tv_sec != VNOVAL) {
540                         ip->i_mtime = vap->va_mtime.tv_sec;
541                         ip->i_mtimensec = vap->va_mtime.tv_nsec;
542                 }
543                 error = UFS_UPDATE(vp, 0);
544                 if (error)
545                         return (error);
546         }
547         error = 0;
548         if (vap->va_mode != (mode_t)VNOVAL) {
549                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
550                         return (EROFS);
551                 error = ufs_chmod(vp, (int)vap->va_mode, cred, ap->a_td);
552         }
553         VN_KNOTE(vp, NOTE_ATTRIB);
554         return (error);
555 }
556
557 /*
558  * Change the mode on a file.
559  * Inode must be locked before calling.
560  */
561 static int
562 ufs_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td)
563 {
564         struct inode *ip = VTOI(vp);
565         int error;
566
567         if (cred->cr_uid != ip->i_uid) {
568             error = suser_cred(cred, PRISON_ROOT);
569             if (error)
570                 return (error);
571         }
572         if (cred->cr_uid) {
573                 if (vp->v_type != VDIR && (mode & S_ISTXT))
574                         return (EFTYPE);
575                 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
576                         return (EPERM);
577         }
578         ip->i_mode &= ~ALLPERMS;
579         ip->i_mode |= (mode & ALLPERMS);
580         ip->i_flag |= IN_CHANGE;
581         return (0);
582 }
583
584 /*
585  * Perform chown operation on inode ip;
586  * inode must be locked prior to call.
587  */
588 static int
589 ufs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
590           struct thread *td)
591 {
592         struct inode *ip = VTOI(vp);
593         uid_t ouid;
594         gid_t ogid;
595         int error = 0;
596 #ifdef QUOTA
597         int i;
598         long change;
599 #endif
600
601         if (uid == (uid_t)VNOVAL)
602                 uid = ip->i_uid;
603         if (gid == (gid_t)VNOVAL)
604                 gid = ip->i_gid;
605         /*
606          * If we don't own the file, are trying to change the owner
607          * of the file, or are not a member of the target group,
608          * the caller must be superuser or the call fails.
609          */
610         if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
611             (gid != ip->i_gid && !(cred->cr_gid == gid ||
612             groupmember((gid_t)gid, cred)))) &&
613             (error = suser_cred(cred, PRISON_ROOT)))
614                 return (error);
615         ogid = ip->i_gid;
616         ouid = ip->i_uid;
617 #ifdef QUOTA
618         if ((error = ufs_getinoquota(ip)) != 0)
619                 return (error);
620         if (ouid == uid) {
621                 ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
622                 ip->i_dquot[USRQUOTA] = NODQUOT;
623         }
624         if (ogid == gid) {
625                 ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
626                 ip->i_dquot[GRPQUOTA] = NODQUOT;
627         }
628         change = ip->i_blocks;
629         (void) ufs_chkdq(ip, -change, cred, CHOWN);
630         (void) ufs_chkiq(ip, -1, cred, CHOWN);
631         for (i = 0; i < MAXQUOTAS; i++) {
632                 ufs_dqrele(vp, ip->i_dquot[i]);
633                 ip->i_dquot[i] = NODQUOT;
634         }
635 #endif
636         ip->i_gid = gid;
637         ip->i_uid = uid;
638 #ifdef QUOTA
639         if ((error = ufs_getinoquota(ip)) == 0) {
640                 if (ouid == uid) {
641                         ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
642                         ip->i_dquot[USRQUOTA] = NODQUOT;
643                 }
644                 if (ogid == gid) {
645                         ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
646                         ip->i_dquot[GRPQUOTA] = NODQUOT;
647                 }
648                 if ((error = ufs_chkdq(ip, change, cred, CHOWN)) == 0) {
649                         if ((error = ufs_chkiq(ip, 1, cred, CHOWN)) == 0)
650                                 goto good;
651                         else
652                                 (void)ufs_chkdq(ip, -change, cred, CHOWN|FORCE);
653                 }
654                 for (i = 0; i < MAXQUOTAS; i++) {
655                         ufs_dqrele(vp, ip->i_dquot[i]);
656                         ip->i_dquot[i] = NODQUOT;
657                 }
658         }
659         ip->i_gid = ogid;
660         ip->i_uid = ouid;
661         if (ufs_getinoquota(ip) == 0) {
662                 if (ouid == uid) {
663                         ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
664                         ip->i_dquot[USRQUOTA] = NODQUOT;
665                 }
666                 if (ogid == gid) {
667                         ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
668                         ip->i_dquot[GRPQUOTA] = NODQUOT;
669                 }
670                 (void) ufs_chkdq(ip, change, cred, FORCE|CHOWN);
671                 (void) ufs_chkiq(ip, 1, cred, FORCE|CHOWN);
672                 (void) ufs_getinoquota(ip);
673         }
674         return (error);
675 good:
676         if (ufs_getinoquota(ip))
677                 panic("ufs_chown: lost quota");
678 #endif /* QUOTA */
679         ip->i_flag |= IN_CHANGE;
680         if (cred->cr_uid != 0 && (ouid != uid || ogid != gid))
681                 ip->i_mode &= ~(ISUID | ISGID);
682         return (0);
683 }
684
685 /*
686  * Mmap a file
687  *
688  * NB Currently unsupported.
689  *
690  * ufs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
691  *          struct thread *a_td)
692  */
693 /* ARGSUSED */
694 static
695 int
696 ufs_mmap(struct vop_mmap_args *ap)
697 {
698         return (EINVAL);
699 }
700
701 /*
702  * ufs_remove(struct vnode *a_dvp, struct vnode *a_vp,
703  *            struct componentname *a_cnp)
704  */
705 static
706 int
707 ufs_remove(struct vop_old_remove_args *ap)
708 {
709         struct inode *ip;
710         struct vnode *vp = ap->a_vp;
711         struct vnode *dvp = ap->a_dvp;
712         int error;
713
714         ip = VTOI(vp);
715         if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
716             (VTOI(dvp)->i_flags & APPEND)) {
717                 error = EPERM;
718                 goto out;
719         }
720         error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
721         VN_KNOTE(vp, NOTE_DELETE);
722         VN_KNOTE(dvp, NOTE_WRITE);
723 out:
724         return (error);
725 }
726
727 /*
728  * link vnode call
729  *
730  * ufs_link(struct vnode *a_tdvp, struct vnode *a_vp,
731  *          struct componentname *a_cnp)
732  */
733 static
734 int
735 ufs_link(struct vop_old_link_args *ap)
736 {
737         struct vnode *vp = ap->a_vp;
738         struct vnode *tdvp = ap->a_tdvp;
739         struct componentname *cnp = ap->a_cnp;
740         struct thread *td = cnp->cn_td;
741         struct inode *ip;
742         struct direct newdir;
743         int error;
744
745         if (tdvp->v_mount != vp->v_mount) {
746                 error = EXDEV;
747                 goto out2;
748         }
749         if (tdvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE, td))) {
750                 goto out2;
751         }
752         ip = VTOI(vp);
753         if ((nlink_t)ip->i_nlink >= LINK_MAX) {
754                 error = EMLINK;
755                 goto out1;
756         }
757         if (ip->i_flags & (IMMUTABLE | APPEND)) {
758                 error = EPERM;
759                 goto out1;
760         }
761         ip->i_effnlink++;
762         ip->i_nlink++;
763         ip->i_flag |= IN_CHANGE;
764         if (DOINGSOFTDEP(vp))
765                 softdep_change_linkcnt(ip);
766         error = UFS_UPDATE(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
767         if (!error) {
768                 ufs_makedirentry(ip, cnp, &newdir);
769                 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
770         }
771
772         if (error) {
773                 ip->i_effnlink--;
774                 ip->i_nlink--;
775                 ip->i_flag |= IN_CHANGE;
776                 if (DOINGSOFTDEP(vp))
777                         softdep_change_linkcnt(ip);
778         }
779 out1:
780         if (tdvp != vp)
781                 VOP_UNLOCK(vp, 0, td);
782 out2:
783         VN_KNOTE(vp, NOTE_LINK);
784         VN_KNOTE(tdvp, NOTE_WRITE);
785         return (error);
786 }
787
788 /*
789  * whiteout vnode call
790  *
791  * ufs_whiteout(struct vnode *a_dvp, struct componentname *a_cnp, int a_flags)
792  */
793 static
794 int
795 ufs_whiteout(struct vop_old_whiteout_args *ap)
796 {
797         struct vnode *dvp = ap->a_dvp;
798         struct componentname *cnp = ap->a_cnp;
799         struct direct newdir;
800         int error = 0;
801
802         switch (ap->a_flags) {
803         case NAMEI_LOOKUP:
804                 /* 4.4 format directories support whiteout operations */
805                 if (dvp->v_mount->mnt_maxsymlinklen > 0)
806                         return (0);
807                 return (EOPNOTSUPP);
808
809         case NAMEI_CREATE:
810                 /* create a new directory whiteout */
811 #ifdef DIAGNOSTIC
812                 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
813                         panic("ufs_whiteout: old format filesystem");
814 #endif
815
816                 newdir.d_ino = WINO;
817                 newdir.d_namlen = cnp->cn_namelen;
818                 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
819                 newdir.d_type = DT_WHT;
820                 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
821                 break;
822
823         case NAMEI_DELETE:
824                 /* remove an existing directory whiteout */
825 #ifdef DIAGNOSTIC
826                 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
827                         panic("ufs_whiteout: old format filesystem");
828 #endif
829
830                 cnp->cn_flags &= ~CNP_DOWHITEOUT;
831                 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
832                 break;
833         default:
834                 panic("ufs_whiteout: unknown op");
835         }
836         return (error);
837 }
838
839 /*
840  * Rename system call.
841  *      rename("foo", "bar");
842  * is essentially
843  *      unlink("bar");
844  *      link("foo", "bar");
845  *      unlink("foo");
846  * but ``atomically''.  Can't do full commit without saving state in the
847  * inode on disk which isn't feasible at this time.  Best we can do is
848  * always guarantee the target exists.
849  *
850  * Basic algorithm is:
851  *
852  * 1) Bump link count on source while we're linking it to the
853  *    target.  This also ensure the inode won't be deleted out
854  *    from underneath us while we work (it may be truncated by
855  *    a concurrent `trunc' or `open' for creation).
856  * 2) Link source to destination.  If destination already exists,
857  *    delete it first.
858  * 3) Unlink source reference to inode if still around. If a
859  *    directory was moved and the parent of the destination
860  *    is different from the source, patch the ".." entry in the
861  *    directory.
862  *
863  * ufs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
864  *            struct componentname *a_fcnp, struct vnode *a_tdvp,
865  *            struct vnode *a_tvp, struct componentname *a_tcnp)
866  */
867 static
868 int
869 ufs_rename(struct vop_old_rename_args *ap)
870 {
871         struct vnode *tvp = ap->a_tvp;
872         struct vnode *tdvp = ap->a_tdvp;
873         struct vnode *fvp = ap->a_fvp;
874         struct vnode *fdvp = ap->a_fdvp;
875         struct componentname *tcnp = ap->a_tcnp;
876         struct componentname *fcnp = ap->a_fcnp;
877         struct thread *td = fcnp->cn_td;
878         struct inode *ip, *xp, *dp;
879         struct direct newdir;
880         ino_t oldparent = 0, newparent = 0;
881         int doingdirectory = 0;
882         int error = 0, ioflag;
883
884         /*
885          * Check for cross-device rename.
886          */
887         if ((fvp->v_mount != tdvp->v_mount) ||
888             (tvp && (fvp->v_mount != tvp->v_mount))) {
889                 error = EXDEV;
890 abortit:
891                 if (tdvp == tvp)
892                         vrele(tdvp);
893                 else
894                         vput(tdvp);
895                 if (tvp)
896                         vput(tvp);
897                 vrele(fdvp);
898                 vrele(fvp);
899                 return (error);
900         }
901
902         if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
903             (VTOI(tdvp)->i_flags & APPEND))) {
904                 error = EPERM;
905                 goto abortit;
906         }
907
908         /*
909          * Renaming a file to itself has no effect.  The upper layers should
910          * not call us in that case.  Temporarily just warn if they do.
911          */
912         if (fvp == tvp) {
913                 printf("ufs_rename: fvp == tvp (can't happen)\n");
914                 error = 0;
915                 goto abortit;
916         }
917
918         if ((error = vn_lock(fvp, LK_EXCLUSIVE, td)) != 0)
919                 goto abortit;
920
921         /*
922          * Note: now that fvp is locked we have to be sure to unlock it before
923          * using the 'abortit' target.
924          */
925         dp = VTOI(fdvp);
926         ip = VTOI(fvp);
927         if (ip->i_nlink >= LINK_MAX) {
928                 VOP_UNLOCK(fvp, 0, td);
929                 error = EMLINK;
930                 goto abortit;
931         }
932         if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
933             || (dp->i_flags & APPEND)) {
934                 VOP_UNLOCK(fvp, 0, td);
935                 error = EPERM;
936                 goto abortit;
937         }
938         if ((ip->i_mode & IFMT) == IFDIR) {
939                 /*
940                  * Avoid ".", "..", and aliases of "." for obvious reasons.
941                  */
942                 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
943                     dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & CNP_ISDOTDOT ||
944                     (ip->i_flag & IN_RENAME)) {
945                         VOP_UNLOCK(fvp, 0, td);
946                         error = EINVAL;
947                         goto abortit;
948                 }
949                 ip->i_flag |= IN_RENAME;
950                 oldparent = dp->i_number;
951                 doingdirectory = 1;
952         }
953         VN_KNOTE(fdvp, NOTE_WRITE);             /* XXX right place? */
954
955         /*
956          * fvp still locked.  ip->i_flag has IN_RENAME set if doingdirectory.
957          * Cleanup fvp requirements so we can unlock it.
958          *
959          * tvp and tdvp are locked.  tvp may be NULL.  Now that dp and xp
960          * is setup we can use the 'bad' target if we unlock fvp.  We cannot
961          * use the abortit target anymore because of IN_RENAME.
962          */
963         dp = VTOI(tdvp);
964         if (tvp)
965                 xp = VTOI(tvp);
966         else
967                 xp = NULL;
968
969         /*
970          * 1) Bump link count while we're moving stuff
971          *    around.  If we crash somewhere before
972          *    completing our work, the link count
973          *    may be wrong, but correctable.
974          */
975         ip->i_effnlink++;
976         ip->i_nlink++;
977         ip->i_flag |= IN_CHANGE;
978         if (DOINGSOFTDEP(fvp))
979                 softdep_change_linkcnt(ip);
980         if ((error = UFS_UPDATE(fvp, !(DOINGSOFTDEP(fvp) |
981                                        DOINGASYNC(fvp)))) != 0) {
982                 VOP_UNLOCK(fvp, 0, td);
983                 goto bad;
984         }
985
986         /*
987          * If ".." must be changed (ie the directory gets a new
988          * parent) then the source directory must not be in the
989          * directory heirarchy above the target, as this would
990          * orphan everything below the source directory. Also
991          * the user must have write permission in the source so
992          * as to be able to change "..". We must repeat the call
993          * to namei, as the parent directory is unlocked by the
994          * call to checkpath().
995          */
996         error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_td);
997         VOP_UNLOCK(fvp, 0, td);
998
999         /*
1000          * We are now back to where we were in that fvp, fdvp are unlocked
1001          * and tvp, tdvp are locked.  tvp may be NULL.  IN_RENAME may be
1002          * set.  Only the bad target or, if we clean up tvp and tdvp, the
1003          * out target, may be used.
1004          */
1005         if (oldparent != dp->i_number)
1006                 newparent = dp->i_number;
1007         if (doingdirectory && newparent) {
1008                 if (error)      /* write access check above */
1009                         goto bad;
1010
1011                 /*
1012                  * Once we start messing with tvp and tdvp we cannot use the
1013                  * 'bad' target, only finish cleaning tdvp and tvp up and
1014                  * use the 'out' target.
1015                  *
1016                  * This cleans up tvp.
1017                  */
1018                 if (xp != NULL) {
1019                         vput(tvp);
1020                         xp = NULL;
1021                 }
1022
1023                 /*
1024                  * This is a real mess. ufs_checkpath vput's the target
1025                  * directory so retain an extra ref and note that tdvp will
1026                  * lose its lock on return.  This leaves us with one good
1027                  * ref after ufs_checkpath returns.
1028                  */
1029                 vref(tdvp);
1030                 error = ufs_checkpath(ip, dp, tcnp->cn_cred);
1031                 tcnp->cn_flags |= CNP_PDIRUNLOCK;
1032                 if (error) {
1033                         vrele(tdvp);
1034                         goto out;
1035                 }
1036
1037                 /*
1038                  * relookup no longer messes with tdvp's refs. tdvp must be
1039                  * unlocked on entry and will be locked on a successful
1040                  * return.
1041                  */
1042                 error = relookup(tdvp, &tvp, tcnp);
1043                 if (error) {
1044                         if (tcnp->cn_flags & CNP_PDIRUNLOCK)
1045                                 vrele(tdvp);
1046                         else
1047                                 vput(tdvp);
1048                         goto out;
1049                 }
1050                 KKASSERT((tcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
1051                 dp = VTOI(tdvp);
1052                 if (tvp)
1053                         xp = VTOI(tvp);
1054         }
1055
1056         /*
1057          * We are back to fvp, fdvp unlocked, tvp, tdvp locked.  tvp may 
1058          * be NULL (xp will also be NULL in that case), and IN_RENAME will
1059          * be set if doingdirectory.  This means we can use the 'bad' target
1060          * again.
1061          */
1062
1063         /*
1064          * 2) If target doesn't exist, link the target
1065          *    to the source and unlink the source.
1066          *    Otherwise, rewrite the target directory
1067          *    entry to reference the source inode and
1068          *    expunge the original entry's existence.
1069          */
1070         if (xp == NULL) {
1071                 if (dp->i_dev != ip->i_dev)
1072                         panic("ufs_rename: EXDEV");
1073                 /*
1074                  * Account for ".." in new directory.
1075                  * When source and destination have the same
1076                  * parent we don't fool with the link count.
1077                  */
1078                 if (doingdirectory && newparent) {
1079                         if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1080                                 error = EMLINK;
1081                                 goto bad;
1082                         }
1083                         dp->i_effnlink++;
1084                         dp->i_nlink++;
1085                         dp->i_flag |= IN_CHANGE;
1086                         if (DOINGSOFTDEP(tdvp))
1087                                 softdep_change_linkcnt(dp);
1088                         error = UFS_UPDATE(tdvp, !(DOINGSOFTDEP(tdvp) |
1089                                                    DOINGASYNC(tdvp)));
1090                         if (error)
1091                                 goto bad;
1092                 }
1093                 ufs_makedirentry(ip, tcnp, &newdir);
1094                 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1095                 if (error) {
1096                         if (doingdirectory && newparent) {
1097                                 dp->i_effnlink--;
1098                                 dp->i_nlink--;
1099                                 dp->i_flag |= IN_CHANGE;
1100                                 if (DOINGSOFTDEP(tdvp))
1101                                         softdep_change_linkcnt(dp);
1102                                 (void)UFS_UPDATE(tdvp, 1);
1103                         }
1104                         goto bad;
1105                 }
1106                 VN_KNOTE(tdvp, NOTE_WRITE);
1107                 vput(tdvp);
1108         } else {
1109                 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1110                         panic("ufs_rename: EXDEV");
1111                 /*
1112                  * Short circuit rename(foo, foo).
1113                  */
1114                 if (xp->i_number == ip->i_number)
1115                         panic("ufs_rename: same file");
1116                 /*
1117                  * If the parent directory is "sticky", then the user must
1118                  * own the parent directory, or the destination of the rename,
1119                  * otherwise the destination may not be changed (except by
1120                  * root). This implements append-only directories.
1121                  */
1122                 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
1123                     tcnp->cn_cred->cr_uid != dp->i_uid &&
1124                     xp->i_uid != tcnp->cn_cred->cr_uid) {
1125                         error = EPERM;
1126                         goto bad;
1127                 }
1128                 /*
1129                  * Target must be empty if a directory and have no links
1130                  * to it. Also, ensure source and target are compatible
1131                  * (both directories, or both not directories).
1132                  *
1133                  * Purge the file or directory being replaced from the
1134                  * nameccache.
1135                  */
1136                 if ((xp->i_mode&IFMT) == IFDIR) {
1137                         if ((xp->i_effnlink > 2) ||
1138                             !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1139                                 error = ENOTEMPTY;
1140                                 goto bad;
1141                         }
1142                         if (!doingdirectory) {
1143                                 error = ENOTDIR;
1144                                 goto bad;
1145                         }
1146                         /* cache_purge removed - handled by VFS compat layer */
1147                 } else if (doingdirectory == 0) {
1148                         /* cache_purge removed - handled by VFS compat layer */
1149                 } else {
1150                         error = EISDIR;
1151                         goto bad;
1152                 }
1153                 /*
1154                  * note: inode passed to ufs_dirrewrite() is 0 for a 
1155                  * non-directory file rename, 1 for a directory rename
1156                  * in the same directory, and > 1 for an inode representing
1157                  * the new directory.
1158                  */
1159                 error = ufs_dirrewrite(dp, xp, ip->i_number,
1160                     IFTODT(ip->i_mode),
1161                     (doingdirectory && newparent) ?
1162                         newparent : (ino_t)doingdirectory);
1163                 if (error)
1164                         goto bad;
1165                 if (doingdirectory) {
1166                         if (!newparent) {
1167                                 dp->i_effnlink--;
1168                                 if (DOINGSOFTDEP(tdvp))
1169                                         softdep_change_linkcnt(dp);
1170                         }
1171                         xp->i_effnlink--;
1172                         if (DOINGSOFTDEP(tvp))
1173                                 softdep_change_linkcnt(xp);
1174                 }
1175                 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1176                         /*
1177                          * Truncate inode. The only stuff left in the directory
1178                          * is "." and "..". The "." reference is inconsequential
1179                          * since we are quashing it. We have removed the "."
1180                          * reference and the reference in the parent directory,
1181                          * but there may be other hard links. The soft
1182                          * dependency code will arrange to do these operations
1183                          * after the parent directory entry has been deleted on
1184                          * disk, so when running with that code we avoid doing
1185                          * them now.
1186                          */
1187                         if (!newparent) {
1188                                 dp->i_nlink--;
1189                                 dp->i_flag |= IN_CHANGE;
1190                         }
1191                         xp->i_nlink--;
1192                         xp->i_flag |= IN_CHANGE;
1193                         ioflag = DOINGASYNC(tvp) ? 0 : IO_SYNC;
1194                         if ((error = UFS_TRUNCATE(tvp, (off_t)0, ioflag,
1195                             tcnp->cn_cred, tcnp->cn_td)) != 0)
1196                                 goto bad;
1197                 }
1198                 VN_KNOTE(tdvp, NOTE_WRITE);
1199                 vput(tdvp);
1200                 VN_KNOTE(tvp, NOTE_DELETE);
1201                 vput(tvp);
1202                 xp = NULL;
1203         }
1204
1205         /*
1206          * tvp and tdvp have been cleaned up.  only fvp and fdvp (both
1207          * unlocked) remain.  We are about to overwrite fvp but we have to
1208          * keep 'ip' intact so we cannot release the old fvp, which is still
1209          * refd and accessible via ap->a_fvp.
1210          *
1211          * This means we cannot use either 'bad' or 'out' to cleanup any 
1212          * more.
1213          */
1214
1215         /*
1216          * 3) Unlink the source.
1217          */
1218         fcnp->cn_flags &= ~CNP_MODMASK;
1219         fcnp->cn_flags |= CNP_LOCKPARENT;
1220         error = relookup(fdvp, &fvp, fcnp);
1221         if (error || fvp == NULL) {
1222                 /*
1223                  * From name has disappeared.  IN_RENAME will not be set if
1224                  * we get past the panic so we don't have to clean it up.
1225                  */
1226                 if (doingdirectory)
1227                         panic("ufs_rename: lost dir entry");
1228                 vrele(ap->a_fvp);
1229                 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
1230                         vrele(fdvp);
1231                 else
1232                         vput(fdvp);
1233                 return(0);
1234         }
1235         KKASSERT((fcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
1236
1237         /*
1238          * fdvp and fvp are locked.
1239          */
1240         xp = VTOI(fvp);
1241         dp = VTOI(fdvp);
1242
1243         /*
1244          * Ensure that the directory entry still exists and has not
1245          * changed while the new name has been entered. If the source is
1246          * a file then the entry may have been unlinked or renamed. In
1247          * either case there is no further work to be done. If the source
1248          * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1249          * flag ensures that it cannot be moved by another rename or removed
1250          * by a rmdir.  Cleanup IN_RENAME.
1251          */
1252         if (xp != ip) {
1253                 if (doingdirectory)
1254                         panic("ufs_rename: lost dir entry");
1255         } else {
1256                 /*
1257                  * If the source is a directory with a
1258                  * new parent, the link count of the old
1259                  * parent directory must be decremented
1260                  * and ".." set to point to the new parent.
1261                  */
1262                 if (doingdirectory && newparent) {
1263                         xp->i_offset = mastertemplate.dot_reclen;
1264                         ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1265                         /* cache_purge removed - handled by VFS compat layer */
1266                 }
1267                 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1268                 xp->i_flag &= ~IN_RENAME;
1269         }
1270
1271         VN_KNOTE(fvp, NOTE_RENAME);
1272         vput(fdvp);
1273         vput(fvp);
1274         vrele(ap->a_fvp);
1275         return (error);
1276
1277 bad:
1278         if (xp)
1279                 vput(ITOV(xp));
1280         vput(ITOV(dp));
1281 out:
1282         if (doingdirectory)
1283                 ip->i_flag &= ~IN_RENAME;
1284         if (vn_lock(fvp, LK_EXCLUSIVE, td) == 0) {
1285                 ip->i_effnlink--;
1286                 ip->i_nlink--;
1287                 ip->i_flag |= IN_CHANGE;
1288                 ip->i_flag &= ~IN_RENAME;
1289                 if (DOINGSOFTDEP(fvp))
1290                         softdep_change_linkcnt(ip);
1291                 vput(fvp);
1292         } else {
1293                 vrele(fvp);
1294         }
1295         return (error);
1296 }
1297
1298 /*
1299  * Mkdir system call
1300  *
1301  * ufs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1302  *           struct componentname *a_cnp, struct vattr *a_vap)
1303  */
1304 static
1305 int
1306 ufs_mkdir(struct vop_old_mkdir_args *ap)
1307 {
1308         struct vnode *dvp = ap->a_dvp;
1309         struct vattr *vap = ap->a_vap;
1310         struct componentname *cnp = ap->a_cnp;
1311         struct inode *ip, *dp;
1312         struct vnode *tvp;
1313         struct buf *bp;
1314         struct dirtemplate dirtemplate, *dtp;
1315         struct direct newdir;
1316         int error, dmode;
1317         long blkoff;
1318
1319         dp = VTOI(dvp);
1320         if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1321                 error = EMLINK;
1322                 goto out;
1323         }
1324         dmode = vap->va_mode & 0777;
1325         dmode |= IFDIR;
1326         /*
1327          * Must simulate part of ufs_makeinode here to acquire the inode,
1328          * but not have it entered in the parent directory. The entry is
1329          * made later after writing "." and ".." entries.
1330          */
1331         error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
1332         if (error)
1333                 goto out;
1334         ip = VTOI(tvp);
1335         ip->i_gid = dp->i_gid;
1336 #ifdef SUIDDIR
1337         {
1338 #ifdef QUOTA
1339                 struct ucred ucred, *ucp;
1340                 ucp = cnp->cn_cred;
1341 #endif
1342                 /*
1343                  * If we are hacking owners here, (only do this where told to)
1344                  * and we are not giving it TO root, (would subvert quotas)
1345                  * then go ahead and give it to the other user.
1346                  * The new directory also inherits the SUID bit.
1347                  * If user's UID and dir UID are the same,
1348                  * 'give it away' so that the SUID is still forced on.
1349                  */
1350                 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1351                     (dp->i_mode & ISUID) && dp->i_uid) {
1352                         dmode |= ISUID;
1353                         ip->i_uid = dp->i_uid;
1354 #ifdef QUOTA
1355                         if (dp->i_uid != cnp->cn_cred->cr_uid) {
1356                                 /*
1357                                  * Make sure the correct user gets charged
1358                                  * for the space.
1359                                  * Make a dummy credential for the victim.
1360                                  * XXX This seems to never be accessed out of
1361                                  * our context so a stack variable is ok.
1362                                  */
1363                                 ucred.cr_ref = 1;
1364                                 ucred.cr_uid = ip->i_uid;
1365                                 ucred.cr_ngroups = 1;
1366                                 ucred.cr_groups[0] = dp->i_gid;
1367                                 ucp = &ucred;
1368                         }
1369 #endif
1370                 } else
1371                         ip->i_uid = cnp->cn_cred->cr_uid;
1372 #ifdef QUOTA
1373                 if ((error = ufs_getinoquota(ip)) ||
1374                     (error = ufs_chkiq(ip, 1, ucp, 0))) {
1375                         UFS_VFREE(tvp, ip->i_number, dmode);
1376                         vput(tvp);
1377                         return (error);
1378                 }
1379 #endif
1380         }
1381 #else   /* !SUIDDIR */
1382         ip->i_uid = cnp->cn_cred->cr_uid;
1383 #ifdef QUOTA
1384         if ((error = ufs_getinoquota(ip)) ||
1385             (error = ufs_chkiq(ip, 1, cnp->cn_cred, 0))) {
1386                 UFS_VFREE(tvp, ip->i_number, dmode);
1387                 vput(tvp);
1388                 return (error);
1389         }
1390 #endif
1391 #endif  /* !SUIDDIR */
1392         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1393         ip->i_mode = dmode;
1394         tvp->v_type = VDIR;     /* Rest init'd in getnewvnode(). */
1395         ip->i_effnlink = 2;
1396         ip->i_nlink = 2;
1397         if (DOINGSOFTDEP(tvp))
1398                 softdep_change_linkcnt(ip);
1399         if (cnp->cn_flags & CNP_ISWHITEOUT)
1400                 ip->i_flags |= UF_OPAQUE;
1401
1402         /*
1403          * Bump link count in parent directory to reflect work done below.
1404          * Should be done before reference is created so cleanup is
1405          * possible if we crash.
1406          */
1407         dp->i_effnlink++;
1408         dp->i_nlink++;
1409         dp->i_flag |= IN_CHANGE;
1410         if (DOINGSOFTDEP(dvp))
1411                 softdep_change_linkcnt(dp);
1412         error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1413         if (error)
1414                 goto bad;
1415
1416         /*
1417          * The vnode must have a VM object in order to issue buffer cache
1418          * ops on it.
1419          */
1420         vinitvmio(tvp, DIRBLKSIZ);
1421
1422         /*
1423          * Initialize directory with "." and ".." from static template.
1424          */
1425         if (dvp->v_mount->mnt_maxsymlinklen > 0)
1426                 dtp = &mastertemplate;
1427         else
1428                 dtp = (struct dirtemplate *)&omastertemplate;
1429         dirtemplate = *dtp;
1430         dirtemplate.dot_ino = ip->i_number;
1431         dirtemplate.dotdot_ino = dp->i_number;
1432         error = VOP_BALLOC(tvp, 0LL, DIRBLKSIZ, cnp->cn_cred, B_CLRBUF, &bp);
1433         if (error)
1434                 goto bad;
1435         ip->i_size = DIRBLKSIZ;
1436         ip->i_flag |= IN_CHANGE | IN_UPDATE;
1437         vnode_pager_setsize(tvp, (u_long)ip->i_size);
1438         bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1439         if (DOINGSOFTDEP(tvp)) {
1440                 /*
1441                  * Ensure that the entire newly allocated block is a
1442                  * valid directory so that future growth within the
1443                  * block does not have to ensure that the block is
1444                  * written before the inode.
1445                  */
1446                 blkoff = DIRBLKSIZ;
1447                 while (blkoff < bp->b_bcount) {
1448                         ((struct direct *)
1449                            (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1450                         blkoff += DIRBLKSIZ;
1451                 }
1452         }
1453         if ((error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) |
1454                                        DOINGASYNC(tvp)))) != 0) {
1455                 VOP_BWRITE(bp->b_vp, bp);
1456                 goto bad;
1457         }
1458         /*
1459          * Directory set up, now install its entry in the parent directory.
1460          *
1461          * If we are not doing soft dependencies, then we must write out the
1462          * buffer containing the new directory body before entering the new 
1463          * name in the parent. If we are doing soft dependencies, then the
1464          * buffer containing the new directory body will be passed to and
1465          * released in the soft dependency code after the code has attached
1466          * an appropriate ordering dependency to the buffer which ensures that
1467          * the buffer is written before the new name is written in the parent.
1468          */
1469         if (DOINGASYNC(dvp))
1470                 bdwrite(bp);
1471         else if (!DOINGSOFTDEP(dvp) && ((error = VOP_BWRITE(bp->b_vp, bp))))
1472                 goto bad;
1473         ufs_makedirentry(ip, cnp, &newdir);
1474         error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1475         
1476 bad:
1477         if (error == 0) {
1478                 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1479                 *ap->a_vpp = tvp;
1480         } else {
1481                 dp->i_effnlink--;
1482                 dp->i_nlink--;
1483                 dp->i_flag |= IN_CHANGE;
1484                 if (DOINGSOFTDEP(dvp))
1485                         softdep_change_linkcnt(dp);
1486                 /*
1487                  * No need to do an explicit VOP_TRUNCATE here, vrele will
1488                  * do this for us because we set the link count to 0.
1489                  */
1490                 ip->i_effnlink = 0;
1491                 ip->i_nlink = 0;
1492                 ip->i_flag |= IN_CHANGE;
1493                 if (DOINGSOFTDEP(tvp))
1494                         softdep_change_linkcnt(ip);
1495                 vput(tvp);
1496         }
1497 out:
1498         return (error);
1499 }
1500
1501 /*
1502  * Rmdir system call.
1503  *
1504  * ufs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1505  *           struct componentname *a_cnp)
1506  */
1507 static
1508 int
1509 ufs_rmdir(struct vop_old_rmdir_args *ap)
1510 {
1511         struct vnode *vp = ap->a_vp;
1512         struct vnode *dvp = ap->a_dvp;
1513         struct componentname *cnp = ap->a_cnp;
1514         struct inode *ip, *dp;
1515         int error, ioflag;
1516
1517         ip = VTOI(vp);
1518         dp = VTOI(dvp);
1519
1520         /*
1521          * Do not remove a directory that is in the process of being renamed.
1522          * Verify the directory is empty (and valid). Rmdir ".." will not be
1523          * valid since ".." will contain a reference to the current directory
1524          * and thus be non-empty. Do not allow the removal of mounted on
1525          * directories (this can happen when an NFS exported filesystem
1526          * tries to remove a locally mounted on directory).
1527          */
1528         error = 0;
1529         if (ip->i_flag & IN_RENAME) {
1530                 error = EINVAL;
1531                 goto out;
1532         }
1533         if (ip->i_effnlink != 2 ||
1534             !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1535                 error = ENOTEMPTY;
1536                 goto out;
1537         }
1538         if ((dp->i_flags & APPEND)
1539             || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1540                 error = EPERM;
1541                 goto out;
1542         }
1543         if (vp->v_mountedhere != 0) {
1544                 error = EINVAL;
1545                 goto out;
1546         }
1547         /*
1548          * Delete reference to directory before purging
1549          * inode.  If we crash in between, the directory
1550          * will be reattached to lost+found,
1551          */
1552         dp->i_effnlink--;
1553         ip->i_effnlink--;
1554         if (DOINGSOFTDEP(vp)) {
1555                 softdep_change_linkcnt(dp);
1556                 softdep_change_linkcnt(ip);
1557         }
1558         error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1559         if (error) {
1560                 dp->i_effnlink++;
1561                 ip->i_effnlink++;
1562                 if (DOINGSOFTDEP(vp)) {
1563                         softdep_change_linkcnt(dp);
1564                         softdep_change_linkcnt(ip);
1565                 }
1566                 goto out;
1567         }
1568         VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1569         /*
1570          * Truncate inode. The only stuff left in the directory is "." and
1571          * "..". The "." reference is inconsequential since we are quashing
1572          * it. The soft dependency code will arrange to do these operations
1573          * after the parent directory entry has been deleted on disk, so
1574          * when running with that code we avoid doing them now.
1575          */
1576         if (!DOINGSOFTDEP(vp)) {
1577                 dp->i_nlink--;
1578                 dp->i_flag |= IN_CHANGE;
1579                 ip->i_nlink--;
1580                 ip->i_flag |= IN_CHANGE;
1581                 ioflag = DOINGASYNC(vp) ? 0 : IO_SYNC;
1582                 error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred,
1583                     cnp->cn_td);
1584         }
1585         /* cache_purge removed - handled by VFS compat layer */
1586 #ifdef UFS_DIRHASH
1587         /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1588         if (ip->i_dirhash != NULL)
1589                 ufsdirhash_free(ip);
1590 #endif
1591 out:
1592         VN_KNOTE(vp, NOTE_DELETE);
1593         return (error);
1594 }
1595
1596 /*
1597  * symlink -- make a symbolic link
1598  *
1599  * ufs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1600  *              struct componentname *a_cnp, struct vattr *a_vap,
1601  *              char *a_target)
1602  */
1603 static
1604 int
1605 ufs_symlink(struct vop_old_symlink_args *ap)
1606 {
1607         struct vnode *vp, **vpp = ap->a_vpp;
1608         struct inode *ip;
1609         int len, error;
1610
1611         error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1612                               vpp, ap->a_cnp);
1613         if (error)
1614                 return (error);
1615         VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1616         vp = *vpp;
1617         len = strlen(ap->a_target);
1618         if (len < vp->v_mount->mnt_maxsymlinklen) {
1619                 ip = VTOI(vp);
1620                 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1621                 ip->i_size = len;
1622                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1623         } else {
1624                 /*
1625                  * Make sure we have a VM object in order to use
1626                  * the buffer cache.
1627                  */
1628                 if (vp->v_object == NULL)
1629                         vinitvmio(vp, 0);
1630                 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1631                                 UIO_SYSSPACE, IO_NODELOCKED, 
1632                                 ap->a_cnp->cn_cred, NULL, NULL);
1633         }
1634         if (error)
1635                 vput(vp);
1636         return (error);
1637 }
1638
1639 /*
1640  * Vnode op for reading directories.
1641  *
1642  * The routine below assumes that the on-disk format of a directory
1643  * is the same as that defined by <sys/dirent.h>. If the on-disk
1644  * format changes, then it will be necessary to do a conversion
1645  * from the on-disk format that read returns to the format defined
1646  * by <sys/dirent.h>.
1647  *
1648  * ufs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
1649  *              int *a_eofflag, int *ncookies, u_long **a_cookies)
1650  */
1651 static
1652 int
1653 ufs_readdir(struct vop_readdir_args *ap)
1654 {
1655         struct uio *uio = ap->a_uio;
1656         struct vnode *vp = ap->a_vp;
1657         struct direct *edp, *dp;
1658         struct uio auio;
1659         struct iovec aiov;
1660         caddr_t dirbuf;
1661         int readcnt, retval;
1662         int count, error;
1663         int ncookies;
1664         off_t startoffset = uio->uio_offset;
1665
1666         count = uio->uio_resid;
1667         /*
1668          * Avoid complications for partial directory entries by adjusting
1669          * the i/o to end at a block boundary.  Don't give up (like the old ufs
1670          * does) if the initial adjustment gives a negative count, since
1671          * many callers don't supply a large enough buffer.  The correct
1672          * size is a little larger than DIRBLKSIZ to allow for expansion
1673          * of directory entries, but some callers just use 512.
1674          */
1675         count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1676         if (count <= 0)
1677                 count += DIRBLKSIZ;
1678
1679         auio = *uio;
1680         auio.uio_iov = &aiov;
1681         auio.uio_iovcnt = 1;
1682         auio.uio_resid = count;
1683         auio.uio_segflg = UIO_SYSSPACE;
1684         aiov.iov_len = count;
1685         MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
1686         aiov.iov_base = dirbuf;
1687         error = VOP_READ(vp, &auio, 0, ap->a_cred);
1688         if (error == 0) {
1689                 readcnt = count - auio.uio_resid;
1690                 edp = (struct direct *)&dirbuf[readcnt];
1691                 ncookies = 0;
1692                 for (dp = (struct direct *)dirbuf; 
1693                     !error && uio->uio_resid > 0 && dp < edp; ) {
1694                         if (dp->d_reclen <= 0) {
1695                                 error = EIO;
1696                                 break;
1697                         }
1698 #if BYTE_ORDER == LITTLE_ENDIAN
1699                         if (OFSFMT(vp)) {
1700                                 retval = vop_write_dirent(&error, uio,
1701                                     dp->d_ino, dp->d_namlen, dp->d_type,
1702                                     dp->d_name);
1703                         } else
1704 #endif
1705                         {
1706                                 retval = vop_write_dirent(&error, uio,
1707                                     dp->d_ino, dp->d_type, dp->d_namlen,
1708                                     dp->d_name);
1709                         }
1710
1711                         if (retval)
1712                                 break;
1713                         /* advance dp */
1714                         dp = (struct direct *)((char *)dp + dp->d_reclen); 
1715                         if (!error)
1716                                 ncookies++;
1717                 }
1718                 /* we need to correct uio_offset */
1719                 uio->uio_offset = startoffset + (caddr_t)dp - dirbuf;
1720
1721                 if (!error && ap->a_ncookies != NULL) {
1722                         u_long *cookiep, *cookies, *ecookies;
1723                         off_t off;
1724
1725                         if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1726                                 panic("ufs_readdir: unexpected uio from NFS server");
1727                         MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1728                                M_WAITOK);
1729                         off = startoffset;
1730                         for (dp = (struct direct *)dirbuf,
1731                              cookiep = cookies, ecookies = cookies + ncookies;
1732                              cookiep < ecookies;
1733                              dp = (struct direct *)((caddr_t) dp + dp->d_reclen)) {
1734                                 off += dp->d_reclen;
1735                                 *cookiep++ = (u_long) off;
1736                         }
1737                         *ap->a_ncookies = ncookies;
1738                         *ap->a_cookies = cookies;
1739                 }
1740         }
1741         FREE(dirbuf, M_TEMP);
1742         if (ap->a_eofflag)
1743                 *ap->a_eofflag = VTOI(vp)->i_size <= uio->uio_offset;
1744         return (error);
1745 }
1746
1747 /*
1748  * Return target name of a symbolic link
1749  *
1750  * ufs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1751  */
1752 static
1753 int
1754 ufs_readlink(struct vop_readlink_args *ap)
1755 {
1756         struct vnode *vp = ap->a_vp;
1757         struct inode *ip = VTOI(vp);
1758         int isize;
1759
1760         isize = ip->i_size;
1761         if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1762             (ip->i_din.di_blocks == 0)) {   /* XXX - for old fastlink support */
1763                 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1764                 return (0);
1765         }
1766
1767         /*
1768          * Perform the equivalent of an OPEN on vp so we can issue a
1769          * VOP_READ.
1770          */
1771         return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1772 }
1773
1774 /*
1775  * Calculate the logical to physical mapping if not done already,
1776  * then call the device strategy routine.
1777  *
1778  * In order to be able to swap to a file, the VOP_BMAP operation may not
1779  * deadlock on memory.  See ufs_bmap() for details.
1780  *
1781  * ufs_strategy(struct vnode *a_vp, struct bio *a_bio)
1782  */
1783 static
1784 int
1785 ufs_strategy(struct vop_strategy_args *ap)
1786 {
1787         struct bio *bio = ap->a_bio;
1788         struct bio *nbio;
1789         struct buf *bp = bio->bio_buf;
1790         struct vnode *vp = ap->a_vp;
1791         struct inode *ip;
1792         int error;
1793
1794         ip = VTOI(vp);
1795         if (vp->v_type == VBLK || vp->v_type == VCHR)
1796                 panic("ufs_strategy: spec");
1797         nbio = push_bio(bio);
1798         if (nbio->bio_offset == NOOFFSET) {
1799                 error = VOP_BMAP(vp, bio->bio_offset, NULL, &nbio->bio_offset,
1800                                  NULL, NULL);
1801                 if (error) {
1802                         bp->b_error = error;
1803                         bp->b_flags |= B_ERROR;
1804                         /* I/O was never started on nbio, must biodone(bio) */
1805                         biodone(bio);
1806                         return (error);
1807                 }
1808                 if (nbio->bio_offset == NOOFFSET)
1809                         vfs_bio_clrbuf(bp);
1810         }
1811         if (nbio->bio_offset == NOOFFSET) {
1812                 /* I/O was never started on nbio, must biodone(bio) */
1813                 biodone(bio);
1814                 return (0);
1815         }
1816         vn_strategy(ip->i_devvp, nbio);
1817         return (0);
1818 }
1819
1820 /*
1821  * Print out the contents of an inode.
1822  *
1823  * ufs_print(struct vnode *a_vp)
1824  */
1825 static
1826 int
1827 ufs_print(struct vop_print_args *ap)
1828 {
1829         struct vnode *vp = ap->a_vp;
1830         struct inode *ip = VTOI(vp);
1831
1832         printf("tag VT_UFS, ino %lu, on dev %s (%d, %d)",
1833             (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev),
1834             minor(ip->i_dev));
1835         if (vp->v_type == VFIFO)
1836                 fifo_printinfo(vp);
1837         lockmgr_printinfo(&vp->v_lock);
1838         printf("\n");
1839         return (0);
1840 }
1841
1842 /*
1843  * Read wrapper for special devices.
1844  *
1845  * ufsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1846  *              struct ucred *a_cred)
1847  */
1848 static
1849 int
1850 ufsspec_read(struct vop_read_args *ap)
1851 {
1852         int error, resid;
1853         struct inode *ip;
1854         struct uio *uio;
1855
1856         uio = ap->a_uio;
1857         resid = uio->uio_resid;
1858         error = VOCALL(spec_vnode_vops, &ap->a_head);
1859         /*
1860          * The inode may have been revoked during the call, so it must not
1861          * be accessed blindly here or in the other wrapper functions.
1862          */
1863         ip = VTOI(ap->a_vp);
1864         if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1865                 ip->i_flag |= IN_ACCESS;
1866         return (error);
1867 }
1868
1869 /*
1870  * Write wrapper for special devices.
1871  *
1872  * ufsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1873  *               struct ucred *a_cred)
1874  */
1875 static
1876 int
1877 ufsspec_write(struct vop_write_args *ap)
1878 {
1879         int error, resid;
1880         struct inode *ip;
1881         struct uio *uio;
1882
1883         uio = ap->a_uio;
1884         resid = uio->uio_resid;
1885         error = VOCALL(spec_vnode_vops, &ap->a_head);
1886         ip = VTOI(ap->a_vp);
1887         if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1888                 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1889         return (error);
1890 }
1891
1892 /*
1893  * Close wrapper for special devices.
1894  *
1895  * Update the times on the inode then do device close.
1896  *
1897  * ufsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
1898  *               struct thread *a_td)
1899  */
1900 static 
1901 int
1902 ufsspec_close(struct vop_close_args *ap)
1903 {
1904         struct vnode *vp = ap->a_vp;
1905
1906         if (vp->v_usecount > 1)
1907                 ufs_itimes(vp);
1908         return (VOCALL(spec_vnode_vops, &ap->a_head));
1909 }
1910
1911 /*
1912  * Read wrapper for fifos.
1913  *
1914  * ufsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1915  *              struct ucred *a_cred)
1916  */
1917 static
1918 int
1919 ufsfifo_read(struct vop_read_args *ap)
1920 {
1921         int error, resid;
1922         struct inode *ip;
1923         struct uio *uio;
1924
1925         uio = ap->a_uio;
1926         resid = uio->uio_resid;
1927         error = VOCALL(fifo_vnode_vops, &ap->a_head);
1928         ip = VTOI(ap->a_vp);
1929         if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL &&
1930             (uio->uio_resid != resid || (error == 0 && resid != 0)))
1931                 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1932         return (error);
1933 }
1934
1935 /*
1936  * Write wrapper for fifos.
1937  *
1938  * ufsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1939  *               struct ucred *a_cred)
1940  */
1941 static
1942 int
1943 ufsfifo_write(struct vop_write_args *ap)
1944 {
1945         int error, resid;
1946         struct inode *ip;
1947         struct uio *uio;
1948
1949         uio = ap->a_uio;
1950         resid = uio->uio_resid;
1951         error = VOCALL(fifo_vnode_vops, &ap->a_head);
1952         ip = VTOI(ap->a_vp);
1953         if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1954                 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1955         return (error);
1956 }
1957
1958 /*
1959  * Close wrapper for fifos.
1960  *
1961  * Update the times on the inode then do device close.
1962  *
1963  * ufsfifo_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
1964  *               struct thread *a_td)
1965  */
1966 static
1967 int
1968 ufsfifo_close(struct vop_close_args *ap)
1969 {
1970         struct vnode *vp = ap->a_vp;
1971
1972         if (vp->v_usecount > 1)
1973                 ufs_itimes(vp);
1974         return (VOCALL(fifo_vnode_vops, &ap->a_head));
1975 }
1976
1977 /*
1978  * Kqfilter wrapper for fifos.
1979  *
1980  * Fall through to ufs kqfilter routines if needed 
1981  */
1982 static
1983 int
1984 ufsfifo_kqfilter(struct vop_kqfilter_args *ap)
1985 {
1986         int error;
1987
1988         error = VOCALL(fifo_vnode_vops, &ap->a_head);
1989         if (error)
1990                 error = ufs_kqfilter(ap);
1991         return (error);
1992 }
1993
1994 /*
1995  * Return POSIX pathconf information applicable to ufs filesystems.
1996  *
1997  * ufs_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
1998  */
1999 static
2000 int
2001 ufs_pathconf(struct vop_pathconf_args *ap)
2002 {
2003         switch (ap->a_name) {
2004         case _PC_LINK_MAX:
2005                 *ap->a_retval = LINK_MAX;
2006                 return (0);
2007         case _PC_NAME_MAX:
2008                 *ap->a_retval = NAME_MAX;
2009                 return (0);
2010         case _PC_PATH_MAX:
2011                 *ap->a_retval = PATH_MAX;
2012                 return (0);
2013         case _PC_PIPE_BUF:
2014                 *ap->a_retval = PIPE_BUF;
2015                 return (0);
2016         case _PC_CHOWN_RESTRICTED:
2017                 *ap->a_retval = 1;
2018                 return (0);
2019         case _PC_NO_TRUNC:
2020                 *ap->a_retval = 1;
2021                 return (0);
2022         default:
2023                 return (EINVAL);
2024         }
2025         /* NOTREACHED */
2026 }
2027
2028 /*
2029  * Advisory record locking support
2030  *
2031  * ufs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
2032  *             int a_flags)
2033  */
2034 static
2035 int
2036 ufs_advlock(struct vop_advlock_args *ap)
2037 {
2038         struct inode *ip = VTOI(ap->a_vp);
2039
2040         return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
2041 }
2042
2043 /*
2044  * Initialize the vnode associated with a new inode, handle aliased
2045  * vnodes.
2046  *
2047  * Make sure directories have their VM object now rather then later,
2048  * saving us from having to check on all the myrid directory VOPs
2049  * that might be executed without a VOP_OPEN being performed.
2050  */
2051 int
2052 ufs_vinit(struct mount *mntp, struct vnode **vpp)
2053 {
2054         struct inode *ip;
2055         struct vnode *vp;
2056         struct timeval tv;
2057
2058         vp = *vpp;
2059         ip = VTOI(vp);
2060
2061         vp->v_type = IFTOVT(ip->i_mode);
2062
2063         switch(vp->v_type) {
2064         case VCHR:
2065         case VBLK:
2066                 vp->v_ops = &mntp->mnt_vn_spec_ops;
2067                 addaliasu(vp, ip->i_rdev);
2068                 break;
2069         case VFIFO:
2070                 vp->v_ops = &mntp->mnt_vn_fifo_ops;
2071                 break;
2072         case VDIR:
2073         case VREG:
2074                 vinitvmio(vp, ip->i_size);
2075                 break;
2076         case VLNK:
2077                 if (ip->i_size >= vp->v_mount->mnt_maxsymlinklen)
2078                         vinitvmio(vp, ip->i_size);
2079                 break;
2080         default:
2081                 break;
2082
2083         }
2084
2085         if (ip->i_number == ROOTINO)
2086                 vp->v_flag |= VROOT;
2087         /*
2088          * Initialize modrev times
2089          */
2090         getmicrouptime(&tv);
2091         SETHIGH(ip->i_modrev, tv.tv_sec);
2092         SETLOW(ip->i_modrev, tv.tv_usec * 4294);
2093         *vpp = vp;
2094         return (0);
2095 }
2096
2097 /*
2098  * Allocate a new inode.
2099  */
2100 static
2101 int
2102 ufs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
2103               struct componentname *cnp)
2104 {
2105         struct inode *ip, *pdir;
2106         struct direct newdir;
2107         struct vnode *tvp;
2108         int error;
2109
2110         pdir = VTOI(dvp);
2111         *vpp = NULL;
2112         if ((mode & IFMT) == 0)
2113                 mode |= IFREG;
2114
2115         error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2116         if (error)
2117                 return (error);
2118         ip = VTOI(tvp);
2119         ip->i_gid = pdir->i_gid;
2120 #ifdef SUIDDIR
2121         {
2122 #ifdef QUOTA
2123                 struct ucred ucred, *ucp;
2124                 ucp = cnp->cn_cred;
2125 #endif
2126                 /*
2127                  * If we are not the owner of the directory,
2128                  * and we are hacking owners here, (only do this where told to)
2129                  * and we are not giving it TO root, (would subvert quotas)
2130                  * then go ahead and give it to the other user.
2131                  * Note that this drops off the execute bits for security.
2132                  */
2133                 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2134                     (pdir->i_mode & ISUID) &&
2135                     (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2136                         ip->i_uid = pdir->i_uid;
2137                         mode &= ~07111;
2138 #ifdef QUOTA
2139                         /*
2140                          * Make sure the correct user gets charged
2141                          * for the space.
2142                          * Quickly knock up a dummy credential for the victim.
2143                          * XXX This seems to never be accessed out of our
2144                          * context so a stack variable is ok.
2145                          */
2146                         ucred.cr_ref = 1;
2147                         ucred.cr_uid = ip->i_uid;
2148                         ucred.cr_ngroups = 1;
2149                         ucred.cr_groups[0] = pdir->i_gid;
2150                         ucp = &ucred;
2151 #endif
2152                 } else
2153                         ip->i_uid = cnp->cn_cred->cr_uid;
2154
2155 #ifdef QUOTA
2156                 if ((error = ufs_getinoquota(ip)) ||
2157                     (error = ufs_chkiq(ip, 1, ucp, 0))) {
2158                         UFS_VFREE(tvp, ip->i_number, mode);
2159                         vput(tvp);
2160                         return (error);
2161                 }
2162 #endif
2163         }
2164 #else   /* !SUIDDIR */
2165         ip->i_uid = cnp->cn_cred->cr_uid;
2166 #ifdef QUOTA
2167         if ((error = ufs_getinoquota(ip)) ||
2168             (error = ufs_chkiq(ip, 1, cnp->cn_cred, 0))) {
2169                 UFS_VFREE(tvp, ip->i_number, mode);
2170                 vput(tvp);
2171                 return (error);
2172         }
2173 #endif
2174 #endif  /* !SUIDDIR */
2175         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2176         ip->i_mode = mode;
2177         tvp->v_type = IFTOVT(mode);     /* Rest init'd in getnewvnode(). */
2178         ip->i_effnlink = 1;
2179         ip->i_nlink = 1;
2180         if (DOINGSOFTDEP(tvp))
2181                 softdep_change_linkcnt(ip);
2182         if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2183             suser_cred(cnp->cn_cred, 0)) {
2184                 ip->i_mode &= ~ISGID;
2185         }
2186
2187         if (cnp->cn_flags & CNP_ISWHITEOUT)
2188                 ip->i_flags |= UF_OPAQUE;
2189
2190         /*
2191          * Regular files and directories need VM objects.  Softlinks do
2192          * not (not immediately anyway).
2193          */
2194         if (tvp->v_type == VREG || tvp->v_type == VDIR)
2195                 vinitvmio(tvp, 0);
2196
2197         /*
2198          * Make sure inode goes to disk before directory entry.
2199          */
2200         error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2201         if (error)
2202                 goto bad;
2203         ufs_makedirentry(ip, cnp, &newdir);
2204         error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2205         if (error)
2206                 goto bad;
2207         *vpp = tvp;
2208         return (0);
2209
2210 bad:
2211         /*
2212          * Write error occurred trying to update the inode
2213          * or the directory so must deallocate the inode.
2214          */
2215         ip->i_effnlink = 0;
2216         ip->i_nlink = 0;
2217         ip->i_flag |= IN_CHANGE;
2218         if (DOINGSOFTDEP(tvp))
2219                 softdep_change_linkcnt(ip);
2220         vput(tvp);
2221         return (error);
2222 }
2223
2224 static int
2225 ufs_missingop(struct vop_generic_args *ap)
2226 {
2227         panic("no vop function for %s in ufs child", ap->a_desc->vdesc_name);
2228         return (EOPNOTSUPP);
2229 }
2230
2231 static struct filterops ufsread_filtops = 
2232         { 1, NULL, filt_ufsdetach, filt_ufsread };
2233 static struct filterops ufswrite_filtops = 
2234         { 1, NULL, filt_ufsdetach, filt_ufswrite };
2235 static struct filterops ufsvnode_filtops = 
2236         { 1, NULL, filt_ufsdetach, filt_ufsvnode };
2237
2238 /*
2239  * ufs_kqfilter(struct vnode *a_vp, struct knote *a_kn)
2240  */
2241 static int
2242 ufs_kqfilter(struct vop_kqfilter_args *ap)
2243 {
2244         struct vnode *vp = ap->a_vp;
2245         struct knote *kn = ap->a_kn;
2246         lwkt_tokref ilock;
2247
2248         switch (kn->kn_filter) {
2249         case EVFILT_READ:
2250                 kn->kn_fop = &ufsread_filtops;
2251                 break;
2252         case EVFILT_WRITE:
2253                 kn->kn_fop = &ufswrite_filtops;
2254                 break;
2255         case EVFILT_VNODE:
2256                 kn->kn_fop = &ufsvnode_filtops;
2257                 break;
2258         default:
2259                 return (1);
2260         }
2261
2262         kn->kn_hook = (caddr_t)vp;
2263
2264         lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2265         SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
2266         lwkt_reltoken(&ilock);
2267
2268         return (0);
2269 }
2270
2271 static void
2272 filt_ufsdetach(struct knote *kn)
2273 {
2274         struct vnode *vp = (struct vnode *)kn->kn_hook;
2275         lwkt_tokref ilock;
2276
2277         lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2278         SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
2279             kn, knote, kn_selnext);
2280         lwkt_reltoken(&ilock);
2281 }
2282
2283 /*ARGSUSED*/
2284 static int
2285 filt_ufsread(struct knote *kn, long hint)
2286 {
2287         struct vnode *vp = (struct vnode *)kn->kn_hook;
2288         struct inode *ip = VTOI(vp);
2289
2290         /*
2291          * filesystem is gone, so set the EOF flag and schedule 
2292          * the knote for deletion.
2293          */
2294         if (hint == NOTE_REVOKE) {
2295                 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2296                 return (1);
2297         }
2298
2299         kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
2300         return (kn->kn_data != 0);
2301 }
2302
2303 /*ARGSUSED*/
2304 static int
2305 filt_ufswrite(struct knote *kn, long hint)
2306 {
2307         /*
2308          * filesystem is gone, so set the EOF flag and schedule 
2309          * the knote for deletion.
2310          */
2311         if (hint == NOTE_REVOKE)
2312                 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2313
2314         kn->kn_data = 0;
2315         return (1);
2316 }
2317
2318 static int
2319 filt_ufsvnode(struct knote *kn, long hint)
2320 {
2321         if (kn->kn_sfflags & hint)
2322                 kn->kn_fflags |= hint;
2323         if (hint == NOTE_REVOKE) {
2324                 kn->kn_flags |= EV_EOF;
2325                 return (1);
2326         }
2327         return (kn->kn_fflags != 0);
2328 }
2329
2330 /* Global vfs data structures for ufs. */
2331 static struct vop_ops *ufs_vnode_vops;
2332 static struct vnodeopv_entry_desc ufs_vnodeop_entries[] = {
2333         { &vop_default_desc,            vop_defaultop },
2334         { &vop_fsync_desc,              (vnodeopv_entry_t) ufs_missingop },
2335         { &vop_read_desc,               (vnodeopv_entry_t) ufs_missingop },
2336         { &vop_reallocblks_desc,        (vnodeopv_entry_t) ufs_missingop },
2337         { &vop_write_desc,              (vnodeopv_entry_t) ufs_missingop },
2338         { &vop_access_desc,             (vnodeopv_entry_t) ufs_access },
2339         { &vop_advlock_desc,            (vnodeopv_entry_t) ufs_advlock },
2340         { &vop_bmap_desc,               (vnodeopv_entry_t) ufs_bmap },
2341         { &vop_old_lookup_desc,         (vnodeopv_entry_t) ufs_lookup },
2342         { &vop_close_desc,              (vnodeopv_entry_t) ufs_close },
2343         { &vop_old_create_desc,         (vnodeopv_entry_t) ufs_create },
2344         { &vop_getattr_desc,            (vnodeopv_entry_t) ufs_getattr },
2345         { &vop_inactive_desc,           (vnodeopv_entry_t) ufs_inactive },
2346         { &vop_islocked_desc,           (vnodeopv_entry_t) vop_stdislocked },
2347         { &vop_old_link_desc,           (vnodeopv_entry_t) ufs_link },
2348         { &vop_lock_desc,               (vnodeopv_entry_t) vop_stdlock },
2349         { &vop_old_mkdir_desc,          (vnodeopv_entry_t) ufs_mkdir },
2350         { &vop_old_mknod_desc,          (vnodeopv_entry_t) ufs_mknod },
2351         { &vop_mmap_desc,               (vnodeopv_entry_t) ufs_mmap },
2352         { &vop_open_desc,               (vnodeopv_entry_t) ufs_open },
2353         { &vop_pathconf_desc,           (vnodeopv_entry_t) ufs_pathconf },
2354         { &vop_poll_desc,               (vnodeopv_entry_t) vop_stdpoll },
2355         { &vop_kqfilter_desc,           (vnodeopv_entry_t) ufs_kqfilter },
2356         { &vop_print_desc,              (vnodeopv_entry_t) ufs_print },
2357         { &vop_readdir_desc,            (vnodeopv_entry_t) ufs_readdir },
2358         { &vop_readlink_desc,           (vnodeopv_entry_t) ufs_readlink },
2359         { &vop_reclaim_desc,            (vnodeopv_entry_t) ufs_reclaim },
2360         { &vop_old_remove_desc,         (vnodeopv_entry_t) ufs_remove },
2361         { &vop_old_rename_desc,         (vnodeopv_entry_t) ufs_rename },
2362         { &vop_old_rmdir_desc,          (vnodeopv_entry_t) ufs_rmdir },
2363         { &vop_setattr_desc,            (vnodeopv_entry_t) ufs_setattr },
2364         { &vop_strategy_desc,           (vnodeopv_entry_t) ufs_strategy },
2365         { &vop_old_symlink_desc,        (vnodeopv_entry_t) ufs_symlink },
2366         { &vop_unlock_desc,             (vnodeopv_entry_t) vop_stdunlock },
2367         { &vop_old_whiteout_desc,       (vnodeopv_entry_t) ufs_whiteout },
2368         { NULL, NULL }
2369 };
2370 static struct vnodeopv_desc ufs_vnodeop_opv_desc =
2371         { &ufs_vnode_vops, ufs_vnodeop_entries, VVF_SUPPORTS_FSMID };
2372
2373 static struct vop_ops *ufs_spec_vops;
2374 static struct vnodeopv_entry_desc ufs_specop_entries[] = {
2375         { &vop_default_desc,            (vnodeopv_entry_t) spec_vnoperate },
2376         { &vop_fsync_desc,              (vnodeopv_entry_t) ufs_missingop },
2377         { &vop_access_desc,             (vnodeopv_entry_t) ufs_access },
2378         { &vop_close_desc,              (vnodeopv_entry_t) ufsspec_close },
2379         { &vop_getattr_desc,            (vnodeopv_entry_t) ufs_getattr },
2380         { &vop_inactive_desc,           (vnodeopv_entry_t) ufs_inactive },
2381         { &vop_islocked_desc,           (vnodeopv_entry_t) vop_stdislocked },
2382         { &vop_lock_desc,               (vnodeopv_entry_t) vop_stdlock },
2383         { &vop_print_desc,              (vnodeopv_entry_t) ufs_print },
2384         { &vop_read_desc,               (vnodeopv_entry_t) ufsspec_read },
2385         { &vop_reclaim_desc,            (vnodeopv_entry_t) ufs_reclaim },
2386         { &vop_setattr_desc,            (vnodeopv_entry_t) ufs_setattr },
2387         { &vop_unlock_desc,             (vnodeopv_entry_t) vop_stdunlock },
2388         { &vop_write_desc,              (vnodeopv_entry_t) ufsspec_write },
2389         { NULL, NULL }
2390 };
2391 static struct vnodeopv_desc ufs_specop_opv_desc =
2392         { &ufs_spec_vops, ufs_specop_entries, VVF_SUPPORTS_FSMID };
2393
2394 static struct vop_ops *ufs_fifo_vops;
2395 static struct vnodeopv_entry_desc ufs_fifoop_entries[] = {
2396         { &vop_default_desc,            (vnodeopv_entry_t) fifo_vnoperate },
2397         { &vop_fsync_desc,              (vnodeopv_entry_t) ufs_missingop },
2398         { &vop_access_desc,             (vnodeopv_entry_t) ufs_access },
2399         { &vop_close_desc,              (vnodeopv_entry_t) ufsfifo_close },
2400         { &vop_getattr_desc,            (vnodeopv_entry_t) ufs_getattr },
2401         { &vop_inactive_desc,           (vnodeopv_entry_t) ufs_inactive },
2402         { &vop_islocked_desc,           (vnodeopv_entry_t) vop_stdislocked },
2403         { &vop_kqfilter_desc,           (vnodeopv_entry_t) ufsfifo_kqfilter },
2404         { &vop_lock_desc,               (vnodeopv_entry_t) vop_stdlock },
2405         { &vop_print_desc,              (vnodeopv_entry_t) ufs_print },
2406         { &vop_read_desc,               (vnodeopv_entry_t) ufsfifo_read },
2407         { &vop_reclaim_desc,            (vnodeopv_entry_t) ufs_reclaim },
2408         { &vop_setattr_desc,            (vnodeopv_entry_t) ufs_setattr },
2409         { &vop_unlock_desc,             (vnodeopv_entry_t) vop_stdunlock },
2410         { &vop_write_desc,              (vnodeopv_entry_t) ufsfifo_write },
2411         { NULL, NULL }
2412 };
2413 static struct vnodeopv_desc ufs_fifoop_opv_desc =
2414         { &ufs_fifo_vops, ufs_fifoop_entries, VVF_SUPPORTS_FSMID };
2415
2416 VNODEOP_SET(ufs_vnodeop_opv_desc);
2417 VNODEOP_SET(ufs_specop_opv_desc);
2418 VNODEOP_SET(ufs_fifoop_opv_desc);
2419
2420 /*
2421  * ufs_vnoperate(struct vnodeop_desc *a_desc)
2422  */
2423 int
2424 ufs_vnoperate(struct vop_generic_args *ap)
2425 {
2426         return (VOCALL(ufs_vnode_vops, ap));
2427 }
2428
2429 /*
2430  * ufs_vnoperatefifo(struct vnodeop_desc *a_desc)
2431  */
2432 int
2433 ufs_vnoperatefifo(struct vop_generic_args *ap)
2434 {
2435         return (VOCALL(ufs_fifo_vops, ap));
2436 }
2437
2438 /*
2439  * ufs_vnoperatespec(struct vnodeop_desc *a_desc)
2440  */
2441 int
2442 ufs_vnoperatespec(struct vop_generic_args *ap)
2443 {
2444         return (VOCALL(ufs_spec_vops, ap));
2445 }