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