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