]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * linux/fs/pipe.c | |
4 | * | |
5 | * Copyright (C) 1991, 1992, 1999 Linus Torvalds | |
6 | */ | |
7 | ||
8 | #include <linux/mm.h> | |
9 | #include <linux/file.h> | |
10 | #include <linux/poll.h> | |
11 | #include <linux/slab.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/fs.h> | |
35f3d14d | 15 | #include <linux/log2.h> |
1da177e4 | 16 | #include <linux/mount.h> |
b502bd11 | 17 | #include <linux/magic.h> |
1da177e4 LT |
18 | #include <linux/pipe_fs_i.h> |
19 | #include <linux/uio.h> | |
20 | #include <linux/highmem.h> | |
5274f052 | 21 | #include <linux/pagemap.h> |
db349509 | 22 | #include <linux/audit.h> |
ba719bae | 23 | #include <linux/syscalls.h> |
b492e95b | 24 | #include <linux/fcntl.h> |
d86133bd | 25 | #include <linux/memcontrol.h> |
1da177e4 | 26 | |
7c0f6ba6 | 27 | #include <linux/uaccess.h> |
1da177e4 LT |
28 | #include <asm/ioctls.h> |
29 | ||
599a0ac1 AV |
30 | #include "internal.h" |
31 | ||
b492e95b JA |
32 | /* |
33 | * The max size that a non-root user is allowed to grow the pipe. Can | |
ff9da691 | 34 | * be set by root in /proc/sys/fs/pipe-max-size |
b492e95b | 35 | */ |
ff9da691 JA |
36 | unsigned int pipe_max_size = 1048576; |
37 | ||
759c0114 WT |
38 | /* Maximum allocatable pages per user. Hard limit is unset by default, soft |
39 | * matches default values. | |
40 | */ | |
41 | unsigned long pipe_user_pages_hard; | |
42 | unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR; | |
43 | ||
1da177e4 LT |
44 | /* |
45 | * We use a start+len construction, which provides full use of the | |
46 | * allocated memory. | |
47 | * -- Florian Coosmann (FGC) | |
48 | * | |
49 | * Reads with count = 0 should always return 0. | |
50 | * -- Julian Bradfield 1999-06-07. | |
51 | * | |
52 | * FIFOs and Pipes now generate SIGIO for both readers and writers. | |
53 | * -- Jeremy Elson <[email protected]> 2001-08-16 | |
54 | * | |
55 | * pipe_read & write cleanup | |
56 | * -- Manfred Spraul <[email protected]> 2002-05-09 | |
57 | */ | |
58 | ||
61e0d47c MS |
59 | static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass) |
60 | { | |
6447a3cf | 61 | if (pipe->files) |
72b0d9aa | 62 | mutex_lock_nested(&pipe->mutex, subclass); |
61e0d47c MS |
63 | } |
64 | ||
65 | void pipe_lock(struct pipe_inode_info *pipe) | |
66 | { | |
67 | /* | |
68 | * pipe_lock() nests non-pipe inode locks (for writing to a file) | |
69 | */ | |
70 | pipe_lock_nested(pipe, I_MUTEX_PARENT); | |
71 | } | |
72 | EXPORT_SYMBOL(pipe_lock); | |
73 | ||
74 | void pipe_unlock(struct pipe_inode_info *pipe) | |
75 | { | |
6447a3cf | 76 | if (pipe->files) |
72b0d9aa | 77 | mutex_unlock(&pipe->mutex); |
61e0d47c MS |
78 | } |
79 | EXPORT_SYMBOL(pipe_unlock); | |
80 | ||
ebec73f4 AV |
81 | static inline void __pipe_lock(struct pipe_inode_info *pipe) |
82 | { | |
83 | mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT); | |
84 | } | |
85 | ||
86 | static inline void __pipe_unlock(struct pipe_inode_info *pipe) | |
87 | { | |
88 | mutex_unlock(&pipe->mutex); | |
89 | } | |
90 | ||
61e0d47c MS |
91 | void pipe_double_lock(struct pipe_inode_info *pipe1, |
92 | struct pipe_inode_info *pipe2) | |
93 | { | |
94 | BUG_ON(pipe1 == pipe2); | |
95 | ||
96 | if (pipe1 < pipe2) { | |
97 | pipe_lock_nested(pipe1, I_MUTEX_PARENT); | |
98 | pipe_lock_nested(pipe2, I_MUTEX_CHILD); | |
99 | } else { | |
023d43c7 PZ |
100 | pipe_lock_nested(pipe2, I_MUTEX_PARENT); |
101 | pipe_lock_nested(pipe1, I_MUTEX_CHILD); | |
61e0d47c MS |
102 | } |
103 | } | |
104 | ||
1da177e4 | 105 | /* Drop the inode semaphore and wait for a pipe event, atomically */ |
3a326a2c | 106 | void pipe_wait(struct pipe_inode_info *pipe) |
1da177e4 LT |
107 | { |
108 | DEFINE_WAIT(wait); | |
109 | ||
d79fc0fc IM |
110 | /* |
111 | * Pipes are system-local resources, so sleeping on them | |
112 | * is considered a noninteractive wait: | |
113 | */ | |
af927232 | 114 | prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE); |
61e0d47c | 115 | pipe_unlock(pipe); |
1da177e4 | 116 | schedule(); |
3a326a2c | 117 | finish_wait(&pipe->wait, &wait); |
61e0d47c | 118 | pipe_lock(pipe); |
1da177e4 LT |
119 | } |
120 | ||
341b446b IM |
121 | static void anon_pipe_buf_release(struct pipe_inode_info *pipe, |
122 | struct pipe_buffer *buf) | |
1da177e4 LT |
123 | { |
124 | struct page *page = buf->page; | |
125 | ||
5274f052 JA |
126 | /* |
127 | * If nobody else uses this page, and we don't already have a | |
128 | * temporary page, let's keep track of it as a one-deep | |
341b446b | 129 | * allocation cache. (Otherwise just release our reference to it) |
5274f052 | 130 | */ |
341b446b | 131 | if (page_count(page) == 1 && !pipe->tmp_page) |
923f4f23 | 132 | pipe->tmp_page = page; |
341b446b | 133 | else |
09cbfeaf | 134 | put_page(page); |
1da177e4 LT |
135 | } |
136 | ||
d86133bd VD |
137 | static int anon_pipe_buf_steal(struct pipe_inode_info *pipe, |
138 | struct pipe_buffer *buf) | |
139 | { | |
140 | struct page *page = buf->page; | |
141 | ||
142 | if (page_count(page) == 1) { | |
60cd4bcd | 143 | memcg_kmem_uncharge(page, 0); |
d86133bd VD |
144 | __SetPageLocked(page); |
145 | return 0; | |
146 | } | |
147 | return 1; | |
148 | } | |
149 | ||
0845718d | 150 | /** |
b51d63c6 | 151 | * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer |
0845718d JA |
152 | * @pipe: the pipe that the buffer belongs to |
153 | * @buf: the buffer to attempt to steal | |
154 | * | |
155 | * Description: | |
b51d63c6 | 156 | * This function attempts to steal the &struct page attached to |
0845718d JA |
157 | * @buf. If successful, this function returns 0 and returns with |
158 | * the page locked. The caller may then reuse the page for whatever | |
b51d63c6 | 159 | * he wishes; the typical use is insertion into a different file |
0845718d JA |
160 | * page cache. |
161 | */ | |
330ab716 JA |
162 | int generic_pipe_buf_steal(struct pipe_inode_info *pipe, |
163 | struct pipe_buffer *buf) | |
5abc97aa | 164 | { |
46e678c9 JA |
165 | struct page *page = buf->page; |
166 | ||
0845718d JA |
167 | /* |
168 | * A reference of one is golden, that means that the owner of this | |
169 | * page is the only one holding a reference to it. lock the page | |
170 | * and return OK. | |
171 | */ | |
46e678c9 | 172 | if (page_count(page) == 1) { |
46e678c9 JA |
173 | lock_page(page); |
174 | return 0; | |
175 | } | |
176 | ||
177 | return 1; | |
5abc97aa | 178 | } |
51921cb7 | 179 | EXPORT_SYMBOL(generic_pipe_buf_steal); |
5abc97aa | 180 | |
0845718d | 181 | /** |
b51d63c6 | 182 | * generic_pipe_buf_get - get a reference to a &struct pipe_buffer |
0845718d JA |
183 | * @pipe: the pipe that the buffer belongs to |
184 | * @buf: the buffer to get a reference to | |
185 | * | |
186 | * Description: | |
187 | * This function grabs an extra reference to @buf. It's used in | |
188 | * in the tee() system call, when we duplicate the buffers in one | |
189 | * pipe into another. | |
190 | */ | |
191 | void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf) | |
70524490 | 192 | { |
09cbfeaf | 193 | get_page(buf->page); |
70524490 | 194 | } |
51921cb7 | 195 | EXPORT_SYMBOL(generic_pipe_buf_get); |
70524490 | 196 | |
0845718d JA |
197 | /** |
198 | * generic_pipe_buf_confirm - verify contents of the pipe buffer | |
79685b8d | 199 | * @info: the pipe that the buffer belongs to |
0845718d JA |
200 | * @buf: the buffer to confirm |
201 | * | |
202 | * Description: | |
203 | * This function does nothing, because the generic pipe code uses | |
204 | * pages that are always good when inserted into the pipe. | |
205 | */ | |
cac36bb0 JA |
206 | int generic_pipe_buf_confirm(struct pipe_inode_info *info, |
207 | struct pipe_buffer *buf) | |
f84d7519 JA |
208 | { |
209 | return 0; | |
210 | } | |
51921cb7 | 211 | EXPORT_SYMBOL(generic_pipe_buf_confirm); |
f84d7519 | 212 | |
6818173b MS |
213 | /** |
214 | * generic_pipe_buf_release - put a reference to a &struct pipe_buffer | |
215 | * @pipe: the pipe that the buffer belongs to | |
216 | * @buf: the buffer to put a reference to | |
217 | * | |
218 | * Description: | |
219 | * This function releases a reference to @buf. | |
220 | */ | |
221 | void generic_pipe_buf_release(struct pipe_inode_info *pipe, | |
222 | struct pipe_buffer *buf) | |
223 | { | |
09cbfeaf | 224 | put_page(buf->page); |
6818173b | 225 | } |
51921cb7 | 226 | EXPORT_SYMBOL(generic_pipe_buf_release); |
6818173b | 227 | |
01e7187b | 228 | /* New data written to a pipe may be appended to a buffer with this type. */ |
d4c3cca9 | 229 | static const struct pipe_buf_operations anon_pipe_buf_ops = { |
cac36bb0 | 230 | .confirm = generic_pipe_buf_confirm, |
1da177e4 | 231 | .release = anon_pipe_buf_release, |
d86133bd | 232 | .steal = anon_pipe_buf_steal, |
f84d7519 | 233 | .get = generic_pipe_buf_get, |
1da177e4 LT |
234 | }; |
235 | ||
a0ce2f0a | 236 | static const struct pipe_buf_operations anon_pipe_buf_nomerge_ops = { |
cac36bb0 | 237 | .confirm = generic_pipe_buf_confirm, |
1da177e4 | 238 | .release = anon_pipe_buf_release, |
d86133bd | 239 | .steal = anon_pipe_buf_steal, |
f84d7519 | 240 | .get = generic_pipe_buf_get, |
1da177e4 LT |
241 | }; |
242 | ||
9883035a | 243 | static const struct pipe_buf_operations packet_pipe_buf_ops = { |
9883035a LT |
244 | .confirm = generic_pipe_buf_confirm, |
245 | .release = anon_pipe_buf_release, | |
d86133bd | 246 | .steal = anon_pipe_buf_steal, |
9883035a LT |
247 | .get = generic_pipe_buf_get, |
248 | }; | |
249 | ||
01e7187b JH |
250 | /** |
251 | * pipe_buf_mark_unmergeable - mark a &struct pipe_buffer as unmergeable | |
252 | * @buf: the buffer to mark | |
253 | * | |
254 | * Description: | |
255 | * This function ensures that no future writes will be merged into the | |
256 | * given &struct pipe_buffer. This is necessary when multiple pipe buffers | |
257 | * share the same backing page. | |
258 | */ | |
a0ce2f0a JH |
259 | void pipe_buf_mark_unmergeable(struct pipe_buffer *buf) |
260 | { | |
261 | if (buf->ops == &anon_pipe_buf_ops) | |
262 | buf->ops = &anon_pipe_buf_nomerge_ops; | |
263 | } | |
264 | ||
01e7187b JH |
265 | static bool pipe_buf_can_merge(struct pipe_buffer *buf) |
266 | { | |
267 | return buf->ops == &anon_pipe_buf_ops; | |
268 | } | |
269 | ||
1da177e4 | 270 | static ssize_t |
fb9096a3 | 271 | pipe_read(struct kiocb *iocb, struct iov_iter *to) |
1da177e4 | 272 | { |
fb9096a3 | 273 | size_t total_len = iov_iter_count(to); |
ee0b3e67 | 274 | struct file *filp = iocb->ki_filp; |
de32ec4c | 275 | struct pipe_inode_info *pipe = filp->private_data; |
1da177e4 LT |
276 | int do_wakeup; |
277 | ssize_t ret; | |
1da177e4 | 278 | |
1da177e4 LT |
279 | /* Null read succeeds. */ |
280 | if (unlikely(total_len == 0)) | |
281 | return 0; | |
282 | ||
283 | do_wakeup = 0; | |
284 | ret = 0; | |
ebec73f4 | 285 | __pipe_lock(pipe); |
1da177e4 | 286 | for (;;) { |
923f4f23 | 287 | int bufs = pipe->nrbufs; |
1da177e4 | 288 | if (bufs) { |
923f4f23 IM |
289 | int curbuf = pipe->curbuf; |
290 | struct pipe_buffer *buf = pipe->bufs + curbuf; | |
1da177e4 | 291 | size_t chars = buf->len; |
637b58c2 AV |
292 | size_t written; |
293 | int error; | |
1da177e4 LT |
294 | |
295 | if (chars > total_len) | |
296 | chars = total_len; | |
297 | ||
fba597db | 298 | error = pipe_buf_confirm(pipe, buf); |
f84d7519 | 299 | if (error) { |
5274f052 | 300 | if (!ret) |
e5953cbd | 301 | ret = error; |
5274f052 JA |
302 | break; |
303 | } | |
f84d7519 | 304 | |
fb9096a3 | 305 | written = copy_page_to_iter(buf->page, buf->offset, chars, to); |
637b58c2 | 306 | if (unlikely(written < chars)) { |
341b446b | 307 | if (!ret) |
637b58c2 | 308 | ret = -EFAULT; |
1da177e4 LT |
309 | break; |
310 | } | |
311 | ret += chars; | |
312 | buf->offset += chars; | |
313 | buf->len -= chars; | |
9883035a LT |
314 | |
315 | /* Was it a packet buffer? Clean up and exit */ | |
316 | if (buf->flags & PIPE_BUF_FLAG_PACKET) { | |
317 | total_len = chars; | |
318 | buf->len = 0; | |
319 | } | |
320 | ||
1da177e4 | 321 | if (!buf->len) { |
a779638c | 322 | pipe_buf_release(pipe, buf); |
35f3d14d | 323 | curbuf = (curbuf + 1) & (pipe->buffers - 1); |
923f4f23 IM |
324 | pipe->curbuf = curbuf; |
325 | pipe->nrbufs = --bufs; | |
1da177e4 LT |
326 | do_wakeup = 1; |
327 | } | |
328 | total_len -= chars; | |
329 | if (!total_len) | |
330 | break; /* common path: read succeeded */ | |
331 | } | |
332 | if (bufs) /* More to do? */ | |
333 | continue; | |
923f4f23 | 334 | if (!pipe->writers) |
1da177e4 | 335 | break; |
923f4f23 | 336 | if (!pipe->waiting_writers) { |
1da177e4 LT |
337 | /* syscall merging: Usually we must not sleep |
338 | * if O_NONBLOCK is set, or if we got some data. | |
339 | * But if a writer sleeps in kernel space, then | |
340 | * we can wait for that data without violating POSIX. | |
341 | */ | |
342 | if (ret) | |
343 | break; | |
344 | if (filp->f_flags & O_NONBLOCK) { | |
345 | ret = -EAGAIN; | |
346 | break; | |
347 | } | |
348 | } | |
349 | if (signal_pending(current)) { | |
341b446b IM |
350 | if (!ret) |
351 | ret = -ERESTARTSYS; | |
1da177e4 LT |
352 | break; |
353 | } | |
354 | if (do_wakeup) { | |
a9a08845 | 355 | wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM); |
923f4f23 | 356 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
1da177e4 | 357 | } |
923f4f23 | 358 | pipe_wait(pipe); |
1da177e4 | 359 | } |
ebec73f4 | 360 | __pipe_unlock(pipe); |
341b446b IM |
361 | |
362 | /* Signal writers asynchronously that there is more room. */ | |
1da177e4 | 363 | if (do_wakeup) { |
a9a08845 | 364 | wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM); |
923f4f23 | 365 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
1da177e4 LT |
366 | } |
367 | if (ret > 0) | |
368 | file_accessed(filp); | |
369 | return ret; | |
370 | } | |
371 | ||
9883035a LT |
372 | static inline int is_packetized(struct file *file) |
373 | { | |
374 | return (file->f_flags & O_DIRECT) != 0; | |
375 | } | |
376 | ||
1da177e4 | 377 | static ssize_t |
f0d1bec9 | 378 | pipe_write(struct kiocb *iocb, struct iov_iter *from) |
1da177e4 | 379 | { |
ee0b3e67 | 380 | struct file *filp = iocb->ki_filp; |
de32ec4c | 381 | struct pipe_inode_info *pipe = filp->private_data; |
f0d1bec9 AV |
382 | ssize_t ret = 0; |
383 | int do_wakeup = 0; | |
384 | size_t total_len = iov_iter_count(from); | |
1da177e4 LT |
385 | ssize_t chars; |
386 | ||
1da177e4 LT |
387 | /* Null write succeeds. */ |
388 | if (unlikely(total_len == 0)) | |
389 | return 0; | |
390 | ||
ebec73f4 | 391 | __pipe_lock(pipe); |
1da177e4 | 392 | |
923f4f23 | 393 | if (!pipe->readers) { |
1da177e4 LT |
394 | send_sig(SIGPIPE, current, 0); |
395 | ret = -EPIPE; | |
396 | goto out; | |
397 | } | |
398 | ||
399 | /* We try to merge small writes */ | |
400 | chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */ | |
923f4f23 | 401 | if (pipe->nrbufs && chars != 0) { |
341b446b | 402 | int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) & |
35f3d14d | 403 | (pipe->buffers - 1); |
923f4f23 | 404 | struct pipe_buffer *buf = pipe->bufs + lastbuf; |
1da177e4 | 405 | int offset = buf->offset + buf->len; |
341b446b | 406 | |
01e7187b | 407 | if (pipe_buf_can_merge(buf) && offset + chars <= PAGE_SIZE) { |
fba597db | 408 | ret = pipe_buf_confirm(pipe, buf); |
6ae08069 | 409 | if (ret) |
5274f052 | 410 | goto out; |
f84d7519 | 411 | |
f0d1bec9 AV |
412 | ret = copy_page_from_iter(buf->page, offset, chars, from); |
413 | if (unlikely(ret < chars)) { | |
6ae08069 | 414 | ret = -EFAULT; |
1da177e4 | 415 | goto out; |
f6762b7a | 416 | } |
f0d1bec9 | 417 | do_wakeup = 1; |
6ae08069 | 418 | buf->len += ret; |
f0d1bec9 | 419 | if (!iov_iter_count(from)) |
1da177e4 LT |
420 | goto out; |
421 | } | |
422 | } | |
423 | ||
424 | for (;;) { | |
425 | int bufs; | |
341b446b | 426 | |
923f4f23 | 427 | if (!pipe->readers) { |
1da177e4 | 428 | send_sig(SIGPIPE, current, 0); |
341b446b IM |
429 | if (!ret) |
430 | ret = -EPIPE; | |
1da177e4 LT |
431 | break; |
432 | } | |
923f4f23 | 433 | bufs = pipe->nrbufs; |
35f3d14d JA |
434 | if (bufs < pipe->buffers) { |
435 | int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1); | |
923f4f23 IM |
436 | struct pipe_buffer *buf = pipe->bufs + newbuf; |
437 | struct page *page = pipe->tmp_page; | |
f0d1bec9 | 438 | int copied; |
1da177e4 LT |
439 | |
440 | if (!page) { | |
d86133bd | 441 | page = alloc_page(GFP_HIGHUSER | __GFP_ACCOUNT); |
1da177e4 LT |
442 | if (unlikely(!page)) { |
443 | ret = ret ? : -ENOMEM; | |
444 | break; | |
445 | } | |
923f4f23 | 446 | pipe->tmp_page = page; |
1da177e4 | 447 | } |
341b446b | 448 | /* Always wake up, even if the copy fails. Otherwise |
1da177e4 LT |
449 | * we lock up (O_NONBLOCK-)readers that sleep due to |
450 | * syscall merging. | |
451 | * FIXME! Is this really true? | |
452 | */ | |
453 | do_wakeup = 1; | |
f0d1bec9 AV |
454 | copied = copy_page_from_iter(page, 0, PAGE_SIZE, from); |
455 | if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) { | |
341b446b | 456 | if (!ret) |
f0d1bec9 | 457 | ret = -EFAULT; |
1da177e4 LT |
458 | break; |
459 | } | |
f0d1bec9 | 460 | ret += copied; |
1da177e4 LT |
461 | |
462 | /* Insert it into the buffer array */ | |
463 | buf->page = page; | |
464 | buf->ops = &anon_pipe_buf_ops; | |
465 | buf->offset = 0; | |
f0d1bec9 | 466 | buf->len = copied; |
9883035a LT |
467 | buf->flags = 0; |
468 | if (is_packetized(filp)) { | |
469 | buf->ops = &packet_pipe_buf_ops; | |
470 | buf->flags = PIPE_BUF_FLAG_PACKET; | |
471 | } | |
923f4f23 IM |
472 | pipe->nrbufs = ++bufs; |
473 | pipe->tmp_page = NULL; | |
1da177e4 | 474 | |
f0d1bec9 | 475 | if (!iov_iter_count(from)) |
1da177e4 LT |
476 | break; |
477 | } | |
35f3d14d | 478 | if (bufs < pipe->buffers) |
1da177e4 LT |
479 | continue; |
480 | if (filp->f_flags & O_NONBLOCK) { | |
341b446b IM |
481 | if (!ret) |
482 | ret = -EAGAIN; | |
1da177e4 LT |
483 | break; |
484 | } | |
485 | if (signal_pending(current)) { | |
341b446b IM |
486 | if (!ret) |
487 | ret = -ERESTARTSYS; | |
1da177e4 LT |
488 | break; |
489 | } | |
490 | if (do_wakeup) { | |
a9a08845 | 491 | wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM); |
923f4f23 | 492 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
1da177e4 LT |
493 | do_wakeup = 0; |
494 | } | |
923f4f23 IM |
495 | pipe->waiting_writers++; |
496 | pipe_wait(pipe); | |
497 | pipe->waiting_writers--; | |
1da177e4 LT |
498 | } |
499 | out: | |
ebec73f4 | 500 | __pipe_unlock(pipe); |
1da177e4 | 501 | if (do_wakeup) { |
a9a08845 | 502 | wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM); |
923f4f23 | 503 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
1da177e4 | 504 | } |
7e775f46 | 505 | if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) { |
c3b2da31 JB |
506 | int err = file_update_time(filp); |
507 | if (err) | |
508 | ret = err; | |
7e775f46 | 509 | sb_end_write(file_inode(filp)->i_sb); |
c3b2da31 | 510 | } |
1da177e4 LT |
511 | return ret; |
512 | } | |
513 | ||
d59d0b1b | 514 | static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
1da177e4 | 515 | { |
de32ec4c | 516 | struct pipe_inode_info *pipe = filp->private_data; |
1da177e4 LT |
517 | int count, buf, nrbufs; |
518 | ||
519 | switch (cmd) { | |
520 | case FIONREAD: | |
ebec73f4 | 521 | __pipe_lock(pipe); |
1da177e4 | 522 | count = 0; |
923f4f23 IM |
523 | buf = pipe->curbuf; |
524 | nrbufs = pipe->nrbufs; | |
1da177e4 | 525 | while (--nrbufs >= 0) { |
923f4f23 | 526 | count += pipe->bufs[buf].len; |
35f3d14d | 527 | buf = (buf+1) & (pipe->buffers - 1); |
1da177e4 | 528 | } |
ebec73f4 | 529 | __pipe_unlock(pipe); |
923f4f23 | 530 | |
1da177e4 LT |
531 | return put_user(count, (int __user *)arg); |
532 | default: | |
46ce341b | 533 | return -ENOIOCTLCMD; |
1da177e4 LT |
534 | } |
535 | } | |
536 | ||
dd67081b | 537 | /* No kernel lock held - fine */ |
a11e1d43 LT |
538 | static __poll_t |
539 | pipe_poll(struct file *filp, poll_table *wait) | |
dd67081b | 540 | { |
a11e1d43 | 541 | __poll_t mask; |
dd67081b | 542 | struct pipe_inode_info *pipe = filp->private_data; |
a11e1d43 LT |
543 | int nrbufs; |
544 | ||
545 | poll_wait(filp, &pipe->wait, wait); | |
1da177e4 LT |
546 | |
547 | /* Reading only -- no need for acquiring the semaphore. */ | |
a11e1d43 LT |
548 | nrbufs = pipe->nrbufs; |
549 | mask = 0; | |
1da177e4 | 550 | if (filp->f_mode & FMODE_READ) { |
a9a08845 | 551 | mask = (nrbufs > 0) ? EPOLLIN | EPOLLRDNORM : 0; |
923f4f23 | 552 | if (!pipe->writers && filp->f_version != pipe->w_counter) |
a9a08845 | 553 | mask |= EPOLLHUP; |
1da177e4 LT |
554 | } |
555 | ||
556 | if (filp->f_mode & FMODE_WRITE) { | |
a9a08845 | 557 | mask |= (nrbufs < pipe->buffers) ? EPOLLOUT | EPOLLWRNORM : 0; |
5e5d7a22 | 558 | /* |
a9a08845 | 559 | * Most Unices do not set EPOLLERR for FIFOs but on Linux they |
5e5d7a22 PE |
560 | * behave exactly like pipes for poll(). |
561 | */ | |
923f4f23 | 562 | if (!pipe->readers) |
a9a08845 | 563 | mask |= EPOLLERR; |
1da177e4 LT |
564 | } |
565 | ||
566 | return mask; | |
567 | } | |
568 | ||
b0d8d229 LT |
569 | static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe) |
570 | { | |
571 | int kill = 0; | |
572 | ||
573 | spin_lock(&inode->i_lock); | |
574 | if (!--pipe->files) { | |
575 | inode->i_pipe = NULL; | |
576 | kill = 1; | |
577 | } | |
578 | spin_unlock(&inode->i_lock); | |
579 | ||
580 | if (kill) | |
581 | free_pipe_info(pipe); | |
582 | } | |
583 | ||
1da177e4 | 584 | static int |
599a0ac1 | 585 | pipe_release(struct inode *inode, struct file *file) |
1da177e4 | 586 | { |
b0d8d229 | 587 | struct pipe_inode_info *pipe = file->private_data; |
923f4f23 | 588 | |
ebec73f4 | 589 | __pipe_lock(pipe); |
599a0ac1 AV |
590 | if (file->f_mode & FMODE_READ) |
591 | pipe->readers--; | |
592 | if (file->f_mode & FMODE_WRITE) | |
593 | pipe->writers--; | |
341b446b | 594 | |
ba5bb147 | 595 | if (pipe->readers || pipe->writers) { |
a9a08845 | 596 | wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM | EPOLLERR | EPOLLHUP); |
923f4f23 IM |
597 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
598 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); | |
1da177e4 | 599 | } |
ebec73f4 | 600 | __pipe_unlock(pipe); |
ba5bb147 | 601 | |
b0d8d229 | 602 | put_pipe_info(inode, pipe); |
1da177e4 LT |
603 | return 0; |
604 | } | |
605 | ||
606 | static int | |
599a0ac1 | 607 | pipe_fasync(int fd, struct file *filp, int on) |
1da177e4 | 608 | { |
de32ec4c | 609 | struct pipe_inode_info *pipe = filp->private_data; |
599a0ac1 | 610 | int retval = 0; |
1da177e4 | 611 | |
ebec73f4 | 612 | __pipe_lock(pipe); |
599a0ac1 AV |
613 | if (filp->f_mode & FMODE_READ) |
614 | retval = fasync_helper(fd, filp, on, &pipe->fasync_readers); | |
615 | if ((filp->f_mode & FMODE_WRITE) && retval >= 0) { | |
341b446b | 616 | retval = fasync_helper(fd, filp, on, &pipe->fasync_writers); |
599a0ac1 AV |
617 | if (retval < 0 && (filp->f_mode & FMODE_READ)) |
618 | /* this can happen only if on == T */ | |
e5bc49ba ON |
619 | fasync_helper(-1, filp, 0, &pipe->fasync_readers); |
620 | } | |
ebec73f4 | 621 | __pipe_unlock(pipe); |
60aa4924 | 622 | return retval; |
1da177e4 LT |
623 | } |
624 | ||
9c87bcf0 | 625 | static unsigned long account_pipe_buffers(struct user_struct *user, |
759c0114 WT |
626 | unsigned long old, unsigned long new) |
627 | { | |
9c87bcf0 | 628 | return atomic_long_add_return(new - old, &user->pipe_bufs); |
759c0114 WT |
629 | } |
630 | ||
9c87bcf0 | 631 | static bool too_many_pipe_buffers_soft(unsigned long user_bufs) |
759c0114 | 632 | { |
f7340761 EB |
633 | unsigned long soft_limit = READ_ONCE(pipe_user_pages_soft); |
634 | ||
635 | return soft_limit && user_bufs > soft_limit; | |
759c0114 WT |
636 | } |
637 | ||
9c87bcf0 | 638 | static bool too_many_pipe_buffers_hard(unsigned long user_bufs) |
759c0114 | 639 | { |
f7340761 EB |
640 | unsigned long hard_limit = READ_ONCE(pipe_user_pages_hard); |
641 | ||
642 | return hard_limit && user_bufs > hard_limit; | |
759c0114 WT |
643 | } |
644 | ||
85c2dd54 EB |
645 | static bool is_unprivileged_user(void) |
646 | { | |
647 | return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN); | |
648 | } | |
649 | ||
7bee130e | 650 | struct pipe_inode_info *alloc_pipe_info(void) |
3a326a2c | 651 | { |
923f4f23 | 652 | struct pipe_inode_info *pipe; |
09b4d199 MK |
653 | unsigned long pipe_bufs = PIPE_DEF_BUFFERS; |
654 | struct user_struct *user = get_current_user(); | |
9c87bcf0 | 655 | unsigned long user_bufs; |
f7340761 | 656 | unsigned int max_size = READ_ONCE(pipe_max_size); |
3a326a2c | 657 | |
d86133bd | 658 | pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT); |
09b4d199 MK |
659 | if (pipe == NULL) |
660 | goto out_free_uid; | |
661 | ||
f7340761 EB |
662 | if (pipe_bufs * PAGE_SIZE > max_size && !capable(CAP_SYS_RESOURCE)) |
663 | pipe_bufs = max_size >> PAGE_SHIFT; | |
086e774a | 664 | |
9c87bcf0 | 665 | user_bufs = account_pipe_buffers(user, 0, pipe_bufs); |
a005ca0e | 666 | |
85c2dd54 | 667 | if (too_many_pipe_buffers_soft(user_bufs) && is_unprivileged_user()) { |
9c87bcf0 | 668 | user_bufs = account_pipe_buffers(user, pipe_bufs, 1); |
a005ca0e | 669 | pipe_bufs = 1; |
09b4d199 | 670 | } |
759c0114 | 671 | |
85c2dd54 | 672 | if (too_many_pipe_buffers_hard(user_bufs) && is_unprivileged_user()) |
a005ca0e MK |
673 | goto out_revert_acct; |
674 | ||
675 | pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer), | |
676 | GFP_KERNEL_ACCOUNT); | |
677 | ||
09b4d199 MK |
678 | if (pipe->bufs) { |
679 | init_waitqueue_head(&pipe->wait); | |
680 | pipe->r_counter = pipe->w_counter = 1; | |
681 | pipe->buffers = pipe_bufs; | |
682 | pipe->user = user; | |
09b4d199 MK |
683 | mutex_init(&pipe->mutex); |
684 | return pipe; | |
3a326a2c IM |
685 | } |
686 | ||
a005ca0e | 687 | out_revert_acct: |
9c87bcf0 | 688 | (void) account_pipe_buffers(user, pipe_bufs, 0); |
09b4d199 MK |
689 | kfree(pipe); |
690 | out_free_uid: | |
691 | free_uid(user); | |
35f3d14d | 692 | return NULL; |
3a326a2c IM |
693 | } |
694 | ||
4b8a8f1e | 695 | void free_pipe_info(struct pipe_inode_info *pipe) |
1da177e4 LT |
696 | { |
697 | int i; | |
1da177e4 | 698 | |
9c87bcf0 | 699 | (void) account_pipe_buffers(pipe->user, pipe->buffers, 0); |
759c0114 | 700 | free_uid(pipe->user); |
35f3d14d | 701 | for (i = 0; i < pipe->buffers; i++) { |
923f4f23 | 702 | struct pipe_buffer *buf = pipe->bufs + i; |
1da177e4 | 703 | if (buf->ops) |
a779638c | 704 | pipe_buf_release(pipe, buf); |
1da177e4 | 705 | } |
923f4f23 IM |
706 | if (pipe->tmp_page) |
707 | __free_page(pipe->tmp_page); | |
35f3d14d | 708 | kfree(pipe->bufs); |
923f4f23 | 709 | kfree(pipe); |
1da177e4 LT |
710 | } |
711 | ||
fa3536cc | 712 | static struct vfsmount *pipe_mnt __read_mostly; |
341b446b | 713 | |
c23fbb6b ED |
714 | /* |
715 | * pipefs_dname() is called from d_path(). | |
716 | */ | |
717 | static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen) | |
718 | { | |
719 | return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]", | |
75c3cfa8 | 720 | d_inode(dentry)->i_ino); |
c23fbb6b ED |
721 | } |
722 | ||
3ba13d17 | 723 | static const struct dentry_operations pipefs_dentry_operations = { |
c23fbb6b | 724 | .d_dname = pipefs_dname, |
1da177e4 LT |
725 | }; |
726 | ||
727 | static struct inode * get_pipe_inode(void) | |
728 | { | |
a209dfc7 | 729 | struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb); |
923f4f23 | 730 | struct pipe_inode_info *pipe; |
1da177e4 LT |
731 | |
732 | if (!inode) | |
733 | goto fail_inode; | |
734 | ||
85fe4025 CH |
735 | inode->i_ino = get_next_ino(); |
736 | ||
7bee130e | 737 | pipe = alloc_pipe_info(); |
923f4f23 | 738 | if (!pipe) |
1da177e4 | 739 | goto fail_iput; |
3a326a2c | 740 | |
ba5bb147 AV |
741 | inode->i_pipe = pipe; |
742 | pipe->files = 2; | |
923f4f23 | 743 | pipe->readers = pipe->writers = 1; |
599a0ac1 | 744 | inode->i_fop = &pipefifo_fops; |
1da177e4 LT |
745 | |
746 | /* | |
747 | * Mark the inode dirty from the very beginning, | |
748 | * that way it will never be moved to the dirty | |
749 | * list because "mark_inode_dirty()" will think | |
750 | * that it already _is_ on the dirty list. | |
751 | */ | |
752 | inode->i_state = I_DIRTY; | |
753 | inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR; | |
da9592ed DH |
754 | inode->i_uid = current_fsuid(); |
755 | inode->i_gid = current_fsgid(); | |
078cd827 | 756 | inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); |
923f4f23 | 757 | |
1da177e4 LT |
758 | return inode; |
759 | ||
760 | fail_iput: | |
761 | iput(inode); | |
341b446b | 762 | |
1da177e4 LT |
763 | fail_inode: |
764 | return NULL; | |
765 | } | |
766 | ||
e4fad8e5 | 767 | int create_pipe_files(struct file **res, int flags) |
1da177e4 | 768 | { |
e4fad8e5 | 769 | struct inode *inode = get_pipe_inode(); |
d6cbd281 | 770 | struct file *f; |
1da177e4 | 771 | |
1da177e4 | 772 | if (!inode) |
e4fad8e5 | 773 | return -ENFILE; |
1da177e4 | 774 | |
152b6372 AV |
775 | f = alloc_file_pseudo(inode, pipe_mnt, "", |
776 | O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)), | |
777 | &pipefifo_fops); | |
e9bb1f9b | 778 | if (IS_ERR(f)) { |
152b6372 AV |
779 | free_pipe_info(inode->i_pipe); |
780 | iput(inode); | |
781 | return PTR_ERR(f); | |
e9bb1f9b | 782 | } |
341b446b | 783 | |
de32ec4c | 784 | f->private_data = inode->i_pipe; |
d6cbd281 | 785 | |
183266f2 AV |
786 | res[0] = alloc_file_clone(f, O_RDONLY | (flags & O_NONBLOCK), |
787 | &pipefifo_fops); | |
e9bb1f9b | 788 | if (IS_ERR(res[0])) { |
b10a4a9f AV |
789 | put_pipe_info(inode, inode->i_pipe); |
790 | fput(f); | |
791 | return PTR_ERR(res[0]); | |
e9bb1f9b | 792 | } |
de32ec4c | 793 | res[0]->private_data = inode->i_pipe; |
e4fad8e5 AV |
794 | res[1] = f; |
795 | return 0; | |
d6cbd281 AK |
796 | } |
797 | ||
5b249b1b | 798 | static int __do_pipe_flags(int *fd, struct file **files, int flags) |
d6cbd281 | 799 | { |
d6cbd281 AK |
800 | int error; |
801 | int fdw, fdr; | |
802 | ||
9883035a | 803 | if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT)) |
ed8cae8b UD |
804 | return -EINVAL; |
805 | ||
e4fad8e5 AV |
806 | error = create_pipe_files(files, flags); |
807 | if (error) | |
808 | return error; | |
d6cbd281 | 809 | |
ed8cae8b | 810 | error = get_unused_fd_flags(flags); |
d6cbd281 AK |
811 | if (error < 0) |
812 | goto err_read_pipe; | |
813 | fdr = error; | |
814 | ||
ed8cae8b | 815 | error = get_unused_fd_flags(flags); |
d6cbd281 AK |
816 | if (error < 0) |
817 | goto err_fdr; | |
818 | fdw = error; | |
819 | ||
157cf649 | 820 | audit_fd_pair(fdr, fdw); |
d6cbd281 AK |
821 | fd[0] = fdr; |
822 | fd[1] = fdw; | |
d6cbd281 AK |
823 | return 0; |
824 | ||
825 | err_fdr: | |
826 | put_unused_fd(fdr); | |
827 | err_read_pipe: | |
e4fad8e5 AV |
828 | fput(files[0]); |
829 | fput(files[1]); | |
d6cbd281 | 830 | return error; |
1da177e4 LT |
831 | } |
832 | ||
5b249b1b AV |
833 | int do_pipe_flags(int *fd, int flags) |
834 | { | |
835 | struct file *files[2]; | |
836 | int error = __do_pipe_flags(fd, files, flags); | |
837 | if (!error) { | |
838 | fd_install(fd[0], files[0]); | |
839 | fd_install(fd[1], files[1]); | |
840 | } | |
841 | return error; | |
842 | } | |
843 | ||
d35c7b0e UD |
844 | /* |
845 | * sys_pipe() is the normal C calling standard for creating | |
846 | * a pipe. It's not the way Unix traditionally does this, though. | |
847 | */ | |
0a216dd1 | 848 | static int do_pipe2(int __user *fildes, int flags) |
d35c7b0e | 849 | { |
5b249b1b | 850 | struct file *files[2]; |
d35c7b0e UD |
851 | int fd[2]; |
852 | int error; | |
853 | ||
5b249b1b | 854 | error = __do_pipe_flags(fd, files, flags); |
d35c7b0e | 855 | if (!error) { |
5b249b1b AV |
856 | if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) { |
857 | fput(files[0]); | |
858 | fput(files[1]); | |
859 | put_unused_fd(fd[0]); | |
860 | put_unused_fd(fd[1]); | |
d35c7b0e | 861 | error = -EFAULT; |
5b249b1b AV |
862 | } else { |
863 | fd_install(fd[0], files[0]); | |
864 | fd_install(fd[1], files[1]); | |
ba719bae | 865 | } |
d35c7b0e UD |
866 | } |
867 | return error; | |
868 | } | |
869 | ||
0a216dd1 DB |
870 | SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags) |
871 | { | |
872 | return do_pipe2(fildes, flags); | |
873 | } | |
874 | ||
2b664219 | 875 | SYSCALL_DEFINE1(pipe, int __user *, fildes) |
ed8cae8b | 876 | { |
0a216dd1 | 877 | return do_pipe2(fildes, 0); |
ed8cae8b UD |
878 | } |
879 | ||
fc7478a2 | 880 | static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt) |
f776c738 AV |
881 | { |
882 | int cur = *cnt; | |
883 | ||
884 | while (cur == *cnt) { | |
fc7478a2 | 885 | pipe_wait(pipe); |
f776c738 AV |
886 | if (signal_pending(current)) |
887 | break; | |
888 | } | |
889 | return cur == *cnt ? -ERESTARTSYS : 0; | |
890 | } | |
891 | ||
fc7478a2 | 892 | static void wake_up_partner(struct pipe_inode_info *pipe) |
f776c738 | 893 | { |
fc7478a2 | 894 | wake_up_interruptible(&pipe->wait); |
f776c738 AV |
895 | } |
896 | ||
897 | static int fifo_open(struct inode *inode, struct file *filp) | |
898 | { | |
899 | struct pipe_inode_info *pipe; | |
599a0ac1 | 900 | bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC; |
f776c738 AV |
901 | int ret; |
902 | ||
ba5bb147 AV |
903 | filp->f_version = 0; |
904 | ||
905 | spin_lock(&inode->i_lock); | |
906 | if (inode->i_pipe) { | |
907 | pipe = inode->i_pipe; | |
908 | pipe->files++; | |
909 | spin_unlock(&inode->i_lock); | |
910 | } else { | |
911 | spin_unlock(&inode->i_lock); | |
7bee130e | 912 | pipe = alloc_pipe_info(); |
f776c738 | 913 | if (!pipe) |
ba5bb147 AV |
914 | return -ENOMEM; |
915 | pipe->files = 1; | |
916 | spin_lock(&inode->i_lock); | |
917 | if (unlikely(inode->i_pipe)) { | |
918 | inode->i_pipe->files++; | |
919 | spin_unlock(&inode->i_lock); | |
4b8a8f1e | 920 | free_pipe_info(pipe); |
ba5bb147 AV |
921 | pipe = inode->i_pipe; |
922 | } else { | |
923 | inode->i_pipe = pipe; | |
924 | spin_unlock(&inode->i_lock); | |
925 | } | |
f776c738 | 926 | } |
de32ec4c | 927 | filp->private_data = pipe; |
ba5bb147 AV |
928 | /* OK, we have a pipe and it's pinned down */ |
929 | ||
ebec73f4 | 930 | __pipe_lock(pipe); |
f776c738 AV |
931 | |
932 | /* We can only do regular read/write on fifos */ | |
933 | filp->f_mode &= (FMODE_READ | FMODE_WRITE); | |
934 | ||
935 | switch (filp->f_mode) { | |
936 | case FMODE_READ: | |
937 | /* | |
938 | * O_RDONLY | |
939 | * POSIX.1 says that O_NONBLOCK means return with the FIFO | |
940 | * opened, even when there is no process writing the FIFO. | |
941 | */ | |
f776c738 AV |
942 | pipe->r_counter++; |
943 | if (pipe->readers++ == 0) | |
fc7478a2 | 944 | wake_up_partner(pipe); |
f776c738 | 945 | |
599a0ac1 | 946 | if (!is_pipe && !pipe->writers) { |
f776c738 | 947 | if ((filp->f_flags & O_NONBLOCK)) { |
a9a08845 | 948 | /* suppress EPOLLHUP until we have |
f776c738 AV |
949 | * seen a writer */ |
950 | filp->f_version = pipe->w_counter; | |
951 | } else { | |
fc7478a2 | 952 | if (wait_for_partner(pipe, &pipe->w_counter)) |
f776c738 AV |
953 | goto err_rd; |
954 | } | |
955 | } | |
956 | break; | |
957 | ||
958 | case FMODE_WRITE: | |
959 | /* | |
960 | * O_WRONLY | |
961 | * POSIX.1 says that O_NONBLOCK means return -1 with | |
962 | * errno=ENXIO when there is no process reading the FIFO. | |
963 | */ | |
964 | ret = -ENXIO; | |
599a0ac1 | 965 | if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers) |
f776c738 AV |
966 | goto err; |
967 | ||
f776c738 AV |
968 | pipe->w_counter++; |
969 | if (!pipe->writers++) | |
fc7478a2 | 970 | wake_up_partner(pipe); |
f776c738 | 971 | |
599a0ac1 | 972 | if (!is_pipe && !pipe->readers) { |
fc7478a2 | 973 | if (wait_for_partner(pipe, &pipe->r_counter)) |
f776c738 AV |
974 | goto err_wr; |
975 | } | |
976 | break; | |
977 | ||
978 | case FMODE_READ | FMODE_WRITE: | |
979 | /* | |
980 | * O_RDWR | |
981 | * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set. | |
982 | * This implementation will NEVER block on a O_RDWR open, since | |
983 | * the process can at least talk to itself. | |
984 | */ | |
f776c738 AV |
985 | |
986 | pipe->readers++; | |
987 | pipe->writers++; | |
988 | pipe->r_counter++; | |
989 | pipe->w_counter++; | |
990 | if (pipe->readers == 1 || pipe->writers == 1) | |
fc7478a2 | 991 | wake_up_partner(pipe); |
f776c738 AV |
992 | break; |
993 | ||
994 | default: | |
995 | ret = -EINVAL; | |
996 | goto err; | |
997 | } | |
998 | ||
999 | /* Ok! */ | |
ebec73f4 | 1000 | __pipe_unlock(pipe); |
f776c738 AV |
1001 | return 0; |
1002 | ||
1003 | err_rd: | |
1004 | if (!--pipe->readers) | |
1005 | wake_up_interruptible(&pipe->wait); | |
1006 | ret = -ERESTARTSYS; | |
1007 | goto err; | |
1008 | ||
1009 | err_wr: | |
1010 | if (!--pipe->writers) | |
1011 | wake_up_interruptible(&pipe->wait); | |
1012 | ret = -ERESTARTSYS; | |
1013 | goto err; | |
1014 | ||
1015 | err: | |
ebec73f4 | 1016 | __pipe_unlock(pipe); |
b0d8d229 LT |
1017 | |
1018 | put_pipe_info(inode, pipe); | |
f776c738 AV |
1019 | return ret; |
1020 | } | |
1021 | ||
599a0ac1 AV |
1022 | const struct file_operations pipefifo_fops = { |
1023 | .open = fifo_open, | |
1024 | .llseek = no_llseek, | |
fb9096a3 | 1025 | .read_iter = pipe_read, |
f0d1bec9 | 1026 | .write_iter = pipe_write, |
a11e1d43 | 1027 | .poll = pipe_poll, |
599a0ac1 AV |
1028 | .unlocked_ioctl = pipe_ioctl, |
1029 | .release = pipe_release, | |
1030 | .fasync = pipe_fasync, | |
f776c738 AV |
1031 | }; |
1032 | ||
f491bd71 MK |
1033 | /* |
1034 | * Currently we rely on the pipe array holding a power-of-2 number | |
d3f14c48 | 1035 | * of pages. Returns 0 on error. |
f491bd71 | 1036 | */ |
96e99be4 | 1037 | unsigned int round_pipe_size(unsigned long size) |
f491bd71 | 1038 | { |
c4fed5a9 | 1039 | if (size > (1U << 31)) |
96e99be4 EB |
1040 | return 0; |
1041 | ||
4c2e4bef EB |
1042 | /* Minimum pipe size, as required by POSIX */ |
1043 | if (size < PAGE_SIZE) | |
c4fed5a9 | 1044 | return PAGE_SIZE; |
d3f14c48 | 1045 | |
c4fed5a9 | 1046 | return roundup_pow_of_two(size); |
f491bd71 MK |
1047 | } |
1048 | ||
35f3d14d JA |
1049 | /* |
1050 | * Allocate a new array of pipe buffers and copy the info over. Returns the | |
1051 | * pipe size if successful, or return -ERROR on error. | |
1052 | */ | |
d37d4166 | 1053 | static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg) |
35f3d14d JA |
1054 | { |
1055 | struct pipe_buffer *bufs; | |
d37d4166 | 1056 | unsigned int size, nr_pages; |
9c87bcf0 | 1057 | unsigned long user_bufs; |
b0b91d18 | 1058 | long ret = 0; |
d37d4166 MK |
1059 | |
1060 | size = round_pipe_size(arg); | |
1061 | nr_pages = size >> PAGE_SHIFT; | |
1062 | ||
1063 | if (!nr_pages) | |
1064 | return -EINVAL; | |
1065 | ||
b0b91d18 MK |
1066 | /* |
1067 | * If trying to increase the pipe capacity, check that an | |
1068 | * unprivileged user is not trying to exceed various limits | |
1069 | * (soft limit check here, hard limit check just below). | |
1070 | * Decreasing the pipe capacity is always permitted, even | |
1071 | * if the user is currently over a limit. | |
1072 | */ | |
1073 | if (nr_pages > pipe->buffers && | |
1074 | size > pipe_max_size && !capable(CAP_SYS_RESOURCE)) | |
d37d4166 MK |
1075 | return -EPERM; |
1076 | ||
9c87bcf0 | 1077 | user_bufs = account_pipe_buffers(pipe->user, pipe->buffers, nr_pages); |
b0b91d18 MK |
1078 | |
1079 | if (nr_pages > pipe->buffers && | |
9c87bcf0 MK |
1080 | (too_many_pipe_buffers_hard(user_bufs) || |
1081 | too_many_pipe_buffers_soft(user_bufs)) && | |
85c2dd54 | 1082 | is_unprivileged_user()) { |
b0b91d18 MK |
1083 | ret = -EPERM; |
1084 | goto out_revert_acct; | |
1085 | } | |
35f3d14d | 1086 | |
35f3d14d JA |
1087 | /* |
1088 | * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't | |
1089 | * expect a lot of shrink+grow operations, just free and allocate | |
1090 | * again like we would do for growing. If the pipe currently | |
1091 | * contains more buffers than arg, then return busy. | |
1092 | */ | |
b0b91d18 MK |
1093 | if (nr_pages < pipe->nrbufs) { |
1094 | ret = -EBUSY; | |
1095 | goto out_revert_acct; | |
1096 | } | |
35f3d14d | 1097 | |
d86133bd VD |
1098 | bufs = kcalloc(nr_pages, sizeof(*bufs), |
1099 | GFP_KERNEL_ACCOUNT | __GFP_NOWARN); | |
b0b91d18 MK |
1100 | if (unlikely(!bufs)) { |
1101 | ret = -ENOMEM; | |
1102 | goto out_revert_acct; | |
1103 | } | |
35f3d14d JA |
1104 | |
1105 | /* | |
1106 | * The pipe array wraps around, so just start the new one at zero | |
1107 | * and adjust the indexes. | |
1108 | */ | |
1109 | if (pipe->nrbufs) { | |
1d862f41 MS |
1110 | unsigned int tail; |
1111 | unsigned int head; | |
35f3d14d | 1112 | |
1d862f41 MS |
1113 | tail = pipe->curbuf + pipe->nrbufs; |
1114 | if (tail < pipe->buffers) | |
1115 | tail = 0; | |
1116 | else | |
1117 | tail &= (pipe->buffers - 1); | |
1118 | ||
1119 | head = pipe->nrbufs - tail; | |
35f3d14d JA |
1120 | if (head) |
1121 | memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer)); | |
1122 | if (tail) | |
1d862f41 | 1123 | memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer)); |
35f3d14d JA |
1124 | } |
1125 | ||
1126 | pipe->curbuf = 0; | |
1127 | kfree(pipe->bufs); | |
1128 | pipe->bufs = bufs; | |
b9598db3 JA |
1129 | pipe->buffers = nr_pages; |
1130 | return nr_pages * PAGE_SIZE; | |
b0b91d18 MK |
1131 | |
1132 | out_revert_acct: | |
9c87bcf0 | 1133 | (void) account_pipe_buffers(pipe->user, nr_pages, pipe->buffers); |
b0b91d18 | 1134 | return ret; |
35f3d14d JA |
1135 | } |
1136 | ||
72083646 LT |
1137 | /* |
1138 | * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same | |
1139 | * location, so checking ->i_pipe is not enough to verify that this is a | |
1140 | * pipe. | |
1141 | */ | |
1142 | struct pipe_inode_info *get_pipe_info(struct file *file) | |
1143 | { | |
de32ec4c | 1144 | return file->f_op == &pipefifo_fops ? file->private_data : NULL; |
72083646 LT |
1145 | } |
1146 | ||
35f3d14d JA |
1147 | long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg) |
1148 | { | |
1149 | struct pipe_inode_info *pipe; | |
1150 | long ret; | |
1151 | ||
c66fb347 | 1152 | pipe = get_pipe_info(file); |
35f3d14d JA |
1153 | if (!pipe) |
1154 | return -EBADF; | |
1155 | ||
ebec73f4 | 1156 | __pipe_lock(pipe); |
35f3d14d JA |
1157 | |
1158 | switch (cmd) { | |
d37d4166 MK |
1159 | case F_SETPIPE_SZ: |
1160 | ret = pipe_set_size(pipe, arg); | |
35f3d14d JA |
1161 | break; |
1162 | case F_GETPIPE_SZ: | |
b9598db3 | 1163 | ret = pipe->buffers * PAGE_SIZE; |
35f3d14d JA |
1164 | break; |
1165 | default: | |
1166 | ret = -EINVAL; | |
1167 | break; | |
1168 | } | |
1169 | ||
ebec73f4 | 1170 | __pipe_unlock(pipe); |
35f3d14d JA |
1171 | return ret; |
1172 | } | |
1173 | ||
ff0c7d15 NP |
1174 | static const struct super_operations pipefs_ops = { |
1175 | .destroy_inode = free_inode_nonrcu, | |
d70ef97b | 1176 | .statfs = simple_statfs, |
ff0c7d15 NP |
1177 | }; |
1178 | ||
1da177e4 LT |
1179 | /* |
1180 | * pipefs should _never_ be mounted by userland - too much of security hassle, | |
1181 | * no real gain from having the whole whorehouse mounted. So we don't need | |
1182 | * any operations on the root directory. However, we need a non-trivial | |
1183 | * d_name - pipe: will go nicely and kill the special-casing in procfs. | |
1184 | */ | |
51139ada AV |
1185 | static struct dentry *pipefs_mount(struct file_system_type *fs_type, |
1186 | int flags, const char *dev_name, void *data) | |
1da177e4 | 1187 | { |
c74a1cbb AV |
1188 | return mount_pseudo(fs_type, "pipe:", &pipefs_ops, |
1189 | &pipefs_dentry_operations, PIPEFS_MAGIC); | |
1da177e4 LT |
1190 | } |
1191 | ||
1192 | static struct file_system_type pipe_fs_type = { | |
1193 | .name = "pipefs", | |
51139ada | 1194 | .mount = pipefs_mount, |
1da177e4 LT |
1195 | .kill_sb = kill_anon_super, |
1196 | }; | |
1197 | ||
1198 | static int __init init_pipe_fs(void) | |
1199 | { | |
1200 | int err = register_filesystem(&pipe_fs_type); | |
341b446b | 1201 | |
1da177e4 LT |
1202 | if (!err) { |
1203 | pipe_mnt = kern_mount(&pipe_fs_type); | |
1204 | if (IS_ERR(pipe_mnt)) { | |
1205 | err = PTR_ERR(pipe_mnt); | |
1206 | unregister_filesystem(&pipe_fs_type); | |
1207 | } | |
1208 | } | |
1209 | return err; | |
1210 | } | |
1211 | ||
1da177e4 | 1212 | fs_initcall(init_pipe_fs); |