pipe.c 26 KB

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