# statfs64 ___________________________________________________
#
# long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
# long compat_sys_statfs64(const char __user *path, compat_size_t sz, struct compat_statfs64 __user *buf)
#

@define _SYSCALL_STATFS64_NAME
%(
	name = "statfs64"
%)

@define _SYSCALL_STATFS64_ARGSTR
%(
	argstr = sprintf("%s, %u, %p", path, sz, buf_uaddr)
%)

@define _SYSCALL_STATFS64_REGARGS
%(
	path = user_string_quoted(pointer_arg(1))
	path_unquoted = user_string_nofault(pointer_arg(1))
	sz = ulong_arg(2)
	buf_uaddr = pointer_arg(3)
%)

@define _SYSCALL_STATFS64_REGARGS_STORE
%(
        if (@probewrite(path_unquoted))
          set_user_string_arg(pointer_arg(1), path_unquoted)

	if (@probewrite(sz))
	  set_ulong_arg(2, sz)

	if (@probewrite(buf_uaddr))
	  set_pointer_arg(3, buf_uaddr)
%)

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

# dw_statfs64 _____________________________________________________

probe dw_syscall.statfs64 = kernel.function("sys_statfs64").call ?,
	kernel.function("compat_sys_statfs64").call ?
{
	@_SYSCALL_STATFS64_NAME
	path = user_string_quoted(@__pointer(@choose_defined($pathname, $path)))
	sz = __ulong($sz)
	buf_uaddr = @__pointer($buf)
	@_SYSCALL_STATFS64_ARGSTR
}
probe dw_syscall.statfs64.return = kernel.function("sys_statfs64").return ?,
	kernel.function("compat_sys_statfs64").return ?
{
	@_SYSCALL_STATFS64_NAME
	@SYSC_RETVALSTR($return)
}

# nd_statfs64 _____________________________________________________

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

probe nd1_syscall.statfs64 = kprobe.function("sys_statfs64") ?,
	kprobe.function("compat_sys_statfs64") ?
{
	@_SYSCALL_STATFS64_NAME
	asmlinkage()
	@_SYSCALL_STATFS64_REGARGS
	@_SYSCALL_STATFS64_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.statfs64 =
	kprobe.function(@arch_syscall_prefix "compat_sys_statfs64") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_STATFS64_NAME
	@_SYSCALL_STATFS64_REGARGS
	@_SYSCALL_STATFS64_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_STATFS64_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.statfs64 = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_statfs64"), @const("__NR_compat_statfs64"))
	@_SYSCALL_STATFS64_NAME
	@_SYSCALL_STATFS64_REGARGS
	@_SYSCALL_STATFS64_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_STATFS64_REGARGS_STORE %)
}

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

probe nd1_syscall.statfs64.return =
	kprobe.function("sys_statfs64").return ?,
	kprobe.function("compat_sys_statfs64").return ?
{
	@_SYSCALL_STATFS64_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.statfs64.return =
	kprobe.function(@arch_syscall_prefix "compat_sys_statfs64").return ?
{
	@_SYSCALL_STATFS64_NAME
	@SYSC_RETVALSTR(returnval())
}

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