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