pipe.c 29 KB

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