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