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