pipe.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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_PARENT);
  64. pipe_lock_nested(pipe1, I_MUTEX_CHILD);
  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. /**
  271. * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
  272. * @pipe: the pipe that the buffer belongs to
  273. * @buf: the buffer to put a reference to
  274. *
  275. * Description:
  276. * This function releases a reference to @buf.
  277. */
  278. void generic_pipe_buf_release(struct pipe_inode_info *pipe,
  279. struct pipe_buffer *buf)
  280. {
  281. page_cache_release(buf->page);
  282. }
  283. static const struct pipe_buf_operations anon_pipe_buf_ops = {
  284. .can_merge = 1,
  285. .map = generic_pipe_buf_map,
  286. .unmap = generic_pipe_buf_unmap,
  287. .confirm = generic_pipe_buf_confirm,
  288. .release = anon_pipe_buf_release,
  289. .steal = generic_pipe_buf_steal,
  290. .get = generic_pipe_buf_get,
  291. };
  292. static ssize_t
  293. pipe_read(struct kiocb *iocb, const struct iovec *_iov,
  294. unsigned long nr_segs, loff_t pos)
  295. {
  296. struct file *filp = iocb->ki_filp;
  297. struct inode *inode = filp->f_path.dentry->d_inode;
  298. struct pipe_inode_info *pipe;
  299. int do_wakeup;
  300. ssize_t ret;
  301. struct iovec *iov = (struct iovec *)_iov;
  302. size_t total_len;
  303. total_len = iov_length(iov, nr_segs);
  304. /* Null read succeeds. */
  305. if (unlikely(total_len == 0))
  306. return 0;
  307. do_wakeup = 0;
  308. ret = 0;
  309. mutex_lock(&inode->i_mutex);
  310. pipe = inode->i_pipe;
  311. for (;;) {
  312. int bufs = pipe->nrbufs;
  313. if (bufs) {
  314. int curbuf = pipe->curbuf;
  315. struct pipe_buffer *buf = pipe->bufs + curbuf;
  316. const struct pipe_buf_operations *ops = buf->ops;
  317. void *addr;
  318. size_t chars = buf->len;
  319. int error, atomic;
  320. if (chars > total_len)
  321. chars = total_len;
  322. error = ops->confirm(pipe, buf);
  323. if (error) {
  324. if (!ret)
  325. error = ret;
  326. break;
  327. }
  328. atomic = !iov_fault_in_pages_write(iov, chars);
  329. redo:
  330. addr = ops->map(pipe, buf, atomic);
  331. error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars, atomic);
  332. ops->unmap(pipe, buf, addr);
  333. if (unlikely(error)) {
  334. /*
  335. * Just retry with the slow path if we failed.
  336. */
  337. if (atomic) {
  338. atomic = 0;
  339. goto redo;
  340. }
  341. if (!ret)
  342. ret = error;
  343. break;
  344. }
  345. ret += chars;
  346. buf->offset += chars;
  347. buf->len -= chars;
  348. if (!buf->len) {
  349. buf->ops = NULL;
  350. ops->release(pipe, buf);
  351. curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
  352. pipe->curbuf = curbuf;
  353. pipe->nrbufs = --bufs;
  354. do_wakeup = 1;
  355. }
  356. total_len -= chars;
  357. if (!total_len)
  358. break; /* common path: read succeeded */
  359. }
  360. if (bufs) /* More to do? */
  361. continue;
  362. if (!pipe->writers)
  363. break;
  364. if (!pipe->waiting_writers) {
  365. /* syscall merging: Usually we must not sleep
  366. * if O_NONBLOCK is set, or if we got some data.
  367. * But if a writer sleeps in kernel space, then
  368. * we can wait for that data without violating POSIX.
  369. */
  370. if (ret)
  371. break;
  372. if (filp->f_flags & O_NONBLOCK) {
  373. ret = -EAGAIN;
  374. break;
  375. }
  376. }
  377. if (signal_pending(current)) {
  378. if (!ret)
  379. ret = -ERESTARTSYS;
  380. break;
  381. }
  382. if (do_wakeup) {
  383. wake_up_interruptible_sync(&pipe->wait);
  384. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  385. }
  386. pipe_wait(pipe);
  387. }
  388. mutex_unlock(&inode->i_mutex);
  389. /* Signal writers asynchronously that there is more room. */
  390. if (do_wakeup) {
  391. wake_up_interruptible_sync(&pipe->wait);
  392. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  393. }
  394. if (ret > 0)
  395. file_accessed(filp);
  396. return ret;
  397. }
  398. static ssize_t
  399. pipe_write(struct kiocb *iocb, const struct iovec *_iov,
  400. unsigned long nr_segs, loff_t ppos)
  401. {
  402. struct file *filp = iocb->ki_filp;
  403. struct inode *inode = filp->f_path.dentry->d_inode;
  404. struct pipe_inode_info *pipe;
  405. ssize_t ret;
  406. int do_wakeup;
  407. struct iovec *iov = (struct iovec *)_iov;
  408. size_t total_len;
  409. ssize_t chars;
  410. total_len = iov_length(iov, nr_segs);
  411. /* Null write succeeds. */
  412. if (unlikely(total_len == 0))
  413. return 0;
  414. do_wakeup = 0;
  415. ret = 0;
  416. mutex_lock(&inode->i_mutex);
  417. pipe = inode->i_pipe;
  418. if (!pipe->readers) {
  419. send_sig(SIGPIPE, current, 0);
  420. ret = -EPIPE;
  421. goto out;
  422. }
  423. /* We try to merge small writes */
  424. chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
  425. if (pipe->nrbufs && chars != 0) {
  426. int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
  427. (PIPE_BUFFERS-1);
  428. struct pipe_buffer *buf = pipe->bufs + lastbuf;
  429. const struct pipe_buf_operations *ops = buf->ops;
  430. int offset = buf->offset + buf->len;
  431. if (ops->can_merge && offset + chars <= PAGE_SIZE) {
  432. int error, atomic = 1;
  433. void *addr;
  434. error = ops->confirm(pipe, buf);
  435. if (error)
  436. goto out;
  437. iov_fault_in_pages_read(iov, chars);
  438. redo1:
  439. addr = ops->map(pipe, buf, atomic);
  440. error = pipe_iov_copy_from_user(offset + addr, iov,
  441. chars, atomic);
  442. ops->unmap(pipe, buf, addr);
  443. ret = error;
  444. do_wakeup = 1;
  445. if (error) {
  446. if (atomic) {
  447. atomic = 0;
  448. goto redo1;
  449. }
  450. goto out;
  451. }
  452. buf->len += chars;
  453. total_len -= chars;
  454. ret = chars;
  455. if (!total_len)
  456. goto out;
  457. }
  458. }
  459. for (;;) {
  460. int bufs;
  461. if (!pipe->readers) {
  462. send_sig(SIGPIPE, current, 0);
  463. if (!ret)
  464. ret = -EPIPE;
  465. break;
  466. }
  467. bufs = pipe->nrbufs;
  468. if (bufs < PIPE_BUFFERS) {
  469. int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
  470. struct pipe_buffer *buf = pipe->bufs + newbuf;
  471. struct page *page = pipe->tmp_page;
  472. char *src;
  473. int error, atomic = 1;
  474. if (!page) {
  475. page = alloc_page(GFP_HIGHUSER);
  476. if (unlikely(!page)) {
  477. ret = ret ? : -ENOMEM;
  478. break;
  479. }
  480. pipe->tmp_page = page;
  481. }
  482. /* Always wake up, even if the copy fails. Otherwise
  483. * we lock up (O_NONBLOCK-)readers that sleep due to
  484. * syscall merging.
  485. * FIXME! Is this really true?
  486. */
  487. do_wakeup = 1;
  488. chars = PAGE_SIZE;
  489. if (chars > total_len)
  490. chars = total_len;
  491. iov_fault_in_pages_read(iov, chars);
  492. redo2:
  493. if (atomic)
  494. src = kmap_atomic(page, KM_USER0);
  495. else
  496. src = kmap(page);
  497. error = pipe_iov_copy_from_user(src, iov, chars,
  498. atomic);
  499. if (atomic)
  500. kunmap_atomic(src, KM_USER0);
  501. else
  502. kunmap(page);
  503. if (unlikely(error)) {
  504. if (atomic) {
  505. atomic = 0;
  506. goto redo2;
  507. }
  508. if (!ret)
  509. ret = error;
  510. break;
  511. }
  512. ret += chars;
  513. /* Insert it into the buffer array */
  514. buf->page = page;
  515. buf->ops = &anon_pipe_buf_ops;
  516. buf->offset = 0;
  517. buf->len = chars;
  518. pipe->nrbufs = ++bufs;
  519. pipe->tmp_page = NULL;
  520. total_len -= chars;
  521. if (!total_len)
  522. break;
  523. }
  524. if (bufs < PIPE_BUFFERS)
  525. continue;
  526. if (filp->f_flags & O_NONBLOCK) {
  527. if (!ret)
  528. ret = -EAGAIN;
  529. break;
  530. }
  531. if (signal_pending(current)) {
  532. if (!ret)
  533. ret = -ERESTARTSYS;
  534. break;
  535. }
  536. if (do_wakeup) {
  537. wake_up_interruptible_sync(&pipe->wait);
  538. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  539. do_wakeup = 0;
  540. }
  541. pipe->waiting_writers++;
  542. pipe_wait(pipe);
  543. pipe->waiting_writers--;
  544. }
  545. out:
  546. mutex_unlock(&inode->i_mutex);
  547. if (do_wakeup) {
  548. wake_up_interruptible_sync(&pipe->wait);
  549. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  550. }
  551. if (ret > 0)
  552. file_update_time(filp);
  553. return ret;
  554. }
  555. static ssize_t
  556. bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
  557. {
  558. return -EBADF;
  559. }
  560. static ssize_t
  561. bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
  562. loff_t *ppos)
  563. {
  564. return -EBADF;
  565. }
  566. static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  567. {
  568. struct inode *inode = filp->f_path.dentry->d_inode;
  569. struct pipe_inode_info *pipe;
  570. int count, buf, nrbufs;
  571. switch (cmd) {
  572. case FIONREAD:
  573. mutex_lock(&inode->i_mutex);
  574. pipe = inode->i_pipe;
  575. count = 0;
  576. buf = pipe->curbuf;
  577. nrbufs = pipe->nrbufs;
  578. while (--nrbufs >= 0) {
  579. count += pipe->bufs[buf].len;
  580. buf = (buf+1) & (PIPE_BUFFERS-1);
  581. }
  582. mutex_unlock(&inode->i_mutex);
  583. return put_user(count, (int __user *)arg);
  584. default:
  585. return -EINVAL;
  586. }
  587. }
  588. /* No kernel lock held - fine */
  589. static unsigned int
  590. pipe_poll(struct file *filp, poll_table *wait)
  591. {
  592. unsigned int mask;
  593. struct inode *inode = filp->f_path.dentry->d_inode;
  594. struct pipe_inode_info *pipe = inode->i_pipe;
  595. int nrbufs;
  596. poll_wait(filp, &pipe->wait, wait);
  597. /* Reading only -- no need for acquiring the semaphore. */
  598. nrbufs = pipe->nrbufs;
  599. mask = 0;
  600. if (filp->f_mode & FMODE_READ) {
  601. mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
  602. if (!pipe->writers && filp->f_version != pipe->w_counter)
  603. mask |= POLLHUP;
  604. }
  605. if (filp->f_mode & FMODE_WRITE) {
  606. mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
  607. /*
  608. * Most Unices do not set POLLERR for FIFOs but on Linux they
  609. * behave exactly like pipes for poll().
  610. */
  611. if (!pipe->readers)
  612. mask |= POLLERR;
  613. }
  614. return mask;
  615. }
  616. static int
  617. pipe_release(struct inode *inode, int decr, int decw)
  618. {
  619. struct pipe_inode_info *pipe;
  620. mutex_lock(&inode->i_mutex);
  621. pipe = inode->i_pipe;
  622. pipe->readers -= decr;
  623. pipe->writers -= decw;
  624. if (!pipe->readers && !pipe->writers) {
  625. free_pipe_info(inode);
  626. } else {
  627. wake_up_interruptible_sync(&pipe->wait);
  628. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  629. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  630. }
  631. mutex_unlock(&inode->i_mutex);
  632. return 0;
  633. }
  634. static int
  635. pipe_read_fasync(int fd, struct file *filp, int on)
  636. {
  637. struct inode *inode = filp->f_path.dentry->d_inode;
  638. int retval;
  639. mutex_lock(&inode->i_mutex);
  640. retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
  641. mutex_unlock(&inode->i_mutex);
  642. return retval;
  643. }
  644. static int
  645. pipe_write_fasync(int fd, struct file *filp, int on)
  646. {
  647. struct inode *inode = filp->f_path.dentry->d_inode;
  648. int retval;
  649. mutex_lock(&inode->i_mutex);
  650. retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
  651. mutex_unlock(&inode->i_mutex);
  652. return retval;
  653. }
  654. static int
  655. pipe_rdwr_fasync(int fd, struct file *filp, int on)
  656. {
  657. struct inode *inode = filp->f_path.dentry->d_inode;
  658. struct pipe_inode_info *pipe = inode->i_pipe;
  659. int retval;
  660. mutex_lock(&inode->i_mutex);
  661. retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
  662. if (retval >= 0) {
  663. retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
  664. if (retval < 0) /* this can happen only if on == T */
  665. fasync_helper(-1, filp, 0, &pipe->fasync_readers);
  666. }
  667. mutex_unlock(&inode->i_mutex);
  668. return retval;
  669. }
  670. static int
  671. pipe_read_release(struct inode *inode, struct file *filp)
  672. {
  673. return pipe_release(inode, 1, 0);
  674. }
  675. static int
  676. pipe_write_release(struct inode *inode, struct file *filp)
  677. {
  678. return pipe_release(inode, 0, 1);
  679. }
  680. static int
  681. pipe_rdwr_release(struct inode *inode, struct file *filp)
  682. {
  683. int decr, decw;
  684. decr = (filp->f_mode & FMODE_READ) != 0;
  685. decw = (filp->f_mode & FMODE_WRITE) != 0;
  686. return pipe_release(inode, decr, decw);
  687. }
  688. static int
  689. pipe_read_open(struct inode *inode, struct file *filp)
  690. {
  691. int ret = -ENOENT;
  692. mutex_lock(&inode->i_mutex);
  693. if (inode->i_pipe) {
  694. ret = 0;
  695. inode->i_pipe->readers++;
  696. }
  697. mutex_unlock(&inode->i_mutex);
  698. return ret;
  699. }
  700. static int
  701. pipe_write_open(struct inode *inode, struct file *filp)
  702. {
  703. int ret = -ENOENT;
  704. mutex_lock(&inode->i_mutex);
  705. if (inode->i_pipe) {
  706. ret = 0;
  707. inode->i_pipe->writers++;
  708. }
  709. mutex_unlock(&inode->i_mutex);
  710. return ret;
  711. }
  712. static int
  713. pipe_rdwr_open(struct inode *inode, struct file *filp)
  714. {
  715. int ret = -ENOENT;
  716. mutex_lock(&inode->i_mutex);
  717. if (inode->i_pipe) {
  718. ret = 0;
  719. if (filp->f_mode & FMODE_READ)
  720. inode->i_pipe->readers++;
  721. if (filp->f_mode & FMODE_WRITE)
  722. inode->i_pipe->writers++;
  723. }
  724. mutex_unlock(&inode->i_mutex);
  725. return ret;
  726. }
  727. /*
  728. * The file_operations structs are not static because they
  729. * are also used in linux/fs/fifo.c to do operations on FIFOs.
  730. *
  731. * Pipes reuse fifos' file_operations structs.
  732. */
  733. const struct file_operations read_pipefifo_fops = {
  734. .llseek = no_llseek,
  735. .read = do_sync_read,
  736. .aio_read = pipe_read,
  737. .write = bad_pipe_w,
  738. .poll = pipe_poll,
  739. .unlocked_ioctl = pipe_ioctl,
  740. .open = pipe_read_open,
  741. .release = pipe_read_release,
  742. .fasync = pipe_read_fasync,
  743. };
  744. const struct file_operations write_pipefifo_fops = {
  745. .llseek = no_llseek,
  746. .read = bad_pipe_r,
  747. .write = do_sync_write,
  748. .aio_write = pipe_write,
  749. .poll = pipe_poll,
  750. .unlocked_ioctl = pipe_ioctl,
  751. .open = pipe_write_open,
  752. .release = pipe_write_release,
  753. .fasync = pipe_write_fasync,
  754. };
  755. const struct file_operations rdwr_pipefifo_fops = {
  756. .llseek = no_llseek,
  757. .read = do_sync_read,
  758. .aio_read = pipe_read,
  759. .write = do_sync_write,
  760. .aio_write = pipe_write,
  761. .poll = pipe_poll,
  762. .unlocked_ioctl = pipe_ioctl,
  763. .open = pipe_rdwr_open,
  764. .release = pipe_rdwr_release,
  765. .fasync = pipe_rdwr_fasync,
  766. };
  767. struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
  768. {
  769. struct pipe_inode_info *pipe;
  770. pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
  771. if (pipe) {
  772. init_waitqueue_head(&pipe->wait);
  773. pipe->r_counter = pipe->w_counter = 1;
  774. pipe->inode = inode;
  775. }
  776. return pipe;
  777. }
  778. void __free_pipe_info(struct pipe_inode_info *pipe)
  779. {
  780. int i;
  781. for (i = 0; i < PIPE_BUFFERS; i++) {
  782. struct pipe_buffer *buf = pipe->bufs + i;
  783. if (buf->ops)
  784. buf->ops->release(pipe, buf);
  785. }
  786. if (pipe->tmp_page)
  787. __free_page(pipe->tmp_page);
  788. kfree(pipe);
  789. }
  790. void free_pipe_info(struct inode *inode)
  791. {
  792. __free_pipe_info(inode->i_pipe);
  793. inode->i_pipe = NULL;
  794. }
  795. static struct vfsmount *pipe_mnt __read_mostly;
  796. static int pipefs_delete_dentry(struct dentry *dentry)
  797. {
  798. /*
  799. * At creation time, we pretended this dentry was hashed
  800. * (by clearing DCACHE_UNHASHED bit in d_flags)
  801. * At delete time, we restore the truth : not hashed.
  802. * (so that dput() can proceed correctly)
  803. */
  804. dentry->d_flags |= DCACHE_UNHASHED;
  805. return 0;
  806. }
  807. /*
  808. * pipefs_dname() is called from d_path().
  809. */
  810. static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
  811. {
  812. return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  813. dentry->d_inode->i_ino);
  814. }
  815. static const struct dentry_operations pipefs_dentry_operations = {
  816. .d_delete = pipefs_delete_dentry,
  817. .d_dname = pipefs_dname,
  818. };
  819. static struct inode * get_pipe_inode(void)
  820. {
  821. struct inode *inode = new_inode(pipe_mnt->mnt_sb);
  822. struct pipe_inode_info *pipe;
  823. if (!inode)
  824. goto fail_inode;
  825. pipe = alloc_pipe_info(inode);
  826. if (!pipe)
  827. goto fail_iput;
  828. inode->i_pipe = pipe;
  829. pipe->readers = pipe->writers = 1;
  830. inode->i_fop = &rdwr_pipefifo_fops;
  831. /*
  832. * Mark the inode dirty from the very beginning,
  833. * that way it will never be moved to the dirty
  834. * list because "mark_inode_dirty()" will think
  835. * that it already _is_ on the dirty list.
  836. */
  837. inode->i_state = I_DIRTY;
  838. inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
  839. inode->i_uid = current_fsuid();
  840. inode->i_gid = current_fsgid();
  841. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  842. return inode;
  843. fail_iput:
  844. iput(inode);
  845. fail_inode:
  846. return NULL;
  847. }
  848. struct file *create_write_pipe(int flags)
  849. {
  850. int err;
  851. struct inode *inode;
  852. struct file *f;
  853. struct dentry *dentry;
  854. struct qstr name = { .name = "" };
  855. err = -ENFILE;
  856. inode = get_pipe_inode();
  857. if (!inode)
  858. goto err;
  859. err = -ENOMEM;
  860. dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &name);
  861. if (!dentry)
  862. goto err_inode;
  863. dentry->d_op = &pipefs_dentry_operations;
  864. /*
  865. * We dont want to publish this dentry into global dentry hash table.
  866. * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
  867. * This permits a working /proc/$pid/fd/XXX on pipes
  868. */
  869. dentry->d_flags &= ~DCACHE_UNHASHED;
  870. d_instantiate(dentry, inode);
  871. err = -ENFILE;
  872. f = alloc_file(pipe_mnt, dentry, FMODE_WRITE, &write_pipefifo_fops);
  873. if (!f)
  874. goto err_dentry;
  875. f->f_mapping = inode->i_mapping;
  876. f->f_flags = O_WRONLY | (flags & O_NONBLOCK);
  877. f->f_version = 0;
  878. return f;
  879. err_dentry:
  880. free_pipe_info(inode);
  881. dput(dentry);
  882. return ERR_PTR(err);
  883. err_inode:
  884. free_pipe_info(inode);
  885. iput(inode);
  886. err:
  887. return ERR_PTR(err);
  888. }
  889. void free_write_pipe(struct file *f)
  890. {
  891. free_pipe_info(f->f_dentry->d_inode);
  892. path_put(&f->f_path);
  893. put_filp(f);
  894. }
  895. struct file *create_read_pipe(struct file *wrf, int flags)
  896. {
  897. struct file *f = get_empty_filp();
  898. if (!f)
  899. return ERR_PTR(-ENFILE);
  900. /* Grab pipe from the writer */
  901. f->f_path = wrf->f_path;
  902. path_get(&wrf->f_path);
  903. f->f_mapping = wrf->f_path.dentry->d_inode->i_mapping;
  904. f->f_pos = 0;
  905. f->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  906. f->f_op = &read_pipefifo_fops;
  907. f->f_mode = FMODE_READ;
  908. f->f_version = 0;
  909. return f;
  910. }
  911. int do_pipe_flags(int *fd, int flags)
  912. {
  913. struct file *fw, *fr;
  914. int error;
  915. int fdw, fdr;
  916. if (flags & ~(O_CLOEXEC | O_NONBLOCK))
  917. return -EINVAL;
  918. fw = create_write_pipe(flags);
  919. if (IS_ERR(fw))
  920. return PTR_ERR(fw);
  921. fr = create_read_pipe(fw, flags);
  922. error = PTR_ERR(fr);
  923. if (IS_ERR(fr))
  924. goto err_write_pipe;
  925. error = get_unused_fd_flags(flags);
  926. if (error < 0)
  927. goto err_read_pipe;
  928. fdr = error;
  929. error = get_unused_fd_flags(flags);
  930. if (error < 0)
  931. goto err_fdr;
  932. fdw = error;
  933. audit_fd_pair(fdr, fdw);
  934. fd_install(fdr, fr);
  935. fd_install(fdw, fw);
  936. fd[0] = fdr;
  937. fd[1] = fdw;
  938. return 0;
  939. err_fdr:
  940. put_unused_fd(fdr);
  941. err_read_pipe:
  942. path_put(&fr->f_path);
  943. put_filp(fr);
  944. err_write_pipe:
  945. free_write_pipe(fw);
  946. return error;
  947. }
  948. /*
  949. * sys_pipe() is the normal C calling standard for creating
  950. * a pipe. It's not the way Unix traditionally does this, though.
  951. */
  952. SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
  953. {
  954. int fd[2];
  955. int error;
  956. error = do_pipe_flags(fd, flags);
  957. if (!error) {
  958. if (copy_to_user(fildes, fd, sizeof(fd))) {
  959. sys_close(fd[0]);
  960. sys_close(fd[1]);
  961. error = -EFAULT;
  962. }
  963. }
  964. return error;
  965. }
  966. SYSCALL_DEFINE1(pipe, int __user *, fildes)
  967. {
  968. return sys_pipe2(fildes, 0);
  969. }
  970. /*
  971. * pipefs should _never_ be mounted by userland - too much of security hassle,
  972. * no real gain from having the whole whorehouse mounted. So we don't need
  973. * any operations on the root directory. However, we need a non-trivial
  974. * d_name - pipe: will go nicely and kill the special-casing in procfs.
  975. */
  976. static int pipefs_get_sb(struct file_system_type *fs_type,
  977. int flags, const char *dev_name, void *data,
  978. struct vfsmount *mnt)
  979. {
  980. return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC, mnt);
  981. }
  982. static struct file_system_type pipe_fs_type = {
  983. .name = "pipefs",
  984. .get_sb = pipefs_get_sb,
  985. .kill_sb = kill_anon_super,
  986. };
  987. static int __init init_pipe_fs(void)
  988. {
  989. int err = register_filesystem(&pipe_fs_type);
  990. if (!err) {
  991. pipe_mnt = kern_mount(&pipe_fs_type);
  992. if (IS_ERR(pipe_mnt)) {
  993. err = PTR_ERR(pipe_mnt);
  994. unregister_filesystem(&pipe_fs_type);
  995. }
  996. }
  997. return err;
  998. }
  999. static void __exit exit_pipe_fs(void)
  1000. {
  1001. unregister_filesystem(&pipe_fs_type);
  1002. mntput(pipe_mnt);
  1003. }
  1004. fs_initcall(init_pipe_fs);
  1005. module_exit(exit_pipe_fs);