pipe.c 28 KB

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