pipe.c 24 KB

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