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