# fallocate ____________________________________________________
#
# SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)

@define _SYSCALL_FALLOCATE_NAME
%(
	name = "fallocate"
%)

@define _SYSCALL_FALLOCATE_ARGSTR
%(
	argstr = sprintf("%d, %s, %#x, %u", fd, mode_str, offset, len)
%)

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

# dw_fallocate _____________________________________________________

probe dw_syscall.fallocate = kernel.function("sys_fallocate").call ?
{
	@__syscall_gate_compat_simple
	@_SYSCALL_FALLOCATE_NAME
	fd = __int32($fd)
	mode = __uint32($mode)
	mode_str = _stp_fallocate_mode_str(mode)
	offset = $offset
	len = $len
	@_SYSCALL_FALLOCATE_ARGSTR
}
probe dw_syscall.fallocate.return = kernel.function("sys_fallocate").return ?
{
	@__syscall_gate_compat_simple
	@_SYSCALL_FALLOCATE_NAME
	@SYSC_RETVALSTR($return)
}

# nd_fallocate _____________________________________________________

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

probe nd1_syscall.fallocate = kprobe.function("sys_fallocate") ?
{
	asmlinkage()
	@__syscall_gate_compat_simple
	@_SYSCALL_FALLOCATE_NAME
	fd = int_arg(1)
	mode = uint_arg(2)
	mode_str = _stp_fallocate_mode_str(mode)
%( CONFIG_64BIT == "y" %?
	offset = longlong_arg(3)
	len = longlong_arg(4)
%:
	# On a 32-bit kernel, 'long long' arguments take the space of
	# 2 arguments, so we have to adjust the following argument
	# numbers.
	offset = longlong_arg(3)
	len = longlong_arg(5)
%)
	@_SYSCALL_FALLOCATE_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.fallocate = kprobe.function(@arch_syscall_prefix "sys_fallocate") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_FALLOCATE_NAME
	fd = int_arg(1)
	mode = uint_arg(2)
	mode_str = _stp_fallocate_mode_str(mode)
	offset = longlong_arg(3)
	len = longlong_arg(4)
	@_SYSCALL_FALLOCATE_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.fallocate = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_fallocate"))
	@_SYSCALL_FALLOCATE_NAME
	fd = int_arg(1)
	mode = uint_arg(2)
	mode_str = _stp_fallocate_mode_str(mode)
%( CONFIG_64BIT == "y" %?
	offset = longlong_arg(3)
	len = longlong_arg(4)
%:
	# On a 32-bit kernel, 'long long' arguments take the space of
	# 2 arguments, so we have to adjust the following argument
	# numbers.
	offset = longlong_arg(3)
	len = longlong_arg(5)
%)
	@_SYSCALL_FALLOCATE_ARGSTR
}

probe nd_syscall.fallocate.return = nd1_syscall.fallocate.return!, nd2_syscall.fallocate.return!, tp_syscall.fallocate.return
  { }
  
probe nd1_syscall.fallocate.return = kprobe.function("sys_fallocate").return ?
{
	asmlinkage()
	@__syscall_gate_compat_simple
	@_SYSCALL_FALLOCATE_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.fallocate.return = kprobe.function(@arch_syscall_prefix "sys_fallocate").return ?
{
	@_SYSCALL_FALLOCATE_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.fallocate.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_fallocate"))
	@_SYSCALL_FALLOCATE_NAME
	@SYSC_RETVALSTR($ret)
}
