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