# syncfs _____________________________________________________
#
# SYSCALL_DEFINE1(syncfs, int, fd)

@define _SYSCALL_SYNCFS_NAME
%(
	name = "syncfs"
%)

@define _SYSCALL_SYNCFS_ARGSTR
%(
	argstr = sprintf("%d", fd)
%)

@define _SYSCALL_SYNCFS_REGARGS
%(
	fd = int_arg(1)
%)

probe syscall.syncfs = dw_syscall.syncfs !, nd_syscall.syncfs ? {}
probe syscall.syncfs.return = dw_syscall.syncfs.return !,
                              nd_syscall.syncfs.return ? {}

# dw_syncfs _____________________________________________________

probe dw_syscall.syncfs = kernel.function("sys_syncfs").call ?
{
	@_SYSCALL_SYNCFS_NAME
	fd = __int32($fd)
	@_SYSCALL_SYNCFS_ARGSTR
}
probe dw_syscall.syncfs.return = kernel.function("sys_syncfs").return ?
{
	@_SYSCALL_SYNCFS_NAME
	@SYSC_RETVALSTR($return)
}

# nd_syncfs _____________________________________________________

probe nd_syscall.syncfs = nd1_syscall.syncfs!, nd2_syscall.syncfs!, tp_syscall.syncfs
  { }

probe nd1_syscall.syncfs = kprobe.function("sys_syncfs") ?
{
	asmlinkage()
	@_SYSCALL_SYNCFS_NAME
	@_SYSCALL_SYNCFS_REGARGS
	@_SYSCALL_SYNCFS_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.syncfs = kprobe.function(@arch_syscall_prefix "sys_syncfs") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_SYNCFS_NAME
	@_SYSCALL_SYNCFS_REGARGS
	@_SYSCALL_SYNCFS_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.syncfs = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_syncfs"), @const("__NR_compat_syncfs"))
	@_SYSCALL_SYNCFS_NAME
	@_SYSCALL_SYNCFS_REGARGS
	@_SYSCALL_SYNCFS_ARGSTR
}

probe nd_syscall.syncfs.return = nd1_syscall.syncfs.return!, nd2_syscall.syncfs.return!, tp_syscall.syncfs.return
  { }

probe nd1_syscall.syncfs.return = kprobe.function("sys_syncfs").return ?
{
	@_SYSCALL_SYNCFS_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.syncfs.return = kprobe.function(@arch_syscall_prefix "sys_syncfs").return ?
{
	@_SYSCALL_SYNCFS_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.syncfs.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_syncfs"), @const("__NR_compat_syncfs"))
	@_SYSCALL_SYNCFS_NAME
	@SYSC_RETVALSTR($ret)
}
