kernel - More buffer cache / VM coherency work
[dragonfly.git] / sys / vfs / ufs / ufs_vnops.c
blob990b5a27bacf86403266b1406ce3204f74105ed7
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.
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.
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.
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 $
43 #include "opt_quota.h"
44 #include "opt_suiddir.h"
45 #include "opt_ufs.h"
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>
65 #include <sys/file.h> /* XXX */
66 #include <sys/jail.h>
68 #include <vm/vm.h>
69 #include <vm/vm_extern.h>
71 #include <vfs/fifofs/fifo.h>
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
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);
117 union _qcvt {
118 int64_t qcvt;
119 int32_t val[2];
121 #define SETHIGH(q, h) { \
122 union _qcvt tmp; \
123 tmp.qcvt = (q); \
124 tmp.val[_QUAD_HIGHWORD] = (h); \
125 (q) = tmp.qcvt; \
127 #define SETLOW(q, l) { \
128 union _qcvt tmp; \
129 tmp.qcvt = (q); \
130 tmp.val[_QUAD_LOWWORD] = (l); \
131 (q) = tmp.qcvt; \
133 #define VN_KNOTE(vp, b) \
134 KNOTE(&vp->v_pollinfo.vpi_selinfo.si_note, (b))
136 #define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
139 * A virgin directory (no blushing please).
141 static struct dirtemplate mastertemplate = {
142 0, 12, DT_DIR, 1, ".",
143 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
145 static struct odirtemplate omastertemplate = {
146 0, 12, 1, ".",
147 0, DIRBLKSIZ - 12, 2, ".."
150 void
151 ufs_itimes(struct vnode *vp)
153 struct inode *ip;
154 struct timespec ts;
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;
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++;
174 if (ip->i_flag & IN_CHANGE) {
175 ip->i_ctime = ts.tv_sec;
176 ip->i_ctimensec = ts.tv_nsec;
179 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
183 * Create a regular file
185 * ufs_create(struct vnode *a_dvp, struct vnode **a_vpp,
186 * struct componentname *a_cnp, struct vattr *a_vap)
188 static
190 ufs_create(struct vop_old_create_args *ap)
192 int error;
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);
204 * Mknod vnode call
206 * ufs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
207 * struct componentname *a_cnp, struct vattr *a_vap)
209 /* ARGSUSED */
210 static
212 ufs_mknod(struct vop_old_mknod_args *ap)
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;
221 * UFS cannot represent the entire major/minor range supported by
222 * the kernel.
224 if (vap->va_rmajor != VNOVAL &&
225 makeudev(vap->va_rmajor, vap->va_rminor) == NOUDEV) {
226 return(EINVAL);
229 /* no special directory support */
230 if (vap->va_type == VDIR)
231 return(EINVAL);
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) {
242 * Want to be able to use this to make badblock
243 * inodes, so don't truncate the dev number.
245 ip->i_rdev = makeudev(vap->va_rmajor, vap->va_rminor);
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.
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);
261 return (0);
265 * Close called.
267 * Update the times on the inode.
269 * ufs_close(struct vnode *a_vp, int a_fflag)
271 /* ARGSUSED */
272 static
274 ufs_close(struct vop_close_args *ap)
276 struct vnode *vp = ap->a_vp;
278 if (vp->v_sysref.refcnt > 1)
279 ufs_itimes(vp);
280 return (vop_stdclose(ap));
284 * ufs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
286 static
288 ufs_access(struct vop_access_args *ap)
290 struct vnode *vp = ap->a_vp;
291 struct inode *ip = VTOI(vp);
292 int error;
294 #ifdef QUOTA
295 if (ap->a_mode & VWRITE) {
296 switch (vp->v_type) {
297 case VDIR:
298 case VLNK:
299 case VREG:
300 if ((error = ufs_getinoquota(ip)) != 0)
301 return (error);
302 break;
303 default:
304 break;
307 #endif
309 error = vop_helper_access(ap, ip->i_uid, ip->i_gid, ip->i_mode, 0);
310 return (error);
314 * ufs_getattr(struct vnode *a_vp, struct vattr *a_vap)
316 /* ARGSUSED */
317 static
319 ufs_getattr(struct vop_getattr_args *ap)
321 struct vnode *vp = ap->a_vp;
322 struct inode *ip = VTOI(vp);
323 struct vattr *vap = ap->a_vap;
325 ufs_itimes(vp);
327 * Copy from inode table
329 vap->va_fsid = dev2udev(ip->i_dev);
330 vap->va_fileid = ip->i_number;
331 vap->va_mode = ip->i_mode & ~IFMT;
332 vap->va_nlink = VFSTOUFS(vp->v_mount)->um_i_effnlink_valid ?
333 ip->i_effnlink : ip->i_nlink;
334 vap->va_uid = ip->i_uid;
335 vap->va_gid = ip->i_gid;
336 vap->va_rmajor = umajor(ip->i_rdev);
337 vap->va_rminor = uminor(ip->i_rdev);
338 vap->va_size = ip->i_din.di_size;
339 vap->va_atime.tv_sec = ip->i_atime;
340 vap->va_atime.tv_nsec = ip->i_atimensec;
341 vap->va_mtime.tv_sec = ip->i_mtime;
342 vap->va_mtime.tv_nsec = ip->i_mtimensec;
343 vap->va_ctime.tv_sec = ip->i_ctime;
344 vap->va_ctime.tv_nsec = ip->i_ctimensec;
345 vap->va_flags = ip->i_flags;
346 vap->va_gen = ip->i_gen;
347 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
348 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
349 vap->va_type = IFTOVT(ip->i_mode);
350 vap->va_filerev = ip->i_modrev;
351 return (0);
354 static
356 ufs_markatime(struct vop_markatime_args *ap)
358 struct vnode *vp = ap->a_vp;
359 struct inode *ip = VTOI(vp);
361 if (vp->v_mount->mnt_flag & MNT_RDONLY)
362 return (EROFS);
363 if (vp->v_mount->mnt_flag & MNT_NOATIME)
364 return (0);
365 ip->i_flag |= IN_ACCESS;
366 VN_KNOTE(vp, NOTE_ATTRIB);
367 return (0);
371 * Set attribute vnode op. called from several syscalls
373 * ufs_setattr(struct vnode *a_vp, struct vattr *a_vap,
374 * struct ucred *a_cred)
376 static
378 ufs_setattr(struct vop_setattr_args *ap)
380 struct vattr *vap = ap->a_vap;
381 struct vnode *vp = ap->a_vp;
382 struct inode *ip = VTOI(vp);
383 struct ucred *cred = ap->a_cred;
384 int error;
387 * Check for unsettable attributes.
389 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
390 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
391 (vap->va_blocksize != VNOVAL) || (vap->va_rmajor != VNOVAL) ||
392 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
393 return (EINVAL);
395 if (vap->va_flags != VNOVAL) {
396 if (vp->v_mount->mnt_flag & MNT_RDONLY)
397 return (EROFS);
398 if (cred->cr_uid != ip->i_uid &&
399 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)))
400 return (error);
402 * Note that a root chflags becomes a user chflags when
403 * we are jailed, unless the jail.chflags_allowed sysctl
404 * is set.
406 if (cred->cr_uid == 0 &&
407 (!jailed(cred) || jail_chflags_allowed)) {
408 if ((ip->i_flags
409 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) &&
410 securelevel > 0)
411 return (EPERM);
412 ip->i_flags = vap->va_flags;
413 } else {
414 if (ip->i_flags
415 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
416 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
417 return (EPERM);
418 ip->i_flags &= SF_SETTABLE;
419 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
421 ip->i_flag |= IN_CHANGE;
422 if (vap->va_flags & (IMMUTABLE | APPEND))
423 return (0);
425 if (ip->i_flags & (IMMUTABLE | APPEND))
426 return (EPERM);
428 * Go through the fields and update iff not VNOVAL.
430 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
431 if (vp->v_mount->mnt_flag & MNT_RDONLY)
432 return (EROFS);
433 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred)) != 0)
434 return (error);
436 if (vap->va_size != VNOVAL) {
438 * Disallow write attempts on read-only filesystems;
439 * unless the file is a socket, fifo, or a block or
440 * character device resident on the filesystem.
442 switch (vp->v_type) {
443 case VDIR:
444 return (EISDIR);
445 case VLNK:
446 case VREG:
447 if (vp->v_mount->mnt_flag & MNT_RDONLY)
448 return (EROFS);
449 break;
450 default:
451 break;
453 if ((error = ffs_truncate(vp, vap->va_size, 0, cred)) != 0)
454 return (error);
456 ip = VTOI(vp);
457 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
458 if (vp->v_mount->mnt_flag & MNT_RDONLY)
459 return (EROFS);
460 if (cred->cr_uid != ip->i_uid &&
461 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)) &&
462 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
463 (error = VOP_EACCESS(vp, VWRITE, cred))))
464 return (error);
465 if (vap->va_atime.tv_sec != VNOVAL)
466 ip->i_flag |= IN_ACCESS;
467 if (vap->va_mtime.tv_sec != VNOVAL)
468 ip->i_flag |= IN_CHANGE | IN_UPDATE;
469 ufs_itimes(vp);
470 if (vap->va_atime.tv_sec != VNOVAL) {
471 ip->i_atime = vap->va_atime.tv_sec;
472 ip->i_atimensec = vap->va_atime.tv_nsec;
474 if (vap->va_mtime.tv_sec != VNOVAL) {
475 ip->i_mtime = vap->va_mtime.tv_sec;
476 ip->i_mtimensec = vap->va_mtime.tv_nsec;
478 error = ffs_update(vp, 0);
479 if (error)
480 return (error);
482 error = 0;
483 if (vap->va_mode != (mode_t)VNOVAL) {
484 if (vp->v_mount->mnt_flag & MNT_RDONLY)
485 return (EROFS);
486 error = ufs_chmod(vp, (int)vap->va_mode, cred);
488 VN_KNOTE(vp, NOTE_ATTRIB);
489 return (error);
493 * Change the mode on a file.
494 * Inode must be locked before calling.
496 static int
497 ufs_chmod(struct vnode *vp, int mode, struct ucred *cred)
499 struct inode *ip = VTOI(vp);
500 int error;
501 mode_t cur_mode = ip->i_mode;
503 error = vop_helper_chmod(vp, mode, cred, ip->i_uid, ip->i_gid,
504 &cur_mode);
505 if (error)
506 return (error);
507 #if 0
508 if (cred->cr_uid != ip->i_uid) {
509 error = priv_check_cred(cred, PRIV_VFS_CHMOD, 0);
510 if (error)
511 return (error);
513 if (cred->cr_uid) {
514 if (vp->v_type != VDIR && (mode & S_ISTXT))
515 return (EFTYPE);
516 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
517 return (EPERM);
519 #endif
520 ip->i_mode = cur_mode;
521 ip->i_flag |= IN_CHANGE;
522 return (0);
526 * Perform chown operation on inode ip;
527 * inode must be locked prior to call.
529 static int
530 ufs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred)
532 struct inode *ip = VTOI(vp);
533 uid_t ouid;
534 gid_t ogid;
535 int error = 0;
536 #ifdef QUOTA
537 int i;
538 long change;
539 #endif
541 if (uid == (uid_t)VNOVAL)
542 uid = ip->i_uid;
543 if (gid == (gid_t)VNOVAL)
544 gid = ip->i_gid;
546 * If we don't own the file, are trying to change the owner
547 * of the file, or are not a member of the target group,
548 * the caller must be superuser or the call fails.
550 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
551 (gid != ip->i_gid && !(cred->cr_gid == gid ||
552 groupmember((gid_t)gid, cred)))) &&
553 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
554 return (error);
555 ogid = ip->i_gid;
556 ouid = ip->i_uid;
557 #ifdef QUOTA
558 if ((error = ufs_getinoquota(ip)) != 0)
559 return (error);
560 if (ouid == uid) {
561 ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
562 ip->i_dquot[USRQUOTA] = NODQUOT;
564 if (ogid == gid) {
565 ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
566 ip->i_dquot[GRPQUOTA] = NODQUOT;
568 change = ip->i_blocks;
569 (void) ufs_chkdq(ip, -change, cred, CHOWN);
570 (void) ufs_chkiq(ip, -1, cred, CHOWN);
571 for (i = 0; i < MAXQUOTAS; i++) {
572 ufs_dqrele(vp, ip->i_dquot[i]);
573 ip->i_dquot[i] = NODQUOT;
575 #endif
576 ip->i_gid = gid;
577 ip->i_uid = uid;
578 #ifdef QUOTA
579 if ((error = ufs_getinoquota(ip)) == 0) {
580 if (ouid == uid) {
581 ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
582 ip->i_dquot[USRQUOTA] = NODQUOT;
584 if (ogid == gid) {
585 ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
586 ip->i_dquot[GRPQUOTA] = NODQUOT;
588 if ((error = ufs_chkdq(ip, change, cred, CHOWN)) == 0) {
589 if ((error = ufs_chkiq(ip, 1, cred, CHOWN)) == 0)
590 goto good;
591 else
592 (void)ufs_chkdq(ip, -change, cred, CHOWN|FORCE);
594 for (i = 0; i < MAXQUOTAS; i++) {
595 ufs_dqrele(vp, ip->i_dquot[i]);
596 ip->i_dquot[i] = NODQUOT;
599 ip->i_gid = ogid;
600 ip->i_uid = ouid;
601 if (ufs_getinoquota(ip) == 0) {
602 if (ouid == uid) {
603 ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
604 ip->i_dquot[USRQUOTA] = NODQUOT;
606 if (ogid == gid) {
607 ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
608 ip->i_dquot[GRPQUOTA] = NODQUOT;
610 (void) ufs_chkdq(ip, change, cred, FORCE|CHOWN);
611 (void) ufs_chkiq(ip, 1, cred, FORCE|CHOWN);
612 (void) ufs_getinoquota(ip);
614 return (error);
615 good:
616 if (ufs_getinoquota(ip))
617 panic("ufs_chown: lost quota");
618 #endif /* QUOTA */
619 ip->i_flag |= IN_CHANGE;
620 if (cred->cr_uid != 0 && (ouid != uid || ogid != gid))
621 ip->i_mode &= ~(ISUID | ISGID);
622 return (0);
626 * Mmap a file
628 * NB Currently unsupported.
630 * ufs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
632 /* ARGSUSED */
633 static
635 ufs_mmap(struct vop_mmap_args *ap)
637 return (EINVAL);
641 * ufs_remove(struct vnode *a_dvp, struct vnode *a_vp,
642 * struct componentname *a_cnp)
644 static
646 ufs_remove(struct vop_old_remove_args *ap)
648 struct inode *ip;
649 struct vnode *vp = ap->a_vp;
650 struct vnode *dvp = ap->a_dvp;
651 int error;
653 ip = VTOI(vp);
654 #if 0 /* handled by kernel now */
655 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
656 (VTOI(dvp)->i_flags & APPEND)) {
657 error = EPERM;
658 goto out;
660 #endif
661 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
662 VN_KNOTE(vp, NOTE_DELETE);
663 VN_KNOTE(dvp, NOTE_WRITE);
664 #if 0
665 out:
666 #endif
667 return (error);
671 * link vnode call
673 * ufs_link(struct vnode *a_tdvp, struct vnode *a_vp,
674 * struct componentname *a_cnp)
676 static
678 ufs_link(struct vop_old_link_args *ap)
680 struct vnode *vp = ap->a_vp;
681 struct vnode *tdvp = ap->a_tdvp;
682 struct componentname *cnp = ap->a_cnp;
683 struct inode *ip;
684 struct direct newdir;
685 int error;
687 if (tdvp->v_mount != vp->v_mount) {
688 error = EXDEV;
689 goto out2;
691 if (tdvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE))) {
692 goto out2;
694 ip = VTOI(vp);
695 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
696 error = EMLINK;
697 goto out1;
699 #if 0 /* handled by kernel now, also DragonFly allows this */
700 if (ip->i_flags & (IMMUTABLE | APPEND)) {
701 error = EPERM;
702 goto out1;
704 #endif
705 ip->i_effnlink++;
706 ip->i_nlink++;
707 ip->i_flag |= IN_CHANGE;
708 if (DOINGSOFTDEP(vp))
709 softdep_change_linkcnt(ip);
710 error = ffs_update(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
711 if (!error) {
712 ufs_makedirentry(ip, cnp, &newdir);
713 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
716 if (error) {
717 ip->i_effnlink--;
718 ip->i_nlink--;
719 ip->i_flag |= IN_CHANGE;
720 if (DOINGSOFTDEP(vp))
721 softdep_change_linkcnt(ip);
723 out1:
724 if (tdvp != vp)
725 vn_unlock(vp);
726 out2:
727 VN_KNOTE(vp, NOTE_LINK);
728 VN_KNOTE(tdvp, NOTE_WRITE);
729 return (error);
733 * whiteout vnode call
735 * ufs_whiteout(struct vnode *a_dvp, struct componentname *a_cnp, int a_flags)
737 static
739 ufs_whiteout(struct vop_old_whiteout_args *ap)
741 struct vnode *dvp = ap->a_dvp;
742 struct componentname *cnp = ap->a_cnp;
743 struct direct newdir;
744 int error = 0;
746 switch (ap->a_flags) {
747 case NAMEI_LOOKUP:
748 /* 4.4 format directories support whiteout operations */
749 if (dvp->v_mount->mnt_maxsymlinklen > 0)
750 return (0);
751 return (EOPNOTSUPP);
753 case NAMEI_CREATE:
754 /* create a new directory whiteout */
755 #ifdef DIAGNOSTIC
756 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
757 panic("ufs_whiteout: old format filesystem");
758 #endif
760 newdir.d_ino = WINO;
761 newdir.d_namlen = cnp->cn_namelen;
762 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
763 newdir.d_type = DT_WHT;
764 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
765 break;
767 case NAMEI_DELETE:
768 /* remove an existing directory whiteout */
769 #ifdef DIAGNOSTIC
770 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
771 panic("ufs_whiteout: old format filesystem");
772 #endif
774 cnp->cn_flags &= ~CNP_DOWHITEOUT;
775 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
776 break;
777 default:
778 panic("ufs_whiteout: unknown op");
780 return (error);
784 * Rename system call.
785 * rename("foo", "bar");
786 * is essentially
787 * unlink("bar");
788 * link("foo", "bar");
789 * unlink("foo");
790 * but ``atomically''. Can't do full commit without saving state in the
791 * inode on disk which isn't feasible at this time. Best we can do is
792 * always guarantee the target exists.
794 * Basic algorithm is:
796 * 1) Bump link count on source while we're linking it to the
797 * target. This also ensure the inode won't be deleted out
798 * from underneath us while we work (it may be truncated by
799 * a concurrent `trunc' or `open' for creation).
800 * 2) Link source to destination. If destination already exists,
801 * delete it first.
802 * 3) Unlink source reference to inode if still around. If a
803 * directory was moved and the parent of the destination
804 * is different from the source, patch the ".." entry in the
805 * directory.
807 * ufs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
808 * struct componentname *a_fcnp, struct vnode *a_tdvp,
809 * struct vnode *a_tvp, struct componentname *a_tcnp)
811 static
813 ufs_rename(struct vop_old_rename_args *ap)
815 struct vnode *tvp = ap->a_tvp;
816 struct vnode *tdvp = ap->a_tdvp;
817 struct vnode *fvp = ap->a_fvp;
818 struct vnode *fdvp = ap->a_fdvp;
819 struct componentname *tcnp = ap->a_tcnp;
820 struct componentname *fcnp = ap->a_fcnp;
821 struct inode *ip, *xp, *dp;
822 struct direct newdir;
823 ino_t oldparent = 0, newparent = 0;
824 int doingdirectory = 0;
825 int error = 0, ioflag;
828 * Check for cross-device rename.
830 if ((fvp->v_mount != tdvp->v_mount) ||
831 (tvp && (fvp->v_mount != tvp->v_mount))) {
832 error = EXDEV;
833 abortit:
834 if (tdvp == tvp)
835 vrele(tdvp);
836 else
837 vput(tdvp);
838 if (tvp)
839 vput(tvp);
840 vrele(fdvp);
841 vrele(fvp);
842 return (error);
845 #if 0 /* handled by kernel now */
846 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
847 (VTOI(tdvp)->i_flags & APPEND))) {
848 error = EPERM;
849 goto abortit;
851 #endif
854 * Renaming a file to itself has no effect. The upper layers should
855 * not call us in that case. Temporarily just warn if they do.
857 if (fvp == tvp) {
858 kprintf("ufs_rename: fvp == tvp (can't happen)\n");
859 error = 0;
860 goto abortit;
863 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
864 goto abortit;
867 * Note: now that fvp is locked we have to be sure to unlock it before
868 * using the 'abortit' target.
870 dp = VTOI(fdvp);
871 ip = VTOI(fvp);
872 if (ip->i_nlink >= LINK_MAX) {
873 vn_unlock(fvp);
874 error = EMLINK;
875 goto abortit;
877 #if 0 /* handled by kernel now */
878 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
879 || (dp->i_flags & APPEND)) {
880 vn_unlock(fvp);
881 error = EPERM;
882 goto abortit;
884 #endif
885 if ((ip->i_mode & IFMT) == IFDIR) {
887 * Avoid ".", "..", and aliases of "." for obvious reasons.
889 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
890 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & CNP_ISDOTDOT ||
891 (ip->i_flag & IN_RENAME)) {
892 vn_unlock(fvp);
893 error = EINVAL;
894 goto abortit;
896 ip->i_flag |= IN_RENAME;
897 oldparent = dp->i_number;
898 doingdirectory = 1;
900 VN_KNOTE(fdvp, NOTE_WRITE); /* XXX right place? */
903 * fvp still locked. ip->i_flag has IN_RENAME set if doingdirectory.
904 * Cleanup fvp requirements so we can unlock it.
906 * tvp and tdvp are locked. tvp may be NULL. Now that dp and xp
907 * is setup we can use the 'bad' target if we unlock fvp. We cannot
908 * use the abortit target anymore because of IN_RENAME.
910 dp = VTOI(tdvp);
911 if (tvp)
912 xp = VTOI(tvp);
913 else
914 xp = NULL;
917 * 1) Bump link count while we're moving stuff
918 * around. If we crash somewhere before
919 * completing our work, the link count
920 * may be wrong, but correctable.
922 ip->i_effnlink++;
923 ip->i_nlink++;
924 ip->i_flag |= IN_CHANGE;
925 if (DOINGSOFTDEP(fvp))
926 softdep_change_linkcnt(ip);
927 if ((error = ffs_update(fvp, !(DOINGSOFTDEP(fvp) |
928 DOINGASYNC(fvp)))) != 0) {
929 vn_unlock(fvp);
930 goto bad;
934 * If ".." must be changed (ie the directory gets a new
935 * parent) then the source directory must not be in the
936 * directory heirarchy above the target, as this would
937 * orphan everything below the source directory. Also
938 * the user must have write permission in the source so
939 * as to be able to change "..". We must repeat the call
940 * to namei, as the parent directory is unlocked by the
941 * call to checkpath().
943 error = VOP_EACCESS(fvp, VWRITE, tcnp->cn_cred);
944 vn_unlock(fvp);
947 * We are now back to where we were in that fvp, fdvp are unlocked
948 * and tvp, tdvp are locked. tvp may be NULL. IN_RENAME may be
949 * set. Only the bad target or, if we clean up tvp and tdvp, the
950 * out target, may be used.
952 if (oldparent != dp->i_number)
953 newparent = dp->i_number;
954 if (doingdirectory && newparent) {
955 if (error) /* write access check above */
956 goto bad;
959 * Once we start messing with tvp and tdvp we cannot use the
960 * 'bad' target, only finish cleaning tdvp and tvp up and
961 * use the 'out' target.
963 * This cleans up tvp.
965 if (xp != NULL) {
966 vput(tvp);
967 xp = NULL;
971 * This is a real mess. ufs_checkpath vput's the target
972 * directory so retain an extra ref and note that tdvp will
973 * lose its lock on return. This leaves us with one good
974 * ref after ufs_checkpath returns.
976 vref(tdvp);
977 error = ufs_checkpath(ip, dp, tcnp->cn_cred);
978 tcnp->cn_flags |= CNP_PDIRUNLOCK;
979 if (error) {
980 vrele(tdvp);
981 goto out;
985 * relookup no longer messes with tdvp's refs. tdvp must be
986 * unlocked on entry and will be locked on a successful
987 * return.
989 error = relookup(tdvp, &tvp, tcnp);
990 if (error) {
991 if (tcnp->cn_flags & CNP_PDIRUNLOCK)
992 vrele(tdvp);
993 else
994 vput(tdvp);
995 goto out;
997 KKASSERT((tcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
998 dp = VTOI(tdvp);
999 if (tvp)
1000 xp = VTOI(tvp);
1004 * We are back to fvp, fdvp unlocked, tvp, tdvp locked. tvp may
1005 * be NULL (xp will also be NULL in that case), and IN_RENAME will
1006 * be set if doingdirectory. This means we can use the 'bad' target
1007 * again.
1011 * 2) If target doesn't exist, link the target
1012 * to the source and unlink the source.
1013 * Otherwise, rewrite the target directory
1014 * entry to reference the source inode and
1015 * expunge the original entry's existence.
1017 if (xp == NULL) {
1018 if (dp->i_dev != ip->i_dev)
1019 panic("ufs_rename: EXDEV");
1021 * Account for ".." in new directory.
1022 * When source and destination have the same
1023 * parent we don't fool with the link count.
1025 if (doingdirectory && newparent) {
1026 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1027 error = EMLINK;
1028 goto bad;
1030 dp->i_effnlink++;
1031 dp->i_nlink++;
1032 dp->i_flag |= IN_CHANGE;
1033 if (DOINGSOFTDEP(tdvp))
1034 softdep_change_linkcnt(dp);
1035 error = ffs_update(tdvp, !(DOINGSOFTDEP(tdvp) |
1036 DOINGASYNC(tdvp)));
1037 if (error)
1038 goto bad;
1040 ufs_makedirentry(ip, tcnp, &newdir);
1041 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1042 if (error) {
1043 if (doingdirectory && newparent) {
1044 dp->i_effnlink--;
1045 dp->i_nlink--;
1046 dp->i_flag |= IN_CHANGE;
1047 if (DOINGSOFTDEP(tdvp))
1048 softdep_change_linkcnt(dp);
1049 (void)ffs_update(tdvp, 1);
1051 goto bad;
1053 VN_KNOTE(tdvp, NOTE_WRITE);
1054 vput(tdvp);
1055 } else {
1056 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1057 panic("ufs_rename: EXDEV");
1059 * Short circuit rename(foo, foo).
1061 if (xp->i_number == ip->i_number)
1062 panic("ufs_rename: same file");
1064 * If the parent directory is "sticky", then the user must
1065 * own the parent directory, or the destination of the rename,
1066 * otherwise the destination may not be changed (except by
1067 * root). This implements append-only directories.
1069 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
1070 tcnp->cn_cred->cr_uid != dp->i_uid &&
1071 xp->i_uid != tcnp->cn_cred->cr_uid) {
1072 error = EPERM;
1073 goto bad;
1076 * Target must be empty if a directory and have no links
1077 * to it. Also, ensure source and target are compatible
1078 * (both directories, or both not directories).
1080 * Purge the file or directory being replaced from the
1081 * nameccache.
1083 if ((xp->i_mode&IFMT) == IFDIR) {
1084 if ((xp->i_effnlink > 2) ||
1085 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1086 error = ENOTEMPTY;
1087 goto bad;
1089 if (!doingdirectory) {
1090 error = ENOTDIR;
1091 goto bad;
1093 /* cache_purge removed - handled by VFS compat layer */
1094 } else if (doingdirectory == 0) {
1095 /* cache_purge removed - handled by VFS compat layer */
1096 } else {
1097 error = EISDIR;
1098 goto bad;
1101 * note: inode passed to ufs_dirrewrite() is 0 for a
1102 * non-directory file rename, 1 for a directory rename
1103 * in the same directory, and > 1 for an inode representing
1104 * the new directory.
1106 error = ufs_dirrewrite(dp, xp, ip->i_number,
1107 IFTODT(ip->i_mode),
1108 (doingdirectory && newparent) ?
1109 newparent : (ino_t)doingdirectory);
1110 if (error)
1111 goto bad;
1112 if (doingdirectory) {
1113 if (!newparent) {
1114 dp->i_effnlink--;
1115 if (DOINGSOFTDEP(tdvp))
1116 softdep_change_linkcnt(dp);
1118 xp->i_effnlink--;
1119 if (DOINGSOFTDEP(tvp))
1120 softdep_change_linkcnt(xp);
1122 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1124 * Truncate inode. The only stuff left in the directory
1125 * is "." and "..". The "." reference is inconsequential
1126 * since we are quashing it. We have removed the "."
1127 * reference and the reference in the parent directory,
1128 * but there may be other hard links. The soft
1129 * dependency code will arrange to do these operations
1130 * after the parent directory entry has been deleted on
1131 * disk, so when running with that code we avoid doing
1132 * them now.
1134 if (!newparent) {
1135 dp->i_nlink--;
1136 dp->i_flag |= IN_CHANGE;
1138 xp->i_nlink--;
1139 xp->i_flag |= IN_CHANGE;
1140 ioflag = DOINGASYNC(tvp) ? 0 : IO_SYNC;
1141 error = ffs_truncate(tvp, (off_t)0, ioflag,
1142 tcnp->cn_cred);
1143 if (error)
1144 goto bad;
1146 VN_KNOTE(tdvp, NOTE_WRITE);
1147 vput(tdvp);
1148 VN_KNOTE(tvp, NOTE_DELETE);
1149 vput(tvp);
1150 xp = NULL;
1154 * tvp and tdvp have been cleaned up. only fvp and fdvp (both
1155 * unlocked) remain. We are about to overwrite fvp but we have to
1156 * keep 'ip' intact so we cannot release the old fvp, which is still
1157 * refd and accessible via ap->a_fvp.
1159 * This means we cannot use either 'bad' or 'out' to cleanup any
1160 * more.
1164 * 3) Unlink the source.
1166 fcnp->cn_flags &= ~CNP_MODMASK;
1167 fcnp->cn_flags |= CNP_LOCKPARENT;
1168 error = relookup(fdvp, &fvp, fcnp);
1169 if (error || fvp == NULL) {
1171 * From name has disappeared. IN_RENAME will not be set if
1172 * we get past the panic so we don't have to clean it up.
1174 if (doingdirectory)
1175 panic("ufs_rename: lost dir entry");
1176 vrele(ap->a_fvp);
1177 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
1178 vrele(fdvp);
1179 else
1180 vput(fdvp);
1181 return(0);
1183 KKASSERT((fcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
1186 * fdvp and fvp are locked.
1188 xp = VTOI(fvp);
1189 dp = VTOI(fdvp);
1192 * Ensure that the directory entry still exists and has not
1193 * changed while the new name has been entered. If the source is
1194 * a file then the entry may have been unlinked or renamed. In
1195 * either case there is no further work to be done. If the source
1196 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1197 * flag ensures that it cannot be moved by another rename or removed
1198 * by a rmdir. Cleanup IN_RENAME.
1200 if (xp != ip) {
1201 if (doingdirectory)
1202 panic("ufs_rename: lost dir entry");
1203 } else {
1205 * If the source is a directory with a
1206 * new parent, the link count of the old
1207 * parent directory must be decremented
1208 * and ".." set to point to the new parent.
1210 if (doingdirectory && newparent) {
1211 xp->i_offset = mastertemplate.dot_reclen;
1212 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1213 /* cache_purge removed - handled by VFS compat layer */
1215 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1216 xp->i_flag &= ~IN_RENAME;
1219 VN_KNOTE(fvp, NOTE_RENAME);
1220 vput(fdvp);
1221 vput(fvp);
1222 vrele(ap->a_fvp);
1223 return (error);
1225 bad:
1226 if (xp)
1227 vput(ITOV(xp));
1228 vput(ITOV(dp));
1229 out:
1230 if (doingdirectory)
1231 ip->i_flag &= ~IN_RENAME;
1232 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1233 ip->i_effnlink--;
1234 ip->i_nlink--;
1235 ip->i_flag |= IN_CHANGE;
1236 ip->i_flag &= ~IN_RENAME;
1237 if (DOINGSOFTDEP(fvp))
1238 softdep_change_linkcnt(ip);
1239 vput(fvp);
1240 } else {
1241 vrele(fvp);
1243 return (error);
1247 * Mkdir system call
1249 * ufs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1250 * struct componentname *a_cnp, struct vattr *a_vap)
1252 static
1254 ufs_mkdir(struct vop_old_mkdir_args *ap)
1256 struct vnode *dvp = ap->a_dvp;
1257 struct vattr *vap = ap->a_vap;
1258 struct componentname *cnp = ap->a_cnp;
1259 struct inode *ip, *dp;
1260 struct vnode *tvp;
1261 struct buf *bp;
1262 struct dirtemplate dirtemplate, *dtp;
1263 struct direct newdir;
1264 int error, dmode;
1265 long blkoff;
1267 dp = VTOI(dvp);
1268 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1269 error = EMLINK;
1270 goto out;
1272 dmode = vap->va_mode & 0777;
1273 dmode |= IFDIR;
1275 * Must simulate part of ufs_makeinode here to acquire the inode,
1276 * but not have it entered in the parent directory. The entry is
1277 * made later after writing "." and ".." entries.
1279 error = ffs_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1280 if (error)
1281 goto out;
1282 ip = VTOI(tvp);
1283 ip->i_gid = dp->i_gid;
1284 #ifdef SUIDDIR
1286 #ifdef QUOTA
1287 struct ucred ucred, *ucp;
1288 ucp = cnp->cn_cred;
1289 #endif
1291 * If we are hacking owners here, (only do this where told to)
1292 * and we are not giving it TO root, (would subvert quotas)
1293 * then go ahead and give it to the other user.
1294 * The new directory also inherits the SUID bit.
1295 * If user's UID and dir UID are the same,
1296 * 'give it away' so that the SUID is still forced on.
1298 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1299 (dp->i_mode & ISUID) && dp->i_uid) {
1300 dmode |= ISUID;
1301 ip->i_uid = dp->i_uid;
1302 #ifdef QUOTA
1303 if (dp->i_uid != cnp->cn_cred->cr_uid) {
1305 * Make sure the correct user gets charged
1306 * for the space.
1307 * Make a dummy credential for the victim.
1308 * XXX This seems to never be accessed out of
1309 * our context so a stack variable is ok.
1311 ucred.cr_ref = 1;
1312 ucred.cr_uid = ip->i_uid;
1313 ucred.cr_ngroups = 1;
1314 ucred.cr_groups[0] = dp->i_gid;
1315 ucp = &ucred;
1317 #endif
1318 } else
1319 ip->i_uid = cnp->cn_cred->cr_uid;
1320 #ifdef QUOTA
1321 if ((error = ufs_getinoquota(ip)) ||
1322 (error = ufs_chkiq(ip, 1, ucp, 0))) {
1323 ffs_vfree(tvp, ip->i_number, dmode);
1324 vput(tvp);
1325 return (error);
1327 #endif
1329 #else /* !SUIDDIR */
1330 ip->i_uid = cnp->cn_cred->cr_uid;
1331 #ifdef QUOTA
1332 if ((error = ufs_getinoquota(ip)) ||
1333 (error = ufs_chkiq(ip, 1, cnp->cn_cred, 0))) {
1334 ffs_vfree(tvp, ip->i_number, dmode);
1335 vput(tvp);
1336 return (error);
1338 #endif
1339 #endif /* !SUIDDIR */
1340 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1341 ip->i_mode = dmode;
1342 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1343 ip->i_effnlink = 2;
1344 ip->i_nlink = 2;
1345 if (DOINGSOFTDEP(tvp))
1346 softdep_change_linkcnt(ip);
1347 if (cnp->cn_flags & CNP_ISWHITEOUT)
1348 ip->i_flags |= UF_OPAQUE;
1351 * Bump link count in parent directory to reflect work done below.
1352 * Should be done before reference is created so cleanup is
1353 * possible if we crash.
1355 dp->i_effnlink++;
1356 dp->i_nlink++;
1357 dp->i_flag |= IN_CHANGE;
1358 if (DOINGSOFTDEP(dvp))
1359 softdep_change_linkcnt(dp);
1360 error = ffs_update(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1361 if (error)
1362 goto bad;
1365 * The vnode must have a VM object in order to issue buffer cache
1366 * ops on it.
1368 vinitvmio(tvp, DIRBLKSIZ);
1371 * Initialize directory with "." and ".." from static template.
1373 if (dvp->v_mount->mnt_maxsymlinklen > 0)
1374 dtp = &mastertemplate;
1375 else
1376 dtp = (struct dirtemplate *)&omastertemplate;
1377 dirtemplate = *dtp;
1378 dirtemplate.dot_ino = ip->i_number;
1379 dirtemplate.dotdot_ino = dp->i_number;
1380 nvnode_pager_setsize(tvp, DIRBLKSIZ, DIRBLKSIZ, -1);
1381 error = VOP_BALLOC(tvp, 0LL, DIRBLKSIZ, cnp->cn_cred, B_CLRBUF, &bp);
1382 if (error)
1383 goto bad;
1384 ip->i_size = DIRBLKSIZ;
1385 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1386 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1387 if (DOINGSOFTDEP(tvp)) {
1389 * Ensure that the entire newly allocated block is a
1390 * valid directory so that future growth within the
1391 * block does not have to ensure that the block is
1392 * written before the inode.
1394 blkoff = DIRBLKSIZ;
1395 while (blkoff < bp->b_bcount) {
1396 ((struct direct *)
1397 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1398 blkoff += DIRBLKSIZ;
1401 if ((error = ffs_update(tvp, !(DOINGSOFTDEP(tvp) |
1402 DOINGASYNC(tvp)))) != 0) {
1403 bwrite(bp);
1404 goto bad;
1407 * Directory set up, now install its entry in the parent directory.
1409 * If we are not doing soft dependencies, then we must write out the
1410 * buffer containing the new directory body before entering the new
1411 * name in the parent. If we are doing soft dependencies, then the
1412 * buffer containing the new directory body will be passed to and
1413 * released in the soft dependency code after the code has attached
1414 * an appropriate ordering dependency to the buffer which ensures that
1415 * the buffer is written before the new name is written in the parent.
1417 if (DOINGASYNC(dvp))
1418 bdwrite(bp);
1419 else if (!DOINGSOFTDEP(dvp) && (error = bwrite(bp)) != 0)
1420 goto bad;
1421 ufs_makedirentry(ip, cnp, &newdir);
1422 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1424 bad:
1425 if (error == 0) {
1426 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1427 *ap->a_vpp = tvp;
1428 } else {
1429 dp->i_effnlink--;
1430 dp->i_nlink--;
1431 dp->i_flag |= IN_CHANGE;
1432 if (DOINGSOFTDEP(dvp))
1433 softdep_change_linkcnt(dp);
1435 * No need to do an explicit VOP_TRUNCATE here, vrele will
1436 * do this for us because we set the link count to 0.
1438 ip->i_effnlink = 0;
1439 ip->i_nlink = 0;
1440 ip->i_flag |= IN_CHANGE;
1441 if (DOINGSOFTDEP(tvp))
1442 softdep_change_linkcnt(ip);
1443 vput(tvp);
1445 out:
1446 return (error);
1450 * Rmdir system call.
1452 * ufs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1453 * struct componentname *a_cnp)
1455 static
1457 ufs_rmdir(struct vop_old_rmdir_args *ap)
1459 struct vnode *vp = ap->a_vp;
1460 struct vnode *dvp = ap->a_dvp;
1461 struct componentname *cnp = ap->a_cnp;
1462 struct inode *ip, *dp;
1463 int error, ioflag;
1465 ip = VTOI(vp);
1466 dp = VTOI(dvp);
1469 * Do not remove a directory that is in the process of being renamed.
1470 * Verify the directory is empty (and valid). Rmdir ".." will not be
1471 * valid since ".." will contain a reference to the current directory
1472 * and thus be non-empty. Do not allow the removal of mounted on
1473 * directories (this can happen when an NFS exported filesystem
1474 * tries to remove a locally mounted on directory).
1476 error = 0;
1477 if (ip->i_flag & IN_RENAME) {
1478 error = EINVAL;
1479 goto out;
1481 if (ip->i_effnlink != 2 ||
1482 !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1483 error = ENOTEMPTY;
1484 goto out;
1486 #if 0 /* handled by kernel now */
1487 if ((dp->i_flags & APPEND)
1488 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1489 error = EPERM;
1490 goto out;
1492 #endif
1494 * Delete reference to directory before purging
1495 * inode. If we crash in between, the directory
1496 * will be reattached to lost+found,
1498 dp->i_effnlink--;
1499 ip->i_effnlink--;
1500 if (DOINGSOFTDEP(vp)) {
1501 softdep_change_linkcnt(dp);
1502 softdep_change_linkcnt(ip);
1504 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1505 if (error) {
1506 dp->i_effnlink++;
1507 ip->i_effnlink++;
1508 if (DOINGSOFTDEP(vp)) {
1509 softdep_change_linkcnt(dp);
1510 softdep_change_linkcnt(ip);
1512 goto out;
1514 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1516 * Truncate inode. The only stuff left in the directory is "." and
1517 * "..". The "." reference is inconsequential since we are quashing
1518 * it. The soft dependency code will arrange to do these operations
1519 * after the parent directory entry has been deleted on disk, so
1520 * when running with that code we avoid doing them now.
1522 if (!DOINGSOFTDEP(vp)) {
1523 dp->i_nlink--;
1524 dp->i_flag |= IN_CHANGE;
1525 ip->i_nlink--;
1526 ip->i_flag |= IN_CHANGE;
1527 ioflag = DOINGASYNC(vp) ? 0 : IO_SYNC;
1528 error = ffs_truncate(vp, (off_t)0, ioflag, cnp->cn_cred);
1530 /* cache_purge removed - handled by VFS compat layer */
1531 #ifdef UFS_DIRHASH
1532 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1533 if (ip->i_dirhash != NULL)
1534 ufsdirhash_free(ip);
1535 #endif
1536 out:
1537 VN_KNOTE(vp, NOTE_DELETE);
1538 return (error);
1542 * symlink -- make a symbolic link
1544 * ufs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1545 * struct componentname *a_cnp, struct vattr *a_vap,
1546 * char *a_target)
1548 static
1550 ufs_symlink(struct vop_old_symlink_args *ap)
1552 struct vnode *vp, **vpp = ap->a_vpp;
1553 struct inode *ip;
1554 int len, error;
1556 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1557 vpp, ap->a_cnp);
1558 if (error)
1559 return (error);
1560 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1561 vp = *vpp;
1562 len = strlen(ap->a_target);
1563 if (len < vp->v_mount->mnt_maxsymlinklen) {
1564 ip = VTOI(vp);
1565 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1566 ip->i_size = len;
1567 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1568 } else {
1570 * Make sure we have a VM object in order to use
1571 * the buffer cache.
1573 if (vp->v_object == NULL)
1574 vinitvmio(vp, 0);
1575 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1576 UIO_SYSSPACE, IO_NODELOCKED,
1577 ap->a_cnp->cn_cred, NULL);
1579 if (error)
1580 vput(vp);
1581 return (error);
1585 * Vnode op for reading directories.
1587 * ufs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
1588 * int *a_eofflag, int *ncookies, off_t **a_cookies)
1590 static
1592 ufs_readdir(struct vop_readdir_args *ap)
1594 struct uio *uio = ap->a_uio;
1595 struct vnode *vp = ap->a_vp;
1596 struct direct *dp;
1597 struct buf *bp;
1598 int retval;
1599 int error;
1600 int offset; /* offset into buffer cache buffer */
1601 int eoffset; /* end of buffer clipped to file EOF */
1602 int pickup; /* pickup point */
1603 int ncookies;
1604 int cookie_index;
1605 off_t *cookies;
1607 if (uio->uio_offset < 0)
1608 return (EINVAL);
1610 * Guess the number of cookies needed. Make sure we compute at
1611 * least 1, and no more then a reasonable limit.
1613 if (ap->a_ncookies) {
1614 ncookies = uio->uio_resid / 16 + 1;
1615 if (ncookies > 1024)
1616 ncookies = 1024;
1617 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1618 } else {
1619 ncookies = -1; /* force conditionals below */
1620 cookies = NULL;
1622 cookie_index = 0;
1624 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
1625 return (error);
1628 * Past or at EOF
1630 if (uio->uio_offset >= VTOI(vp)->i_size) {
1631 if (ap->a_eofflag)
1632 *ap->a_eofflag = 1;
1633 if (ap->a_ncookies) {
1634 *ap->a_ncookies = cookie_index;
1635 *ap->a_cookies = cookies;
1637 goto done;
1641 * Loop until we run out of cookies, we run out of user buffer,
1642 * or we hit the directory EOF.
1644 * Always start scans at the beginning of the buffer, don't trust
1645 * the offset supplied by userland.
1647 while ((error = ffs_blkatoff_ra(vp, uio->uio_offset, NULL, &bp, 2)) == 0) {
1648 pickup = (int)(uio->uio_offset - bp->b_loffset);
1649 offset = 0;
1650 retval = 0;
1651 if (bp->b_loffset + bp->b_bcount > VTOI(vp)->i_size)
1652 eoffset = (int)(VTOI(vp)->i_size - bp->b_loffset);
1653 else
1654 eoffset = bp->b_bcount;
1656 while (offset < eoffset) {
1657 dp = (struct direct *)(bp->b_data + offset);
1658 if (dp->d_reclen <= 0 || (dp->d_reclen & 3) ||
1659 offset + dp->d_reclen > bp->b_bcount) {
1660 error = EIO;
1661 break;
1663 if (offsetof(struct direct, d_name[dp->d_namlen]) > dp->d_reclen) {
1664 error = EIO;
1665 break;
1667 if (offset < pickup) {
1668 offset += dp->d_reclen;
1669 continue;
1671 #if BYTE_ORDER == LITTLE_ENDIAN
1672 if (OFSFMT(vp)) {
1673 retval = vop_write_dirent(&error, uio,
1674 dp->d_ino, dp->d_namlen, dp->d_type,
1675 dp->d_name);
1676 } else
1677 #endif
1679 retval = vop_write_dirent(&error, uio,
1680 dp->d_ino, dp->d_type, dp->d_namlen,
1681 dp->d_name);
1683 if (retval)
1684 break;
1685 if (cookies)
1686 cookies[cookie_index] = bp->b_loffset + offset;
1687 ++cookie_index;
1688 offset += dp->d_reclen;
1689 if (cookie_index == ncookies)
1690 break;
1694 * This will align the next loop to the beginning of the
1695 * next block, and pickup will calculate to 0.
1697 uio->uio_offset = bp->b_loffset + offset;
1698 brelse(bp);
1700 if (retval || error || cookie_index == ncookies ||
1701 uio->uio_offset >= VTOI(vp)->i_size) {
1702 break;
1705 if (ap->a_eofflag)
1706 *ap->a_eofflag = VTOI(vp)->i_size <= uio->uio_offset;
1709 * Report errors only if we didn't manage to read anything
1711 if (error && cookie_index == 0) {
1712 if (cookies) {
1713 kfree(cookies, M_TEMP);
1714 *ap->a_ncookies = 0;
1715 *ap->a_cookies = NULL;
1717 } else {
1718 error = 0;
1719 if (cookies) {
1720 *ap->a_ncookies = cookie_index;
1721 *ap->a_cookies = cookies;
1724 done:
1725 vn_unlock(vp);
1726 return (error);
1730 * Return target name of a symbolic link
1732 * ufs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1734 static
1736 ufs_readlink(struct vop_readlink_args *ap)
1738 struct vnode *vp = ap->a_vp;
1739 struct inode *ip = VTOI(vp);
1740 int isize;
1742 isize = ip->i_size;
1743 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1744 (ip->i_din.di_blocks == 0)) { /* XXX - for old fastlink support */
1745 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1746 return (0);
1750 * Perform the equivalent of an OPEN on vp so we can issue a
1751 * VOP_READ.
1753 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1757 * Calculate the logical to physical mapping if not done already,
1758 * then call the device strategy routine.
1760 * In order to be able to swap to a file, the VOP_BMAP operation may not
1761 * deadlock on memory. See ufs_bmap() for details.
1763 * ufs_strategy(struct vnode *a_vp, struct bio *a_bio)
1765 static
1767 ufs_strategy(struct vop_strategy_args *ap)
1769 struct bio *bio = ap->a_bio;
1770 struct bio *nbio;
1771 struct buf *bp = bio->bio_buf;
1772 struct vnode *vp = ap->a_vp;
1773 struct inode *ip;
1774 int error;
1776 ip = VTOI(vp);
1777 if (vp->v_type == VBLK || vp->v_type == VCHR)
1778 panic("ufs_strategy: spec");
1779 nbio = push_bio(bio);
1780 if (nbio->bio_offset == NOOFFSET) {
1781 error = VOP_BMAP(vp, bio->bio_offset, &nbio->bio_offset,
1782 NULL, NULL, bp->b_cmd);
1783 if (error) {
1784 bp->b_error = error;
1785 bp->b_flags |= B_ERROR;
1786 /* I/O was never started on nbio, must biodone(bio) */
1787 biodone(bio);
1788 return (error);
1790 if (nbio->bio_offset == NOOFFSET)
1791 vfs_bio_clrbuf(bp);
1793 if (nbio->bio_offset == NOOFFSET) {
1795 * We hit a hole in the file. The buffer has been zero-filled
1796 * so just biodone() it.
1798 biodone(bio);
1799 } else {
1800 vn_strategy(ip->i_devvp, nbio);
1802 return (0);
1806 * Print out the contents of an inode.
1808 * ufs_print(struct vnode *a_vp)
1810 static
1812 ufs_print(struct vop_print_args *ap)
1814 struct vnode *vp = ap->a_vp;
1815 struct inode *ip = VTOI(vp);
1817 kprintf("tag VT_UFS, ino %lu, on dev %s (%d, %d)",
1818 (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev),
1819 minor(ip->i_dev));
1820 if (vp->v_type == VFIFO)
1821 fifo_printinfo(vp);
1822 lockmgr_printinfo(&vp->v_lock);
1823 kprintf("\n");
1824 return (0);
1828 * Read wrapper for fifos.
1830 * ufsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1831 * struct ucred *a_cred)
1833 static
1835 ufsfifo_read(struct vop_read_args *ap)
1837 int error, resid;
1838 struct inode *ip;
1839 struct uio *uio;
1841 uio = ap->a_uio;
1842 resid = uio->uio_resid;
1843 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1844 ip = VTOI(ap->a_vp);
1845 if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL &&
1846 (uio->uio_resid != resid || (error == 0 && resid != 0)))
1847 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1848 return (error);
1852 * Write wrapper for fifos.
1854 * ufsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1855 * struct ucred *a_cred)
1857 static
1859 ufsfifo_write(struct vop_write_args *ap)
1861 int error, resid;
1862 struct inode *ip;
1863 struct uio *uio;
1865 uio = ap->a_uio;
1866 resid = uio->uio_resid;
1867 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1868 ip = VTOI(ap->a_vp);
1869 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1870 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1871 return (error);
1875 * Close wrapper for fifos.
1877 * Update the times on the inode then do device close.
1879 * ufsfifo_close(struct vnode *a_vp, int a_fflag)
1881 static
1883 ufsfifo_close(struct vop_close_args *ap)
1885 struct vnode *vp = ap->a_vp;
1887 if (vp->v_sysref.refcnt > 1)
1888 ufs_itimes(vp);
1889 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
1893 * Kqfilter wrapper for fifos.
1895 * Fall through to ufs kqfilter routines if needed
1897 static
1899 ufsfifo_kqfilter(struct vop_kqfilter_args *ap)
1901 int error;
1903 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1904 if (error)
1905 error = ufs_kqfilter(ap);
1906 return (error);
1910 * Advisory record locking support
1912 * ufs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
1913 * int a_flags)
1915 static
1917 ufs_advlock(struct vop_advlock_args *ap)
1919 struct inode *ip = VTOI(ap->a_vp);
1921 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
1925 * Initialize the vnode associated with a new inode, handle aliased
1926 * vnodes.
1928 * Make sure directories have their VM object now rather then later,
1929 * saving us from having to check on all the myrid directory VOPs
1930 * that might be executed without a VOP_OPEN being performed.
1933 ufs_vinit(struct mount *mntp, struct vnode **vpp)
1935 struct inode *ip;
1936 struct vnode *vp;
1937 struct timeval tv;
1939 vp = *vpp;
1940 ip = VTOI(vp);
1942 vp->v_type = IFTOVT(ip->i_mode);
1944 switch(vp->v_type) {
1945 case VCHR:
1946 case VBLK:
1947 vp->v_ops = &mntp->mnt_vn_spec_ops;
1948 addaliasu(vp, umajor(ip->i_rdev), uminor(ip->i_rdev));
1949 break;
1950 case VFIFO:
1951 vp->v_ops = &mntp->mnt_vn_fifo_ops;
1952 break;
1953 case VDIR:
1954 case VREG:
1955 vinitvmio(vp, ip->i_size);
1956 break;
1957 case VLNK:
1958 if (ip->i_size >= vp->v_mount->mnt_maxsymlinklen)
1959 vinitvmio(vp, ip->i_size);
1960 break;
1961 default:
1962 break;
1966 if (ip->i_number == ROOTINO)
1967 vsetflags(vp, VROOT);
1969 * Initialize modrev times
1971 getmicrouptime(&tv);
1972 SETHIGH(ip->i_modrev, tv.tv_sec);
1973 SETLOW(ip->i_modrev, tv.tv_usec * 4294);
1974 *vpp = vp;
1975 return (0);
1979 * Allocate a new inode.
1981 static
1983 ufs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
1984 struct componentname *cnp)
1986 struct inode *ip, *pdir;
1987 struct direct newdir;
1988 struct vnode *tvp;
1989 int error;
1991 pdir = VTOI(dvp);
1992 *vpp = NULL;
1993 if ((mode & IFMT) == 0)
1994 mode |= IFREG;
1996 error = ffs_valloc(dvp, mode, cnp->cn_cred, &tvp);
1997 if (error)
1998 return (error);
1999 ip = VTOI(tvp);
2000 ip->i_flags = pdir->i_flags & (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP);
2001 ip->i_gid = pdir->i_gid;
2002 #ifdef SUIDDIR
2004 #ifdef QUOTA
2005 struct ucred ucred, *ucp;
2006 ucp = cnp->cn_cred;
2007 #endif
2009 * If we are not the owner of the directory,
2010 * and we are hacking owners here, (only do this where told to)
2011 * and we are not giving it TO root, (would subvert quotas)
2012 * then go ahead and give it to the other user.
2013 * Note that this drops off the execute bits for security.
2015 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2016 (pdir->i_mode & ISUID) &&
2017 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2018 ip->i_uid = pdir->i_uid;
2019 mode &= ~07111;
2020 #ifdef QUOTA
2022 * Make sure the correct user gets charged
2023 * for the space.
2024 * Quickly knock up a dummy credential for the victim.
2025 * XXX This seems to never be accessed out of our
2026 * context so a stack variable is ok.
2028 ucred.cr_ref = 1;
2029 ucred.cr_uid = ip->i_uid;
2030 ucred.cr_ngroups = 1;
2031 ucred.cr_groups[0] = pdir->i_gid;
2032 ucp = &ucred;
2033 #endif
2034 } else
2035 ip->i_uid = cnp->cn_cred->cr_uid;
2037 #ifdef QUOTA
2038 if ((error = ufs_getinoquota(ip)) ||
2039 (error = ufs_chkiq(ip, 1, ucp, 0))) {
2040 ffs_vfree(tvp, ip->i_number, mode);
2041 vput(tvp);
2042 return (error);
2044 #endif
2046 #else /* !SUIDDIR */
2047 ip->i_uid = cnp->cn_cred->cr_uid;
2048 #ifdef QUOTA
2049 if ((error = ufs_getinoquota(ip)) ||
2050 (error = ufs_chkiq(ip, 1, cnp->cn_cred, 0))) {
2051 ffs_vfree(tvp, ip->i_number, mode);
2052 vput(tvp);
2053 return (error);
2055 #endif
2056 #endif /* !SUIDDIR */
2057 ip->i_din.di_spare[0] = 0;
2058 ip->i_din.di_spare[1] = 0;
2059 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2060 ip->i_mode = mode;
2061 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2062 ip->i_effnlink = 1;
2063 ip->i_nlink = 1;
2064 if (DOINGSOFTDEP(tvp))
2065 softdep_change_linkcnt(ip);
2066 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2067 priv_check_cred(cnp->cn_cred, PRIV_VFS_SETGID, 0)) {
2068 ip->i_mode &= ~ISGID;
2071 if (cnp->cn_flags & CNP_ISWHITEOUT)
2072 ip->i_flags |= UF_OPAQUE;
2075 * Regular files and directories need VM objects. Softlinks do
2076 * not (not immediately anyway).
2078 if (tvp->v_type == VREG || tvp->v_type == VDIR)
2079 vinitvmio(tvp, 0);
2082 * Make sure inode goes to disk before directory entry.
2084 error = ffs_update(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2085 if (error)
2086 goto bad;
2087 ufs_makedirentry(ip, cnp, &newdir);
2088 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2089 if (error)
2090 goto bad;
2091 *vpp = tvp;
2092 return (0);
2094 bad:
2096 * Write error occurred trying to update the inode
2097 * or the directory so must deallocate the inode.
2099 ip->i_effnlink = 0;
2100 ip->i_nlink = 0;
2101 ip->i_flag |= IN_CHANGE;
2102 if (DOINGSOFTDEP(tvp))
2103 softdep_change_linkcnt(ip);
2104 vput(tvp);
2105 return (error);
2108 static int
2109 ufs_missingop(struct vop_generic_args *ap)
2111 panic("no vop function for %s in ufs child", ap->a_desc->sd_name);
2112 return (EOPNOTSUPP);
2115 static struct filterops ufsread_filtops =
2116 { 1, NULL, filt_ufsdetach, filt_ufsread };
2117 static struct filterops ufswrite_filtops =
2118 { 1, NULL, filt_ufsdetach, filt_ufswrite };
2119 static struct filterops ufsvnode_filtops =
2120 { 1, NULL, filt_ufsdetach, filt_ufsvnode };
2123 * ufs_kqfilter(struct vnode *a_vp, struct knote *a_kn)
2125 static int
2126 ufs_kqfilter(struct vop_kqfilter_args *ap)
2128 struct vnode *vp = ap->a_vp;
2129 struct knote *kn = ap->a_kn;
2130 lwkt_tokref vlock;
2132 switch (kn->kn_filter) {
2133 case EVFILT_READ:
2134 kn->kn_fop = &ufsread_filtops;
2135 break;
2136 case EVFILT_WRITE:
2137 kn->kn_fop = &ufswrite_filtops;
2138 break;
2139 case EVFILT_VNODE:
2140 kn->kn_fop = &ufsvnode_filtops;
2141 break;
2142 default:
2143 return (1);
2146 kn->kn_hook = (caddr_t)vp;
2148 lwkt_gettoken(&vlock, &vp->v_token);
2149 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
2150 lwkt_reltoken(&vlock);
2152 return (0);
2155 static void
2156 filt_ufsdetach(struct knote *kn)
2158 struct vnode *vp = (struct vnode *)kn->kn_hook;
2159 lwkt_tokref vlock;
2161 lwkt_gettoken(&vlock, &vp->v_token);
2162 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
2163 kn, knote, kn_selnext);
2164 lwkt_reltoken(&vlock);
2167 /*ARGSUSED*/
2168 static int
2169 filt_ufsread(struct knote *kn, long hint)
2171 struct vnode *vp = (struct vnode *)kn->kn_hook;
2172 struct inode *ip = VTOI(vp);
2175 * filesystem is gone, so set the EOF flag and schedule
2176 * the knote for deletion.
2178 if (hint == NOTE_REVOKE) {
2179 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2180 return (1);
2183 kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
2184 return (kn->kn_data != 0);
2187 /*ARGSUSED*/
2188 static int
2189 filt_ufswrite(struct knote *kn, long hint)
2192 * filesystem is gone, so set the EOF flag and schedule
2193 * the knote for deletion.
2195 if (hint == NOTE_REVOKE)
2196 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2198 kn->kn_data = 0;
2199 return (1);
2202 static int
2203 filt_ufsvnode(struct knote *kn, long hint)
2205 if (kn->kn_sfflags & hint)
2206 kn->kn_fflags |= hint;
2207 if (hint == NOTE_REVOKE) {
2208 kn->kn_flags |= EV_EOF;
2209 return (1);
2211 return (kn->kn_fflags != 0);
2214 /* Global vfs data structures for ufs. */
2215 static struct vop_ops ufs_vnode_vops = {
2216 .vop_default = vop_defaultop,
2217 .vop_fsync = (void *)ufs_missingop,
2218 .vop_read = (void *)ufs_missingop,
2219 .vop_reallocblks = (void *)ufs_missingop,
2220 .vop_write = (void *)ufs_missingop,
2221 .vop_access = ufs_access,
2222 .vop_advlock = ufs_advlock,
2223 .vop_bmap = ufs_bmap,
2224 .vop_old_lookup = ufs_lookup,
2225 .vop_close = ufs_close,
2226 .vop_old_create = ufs_create,
2227 .vop_getattr = ufs_getattr,
2228 .vop_inactive = ufs_inactive,
2229 .vop_old_link = ufs_link,
2230 .vop_old_mkdir = ufs_mkdir,
2231 .vop_old_mknod = ufs_mknod,
2232 .vop_mmap = ufs_mmap,
2233 .vop_open = vop_stdopen,
2234 .vop_pathconf = vop_stdpathconf,
2235 .vop_poll = vop_stdpoll,
2236 .vop_kqfilter = ufs_kqfilter,
2237 .vop_print = ufs_print,
2238 .vop_readdir = ufs_readdir,
2239 .vop_readlink = ufs_readlink,
2240 .vop_reclaim = ufs_reclaim,
2241 .vop_old_remove = ufs_remove,
2242 .vop_old_rename = ufs_rename,
2243 .vop_old_rmdir = ufs_rmdir,
2244 .vop_setattr = ufs_setattr,
2245 .vop_markatime = ufs_markatime,
2246 .vop_strategy = ufs_strategy,
2247 .vop_old_symlink = ufs_symlink,
2248 .vop_old_whiteout = ufs_whiteout
2251 static struct vop_ops ufs_spec_vops = {
2252 .vop_default = vop_defaultop,
2253 .vop_fsync = (void *)ufs_missingop,
2254 .vop_access = ufs_access,
2255 .vop_close = ufs_close,
2256 .vop_getattr = ufs_getattr,
2257 .vop_inactive = ufs_inactive,
2258 .vop_print = ufs_print,
2259 .vop_read = vop_stdnoread,
2260 .vop_reclaim = ufs_reclaim,
2261 .vop_setattr = ufs_setattr,
2262 .vop_markatime = ufs_markatime,
2263 .vop_write = vop_stdnowrite
2266 static struct vop_ops ufs_fifo_vops = {
2267 .vop_default = fifo_vnoperate,
2268 .vop_fsync = (void *)ufs_missingop,
2269 .vop_access = ufs_access,
2270 .vop_close = ufsfifo_close,
2271 .vop_getattr = ufs_getattr,
2272 .vop_inactive = ufs_inactive,
2273 .vop_kqfilter = ufsfifo_kqfilter,
2274 .vop_print = ufs_print,
2275 .vop_read = ufsfifo_read,
2276 .vop_reclaim = ufs_reclaim,
2277 .vop_setattr = ufs_setattr,
2278 .vop_markatime = ufs_markatime,
2279 .vop_write = ufsfifo_write
2282 VNODEOP_SET(ufs_vnode_vops);
2283 VNODEOP_SET(ufs_spec_vops);
2284 VNODEOP_SET(ufs_fifo_vops);
2287 * ufs_vnoperate()
2290 ufs_vnoperate(struct vop_generic_args *ap)
2292 return (VOCALL(&ufs_vnode_vops, ap));
2296 * ufs_vnoperatefifo()
2299 ufs_vnoperatefifo(struct vop_generic_args *ap)
2301 return (VOCALL(&ufs_fifo_vops, ap));
2305 * ufs_vnoperatespec()
2308 ufs_vnoperatespec(struct vop_generic_args *ap)
2310 return (VOCALL(&ufs_spec_vops, ap));