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