pipe.c 25 KB

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