pipe.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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. int ret = -ENOENT;
  692. mutex_lock(&inode->i_mutex);
  693. if (inode->i_pipe) {
  694. ret = 0;
  695. inode->i_pipe->readers++;
  696. }
  697. mutex_unlock(&inode->i_mutex);
  698. return ret;
  699. }
  700. static int
  701. pipe_write_open(struct inode *inode, struct file *filp)
  702. {
  703. int ret = -ENOENT;
  704. mutex_lock(&inode->i_mutex);
  705. if (inode->i_pipe) {
  706. ret = 0;
  707. inode->i_pipe->writers++;
  708. }
  709. mutex_unlock(&inode->i_mutex);
  710. return ret;
  711. }
  712. static int
  713. pipe_rdwr_open(struct inode *inode, struct file *filp)
  714. {
  715. int ret = -ENOENT;
  716. mutex_lock(&inode->i_mutex);
  717. if (inode->i_pipe) {
  718. ret = 0;
  719. if (filp->f_mode & FMODE_READ)
  720. inode->i_pipe->readers++;
  721. if (filp->f_mode & FMODE_WRITE)
  722. inode->i_pipe->writers++;
  723. }
  724. mutex_unlock(&inode->i_mutex);
  725. return ret;
  726. }
  727. /*
  728. * The file_operations structs are not static because they
  729. * are also used in linux/fs/fifo.c to do operations on FIFOs.
  730. *
  731. * Pipes reuse fifos' file_operations structs.
  732. */
  733. const struct file_operations read_pipefifo_fops = {
  734. .llseek = no_llseek,
  735. .read = do_sync_read,
  736. .aio_read = pipe_read,
  737. .write = bad_pipe_w,
  738. .poll = pipe_poll,
  739. .unlocked_ioctl = pipe_ioctl,
  740. .open = pipe_read_open,
  741. .release = pipe_read_release,
  742. .fasync = pipe_read_fasync,
  743. };
  744. const struct file_operations write_pipefifo_fops = {
  745. .llseek = no_llseek,
  746. .read = bad_pipe_r,
  747. .write = do_sync_write,
  748. .aio_write = pipe_write,
  749. .poll = pipe_poll,
  750. .unlocked_ioctl = pipe_ioctl,
  751. .open = pipe_write_open,
  752. .release = pipe_write_release,
  753. .fasync = pipe_write_fasync,
  754. };
  755. const struct file_operations rdwr_pipefifo_fops = {
  756. .llseek = no_llseek,
  757. .read = do_sync_read,
  758. .aio_read = pipe_read,
  759. .write = do_sync_write,
  760. .aio_write = pipe_write,
  761. .poll = pipe_poll,
  762. .unlocked_ioctl = pipe_ioctl,
  763. .open = pipe_rdwr_open,
  764. .release = pipe_rdwr_release,
  765. .fasync = pipe_rdwr_fasync,
  766. };
  767. struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
  768. {
  769. struct pipe_inode_info *pipe;
  770. pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
  771. if (pipe) {
  772. init_waitqueue_head(&pipe->wait);
  773. pipe->r_counter = pipe->w_counter = 1;
  774. pipe->inode = inode;
  775. }
  776. return pipe;
  777. }
  778. void __free_pipe_info(struct pipe_inode_info *pipe)
  779. {
  780. int i;
  781. for (i = 0; i < PIPE_BUFFERS; i++) {
  782. struct pipe_buffer *buf = pipe->bufs + i;
  783. if (buf->ops)
  784. buf->ops->release(pipe, buf);
  785. }
  786. if (pipe->tmp_page)
  787. __free_page(pipe->tmp_page);
  788. kfree(pipe);
  789. }
  790. void free_pipe_info(struct inode *inode)
  791. {
  792. __free_pipe_info(inode->i_pipe);
  793. inode->i_pipe = NULL;
  794. }
  795. static struct vfsmount *pipe_mnt __read_mostly;
  796. /*
  797. * pipefs_dname() is called from d_path().
  798. */
  799. static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
  800. {
  801. return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  802. dentry->d_inode->i_ino);
  803. }
  804. static const struct dentry_operations pipefs_dentry_operations = {
  805. .d_dname = pipefs_dname,
  806. };
  807. static struct inode * get_pipe_inode(void)
  808. {
  809. struct inode *inode = new_inode(pipe_mnt->mnt_sb);
  810. struct pipe_inode_info *pipe;
  811. if (!inode)
  812. goto fail_inode;
  813. pipe = alloc_pipe_info(inode);
  814. if (!pipe)
  815. goto fail_iput;
  816. inode->i_pipe = pipe;
  817. pipe->readers = pipe->writers = 1;
  818. inode->i_fop = &rdwr_pipefifo_fops;
  819. /*
  820. * Mark the inode dirty from the very beginning,
  821. * that way it will never be moved to the dirty
  822. * list because "mark_inode_dirty()" will think
  823. * that it already _is_ on the dirty list.
  824. */
  825. inode->i_state = I_DIRTY;
  826. inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
  827. inode->i_uid = current_fsuid();
  828. inode->i_gid = current_fsgid();
  829. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  830. return inode;
  831. fail_iput:
  832. iput(inode);
  833. fail_inode:
  834. return NULL;
  835. }
  836. struct file *create_write_pipe(int flags)
  837. {
  838. int err;
  839. struct inode *inode;
  840. struct file *f;
  841. struct path path;
  842. struct qstr name = { .name = "" };
  843. err = -ENFILE;
  844. inode = get_pipe_inode();
  845. if (!inode)
  846. goto err;
  847. err = -ENOMEM;
  848. path.dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &name);
  849. if (!path.dentry)
  850. goto err_inode;
  851. path.mnt = mntget(pipe_mnt);
  852. path.dentry->d_op = &pipefs_dentry_operations;
  853. d_instantiate(path.dentry, inode);
  854. err = -ENFILE;
  855. f = alloc_file(&path, FMODE_WRITE, &write_pipefifo_fops);
  856. if (!f)
  857. goto err_dentry;
  858. f->f_mapping = inode->i_mapping;
  859. f->f_flags = O_WRONLY | (flags & O_NONBLOCK);
  860. f->f_version = 0;
  861. return f;
  862. err_dentry:
  863. free_pipe_info(inode);
  864. path_put(&path);
  865. return ERR_PTR(err);
  866. err_inode:
  867. free_pipe_info(inode);
  868. iput(inode);
  869. err:
  870. return ERR_PTR(err);
  871. }
  872. void free_write_pipe(struct file *f)
  873. {
  874. free_pipe_info(f->f_dentry->d_inode);
  875. path_put(&f->f_path);
  876. put_filp(f);
  877. }
  878. struct file *create_read_pipe(struct file *wrf, int flags)
  879. {
  880. /* Grab pipe from the writer */
  881. struct file *f = alloc_file(&wrf->f_path, FMODE_READ,
  882. &read_pipefifo_fops);
  883. if (!f)
  884. return ERR_PTR(-ENFILE);
  885. path_get(&wrf->f_path);
  886. f->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  887. return f;
  888. }
  889. int do_pipe_flags(int *fd, int flags)
  890. {
  891. struct file *fw, *fr;
  892. int error;
  893. int fdw, fdr;
  894. if (flags & ~(O_CLOEXEC | O_NONBLOCK))
  895. return -EINVAL;
  896. fw = create_write_pipe(flags);
  897. if (IS_ERR(fw))
  898. return PTR_ERR(fw);
  899. fr = create_read_pipe(fw, flags);
  900. error = PTR_ERR(fr);
  901. if (IS_ERR(fr))
  902. goto err_write_pipe;
  903. error = get_unused_fd_flags(flags);
  904. if (error < 0)
  905. goto err_read_pipe;
  906. fdr = error;
  907. error = get_unused_fd_flags(flags);
  908. if (error < 0)
  909. goto err_fdr;
  910. fdw = error;
  911. audit_fd_pair(fdr, fdw);
  912. fd_install(fdr, fr);
  913. fd_install(fdw, fw);
  914. fd[0] = fdr;
  915. fd[1] = fdw;
  916. return 0;
  917. err_fdr:
  918. put_unused_fd(fdr);
  919. err_read_pipe:
  920. path_put(&fr->f_path);
  921. put_filp(fr);
  922. err_write_pipe:
  923. free_write_pipe(fw);
  924. return error;
  925. }
  926. /*
  927. * sys_pipe() is the normal C calling standard for creating
  928. * a pipe. It's not the way Unix traditionally does this, though.
  929. */
  930. SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
  931. {
  932. int fd[2];
  933. int error;
  934. error = do_pipe_flags(fd, flags);
  935. if (!error) {
  936. if (copy_to_user(fildes, fd, sizeof(fd))) {
  937. sys_close(fd[0]);
  938. sys_close(fd[1]);
  939. error = -EFAULT;
  940. }
  941. }
  942. return error;
  943. }
  944. SYSCALL_DEFINE1(pipe, int __user *, fildes)
  945. {
  946. return sys_pipe2(fildes, 0);
  947. }
  948. /*
  949. * pipefs should _never_ be mounted by userland - too much of security hassle,
  950. * no real gain from having the whole whorehouse mounted. So we don't need
  951. * any operations on the root directory. However, we need a non-trivial
  952. * d_name - pipe: will go nicely and kill the special-casing in procfs.
  953. */
  954. static int pipefs_get_sb(struct file_system_type *fs_type,
  955. int flags, const char *dev_name, void *data,
  956. struct vfsmount *mnt)
  957. {
  958. return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC, mnt);
  959. }
  960. static struct file_system_type pipe_fs_type = {
  961. .name = "pipefs",
  962. .get_sb = pipefs_get_sb,
  963. .kill_sb = kill_anon_super,
  964. };
  965. static int __init init_pipe_fs(void)
  966. {
  967. int err = register_filesystem(&pipe_fs_type);
  968. if (!err) {
  969. pipe_mnt = kern_mount(&pipe_fs_type);
  970. if (IS_ERR(pipe_mnt)) {
  971. err = PTR_ERR(pipe_mnt);
  972. unregister_filesystem(&pipe_fs_type);
  973. }
  974. }
  975. return err;
  976. }
  977. static void __exit exit_pipe_fs(void)
  978. {
  979. unregister_filesystem(&pipe_fs_type);
  980. mntput(pipe_mnt);
  981. }
  982. fs_initcall(init_pipe_fs);
  983. module_exit(exit_pipe_fs);