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