# fallocate __________________________________________________
#
# SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
# asmlinkage long sys32_fallocate(int fd, int mode, unsigned offset_lo,
#				unsigned offset_hi, unsigned len_lo,
#				unsigned len_hi)
#
# We've got a problem here on RHEL6 (2.6.32-504.el6.s390x) and RHEL7
# (3.10.0-229.el7.s390x).  Normally we'd probe the "compat" version of
# the function here.  However, on those kernels, the compat wrapper
# for fallocate (sys_fallocate_wrapper) is in assembly language, which
# stap can't find.
#
# So, we'll probe the "real" syscall here.

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

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

@define _SYSCALL_COMPAT_FALLOCATE_REGARGS
%(
	fd = int_arg(1)
	mode = int_arg(2)
	# This logic is stolen from _stp_arg (in
	# tapset/s390/registers.stp).
	mode_str = _stp_fallocate_mode_str(mode)
	offset = u_register("r4")
	len = u_register("r5")
%)

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

# dw_compat_fallocate _____________________________________________________

probe dw_syscall.compat_fallocate = kernel.function("sys_fallocate").call ?
{
	@__syscall_gate_noncompat_simple
	@_SYSCALL_COMPAT_FALLOCATE_NAME
	fd = __int32($fd)
	mode = __uint32($mode)
	mode_str = _stp_fallocate_mode_str(mode)
	offset = $offset
	len = $len
	@_SYSCALL_COMPAT_FALLOCATE_ARGSTR
}
probe dw_syscall.compat_fallocate.return =
	kernel.function("sys_fallocate").return ?
{
	@__syscall_gate_noncompat_simple
	@_SYSCALL_COMPAT_FALLOCATE_NAME
	@SYSC_RETVALSTR(returnval())
}

# nd_compat_fallocate _____________________________________________________

probe nd_syscall.compat_fallocate = nd1_syscall.compat_fallocate!, tp_syscall.compat_fallocate
  { }

probe nd1_syscall.compat_fallocate = kprobe.function("sys_fallocate") ?
{
	@__syscall_gate_noncompat_simple
	asmlinkage()
	@_SYSCALL_COMPAT_FALLOCATE_NAME
	@_SYSCALL_COMPAT_FALLOCATE_REGARGS
	@_SYSCALL_COMPAT_FALLOCATE_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.compat_fallocate = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_fallocate"))
	@_SYSCALL_COMPAT_FALLOCATE_NAME
	@_SYSCALL_COMPAT_FALLOCATE_REGARGS
	@_SYSCALL_COMPAT_FALLOCATE_ARGSTR
}

probe nd_syscall.compat_fallocate.return = nd1_syscall.compat_fallocate.return!, tp_syscall.compat_fallocate.return
  { }

probe nd1_syscall.compat_fallocate.return =
	kprobe.function("sys_fallocate").return ?
{
	@__syscall_gate_noncompat_simple
	@_SYSCALL_COMPAT_FALLOCATE_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.compat_fallocate.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_fallocate"))
	@_SYSCALL_COMPAT_FALLOCATE_NAME
	@SYSC_RETVALSTR($ret)
}
