pipe.c 25 KB

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