Blame view
fs/pipe.c
26.6 KB
1da177e4c Linux-2.6.12-rc2 |
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 pipe: add support... |
14 |
#include <linux/log2.h> |
1da177e4c Linux-2.6.12-rc2 |
15 |
#include <linux/mount.h> |
b502bd115 magic.h: move som... |
16 |
#include <linux/magic.h> |
1da177e4c Linux-2.6.12-rc2 |
17 18 19 |
#include <linux/pipe_fs_i.h> #include <linux/uio.h> #include <linux/highmem.h> |
5274f052e [PATCH] Introduce... |
20 |
#include <linux/pagemap.h> |
db3495099 [PATCH] AUDIT_FD_... |
21 |
#include <linux/audit.h> |
ba719baea sys_pipe(): fix f... |
22 |
#include <linux/syscalls.h> |
b492e95be pipe: set lower a... |
23 |
#include <linux/fcntl.h> |
d86133bd3 pipe: account to ... |
24 |
#include <linux/memcontrol.h> |
1da177e4c Linux-2.6.12-rc2 |
25 26 27 |
#include <asm/uaccess.h> #include <asm/ioctls.h> |
599a0ac14 pipe: fold file_o... |
28 |
#include "internal.h" |
1da177e4c Linux-2.6.12-rc2 |
29 |
/* |
b492e95be pipe: set lower a... |
30 |
* The max size that a non-root user is allowed to grow the pipe. Can |
ff9da691c pipe: change /pro... |
31 |
* be set by root in /proc/sys/fs/pipe-max-size |
b492e95be pipe: set lower a... |
32 |
*/ |
ff9da691c pipe: change /pro... |
33 34 35 36 37 38 |
unsigned int pipe_max_size = 1048576; /* * Minimum pipe size, as required by POSIX */ unsigned int pipe_min_size = PAGE_SIZE; |
b492e95be pipe: set lower a... |
39 |
|
759c01142 pipe: limit the p... |
40 41 42 43 44 |
/* Maximum allocatable pages per user. Hard limit is unset by default, soft * matches default values. */ unsigned long pipe_user_pages_hard; unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR; |
b492e95be pipe: set lower a... |
45 |
/* |
1da177e4c Linux-2.6.12-rc2 |
46 47 48 49 50 51 52 53 54 55 56 57 58 |
* 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 splice: add helpe... |
59 60 |
static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass) { |
6447a3cf1 get rid of pipe->... |
61 |
if (pipe->files) |
72b0d9aac pipe: don't use -... |
62 |
mutex_lock_nested(&pipe->mutex, subclass); |
61e0d47c3 splice: add helpe... |
63 64 65 66 67 68 69 70 71 72 73 74 75 |
} 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) { |
6447a3cf1 get rid of pipe->... |
76 |
if (pipe->files) |
72b0d9aac pipe: don't use -... |
77 |
mutex_unlock(&pipe->mutex); |
61e0d47c3 splice: add helpe... |
78 79 |
} EXPORT_SYMBOL(pipe_unlock); |
ebec73f47 introduce variant... |
80 81 82 83 84 85 86 87 88 |
static inline void __pipe_lock(struct pipe_inode_info *pipe) { mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT); } static inline void __pipe_unlock(struct pipe_inode_info *pipe) { mutex_unlock(&pipe->mutex); } |
61e0d47c3 splice: add helpe... |
89 90 91 92 93 94 95 96 97 |
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 lockdep: Fix lock... |
98 99 |
pipe_lock_nested(pipe2, I_MUTEX_PARENT); pipe_lock_nested(pipe1, I_MUTEX_CHILD); |
61e0d47c3 splice: add helpe... |
100 101 |
} } |
1da177e4c Linux-2.6.12-rc2 |
102 |
/* Drop the inode semaphore and wait for a pipe event, atomically */ |
3a326a2ce [PATCH] introduce... |
103 |
void pipe_wait(struct pipe_inode_info *pipe) |
1da177e4c Linux-2.6.12-rc2 |
104 105 |
{ DEFINE_WAIT(wait); |
d79fc0fc6 [PATCH] sched: TA... |
106 107 108 109 |
/* * Pipes are system-local resources, so sleeping on them * is considered a noninteractive wait: */ |
af9272326 sched: cleanup, r... |
110 |
prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE); |
61e0d47c3 splice: add helpe... |
111 |
pipe_unlock(pipe); |
1da177e4c Linux-2.6.12-rc2 |
112 |
schedule(); |
3a326a2ce [PATCH] introduce... |
113 |
finish_wait(&pipe->wait, &wait); |
61e0d47c3 splice: add helpe... |
114 |
pipe_lock(pipe); |
1da177e4c Linux-2.6.12-rc2 |
115 |
} |
341b446bc [PATCH] another r... |
116 117 |
static void anon_pipe_buf_release(struct pipe_inode_info *pipe, struct pipe_buffer *buf) |
1da177e4c Linux-2.6.12-rc2 |
118 119 |
{ struct page *page = buf->page; |
5274f052e [PATCH] Introduce... |
120 121 122 |
/* * 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 [PATCH] another r... |
123 |
* allocation cache. (Otherwise just release our reference to it) |
5274f052e [PATCH] Introduce... |
124 |
*/ |
341b446bc [PATCH] another r... |
125 |
if (page_count(page) == 1 && !pipe->tmp_page) |
923f4f239 [PATCH] pipe.c/fi... |
126 |
pipe->tmp_page = page; |
341b446bc [PATCH] another r... |
127 |
else |
09cbfeaf1 mm, fs: get rid o... |
128 |
put_page(page); |
1da177e4c Linux-2.6.12-rc2 |
129 |
} |
d86133bd3 pipe: account to ... |
130 131 132 133 134 135 |
static int anon_pipe_buf_steal(struct pipe_inode_info *pipe, struct pipe_buffer *buf) { struct page *page = buf->page; if (page_count(page) == 1) { |
c4159a75b mm: memcontrol: o... |
136 |
if (memcg_kmem_enabled()) |
d86133bd3 pipe: account to ... |
137 |
memcg_kmem_uncharge(page, 0); |
d86133bd3 pipe: account to ... |
138 139 140 141 142 |
__SetPageLocked(page); return 0; } return 1; } |
0845718da pipe: add documen... |
143 |
/** |
b51d63c6d kernel-doc: fix f... |
144 |
* generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer |
0845718da pipe: add documen... |
145 146 147 148 |
* @pipe: the pipe that the buffer belongs to * @buf: the buffer to attempt to steal * * Description: |
b51d63c6d kernel-doc: fix f... |
149 |
* This function attempts to steal the &struct page attached to |
0845718da pipe: add documen... |
150 151 |
* @buf. If successful, this function returns 0 and returns with * the page locked. The caller may then reuse the page for whatever |
b51d63c6d kernel-doc: fix f... |
152 |
* he wishes; the typical use is insertion into a different file |
0845718da pipe: add documen... |
153 154 |
* page cache. */ |
330ab7161 [PATCH] vmsplice:... |
155 156 |
int generic_pipe_buf_steal(struct pipe_inode_info *pipe, struct pipe_buffer *buf) |
5abc97aa2 [PATCH] splice: a... |
157 |
{ |
46e678c96 [PATCH] splice: f... |
158 |
struct page *page = buf->page; |
0845718da pipe: add documen... |
159 160 161 162 163 |
/* * 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 [PATCH] splice: f... |
164 |
if (page_count(page) == 1) { |
46e678c96 [PATCH] splice: f... |
165 166 167 168 169 |
lock_page(page); return 0; } return 1; |
5abc97aa2 [PATCH] splice: a... |
170 |
} |
51921cb74 mm: export generi... |
171 |
EXPORT_SYMBOL(generic_pipe_buf_steal); |
5abc97aa2 [PATCH] splice: a... |
172 |
|
0845718da pipe: add documen... |
173 |
/** |
b51d63c6d kernel-doc: fix f... |
174 |
* generic_pipe_buf_get - get a reference to a &struct pipe_buffer |
0845718da pipe: add documen... |
175 176 177 178 179 180 181 182 183 |
* @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 [PATCH] splice: a... |
184 |
{ |
09cbfeaf1 mm, fs: get rid o... |
185 |
get_page(buf->page); |
70524490e [PATCH] splice: a... |
186 |
} |
51921cb74 mm: export generi... |
187 |
EXPORT_SYMBOL(generic_pipe_buf_get); |
70524490e [PATCH] splice: a... |
188 |
|
0845718da pipe: add documen... |
189 190 |
/** * generic_pipe_buf_confirm - verify contents of the pipe buffer |
79685b8de docbook: add pipe... |
191 |
* @info: the pipe that the buffer belongs to |
0845718da pipe: add documen... |
192 193 194 195 196 197 |
* @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 pipe: change the ... |
198 199 |
int generic_pipe_buf_confirm(struct pipe_inode_info *info, struct pipe_buffer *buf) |
f84d75199 [PATCH] pipe: int... |
200 201 202 |
{ return 0; } |
51921cb74 mm: export generi... |
203 |
EXPORT_SYMBOL(generic_pipe_buf_confirm); |
f84d75199 [PATCH] pipe: int... |
204 |
|
6818173bd splice: implement... |
205 206 207 208 209 210 211 212 213 214 215 |
/** * 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) { |
09cbfeaf1 mm, fs: get rid o... |
216 |
put_page(buf->page); |
6818173bd splice: implement... |
217 |
} |
51921cb74 mm: export generi... |
218 |
EXPORT_SYMBOL(generic_pipe_buf_release); |
6818173bd splice: implement... |
219 |
|
d4c3cca94 [PATCH] constify ... |
220 |
static const struct pipe_buf_operations anon_pipe_buf_ops = { |
1da177e4c Linux-2.6.12-rc2 |
221 |
.can_merge = 1, |
cac36bb06 pipe: change the ... |
222 |
.confirm = generic_pipe_buf_confirm, |
1da177e4c Linux-2.6.12-rc2 |
223 |
.release = anon_pipe_buf_release, |
d86133bd3 pipe: account to ... |
224 |
.steal = anon_pipe_buf_steal, |
f84d75199 [PATCH] pipe: int... |
225 |
.get = generic_pipe_buf_get, |
1da177e4c Linux-2.6.12-rc2 |
226 |
}; |
9883035ae pipes: add a "pac... |
227 228 |
static const struct pipe_buf_operations packet_pipe_buf_ops = { .can_merge = 0, |
9883035ae pipes: add a "pac... |
229 230 |
.confirm = generic_pipe_buf_confirm, .release = anon_pipe_buf_release, |
d86133bd3 pipe: account to ... |
231 |
.steal = anon_pipe_buf_steal, |
9883035ae pipes: add a "pac... |
232 233 |
.get = generic_pipe_buf_get, }; |
1da177e4c Linux-2.6.12-rc2 |
234 |
static ssize_t |
fb9096a34 pipe: switch to -... |
235 |
pipe_read(struct kiocb *iocb, struct iov_iter *to) |
1da177e4c Linux-2.6.12-rc2 |
236 |
{ |
fb9096a34 pipe: switch to -... |
237 |
size_t total_len = iov_iter_count(to); |
ee0b3e671 [PATCH] Remove re... |
238 |
struct file *filp = iocb->ki_filp; |
de32ec4cf pipe: set file->p... |
239 |
struct pipe_inode_info *pipe = filp->private_data; |
1da177e4c Linux-2.6.12-rc2 |
240 241 |
int do_wakeup; ssize_t ret; |
1da177e4c Linux-2.6.12-rc2 |
242 |
|
1da177e4c Linux-2.6.12-rc2 |
243 244 245 246 247 248 |
/* Null read succeeds. */ if (unlikely(total_len == 0)) return 0; do_wakeup = 0; ret = 0; |
ebec73f47 introduce variant... |
249 |
__pipe_lock(pipe); |
1da177e4c Linux-2.6.12-rc2 |
250 |
for (;;) { |
923f4f239 [PATCH] pipe.c/fi... |
251 |
int bufs = pipe->nrbufs; |
1da177e4c Linux-2.6.12-rc2 |
252 |
if (bufs) { |
923f4f239 [PATCH] pipe.c/fi... |
253 254 |
int curbuf = pipe->curbuf; struct pipe_buffer *buf = pipe->bufs + curbuf; |
d4c3cca94 [PATCH] constify ... |
255 |
const struct pipe_buf_operations *ops = buf->ops; |
1da177e4c Linux-2.6.12-rc2 |
256 |
size_t chars = buf->len; |
637b58c28 switch pipe_read(... |
257 258 |
size_t written; int error; |
1da177e4c Linux-2.6.12-rc2 |
259 260 261 |
if (chars > total_len) chars = total_len; |
cac36bb06 pipe: change the ... |
262 |
error = ops->confirm(pipe, buf); |
f84d75199 [PATCH] pipe: int... |
263 |
if (error) { |
5274f052e [PATCH] Introduce... |
264 |
if (!ret) |
e5953cbdf pipe: fix failure... |
265 |
ret = error; |
5274f052e [PATCH] Introduce... |
266 267 |
break; } |
f84d75199 [PATCH] pipe: int... |
268 |
|
fb9096a34 pipe: switch to -... |
269 |
written = copy_page_to_iter(buf->page, buf->offset, chars, to); |
637b58c28 switch pipe_read(... |
270 |
if (unlikely(written < chars)) { |
341b446bc [PATCH] another r... |
271 |
if (!ret) |
637b58c28 switch pipe_read(... |
272 |
ret = -EFAULT; |
1da177e4c Linux-2.6.12-rc2 |
273 274 275 276 277 |
break; } ret += chars; buf->offset += chars; buf->len -= chars; |
9883035ae pipes: add a "pac... |
278 279 280 281 282 283 |
/* Was it a packet buffer? Clean up and exit */ if (buf->flags & PIPE_BUF_FLAG_PACKET) { total_len = chars; buf->len = 0; } |
1da177e4c Linux-2.6.12-rc2 |
284 285 |
if (!buf->len) { buf->ops = NULL; |
923f4f239 [PATCH] pipe.c/fi... |
286 |
ops->release(pipe, buf); |
35f3d14db pipe: add support... |
287 |
curbuf = (curbuf + 1) & (pipe->buffers - 1); |
923f4f239 [PATCH] pipe.c/fi... |
288 289 |
pipe->curbuf = curbuf; pipe->nrbufs = --bufs; |
1da177e4c Linux-2.6.12-rc2 |
290 291 292 293 294 295 296 297 |
do_wakeup = 1; } total_len -= chars; if (!total_len) break; /* common path: read succeeded */ } if (bufs) /* More to do? */ continue; |
923f4f239 [PATCH] pipe.c/fi... |
298 |
if (!pipe->writers) |
1da177e4c Linux-2.6.12-rc2 |
299 |
break; |
923f4f239 [PATCH] pipe.c/fi... |
300 |
if (!pipe->waiting_writers) { |
1da177e4c Linux-2.6.12-rc2 |
301 302 303 304 305 306 307 308 309 310 311 312 313 |
/* 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 [PATCH] another r... |
314 315 |
if (!ret) ret = -ERESTARTSYS; |
1da177e4c Linux-2.6.12-rc2 |
316 317 318 |
break; } if (do_wakeup) { |
28e58ee8c Fix broken "pipe:... |
319 |
wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM); |
923f4f239 [PATCH] pipe.c/fi... |
320 |
kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
1da177e4c Linux-2.6.12-rc2 |
321 |
} |
923f4f239 [PATCH] pipe.c/fi... |
322 |
pipe_wait(pipe); |
1da177e4c Linux-2.6.12-rc2 |
323 |
} |
ebec73f47 introduce variant... |
324 |
__pipe_unlock(pipe); |
341b446bc [PATCH] another r... |
325 326 |
/* Signal writers asynchronously that there is more room. */ |
1da177e4c Linux-2.6.12-rc2 |
327 |
if (do_wakeup) { |
28e58ee8c Fix broken "pipe:... |
328 |
wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM); |
923f4f239 [PATCH] pipe.c/fi... |
329 |
kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
1da177e4c Linux-2.6.12-rc2 |
330 331 332 333 334 |
} if (ret > 0) file_accessed(filp); return ret; } |
9883035ae pipes: add a "pac... |
335 336 337 338 |
static inline int is_packetized(struct file *file) { return (file->f_flags & O_DIRECT) != 0; } |
1da177e4c Linux-2.6.12-rc2 |
339 |
static ssize_t |
f0d1bec9d new helper: copy_... |
340 |
pipe_write(struct kiocb *iocb, struct iov_iter *from) |
1da177e4c Linux-2.6.12-rc2 |
341 |
{ |
ee0b3e671 [PATCH] Remove re... |
342 |
struct file *filp = iocb->ki_filp; |
de32ec4cf pipe: set file->p... |
343 |
struct pipe_inode_info *pipe = filp->private_data; |
f0d1bec9d new helper: copy_... |
344 345 346 |
ssize_t ret = 0; int do_wakeup = 0; size_t total_len = iov_iter_count(from); |
1da177e4c Linux-2.6.12-rc2 |
347 |
ssize_t chars; |
1da177e4c Linux-2.6.12-rc2 |
348 349 350 |
/* Null write succeeds. */ if (unlikely(total_len == 0)) return 0; |
ebec73f47 introduce variant... |
351 |
__pipe_lock(pipe); |
1da177e4c Linux-2.6.12-rc2 |
352 |
|
923f4f239 [PATCH] pipe.c/fi... |
353 |
if (!pipe->readers) { |
1da177e4c Linux-2.6.12-rc2 |
354 355 356 357 358 359 360 |
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 [PATCH] pipe.c/fi... |
361 |
if (pipe->nrbufs && chars != 0) { |
341b446bc [PATCH] another r... |
362 |
int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) & |
35f3d14db pipe: add support... |
363 |
(pipe->buffers - 1); |
923f4f239 [PATCH] pipe.c/fi... |
364 |
struct pipe_buffer *buf = pipe->bufs + lastbuf; |
d4c3cca94 [PATCH] constify ... |
365 |
const struct pipe_buf_operations *ops = buf->ops; |
1da177e4c Linux-2.6.12-rc2 |
366 |
int offset = buf->offset + buf->len; |
341b446bc [PATCH] another r... |
367 |
|
1da177e4c Linux-2.6.12-rc2 |
368 |
if (ops->can_merge && offset + chars <= PAGE_SIZE) { |
6ae080699 fs/pipe.c: return... |
369 370 |
ret = ops->confirm(pipe, buf); if (ret) |
5274f052e [PATCH] Introduce... |
371 |
goto out; |
f84d75199 [PATCH] pipe: int... |
372 |
|
f0d1bec9d new helper: copy_... |
373 374 |
ret = copy_page_from_iter(buf->page, offset, chars, from); if (unlikely(ret < chars)) { |
6ae080699 fs/pipe.c: return... |
375 |
ret = -EFAULT; |
1da177e4c Linux-2.6.12-rc2 |
376 |
goto out; |
f6762b7ad [PATCH] pipe: ena... |
377 |
} |
f0d1bec9d new helper: copy_... |
378 |
do_wakeup = 1; |
6ae080699 fs/pipe.c: return... |
379 |
buf->len += ret; |
f0d1bec9d new helper: copy_... |
380 |
if (!iov_iter_count(from)) |
1da177e4c Linux-2.6.12-rc2 |
381 382 383 384 385 386 |
goto out; } } for (;;) { int bufs; |
341b446bc [PATCH] another r... |
387 |
|
923f4f239 [PATCH] pipe.c/fi... |
388 |
if (!pipe->readers) { |
1da177e4c Linux-2.6.12-rc2 |
389 |
send_sig(SIGPIPE, current, 0); |
341b446bc [PATCH] another r... |
390 391 |
if (!ret) ret = -EPIPE; |
1da177e4c Linux-2.6.12-rc2 |
392 393 |
break; } |
923f4f239 [PATCH] pipe.c/fi... |
394 |
bufs = pipe->nrbufs; |
35f3d14db pipe: add support... |
395 396 |
if (bufs < pipe->buffers) { int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1); |
923f4f239 [PATCH] pipe.c/fi... |
397 398 |
struct pipe_buffer *buf = pipe->bufs + newbuf; struct page *page = pipe->tmp_page; |
f0d1bec9d new helper: copy_... |
399 |
int copied; |
1da177e4c Linux-2.6.12-rc2 |
400 401 |
if (!page) { |
d86133bd3 pipe: account to ... |
402 |
page = alloc_page(GFP_HIGHUSER | __GFP_ACCOUNT); |
1da177e4c Linux-2.6.12-rc2 |
403 404 405 406 |
if (unlikely(!page)) { ret = ret ? : -ENOMEM; break; } |
923f4f239 [PATCH] pipe.c/fi... |
407 |
pipe->tmp_page = page; |
1da177e4c Linux-2.6.12-rc2 |
408 |
} |
341b446bc [PATCH] another r... |
409 |
/* Always wake up, even if the copy fails. Otherwise |
1da177e4c Linux-2.6.12-rc2 |
410 411 412 413 414 |
* we lock up (O_NONBLOCK-)readers that sleep due to * syscall merging. * FIXME! Is this really true? */ do_wakeup = 1; |
f0d1bec9d new helper: copy_... |
415 416 |
copied = copy_page_from_iter(page, 0, PAGE_SIZE, from); if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) { |
341b446bc [PATCH] another r... |
417 |
if (!ret) |
f0d1bec9d new helper: copy_... |
418 |
ret = -EFAULT; |
1da177e4c Linux-2.6.12-rc2 |
419 420 |
break; } |
f0d1bec9d new helper: copy_... |
421 |
ret += copied; |
1da177e4c Linux-2.6.12-rc2 |
422 423 424 425 426 |
/* Insert it into the buffer array */ buf->page = page; buf->ops = &anon_pipe_buf_ops; buf->offset = 0; |
f0d1bec9d new helper: copy_... |
427 |
buf->len = copied; |
9883035ae pipes: add a "pac... |
428 429 430 431 432 |
buf->flags = 0; if (is_packetized(filp)) { buf->ops = &packet_pipe_buf_ops; buf->flags = PIPE_BUF_FLAG_PACKET; } |
923f4f239 [PATCH] pipe.c/fi... |
433 434 |
pipe->nrbufs = ++bufs; pipe->tmp_page = NULL; |
1da177e4c Linux-2.6.12-rc2 |
435 |
|
f0d1bec9d new helper: copy_... |
436 |
if (!iov_iter_count(from)) |
1da177e4c Linux-2.6.12-rc2 |
437 438 |
break; } |
35f3d14db pipe: add support... |
439 |
if (bufs < pipe->buffers) |
1da177e4c Linux-2.6.12-rc2 |
440 441 |
continue; if (filp->f_flags & O_NONBLOCK) { |
341b446bc [PATCH] another r... |
442 443 |
if (!ret) ret = -EAGAIN; |
1da177e4c Linux-2.6.12-rc2 |
444 445 446 |
break; } if (signal_pending(current)) { |
341b446bc [PATCH] another r... |
447 448 |
if (!ret) ret = -ERESTARTSYS; |
1da177e4c Linux-2.6.12-rc2 |
449 450 451 |
break; } if (do_wakeup) { |
28e58ee8c Fix broken "pipe:... |
452 |
wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM); |
923f4f239 [PATCH] pipe.c/fi... |
453 |
kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
1da177e4c Linux-2.6.12-rc2 |
454 455 |
do_wakeup = 0; } |
923f4f239 [PATCH] pipe.c/fi... |
456 457 458 |
pipe->waiting_writers++; pipe_wait(pipe); pipe->waiting_writers--; |
1da177e4c Linux-2.6.12-rc2 |
459 460 |
} out: |
ebec73f47 introduce variant... |
461 |
__pipe_unlock(pipe); |
1da177e4c Linux-2.6.12-rc2 |
462 |
if (do_wakeup) { |
28e58ee8c Fix broken "pipe:... |
463 |
wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM); |
923f4f239 [PATCH] pipe.c/fi... |
464 |
kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
1da177e4c Linux-2.6.12-rc2 |
465 |
} |
7e775f46a fs/pipe.c: skip f... |
466 |
if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) { |
c3b2da314 fs: introduce ino... |
467 468 469 |
int err = file_update_time(filp); if (err) ret = err; |
7e775f46a fs/pipe.c: skip f... |
470 |
sb_end_write(file_inode(filp)->i_sb); |
c3b2da314 fs: introduce ino... |
471 |
} |
1da177e4c Linux-2.6.12-rc2 |
472 473 |
return ret; } |
d59d0b1b8 BKL-Removal: conv... |
474 |
static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
1da177e4c Linux-2.6.12-rc2 |
475 |
{ |
de32ec4cf pipe: set file->p... |
476 |
struct pipe_inode_info *pipe = filp->private_data; |
1da177e4c Linux-2.6.12-rc2 |
477 478 479 480 |
int count, buf, nrbufs; switch (cmd) { case FIONREAD: |
ebec73f47 introduce variant... |
481 |
__pipe_lock(pipe); |
1da177e4c Linux-2.6.12-rc2 |
482 |
count = 0; |
923f4f239 [PATCH] pipe.c/fi... |
483 484 |
buf = pipe->curbuf; nrbufs = pipe->nrbufs; |
1da177e4c Linux-2.6.12-rc2 |
485 |
while (--nrbufs >= 0) { |
923f4f239 [PATCH] pipe.c/fi... |
486 |
count += pipe->bufs[buf].len; |
35f3d14db pipe: add support... |
487 |
buf = (buf+1) & (pipe->buffers - 1); |
1da177e4c Linux-2.6.12-rc2 |
488 |
} |
ebec73f47 introduce variant... |
489 |
__pipe_unlock(pipe); |
923f4f239 [PATCH] pipe.c/fi... |
490 |
|
1da177e4c Linux-2.6.12-rc2 |
491 492 |
return put_user(count, (int __user *)arg); default: |
46ce341b2 pipe: return -ENO... |
493 |
return -ENOIOCTLCMD; |
1da177e4c Linux-2.6.12-rc2 |
494 495 496 497 498 499 500 501 |
} } /* No kernel lock held - fine */ static unsigned int pipe_poll(struct file *filp, poll_table *wait) { unsigned int mask; |
de32ec4cf pipe: set file->p... |
502 |
struct pipe_inode_info *pipe = filp->private_data; |
1da177e4c Linux-2.6.12-rc2 |
503 |
int nrbufs; |
923f4f239 [PATCH] pipe.c/fi... |
504 |
poll_wait(filp, &pipe->wait, wait); |
1da177e4c Linux-2.6.12-rc2 |
505 506 |
/* Reading only -- no need for acquiring the semaphore. */ |
923f4f239 [PATCH] pipe.c/fi... |
507 |
nrbufs = pipe->nrbufs; |
1da177e4c Linux-2.6.12-rc2 |
508 509 510 |
mask = 0; if (filp->f_mode & FMODE_READ) { mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0; |
923f4f239 [PATCH] pipe.c/fi... |
511 |
if (!pipe->writers && filp->f_version != pipe->w_counter) |
1da177e4c Linux-2.6.12-rc2 |
512 513 514 515 |
mask |= POLLHUP; } if (filp->f_mode & FMODE_WRITE) { |
35f3d14db pipe: add support... |
516 |
mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0; |
5e5d7a222 [PATCH] pipe: rem... |
517 518 519 520 |
/* * Most Unices do not set POLLERR for FIFOs but on Linux they * behave exactly like pipes for poll(). */ |
923f4f239 [PATCH] pipe.c/fi... |
521 |
if (!pipe->readers) |
1da177e4c Linux-2.6.12-rc2 |
522 523 524 525 526 |
mask |= POLLERR; } return mask; } |
b0d8d2292 vfs: fix subtle u... |
527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe) { int kill = 0; spin_lock(&inode->i_lock); if (!--pipe->files) { inode->i_pipe = NULL; kill = 1; } spin_unlock(&inode->i_lock); if (kill) free_pipe_info(pipe); } |
1da177e4c Linux-2.6.12-rc2 |
541 |
static int |
599a0ac14 pipe: fold file_o... |
542 |
pipe_release(struct inode *inode, struct file *file) |
1da177e4c Linux-2.6.12-rc2 |
543 |
{ |
b0d8d2292 vfs: fix subtle u... |
544 |
struct pipe_inode_info *pipe = file->private_data; |
923f4f239 [PATCH] pipe.c/fi... |
545 |
|
ebec73f47 introduce variant... |
546 |
__pipe_lock(pipe); |
599a0ac14 pipe: fold file_o... |
547 548 549 550 |
if (file->f_mode & FMODE_READ) pipe->readers--; if (file->f_mode & FMODE_WRITE) pipe->writers--; |
341b446bc [PATCH] another r... |
551 |
|
ba5bb1473 pipe: take alloca... |
552 |
if (pipe->readers || pipe->writers) { |
28e58ee8c Fix broken "pipe:... |
553 |
wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP); |
923f4f239 [PATCH] pipe.c/fi... |
554 555 |
kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
1da177e4c Linux-2.6.12-rc2 |
556 |
} |
ebec73f47 introduce variant... |
557 |
__pipe_unlock(pipe); |
ba5bb1473 pipe: take alloca... |
558 |
|
b0d8d2292 vfs: fix subtle u... |
559 |
put_pipe_info(inode, pipe); |
1da177e4c Linux-2.6.12-rc2 |
560 561 562 563 |
return 0; } static int |
599a0ac14 pipe: fold file_o... |
564 |
pipe_fasync(int fd, struct file *filp, int on) |
1da177e4c Linux-2.6.12-rc2 |
565 |
{ |
de32ec4cf pipe: set file->p... |
566 |
struct pipe_inode_info *pipe = filp->private_data; |
599a0ac14 pipe: fold file_o... |
567 |
int retval = 0; |
1da177e4c Linux-2.6.12-rc2 |
568 |
|
ebec73f47 introduce variant... |
569 |
__pipe_lock(pipe); |
599a0ac14 pipe: fold file_o... |
570 571 572 |
if (filp->f_mode & FMODE_READ) retval = fasync_helper(fd, filp, on, &pipe->fasync_readers); if ((filp->f_mode & FMODE_WRITE) && retval >= 0) { |
341b446bc [PATCH] another r... |
573 |
retval = fasync_helper(fd, filp, on, &pipe->fasync_writers); |
599a0ac14 pipe: fold file_o... |
574 575 |
if (retval < 0 && (filp->f_mode & FMODE_READ)) /* this can happen only if on == T */ |
e5bc49ba7 pipe_rdwr_fasync:... |
576 577 |
fasync_helper(-1, filp, 0, &pipe->fasync_readers); } |
ebec73f47 introduce variant... |
578 |
__pipe_unlock(pipe); |
60aa49243 Rationalize fasyn... |
579 |
return retval; |
1da177e4c Linux-2.6.12-rc2 |
580 |
} |
759c01142 pipe: limit the p... |
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 |
static void account_pipe_buffers(struct pipe_inode_info *pipe, unsigned long old, unsigned long new) { atomic_long_add(new - old, &pipe->user->pipe_bufs); } static bool too_many_pipe_buffers_soft(struct user_struct *user) { return pipe_user_pages_soft && atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_soft; } static bool too_many_pipe_buffers_hard(struct user_struct *user) { return pipe_user_pages_hard && atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_hard; } |
7bee130e2 get rid of alloc_... |
598 |
struct pipe_inode_info *alloc_pipe_info(void) |
3a326a2ce [PATCH] introduce... |
599 |
{ |
923f4f239 [PATCH] pipe.c/fi... |
600 |
struct pipe_inode_info *pipe; |
3a326a2ce [PATCH] introduce... |
601 |
|
d86133bd3 pipe: account to ... |
602 |
pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT); |
923f4f239 [PATCH] pipe.c/fi... |
603 |
if (pipe) { |
759c01142 pipe: limit the p... |
604 605 606 607 608 609 |
unsigned long pipe_bufs = PIPE_DEF_BUFFERS; struct user_struct *user = get_current_user(); if (!too_many_pipe_buffers_hard(user)) { if (too_many_pipe_buffers_soft(user)) pipe_bufs = 1; |
d86133bd3 pipe: account to ... |
610 611 612 |
pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer), GFP_KERNEL_ACCOUNT); |
759c01142 pipe: limit the p... |
613 |
} |
35f3d14db pipe: add support... |
614 615 616 |
if (pipe->bufs) { init_waitqueue_head(&pipe->wait); pipe->r_counter = pipe->w_counter = 1; |
759c01142 pipe: limit the p... |
617 618 619 |
pipe->buffers = pipe_bufs; pipe->user = user; account_pipe_buffers(pipe, 0, pipe_bufs); |
72b0d9aac pipe: don't use -... |
620 |
mutex_init(&pipe->mutex); |
35f3d14db pipe: add support... |
621 622 |
return pipe; } |
759c01142 pipe: limit the p... |
623 |
free_uid(user); |
35f3d14db pipe: add support... |
624 |
kfree(pipe); |
3a326a2ce [PATCH] introduce... |
625 |
} |
35f3d14db pipe: add support... |
626 |
return NULL; |
3a326a2ce [PATCH] introduce... |
627 |
} |
4b8a8f1e4 get rid of the la... |
628 |
void free_pipe_info(struct pipe_inode_info *pipe) |
1da177e4c Linux-2.6.12-rc2 |
629 630 |
{ int i; |
1da177e4c Linux-2.6.12-rc2 |
631 |
|
759c01142 pipe: limit the p... |
632 633 |
account_pipe_buffers(pipe, pipe->buffers, 0); free_uid(pipe->user); |
35f3d14db pipe: add support... |
634 |
for (i = 0; i < pipe->buffers; i++) { |
923f4f239 [PATCH] pipe.c/fi... |
635 |
struct pipe_buffer *buf = pipe->bufs + i; |
1da177e4c Linux-2.6.12-rc2 |
636 |
if (buf->ops) |
923f4f239 [PATCH] pipe.c/fi... |
637 |
buf->ops->release(pipe, buf); |
1da177e4c Linux-2.6.12-rc2 |
638 |
} |
923f4f239 [PATCH] pipe.c/fi... |
639 640 |
if (pipe->tmp_page) __free_page(pipe->tmp_page); |
35f3d14db pipe: add support... |
641 |
kfree(pipe->bufs); |
923f4f239 [PATCH] pipe.c/fi... |
642 |
kfree(pipe); |
1da177e4c Linux-2.6.12-rc2 |
643 |
} |
fa3536cc1 [PATCH] Use __rea... |
644 |
static struct vfsmount *pipe_mnt __read_mostly; |
341b446bc [PATCH] another r... |
645 |
|
c23fbb6bc VFS: delay the de... |
646 647 648 649 650 651 |
/* * 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]", |
75c3cfa85 VFS: assorted wei... |
652 |
d_inode(dentry)->i_ino); |
c23fbb6bc VFS: delay the de... |
653 |
} |
3ba13d179 constify dentry_o... |
654 |
static const struct dentry_operations pipefs_dentry_operations = { |
c23fbb6bc VFS: delay the de... |
655 |
.d_dname = pipefs_dname, |
1da177e4c Linux-2.6.12-rc2 |
656 657 658 659 |
}; static struct inode * get_pipe_inode(void) { |
a209dfc7b vfs: dont chain p... |
660 |
struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb); |
923f4f239 [PATCH] pipe.c/fi... |
661 |
struct pipe_inode_info *pipe; |
1da177e4c Linux-2.6.12-rc2 |
662 663 664 |
if (!inode) goto fail_inode; |
85fe4025c fs: do not assign... |
665 |
inode->i_ino = get_next_ino(); |
7bee130e2 get rid of alloc_... |
666 |
pipe = alloc_pipe_info(); |
923f4f239 [PATCH] pipe.c/fi... |
667 |
if (!pipe) |
1da177e4c Linux-2.6.12-rc2 |
668 |
goto fail_iput; |
3a326a2ce [PATCH] introduce... |
669 |
|
ba5bb1473 pipe: take alloca... |
670 671 |
inode->i_pipe = pipe; pipe->files = 2; |
923f4f239 [PATCH] pipe.c/fi... |
672 |
pipe->readers = pipe->writers = 1; |
599a0ac14 pipe: fold file_o... |
673 |
inode->i_fop = &pipefifo_fops; |
1da177e4c Linux-2.6.12-rc2 |
674 675 676 677 678 679 680 681 682 |
/* * 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 CRED: Wrap task c... |
683 684 |
inode->i_uid = current_fsuid(); inode->i_gid = current_fsgid(); |
1da177e4c Linux-2.6.12-rc2 |
685 |
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
923f4f239 [PATCH] pipe.c/fi... |
686 |
|
1da177e4c Linux-2.6.12-rc2 |
687 688 689 690 |
return inode; fail_iput: iput(inode); |
341b446bc [PATCH] another r... |
691 |
|
1da177e4c Linux-2.6.12-rc2 |
692 693 694 |
fail_inode: return NULL; } |
e4fad8e5d consolidate pipe ... |
695 |
int create_pipe_files(struct file **res, int flags) |
1da177e4c Linux-2.6.12-rc2 |
696 |
{ |
d6cbd281d [PATCH] Some clea... |
697 |
int err; |
e4fad8e5d consolidate pipe ... |
698 |
struct inode *inode = get_pipe_inode(); |
d6cbd281d [PATCH] Some clea... |
699 |
struct file *f; |
2c48b9c45 switch alloc_file... |
700 |
struct path path; |
e4fad8e5d consolidate pipe ... |
701 |
static struct qstr name = { .name = "" }; |
1da177e4c Linux-2.6.12-rc2 |
702 |
|
1da177e4c Linux-2.6.12-rc2 |
703 |
if (!inode) |
e4fad8e5d consolidate pipe ... |
704 |
return -ENFILE; |
1da177e4c Linux-2.6.12-rc2 |
705 |
|
d6cbd281d [PATCH] Some clea... |
706 |
err = -ENOMEM; |
4b936885a fs: improve scala... |
707 |
path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name); |
2c48b9c45 switch alloc_file... |
708 |
if (!path.dentry) |
d6cbd281d [PATCH] Some clea... |
709 |
goto err_inode; |
2c48b9c45 switch alloc_file... |
710 |
path.mnt = mntget(pipe_mnt); |
341b446bc [PATCH] another r... |
711 |
|
2c48b9c45 switch alloc_file... |
712 |
d_instantiate(path.dentry, inode); |
430e285e0 [PATCH] fix up ne... |
713 |
|
599a0ac14 pipe: fold file_o... |
714 |
f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops); |
e9bb1f9b1 fs/pipe.c: preser... |
715 716 |
if (IS_ERR(f)) { err = PTR_ERR(f); |
430e285e0 [PATCH] fix up ne... |
717 |
goto err_dentry; |
e9bb1f9b1 fs/pipe.c: preser... |
718 |
} |
341b446bc [PATCH] another r... |
719 |
|
9883035ae pipes: add a "pac... |
720 |
f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)); |
de32ec4cf pipe: set file->p... |
721 |
f->private_data = inode->i_pipe; |
d6cbd281d [PATCH] Some clea... |
722 |
|
599a0ac14 pipe: fold file_o... |
723 |
res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops); |
e9bb1f9b1 fs/pipe.c: preser... |
724 725 |
if (IS_ERR(res[0])) { err = PTR_ERR(res[0]); |
e4fad8e5d consolidate pipe ... |
726 |
goto err_file; |
e9bb1f9b1 fs/pipe.c: preser... |
727 |
} |
e4fad8e5d consolidate pipe ... |
728 729 |
path_get(&path); |
de32ec4cf pipe: set file->p... |
730 |
res[0]->private_data = inode->i_pipe; |
e4fad8e5d consolidate pipe ... |
731 732 733 |
res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK); res[1] = f; return 0; |
1da177e4c Linux-2.6.12-rc2 |
734 |
|
e4fad8e5d consolidate pipe ... |
735 736 737 |
err_file: put_filp(f); err_dentry: |
4b8a8f1e4 get rid of the la... |
738 |
free_pipe_info(inode->i_pipe); |
2c48b9c45 switch alloc_file... |
739 |
path_put(&path); |
e4fad8e5d consolidate pipe ... |
740 |
return err; |
ed1524371 [PATCH] double-fr... |
741 |
|
e4fad8e5d consolidate pipe ... |
742 |
err_inode: |
4b8a8f1e4 get rid of the la... |
743 |
free_pipe_info(inode->i_pipe); |
1da177e4c Linux-2.6.12-rc2 |
744 |
iput(inode); |
e4fad8e5d consolidate pipe ... |
745 |
return err; |
d6cbd281d [PATCH] Some clea... |
746 |
} |
5b249b1b0 pipe(2) - race-fr... |
747 |
static int __do_pipe_flags(int *fd, struct file **files, int flags) |
d6cbd281d [PATCH] Some clea... |
748 |
{ |
d6cbd281d [PATCH] Some clea... |
749 750 |
int error; int fdw, fdr; |
9883035ae pipes: add a "pac... |
751 |
if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT)) |
ed8cae8ba flag parameters: ... |
752 |
return -EINVAL; |
e4fad8e5d consolidate pipe ... |
753 754 755 |
error = create_pipe_files(files, flags); if (error) return error; |
d6cbd281d [PATCH] Some clea... |
756 |
|
ed8cae8ba flag parameters: ... |
757 |
error = get_unused_fd_flags(flags); |
d6cbd281d [PATCH] Some clea... |
758 759 760 |
if (error < 0) goto err_read_pipe; fdr = error; |
ed8cae8ba flag parameters: ... |
761 |
error = get_unused_fd_flags(flags); |
d6cbd281d [PATCH] Some clea... |
762 763 764 |
if (error < 0) goto err_fdr; fdw = error; |
157cf649a sanitize audit_fd... |
765 |
audit_fd_pair(fdr, fdw); |
d6cbd281d [PATCH] Some clea... |
766 767 |
fd[0] = fdr; fd[1] = fdw; |
d6cbd281d [PATCH] Some clea... |
768 769 770 771 772 |
return 0; err_fdr: put_unused_fd(fdr); err_read_pipe: |
e4fad8e5d consolidate pipe ... |
773 774 |
fput(files[0]); fput(files[1]); |
d6cbd281d [PATCH] Some clea... |
775 |
return error; |
1da177e4c Linux-2.6.12-rc2 |
776 |
} |
5b249b1b0 pipe(2) - race-fr... |
777 778 779 780 781 782 783 784 785 786 |
int do_pipe_flags(int *fd, int flags) { struct file *files[2]; int error = __do_pipe_flags(fd, files, flags); if (!error) { fd_install(fd[0], files[0]); fd_install(fd[1], files[1]); } return error; } |
1da177e4c Linux-2.6.12-rc2 |
787 |
/* |
d35c7b0e5 unified (weak) sy... |
788 789 790 |
* sys_pipe() is the normal C calling standard for creating * a pipe. It's not the way Unix traditionally does this, though. */ |
d4e82042c [CVE-2009-0029] S... |
791 |
SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags) |
d35c7b0e5 unified (weak) sy... |
792 |
{ |
5b249b1b0 pipe(2) - race-fr... |
793 |
struct file *files[2]; |
d35c7b0e5 unified (weak) sy... |
794 795 |
int fd[2]; int error; |
5b249b1b0 pipe(2) - race-fr... |
796 |
error = __do_pipe_flags(fd, files, flags); |
d35c7b0e5 unified (weak) sy... |
797 |
if (!error) { |
5b249b1b0 pipe(2) - race-fr... |
798 799 800 801 802 |
if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) { fput(files[0]); fput(files[1]); put_unused_fd(fd[0]); put_unused_fd(fd[1]); |
d35c7b0e5 unified (weak) sy... |
803 |
error = -EFAULT; |
5b249b1b0 pipe(2) - race-fr... |
804 805 806 |
} else { fd_install(fd[0], files[0]); fd_install(fd[1], files[1]); |
ba719baea sys_pipe(): fix f... |
807 |
} |
d35c7b0e5 unified (weak) sy... |
808 809 810 |
} return error; } |
2b6642199 [CVE-2009-0029] S... |
811 |
SYSCALL_DEFINE1(pipe, int __user *, fildes) |
ed8cae8ba flag parameters: ... |
812 813 814 |
{ return sys_pipe2(fildes, 0); } |
fc7478a2b pipe: switch wait... |
815 |
static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt) |
f776c7388 fold fifo.c into ... |
816 817 818 819 |
{ int cur = *cnt; while (cur == *cnt) { |
fc7478a2b pipe: switch wait... |
820 |
pipe_wait(pipe); |
f776c7388 fold fifo.c into ... |
821 822 823 824 825 |
if (signal_pending(current)) break; } return cur == *cnt ? -ERESTARTSYS : 0; } |
fc7478a2b pipe: switch wait... |
826 |
static void wake_up_partner(struct pipe_inode_info *pipe) |
f776c7388 fold fifo.c into ... |
827 |
{ |
fc7478a2b pipe: switch wait... |
828 |
wake_up_interruptible(&pipe->wait); |
f776c7388 fold fifo.c into ... |
829 830 831 832 833 |
} static int fifo_open(struct inode *inode, struct file *filp) { struct pipe_inode_info *pipe; |
599a0ac14 pipe: fold file_o... |
834 |
bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC; |
f776c7388 fold fifo.c into ... |
835 |
int ret; |
ba5bb1473 pipe: take alloca... |
836 837 838 839 840 841 842 843 844 |
filp->f_version = 0; spin_lock(&inode->i_lock); if (inode->i_pipe) { pipe = inode->i_pipe; pipe->files++; spin_unlock(&inode->i_lock); } else { spin_unlock(&inode->i_lock); |
7bee130e2 get rid of alloc_... |
845 |
pipe = alloc_pipe_info(); |
f776c7388 fold fifo.c into ... |
846 |
if (!pipe) |
ba5bb1473 pipe: take alloca... |
847 848 849 850 851 852 |
return -ENOMEM; pipe->files = 1; spin_lock(&inode->i_lock); if (unlikely(inode->i_pipe)) { inode->i_pipe->files++; spin_unlock(&inode->i_lock); |
4b8a8f1e4 get rid of the la... |
853 |
free_pipe_info(pipe); |
ba5bb1473 pipe: take alloca... |
854 855 856 857 858 |
pipe = inode->i_pipe; } else { inode->i_pipe = pipe; spin_unlock(&inode->i_lock); } |
f776c7388 fold fifo.c into ... |
859 |
} |
de32ec4cf pipe: set file->p... |
860 |
filp->private_data = pipe; |
ba5bb1473 pipe: take alloca... |
861 |
/* OK, we have a pipe and it's pinned down */ |
ebec73f47 introduce variant... |
862 |
__pipe_lock(pipe); |
f776c7388 fold fifo.c into ... |
863 864 865 866 867 868 869 870 871 872 873 |
/* We can only do regular read/write on fifos */ filp->f_mode &= (FMODE_READ | FMODE_WRITE); switch (filp->f_mode) { case FMODE_READ: /* * O_RDONLY * POSIX.1 says that O_NONBLOCK means return with the FIFO * opened, even when there is no process writing the FIFO. */ |
f776c7388 fold fifo.c into ... |
874 875 |
pipe->r_counter++; if (pipe->readers++ == 0) |
fc7478a2b pipe: switch wait... |
876 |
wake_up_partner(pipe); |
f776c7388 fold fifo.c into ... |
877 |
|
599a0ac14 pipe: fold file_o... |
878 |
if (!is_pipe && !pipe->writers) { |
f776c7388 fold fifo.c into ... |
879 880 881 882 883 |
if ((filp->f_flags & O_NONBLOCK)) { /* suppress POLLHUP until we have * seen a writer */ filp->f_version = pipe->w_counter; } else { |
fc7478a2b pipe: switch wait... |
884 |
if (wait_for_partner(pipe, &pipe->w_counter)) |
f776c7388 fold fifo.c into ... |
885 886 887 888 889 890 891 892 893 894 895 896 |
goto err_rd; } } break; case FMODE_WRITE: /* * O_WRONLY * POSIX.1 says that O_NONBLOCK means return -1 with * errno=ENXIO when there is no process reading the FIFO. */ ret = -ENXIO; |
599a0ac14 pipe: fold file_o... |
897 |
if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers) |
f776c7388 fold fifo.c into ... |
898 |
goto err; |
f776c7388 fold fifo.c into ... |
899 900 |
pipe->w_counter++; if (!pipe->writers++) |
fc7478a2b pipe: switch wait... |
901 |
wake_up_partner(pipe); |
f776c7388 fold fifo.c into ... |
902 |
|
599a0ac14 pipe: fold file_o... |
903 |
if (!is_pipe && !pipe->readers) { |
fc7478a2b pipe: switch wait... |
904 |
if (wait_for_partner(pipe, &pipe->r_counter)) |
f776c7388 fold fifo.c into ... |
905 906 907 908 909 910 911 912 913 914 915 |
goto err_wr; } break; case FMODE_READ | FMODE_WRITE: /* * O_RDWR * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set. * This implementation will NEVER block on a O_RDWR open, since * the process can at least talk to itself. */ |
f776c7388 fold fifo.c into ... |
916 917 918 919 920 921 |
pipe->readers++; pipe->writers++; pipe->r_counter++; pipe->w_counter++; if (pipe->readers == 1 || pipe->writers == 1) |
fc7478a2b pipe: switch wait... |
922 |
wake_up_partner(pipe); |
f776c7388 fold fifo.c into ... |
923 924 925 926 927 928 929 930 |
break; default: ret = -EINVAL; goto err; } /* Ok! */ |
ebec73f47 introduce variant... |
931 |
__pipe_unlock(pipe); |
f776c7388 fold fifo.c into ... |
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 |
return 0; err_rd: if (!--pipe->readers) wake_up_interruptible(&pipe->wait); ret = -ERESTARTSYS; goto err; err_wr: if (!--pipe->writers) wake_up_interruptible(&pipe->wait); ret = -ERESTARTSYS; goto err; err: |
ebec73f47 introduce variant... |
947 |
__pipe_unlock(pipe); |
b0d8d2292 vfs: fix subtle u... |
948 949 |
put_pipe_info(inode, pipe); |
f776c7388 fold fifo.c into ... |
950 951 |
return ret; } |
599a0ac14 pipe: fold file_o... |
952 953 954 |
const struct file_operations pipefifo_fops = { .open = fifo_open, .llseek = no_llseek, |
fb9096a34 pipe: switch to -... |
955 |
.read_iter = pipe_read, |
f0d1bec9d new helper: copy_... |
956 |
.write_iter = pipe_write, |
599a0ac14 pipe: fold file_o... |
957 958 959 960 |
.poll = pipe_poll, .unlocked_ioctl = pipe_ioctl, .release = pipe_release, .fasync = pipe_fasync, |
f776c7388 fold fifo.c into ... |
961 |
}; |
d35c7b0e5 unified (weak) sy... |
962 |
/* |
35f3d14db pipe: add support... |
963 964 965 |
* Allocate a new array of pipe buffers and copy the info over. Returns the * pipe size if successful, or return -ERROR on error. */ |
b9598db34 pipe: make F_{GET... |
966 |
static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages) |
35f3d14db pipe: add support... |
967 968 969 970 |
{ struct pipe_buffer *bufs; /* |
35f3d14db pipe: add support... |
971 972 973 974 975 |
* 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 pipe: make F_{GET... |
976 |
if (nr_pages < pipe->nrbufs) |
35f3d14db pipe: add support... |
977 |
return -EBUSY; |
d86133bd3 pipe: account to ... |
978 979 |
bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL_ACCOUNT | __GFP_NOWARN); |
35f3d14db pipe: add support... |
980 981 982 983 984 985 986 987 |
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 pipe: fix pipe bu... |
988 989 |
unsigned int tail; unsigned int head; |
35f3d14db pipe: add support... |
990 |
|
1d862f412 pipe: fix pipe bu... |
991 992 993 994 995 996 997 |
tail = pipe->curbuf + pipe->nrbufs; if (tail < pipe->buffers) tail = 0; else tail &= (pipe->buffers - 1); head = pipe->nrbufs - tail; |
35f3d14db pipe: add support... |
998 999 1000 |
if (head) memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer)); if (tail) |
1d862f412 pipe: fix pipe bu... |
1001 |
memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer)); |
35f3d14db pipe: add support... |
1002 |
} |
759c01142 pipe: limit the p... |
1003 |
account_pipe_buffers(pipe, pipe->buffers, nr_pages); |
35f3d14db pipe: add support... |
1004 1005 1006 |
pipe->curbuf = 0; kfree(pipe->bufs); pipe->bufs = bufs; |
b9598db34 pipe: make F_{GET... |
1007 1008 |
pipe->buffers = nr_pages; return nr_pages * PAGE_SIZE; |
35f3d14db pipe: add support... |
1009 |
} |
ff9da691c pipe: change /pro... |
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 |
/* * 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; } |
720836465 Un-inline get_pip... |
1038 1039 1040 1041 1042 1043 1044 |
/* * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same * location, so checking ->i_pipe is not enough to verify that this is a * pipe. */ struct pipe_inode_info *get_pipe_info(struct file *file) { |
de32ec4cf pipe: set file->p... |
1045 |
return file->f_op == &pipefifo_fops ? file->private_data : NULL; |
720836465 Un-inline get_pip... |
1046 |
} |
35f3d14db pipe: add support... |
1047 1048 1049 1050 |
long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg) { struct pipe_inode_info *pipe; long ret; |
c66fb3479 Export 'get_pipe_... |
1051 |
pipe = get_pipe_info(file); |
35f3d14db pipe: add support... |
1052 1053 |
if (!pipe) return -EBADF; |
ebec73f47 introduce variant... |
1054 |
__pipe_lock(pipe); |
35f3d14db pipe: add support... |
1055 1056 |
switch (cmd) { |
b9598db34 pipe: make F_{GET... |
1057 |
case F_SETPIPE_SZ: { |
ff9da691c pipe: change /pro... |
1058 |
unsigned int size, nr_pages; |
b9598db34 pipe: make F_{GET... |
1059 |
|
ff9da691c pipe: change /pro... |
1060 1061 |
size = round_pipe_size(arg); nr_pages = size >> PAGE_SHIFT; |
b9598db34 pipe: make F_{GET... |
1062 |
|
6db40cf04 pipe: fix check i... |
1063 1064 1065 |
ret = -EINVAL; if (!nr_pages) goto out; |
ff9da691c pipe: change /pro... |
1066 |
if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) { |
b4ca76157 Merge branch 'mas... |
1067 |
ret = -EPERM; |
cc967be54 fs: Add missing m... |
1068 |
goto out; |
759c01142 pipe: limit the p... |
1069 1070 1071 1072 1073 |
} else if ((too_many_pipe_buffers_hard(pipe->user) || too_many_pipe_buffers_soft(pipe->user)) && !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { ret = -EPERM; goto out; |
cc967be54 fs: Add missing m... |
1074 |
} |
ff9da691c pipe: change /pro... |
1075 |
ret = pipe_set_size(pipe, nr_pages); |
35f3d14db pipe: add support... |
1076 |
break; |
b9598db34 pipe: make F_{GET... |
1077 |
} |
35f3d14db pipe: add support... |
1078 |
case F_GETPIPE_SZ: |
b9598db34 pipe: make F_{GET... |
1079 |
ret = pipe->buffers * PAGE_SIZE; |
35f3d14db pipe: add support... |
1080 1081 1082 1083 1084 |
break; default: ret = -EINVAL; break; } |
cc967be54 fs: Add missing m... |
1085 |
out: |
ebec73f47 introduce variant... |
1086 |
__pipe_unlock(pipe); |
35f3d14db pipe: add support... |
1087 1088 |
return ret; } |
ff0c7d15f fs: avoid inode R... |
1089 1090 |
static const struct super_operations pipefs_ops = { .destroy_inode = free_inode_nonrcu, |
d70ef97ba fs/pipe.c: add ->... |
1091 |
.statfs = simple_statfs, |
ff0c7d15f fs: avoid inode R... |
1092 |
}; |
35f3d14db pipe: add support... |
1093 |
/* |
1da177e4c Linux-2.6.12-rc2 |
1094 1095 1096 1097 1098 |
* 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. */ |
51139adac convert get_sb_ps... |
1099 1100 |
static struct dentry *pipefs_mount(struct file_system_type *fs_type, int flags, const char *dev_name, void *data) |
1da177e4c Linux-2.6.12-rc2 |
1101 |
{ |
c74a1cbb3 pass default dent... |
1102 1103 |
return mount_pseudo(fs_type, "pipe:", &pipefs_ops, &pipefs_dentry_operations, PIPEFS_MAGIC); |
1da177e4c Linux-2.6.12-rc2 |
1104 1105 1106 1107 |
} static struct file_system_type pipe_fs_type = { .name = "pipefs", |
51139adac convert get_sb_ps... |
1108 |
.mount = pipefs_mount, |
1da177e4c Linux-2.6.12-rc2 |
1109 1110 1111 1112 1113 1114 |
.kill_sb = kill_anon_super, }; static int __init init_pipe_fs(void) { int err = register_filesystem(&pipe_fs_type); |
341b446bc [PATCH] another r... |
1115 |
|
1da177e4c Linux-2.6.12-rc2 |
1116 1117 1118 1119 1120 1121 1122 1123 1124 |
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; } |
1da177e4c Linux-2.6.12-rc2 |
1125 |
fs_initcall(init_pipe_fs); |