pipe.c 27 KB

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