---
name: "VOP_FSYNC"
section: "9"
platform: "linux"
package: "freebsd-manpages"
version: "6.0-5"
release: "Debian 4.0 (Etch)"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/VOP_FSYNC"
---

# VOP\_FSYNC(9)

flush file system buffers for a file

## NAME

**VOP\_FSYNC** – flush file system buffers for a file

## SYNOPSIS

**#include \<sys/param.h\>**

**#include \<sys/vnode.h\>**

*int*\

**VOP\_FSYNC**(*struct vnode \*vp*, *struct ucred \*cred*, *int waitfor*, *struct thread \*td*);

## DESCRIPTION

This call flushes any dirty file system buffers for the file. It is used to implement the [sync(2)](/sync/2.md) and [fsync(2)](/fsync/2.md) system calls.

Its arguments are:

- *vp*

  The vnode of the file.

- *cred*

  The caller's credentials.

- *waitfor*

  Whether the function should wait for I/O to complete. Possible values are:

  - MNT\_WAIT

    Synchronously wait for I/O to complete.

  - MNT\_NOWAIT

    Start all I/O, but do not wait for it.

  - MNT\_LAZY

    Push data not written by file system syncer.

- *td*

  The calling thread.

The argument *waitfor* is either MNT\_WAIT or MNT\_NOWAIT and specifies whether or not the function should wait for the writes to finish before returning.

## LOCKS

The file should be locked on entry.

## RETURN VALUES

Zero is returned if the call is successful, otherwise an appropriate error code is returned.

## PSEUDOCODE

```
int
vop_fsync(struct vnode *vp, struct ucred *cred, int waitfor, struct thread *td)
{
    struct buf *bp;
    struct buf *nbp;
    struct timeval tv;
    int s;
loop:
    s = splbio();
    for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
	nbp = bp->b_vnbufs.le_next;
	/*
	 * Ignore buffers which are already being written.
	 */
	if (bp->b_flags & B_BUSY)
	    continue;
	/*
	 * Make sure the buffer is dirty.
	 */
	if ((bp->b_flags & B_DELWRI) == 0)
	    panic("vop_fsync: not dirty");
	vfs_bio_awrite(bp);
	splx(s);
	goto loop;
    }
    splx(s);
    if (waitfor == MNT_WAIT) {
	s = splbio();
	while (vp->v_numoutput) {
	    vp->v_flag |= VBWAIT;
	    tsleep((caddr_t)&vp->v_numoutput, PRIBIO + 1, "vopfsn");
	}
	splx(s);
#ifdef DIAGNOSTIC
	if (vp->v_dirtyblkhd.lh_first) {
	    vprint("vop_fsync: dirty", vp);
	    goto loop;
	}
#endif
    }
    /*
     * Write out the on-disc version of the vnode.
     */
    tv = time;
    return VOP_UPDATE(vp, &tv, &tv, waitfor == MNT_WAIT);
}
```

## ERRORS

- \[ENOSPC\]

  The file system is full.

- \[EDQUOT\]

  Quota exceeded.

## SEE ALSO

[vnode(9)](/vnode/9.md)

## AUTHORS

This manual page was written by Doug Rabson.
