pipe.c 24 KB

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