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