1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* Iterator helpers. 3 * 4 * Copyright (C) 2022 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 8 #include <linux/export.h> 9 #include <linux/slab.h> 10 #include <linux/mm.h> 11 #include <linux/uio.h> 12 #include <linux/scatterlist.h> 13 #include <linux/netfs.h> 14 #include "internal.h" 15 16 /** 17 * netfs_extract_user_iter - Extract the pages from a user iterator into a bvec 18 * @orig: The original iterator 19 * @orig_len: The amount of iterator to copy 20 * @new: The iterator to be set up 21 * @extraction_flags: Flags to qualify the request 22 * 23 * Extract the page fragments from the given amount of the source iterator and 24 * build up a second iterator that refers to all of those bits. This allows 25 * the original iterator to disposed of. 26 * 27 * @extraction_flags can have ITER_ALLOW_P2PDMA set to request peer-to-peer DMA be 28 * allowed on the pages extracted. 29 * 30 * On success, the number of elements in the bvec is returned, the original 31 * iterator will have been advanced by the amount extracted. 32 * 33 * The iov_iter_extract_mode() function should be used to query how cleanup 34 * should be performed. 35 */ 36 ssize_t netfs_extract_user_iter(struct iov_iter *orig, size_t orig_len, 37 struct iov_iter *new, 38 iov_iter_extraction_t extraction_flags) 39 { 40 struct bio_vec *bv = NULL; 41 struct page **pages; 42 unsigned int cur_npages; 43 unsigned int max_pages; 44 unsigned int npages = 0; 45 unsigned int i; 46 ssize_t ret; 47 size_t count = orig_len, offset, len; 48 size_t bv_size, pg_size; 49 50 if (WARN_ON_ONCE(!iter_is_ubuf(orig) && !iter_is_iovec(orig))) 51 return -EIO; 52 53 max_pages = iov_iter_npages(orig, INT_MAX); 54 bv_size = array_size(max_pages, sizeof(*bv)); 55 bv = kvmalloc(bv_size, GFP_KERNEL); 56 if (!bv) 57 return -ENOMEM; 58 59 /* Put the page list at the end of the bvec list storage. bvec 60 * elements are larger than page pointers, so as long as we work 61 * 0->last, we should be fine. 62 */ 63 pg_size = array_size(max_pages, sizeof(*pages)); 64 pages = (void *)bv + bv_size - pg_size; 65 66 while (count && npages < max_pages) { 67 ret = iov_iter_extract_pages(orig, &pages, count, 68 max_pages - npages, extraction_flags, 69 &offset); 70 if (ret < 0) { 71 pr_err("Couldn't get user pages (rc=%zd)\n", ret); 72 break; 73 } 74 75 if (ret > count) { 76 pr_err("get_pages rc=%zd more than %zu\n", ret, count); 77 break; 78 } 79 80 count -= ret; 81 ret += offset; 82 cur_npages = DIV_ROUND_UP(ret, PAGE_SIZE); 83 84 if (npages + cur_npages > max_pages) { 85 pr_err("Out of bvec array capacity (%u vs %u)\n", 86 npages + cur_npages, max_pages); 87 break; 88 } 89 90 for (i = 0; i < cur_npages; i++) { 91 len = ret > PAGE_SIZE ? PAGE_SIZE : ret; 92 bvec_set_page(bv + npages + i, *pages++, len - offset, offset); 93 ret -= len; 94 offset = 0; 95 } 96 97 npages += cur_npages; 98 } 99 100 iov_iter_bvec(new, orig->data_source, bv, npages, orig_len - count); 101 return npages; 102 } 103 EXPORT_SYMBOL_GPL(netfs_extract_user_iter); 104