pipe.c 28 KB

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