pipe.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  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->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->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 pipe_inode_info *pipe = filp->private_data;
  326. int do_wakeup;
  327. ssize_t ret;
  328. struct iovec *iov = (struct iovec *)_iov;
  329. size_t total_len;
  330. total_len = iov_length(iov, nr_segs);
  331. /* Null read succeeds. */
  332. if (unlikely(total_len == 0))
  333. return 0;
  334. do_wakeup = 0;
  335. ret = 0;
  336. pipe_lock(pipe);
  337. for (;;) {
  338. int bufs = pipe->nrbufs;
  339. if (bufs) {
  340. int curbuf = pipe->curbuf;
  341. struct pipe_buffer *buf = pipe->bufs + curbuf;
  342. const struct pipe_buf_operations *ops = buf->ops;
  343. void *addr;
  344. size_t chars = buf->len;
  345. int error, atomic;
  346. if (chars > total_len)
  347. chars = total_len;
  348. error = ops->confirm(pipe, buf);
  349. if (error) {
  350. if (!ret)
  351. ret = error;
  352. break;
  353. }
  354. atomic = !iov_fault_in_pages_write(iov, chars);
  355. redo:
  356. addr = ops->map(pipe, buf, atomic);
  357. error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars, atomic);
  358. ops->unmap(pipe, buf, addr);
  359. if (unlikely(error)) {
  360. /*
  361. * Just retry with the slow path if we failed.
  362. */
  363. if (atomic) {
  364. atomic = 0;
  365. goto redo;
  366. }
  367. if (!ret)
  368. ret = error;
  369. break;
  370. }
  371. ret += chars;
  372. buf->offset += chars;
  373. buf->len -= chars;
  374. /* Was it a packet buffer? Clean up and exit */
  375. if (buf->flags & PIPE_BUF_FLAG_PACKET) {
  376. total_len = chars;
  377. buf->len = 0;
  378. }
  379. if (!buf->len) {
  380. buf->ops = NULL;
  381. ops->release(pipe, buf);
  382. curbuf = (curbuf + 1) & (pipe->buffers - 1);
  383. pipe->curbuf = curbuf;
  384. pipe->nrbufs = --bufs;
  385. do_wakeup = 1;
  386. }
  387. total_len -= chars;
  388. if (!total_len)
  389. break; /* common path: read succeeded */
  390. }
  391. if (bufs) /* More to do? */
  392. continue;
  393. if (!pipe->writers)
  394. break;
  395. if (!pipe->waiting_writers) {
  396. /* syscall merging: Usually we must not sleep
  397. * if O_NONBLOCK is set, or if we got some data.
  398. * But if a writer sleeps in kernel space, then
  399. * we can wait for that data without violating POSIX.
  400. */
  401. if (ret)
  402. break;
  403. if (filp->f_flags & O_NONBLOCK) {
  404. ret = -EAGAIN;
  405. break;
  406. }
  407. }
  408. if (signal_pending(current)) {
  409. if (!ret)
  410. ret = -ERESTARTSYS;
  411. break;
  412. }
  413. if (do_wakeup) {
  414. wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
  415. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  416. }
  417. pipe_wait(pipe);
  418. }
  419. pipe_unlock(pipe);
  420. /* Signal writers asynchronously that there is more room. */
  421. if (do_wakeup) {
  422. wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
  423. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  424. }
  425. if (ret > 0)
  426. file_accessed(filp);
  427. return ret;
  428. }
  429. static inline int is_packetized(struct file *file)
  430. {
  431. return (file->f_flags & O_DIRECT) != 0;
  432. }
  433. static ssize_t
  434. pipe_write(struct kiocb *iocb, const struct iovec *_iov,
  435. unsigned long nr_segs, loff_t ppos)
  436. {
  437. struct file *filp = iocb->ki_filp;
  438. struct pipe_inode_info *pipe = filp->private_data;
  439. ssize_t ret;
  440. int do_wakeup;
  441. struct iovec *iov = (struct iovec *)_iov;
  442. size_t total_len;
  443. ssize_t chars;
  444. total_len = iov_length(iov, nr_segs);
  445. /* Null write succeeds. */
  446. if (unlikely(total_len == 0))
  447. return 0;
  448. do_wakeup = 0;
  449. ret = 0;
  450. pipe_lock(pipe);
  451. if (!pipe->readers) {
  452. send_sig(SIGPIPE, current, 0);
  453. ret = -EPIPE;
  454. goto out;
  455. }
  456. /* We try to merge small writes */
  457. chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
  458. if (pipe->nrbufs && chars != 0) {
  459. int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
  460. (pipe->buffers - 1);
  461. struct pipe_buffer *buf = pipe->bufs + lastbuf;
  462. const struct pipe_buf_operations *ops = buf->ops;
  463. int offset = buf->offset + buf->len;
  464. if (ops->can_merge && offset + chars <= PAGE_SIZE) {
  465. int error, atomic = 1;
  466. void *addr;
  467. error = ops->confirm(pipe, buf);
  468. if (error)
  469. goto out;
  470. iov_fault_in_pages_read(iov, chars);
  471. redo1:
  472. addr = ops->map(pipe, buf, atomic);
  473. error = pipe_iov_copy_from_user(offset + addr, iov,
  474. chars, atomic);
  475. ops->unmap(pipe, buf, addr);
  476. ret = error;
  477. do_wakeup = 1;
  478. if (error) {
  479. if (atomic) {
  480. atomic = 0;
  481. goto redo1;
  482. }
  483. goto out;
  484. }
  485. buf->len += chars;
  486. total_len -= chars;
  487. ret = chars;
  488. if (!total_len)
  489. goto out;
  490. }
  491. }
  492. for (;;) {
  493. int bufs;
  494. if (!pipe->readers) {
  495. send_sig(SIGPIPE, current, 0);
  496. if (!ret)
  497. ret = -EPIPE;
  498. break;
  499. }
  500. bufs = pipe->nrbufs;
  501. if (bufs < pipe->buffers) {
  502. int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
  503. struct pipe_buffer *buf = pipe->bufs + newbuf;
  504. struct page *page = pipe->tmp_page;
  505. char *src;
  506. int error, atomic = 1;
  507. if (!page) {
  508. page = alloc_page(GFP_HIGHUSER);
  509. if (unlikely(!page)) {
  510. ret = ret ? : -ENOMEM;
  511. break;
  512. }
  513. pipe->tmp_page = page;
  514. }
  515. /* Always wake up, even if the copy fails. Otherwise
  516. * we lock up (O_NONBLOCK-)readers that sleep due to
  517. * syscall merging.
  518. * FIXME! Is this really true?
  519. */
  520. do_wakeup = 1;
  521. chars = PAGE_SIZE;
  522. if (chars > total_len)
  523. chars = total_len;
  524. iov_fault_in_pages_read(iov, chars);
  525. redo2:
  526. if (atomic)
  527. src = kmap_atomic(page);
  528. else
  529. src = kmap(page);
  530. error = pipe_iov_copy_from_user(src, iov, chars,
  531. atomic);
  532. if (atomic)
  533. kunmap_atomic(src);
  534. else
  535. kunmap(page);
  536. if (unlikely(error)) {
  537. if (atomic) {
  538. atomic = 0;
  539. goto redo2;
  540. }
  541. if (!ret)
  542. ret = error;
  543. break;
  544. }
  545. ret += chars;
  546. /* Insert it into the buffer array */
  547. buf->page = page;
  548. buf->ops = &anon_pipe_buf_ops;
  549. buf->offset = 0;
  550. buf->len = chars;
  551. buf->flags = 0;
  552. if (is_packetized(filp)) {
  553. buf->ops = &packet_pipe_buf_ops;
  554. buf->flags = PIPE_BUF_FLAG_PACKET;
  555. }
  556. pipe->nrbufs = ++bufs;
  557. pipe->tmp_page = NULL;
  558. total_len -= chars;
  559. if (!total_len)
  560. break;
  561. }
  562. if (bufs < pipe->buffers)
  563. continue;
  564. if (filp->f_flags & O_NONBLOCK) {
  565. if (!ret)
  566. ret = -EAGAIN;
  567. break;
  568. }
  569. if (signal_pending(current)) {
  570. if (!ret)
  571. ret = -ERESTARTSYS;
  572. break;
  573. }
  574. if (do_wakeup) {
  575. wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
  576. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  577. do_wakeup = 0;
  578. }
  579. pipe->waiting_writers++;
  580. pipe_wait(pipe);
  581. pipe->waiting_writers--;
  582. }
  583. out:
  584. pipe_unlock(pipe);
  585. if (do_wakeup) {
  586. wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
  587. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  588. }
  589. if (ret > 0) {
  590. int err = file_update_time(filp);
  591. if (err)
  592. ret = err;
  593. }
  594. return ret;
  595. }
  596. static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  597. {
  598. struct pipe_inode_info *pipe = filp->private_data;
  599. int count, buf, nrbufs;
  600. switch (cmd) {
  601. case FIONREAD:
  602. pipe_lock(pipe);
  603. count = 0;
  604. buf = pipe->curbuf;
  605. nrbufs = pipe->nrbufs;
  606. while (--nrbufs >= 0) {
  607. count += pipe->bufs[buf].len;
  608. buf = (buf+1) & (pipe->buffers - 1);
  609. }
  610. pipe_unlock(pipe);
  611. return put_user(count, (int __user *)arg);
  612. default:
  613. return -ENOIOCTLCMD;
  614. }
  615. }
  616. /* No kernel lock held - fine */
  617. static unsigned int
  618. pipe_poll(struct file *filp, poll_table *wait)
  619. {
  620. unsigned int mask;
  621. struct pipe_inode_info *pipe = filp->private_data;
  622. int nrbufs;
  623. poll_wait(filp, &pipe->wait, wait);
  624. /* Reading only -- no need for acquiring the semaphore. */
  625. nrbufs = pipe->nrbufs;
  626. mask = 0;
  627. if (filp->f_mode & FMODE_READ) {
  628. mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
  629. if (!pipe->writers && filp->f_version != pipe->w_counter)
  630. mask |= POLLHUP;
  631. }
  632. if (filp->f_mode & FMODE_WRITE) {
  633. mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
  634. /*
  635. * Most Unices do not set POLLERR for FIFOs but on Linux they
  636. * behave exactly like pipes for poll().
  637. */
  638. if (!pipe->readers)
  639. mask |= POLLERR;
  640. }
  641. return mask;
  642. }
  643. static int
  644. pipe_release(struct inode *inode, struct file *file)
  645. {
  646. struct pipe_inode_info *pipe = inode->i_pipe;
  647. int kill = 0;
  648. pipe_lock(pipe);
  649. if (file->f_mode & FMODE_READ)
  650. pipe->readers--;
  651. if (file->f_mode & FMODE_WRITE)
  652. pipe->writers--;
  653. if (pipe->readers || pipe->writers) {
  654. wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
  655. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  656. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  657. }
  658. spin_lock(&inode->i_lock);
  659. if (!--pipe->files) {
  660. inode->i_pipe = NULL;
  661. kill = 1;
  662. }
  663. spin_unlock(&inode->i_lock);
  664. pipe_unlock(pipe);
  665. if (kill)
  666. __free_pipe_info(pipe);
  667. return 0;
  668. }
  669. static int
  670. pipe_fasync(int fd, struct file *filp, int on)
  671. {
  672. struct pipe_inode_info *pipe = filp->private_data;
  673. int retval = 0;
  674. pipe_lock(pipe);
  675. if (filp->f_mode & FMODE_READ)
  676. retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
  677. if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
  678. retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
  679. if (retval < 0 && (filp->f_mode & FMODE_READ))
  680. /* this can happen only if on == T */
  681. fasync_helper(-1, filp, 0, &pipe->fasync_readers);
  682. }
  683. pipe_unlock(pipe);
  684. return retval;
  685. }
  686. struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
  687. {
  688. struct pipe_inode_info *pipe;
  689. pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
  690. if (pipe) {
  691. pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * PIPE_DEF_BUFFERS, GFP_KERNEL);
  692. if (pipe->bufs) {
  693. init_waitqueue_head(&pipe->wait);
  694. pipe->r_counter = pipe->w_counter = 1;
  695. pipe->inode = inode;
  696. pipe->buffers = PIPE_DEF_BUFFERS;
  697. mutex_init(&pipe->mutex);
  698. return pipe;
  699. }
  700. kfree(pipe);
  701. }
  702. return NULL;
  703. }
  704. void __free_pipe_info(struct pipe_inode_info *pipe)
  705. {
  706. int i;
  707. for (i = 0; i < pipe->buffers; i++) {
  708. struct pipe_buffer *buf = pipe->bufs + i;
  709. if (buf->ops)
  710. buf->ops->release(pipe, buf);
  711. }
  712. if (pipe->tmp_page)
  713. __free_page(pipe->tmp_page);
  714. kfree(pipe->bufs);
  715. kfree(pipe);
  716. }
  717. void free_pipe_info(struct inode *inode)
  718. {
  719. __free_pipe_info(inode->i_pipe);
  720. inode->i_pipe = NULL;
  721. }
  722. static struct vfsmount *pipe_mnt __read_mostly;
  723. /*
  724. * pipefs_dname() is called from d_path().
  725. */
  726. static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
  727. {
  728. return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  729. dentry->d_inode->i_ino);
  730. }
  731. static const struct dentry_operations pipefs_dentry_operations = {
  732. .d_dname = pipefs_dname,
  733. };
  734. static struct inode * get_pipe_inode(void)
  735. {
  736. struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
  737. struct pipe_inode_info *pipe;
  738. if (!inode)
  739. goto fail_inode;
  740. inode->i_ino = get_next_ino();
  741. pipe = alloc_pipe_info(inode);
  742. if (!pipe)
  743. goto fail_iput;
  744. inode->i_pipe = pipe;
  745. pipe->files = 2;
  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. f->private_data = inode->i_pipe;
  786. res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops);
  787. if (IS_ERR(res[0]))
  788. goto err_file;
  789. path_get(&path);
  790. res[0]->private_data = inode->i_pipe;
  791. res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  792. res[1] = f;
  793. return 0;
  794. err_file:
  795. put_filp(f);
  796. err_dentry:
  797. free_pipe_info(inode);
  798. path_put(&path);
  799. return err;
  800. err_inode:
  801. free_pipe_info(inode);
  802. iput(inode);
  803. return err;
  804. }
  805. static int __do_pipe_flags(int *fd, struct file **files, int flags)
  806. {
  807. int error;
  808. int fdw, fdr;
  809. if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
  810. return -EINVAL;
  811. error = create_pipe_files(files, flags);
  812. if (error)
  813. return error;
  814. error = get_unused_fd_flags(flags);
  815. if (error < 0)
  816. goto err_read_pipe;
  817. fdr = error;
  818. error = get_unused_fd_flags(flags);
  819. if (error < 0)
  820. goto err_fdr;
  821. fdw = error;
  822. audit_fd_pair(fdr, fdw);
  823. fd[0] = fdr;
  824. fd[1] = fdw;
  825. return 0;
  826. err_fdr:
  827. put_unused_fd(fdr);
  828. err_read_pipe:
  829. fput(files[0]);
  830. fput(files[1]);
  831. return error;
  832. }
  833. int do_pipe_flags(int *fd, int flags)
  834. {
  835. struct file *files[2];
  836. int error = __do_pipe_flags(fd, files, flags);
  837. if (!error) {
  838. fd_install(fd[0], files[0]);
  839. fd_install(fd[1], files[1]);
  840. }
  841. return error;
  842. }
  843. /*
  844. * sys_pipe() is the normal C calling standard for creating
  845. * a pipe. It's not the way Unix traditionally does this, though.
  846. */
  847. SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
  848. {
  849. struct file *files[2];
  850. int fd[2];
  851. int error;
  852. error = __do_pipe_flags(fd, files, flags);
  853. if (!error) {
  854. if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
  855. fput(files[0]);
  856. fput(files[1]);
  857. put_unused_fd(fd[0]);
  858. put_unused_fd(fd[1]);
  859. error = -EFAULT;
  860. } else {
  861. fd_install(fd[0], files[0]);
  862. fd_install(fd[1], files[1]);
  863. }
  864. }
  865. return error;
  866. }
  867. SYSCALL_DEFINE1(pipe, int __user *, fildes)
  868. {
  869. return sys_pipe2(fildes, 0);
  870. }
  871. static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
  872. {
  873. int cur = *cnt;
  874. while (cur == *cnt) {
  875. pipe_wait(pipe);
  876. if (signal_pending(current))
  877. break;
  878. }
  879. return cur == *cnt ? -ERESTARTSYS : 0;
  880. }
  881. static void wake_up_partner(struct pipe_inode_info *pipe)
  882. {
  883. wake_up_interruptible(&pipe->wait);
  884. }
  885. static int fifo_open(struct inode *inode, struct file *filp)
  886. {
  887. struct pipe_inode_info *pipe;
  888. bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
  889. int kill = 0;
  890. int ret;
  891. filp->f_version = 0;
  892. spin_lock(&inode->i_lock);
  893. if (inode->i_pipe) {
  894. pipe = inode->i_pipe;
  895. pipe->files++;
  896. spin_unlock(&inode->i_lock);
  897. } else {
  898. spin_unlock(&inode->i_lock);
  899. pipe = alloc_pipe_info(inode);
  900. if (!pipe)
  901. return -ENOMEM;
  902. pipe->files = 1;
  903. spin_lock(&inode->i_lock);
  904. if (unlikely(inode->i_pipe)) {
  905. inode->i_pipe->files++;
  906. spin_unlock(&inode->i_lock);
  907. __free_pipe_info(pipe);
  908. pipe = inode->i_pipe;
  909. } else {
  910. inode->i_pipe = pipe;
  911. spin_unlock(&inode->i_lock);
  912. }
  913. }
  914. filp->private_data = pipe;
  915. /* OK, we have a pipe and it's pinned down */
  916. pipe_lock(pipe);
  917. /* We can only do regular read/write on fifos */
  918. filp->f_mode &= (FMODE_READ | FMODE_WRITE);
  919. switch (filp->f_mode) {
  920. case FMODE_READ:
  921. /*
  922. * O_RDONLY
  923. * POSIX.1 says that O_NONBLOCK means return with the FIFO
  924. * opened, even when there is no process writing the FIFO.
  925. */
  926. pipe->r_counter++;
  927. if (pipe->readers++ == 0)
  928. wake_up_partner(pipe);
  929. if (!is_pipe && !pipe->writers) {
  930. if ((filp->f_flags & O_NONBLOCK)) {
  931. /* suppress POLLHUP until we have
  932. * seen a writer */
  933. filp->f_version = pipe->w_counter;
  934. } else {
  935. if (wait_for_partner(pipe, &pipe->w_counter))
  936. goto err_rd;
  937. }
  938. }
  939. break;
  940. case FMODE_WRITE:
  941. /*
  942. * O_WRONLY
  943. * POSIX.1 says that O_NONBLOCK means return -1 with
  944. * errno=ENXIO when there is no process reading the FIFO.
  945. */
  946. ret = -ENXIO;
  947. if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
  948. goto err;
  949. pipe->w_counter++;
  950. if (!pipe->writers++)
  951. wake_up_partner(pipe);
  952. if (!is_pipe && !pipe->readers) {
  953. if (wait_for_partner(pipe, &pipe->r_counter))
  954. goto err_wr;
  955. }
  956. break;
  957. case FMODE_READ | FMODE_WRITE:
  958. /*
  959. * O_RDWR
  960. * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
  961. * This implementation will NEVER block on a O_RDWR open, since
  962. * the process can at least talk to itself.
  963. */
  964. pipe->readers++;
  965. pipe->writers++;
  966. pipe->r_counter++;
  967. pipe->w_counter++;
  968. if (pipe->readers == 1 || pipe->writers == 1)
  969. wake_up_partner(pipe);
  970. break;
  971. default:
  972. ret = -EINVAL;
  973. goto err;
  974. }
  975. /* Ok! */
  976. pipe_unlock(pipe);
  977. return 0;
  978. err_rd:
  979. if (!--pipe->readers)
  980. wake_up_interruptible(&pipe->wait);
  981. ret = -ERESTARTSYS;
  982. goto err;
  983. err_wr:
  984. if (!--pipe->writers)
  985. wake_up_interruptible(&pipe->wait);
  986. ret = -ERESTARTSYS;
  987. goto err;
  988. err:
  989. spin_lock(&inode->i_lock);
  990. if (!--pipe->files) {
  991. inode->i_pipe = NULL;
  992. kill = 1;
  993. }
  994. spin_unlock(&inode->i_lock);
  995. pipe_unlock(pipe);
  996. if (kill)
  997. __free_pipe_info(pipe);
  998. return ret;
  999. }
  1000. const struct file_operations pipefifo_fops = {
  1001. .open = fifo_open,
  1002. .llseek = no_llseek,
  1003. .read = do_sync_read,
  1004. .aio_read = pipe_read,
  1005. .write = do_sync_write,
  1006. .aio_write = pipe_write,
  1007. .poll = pipe_poll,
  1008. .unlocked_ioctl = pipe_ioctl,
  1009. .release = pipe_release,
  1010. .fasync = pipe_fasync,
  1011. };
  1012. /*
  1013. * Allocate a new array of pipe buffers and copy the info over. Returns the
  1014. * pipe size if successful, or return -ERROR on error.
  1015. */
  1016. static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
  1017. {
  1018. struct pipe_buffer *bufs;
  1019. /*
  1020. * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
  1021. * expect a lot of shrink+grow operations, just free and allocate
  1022. * again like we would do for growing. If the pipe currently
  1023. * contains more buffers than arg, then return busy.
  1024. */
  1025. if (nr_pages < pipe->nrbufs)
  1026. return -EBUSY;
  1027. bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
  1028. if (unlikely(!bufs))
  1029. return -ENOMEM;
  1030. /*
  1031. * The pipe array wraps around, so just start the new one at zero
  1032. * and adjust the indexes.
  1033. */
  1034. if (pipe->nrbufs) {
  1035. unsigned int tail;
  1036. unsigned int head;
  1037. tail = pipe->curbuf + pipe->nrbufs;
  1038. if (tail < pipe->buffers)
  1039. tail = 0;
  1040. else
  1041. tail &= (pipe->buffers - 1);
  1042. head = pipe->nrbufs - tail;
  1043. if (head)
  1044. memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
  1045. if (tail)
  1046. memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
  1047. }
  1048. pipe->curbuf = 0;
  1049. kfree(pipe->bufs);
  1050. pipe->bufs = bufs;
  1051. pipe->buffers = nr_pages;
  1052. return nr_pages * PAGE_SIZE;
  1053. }
  1054. /*
  1055. * Currently we rely on the pipe array holding a power-of-2 number
  1056. * of pages.
  1057. */
  1058. static inline unsigned int round_pipe_size(unsigned int size)
  1059. {
  1060. unsigned long nr_pages;
  1061. nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1062. return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
  1063. }
  1064. /*
  1065. * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
  1066. * will return an error.
  1067. */
  1068. int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
  1069. size_t *lenp, loff_t *ppos)
  1070. {
  1071. int ret;
  1072. ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
  1073. if (ret < 0 || !write)
  1074. return ret;
  1075. pipe_max_size = round_pipe_size(pipe_max_size);
  1076. return ret;
  1077. }
  1078. /*
  1079. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  1080. * location, so checking ->i_pipe is not enough to verify that this is a
  1081. * pipe.
  1082. */
  1083. struct pipe_inode_info *get_pipe_info(struct file *file)
  1084. {
  1085. return file->f_op == &pipefifo_fops ? file->private_data : NULL;
  1086. }
  1087. long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  1088. {
  1089. struct pipe_inode_info *pipe;
  1090. long ret;
  1091. pipe = get_pipe_info(file);
  1092. if (!pipe)
  1093. return -EBADF;
  1094. pipe_lock(pipe);
  1095. switch (cmd) {
  1096. case F_SETPIPE_SZ: {
  1097. unsigned int size, nr_pages;
  1098. size = round_pipe_size(arg);
  1099. nr_pages = size >> PAGE_SHIFT;
  1100. ret = -EINVAL;
  1101. if (!nr_pages)
  1102. goto out;
  1103. if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
  1104. ret = -EPERM;
  1105. goto out;
  1106. }
  1107. ret = pipe_set_size(pipe, nr_pages);
  1108. break;
  1109. }
  1110. case F_GETPIPE_SZ:
  1111. ret = pipe->buffers * PAGE_SIZE;
  1112. break;
  1113. default:
  1114. ret = -EINVAL;
  1115. break;
  1116. }
  1117. out:
  1118. pipe_unlock(pipe);
  1119. return ret;
  1120. }
  1121. static const struct super_operations pipefs_ops = {
  1122. .destroy_inode = free_inode_nonrcu,
  1123. .statfs = simple_statfs,
  1124. };
  1125. /*
  1126. * pipefs should _never_ be mounted by userland - too much of security hassle,
  1127. * no real gain from having the whole whorehouse mounted. So we don't need
  1128. * any operations on the root directory. However, we need a non-trivial
  1129. * d_name - pipe: will go nicely and kill the special-casing in procfs.
  1130. */
  1131. static struct dentry *pipefs_mount(struct file_system_type *fs_type,
  1132. int flags, const char *dev_name, void *data)
  1133. {
  1134. return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
  1135. &pipefs_dentry_operations, PIPEFS_MAGIC);
  1136. }
  1137. static struct file_system_type pipe_fs_type = {
  1138. .name = "pipefs",
  1139. .mount = pipefs_mount,
  1140. .kill_sb = kill_anon_super,
  1141. };
  1142. static int __init init_pipe_fs(void)
  1143. {
  1144. int err = register_filesystem(&pipe_fs_type);
  1145. if (!err) {
  1146. pipe_mnt = kern_mount(&pipe_fs_type);
  1147. if (IS_ERR(pipe_mnt)) {
  1148. err = PTR_ERR(pipe_mnt);
  1149. unregister_filesystem(&pipe_fs_type);
  1150. }
  1151. }
  1152. return err;
  1153. }
  1154. fs_initcall(init_pipe_fs);