pipe.c 29 KB

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