pipe.c 27 KB

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