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