pipe.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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 occupies KM_USER0 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. file_update_time(filp);
  594. return ret;
  595. }
  596. static ssize_t
  597. bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
  598. {
  599. return -EBADF;
  600. }
  601. static ssize_t
  602. bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
  603. loff_t *ppos)
  604. {
  605. return -EBADF;
  606. }
  607. static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  608. {
  609. struct inode *inode = filp->f_path.dentry->d_inode;
  610. struct pipe_inode_info *pipe;
  611. int count, buf, nrbufs;
  612. switch (cmd) {
  613. case FIONREAD:
  614. mutex_lock(&inode->i_mutex);
  615. pipe = inode->i_pipe;
  616. count = 0;
  617. buf = pipe->curbuf;
  618. nrbufs = pipe->nrbufs;
  619. while (--nrbufs >= 0) {
  620. count += pipe->bufs[buf].len;
  621. buf = (buf+1) & (pipe->buffers - 1);
  622. }
  623. mutex_unlock(&inode->i_mutex);
  624. return put_user(count, (int __user *)arg);
  625. default:
  626. return -ENOIOCTLCMD;
  627. }
  628. }
  629. /* No kernel lock held - fine */
  630. static unsigned int
  631. pipe_poll(struct file *filp, poll_table *wait)
  632. {
  633. unsigned int mask;
  634. struct inode *inode = filp->f_path.dentry->d_inode;
  635. struct pipe_inode_info *pipe = inode->i_pipe;
  636. int nrbufs;
  637. poll_wait(filp, &pipe->wait, wait);
  638. /* Reading only -- no need for acquiring the semaphore. */
  639. nrbufs = pipe->nrbufs;
  640. mask = 0;
  641. if (filp->f_mode & FMODE_READ) {
  642. mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
  643. if (!pipe->writers && filp->f_version != pipe->w_counter)
  644. mask |= POLLHUP;
  645. }
  646. if (filp->f_mode & FMODE_WRITE) {
  647. mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
  648. /*
  649. * Most Unices do not set POLLERR for FIFOs but on Linux they
  650. * behave exactly like pipes for poll().
  651. */
  652. if (!pipe->readers)
  653. mask |= POLLERR;
  654. }
  655. return mask;
  656. }
  657. static int
  658. pipe_release(struct inode *inode, int decr, int decw)
  659. {
  660. struct pipe_inode_info *pipe;
  661. mutex_lock(&inode->i_mutex);
  662. pipe = inode->i_pipe;
  663. pipe->readers -= decr;
  664. pipe->writers -= decw;
  665. if (!pipe->readers && !pipe->writers) {
  666. free_pipe_info(inode);
  667. } else {
  668. wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
  669. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  670. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  671. }
  672. mutex_unlock(&inode->i_mutex);
  673. return 0;
  674. }
  675. static int
  676. pipe_read_fasync(int fd, struct file *filp, int on)
  677. {
  678. struct inode *inode = filp->f_path.dentry->d_inode;
  679. int retval;
  680. mutex_lock(&inode->i_mutex);
  681. retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
  682. mutex_unlock(&inode->i_mutex);
  683. return retval;
  684. }
  685. static int
  686. pipe_write_fasync(int fd, struct file *filp, int on)
  687. {
  688. struct inode *inode = filp->f_path.dentry->d_inode;
  689. int retval;
  690. mutex_lock(&inode->i_mutex);
  691. retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
  692. mutex_unlock(&inode->i_mutex);
  693. return retval;
  694. }
  695. static int
  696. pipe_rdwr_fasync(int fd, struct file *filp, int on)
  697. {
  698. struct inode *inode = filp->f_path.dentry->d_inode;
  699. struct pipe_inode_info *pipe = inode->i_pipe;
  700. int retval;
  701. mutex_lock(&inode->i_mutex);
  702. retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
  703. if (retval >= 0) {
  704. retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
  705. if (retval < 0) /* this can happen only if on == T */
  706. fasync_helper(-1, filp, 0, &pipe->fasync_readers);
  707. }
  708. mutex_unlock(&inode->i_mutex);
  709. return retval;
  710. }
  711. static int
  712. pipe_read_release(struct inode *inode, struct file *filp)
  713. {
  714. return pipe_release(inode, 1, 0);
  715. }
  716. static int
  717. pipe_write_release(struct inode *inode, struct file *filp)
  718. {
  719. return pipe_release(inode, 0, 1);
  720. }
  721. static int
  722. pipe_rdwr_release(struct inode *inode, struct file *filp)
  723. {
  724. int decr, decw;
  725. decr = (filp->f_mode & FMODE_READ) != 0;
  726. decw = (filp->f_mode & FMODE_WRITE) != 0;
  727. return pipe_release(inode, decr, decw);
  728. }
  729. static int
  730. pipe_read_open(struct inode *inode, struct file *filp)
  731. {
  732. int ret = -ENOENT;
  733. mutex_lock(&inode->i_mutex);
  734. if (inode->i_pipe) {
  735. ret = 0;
  736. inode->i_pipe->readers++;
  737. }
  738. mutex_unlock(&inode->i_mutex);
  739. return ret;
  740. }
  741. static int
  742. pipe_write_open(struct inode *inode, struct file *filp)
  743. {
  744. int ret = -ENOENT;
  745. mutex_lock(&inode->i_mutex);
  746. if (inode->i_pipe) {
  747. ret = 0;
  748. inode->i_pipe->writers++;
  749. }
  750. mutex_unlock(&inode->i_mutex);
  751. return ret;
  752. }
  753. static int
  754. pipe_rdwr_open(struct inode *inode, struct file *filp)
  755. {
  756. int ret = -ENOENT;
  757. mutex_lock(&inode->i_mutex);
  758. if (inode->i_pipe) {
  759. ret = 0;
  760. if (filp->f_mode & FMODE_READ)
  761. inode->i_pipe->readers++;
  762. if (filp->f_mode & FMODE_WRITE)
  763. inode->i_pipe->writers++;
  764. }
  765. mutex_unlock(&inode->i_mutex);
  766. return ret;
  767. }
  768. /*
  769. * The file_operations structs are not static because they
  770. * are also used in linux/fs/fifo.c to do operations on FIFOs.
  771. *
  772. * Pipes reuse fifos' file_operations structs.
  773. */
  774. const struct file_operations read_pipefifo_fops = {
  775. .llseek = no_llseek,
  776. .read = do_sync_read,
  777. .aio_read = pipe_read,
  778. .write = bad_pipe_w,
  779. .poll = pipe_poll,
  780. .unlocked_ioctl = pipe_ioctl,
  781. .open = pipe_read_open,
  782. .release = pipe_read_release,
  783. .fasync = pipe_read_fasync,
  784. };
  785. const struct file_operations write_pipefifo_fops = {
  786. .llseek = no_llseek,
  787. .read = bad_pipe_r,
  788. .write = do_sync_write,
  789. .aio_write = pipe_write,
  790. .poll = pipe_poll,
  791. .unlocked_ioctl = pipe_ioctl,
  792. .open = pipe_write_open,
  793. .release = pipe_write_release,
  794. .fasync = pipe_write_fasync,
  795. };
  796. const struct file_operations rdwr_pipefifo_fops = {
  797. .llseek = no_llseek,
  798. .read = do_sync_read,
  799. .aio_read = pipe_read,
  800. .write = do_sync_write,
  801. .aio_write = pipe_write,
  802. .poll = pipe_poll,
  803. .unlocked_ioctl = pipe_ioctl,
  804. .open = pipe_rdwr_open,
  805. .release = pipe_rdwr_release,
  806. .fasync = pipe_rdwr_fasync,
  807. };
  808. struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
  809. {
  810. struct pipe_inode_info *pipe;
  811. pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
  812. if (pipe) {
  813. pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * PIPE_DEF_BUFFERS, GFP_KERNEL);
  814. if (pipe->bufs) {
  815. init_waitqueue_head(&pipe->wait);
  816. pipe->r_counter = pipe->w_counter = 1;
  817. pipe->inode = inode;
  818. pipe->buffers = PIPE_DEF_BUFFERS;
  819. return pipe;
  820. }
  821. kfree(pipe);
  822. }
  823. return NULL;
  824. }
  825. void __free_pipe_info(struct pipe_inode_info *pipe)
  826. {
  827. int i;
  828. for (i = 0; i < pipe->buffers; i++) {
  829. struct pipe_buffer *buf = pipe->bufs + i;
  830. if (buf->ops)
  831. buf->ops->release(pipe, buf);
  832. }
  833. if (pipe->tmp_page)
  834. __free_page(pipe->tmp_page);
  835. kfree(pipe->bufs);
  836. kfree(pipe);
  837. }
  838. void free_pipe_info(struct inode *inode)
  839. {
  840. __free_pipe_info(inode->i_pipe);
  841. inode->i_pipe = NULL;
  842. }
  843. static struct vfsmount *pipe_mnt __read_mostly;
  844. /*
  845. * pipefs_dname() is called from d_path().
  846. */
  847. static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
  848. {
  849. return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  850. dentry->d_inode->i_ino);
  851. }
  852. static const struct dentry_operations pipefs_dentry_operations = {
  853. .d_dname = pipefs_dname,
  854. };
  855. static struct inode * get_pipe_inode(void)
  856. {
  857. struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
  858. struct pipe_inode_info *pipe;
  859. if (!inode)
  860. goto fail_inode;
  861. inode->i_ino = get_next_ino();
  862. pipe = alloc_pipe_info(inode);
  863. if (!pipe)
  864. goto fail_iput;
  865. inode->i_pipe = pipe;
  866. pipe->readers = pipe->writers = 1;
  867. inode->i_fop = &rdwr_pipefifo_fops;
  868. /*
  869. * Mark the inode dirty from the very beginning,
  870. * that way it will never be moved to the dirty
  871. * list because "mark_inode_dirty()" will think
  872. * that it already _is_ on the dirty list.
  873. */
  874. inode->i_state = I_DIRTY;
  875. inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
  876. inode->i_uid = current_fsuid();
  877. inode->i_gid = current_fsgid();
  878. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  879. return inode;
  880. fail_iput:
  881. iput(inode);
  882. fail_inode:
  883. return NULL;
  884. }
  885. struct file *create_write_pipe(int flags)
  886. {
  887. int err;
  888. struct inode *inode;
  889. struct file *f;
  890. struct path path;
  891. struct qstr name = { .name = "" };
  892. err = -ENFILE;
  893. inode = get_pipe_inode();
  894. if (!inode)
  895. goto err;
  896. err = -ENOMEM;
  897. path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
  898. if (!path.dentry)
  899. goto err_inode;
  900. path.mnt = mntget(pipe_mnt);
  901. d_instantiate(path.dentry, inode);
  902. err = -ENFILE;
  903. f = alloc_file(&path, FMODE_WRITE, &write_pipefifo_fops);
  904. if (!f)
  905. goto err_dentry;
  906. f->f_mapping = inode->i_mapping;
  907. f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
  908. f->f_version = 0;
  909. return f;
  910. err_dentry:
  911. free_pipe_info(inode);
  912. path_put(&path);
  913. return ERR_PTR(err);
  914. err_inode:
  915. free_pipe_info(inode);
  916. iput(inode);
  917. err:
  918. return ERR_PTR(err);
  919. }
  920. void free_write_pipe(struct file *f)
  921. {
  922. free_pipe_info(f->f_dentry->d_inode);
  923. path_put(&f->f_path);
  924. put_filp(f);
  925. }
  926. struct file *create_read_pipe(struct file *wrf, int flags)
  927. {
  928. /* Grab pipe from the writer */
  929. struct file *f = alloc_file(&wrf->f_path, FMODE_READ,
  930. &read_pipefifo_fops);
  931. if (!f)
  932. return ERR_PTR(-ENFILE);
  933. path_get(&wrf->f_path);
  934. f->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  935. return f;
  936. }
  937. int do_pipe_flags(int *fd, int flags)
  938. {
  939. struct file *fw, *fr;
  940. int error;
  941. int fdw, fdr;
  942. if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
  943. return -EINVAL;
  944. fw = create_write_pipe(flags);
  945. if (IS_ERR(fw))
  946. return PTR_ERR(fw);
  947. fr = create_read_pipe(fw, flags);
  948. error = PTR_ERR(fr);
  949. if (IS_ERR(fr))
  950. goto err_write_pipe;
  951. error = get_unused_fd_flags(flags);
  952. if (error < 0)
  953. goto err_read_pipe;
  954. fdr = error;
  955. error = get_unused_fd_flags(flags);
  956. if (error < 0)
  957. goto err_fdr;
  958. fdw = error;
  959. audit_fd_pair(fdr, fdw);
  960. fd_install(fdr, fr);
  961. fd_install(fdw, fw);
  962. fd[0] = fdr;
  963. fd[1] = fdw;
  964. return 0;
  965. err_fdr:
  966. put_unused_fd(fdr);
  967. err_read_pipe:
  968. path_put(&fr->f_path);
  969. put_filp(fr);
  970. err_write_pipe:
  971. free_write_pipe(fw);
  972. return error;
  973. }
  974. /*
  975. * sys_pipe() is the normal C calling standard for creating
  976. * a pipe. It's not the way Unix traditionally does this, though.
  977. */
  978. SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
  979. {
  980. int fd[2];
  981. int error;
  982. error = do_pipe_flags(fd, flags);
  983. if (!error) {
  984. if (copy_to_user(fildes, fd, sizeof(fd))) {
  985. sys_close(fd[0]);
  986. sys_close(fd[1]);
  987. error = -EFAULT;
  988. }
  989. }
  990. return error;
  991. }
  992. SYSCALL_DEFINE1(pipe, int __user *, fildes)
  993. {
  994. return sys_pipe2(fildes, 0);
  995. }
  996. /*
  997. * Allocate a new array of pipe buffers and copy the info over. Returns the
  998. * pipe size if successful, or return -ERROR on error.
  999. */
  1000. static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
  1001. {
  1002. struct pipe_buffer *bufs;
  1003. /*
  1004. * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
  1005. * expect a lot of shrink+grow operations, just free and allocate
  1006. * again like we would do for growing. If the pipe currently
  1007. * contains more buffers than arg, then return busy.
  1008. */
  1009. if (nr_pages < pipe->nrbufs)
  1010. return -EBUSY;
  1011. bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
  1012. if (unlikely(!bufs))
  1013. return -ENOMEM;
  1014. /*
  1015. * The pipe array wraps around, so just start the new one at zero
  1016. * and adjust the indexes.
  1017. */
  1018. if (pipe->nrbufs) {
  1019. unsigned int tail;
  1020. unsigned int head;
  1021. tail = pipe->curbuf + pipe->nrbufs;
  1022. if (tail < pipe->buffers)
  1023. tail = 0;
  1024. else
  1025. tail &= (pipe->buffers - 1);
  1026. head = pipe->nrbufs - tail;
  1027. if (head)
  1028. memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
  1029. if (tail)
  1030. memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
  1031. }
  1032. pipe->curbuf = 0;
  1033. kfree(pipe->bufs);
  1034. pipe->bufs = bufs;
  1035. pipe->buffers = nr_pages;
  1036. return nr_pages * PAGE_SIZE;
  1037. }
  1038. /*
  1039. * Currently we rely on the pipe array holding a power-of-2 number
  1040. * of pages.
  1041. */
  1042. static inline unsigned int round_pipe_size(unsigned int size)
  1043. {
  1044. unsigned long nr_pages;
  1045. nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1046. return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
  1047. }
  1048. /*
  1049. * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
  1050. * will return an error.
  1051. */
  1052. int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
  1053. size_t *lenp, loff_t *ppos)
  1054. {
  1055. int ret;
  1056. ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
  1057. if (ret < 0 || !write)
  1058. return ret;
  1059. pipe_max_size = round_pipe_size(pipe_max_size);
  1060. return ret;
  1061. }
  1062. /*
  1063. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  1064. * location, so checking ->i_pipe is not enough to verify that this is a
  1065. * pipe.
  1066. */
  1067. struct pipe_inode_info *get_pipe_info(struct file *file)
  1068. {
  1069. struct inode *i = file->f_path.dentry->d_inode;
  1070. return S_ISFIFO(i->i_mode) ? i->i_pipe : NULL;
  1071. }
  1072. long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  1073. {
  1074. struct pipe_inode_info *pipe;
  1075. long ret;
  1076. pipe = get_pipe_info(file);
  1077. if (!pipe)
  1078. return -EBADF;
  1079. mutex_lock(&pipe->inode->i_mutex);
  1080. switch (cmd) {
  1081. case F_SETPIPE_SZ: {
  1082. unsigned int size, nr_pages;
  1083. size = round_pipe_size(arg);
  1084. nr_pages = size >> PAGE_SHIFT;
  1085. ret = -EINVAL;
  1086. if (!nr_pages)
  1087. goto out;
  1088. if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
  1089. ret = -EPERM;
  1090. goto out;
  1091. }
  1092. ret = pipe_set_size(pipe, nr_pages);
  1093. break;
  1094. }
  1095. case F_GETPIPE_SZ:
  1096. ret = pipe->buffers * PAGE_SIZE;
  1097. break;
  1098. default:
  1099. ret = -EINVAL;
  1100. break;
  1101. }
  1102. out:
  1103. mutex_unlock(&pipe->inode->i_mutex);
  1104. return ret;
  1105. }
  1106. static const struct super_operations pipefs_ops = {
  1107. .destroy_inode = free_inode_nonrcu,
  1108. .statfs = simple_statfs,
  1109. };
  1110. /*
  1111. * pipefs should _never_ be mounted by userland - too much of security hassle,
  1112. * no real gain from having the whole whorehouse mounted. So we don't need
  1113. * any operations on the root directory. However, we need a non-trivial
  1114. * d_name - pipe: will go nicely and kill the special-casing in procfs.
  1115. */
  1116. static struct dentry *pipefs_mount(struct file_system_type *fs_type,
  1117. int flags, const char *dev_name, void *data)
  1118. {
  1119. return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
  1120. &pipefs_dentry_operations, PIPEFS_MAGIC);
  1121. }
  1122. static struct file_system_type pipe_fs_type = {
  1123. .name = "pipefs",
  1124. .mount = pipefs_mount,
  1125. .kill_sb = kill_anon_super,
  1126. };
  1127. static int __init init_pipe_fs(void)
  1128. {
  1129. int err = register_filesystem(&pipe_fs_type);
  1130. if (!err) {
  1131. pipe_mnt = kern_mount(&pipe_fs_type);
  1132. if (IS_ERR(pipe_mnt)) {
  1133. err = PTR_ERR(pipe_mnt);
  1134. unregister_filesystem(&pipe_fs_type);
  1135. }
  1136. }
  1137. return err;
  1138. }
  1139. fs_initcall(init_pipe_fs);