pipe.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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 void anon_pipe_buf_release(struct pipe_inode_info *pipe,
  104. struct pipe_buffer *buf)
  105. {
  106. struct page *page = buf->page;
  107. /*
  108. * If nobody else uses this page, and we don't already have a
  109. * temporary page, let's keep track of it as a one-deep
  110. * allocation cache. (Otherwise just release our reference to it)
  111. */
  112. if (page_count(page) == 1 && !pipe->tmp_page)
  113. pipe->tmp_page = page;
  114. else
  115. page_cache_release(page);
  116. }
  117. /**
  118. * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
  119. * @pipe: the pipe that the buffer belongs to
  120. * @buf: the buffer to attempt to steal
  121. *
  122. * Description:
  123. * This function attempts to steal the &struct page attached to
  124. * @buf. If successful, this function returns 0 and returns with
  125. * the page locked. The caller may then reuse the page for whatever
  126. * he wishes; the typical use is insertion into a different file
  127. * page cache.
  128. */
  129. int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
  130. struct pipe_buffer *buf)
  131. {
  132. struct page *page = buf->page;
  133. /*
  134. * A reference of one is golden, that means that the owner of this
  135. * page is the only one holding a reference to it. lock the page
  136. * and return OK.
  137. */
  138. if (page_count(page) == 1) {
  139. lock_page(page);
  140. return 0;
  141. }
  142. return 1;
  143. }
  144. EXPORT_SYMBOL(generic_pipe_buf_steal);
  145. /**
  146. * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
  147. * @pipe: the pipe that the buffer belongs to
  148. * @buf: the buffer to get a reference to
  149. *
  150. * Description:
  151. * This function grabs an extra reference to @buf. It's used in
  152. * in the tee() system call, when we duplicate the buffers in one
  153. * pipe into another.
  154. */
  155. void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
  156. {
  157. page_cache_get(buf->page);
  158. }
  159. EXPORT_SYMBOL(generic_pipe_buf_get);
  160. /**
  161. * generic_pipe_buf_confirm - verify contents of the pipe buffer
  162. * @info: the pipe that the buffer belongs to
  163. * @buf: the buffer to confirm
  164. *
  165. * Description:
  166. * This function does nothing, because the generic pipe code uses
  167. * pages that are always good when inserted into the pipe.
  168. */
  169. int generic_pipe_buf_confirm(struct pipe_inode_info *info,
  170. struct pipe_buffer *buf)
  171. {
  172. return 0;
  173. }
  174. EXPORT_SYMBOL(generic_pipe_buf_confirm);
  175. /**
  176. * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
  177. * @pipe: the pipe that the buffer belongs to
  178. * @buf: the buffer to put a reference to
  179. *
  180. * Description:
  181. * This function releases a reference to @buf.
  182. */
  183. void generic_pipe_buf_release(struct pipe_inode_info *pipe,
  184. struct pipe_buffer *buf)
  185. {
  186. page_cache_release(buf->page);
  187. }
  188. EXPORT_SYMBOL(generic_pipe_buf_release);
  189. static const struct pipe_buf_operations anon_pipe_buf_ops = {
  190. .can_merge = 1,
  191. .confirm = generic_pipe_buf_confirm,
  192. .release = anon_pipe_buf_release,
  193. .steal = generic_pipe_buf_steal,
  194. .get = generic_pipe_buf_get,
  195. };
  196. static const struct pipe_buf_operations packet_pipe_buf_ops = {
  197. .can_merge = 0,
  198. .confirm = generic_pipe_buf_confirm,
  199. .release = anon_pipe_buf_release,
  200. .steal = generic_pipe_buf_steal,
  201. .get = generic_pipe_buf_get,
  202. };
  203. static ssize_t
  204. pipe_read(struct kiocb *iocb, struct iov_iter *to)
  205. {
  206. size_t total_len = iov_iter_count(to);
  207. struct file *filp = iocb->ki_filp;
  208. struct pipe_inode_info *pipe = filp->private_data;
  209. int do_wakeup;
  210. ssize_t ret;
  211. /* Null read succeeds. */
  212. if (unlikely(total_len == 0))
  213. return 0;
  214. do_wakeup = 0;
  215. ret = 0;
  216. __pipe_lock(pipe);
  217. for (;;) {
  218. int bufs = pipe->nrbufs;
  219. if (bufs) {
  220. int curbuf = pipe->curbuf;
  221. struct pipe_buffer *buf = pipe->bufs + curbuf;
  222. const struct pipe_buf_operations *ops = buf->ops;
  223. size_t chars = buf->len;
  224. size_t written;
  225. int error;
  226. if (chars > total_len)
  227. chars = total_len;
  228. error = ops->confirm(pipe, buf);
  229. if (error) {
  230. if (!ret)
  231. ret = error;
  232. break;
  233. }
  234. written = copy_page_to_iter(buf->page, buf->offset, chars, to);
  235. if (unlikely(written < chars)) {
  236. if (!ret)
  237. ret = -EFAULT;
  238. break;
  239. }
  240. ret += chars;
  241. buf->offset += chars;
  242. buf->len -= chars;
  243. /* Was it a packet buffer? Clean up and exit */
  244. if (buf->flags & PIPE_BUF_FLAG_PACKET) {
  245. total_len = chars;
  246. buf->len = 0;
  247. }
  248. if (!buf->len) {
  249. buf->ops = NULL;
  250. ops->release(pipe, buf);
  251. curbuf = (curbuf + 1) & (pipe->buffers - 1);
  252. pipe->curbuf = curbuf;
  253. pipe->nrbufs = --bufs;
  254. do_wakeup = 1;
  255. }
  256. total_len -= chars;
  257. if (!total_len)
  258. break; /* common path: read succeeded */
  259. }
  260. if (bufs) /* More to do? */
  261. continue;
  262. if (!pipe->writers)
  263. break;
  264. if (!pipe->waiting_writers) {
  265. /* syscall merging: Usually we must not sleep
  266. * if O_NONBLOCK is set, or if we got some data.
  267. * But if a writer sleeps in kernel space, then
  268. * we can wait for that data without violating POSIX.
  269. */
  270. if (ret)
  271. break;
  272. if (filp->f_flags & O_NONBLOCK) {
  273. ret = -EAGAIN;
  274. break;
  275. }
  276. }
  277. if (signal_pending(current)) {
  278. if (!ret)
  279. ret = -ERESTARTSYS;
  280. break;
  281. }
  282. if (do_wakeup) {
  283. wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
  284. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  285. }
  286. pipe_wait(pipe);
  287. }
  288. __pipe_unlock(pipe);
  289. /* Signal writers asynchronously that there is more room. */
  290. if (do_wakeup) {
  291. wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
  292. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  293. }
  294. if (ret > 0)
  295. file_accessed(filp);
  296. return ret;
  297. }
  298. static inline int is_packetized(struct file *file)
  299. {
  300. return (file->f_flags & O_DIRECT) != 0;
  301. }
  302. static ssize_t
  303. pipe_write(struct kiocb *iocb, struct iov_iter *from)
  304. {
  305. struct file *filp = iocb->ki_filp;
  306. struct pipe_inode_info *pipe = filp->private_data;
  307. ssize_t ret = 0;
  308. int do_wakeup = 0;
  309. size_t total_len = iov_iter_count(from);
  310. ssize_t chars;
  311. /* Null write succeeds. */
  312. if (unlikely(total_len == 0))
  313. return 0;
  314. __pipe_lock(pipe);
  315. if (!pipe->readers) {
  316. send_sig(SIGPIPE, current, 0);
  317. ret = -EPIPE;
  318. goto out;
  319. }
  320. /* We try to merge small writes */
  321. chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
  322. if (pipe->nrbufs && chars != 0) {
  323. int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
  324. (pipe->buffers - 1);
  325. struct pipe_buffer *buf = pipe->bufs + lastbuf;
  326. const struct pipe_buf_operations *ops = buf->ops;
  327. int offset = buf->offset + buf->len;
  328. if (ops->can_merge && offset + chars <= PAGE_SIZE) {
  329. int error = ops->confirm(pipe, buf);
  330. if (error)
  331. goto out;
  332. ret = copy_page_from_iter(buf->page, offset, chars, from);
  333. if (unlikely(ret < chars)) {
  334. error = -EFAULT;
  335. goto out;
  336. }
  337. do_wakeup = 1;
  338. buf->len += chars;
  339. ret = chars;
  340. if (!iov_iter_count(from))
  341. goto out;
  342. }
  343. }
  344. for (;;) {
  345. int bufs;
  346. if (!pipe->readers) {
  347. send_sig(SIGPIPE, current, 0);
  348. if (!ret)
  349. ret = -EPIPE;
  350. break;
  351. }
  352. bufs = pipe->nrbufs;
  353. if (bufs < pipe->buffers) {
  354. int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
  355. struct pipe_buffer *buf = pipe->bufs + newbuf;
  356. struct page *page = pipe->tmp_page;
  357. int copied;
  358. if (!page) {
  359. page = alloc_page(GFP_HIGHUSER);
  360. if (unlikely(!page)) {
  361. ret = ret ? : -ENOMEM;
  362. break;
  363. }
  364. pipe->tmp_page = page;
  365. }
  366. /* Always wake up, even if the copy fails. Otherwise
  367. * we lock up (O_NONBLOCK-)readers that sleep due to
  368. * syscall merging.
  369. * FIXME! Is this really true?
  370. */
  371. do_wakeup = 1;
  372. copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
  373. if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) {
  374. if (!ret)
  375. ret = -EFAULT;
  376. break;
  377. }
  378. ret += copied;
  379. /* Insert it into the buffer array */
  380. buf->page = page;
  381. buf->ops = &anon_pipe_buf_ops;
  382. buf->offset = 0;
  383. buf->len = copied;
  384. buf->flags = 0;
  385. if (is_packetized(filp)) {
  386. buf->ops = &packet_pipe_buf_ops;
  387. buf->flags = PIPE_BUF_FLAG_PACKET;
  388. }
  389. pipe->nrbufs = ++bufs;
  390. pipe->tmp_page = NULL;
  391. if (!iov_iter_count(from))
  392. break;
  393. }
  394. if (bufs < pipe->buffers)
  395. continue;
  396. if (filp->f_flags & O_NONBLOCK) {
  397. if (!ret)
  398. ret = -EAGAIN;
  399. break;
  400. }
  401. if (signal_pending(current)) {
  402. if (!ret)
  403. ret = -ERESTARTSYS;
  404. break;
  405. }
  406. if (do_wakeup) {
  407. wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
  408. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  409. do_wakeup = 0;
  410. }
  411. pipe->waiting_writers++;
  412. pipe_wait(pipe);
  413. pipe->waiting_writers--;
  414. }
  415. out:
  416. __pipe_unlock(pipe);
  417. if (do_wakeup) {
  418. wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
  419. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  420. }
  421. if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
  422. int err = file_update_time(filp);
  423. if (err)
  424. ret = err;
  425. sb_end_write(file_inode(filp)->i_sb);
  426. }
  427. return ret;
  428. }
  429. static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  430. {
  431. struct pipe_inode_info *pipe = filp->private_data;
  432. int count, buf, nrbufs;
  433. switch (cmd) {
  434. case FIONREAD:
  435. __pipe_lock(pipe);
  436. count = 0;
  437. buf = pipe->curbuf;
  438. nrbufs = pipe->nrbufs;
  439. while (--nrbufs >= 0) {
  440. count += pipe->bufs[buf].len;
  441. buf = (buf+1) & (pipe->buffers - 1);
  442. }
  443. __pipe_unlock(pipe);
  444. return put_user(count, (int __user *)arg);
  445. default:
  446. return -ENOIOCTLCMD;
  447. }
  448. }
  449. /* No kernel lock held - fine */
  450. static unsigned int
  451. pipe_poll(struct file *filp, poll_table *wait)
  452. {
  453. unsigned int mask;
  454. struct pipe_inode_info *pipe = filp->private_data;
  455. int nrbufs;
  456. poll_wait(filp, &pipe->wait, wait);
  457. /* Reading only -- no need for acquiring the semaphore. */
  458. nrbufs = pipe->nrbufs;
  459. mask = 0;
  460. if (filp->f_mode & FMODE_READ) {
  461. mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
  462. if (!pipe->writers && filp->f_version != pipe->w_counter)
  463. mask |= POLLHUP;
  464. }
  465. if (filp->f_mode & FMODE_WRITE) {
  466. mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
  467. /*
  468. * Most Unices do not set POLLERR for FIFOs but on Linux they
  469. * behave exactly like pipes for poll().
  470. */
  471. if (!pipe->readers)
  472. mask |= POLLERR;
  473. }
  474. return mask;
  475. }
  476. static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
  477. {
  478. int kill = 0;
  479. spin_lock(&inode->i_lock);
  480. if (!--pipe->files) {
  481. inode->i_pipe = NULL;
  482. kill = 1;
  483. }
  484. spin_unlock(&inode->i_lock);
  485. if (kill)
  486. free_pipe_info(pipe);
  487. }
  488. static int
  489. pipe_release(struct inode *inode, struct file *file)
  490. {
  491. struct pipe_inode_info *pipe = file->private_data;
  492. __pipe_lock(pipe);
  493. if (file->f_mode & FMODE_READ)
  494. pipe->readers--;
  495. if (file->f_mode & FMODE_WRITE)
  496. pipe->writers--;
  497. if (pipe->readers || pipe->writers) {
  498. wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
  499. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  500. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  501. }
  502. __pipe_unlock(pipe);
  503. put_pipe_info(inode, pipe);
  504. return 0;
  505. }
  506. static int
  507. pipe_fasync(int fd, struct file *filp, int on)
  508. {
  509. struct pipe_inode_info *pipe = filp->private_data;
  510. int retval = 0;
  511. __pipe_lock(pipe);
  512. if (filp->f_mode & FMODE_READ)
  513. retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
  514. if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
  515. retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
  516. if (retval < 0 && (filp->f_mode & FMODE_READ))
  517. /* this can happen only if on == T */
  518. fasync_helper(-1, filp, 0, &pipe->fasync_readers);
  519. }
  520. __pipe_unlock(pipe);
  521. return retval;
  522. }
  523. struct pipe_inode_info *alloc_pipe_info(void)
  524. {
  525. struct pipe_inode_info *pipe;
  526. pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
  527. if (pipe) {
  528. pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * PIPE_DEF_BUFFERS, GFP_KERNEL);
  529. if (pipe->bufs) {
  530. init_waitqueue_head(&pipe->wait);
  531. pipe->r_counter = pipe->w_counter = 1;
  532. pipe->buffers = PIPE_DEF_BUFFERS;
  533. mutex_init(&pipe->mutex);
  534. return pipe;
  535. }
  536. kfree(pipe);
  537. }
  538. return NULL;
  539. }
  540. void free_pipe_info(struct pipe_inode_info *pipe)
  541. {
  542. int i;
  543. for (i = 0; i < pipe->buffers; i++) {
  544. struct pipe_buffer *buf = pipe->bufs + i;
  545. if (buf->ops)
  546. buf->ops->release(pipe, buf);
  547. }
  548. if (pipe->tmp_page)
  549. __free_page(pipe->tmp_page);
  550. kfree(pipe->bufs);
  551. kfree(pipe);
  552. }
  553. static struct vfsmount *pipe_mnt __read_mostly;
  554. /*
  555. * pipefs_dname() is called from d_path().
  556. */
  557. static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
  558. {
  559. return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  560. dentry->d_inode->i_ino);
  561. }
  562. static const struct dentry_operations pipefs_dentry_operations = {
  563. .d_dname = pipefs_dname,
  564. };
  565. static struct inode * get_pipe_inode(void)
  566. {
  567. struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
  568. struct pipe_inode_info *pipe;
  569. if (!inode)
  570. goto fail_inode;
  571. inode->i_ino = get_next_ino();
  572. pipe = alloc_pipe_info();
  573. if (!pipe)
  574. goto fail_iput;
  575. inode->i_pipe = pipe;
  576. pipe->files = 2;
  577. pipe->readers = pipe->writers = 1;
  578. inode->i_fop = &pipefifo_fops;
  579. /*
  580. * Mark the inode dirty from the very beginning,
  581. * that way it will never be moved to the dirty
  582. * list because "mark_inode_dirty()" will think
  583. * that it already _is_ on the dirty list.
  584. */
  585. inode->i_state = I_DIRTY;
  586. inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
  587. inode->i_uid = current_fsuid();
  588. inode->i_gid = current_fsgid();
  589. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  590. return inode;
  591. fail_iput:
  592. iput(inode);
  593. fail_inode:
  594. return NULL;
  595. }
  596. int create_pipe_files(struct file **res, int flags)
  597. {
  598. int err;
  599. struct inode *inode = get_pipe_inode();
  600. struct file *f;
  601. struct path path;
  602. static struct qstr name = { .name = "" };
  603. if (!inode)
  604. return -ENFILE;
  605. err = -ENOMEM;
  606. path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
  607. if (!path.dentry)
  608. goto err_inode;
  609. path.mnt = mntget(pipe_mnt);
  610. d_instantiate(path.dentry, inode);
  611. err = -ENFILE;
  612. f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops);
  613. if (IS_ERR(f))
  614. goto err_dentry;
  615. f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
  616. f->private_data = inode->i_pipe;
  617. res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops);
  618. if (IS_ERR(res[0]))
  619. goto err_file;
  620. path_get(&path);
  621. res[0]->private_data = inode->i_pipe;
  622. res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  623. res[1] = f;
  624. return 0;
  625. err_file:
  626. put_filp(f);
  627. err_dentry:
  628. free_pipe_info(inode->i_pipe);
  629. path_put(&path);
  630. return err;
  631. err_inode:
  632. free_pipe_info(inode->i_pipe);
  633. iput(inode);
  634. return err;
  635. }
  636. static int __do_pipe_flags(int *fd, struct file **files, int flags)
  637. {
  638. int error;
  639. int fdw, fdr;
  640. if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
  641. return -EINVAL;
  642. error = create_pipe_files(files, flags);
  643. if (error)
  644. return error;
  645. error = get_unused_fd_flags(flags);
  646. if (error < 0)
  647. goto err_read_pipe;
  648. fdr = error;
  649. error = get_unused_fd_flags(flags);
  650. if (error < 0)
  651. goto err_fdr;
  652. fdw = error;
  653. audit_fd_pair(fdr, fdw);
  654. fd[0] = fdr;
  655. fd[1] = fdw;
  656. return 0;
  657. err_fdr:
  658. put_unused_fd(fdr);
  659. err_read_pipe:
  660. fput(files[0]);
  661. fput(files[1]);
  662. return error;
  663. }
  664. int do_pipe_flags(int *fd, int flags)
  665. {
  666. struct file *files[2];
  667. int error = __do_pipe_flags(fd, files, flags);
  668. if (!error) {
  669. fd_install(fd[0], files[0]);
  670. fd_install(fd[1], files[1]);
  671. }
  672. return error;
  673. }
  674. /*
  675. * sys_pipe() is the normal C calling standard for creating
  676. * a pipe. It's not the way Unix traditionally does this, though.
  677. */
  678. SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
  679. {
  680. struct file *files[2];
  681. int fd[2];
  682. int error;
  683. error = __do_pipe_flags(fd, files, flags);
  684. if (!error) {
  685. if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
  686. fput(files[0]);
  687. fput(files[1]);
  688. put_unused_fd(fd[0]);
  689. put_unused_fd(fd[1]);
  690. error = -EFAULT;
  691. } else {
  692. fd_install(fd[0], files[0]);
  693. fd_install(fd[1], files[1]);
  694. }
  695. }
  696. return error;
  697. }
  698. SYSCALL_DEFINE1(pipe, int __user *, fildes)
  699. {
  700. return sys_pipe2(fildes, 0);
  701. }
  702. static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
  703. {
  704. int cur = *cnt;
  705. while (cur == *cnt) {
  706. pipe_wait(pipe);
  707. if (signal_pending(current))
  708. break;
  709. }
  710. return cur == *cnt ? -ERESTARTSYS : 0;
  711. }
  712. static void wake_up_partner(struct pipe_inode_info *pipe)
  713. {
  714. wake_up_interruptible(&pipe->wait);
  715. }
  716. static int fifo_open(struct inode *inode, struct file *filp)
  717. {
  718. struct pipe_inode_info *pipe;
  719. bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
  720. int ret;
  721. filp->f_version = 0;
  722. spin_lock(&inode->i_lock);
  723. if (inode->i_pipe) {
  724. pipe = inode->i_pipe;
  725. pipe->files++;
  726. spin_unlock(&inode->i_lock);
  727. } else {
  728. spin_unlock(&inode->i_lock);
  729. pipe = alloc_pipe_info();
  730. if (!pipe)
  731. return -ENOMEM;
  732. pipe->files = 1;
  733. spin_lock(&inode->i_lock);
  734. if (unlikely(inode->i_pipe)) {
  735. inode->i_pipe->files++;
  736. spin_unlock(&inode->i_lock);
  737. free_pipe_info(pipe);
  738. pipe = inode->i_pipe;
  739. } else {
  740. inode->i_pipe = pipe;
  741. spin_unlock(&inode->i_lock);
  742. }
  743. }
  744. filp->private_data = pipe;
  745. /* OK, we have a pipe and it's pinned down */
  746. __pipe_lock(pipe);
  747. /* We can only do regular read/write on fifos */
  748. filp->f_mode &= (FMODE_READ | FMODE_WRITE);
  749. switch (filp->f_mode) {
  750. case FMODE_READ:
  751. /*
  752. * O_RDONLY
  753. * POSIX.1 says that O_NONBLOCK means return with the FIFO
  754. * opened, even when there is no process writing the FIFO.
  755. */
  756. pipe->r_counter++;
  757. if (pipe->readers++ == 0)
  758. wake_up_partner(pipe);
  759. if (!is_pipe && !pipe->writers) {
  760. if ((filp->f_flags & O_NONBLOCK)) {
  761. /* suppress POLLHUP until we have
  762. * seen a writer */
  763. filp->f_version = pipe->w_counter;
  764. } else {
  765. if (wait_for_partner(pipe, &pipe->w_counter))
  766. goto err_rd;
  767. }
  768. }
  769. break;
  770. case FMODE_WRITE:
  771. /*
  772. * O_WRONLY
  773. * POSIX.1 says that O_NONBLOCK means return -1 with
  774. * errno=ENXIO when there is no process reading the FIFO.
  775. */
  776. ret = -ENXIO;
  777. if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
  778. goto err;
  779. pipe->w_counter++;
  780. if (!pipe->writers++)
  781. wake_up_partner(pipe);
  782. if (!is_pipe && !pipe->readers) {
  783. if (wait_for_partner(pipe, &pipe->r_counter))
  784. goto err_wr;
  785. }
  786. break;
  787. case FMODE_READ | FMODE_WRITE:
  788. /*
  789. * O_RDWR
  790. * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
  791. * This implementation will NEVER block on a O_RDWR open, since
  792. * the process can at least talk to itself.
  793. */
  794. pipe->readers++;
  795. pipe->writers++;
  796. pipe->r_counter++;
  797. pipe->w_counter++;
  798. if (pipe->readers == 1 || pipe->writers == 1)
  799. wake_up_partner(pipe);
  800. break;
  801. default:
  802. ret = -EINVAL;
  803. goto err;
  804. }
  805. /* Ok! */
  806. __pipe_unlock(pipe);
  807. return 0;
  808. err_rd:
  809. if (!--pipe->readers)
  810. wake_up_interruptible(&pipe->wait);
  811. ret = -ERESTARTSYS;
  812. goto err;
  813. err_wr:
  814. if (!--pipe->writers)
  815. wake_up_interruptible(&pipe->wait);
  816. ret = -ERESTARTSYS;
  817. goto err;
  818. err:
  819. __pipe_unlock(pipe);
  820. put_pipe_info(inode, pipe);
  821. return ret;
  822. }
  823. const struct file_operations pipefifo_fops = {
  824. .open = fifo_open,
  825. .llseek = no_llseek,
  826. .read = new_sync_read,
  827. .read_iter = pipe_read,
  828. .write = new_sync_write,
  829. .write_iter = pipe_write,
  830. .poll = pipe_poll,
  831. .unlocked_ioctl = pipe_ioctl,
  832. .release = pipe_release,
  833. .fasync = pipe_fasync,
  834. };
  835. /*
  836. * Allocate a new array of pipe buffers and copy the info over. Returns the
  837. * pipe size if successful, or return -ERROR on error.
  838. */
  839. static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
  840. {
  841. struct pipe_buffer *bufs;
  842. /*
  843. * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
  844. * expect a lot of shrink+grow operations, just free and allocate
  845. * again like we would do for growing. If the pipe currently
  846. * contains more buffers than arg, then return busy.
  847. */
  848. if (nr_pages < pipe->nrbufs)
  849. return -EBUSY;
  850. bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
  851. if (unlikely(!bufs))
  852. return -ENOMEM;
  853. /*
  854. * The pipe array wraps around, so just start the new one at zero
  855. * and adjust the indexes.
  856. */
  857. if (pipe->nrbufs) {
  858. unsigned int tail;
  859. unsigned int head;
  860. tail = pipe->curbuf + pipe->nrbufs;
  861. if (tail < pipe->buffers)
  862. tail = 0;
  863. else
  864. tail &= (pipe->buffers - 1);
  865. head = pipe->nrbufs - tail;
  866. if (head)
  867. memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
  868. if (tail)
  869. memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
  870. }
  871. pipe->curbuf = 0;
  872. kfree(pipe->bufs);
  873. pipe->bufs = bufs;
  874. pipe->buffers = nr_pages;
  875. return nr_pages * PAGE_SIZE;
  876. }
  877. /*
  878. * Currently we rely on the pipe array holding a power-of-2 number
  879. * of pages.
  880. */
  881. static inline unsigned int round_pipe_size(unsigned int size)
  882. {
  883. unsigned long nr_pages;
  884. nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  885. return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
  886. }
  887. /*
  888. * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
  889. * will return an error.
  890. */
  891. int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
  892. size_t *lenp, loff_t *ppos)
  893. {
  894. int ret;
  895. ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
  896. if (ret < 0 || !write)
  897. return ret;
  898. pipe_max_size = round_pipe_size(pipe_max_size);
  899. return ret;
  900. }
  901. /*
  902. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  903. * location, so checking ->i_pipe is not enough to verify that this is a
  904. * pipe.
  905. */
  906. struct pipe_inode_info *get_pipe_info(struct file *file)
  907. {
  908. return file->f_op == &pipefifo_fops ? file->private_data : NULL;
  909. }
  910. long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  911. {
  912. struct pipe_inode_info *pipe;
  913. long ret;
  914. pipe = get_pipe_info(file);
  915. if (!pipe)
  916. return -EBADF;
  917. __pipe_lock(pipe);
  918. switch (cmd) {
  919. case F_SETPIPE_SZ: {
  920. unsigned int size, nr_pages;
  921. size = round_pipe_size(arg);
  922. nr_pages = size >> PAGE_SHIFT;
  923. ret = -EINVAL;
  924. if (!nr_pages)
  925. goto out;
  926. if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
  927. ret = -EPERM;
  928. goto out;
  929. }
  930. ret = pipe_set_size(pipe, nr_pages);
  931. break;
  932. }
  933. case F_GETPIPE_SZ:
  934. ret = pipe->buffers * PAGE_SIZE;
  935. break;
  936. default:
  937. ret = -EINVAL;
  938. break;
  939. }
  940. out:
  941. __pipe_unlock(pipe);
  942. return ret;
  943. }
  944. static const struct super_operations pipefs_ops = {
  945. .destroy_inode = free_inode_nonrcu,
  946. .statfs = simple_statfs,
  947. };
  948. /*
  949. * pipefs should _never_ be mounted by userland - too much of security hassle,
  950. * no real gain from having the whole whorehouse mounted. So we don't need
  951. * any operations on the root directory. However, we need a non-trivial
  952. * d_name - pipe: will go nicely and kill the special-casing in procfs.
  953. */
  954. static struct dentry *pipefs_mount(struct file_system_type *fs_type,
  955. int flags, const char *dev_name, void *data)
  956. {
  957. return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
  958. &pipefs_dentry_operations, PIPEFS_MAGIC);
  959. }
  960. static struct file_system_type pipe_fs_type = {
  961. .name = "pipefs",
  962. .mount = pipefs_mount,
  963. .kill_sb = kill_anon_super,
  964. };
  965. static int __init init_pipe_fs(void)
  966. {
  967. int err = register_filesystem(&pipe_fs_type);
  968. if (!err) {
  969. pipe_mnt = kern_mount(&pipe_fs_type);
  970. if (IS_ERR(pipe_mnt)) {
  971. err = PTR_ERR(pipe_mnt);
  972. unregister_filesystem(&pipe_fs_type);
  973. }
  974. }
  975. return err;
  976. }
  977. fs_initcall(init_pipe_fs);