pipe.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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 calls kmap_atomic() 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 = file_inode(filp);
  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 = file_inode(filp);
  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 = file_inode(filp);
  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 = file_inode(filp);
  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 = file_inode(filp);
  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 = file_inode(filp);
  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 = file_inode(filp);
  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. if (!(filp->f_mode & (FMODE_READ|FMODE_WRITE)))
  761. return -EINVAL;
  762. mutex_lock(&inode->i_mutex);
  763. if (inode->i_pipe) {
  764. ret = 0;
  765. if (filp->f_mode & FMODE_READ)
  766. inode->i_pipe->readers++;
  767. if (filp->f_mode & FMODE_WRITE)
  768. inode->i_pipe->writers++;
  769. }
  770. mutex_unlock(&inode->i_mutex);
  771. return ret;
  772. }
  773. /*
  774. * The file_operations structs are not static because they
  775. * are also used in linux/fs/fifo.c to do operations on FIFOs.
  776. *
  777. * Pipes reuse fifos' file_operations structs.
  778. */
  779. const struct file_operations read_pipefifo_fops = {
  780. .llseek = no_llseek,
  781. .read = do_sync_read,
  782. .aio_read = pipe_read,
  783. .write = bad_pipe_w,
  784. .poll = pipe_poll,
  785. .unlocked_ioctl = pipe_ioctl,
  786. .open = pipe_read_open,
  787. .release = pipe_read_release,
  788. .fasync = pipe_read_fasync,
  789. };
  790. const struct file_operations write_pipefifo_fops = {
  791. .llseek = no_llseek,
  792. .read = bad_pipe_r,
  793. .write = do_sync_write,
  794. .aio_write = pipe_write,
  795. .poll = pipe_poll,
  796. .unlocked_ioctl = pipe_ioctl,
  797. .open = pipe_write_open,
  798. .release = pipe_write_release,
  799. .fasync = pipe_write_fasync,
  800. };
  801. const struct file_operations rdwr_pipefifo_fops = {
  802. .llseek = no_llseek,
  803. .read = do_sync_read,
  804. .aio_read = pipe_read,
  805. .write = do_sync_write,
  806. .aio_write = pipe_write,
  807. .poll = pipe_poll,
  808. .unlocked_ioctl = pipe_ioctl,
  809. .open = pipe_rdwr_open,
  810. .release = pipe_rdwr_release,
  811. .fasync = pipe_rdwr_fasync,
  812. };
  813. struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
  814. {
  815. struct pipe_inode_info *pipe;
  816. pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
  817. if (pipe) {
  818. pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * PIPE_DEF_BUFFERS, GFP_KERNEL);
  819. if (pipe->bufs) {
  820. init_waitqueue_head(&pipe->wait);
  821. pipe->r_counter = pipe->w_counter = 1;
  822. pipe->inode = inode;
  823. pipe->buffers = PIPE_DEF_BUFFERS;
  824. return pipe;
  825. }
  826. kfree(pipe);
  827. }
  828. return NULL;
  829. }
  830. void __free_pipe_info(struct pipe_inode_info *pipe)
  831. {
  832. int i;
  833. for (i = 0; i < pipe->buffers; i++) {
  834. struct pipe_buffer *buf = pipe->bufs + i;
  835. if (buf->ops)
  836. buf->ops->release(pipe, buf);
  837. }
  838. if (pipe->tmp_page)
  839. __free_page(pipe->tmp_page);
  840. kfree(pipe->bufs);
  841. kfree(pipe);
  842. }
  843. void free_pipe_info(struct inode *inode)
  844. {
  845. __free_pipe_info(inode->i_pipe);
  846. inode->i_pipe = NULL;
  847. }
  848. static struct vfsmount *pipe_mnt __read_mostly;
  849. /*
  850. * pipefs_dname() is called from d_path().
  851. */
  852. static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
  853. {
  854. return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  855. dentry->d_inode->i_ino);
  856. }
  857. static const struct dentry_operations pipefs_dentry_operations = {
  858. .d_dname = pipefs_dname,
  859. };
  860. static struct inode * get_pipe_inode(void)
  861. {
  862. struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
  863. struct pipe_inode_info *pipe;
  864. if (!inode)
  865. goto fail_inode;
  866. inode->i_ino = get_next_ino();
  867. pipe = alloc_pipe_info(inode);
  868. if (!pipe)
  869. goto fail_iput;
  870. inode->i_pipe = pipe;
  871. pipe->readers = pipe->writers = 1;
  872. inode->i_fop = &rdwr_pipefifo_fops;
  873. /*
  874. * Mark the inode dirty from the very beginning,
  875. * that way it will never be moved to the dirty
  876. * list because "mark_inode_dirty()" will think
  877. * that it already _is_ on the dirty list.
  878. */
  879. inode->i_state = I_DIRTY;
  880. inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
  881. inode->i_uid = current_fsuid();
  882. inode->i_gid = current_fsgid();
  883. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  884. return inode;
  885. fail_iput:
  886. iput(inode);
  887. fail_inode:
  888. return NULL;
  889. }
  890. int create_pipe_files(struct file **res, int flags)
  891. {
  892. int err;
  893. struct inode *inode = get_pipe_inode();
  894. struct file *f;
  895. struct path path;
  896. static struct qstr name = { .name = "" };
  897. if (!inode)
  898. return -ENFILE;
  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 (IS_ERR(f))
  908. goto err_dentry;
  909. f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
  910. res[0] = alloc_file(&path, FMODE_READ, &read_pipefifo_fops);
  911. if (IS_ERR(res[0]))
  912. goto err_file;
  913. path_get(&path);
  914. res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  915. res[1] = f;
  916. return 0;
  917. err_file:
  918. put_filp(f);
  919. err_dentry:
  920. free_pipe_info(inode);
  921. path_put(&path);
  922. return err;
  923. err_inode:
  924. free_pipe_info(inode);
  925. iput(inode);
  926. return err;
  927. }
  928. static int __do_pipe_flags(int *fd, struct file **files, int flags)
  929. {
  930. int error;
  931. int fdw, fdr;
  932. if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
  933. return -EINVAL;
  934. error = create_pipe_files(files, flags);
  935. if (error)
  936. return error;
  937. error = get_unused_fd_flags(flags);
  938. if (error < 0)
  939. goto err_read_pipe;
  940. fdr = error;
  941. error = get_unused_fd_flags(flags);
  942. if (error < 0)
  943. goto err_fdr;
  944. fdw = error;
  945. audit_fd_pair(fdr, fdw);
  946. fd[0] = fdr;
  947. fd[1] = fdw;
  948. return 0;
  949. err_fdr:
  950. put_unused_fd(fdr);
  951. err_read_pipe:
  952. fput(files[0]);
  953. fput(files[1]);
  954. return error;
  955. }
  956. int do_pipe_flags(int *fd, int flags)
  957. {
  958. struct file *files[2];
  959. int error = __do_pipe_flags(fd, files, flags);
  960. if (!error) {
  961. fd_install(fd[0], files[0]);
  962. fd_install(fd[1], files[1]);
  963. }
  964. return error;
  965. }
  966. /*
  967. * sys_pipe() is the normal C calling standard for creating
  968. * a pipe. It's not the way Unix traditionally does this, though.
  969. */
  970. SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
  971. {
  972. struct file *files[2];
  973. int fd[2];
  974. int error;
  975. error = __do_pipe_flags(fd, files, flags);
  976. if (!error) {
  977. if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
  978. fput(files[0]);
  979. fput(files[1]);
  980. put_unused_fd(fd[0]);
  981. put_unused_fd(fd[1]);
  982. error = -EFAULT;
  983. } else {
  984. fd_install(fd[0], files[0]);
  985. fd_install(fd[1], files[1]);
  986. }
  987. }
  988. return error;
  989. }
  990. SYSCALL_DEFINE1(pipe, int __user *, fildes)
  991. {
  992. return sys_pipe2(fildes, 0);
  993. }
  994. /*
  995. * Allocate a new array of pipe buffers and copy the info over. Returns the
  996. * pipe size if successful, or return -ERROR on error.
  997. */
  998. static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
  999. {
  1000. struct pipe_buffer *bufs;
  1001. /*
  1002. * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
  1003. * expect a lot of shrink+grow operations, just free and allocate
  1004. * again like we would do for growing. If the pipe currently
  1005. * contains more buffers than arg, then return busy.
  1006. */
  1007. if (nr_pages < pipe->nrbufs)
  1008. return -EBUSY;
  1009. bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
  1010. if (unlikely(!bufs))
  1011. return -ENOMEM;
  1012. /*
  1013. * The pipe array wraps around, so just start the new one at zero
  1014. * and adjust the indexes.
  1015. */
  1016. if (pipe->nrbufs) {
  1017. unsigned int tail;
  1018. unsigned int head;
  1019. tail = pipe->curbuf + pipe->nrbufs;
  1020. if (tail < pipe->buffers)
  1021. tail = 0;
  1022. else
  1023. tail &= (pipe->buffers - 1);
  1024. head = pipe->nrbufs - tail;
  1025. if (head)
  1026. memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
  1027. if (tail)
  1028. memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
  1029. }
  1030. pipe->curbuf = 0;
  1031. kfree(pipe->bufs);
  1032. pipe->bufs = bufs;
  1033. pipe->buffers = nr_pages;
  1034. return nr_pages * PAGE_SIZE;
  1035. }
  1036. /*
  1037. * Currently we rely on the pipe array holding a power-of-2 number
  1038. * of pages.
  1039. */
  1040. static inline unsigned int round_pipe_size(unsigned int size)
  1041. {
  1042. unsigned long nr_pages;
  1043. nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1044. return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
  1045. }
  1046. /*
  1047. * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
  1048. * will return an error.
  1049. */
  1050. int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
  1051. size_t *lenp, loff_t *ppos)
  1052. {
  1053. int ret;
  1054. ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
  1055. if (ret < 0 || !write)
  1056. return ret;
  1057. pipe_max_size = round_pipe_size(pipe_max_size);
  1058. return ret;
  1059. }
  1060. /*
  1061. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  1062. * location, so checking ->i_pipe is not enough to verify that this is a
  1063. * pipe.
  1064. */
  1065. struct pipe_inode_info *get_pipe_info(struct file *file)
  1066. {
  1067. struct inode *i = file_inode(file);
  1068. return S_ISFIFO(i->i_mode) ? i->i_pipe : NULL;
  1069. }
  1070. long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  1071. {
  1072. struct pipe_inode_info *pipe;
  1073. long ret;
  1074. pipe = get_pipe_info(file);
  1075. if (!pipe)
  1076. return -EBADF;
  1077. mutex_lock(&pipe->inode->i_mutex);
  1078. switch (cmd) {
  1079. case F_SETPIPE_SZ: {
  1080. unsigned int size, nr_pages;
  1081. size = round_pipe_size(arg);
  1082. nr_pages = size >> PAGE_SHIFT;
  1083. ret = -EINVAL;
  1084. if (!nr_pages)
  1085. goto out;
  1086. if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
  1087. ret = -EPERM;
  1088. goto out;
  1089. }
  1090. ret = pipe_set_size(pipe, nr_pages);
  1091. break;
  1092. }
  1093. case F_GETPIPE_SZ:
  1094. ret = pipe->buffers * PAGE_SIZE;
  1095. break;
  1096. default:
  1097. ret = -EINVAL;
  1098. break;
  1099. }
  1100. out:
  1101. mutex_unlock(&pipe->inode->i_mutex);
  1102. return ret;
  1103. }
  1104. static const struct super_operations pipefs_ops = {
  1105. .destroy_inode = free_inode_nonrcu,
  1106. .statfs = simple_statfs,
  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. fs_initcall(init_pipe_fs);