pipe.c 24 KB

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