static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)

{

    size_t pos = 0;

    int i, j;

    struct iovec *src_sg;

    unsigned int num;



    if (rx) {

        src_sg = pdu->elem.in_sg;

        num = pdu->elem.in_num;

    } else {

        src_sg = pdu->elem.out_sg;

        num = pdu->elem.out_num;

    }



    j = 0;

    for (i = 0; i < num; i++) {

        if (offset <= pos) {

            sg[j].iov_base = src_sg[i].iov_base;

            sg[j].iov_len = src_sg[i].iov_len;

            j++;

        } else if (offset < (src_sg[i].iov_len + pos)) {

            sg[j].iov_base = src_sg[i].iov_base;

            sg[j].iov_len = src_sg[i].iov_len;

            sg[j].iov_base += (offset - pos);

            sg[j].iov_len -= (offset - pos);

            j++;

        }

        pos += src_sg[i].iov_len;

    }



    return j;

}
