pipe.c 27 KB

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