]>
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> | |
14 | #include <linux/mount.h> | |
15 | #include <linux/pipe_fs_i.h> | |
16 | #include <linux/uio.h> | |
17 | #include <linux/highmem.h> | |
5274f052 | 18 | #include <linux/pagemap.h> |
db349509 | 19 | #include <linux/audit.h> |
1da177e4 LT |
20 | |
21 | #include <asm/uaccess.h> | |
22 | #include <asm/ioctls.h> | |
23 | ||
24 | /* | |
25 | * We use a start+len construction, which provides full use of the | |
26 | * allocated memory. | |
27 | * -- Florian Coosmann (FGC) | |
28 | * | |
29 | * Reads with count = 0 should always return 0. | |
30 | * -- Julian Bradfield 1999-06-07. | |
31 | * | |
32 | * FIFOs and Pipes now generate SIGIO for both readers and writers. | |
33 | * -- Jeremy Elson <[email protected]> 2001-08-16 | |
34 | * | |
35 | * pipe_read & write cleanup | |
36 | * -- Manfred Spraul <[email protected]> 2002-05-09 | |
37 | */ | |
38 | ||
39 | /* Drop the inode semaphore and wait for a pipe event, atomically */ | |
3a326a2c | 40 | void pipe_wait(struct pipe_inode_info *pipe) |
1da177e4 LT |
41 | { |
42 | DEFINE_WAIT(wait); | |
43 | ||
d79fc0fc IM |
44 | /* |
45 | * Pipes are system-local resources, so sleeping on them | |
46 | * is considered a noninteractive wait: | |
47 | */ | |
341b446b IM |
48 | prepare_to_wait(&pipe->wait, &wait, |
49 | TASK_INTERRUPTIBLE | TASK_NONINTERACTIVE); | |
3a326a2c IM |
50 | if (pipe->inode) |
51 | mutex_unlock(&pipe->inode->i_mutex); | |
1da177e4 | 52 | schedule(); |
3a326a2c IM |
53 | finish_wait(&pipe->wait, &wait); |
54 | if (pipe->inode) | |
55 | mutex_lock(&pipe->inode->i_mutex); | |
1da177e4 LT |
56 | } |
57 | ||
858119e1 | 58 | static int |
f6762b7a JA |
59 | pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len, |
60 | int atomic) | |
1da177e4 LT |
61 | { |
62 | unsigned long copy; | |
63 | ||
64 | while (len > 0) { | |
65 | while (!iov->iov_len) | |
66 | iov++; | |
67 | copy = min_t(unsigned long, len, iov->iov_len); | |
68 | ||
f6762b7a JA |
69 | if (atomic) { |
70 | if (__copy_from_user_inatomic(to, iov->iov_base, copy)) | |
71 | return -EFAULT; | |
72 | } else { | |
73 | if (copy_from_user(to, iov->iov_base, copy)) | |
74 | return -EFAULT; | |
75 | } | |
1da177e4 LT |
76 | to += copy; |
77 | len -= copy; | |
78 | iov->iov_base += copy; | |
79 | iov->iov_len -= copy; | |
80 | } | |
81 | return 0; | |
82 | } | |
83 | ||
858119e1 | 84 | static int |
f6762b7a JA |
85 | pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len, |
86 | int atomic) | |
1da177e4 LT |
87 | { |
88 | unsigned long copy; | |
89 | ||
90 | while (len > 0) { | |
91 | while (!iov->iov_len) | |
92 | iov++; | |
93 | copy = min_t(unsigned long, len, iov->iov_len); | |
94 | ||
f6762b7a JA |
95 | if (atomic) { |
96 | if (__copy_to_user_inatomic(iov->iov_base, from, copy)) | |
97 | return -EFAULT; | |
98 | } else { | |
99 | if (copy_to_user(iov->iov_base, from, copy)) | |
100 | return -EFAULT; | |
101 | } | |
1da177e4 LT |
102 | from += copy; |
103 | len -= copy; | |
104 | iov->iov_base += copy; | |
105 | iov->iov_len -= copy; | |
106 | } | |
107 | return 0; | |
108 | } | |
109 | ||
f6762b7a JA |
110 | /* |
111 | * Attempt to pre-fault in the user memory, so we can use atomic copies. | |
112 | * Returns the number of bytes not faulted in. | |
113 | */ | |
114 | static int iov_fault_in_pages_write(struct iovec *iov, unsigned long len) | |
115 | { | |
116 | while (!iov->iov_len) | |
117 | iov++; | |
118 | ||
119 | while (len > 0) { | |
120 | unsigned long this_len; | |
121 | ||
122 | this_len = min_t(unsigned long, len, iov->iov_len); | |
123 | if (fault_in_pages_writeable(iov->iov_base, this_len)) | |
124 | break; | |
125 | ||
126 | len -= this_len; | |
127 | iov++; | |
128 | } | |
129 | ||
130 | return len; | |
131 | } | |
132 | ||
133 | /* | |
134 | * Pre-fault in the user memory, so we can use atomic copies. | |
135 | */ | |
136 | static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len) | |
137 | { | |
138 | while (!iov->iov_len) | |
139 | iov++; | |
140 | ||
141 | while (len > 0) { | |
142 | unsigned long this_len; | |
143 | ||
144 | this_len = min_t(unsigned long, len, iov->iov_len); | |
145 | fault_in_pages_readable(iov->iov_base, this_len); | |
146 | len -= this_len; | |
147 | iov++; | |
148 | } | |
149 | } | |
150 | ||
341b446b IM |
151 | static void anon_pipe_buf_release(struct pipe_inode_info *pipe, |
152 | struct pipe_buffer *buf) | |
1da177e4 LT |
153 | { |
154 | struct page *page = buf->page; | |
155 | ||
5274f052 JA |
156 | /* |
157 | * If nobody else uses this page, and we don't already have a | |
158 | * temporary page, let's keep track of it as a one-deep | |
341b446b | 159 | * allocation cache. (Otherwise just release our reference to it) |
5274f052 | 160 | */ |
341b446b | 161 | if (page_count(page) == 1 && !pipe->tmp_page) |
923f4f23 | 162 | pipe->tmp_page = page; |
341b446b IM |
163 | else |
164 | page_cache_release(page); | |
1da177e4 LT |
165 | } |
166 | ||
f84d7519 | 167 | void *generic_pipe_buf_map(struct pipe_inode_info *pipe, |
f6762b7a | 168 | struct pipe_buffer *buf, int atomic) |
1da177e4 | 169 | { |
f6762b7a JA |
170 | if (atomic) { |
171 | buf->flags |= PIPE_BUF_FLAG_ATOMIC; | |
172 | return kmap_atomic(buf->page, KM_USER0); | |
173 | } | |
174 | ||
1da177e4 LT |
175 | return kmap(buf->page); |
176 | } | |
177 | ||
f84d7519 | 178 | void generic_pipe_buf_unmap(struct pipe_inode_info *pipe, |
f6762b7a | 179 | struct pipe_buffer *buf, void *map_data) |
1da177e4 | 180 | { |
f6762b7a JA |
181 | if (buf->flags & PIPE_BUF_FLAG_ATOMIC) { |
182 | buf->flags &= ~PIPE_BUF_FLAG_ATOMIC; | |
183 | kunmap_atomic(map_data, KM_USER0); | |
184 | } else | |
185 | kunmap(buf->page); | |
1da177e4 LT |
186 | } |
187 | ||
330ab716 JA |
188 | int generic_pipe_buf_steal(struct pipe_inode_info *pipe, |
189 | struct pipe_buffer *buf) | |
5abc97aa | 190 | { |
46e678c9 JA |
191 | struct page *page = buf->page; |
192 | ||
193 | if (page_count(page) == 1) { | |
46e678c9 JA |
194 | lock_page(page); |
195 | return 0; | |
196 | } | |
197 | ||
198 | return 1; | |
5abc97aa JA |
199 | } |
200 | ||
f84d7519 | 201 | void generic_pipe_buf_get(struct pipe_inode_info *info, struct pipe_buffer *buf) |
70524490 JA |
202 | { |
203 | page_cache_get(buf->page); | |
204 | } | |
205 | ||
f84d7519 JA |
206 | int generic_pipe_buf_pin(struct pipe_inode_info *info, struct pipe_buffer *buf) |
207 | { | |
208 | return 0; | |
209 | } | |
210 | ||
d4c3cca9 | 211 | static const struct pipe_buf_operations anon_pipe_buf_ops = { |
1da177e4 | 212 | .can_merge = 1, |
f84d7519 JA |
213 | .map = generic_pipe_buf_map, |
214 | .unmap = generic_pipe_buf_unmap, | |
215 | .pin = generic_pipe_buf_pin, | |
1da177e4 | 216 | .release = anon_pipe_buf_release, |
330ab716 | 217 | .steal = generic_pipe_buf_steal, |
f84d7519 | 218 | .get = generic_pipe_buf_get, |
1da177e4 LT |
219 | }; |
220 | ||
221 | static ssize_t | |
ee0b3e67 BP |
222 | pipe_read(struct kiocb *iocb, const struct iovec *_iov, |
223 | unsigned long nr_segs, loff_t pos) | |
1da177e4 | 224 | { |
ee0b3e67 | 225 | struct file *filp = iocb->ki_filp; |
0f7fc9e4 | 226 | struct inode *inode = filp->f_path.dentry->d_inode; |
923f4f23 | 227 | struct pipe_inode_info *pipe; |
1da177e4 LT |
228 | int do_wakeup; |
229 | ssize_t ret; | |
230 | struct iovec *iov = (struct iovec *)_iov; | |
231 | size_t total_len; | |
232 | ||
233 | total_len = iov_length(iov, nr_segs); | |
234 | /* Null read succeeds. */ | |
235 | if (unlikely(total_len == 0)) | |
236 | return 0; | |
237 | ||
238 | do_wakeup = 0; | |
239 | ret = 0; | |
9aeedfc4 | 240 | mutex_lock(&inode->i_mutex); |
923f4f23 | 241 | pipe = inode->i_pipe; |
1da177e4 | 242 | for (;;) { |
923f4f23 | 243 | int bufs = pipe->nrbufs; |
1da177e4 | 244 | if (bufs) { |
923f4f23 IM |
245 | int curbuf = pipe->curbuf; |
246 | struct pipe_buffer *buf = pipe->bufs + curbuf; | |
d4c3cca9 | 247 | const struct pipe_buf_operations *ops = buf->ops; |
1da177e4 LT |
248 | void *addr; |
249 | size_t chars = buf->len; | |
f6762b7a | 250 | int error, atomic; |
1da177e4 LT |
251 | |
252 | if (chars > total_len) | |
253 | chars = total_len; | |
254 | ||
f84d7519 JA |
255 | error = ops->pin(pipe, buf); |
256 | if (error) { | |
5274f052 | 257 | if (!ret) |
f84d7519 | 258 | error = ret; |
5274f052 JA |
259 | break; |
260 | } | |
f84d7519 | 261 | |
f6762b7a JA |
262 | atomic = !iov_fault_in_pages_write(iov, chars); |
263 | redo: | |
264 | addr = ops->map(pipe, buf, atomic); | |
265 | error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars, atomic); | |
266 | ops->unmap(pipe, buf, addr); | |
1da177e4 | 267 | if (unlikely(error)) { |
f6762b7a JA |
268 | /* |
269 | * Just retry with the slow path if we failed. | |
270 | */ | |
271 | if (atomic) { | |
272 | atomic = 0; | |
273 | goto redo; | |
274 | } | |
341b446b | 275 | if (!ret) |
f6762b7a | 276 | ret = error; |
1da177e4 LT |
277 | break; |
278 | } | |
279 | ret += chars; | |
280 | buf->offset += chars; | |
281 | buf->len -= chars; | |
282 | if (!buf->len) { | |
283 | buf->ops = NULL; | |
923f4f23 | 284 | ops->release(pipe, buf); |
1da177e4 | 285 | curbuf = (curbuf + 1) & (PIPE_BUFFERS-1); |
923f4f23 IM |
286 | pipe->curbuf = curbuf; |
287 | pipe->nrbufs = --bufs; | |
1da177e4 LT |
288 | do_wakeup = 1; |
289 | } | |
290 | total_len -= chars; | |
291 | if (!total_len) | |
292 | break; /* common path: read succeeded */ | |
293 | } | |
294 | if (bufs) /* More to do? */ | |
295 | continue; | |
923f4f23 | 296 | if (!pipe->writers) |
1da177e4 | 297 | break; |
923f4f23 | 298 | if (!pipe->waiting_writers) { |
1da177e4 LT |
299 | /* syscall merging: Usually we must not sleep |
300 | * if O_NONBLOCK is set, or if we got some data. | |
301 | * But if a writer sleeps in kernel space, then | |
302 | * we can wait for that data without violating POSIX. | |
303 | */ | |
304 | if (ret) | |
305 | break; | |
306 | if (filp->f_flags & O_NONBLOCK) { | |
307 | ret = -EAGAIN; | |
308 | break; | |
309 | } | |
310 | } | |
311 | if (signal_pending(current)) { | |
341b446b IM |
312 | if (!ret) |
313 | ret = -ERESTARTSYS; | |
1da177e4 LT |
314 | break; |
315 | } | |
316 | if (do_wakeup) { | |
923f4f23 IM |
317 | wake_up_interruptible_sync(&pipe->wait); |
318 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); | |
1da177e4 | 319 | } |
923f4f23 | 320 | pipe_wait(pipe); |
1da177e4 | 321 | } |
9aeedfc4 | 322 | mutex_unlock(&inode->i_mutex); |
341b446b IM |
323 | |
324 | /* Signal writers asynchronously that there is more room. */ | |
1da177e4 | 325 | if (do_wakeup) { |
923f4f23 IM |
326 | wake_up_interruptible(&pipe->wait); |
327 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); | |
1da177e4 LT |
328 | } |
329 | if (ret > 0) | |
330 | file_accessed(filp); | |
331 | return ret; | |
332 | } | |
333 | ||
334 | static ssize_t | |
ee0b3e67 BP |
335 | pipe_write(struct kiocb *iocb, const struct iovec *_iov, |
336 | unsigned long nr_segs, loff_t ppos) | |
1da177e4 | 337 | { |
ee0b3e67 | 338 | struct file *filp = iocb->ki_filp; |
0f7fc9e4 | 339 | struct inode *inode = filp->f_path.dentry->d_inode; |
923f4f23 | 340 | struct pipe_inode_info *pipe; |
1da177e4 LT |
341 | ssize_t ret; |
342 | int do_wakeup; | |
343 | struct iovec *iov = (struct iovec *)_iov; | |
344 | size_t total_len; | |
345 | ssize_t chars; | |
346 | ||
347 | total_len = iov_length(iov, nr_segs); | |
348 | /* Null write succeeds. */ | |
349 | if (unlikely(total_len == 0)) | |
350 | return 0; | |
351 | ||
352 | do_wakeup = 0; | |
353 | ret = 0; | |
9aeedfc4 | 354 | mutex_lock(&inode->i_mutex); |
923f4f23 | 355 | pipe = inode->i_pipe; |
1da177e4 | 356 | |
923f4f23 | 357 | if (!pipe->readers) { |
1da177e4 LT |
358 | send_sig(SIGPIPE, current, 0); |
359 | ret = -EPIPE; | |
360 | goto out; | |
361 | } | |
362 | ||
363 | /* We try to merge small writes */ | |
364 | chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */ | |
923f4f23 | 365 | if (pipe->nrbufs && chars != 0) { |
341b446b IM |
366 | int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) & |
367 | (PIPE_BUFFERS-1); | |
923f4f23 | 368 | struct pipe_buffer *buf = pipe->bufs + lastbuf; |
d4c3cca9 | 369 | const struct pipe_buf_operations *ops = buf->ops; |
1da177e4 | 370 | int offset = buf->offset + buf->len; |
341b446b | 371 | |
1da177e4 | 372 | if (ops->can_merge && offset + chars <= PAGE_SIZE) { |
f6762b7a | 373 | int error, atomic = 1; |
5274f052 | 374 | void *addr; |
5274f052 | 375 | |
f84d7519 JA |
376 | error = ops->pin(pipe, buf); |
377 | if (error) | |
5274f052 | 378 | goto out; |
f84d7519 | 379 | |
f6762b7a JA |
380 | iov_fault_in_pages_read(iov, chars); |
381 | redo1: | |
382 | addr = ops->map(pipe, buf, atomic); | |
5274f052 | 383 | error = pipe_iov_copy_from_user(offset + addr, iov, |
f6762b7a JA |
384 | chars, atomic); |
385 | ops->unmap(pipe, buf, addr); | |
1da177e4 LT |
386 | ret = error; |
387 | do_wakeup = 1; | |
f6762b7a JA |
388 | if (error) { |
389 | if (atomic) { | |
390 | atomic = 0; | |
391 | goto redo1; | |
392 | } | |
1da177e4 | 393 | goto out; |
f6762b7a | 394 | } |
1da177e4 LT |
395 | buf->len += chars; |
396 | total_len -= chars; | |
397 | ret = chars; | |
398 | if (!total_len) | |
399 | goto out; | |
400 | } | |
401 | } | |
402 | ||
403 | for (;;) { | |
404 | int bufs; | |
341b446b | 405 | |
923f4f23 | 406 | if (!pipe->readers) { |
1da177e4 | 407 | send_sig(SIGPIPE, current, 0); |
341b446b IM |
408 | if (!ret) |
409 | ret = -EPIPE; | |
1da177e4 LT |
410 | break; |
411 | } | |
923f4f23 | 412 | bufs = pipe->nrbufs; |
1da177e4 | 413 | if (bufs < PIPE_BUFFERS) { |
923f4f23 IM |
414 | int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1); |
415 | struct pipe_buffer *buf = pipe->bufs + newbuf; | |
416 | struct page *page = pipe->tmp_page; | |
f6762b7a JA |
417 | char *src; |
418 | int error, atomic = 1; | |
1da177e4 LT |
419 | |
420 | if (!page) { | |
421 | page = alloc_page(GFP_HIGHUSER); | |
422 | if (unlikely(!page)) { | |
423 | ret = ret ? : -ENOMEM; | |
424 | break; | |
425 | } | |
923f4f23 | 426 | pipe->tmp_page = page; |
1da177e4 | 427 | } |
341b446b | 428 | /* Always wake up, even if the copy fails. Otherwise |
1da177e4 LT |
429 | * we lock up (O_NONBLOCK-)readers that sleep due to |
430 | * syscall merging. | |
431 | * FIXME! Is this really true? | |
432 | */ | |
433 | do_wakeup = 1; | |
434 | chars = PAGE_SIZE; | |
435 | if (chars > total_len) | |
436 | chars = total_len; | |
437 | ||
f6762b7a JA |
438 | iov_fault_in_pages_read(iov, chars); |
439 | redo2: | |
440 | if (atomic) | |
441 | src = kmap_atomic(page, KM_USER0); | |
442 | else | |
443 | src = kmap(page); | |
444 | ||
445 | error = pipe_iov_copy_from_user(src, iov, chars, | |
446 | atomic); | |
447 | if (atomic) | |
448 | kunmap_atomic(src, KM_USER0); | |
449 | else | |
450 | kunmap(page); | |
451 | ||
1da177e4 | 452 | if (unlikely(error)) { |
f6762b7a JA |
453 | if (atomic) { |
454 | atomic = 0; | |
455 | goto redo2; | |
456 | } | |
341b446b | 457 | if (!ret) |
f6762b7a | 458 | ret = error; |
1da177e4 LT |
459 | break; |
460 | } | |
461 | ret += chars; | |
462 | ||
463 | /* Insert it into the buffer array */ | |
464 | buf->page = page; | |
465 | buf->ops = &anon_pipe_buf_ops; | |
466 | buf->offset = 0; | |
467 | buf->len = chars; | |
923f4f23 IM |
468 | pipe->nrbufs = ++bufs; |
469 | pipe->tmp_page = NULL; | |
1da177e4 LT |
470 | |
471 | total_len -= chars; | |
472 | if (!total_len) | |
473 | break; | |
474 | } | |
475 | if (bufs < PIPE_BUFFERS) | |
476 | continue; | |
477 | if (filp->f_flags & O_NONBLOCK) { | |
341b446b IM |
478 | if (!ret) |
479 | ret = -EAGAIN; | |
1da177e4 LT |
480 | break; |
481 | } | |
482 | if (signal_pending(current)) { | |
341b446b IM |
483 | if (!ret) |
484 | ret = -ERESTARTSYS; | |
1da177e4 LT |
485 | break; |
486 | } | |
487 | if (do_wakeup) { | |
923f4f23 IM |
488 | wake_up_interruptible_sync(&pipe->wait); |
489 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); | |
1da177e4 LT |
490 | do_wakeup = 0; |
491 | } | |
923f4f23 IM |
492 | pipe->waiting_writers++; |
493 | pipe_wait(pipe); | |
494 | pipe->waiting_writers--; | |
1da177e4 LT |
495 | } |
496 | out: | |
9aeedfc4 | 497 | mutex_unlock(&inode->i_mutex); |
1da177e4 | 498 | if (do_wakeup) { |
923f4f23 IM |
499 | wake_up_interruptible(&pipe->wait); |
500 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); | |
1da177e4 LT |
501 | } |
502 | if (ret > 0) | |
870f4817 | 503 | file_update_time(filp); |
1da177e4 LT |
504 | return ret; |
505 | } | |
506 | ||
1da177e4 LT |
507 | static ssize_t |
508 | bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos) | |
509 | { | |
510 | return -EBADF; | |
511 | } | |
512 | ||
513 | static ssize_t | |
341b446b IM |
514 | bad_pipe_w(struct file *filp, const char __user *buf, size_t count, |
515 | loff_t *ppos) | |
1da177e4 LT |
516 | { |
517 | return -EBADF; | |
518 | } | |
519 | ||
520 | static int | |
521 | pipe_ioctl(struct inode *pino, struct file *filp, | |
522 | unsigned int cmd, unsigned long arg) | |
523 | { | |
0f7fc9e4 | 524 | struct inode *inode = filp->f_path.dentry->d_inode; |
923f4f23 | 525 | struct pipe_inode_info *pipe; |
1da177e4 LT |
526 | int count, buf, nrbufs; |
527 | ||
528 | switch (cmd) { | |
529 | case FIONREAD: | |
9aeedfc4 | 530 | mutex_lock(&inode->i_mutex); |
923f4f23 | 531 | pipe = inode->i_pipe; |
1da177e4 | 532 | count = 0; |
923f4f23 IM |
533 | buf = pipe->curbuf; |
534 | nrbufs = pipe->nrbufs; | |
1da177e4 | 535 | while (--nrbufs >= 0) { |
923f4f23 | 536 | count += pipe->bufs[buf].len; |
1da177e4 LT |
537 | buf = (buf+1) & (PIPE_BUFFERS-1); |
538 | } | |
9aeedfc4 | 539 | mutex_unlock(&inode->i_mutex); |
923f4f23 | 540 | |
1da177e4 LT |
541 | return put_user(count, (int __user *)arg); |
542 | default: | |
543 | return -EINVAL; | |
544 | } | |
545 | } | |
546 | ||
547 | /* No kernel lock held - fine */ | |
548 | static unsigned int | |
549 | pipe_poll(struct file *filp, poll_table *wait) | |
550 | { | |
551 | unsigned int mask; | |
0f7fc9e4 | 552 | struct inode *inode = filp->f_path.dentry->d_inode; |
923f4f23 | 553 | struct pipe_inode_info *pipe = inode->i_pipe; |
1da177e4 LT |
554 | int nrbufs; |
555 | ||
923f4f23 | 556 | poll_wait(filp, &pipe->wait, wait); |
1da177e4 LT |
557 | |
558 | /* Reading only -- no need for acquiring the semaphore. */ | |
923f4f23 | 559 | nrbufs = pipe->nrbufs; |
1da177e4 LT |
560 | mask = 0; |
561 | if (filp->f_mode & FMODE_READ) { | |
562 | mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0; | |
923f4f23 | 563 | if (!pipe->writers && filp->f_version != pipe->w_counter) |
1da177e4 LT |
564 | mask |= POLLHUP; |
565 | } | |
566 | ||
567 | if (filp->f_mode & FMODE_WRITE) { | |
568 | mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0; | |
5e5d7a22 PE |
569 | /* |
570 | * Most Unices do not set POLLERR for FIFOs but on Linux they | |
571 | * behave exactly like pipes for poll(). | |
572 | */ | |
923f4f23 | 573 | if (!pipe->readers) |
1da177e4 LT |
574 | mask |= POLLERR; |
575 | } | |
576 | ||
577 | return mask; | |
578 | } | |
579 | ||
1da177e4 LT |
580 | static int |
581 | pipe_release(struct inode *inode, int decr, int decw) | |
582 | { | |
923f4f23 IM |
583 | struct pipe_inode_info *pipe; |
584 | ||
9aeedfc4 | 585 | mutex_lock(&inode->i_mutex); |
923f4f23 IM |
586 | pipe = inode->i_pipe; |
587 | pipe->readers -= decr; | |
588 | pipe->writers -= decw; | |
341b446b | 589 | |
923f4f23 | 590 | if (!pipe->readers && !pipe->writers) { |
1da177e4 LT |
591 | free_pipe_info(inode); |
592 | } else { | |
923f4f23 IM |
593 | wake_up_interruptible(&pipe->wait); |
594 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); | |
595 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); | |
1da177e4 | 596 | } |
9aeedfc4 | 597 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
598 | |
599 | return 0; | |
600 | } | |
601 | ||
602 | static int | |
603 | pipe_read_fasync(int fd, struct file *filp, int on) | |
604 | { | |
0f7fc9e4 | 605 | struct inode *inode = filp->f_path.dentry->d_inode; |
1da177e4 LT |
606 | int retval; |
607 | ||
9aeedfc4 IM |
608 | mutex_lock(&inode->i_mutex); |
609 | retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers); | |
610 | mutex_unlock(&inode->i_mutex); | |
1da177e4 LT |
611 | |
612 | if (retval < 0) | |
613 | return retval; | |
614 | ||
615 | return 0; | |
616 | } | |
617 | ||
618 | ||
619 | static int | |
620 | pipe_write_fasync(int fd, struct file *filp, int on) | |
621 | { | |
0f7fc9e4 | 622 | struct inode *inode = filp->f_path.dentry->d_inode; |
1da177e4 LT |
623 | int retval; |
624 | ||
9aeedfc4 IM |
625 | mutex_lock(&inode->i_mutex); |
626 | retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers); | |
627 | mutex_unlock(&inode->i_mutex); | |
1da177e4 LT |
628 | |
629 | if (retval < 0) | |
630 | return retval; | |
631 | ||
632 | return 0; | |
633 | } | |
634 | ||
635 | ||
636 | static int | |
637 | pipe_rdwr_fasync(int fd, struct file *filp, int on) | |
638 | { | |
0f7fc9e4 | 639 | struct inode *inode = filp->f_path.dentry->d_inode; |
341b446b | 640 | struct pipe_inode_info *pipe = inode->i_pipe; |
1da177e4 LT |
641 | int retval; |
642 | ||
9aeedfc4 | 643 | mutex_lock(&inode->i_mutex); |
1da177e4 | 644 | |
341b446b | 645 | retval = fasync_helper(fd, filp, on, &pipe->fasync_readers); |
1da177e4 LT |
646 | |
647 | if (retval >= 0) | |
341b446b | 648 | retval = fasync_helper(fd, filp, on, &pipe->fasync_writers); |
1da177e4 | 649 | |
9aeedfc4 | 650 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
651 | |
652 | if (retval < 0) | |
653 | return retval; | |
654 | ||
655 | return 0; | |
656 | } | |
657 | ||
658 | ||
659 | static int | |
660 | pipe_read_release(struct inode *inode, struct file *filp) | |
661 | { | |
662 | pipe_read_fasync(-1, filp, 0); | |
663 | return pipe_release(inode, 1, 0); | |
664 | } | |
665 | ||
666 | static int | |
667 | pipe_write_release(struct inode *inode, struct file *filp) | |
668 | { | |
669 | pipe_write_fasync(-1, filp, 0); | |
670 | return pipe_release(inode, 0, 1); | |
671 | } | |
672 | ||
673 | static int | |
674 | pipe_rdwr_release(struct inode *inode, struct file *filp) | |
675 | { | |
676 | int decr, decw; | |
677 | ||
678 | pipe_rdwr_fasync(-1, filp, 0); | |
679 | decr = (filp->f_mode & FMODE_READ) != 0; | |
680 | decw = (filp->f_mode & FMODE_WRITE) != 0; | |
681 | return pipe_release(inode, decr, decw); | |
682 | } | |
683 | ||
684 | static int | |
685 | pipe_read_open(struct inode *inode, struct file *filp) | |
686 | { | |
687 | /* We could have perhaps used atomic_t, but this and friends | |
688 | below are the only places. So it doesn't seem worthwhile. */ | |
9aeedfc4 IM |
689 | mutex_lock(&inode->i_mutex); |
690 | inode->i_pipe->readers++; | |
691 | mutex_unlock(&inode->i_mutex); | |
1da177e4 LT |
692 | |
693 | return 0; | |
694 | } | |
695 | ||
696 | static int | |
697 | pipe_write_open(struct inode *inode, struct file *filp) | |
698 | { | |
9aeedfc4 IM |
699 | mutex_lock(&inode->i_mutex); |
700 | inode->i_pipe->writers++; | |
701 | mutex_unlock(&inode->i_mutex); | |
1da177e4 LT |
702 | |
703 | return 0; | |
704 | } | |
705 | ||
706 | static int | |
707 | pipe_rdwr_open(struct inode *inode, struct file *filp) | |
708 | { | |
9aeedfc4 | 709 | mutex_lock(&inode->i_mutex); |
1da177e4 | 710 | if (filp->f_mode & FMODE_READ) |
9aeedfc4 | 711 | inode->i_pipe->readers++; |
1da177e4 | 712 | if (filp->f_mode & FMODE_WRITE) |
9aeedfc4 IM |
713 | inode->i_pipe->writers++; |
714 | mutex_unlock(&inode->i_mutex); | |
1da177e4 LT |
715 | |
716 | return 0; | |
717 | } | |
718 | ||
719 | /* | |
720 | * The file_operations structs are not static because they | |
721 | * are also used in linux/fs/fifo.c to do operations on FIFOs. | |
722 | */ | |
4b6f5d20 | 723 | const struct file_operations read_fifo_fops = { |
1da177e4 | 724 | .llseek = no_llseek, |
ee0b3e67 BP |
725 | .read = do_sync_read, |
726 | .aio_read = pipe_read, | |
1da177e4 | 727 | .write = bad_pipe_w, |
5e5d7a22 | 728 | .poll = pipe_poll, |
1da177e4 LT |
729 | .ioctl = pipe_ioctl, |
730 | .open = pipe_read_open, | |
731 | .release = pipe_read_release, | |
732 | .fasync = pipe_read_fasync, | |
733 | }; | |
734 | ||
4b6f5d20 | 735 | const struct file_operations write_fifo_fops = { |
1da177e4 LT |
736 | .llseek = no_llseek, |
737 | .read = bad_pipe_r, | |
ee0b3e67 BP |
738 | .write = do_sync_write, |
739 | .aio_write = pipe_write, | |
5e5d7a22 | 740 | .poll = pipe_poll, |
1da177e4 LT |
741 | .ioctl = pipe_ioctl, |
742 | .open = pipe_write_open, | |
743 | .release = pipe_write_release, | |
744 | .fasync = pipe_write_fasync, | |
745 | }; | |
746 | ||
4b6f5d20 | 747 | const struct file_operations rdwr_fifo_fops = { |
1da177e4 | 748 | .llseek = no_llseek, |
ee0b3e67 BP |
749 | .read = do_sync_read, |
750 | .aio_read = pipe_read, | |
751 | .write = do_sync_write, | |
752 | .aio_write = pipe_write, | |
5e5d7a22 | 753 | .poll = pipe_poll, |
1da177e4 LT |
754 | .ioctl = pipe_ioctl, |
755 | .open = pipe_rdwr_open, | |
756 | .release = pipe_rdwr_release, | |
757 | .fasync = pipe_rdwr_fasync, | |
758 | }; | |
759 | ||
d4c3cca9 | 760 | static const struct file_operations read_pipe_fops = { |
1da177e4 | 761 | .llseek = no_llseek, |
ee0b3e67 BP |
762 | .read = do_sync_read, |
763 | .aio_read = pipe_read, | |
1da177e4 LT |
764 | .write = bad_pipe_w, |
765 | .poll = pipe_poll, | |
766 | .ioctl = pipe_ioctl, | |
767 | .open = pipe_read_open, | |
768 | .release = pipe_read_release, | |
769 | .fasync = pipe_read_fasync, | |
770 | }; | |
771 | ||
d4c3cca9 | 772 | static const struct file_operations write_pipe_fops = { |
1da177e4 LT |
773 | .llseek = no_llseek, |
774 | .read = bad_pipe_r, | |
ee0b3e67 BP |
775 | .write = do_sync_write, |
776 | .aio_write = pipe_write, | |
1da177e4 LT |
777 | .poll = pipe_poll, |
778 | .ioctl = pipe_ioctl, | |
779 | .open = pipe_write_open, | |
780 | .release = pipe_write_release, | |
781 | .fasync = pipe_write_fasync, | |
782 | }; | |
783 | ||
d4c3cca9 | 784 | static const struct file_operations rdwr_pipe_fops = { |
1da177e4 | 785 | .llseek = no_llseek, |
ee0b3e67 BP |
786 | .read = do_sync_read, |
787 | .aio_read = pipe_read, | |
788 | .write = do_sync_write, | |
789 | .aio_write = pipe_write, | |
1da177e4 LT |
790 | .poll = pipe_poll, |
791 | .ioctl = pipe_ioctl, | |
792 | .open = pipe_rdwr_open, | |
793 | .release = pipe_rdwr_release, | |
794 | .fasync = pipe_rdwr_fasync, | |
795 | }; | |
796 | ||
3a326a2c IM |
797 | struct pipe_inode_info * alloc_pipe_info(struct inode *inode) |
798 | { | |
923f4f23 | 799 | struct pipe_inode_info *pipe; |
3a326a2c | 800 | |
923f4f23 IM |
801 | pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL); |
802 | if (pipe) { | |
803 | init_waitqueue_head(&pipe->wait); | |
804 | pipe->r_counter = pipe->w_counter = 1; | |
805 | pipe->inode = inode; | |
3a326a2c IM |
806 | } |
807 | ||
923f4f23 | 808 | return pipe; |
3a326a2c IM |
809 | } |
810 | ||
923f4f23 | 811 | void __free_pipe_info(struct pipe_inode_info *pipe) |
1da177e4 LT |
812 | { |
813 | int i; | |
1da177e4 | 814 | |
1da177e4 | 815 | for (i = 0; i < PIPE_BUFFERS; i++) { |
923f4f23 | 816 | struct pipe_buffer *buf = pipe->bufs + i; |
1da177e4 | 817 | if (buf->ops) |
923f4f23 | 818 | buf->ops->release(pipe, buf); |
1da177e4 | 819 | } |
923f4f23 IM |
820 | if (pipe->tmp_page) |
821 | __free_page(pipe->tmp_page); | |
822 | kfree(pipe); | |
1da177e4 LT |
823 | } |
824 | ||
b92ce558 JA |
825 | void free_pipe_info(struct inode *inode) |
826 | { | |
827 | __free_pipe_info(inode->i_pipe); | |
828 | inode->i_pipe = NULL; | |
829 | } | |
830 | ||
fa3536cc | 831 | static struct vfsmount *pipe_mnt __read_mostly; |
1da177e4 LT |
832 | static int pipefs_delete_dentry(struct dentry *dentry) |
833 | { | |
d18de5a2 ED |
834 | /* |
835 | * At creation time, we pretended this dentry was hashed | |
836 | * (by clearing DCACHE_UNHASHED bit in d_flags) | |
837 | * At delete time, we restore the truth : not hashed. | |
838 | * (so that dput() can proceed correctly) | |
839 | */ | |
840 | dentry->d_flags |= DCACHE_UNHASHED; | |
841 | return 0; | |
1da177e4 | 842 | } |
341b446b | 843 | |
c23fbb6b ED |
844 | /* |
845 | * pipefs_dname() is called from d_path(). | |
846 | */ | |
847 | static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen) | |
848 | { | |
849 | return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]", | |
850 | dentry->d_inode->i_ino); | |
851 | } | |
852 | ||
1da177e4 LT |
853 | static struct dentry_operations pipefs_dentry_operations = { |
854 | .d_delete = pipefs_delete_dentry, | |
c23fbb6b | 855 | .d_dname = pipefs_dname, |
1da177e4 LT |
856 | }; |
857 | ||
858 | static struct inode * get_pipe_inode(void) | |
859 | { | |
860 | struct inode *inode = new_inode(pipe_mnt->mnt_sb); | |
923f4f23 | 861 | struct pipe_inode_info *pipe; |
1da177e4 LT |
862 | |
863 | if (!inode) | |
864 | goto fail_inode; | |
865 | ||
923f4f23 IM |
866 | pipe = alloc_pipe_info(inode); |
867 | if (!pipe) | |
1da177e4 | 868 | goto fail_iput; |
923f4f23 | 869 | inode->i_pipe = pipe; |
3a326a2c | 870 | |
923f4f23 | 871 | pipe->readers = pipe->writers = 1; |
1da177e4 LT |
872 | inode->i_fop = &rdwr_pipe_fops; |
873 | ||
874 | /* | |
875 | * Mark the inode dirty from the very beginning, | |
876 | * that way it will never be moved to the dirty | |
877 | * list because "mark_inode_dirty()" will think | |
878 | * that it already _is_ on the dirty list. | |
879 | */ | |
880 | inode->i_state = I_DIRTY; | |
881 | inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR; | |
882 | inode->i_uid = current->fsuid; | |
883 | inode->i_gid = current->fsgid; | |
884 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
923f4f23 | 885 | |
1da177e4 LT |
886 | return inode; |
887 | ||
888 | fail_iput: | |
889 | iput(inode); | |
341b446b | 890 | |
1da177e4 LT |
891 | fail_inode: |
892 | return NULL; | |
893 | } | |
894 | ||
d6cbd281 | 895 | struct file *create_write_pipe(void) |
1da177e4 | 896 | { |
d6cbd281 AK |
897 | int err; |
898 | struct inode *inode; | |
899 | struct file *f; | |
1da177e4 | 900 | struct dentry *dentry; |
c23fbb6b | 901 | struct qstr name = { .name = "" }; |
1da177e4 | 902 | |
d6cbd281 AK |
903 | f = get_empty_filp(); |
904 | if (!f) | |
905 | return ERR_PTR(-ENFILE); | |
906 | err = -ENFILE; | |
1da177e4 LT |
907 | inode = get_pipe_inode(); |
908 | if (!inode) | |
d6cbd281 | 909 | goto err_file; |
1da177e4 | 910 | |
d6cbd281 | 911 | err = -ENOMEM; |
c23fbb6b | 912 | dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &name); |
1da177e4 | 913 | if (!dentry) |
d6cbd281 | 914 | goto err_inode; |
341b446b | 915 | |
1da177e4 | 916 | dentry->d_op = &pipefs_dentry_operations; |
d18de5a2 ED |
917 | /* |
918 | * We dont want to publish this dentry into global dentry hash table. | |
919 | * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED | |
920 | * This permits a working /proc/$pid/fd/XXX on pipes | |
921 | */ | |
922 | dentry->d_flags &= ~DCACHE_UNHASHED; | |
923 | d_instantiate(dentry, inode); | |
0f7fc9e4 JJS |
924 | f->f_path.mnt = mntget(pipe_mnt); |
925 | f->f_path.dentry = dentry; | |
d6cbd281 | 926 | f->f_mapping = inode->i_mapping; |
341b446b | 927 | |
d6cbd281 AK |
928 | f->f_flags = O_WRONLY; |
929 | f->f_op = &write_pipe_fops; | |
930 | f->f_mode = FMODE_WRITE; | |
931 | f->f_version = 0; | |
932 | ||
933 | return f; | |
1da177e4 | 934 | |
d6cbd281 | 935 | err_inode: |
1da177e4 LT |
936 | free_pipe_info(inode); |
937 | iput(inode); | |
d6cbd281 AK |
938 | err_file: |
939 | put_filp(f); | |
940 | return ERR_PTR(err); | |
941 | } | |
942 | ||
943 | void free_write_pipe(struct file *f) | |
944 | { | |
5ccac88e | 945 | free_pipe_info(f->f_dentry->d_inode); |
0f7fc9e4 | 946 | dput(f->f_path.dentry); |
5ccac88e | 947 | mntput(f->f_path.mnt); |
d6cbd281 AK |
948 | put_filp(f); |
949 | } | |
950 | ||
951 | struct file *create_read_pipe(struct file *wrf) | |
952 | { | |
953 | struct file *f = get_empty_filp(); | |
954 | if (!f) | |
955 | return ERR_PTR(-ENFILE); | |
956 | ||
957 | /* Grab pipe from the writer */ | |
0f7fc9e4 JJS |
958 | f->f_path.mnt = mntget(wrf->f_path.mnt); |
959 | f->f_path.dentry = dget(wrf->f_path.dentry); | |
960 | f->f_mapping = wrf->f_path.dentry->d_inode->i_mapping; | |
d6cbd281 AK |
961 | |
962 | f->f_pos = 0; | |
963 | f->f_flags = O_RDONLY; | |
964 | f->f_op = &read_pipe_fops; | |
965 | f->f_mode = FMODE_READ; | |
966 | f->f_version = 0; | |
967 | ||
968 | return f; | |
969 | } | |
970 | ||
971 | int do_pipe(int *fd) | |
972 | { | |
973 | struct file *fw, *fr; | |
974 | int error; | |
975 | int fdw, fdr; | |
976 | ||
977 | fw = create_write_pipe(); | |
978 | if (IS_ERR(fw)) | |
979 | return PTR_ERR(fw); | |
980 | fr = create_read_pipe(fw); | |
981 | error = PTR_ERR(fr); | |
982 | if (IS_ERR(fr)) | |
983 | goto err_write_pipe; | |
984 | ||
985 | error = get_unused_fd(); | |
986 | if (error < 0) | |
987 | goto err_read_pipe; | |
988 | fdr = error; | |
989 | ||
990 | error = get_unused_fd(); | |
991 | if (error < 0) | |
992 | goto err_fdr; | |
993 | fdw = error; | |
994 | ||
db349509 AV |
995 | error = audit_fd_pair(fdr, fdw); |
996 | if (error < 0) | |
997 | goto err_fdw; | |
998 | ||
d6cbd281 AK |
999 | fd_install(fdr, fr); |
1000 | fd_install(fdw, fw); | |
1001 | fd[0] = fdr; | |
1002 | fd[1] = fdw; | |
1003 | ||
1004 | return 0; | |
1005 | ||
db349509 AV |
1006 | err_fdw: |
1007 | put_unused_fd(fdw); | |
d6cbd281 AK |
1008 | err_fdr: |
1009 | put_unused_fd(fdr); | |
1010 | err_read_pipe: | |
5ccac88e AV |
1011 | dput(fr->f_dentry); |
1012 | mntput(fr->f_vfsmnt); | |
d6cbd281 AK |
1013 | put_filp(fr); |
1014 | err_write_pipe: | |
1015 | free_write_pipe(fw); | |
1016 | return error; | |
1da177e4 LT |
1017 | } |
1018 | ||
1019 | /* | |
1020 | * pipefs should _never_ be mounted by userland - too much of security hassle, | |
1021 | * no real gain from having the whole whorehouse mounted. So we don't need | |
1022 | * any operations on the root directory. However, we need a non-trivial | |
1023 | * d_name - pipe: will go nicely and kill the special-casing in procfs. | |
1024 | */ | |
454e2398 DH |
1025 | static int pipefs_get_sb(struct file_system_type *fs_type, |
1026 | int flags, const char *dev_name, void *data, | |
1027 | struct vfsmount *mnt) | |
1da177e4 | 1028 | { |
454e2398 | 1029 | return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC, mnt); |
1da177e4 LT |
1030 | } |
1031 | ||
1032 | static struct file_system_type pipe_fs_type = { | |
1033 | .name = "pipefs", | |
1034 | .get_sb = pipefs_get_sb, | |
1035 | .kill_sb = kill_anon_super, | |
1036 | }; | |
1037 | ||
1038 | static int __init init_pipe_fs(void) | |
1039 | { | |
1040 | int err = register_filesystem(&pipe_fs_type); | |
341b446b | 1041 | |
1da177e4 LT |
1042 | if (!err) { |
1043 | pipe_mnt = kern_mount(&pipe_fs_type); | |
1044 | if (IS_ERR(pipe_mnt)) { | |
1045 | err = PTR_ERR(pipe_mnt); | |
1046 | unregister_filesystem(&pipe_fs_type); | |
1047 | } | |
1048 | } | |
1049 | return err; | |
1050 | } | |
1051 | ||
1052 | static void __exit exit_pipe_fs(void) | |
1053 | { | |
1054 | unregister_filesystem(&pipe_fs_type); | |
1055 | mntput(pipe_mnt); | |
1056 | } | |
1057 | ||
1058 | fs_initcall(init_pipe_fs); | |
1059 | module_exit(exit_pipe_fs); |