pipe.c 29 KB

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