pipe.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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 <asm/uaccess.h>
  20. #include <asm/ioctls.h>
  21. /*
  22. * We use a start+len construction, which provides full use of the
  23. * allocated memory.
  24. * -- Florian Coosmann (FGC)
  25. *
  26. * Reads with count = 0 should always return 0.
  27. * -- Julian Bradfield 1999-06-07.
  28. *
  29. * FIFOs and Pipes now generate SIGIO for both readers and writers.
  30. * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
  31. *
  32. * pipe_read & write cleanup
  33. * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
  34. */
  35. /* Drop the inode semaphore and wait for a pipe event, atomically */
  36. void pipe_wait(struct pipe_inode_info *pipe)
  37. {
  38. DEFINE_WAIT(wait);
  39. /*
  40. * Pipes are system-local resources, so sleeping on them
  41. * is considered a noninteractive wait:
  42. */
  43. prepare_to_wait(&pipe->wait, &wait,
  44. TASK_INTERRUPTIBLE | TASK_NONINTERACTIVE);
  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. * passed in @pipe_buffer. 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(&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(&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 int
  525. pipe_ioctl(struct inode *pino, struct file *filp,
  526. unsigned int cmd, unsigned long arg)
  527. {
  528. struct inode *inode = filp->f_path.dentry->d_inode;
  529. struct pipe_inode_info *pipe;
  530. int count, buf, nrbufs;
  531. switch (cmd) {
  532. case FIONREAD:
  533. mutex_lock(&inode->i_mutex);
  534. pipe = inode->i_pipe;
  535. count = 0;
  536. buf = pipe->curbuf;
  537. nrbufs = pipe->nrbufs;
  538. while (--nrbufs >= 0) {
  539. count += pipe->bufs[buf].len;
  540. buf = (buf+1) & (PIPE_BUFFERS-1);
  541. }
  542. mutex_unlock(&inode->i_mutex);
  543. return put_user(count, (int __user *)arg);
  544. default:
  545. return -EINVAL;
  546. }
  547. }
  548. /* No kernel lock held - fine */
  549. static unsigned int
  550. pipe_poll(struct file *filp, poll_table *wait)
  551. {
  552. unsigned int mask;
  553. struct inode *inode = filp->f_path.dentry->d_inode;
  554. struct pipe_inode_info *pipe = inode->i_pipe;
  555. int nrbufs;
  556. poll_wait(filp, &pipe->wait, wait);
  557. /* Reading only -- no need for acquiring the semaphore. */
  558. nrbufs = pipe->nrbufs;
  559. mask = 0;
  560. if (filp->f_mode & FMODE_READ) {
  561. mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
  562. if (!pipe->writers && filp->f_version != pipe->w_counter)
  563. mask |= POLLHUP;
  564. }
  565. if (filp->f_mode & FMODE_WRITE) {
  566. mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
  567. /*
  568. * Most Unices do not set POLLERR for FIFOs but on Linux they
  569. * behave exactly like pipes for poll().
  570. */
  571. if (!pipe->readers)
  572. mask |= POLLERR;
  573. }
  574. return mask;
  575. }
  576. static int
  577. pipe_release(struct inode *inode, int decr, int decw)
  578. {
  579. struct pipe_inode_info *pipe;
  580. mutex_lock(&inode->i_mutex);
  581. pipe = inode->i_pipe;
  582. pipe->readers -= decr;
  583. pipe->writers -= decw;
  584. if (!pipe->readers && !pipe->writers) {
  585. free_pipe_info(inode);
  586. } else {
  587. wake_up_interruptible(&pipe->wait);
  588. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  589. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  590. }
  591. mutex_unlock(&inode->i_mutex);
  592. return 0;
  593. }
  594. static int
  595. pipe_read_fasync(int fd, struct file *filp, int on)
  596. {
  597. struct inode *inode = filp->f_path.dentry->d_inode;
  598. int retval;
  599. mutex_lock(&inode->i_mutex);
  600. retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
  601. mutex_unlock(&inode->i_mutex);
  602. if (retval < 0)
  603. return retval;
  604. return 0;
  605. }
  606. static int
  607. pipe_write_fasync(int fd, struct file *filp, int on)
  608. {
  609. struct inode *inode = filp->f_path.dentry->d_inode;
  610. int retval;
  611. mutex_lock(&inode->i_mutex);
  612. retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
  613. mutex_unlock(&inode->i_mutex);
  614. if (retval < 0)
  615. return retval;
  616. return 0;
  617. }
  618. static int
  619. pipe_rdwr_fasync(int fd, struct file *filp, int on)
  620. {
  621. struct inode *inode = filp->f_path.dentry->d_inode;
  622. struct pipe_inode_info *pipe = inode->i_pipe;
  623. int retval;
  624. mutex_lock(&inode->i_mutex);
  625. retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
  626. if (retval >= 0)
  627. retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
  628. mutex_unlock(&inode->i_mutex);
  629. if (retval < 0)
  630. return retval;
  631. return 0;
  632. }
  633. static int
  634. pipe_read_release(struct inode *inode, struct file *filp)
  635. {
  636. pipe_read_fasync(-1, filp, 0);
  637. return pipe_release(inode, 1, 0);
  638. }
  639. static int
  640. pipe_write_release(struct inode *inode, struct file *filp)
  641. {
  642. pipe_write_fasync(-1, filp, 0);
  643. return pipe_release(inode, 0, 1);
  644. }
  645. static int
  646. pipe_rdwr_release(struct inode *inode, struct file *filp)
  647. {
  648. int decr, decw;
  649. pipe_rdwr_fasync(-1, filp, 0);
  650. decr = (filp->f_mode & FMODE_READ) != 0;
  651. decw = (filp->f_mode & FMODE_WRITE) != 0;
  652. return pipe_release(inode, decr, decw);
  653. }
  654. static int
  655. pipe_read_open(struct inode *inode, struct file *filp)
  656. {
  657. /* We could have perhaps used atomic_t, but this and friends
  658. below are the only places. So it doesn't seem worthwhile. */
  659. mutex_lock(&inode->i_mutex);
  660. inode->i_pipe->readers++;
  661. mutex_unlock(&inode->i_mutex);
  662. return 0;
  663. }
  664. static int
  665. pipe_write_open(struct inode *inode, struct file *filp)
  666. {
  667. mutex_lock(&inode->i_mutex);
  668. inode->i_pipe->writers++;
  669. mutex_unlock(&inode->i_mutex);
  670. return 0;
  671. }
  672. static int
  673. pipe_rdwr_open(struct inode *inode, struct file *filp)
  674. {
  675. mutex_lock(&inode->i_mutex);
  676. if (filp->f_mode & FMODE_READ)
  677. inode->i_pipe->readers++;
  678. if (filp->f_mode & FMODE_WRITE)
  679. inode->i_pipe->writers++;
  680. mutex_unlock(&inode->i_mutex);
  681. return 0;
  682. }
  683. /*
  684. * The file_operations structs are not static because they
  685. * are also used in linux/fs/fifo.c to do operations on FIFOs.
  686. */
  687. const struct file_operations read_fifo_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. .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_fifo_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. .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_fifo_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. .ioctl = pipe_ioctl,
  717. .open = pipe_rdwr_open,
  718. .release = pipe_rdwr_release,
  719. .fasync = pipe_rdwr_fasync,
  720. };
  721. static const struct file_operations read_pipe_fops = {
  722. .llseek = no_llseek,
  723. .read = do_sync_read,
  724. .aio_read = pipe_read,
  725. .write = bad_pipe_w,
  726. .poll = pipe_poll,
  727. .ioctl = pipe_ioctl,
  728. .open = pipe_read_open,
  729. .release = pipe_read_release,
  730. .fasync = pipe_read_fasync,
  731. };
  732. static const struct file_operations write_pipe_fops = {
  733. .llseek = no_llseek,
  734. .read = bad_pipe_r,
  735. .write = do_sync_write,
  736. .aio_write = pipe_write,
  737. .poll = pipe_poll,
  738. .ioctl = pipe_ioctl,
  739. .open = pipe_write_open,
  740. .release = pipe_write_release,
  741. .fasync = pipe_write_fasync,
  742. };
  743. static const struct file_operations rdwr_pipe_fops = {
  744. .llseek = no_llseek,
  745. .read = do_sync_read,
  746. .aio_read = pipe_read,
  747. .write = do_sync_write,
  748. .aio_write = pipe_write,
  749. .poll = pipe_poll,
  750. .ioctl = pipe_ioctl,
  751. .open = pipe_rdwr_open,
  752. .release = pipe_rdwr_release,
  753. .fasync = pipe_rdwr_fasync,
  754. };
  755. struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
  756. {
  757. struct pipe_inode_info *pipe;
  758. pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
  759. if (pipe) {
  760. init_waitqueue_head(&pipe->wait);
  761. pipe->r_counter = pipe->w_counter = 1;
  762. pipe->inode = inode;
  763. }
  764. return pipe;
  765. }
  766. void __free_pipe_info(struct pipe_inode_info *pipe)
  767. {
  768. int i;
  769. for (i = 0; i < PIPE_BUFFERS; i++) {
  770. struct pipe_buffer *buf = pipe->bufs + i;
  771. if (buf->ops)
  772. buf->ops->release(pipe, buf);
  773. }
  774. if (pipe->tmp_page)
  775. __free_page(pipe->tmp_page);
  776. kfree(pipe);
  777. }
  778. void free_pipe_info(struct inode *inode)
  779. {
  780. __free_pipe_info(inode->i_pipe);
  781. inode->i_pipe = NULL;
  782. }
  783. static struct vfsmount *pipe_mnt __read_mostly;
  784. static int pipefs_delete_dentry(struct dentry *dentry)
  785. {
  786. /*
  787. * At creation time, we pretended this dentry was hashed
  788. * (by clearing DCACHE_UNHASHED bit in d_flags)
  789. * At delete time, we restore the truth : not hashed.
  790. * (so that dput() can proceed correctly)
  791. */
  792. dentry->d_flags |= DCACHE_UNHASHED;
  793. return 0;
  794. }
  795. /*
  796. * pipefs_dname() is called from d_path().
  797. */
  798. static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
  799. {
  800. return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  801. dentry->d_inode->i_ino);
  802. }
  803. static struct dentry_operations pipefs_dentry_operations = {
  804. .d_delete = pipefs_delete_dentry,
  805. .d_dname = pipefs_dname,
  806. };
  807. static struct inode * get_pipe_inode(void)
  808. {
  809. struct inode *inode = new_inode(pipe_mnt->mnt_sb);
  810. struct pipe_inode_info *pipe;
  811. if (!inode)
  812. goto fail_inode;
  813. pipe = alloc_pipe_info(inode);
  814. if (!pipe)
  815. goto fail_iput;
  816. inode->i_pipe = pipe;
  817. pipe->readers = pipe->writers = 1;
  818. inode->i_fop = &rdwr_pipe_fops;
  819. /*
  820. * Mark the inode dirty from the very beginning,
  821. * that way it will never be moved to the dirty
  822. * list because "mark_inode_dirty()" will think
  823. * that it already _is_ on the dirty list.
  824. */
  825. inode->i_state = I_DIRTY;
  826. inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
  827. inode->i_uid = current->fsuid;
  828. inode->i_gid = current->fsgid;
  829. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  830. return inode;
  831. fail_iput:
  832. iput(inode);
  833. fail_inode:
  834. return NULL;
  835. }
  836. struct file *create_write_pipe(void)
  837. {
  838. int err;
  839. struct inode *inode;
  840. struct file *f;
  841. struct dentry *dentry;
  842. struct qstr name = { .name = "" };
  843. f = get_empty_filp();
  844. if (!f)
  845. return ERR_PTR(-ENFILE);
  846. err = -ENFILE;
  847. inode = get_pipe_inode();
  848. if (!inode)
  849. goto err_file;
  850. err = -ENOMEM;
  851. dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &name);
  852. if (!dentry)
  853. goto err_inode;
  854. dentry->d_op = &pipefs_dentry_operations;
  855. /*
  856. * We dont want to publish this dentry into global dentry hash table.
  857. * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
  858. * This permits a working /proc/$pid/fd/XXX on pipes
  859. */
  860. dentry->d_flags &= ~DCACHE_UNHASHED;
  861. d_instantiate(dentry, inode);
  862. f->f_path.mnt = mntget(pipe_mnt);
  863. f->f_path.dentry = dentry;
  864. f->f_mapping = inode->i_mapping;
  865. f->f_flags = O_WRONLY;
  866. f->f_op = &write_pipe_fops;
  867. f->f_mode = FMODE_WRITE;
  868. f->f_version = 0;
  869. return f;
  870. err_inode:
  871. free_pipe_info(inode);
  872. iput(inode);
  873. err_file:
  874. put_filp(f);
  875. return ERR_PTR(err);
  876. }
  877. void free_write_pipe(struct file *f)
  878. {
  879. free_pipe_info(f->f_dentry->d_inode);
  880. dput(f->f_path.dentry);
  881. mntput(f->f_path.mnt);
  882. put_filp(f);
  883. }
  884. struct file *create_read_pipe(struct file *wrf)
  885. {
  886. struct file *f = get_empty_filp();
  887. if (!f)
  888. return ERR_PTR(-ENFILE);
  889. /* Grab pipe from the writer */
  890. f->f_path.mnt = mntget(wrf->f_path.mnt);
  891. f->f_path.dentry = dget(wrf->f_path.dentry);
  892. f->f_mapping = wrf->f_path.dentry->d_inode->i_mapping;
  893. f->f_pos = 0;
  894. f->f_flags = O_RDONLY;
  895. f->f_op = &read_pipe_fops;
  896. f->f_mode = FMODE_READ;
  897. f->f_version = 0;
  898. return f;
  899. }
  900. int do_pipe(int *fd)
  901. {
  902. struct file *fw, *fr;
  903. int error;
  904. int fdw, fdr;
  905. fw = create_write_pipe();
  906. if (IS_ERR(fw))
  907. return PTR_ERR(fw);
  908. fr = create_read_pipe(fw);
  909. error = PTR_ERR(fr);
  910. if (IS_ERR(fr))
  911. goto err_write_pipe;
  912. error = get_unused_fd();
  913. if (error < 0)
  914. goto err_read_pipe;
  915. fdr = error;
  916. error = get_unused_fd();
  917. if (error < 0)
  918. goto err_fdr;
  919. fdw = error;
  920. error = audit_fd_pair(fdr, fdw);
  921. if (error < 0)
  922. goto err_fdw;
  923. fd_install(fdr, fr);
  924. fd_install(fdw, fw);
  925. fd[0] = fdr;
  926. fd[1] = fdw;
  927. return 0;
  928. err_fdw:
  929. put_unused_fd(fdw);
  930. err_fdr:
  931. put_unused_fd(fdr);
  932. err_read_pipe:
  933. dput(fr->f_dentry);
  934. mntput(fr->f_vfsmnt);
  935. put_filp(fr);
  936. err_write_pipe:
  937. free_write_pipe(fw);
  938. return error;
  939. }
  940. /*
  941. * pipefs should _never_ be mounted by userland - too much of security hassle,
  942. * no real gain from having the whole whorehouse mounted. So we don't need
  943. * any operations on the root directory. However, we need a non-trivial
  944. * d_name - pipe: will go nicely and kill the special-casing in procfs.
  945. */
  946. static int pipefs_get_sb(struct file_system_type *fs_type,
  947. int flags, const char *dev_name, void *data,
  948. struct vfsmount *mnt)
  949. {
  950. return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC, mnt);
  951. }
  952. static struct file_system_type pipe_fs_type = {
  953. .name = "pipefs",
  954. .get_sb = pipefs_get_sb,
  955. .kill_sb = kill_anon_super,
  956. };
  957. static int __init init_pipe_fs(void)
  958. {
  959. int err = register_filesystem(&pipe_fs_type);
  960. if (!err) {
  961. pipe_mnt = kern_mount(&pipe_fs_type);
  962. if (IS_ERR(pipe_mnt)) {
  963. err = PTR_ERR(pipe_mnt);
  964. unregister_filesystem(&pipe_fs_type);
  965. }
  966. }
  967. return err;
  968. }
  969. static void __exit exit_pipe_fs(void)
  970. {
  971. unregister_filesystem(&pipe_fs_type);
  972. mntput(pipe_mnt);
  973. }
  974. fs_initcall(init_pipe_fs);
  975. module_exit(exit_pipe_fs);