pipe.c 26 KB

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