int rds_message_copy_from_user(struct rds_message *rm, struct iovec *first_iov, size_t total_len) { unsigned long to_copy; unsigned long iov_off; unsigned long sg_off; struct iovec *iov; struct scatterlist *sg; int ret = 0; rm->m_inc.i_hdr.h_len = cpu_to_be32(total_len); /* * now allocate and copy in the data payload. */ sg = rm->data.op_sg; iov = first_iov; iov_off = 0; sg_off = 0; /* Dear gcc, sg->page will be null from kzalloc. */ while (total_len) { if (!sg_page(sg)) { ret = rds_page_remainder_alloc(sg, total_len, GFP_HIGHUSER); if (ret) goto out; rm->data.op_nents++; sg_off = 0; } while (iov_off == iov->iov_len) { iov_off = 0; iov++; } to_copy = min(iov->iov_len - iov_off, sg->length - sg_off); to_copy = min_t(size_t, to_copy, total_len); rdsdebug("copying %lu bytes from user iov [%p, %zu] + %lu to " "sg [%p, %u, %u] + %lu\n", to_copy, iov->iov_base, iov->iov_len, iov_off, (void *)sg_page(sg), sg->offset, sg->length, sg_off); ret = rds_page_copy_from_user(sg_page(sg), sg->offset + sg_off, iov->iov_base + iov_off, to_copy); if (ret) goto out; iov_off += to_copy; total_len -= to_copy; sg_off += to_copy; if (sg_off == sg->length) sg++; } out: return ret; }
int rds_message_copy_from_user(struct rds_message *rm, struct iov_iter *from) { unsigned long to_copy, nbytes; unsigned long sg_off; struct scatterlist *sg; int ret = 0; rm->m_inc.i_hdr.h_len = cpu_to_be32(iov_iter_count(from)); /* * now allocate and copy in the data payload. */ sg = rm->data.op_sg; sg_off = 0; /* Dear gcc, sg->page will be null from kzalloc. */ while (iov_iter_count(from)) { if (!sg_page(sg)) { ret = rds_page_remainder_alloc(sg, iov_iter_count(from), GFP_HIGHUSER); if (ret) return ret; rm->data.op_nents++; sg_off = 0; } to_copy = min_t(unsigned long, iov_iter_count(from), sg->length - sg_off); rds_stats_add(s_copy_from_user, to_copy); nbytes = copy_page_from_iter(sg_page(sg), sg->offset + sg_off, to_copy, from); if (nbytes != to_copy) return -EFAULT; sg_off += to_copy; if (sg_off == sg->length) sg++; } return ret; }