pipe.c 29 KB

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