pipe.c 29 KB

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