pipe.c 29 KB

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