pipe.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. return retval;
  601. }
  602. static int
  603. pipe_write_fasync(int fd, struct file *filp, int on)
  604. {
  605. struct inode *inode = filp->f_path.dentry->d_inode;
  606. int retval;
  607. mutex_lock(&inode->i_mutex);
  608. retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
  609. mutex_unlock(&inode->i_mutex);
  610. return retval;
  611. }
  612. static int
  613. pipe_rdwr_fasync(int fd, struct file *filp, int on)
  614. {
  615. struct inode *inode = filp->f_path.dentry->d_inode;
  616. struct pipe_inode_info *pipe = inode->i_pipe;
  617. int retval;
  618. mutex_lock(&inode->i_mutex);
  619. retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
  620. if (retval >= 0) {
  621. retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
  622. if (retval < 0) /* this can happen only if on == T */
  623. fasync_helper(-1, filp, 0, &pipe->fasync_readers);
  624. }
  625. mutex_unlock(&inode->i_mutex);
  626. return retval;
  627. }
  628. static int
  629. pipe_read_release(struct inode *inode, struct file *filp)
  630. {
  631. return pipe_release(inode, 1, 0);
  632. }
  633. static int
  634. pipe_write_release(struct inode *inode, struct file *filp)
  635. {
  636. return pipe_release(inode, 0, 1);
  637. }
  638. static int
  639. pipe_rdwr_release(struct inode *inode, struct file *filp)
  640. {
  641. int decr, decw;
  642. decr = (filp->f_mode & FMODE_READ) != 0;
  643. decw = (filp->f_mode & FMODE_WRITE) != 0;
  644. return pipe_release(inode, decr, decw);
  645. }
  646. static int
  647. pipe_read_open(struct inode *inode, struct file *filp)
  648. {
  649. /* We could have perhaps used atomic_t, but this and friends
  650. below are the only places. So it doesn't seem worthwhile. */
  651. mutex_lock(&inode->i_mutex);
  652. inode->i_pipe->readers++;
  653. mutex_unlock(&inode->i_mutex);
  654. return 0;
  655. }
  656. static int
  657. pipe_write_open(struct inode *inode, struct file *filp)
  658. {
  659. mutex_lock(&inode->i_mutex);
  660. inode->i_pipe->writers++;
  661. mutex_unlock(&inode->i_mutex);
  662. return 0;
  663. }
  664. static int
  665. pipe_rdwr_open(struct inode *inode, struct file *filp)
  666. {
  667. mutex_lock(&inode->i_mutex);
  668. if (filp->f_mode & FMODE_READ)
  669. inode->i_pipe->readers++;
  670. if (filp->f_mode & FMODE_WRITE)
  671. inode->i_pipe->writers++;
  672. mutex_unlock(&inode->i_mutex);
  673. return 0;
  674. }
  675. /*
  676. * The file_operations structs are not static because they
  677. * are also used in linux/fs/fifo.c to do operations on FIFOs.
  678. *
  679. * Pipes reuse fifos' file_operations structs.
  680. */
  681. const struct file_operations read_pipefifo_fops = {
  682. .llseek = no_llseek,
  683. .read = do_sync_read,
  684. .aio_read = pipe_read,
  685. .write = bad_pipe_w,
  686. .poll = pipe_poll,
  687. .unlocked_ioctl = pipe_ioctl,
  688. .open = pipe_read_open,
  689. .release = pipe_read_release,
  690. .fasync = pipe_read_fasync,
  691. };
  692. const struct file_operations write_pipefifo_fops = {
  693. .llseek = no_llseek,
  694. .read = bad_pipe_r,
  695. .write = do_sync_write,
  696. .aio_write = pipe_write,
  697. .poll = pipe_poll,
  698. .unlocked_ioctl = pipe_ioctl,
  699. .open = pipe_write_open,
  700. .release = pipe_write_release,
  701. .fasync = pipe_write_fasync,
  702. };
  703. const struct file_operations rdwr_pipefifo_fops = {
  704. .llseek = no_llseek,
  705. .read = do_sync_read,
  706. .aio_read = pipe_read,
  707. .write = do_sync_write,
  708. .aio_write = pipe_write,
  709. .poll = pipe_poll,
  710. .unlocked_ioctl = pipe_ioctl,
  711. .open = pipe_rdwr_open,
  712. .release = pipe_rdwr_release,
  713. .fasync = pipe_rdwr_fasync,
  714. };
  715. struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
  716. {
  717. struct pipe_inode_info *pipe;
  718. pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
  719. if (pipe) {
  720. init_waitqueue_head(&pipe->wait);
  721. pipe->r_counter = pipe->w_counter = 1;
  722. pipe->inode = inode;
  723. }
  724. return pipe;
  725. }
  726. void __free_pipe_info(struct pipe_inode_info *pipe)
  727. {
  728. int i;
  729. for (i = 0; i < PIPE_BUFFERS; i++) {
  730. struct pipe_buffer *buf = pipe->bufs + i;
  731. if (buf->ops)
  732. buf->ops->release(pipe, buf);
  733. }
  734. if (pipe->tmp_page)
  735. __free_page(pipe->tmp_page);
  736. kfree(pipe);
  737. }
  738. void free_pipe_info(struct inode *inode)
  739. {
  740. __free_pipe_info(inode->i_pipe);
  741. inode->i_pipe = NULL;
  742. }
  743. static struct vfsmount *pipe_mnt __read_mostly;
  744. static int pipefs_delete_dentry(struct dentry *dentry)
  745. {
  746. /*
  747. * At creation time, we pretended this dentry was hashed
  748. * (by clearing DCACHE_UNHASHED bit in d_flags)
  749. * At delete time, we restore the truth : not hashed.
  750. * (so that dput() can proceed correctly)
  751. */
  752. dentry->d_flags |= DCACHE_UNHASHED;
  753. return 0;
  754. }
  755. /*
  756. * pipefs_dname() is called from d_path().
  757. */
  758. static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
  759. {
  760. return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  761. dentry->d_inode->i_ino);
  762. }
  763. static const struct dentry_operations pipefs_dentry_operations = {
  764. .d_delete = pipefs_delete_dentry,
  765. .d_dname = pipefs_dname,
  766. };
  767. static struct inode * get_pipe_inode(void)
  768. {
  769. struct inode *inode = new_inode(pipe_mnt->mnt_sb);
  770. struct pipe_inode_info *pipe;
  771. if (!inode)
  772. goto fail_inode;
  773. pipe = alloc_pipe_info(inode);
  774. if (!pipe)
  775. goto fail_iput;
  776. inode->i_pipe = pipe;
  777. pipe->readers = pipe->writers = 1;
  778. inode->i_fop = &rdwr_pipefifo_fops;
  779. /*
  780. * Mark the inode dirty from the very beginning,
  781. * that way it will never be moved to the dirty
  782. * list because "mark_inode_dirty()" will think
  783. * that it already _is_ on the dirty list.
  784. */
  785. inode->i_state = I_DIRTY;
  786. inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
  787. inode->i_uid = current_fsuid();
  788. inode->i_gid = current_fsgid();
  789. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  790. return inode;
  791. fail_iput:
  792. iput(inode);
  793. fail_inode:
  794. return NULL;
  795. }
  796. struct file *create_write_pipe(int flags)
  797. {
  798. int err;
  799. struct inode *inode;
  800. struct file *f;
  801. struct dentry *dentry;
  802. struct qstr name = { .name = "" };
  803. err = -ENFILE;
  804. inode = get_pipe_inode();
  805. if (!inode)
  806. goto err;
  807. err = -ENOMEM;
  808. dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &name);
  809. if (!dentry)
  810. goto err_inode;
  811. dentry->d_op = &pipefs_dentry_operations;
  812. /*
  813. * We dont want to publish this dentry into global dentry hash table.
  814. * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
  815. * This permits a working /proc/$pid/fd/XXX on pipes
  816. */
  817. dentry->d_flags &= ~DCACHE_UNHASHED;
  818. d_instantiate(dentry, inode);
  819. err = -ENFILE;
  820. f = alloc_file(pipe_mnt, dentry, FMODE_WRITE, &write_pipefifo_fops);
  821. if (!f)
  822. goto err_dentry;
  823. f->f_mapping = inode->i_mapping;
  824. f->f_flags = O_WRONLY | (flags & O_NONBLOCK);
  825. f->f_version = 0;
  826. return f;
  827. err_dentry:
  828. free_pipe_info(inode);
  829. dput(dentry);
  830. return ERR_PTR(err);
  831. err_inode:
  832. free_pipe_info(inode);
  833. iput(inode);
  834. err:
  835. return ERR_PTR(err);
  836. }
  837. void free_write_pipe(struct file *f)
  838. {
  839. free_pipe_info(f->f_dentry->d_inode);
  840. path_put(&f->f_path);
  841. put_filp(f);
  842. }
  843. struct file *create_read_pipe(struct file *wrf, int flags)
  844. {
  845. struct file *f = get_empty_filp();
  846. if (!f)
  847. return ERR_PTR(-ENFILE);
  848. /* Grab pipe from the writer */
  849. f->f_path = wrf->f_path;
  850. path_get(&wrf->f_path);
  851. f->f_mapping = wrf->f_path.dentry->d_inode->i_mapping;
  852. f->f_pos = 0;
  853. f->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  854. f->f_op = &read_pipefifo_fops;
  855. f->f_mode = FMODE_READ;
  856. f->f_version = 0;
  857. return f;
  858. }
  859. int do_pipe_flags(int *fd, int flags)
  860. {
  861. struct file *fw, *fr;
  862. int error;
  863. int fdw, fdr;
  864. if (flags & ~(O_CLOEXEC | O_NONBLOCK))
  865. return -EINVAL;
  866. fw = create_write_pipe(flags);
  867. if (IS_ERR(fw))
  868. return PTR_ERR(fw);
  869. fr = create_read_pipe(fw, flags);
  870. error = PTR_ERR(fr);
  871. if (IS_ERR(fr))
  872. goto err_write_pipe;
  873. error = get_unused_fd_flags(flags);
  874. if (error < 0)
  875. goto err_read_pipe;
  876. fdr = error;
  877. error = get_unused_fd_flags(flags);
  878. if (error < 0)
  879. goto err_fdr;
  880. fdw = error;
  881. audit_fd_pair(fdr, fdw);
  882. fd_install(fdr, fr);
  883. fd_install(fdw, fw);
  884. fd[0] = fdr;
  885. fd[1] = fdw;
  886. return 0;
  887. err_fdr:
  888. put_unused_fd(fdr);
  889. err_read_pipe:
  890. path_put(&fr->f_path);
  891. put_filp(fr);
  892. err_write_pipe:
  893. free_write_pipe(fw);
  894. return error;
  895. }
  896. /*
  897. * sys_pipe() is the normal C calling standard for creating
  898. * a pipe. It's not the way Unix traditionally does this, though.
  899. */
  900. SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
  901. {
  902. int fd[2];
  903. int error;
  904. error = do_pipe_flags(fd, flags);
  905. if (!error) {
  906. if (copy_to_user(fildes, fd, sizeof(fd))) {
  907. sys_close(fd[0]);
  908. sys_close(fd[1]);
  909. error = -EFAULT;
  910. }
  911. }
  912. return error;
  913. }
  914. SYSCALL_DEFINE1(pipe, int __user *, fildes)
  915. {
  916. return sys_pipe2(fildes, 0);
  917. }
  918. /*
  919. * pipefs should _never_ be mounted by userland - too much of security hassle,
  920. * no real gain from having the whole whorehouse mounted. So we don't need
  921. * any operations on the root directory. However, we need a non-trivial
  922. * d_name - pipe: will go nicely and kill the special-casing in procfs.
  923. */
  924. static int pipefs_get_sb(struct file_system_type *fs_type,
  925. int flags, const char *dev_name, void *data,
  926. struct vfsmount *mnt)
  927. {
  928. return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC, mnt);
  929. }
  930. static struct file_system_type pipe_fs_type = {
  931. .name = "pipefs",
  932. .get_sb = pipefs_get_sb,
  933. .kill_sb = kill_anon_super,
  934. };
  935. static int __init init_pipe_fs(void)
  936. {
  937. int err = register_filesystem(&pipe_fs_type);
  938. if (!err) {
  939. pipe_mnt = kern_mount(&pipe_fs_type);
  940. if (IS_ERR(pipe_mnt)) {
  941. err = PTR_ERR(pipe_mnt);
  942. unregister_filesystem(&pipe_fs_type);
  943. }
  944. }
  945. return err;
  946. }
  947. static void __exit exit_pipe_fs(void)
  948. {
  949. unregister_filesystem(&pipe_fs_type);
  950. mntput(pipe_mnt);
  951. }
  952. fs_initcall(init_pipe_fs);
  953. module_exit(exit_pipe_fs);