pipe.c 27 KB

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