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