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