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