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