pipe.c 25 KB

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