(root)/
strace-6.5/
src/
sg_io_v4.c
       1  /*
       2   * Copyright (c) 2015 Bart Van Assche <bart.vanassche@sandisk.com>
       3   * Copyright (c) 2015-2021 Dmitry V. Levin <ldv@strace.io>
       4   * Copyright (c) 2021-2023 The strace developers.
       5   * All rights reserved.
       6   *
       7   * SPDX-License-Identifier: LGPL-2.1-or-later
       8   */
       9  
      10  #include "defs.h"
      11  #include <linux/bsg.h>
      12  #include "xlat/bsg_protocol.h"
      13  #include "xlat/bsg_subprotocol.h"
      14  #include "xlat/bsg_flags.h"
      15  
      16  static void
      17  print_sg_io_buffer(struct tcb *const tcp, const kernel_ulong_t addr,
      18  		   const unsigned int data_size, const unsigned int iovec_count)
      19  {
      20  	if (iovec_count) {
      21  		tprint_iov_upto(tcp, iovec_count, addr, data_size,
      22  				iov_decode_str, NULL);
      23  	} else {
      24  		printstr_ex(tcp, addr, data_size, QUOTE_FORCE_HEX);
      25  	}
      26  }
      27  
      28  #define PRINT_FIELD_SG_IO_BUFFER(where_, field_, size_, count_, tcp_)		\
      29  	do {									\
      30  		tprints_field_name(#field_);					\
      31  		print_sg_io_buffer((tcp_), (where_).field_, (size_), (count_));	\
      32  	} while (0)
      33  
      34  static int
      35  decode_request(struct tcb *const tcp, const kernel_ulong_t arg)
      36  {
      37  	struct sg_io_v4 sg_io;
      38  	static const size_t skip_iid = offsetof(struct sg_io_v4, protocol);
      39  
      40  	tprint_struct_begin();
      41  	tprints_field_name("guard");
      42  	tprints_string("'Q'");
      43  	tprint_struct_next();
      44  	if (umoven_or_printaddr(tcp, arg + skip_iid, sizeof(sg_io) - skip_iid,
      45  				&sg_io.protocol)) {
      46  		tprint_struct_end();
      47  		return RVAL_IOCTL_DECODED;
      48  	}
      49  
      50  	PRINT_FIELD_XVAL(sg_io, protocol, bsg_protocol, "BSG_PROTOCOL_???");
      51  	tprint_struct_next();
      52  	PRINT_FIELD_XVAL(sg_io, subprotocol, bsg_subprotocol,
      53  			 "BSG_SUB_PROTOCOL_???");
      54  	tprint_struct_next();
      55  	PRINT_FIELD_U(sg_io, request_len);
      56  	tprint_struct_next();
      57  	PRINT_FIELD_SG_IO_BUFFER(sg_io, request, sg_io.request_len, 0, tcp);
      58  	tprint_struct_next();
      59  	PRINT_FIELD_X(sg_io, request_tag);
      60  	tprint_struct_next();
      61  	PRINT_FIELD_U(sg_io, request_attr);
      62  	tprint_struct_next();
      63  	PRINT_FIELD_U(sg_io, request_priority);
      64  	tprint_struct_next();
      65  	PRINT_FIELD_U(sg_io, request_extra);
      66  	tprint_struct_next();
      67  	PRINT_FIELD_U(sg_io, max_response_len);
      68  
      69  	tprint_struct_next();
      70  	PRINT_FIELD_U(sg_io, dout_iovec_count);
      71  	tprint_struct_next();
      72  	PRINT_FIELD_U(sg_io, dout_xfer_len);
      73  	tprint_struct_next();
      74  	PRINT_FIELD_U(sg_io, din_iovec_count);
      75  	tprint_struct_next();
      76  	PRINT_FIELD_U(sg_io, din_xfer_len);
      77  	tprint_struct_next();
      78  	PRINT_FIELD_SG_IO_BUFFER(sg_io, dout_xferp, sg_io.dout_xfer_len,
      79  				 sg_io.dout_iovec_count, tcp);
      80  	tprint_struct_next();
      81  	PRINT_FIELD_U(sg_io, timeout);
      82  	tprint_struct_next();
      83  	PRINT_FIELD_FLAGS(sg_io, flags, bsg_flags, "BSG_FLAG_???");
      84  	tprint_struct_next();
      85  	PRINT_FIELD_X(sg_io, usr_ptr);
      86  
      87  	struct sg_io_v4 *entering_sg_io = xobjdup(&sg_io);
      88  	entering_sg_io->guard = (unsigned char) 'Q';
      89  	set_tcb_priv_data(tcp, entering_sg_io, free);
      90  
      91  	return 0;
      92  }
      93  
      94  static int
      95  decode_response(struct tcb *const tcp, const kernel_ulong_t arg)
      96  {
      97  	struct sg_io_v4 *entering_sg_io = get_tcb_priv_data(tcp);
      98  	struct sg_io_v4 sg_io;
      99  	uint32_t din_len;
     100  
     101  	if (umove(tcp, arg, &sg_io) < 0) {
     102  		/* print i/o fields fetched on entering syscall */
     103  		tprint_struct_next();
     104  		PRINT_FIELD_X(*entering_sg_io, response);
     105  		tprint_struct_next();
     106  		PRINT_FIELD_X(*entering_sg_io, din_xferp);
     107  		return RVAL_IOCTL_DECODED;
     108  	}
     109  
     110  	if (sg_io.guard != entering_sg_io->guard) {
     111  		tprint_value_changed();
     112  		PRINT_FIELD_U(sg_io, guard);
     113  		return RVAL_IOCTL_DECODED;
     114  	}
     115  
     116  	tprint_struct_next();
     117  	PRINT_FIELD_U(sg_io, response_len);
     118  	tprint_struct_next();
     119  	PRINT_FIELD_SG_IO_BUFFER(sg_io, response, sg_io.response_len, 0, tcp);
     120  	din_len = sg_io.din_xfer_len;
     121  	if (sg_io.din_resid > 0 && (unsigned int) sg_io.din_resid <= din_len)
     122  		din_len -= sg_io.din_resid;
     123  	tprint_struct_next();
     124  	PRINT_FIELD_SG_IO_BUFFER(sg_io, din_xferp, din_len,
     125  				 sg_io.din_iovec_count, tcp);
     126  	tprint_struct_next();
     127  	PRINT_FIELD_X(sg_io, driver_status);
     128  	tprint_struct_next();
     129  	PRINT_FIELD_X(sg_io, transport_status);
     130  	tprint_struct_next();
     131  	PRINT_FIELD_X(sg_io, device_status);
     132  	tprint_struct_next();
     133  	PRINT_FIELD_U(sg_io, retry_delay);
     134  	tprint_struct_next();
     135  	PRINT_FIELD_FLAGS(sg_io, info, sg_io_info, "SG_INFO_???");
     136  	tprint_struct_next();
     137  	PRINT_FIELD_U(sg_io, duration);
     138  	tprint_struct_next();
     139  	PRINT_FIELD_U(sg_io, response_len);
     140  	tprint_struct_next();
     141  	PRINT_FIELD_D(sg_io, din_resid);
     142  	tprint_struct_next();
     143  	PRINT_FIELD_D(sg_io, dout_resid);
     144  	tprint_struct_next();
     145  	PRINT_FIELD_X(sg_io, generated_tag);
     146  
     147  	return RVAL_IOCTL_DECODED;
     148  }
     149  
     150  int
     151  decode_sg_io_v4(struct tcb *const tcp, const kernel_ulong_t arg)
     152  {
     153  	return entering(tcp) ? decode_request(tcp, arg)
     154  			     : decode_response(tcp, arg);
     155  }