Commit 12d426ab authored by Al Viro's avatar Al Viro

ITER_PIPE: fold data_start() and pipe_space_for_user() together

All their callers are next to each other; all of them
want the total amount of pages and, possibly, the
offset in the partial final buffer.

Combine into a new helper (pipe_npages()), fix the
bogosity in pipe_space_for_user(), while we are at it.
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent 10f525a8
...@@ -156,26 +156,6 @@ static inline bool pipe_full(unsigned int head, unsigned int tail, ...@@ -156,26 +156,6 @@ static inline bool pipe_full(unsigned int head, unsigned int tail,
return pipe_occupancy(head, tail) >= limit; return pipe_occupancy(head, tail) >= limit;
} }
/**
* pipe_space_for_user - Return number of slots available to userspace
* @head: The pipe ring head pointer
* @tail: The pipe ring tail pointer
* @pipe: The pipe info structure
*/
static inline unsigned int pipe_space_for_user(unsigned int head, unsigned int tail,
struct pipe_inode_info *pipe)
{
unsigned int p_occupancy, p_space;
p_occupancy = pipe_occupancy(head, tail);
if (p_occupancy >= pipe->max_usage)
return 0;
p_space = pipe->ring_size - p_occupancy;
if (p_space > pipe->max_usage)
p_space = pipe->max_usage;
return p_space;
}
/** /**
* pipe_buf_get - get a reference to a pipe_buffer * pipe_buf_get - get a reference to a pipe_buffer
* @pipe: the pipe that the buffer belongs to * @pipe: the pipe that the buffer belongs to
......
...@@ -436,18 +436,20 @@ void iov_iter_init(struct iov_iter *i, unsigned int direction, ...@@ -436,18 +436,20 @@ void iov_iter_init(struct iov_iter *i, unsigned int direction,
} }
EXPORT_SYMBOL(iov_iter_init); EXPORT_SYMBOL(iov_iter_init);
static inline void data_start(const struct iov_iter *i, // returns the offset in partial buffer (if any)
unsigned int *iter_headp, size_t *offp) static inline unsigned int pipe_npages(const struct iov_iter *i, int *npages)
{ {
struct pipe_inode_info *pipe = i->pipe;
int used = pipe->head - pipe->tail;
int off = i->last_offset; int off = i->last_offset;
*npages = max((int)pipe->max_usage - used, 0);
if (off > 0 && off < PAGE_SIZE) { // anon and not full if (off > 0 && off < PAGE_SIZE) { // anon and not full
*iter_headp = i->pipe->head - 1; (*npages)++;
*offp = off; return off;
} else {
*iter_headp = i->pipe->head;
*offp = 0;
} }
return 0;
} }
static size_t copy_pipe_to_iter(const void *addr, size_t bytes, static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
...@@ -1318,18 +1320,16 @@ static ssize_t pipe_get_pages(struct iov_iter *i, ...@@ -1318,18 +1320,16 @@ static ssize_t pipe_get_pages(struct iov_iter *i,
struct page **pages, size_t maxsize, unsigned maxpages, struct page **pages, size_t maxsize, unsigned maxpages,
size_t *start) size_t *start)
{ {
unsigned int iter_head, npages; unsigned int npages, off;
size_t capacity; size_t capacity;
if (!sanity(i)) if (!sanity(i))
return -EFAULT; return -EFAULT;
data_start(i, &iter_head, start); *start = off = pipe_npages(i, &npages);
/* Amount of free space: some of this one + all after this one */ capacity = min(npages, maxpages) * PAGE_SIZE - off;
npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
capacity = min(npages, maxpages) * PAGE_SIZE - *start;
return __pipe_get_pages(i, min(maxsize, capacity), pages, *start); return __pipe_get_pages(i, min(maxsize, capacity), pages, off);
} }
static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa, static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
...@@ -1494,24 +1494,22 @@ static ssize_t pipe_get_pages_alloc(struct iov_iter *i, ...@@ -1494,24 +1494,22 @@ static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
size_t *start) size_t *start)
{ {
struct page **p; struct page **p;
unsigned int iter_head, npages; unsigned int npages, off;
ssize_t n; ssize_t n;
if (!sanity(i)) if (!sanity(i))
return -EFAULT; return -EFAULT;
data_start(i, &iter_head, start); *start = off = pipe_npages(i, &npages);
/* Amount of free space: some of this one + all after this one */ n = npages * PAGE_SIZE - off;
npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
n = npages * PAGE_SIZE - *start;
if (maxsize > n) if (maxsize > n)
maxsize = n; maxsize = n;
else else
npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE); npages = DIV_ROUND_UP(maxsize + off, PAGE_SIZE);
p = get_pages_array(npages); p = get_pages_array(npages);
if (!p) if (!p)
return -ENOMEM; return -ENOMEM;
n = __pipe_get_pages(i, maxsize, p, *start); n = __pipe_get_pages(i, maxsize, p, off);
if (n > 0) if (n > 0)
*pages = p; *pages = p;
else else
...@@ -1739,16 +1737,12 @@ int iov_iter_npages(const struct iov_iter *i, int maxpages) ...@@ -1739,16 +1737,12 @@ int iov_iter_npages(const struct iov_iter *i, int maxpages)
if (iov_iter_is_bvec(i)) if (iov_iter_is_bvec(i))
return bvec_npages(i, maxpages); return bvec_npages(i, maxpages);
if (iov_iter_is_pipe(i)) { if (iov_iter_is_pipe(i)) {
unsigned int iter_head;
int npages; int npages;
size_t off;
if (!sanity(i)) if (!sanity(i))
return 0; return 0;
data_start(i, &iter_head, &off); pipe_npages(i, &npages);
/* some of this one + all after this one */
npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe);
return min(npages, maxpages); return min(npages, maxpages);
} }
if (iov_iter_is_xarray(i)) { if (iov_iter_is_xarray(i)) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment