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