pipe.c 24 KB

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