pipe.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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 (pipe_bufs * PAGE_SIZE > pipe_max_size && !capable(CAP_SYS_RESOURCE))
  549. pipe_bufs = pipe_max_size >> PAGE_SHIFT;
  550. if (!too_many_pipe_buffers_hard(user)) {
  551. if (too_many_pipe_buffers_soft(user))
  552. pipe_bufs = 1;
  553. pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * pipe_bufs, GFP_KERNEL);
  554. }
  555. if (pipe->bufs) {
  556. init_waitqueue_head(&pipe->wait);
  557. pipe->r_counter = pipe->w_counter = 1;
  558. pipe->buffers = pipe_bufs;
  559. pipe->user = user;
  560. account_pipe_buffers(pipe, 0, pipe_bufs);
  561. mutex_init(&pipe->mutex);
  562. return pipe;
  563. }
  564. free_uid(user);
  565. kfree(pipe);
  566. }
  567. return NULL;
  568. }
  569. void free_pipe_info(struct pipe_inode_info *pipe)
  570. {
  571. int i;
  572. account_pipe_buffers(pipe, pipe->buffers, 0);
  573. free_uid(pipe->user);
  574. for (i = 0; i < pipe->buffers; i++) {
  575. struct pipe_buffer *buf = pipe->bufs + i;
  576. if (buf->ops)
  577. buf->ops->release(pipe, buf);
  578. }
  579. if (pipe->tmp_page)
  580. __free_page(pipe->tmp_page);
  581. kfree(pipe->bufs);
  582. kfree(pipe);
  583. }
  584. static struct vfsmount *pipe_mnt __read_mostly;
  585. /*
  586. * pipefs_dname() is called from d_path().
  587. */
  588. static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
  589. {
  590. return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  591. d_inode(dentry)->i_ino);
  592. }
  593. static const struct dentry_operations pipefs_dentry_operations = {
  594. .d_dname = pipefs_dname,
  595. };
  596. static struct inode * get_pipe_inode(void)
  597. {
  598. struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
  599. struct pipe_inode_info *pipe;
  600. if (!inode)
  601. goto fail_inode;
  602. inode->i_ino = get_next_ino();
  603. pipe = alloc_pipe_info();
  604. if (!pipe)
  605. goto fail_iput;
  606. inode->i_pipe = pipe;
  607. pipe->files = 2;
  608. pipe->readers = pipe->writers = 1;
  609. inode->i_fop = &pipefifo_fops;
  610. /*
  611. * Mark the inode dirty from the very beginning,
  612. * that way it will never be moved to the dirty
  613. * list because "mark_inode_dirty()" will think
  614. * that it already _is_ on the dirty list.
  615. */
  616. inode->i_state = I_DIRTY;
  617. inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
  618. inode->i_uid = current_fsuid();
  619. inode->i_gid = current_fsgid();
  620. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  621. return inode;
  622. fail_iput:
  623. iput(inode);
  624. fail_inode:
  625. return NULL;
  626. }
  627. int create_pipe_files(struct file **res, int flags)
  628. {
  629. int err;
  630. struct inode *inode = get_pipe_inode();
  631. struct file *f;
  632. struct path path;
  633. static struct qstr name = { .name = "" };
  634. if (!inode)
  635. return -ENFILE;
  636. err = -ENOMEM;
  637. path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
  638. if (!path.dentry)
  639. goto err_inode;
  640. path.mnt = mntget(pipe_mnt);
  641. d_instantiate(path.dentry, inode);
  642. f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops);
  643. if (IS_ERR(f)) {
  644. err = PTR_ERR(f);
  645. goto err_dentry;
  646. }
  647. f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
  648. f->private_data = inode->i_pipe;
  649. res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops);
  650. if (IS_ERR(res[0])) {
  651. err = PTR_ERR(res[0]);
  652. goto err_file;
  653. }
  654. path_get(&path);
  655. res[0]->private_data = inode->i_pipe;
  656. res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  657. res[1] = f;
  658. return 0;
  659. err_file:
  660. put_filp(f);
  661. err_dentry:
  662. free_pipe_info(inode->i_pipe);
  663. path_put(&path);
  664. return err;
  665. err_inode:
  666. free_pipe_info(inode->i_pipe);
  667. iput(inode);
  668. return err;
  669. }
  670. static int __do_pipe_flags(int *fd, struct file **files, int flags)
  671. {
  672. int error;
  673. int fdw, fdr;
  674. if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
  675. return -EINVAL;
  676. error = create_pipe_files(files, flags);
  677. if (error)
  678. return error;
  679. error = get_unused_fd_flags(flags);
  680. if (error < 0)
  681. goto err_read_pipe;
  682. fdr = error;
  683. error = get_unused_fd_flags(flags);
  684. if (error < 0)
  685. goto err_fdr;
  686. fdw = error;
  687. audit_fd_pair(fdr, fdw);
  688. fd[0] = fdr;
  689. fd[1] = fdw;
  690. return 0;
  691. err_fdr:
  692. put_unused_fd(fdr);
  693. err_read_pipe:
  694. fput(files[0]);
  695. fput(files[1]);
  696. return error;
  697. }
  698. int do_pipe_flags(int *fd, int flags)
  699. {
  700. struct file *files[2];
  701. int error = __do_pipe_flags(fd, files, flags);
  702. if (!error) {
  703. fd_install(fd[0], files[0]);
  704. fd_install(fd[1], files[1]);
  705. }
  706. return error;
  707. }
  708. /*
  709. * sys_pipe() is the normal C calling standard for creating
  710. * a pipe. It's not the way Unix traditionally does this, though.
  711. */
  712. SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
  713. {
  714. struct file *files[2];
  715. int fd[2];
  716. int error;
  717. error = __do_pipe_flags(fd, files, flags);
  718. if (!error) {
  719. if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
  720. fput(files[0]);
  721. fput(files[1]);
  722. put_unused_fd(fd[0]);
  723. put_unused_fd(fd[1]);
  724. error = -EFAULT;
  725. } else {
  726. fd_install(fd[0], files[0]);
  727. fd_install(fd[1], files[1]);
  728. }
  729. }
  730. return error;
  731. }
  732. SYSCALL_DEFINE1(pipe, int __user *, fildes)
  733. {
  734. return sys_pipe2(fildes, 0);
  735. }
  736. static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
  737. {
  738. int cur = *cnt;
  739. while (cur == *cnt) {
  740. pipe_wait(pipe);
  741. if (signal_pending(current))
  742. break;
  743. }
  744. return cur == *cnt ? -ERESTARTSYS : 0;
  745. }
  746. static void wake_up_partner(struct pipe_inode_info *pipe)
  747. {
  748. wake_up_interruptible(&pipe->wait);
  749. }
  750. static int fifo_open(struct inode *inode, struct file *filp)
  751. {
  752. struct pipe_inode_info *pipe;
  753. bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
  754. int ret;
  755. filp->f_version = 0;
  756. spin_lock(&inode->i_lock);
  757. if (inode->i_pipe) {
  758. pipe = inode->i_pipe;
  759. pipe->files++;
  760. spin_unlock(&inode->i_lock);
  761. } else {
  762. spin_unlock(&inode->i_lock);
  763. pipe = alloc_pipe_info();
  764. if (!pipe)
  765. return -ENOMEM;
  766. pipe->files = 1;
  767. spin_lock(&inode->i_lock);
  768. if (unlikely(inode->i_pipe)) {
  769. inode->i_pipe->files++;
  770. spin_unlock(&inode->i_lock);
  771. free_pipe_info(pipe);
  772. pipe = inode->i_pipe;
  773. } else {
  774. inode->i_pipe = pipe;
  775. spin_unlock(&inode->i_lock);
  776. }
  777. }
  778. filp->private_data = pipe;
  779. /* OK, we have a pipe and it's pinned down */
  780. __pipe_lock(pipe);
  781. /* We can only do regular read/write on fifos */
  782. filp->f_mode &= (FMODE_READ | FMODE_WRITE);
  783. switch (filp->f_mode) {
  784. case FMODE_READ:
  785. /*
  786. * O_RDONLY
  787. * POSIX.1 says that O_NONBLOCK means return with the FIFO
  788. * opened, even when there is no process writing the FIFO.
  789. */
  790. pipe->r_counter++;
  791. if (pipe->readers++ == 0)
  792. wake_up_partner(pipe);
  793. if (!is_pipe && !pipe->writers) {
  794. if ((filp->f_flags & O_NONBLOCK)) {
  795. /* suppress POLLHUP until we have
  796. * seen a writer */
  797. filp->f_version = pipe->w_counter;
  798. } else {
  799. if (wait_for_partner(pipe, &pipe->w_counter))
  800. goto err_rd;
  801. }
  802. }
  803. break;
  804. case FMODE_WRITE:
  805. /*
  806. * O_WRONLY
  807. * POSIX.1 says that O_NONBLOCK means return -1 with
  808. * errno=ENXIO when there is no process reading the FIFO.
  809. */
  810. ret = -ENXIO;
  811. if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
  812. goto err;
  813. pipe->w_counter++;
  814. if (!pipe->writers++)
  815. wake_up_partner(pipe);
  816. if (!is_pipe && !pipe->readers) {
  817. if (wait_for_partner(pipe, &pipe->r_counter))
  818. goto err_wr;
  819. }
  820. break;
  821. case FMODE_READ | FMODE_WRITE:
  822. /*
  823. * O_RDWR
  824. * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
  825. * This implementation will NEVER block on a O_RDWR open, since
  826. * the process can at least talk to itself.
  827. */
  828. pipe->readers++;
  829. pipe->writers++;
  830. pipe->r_counter++;
  831. pipe->w_counter++;
  832. if (pipe->readers == 1 || pipe->writers == 1)
  833. wake_up_partner(pipe);
  834. break;
  835. default:
  836. ret = -EINVAL;
  837. goto err;
  838. }
  839. /* Ok! */
  840. __pipe_unlock(pipe);
  841. return 0;
  842. err_rd:
  843. if (!--pipe->readers)
  844. wake_up_interruptible(&pipe->wait);
  845. ret = -ERESTARTSYS;
  846. goto err;
  847. err_wr:
  848. if (!--pipe->writers)
  849. wake_up_interruptible(&pipe->wait);
  850. ret = -ERESTARTSYS;
  851. goto err;
  852. err:
  853. __pipe_unlock(pipe);
  854. put_pipe_info(inode, pipe);
  855. return ret;
  856. }
  857. const struct file_operations pipefifo_fops = {
  858. .open = fifo_open,
  859. .llseek = no_llseek,
  860. .read_iter = pipe_read,
  861. .write_iter = pipe_write,
  862. .poll = pipe_poll,
  863. .unlocked_ioctl = pipe_ioctl,
  864. .release = pipe_release,
  865. .fasync = pipe_fasync,
  866. };
  867. /*
  868. * Allocate a new array of pipe buffers and copy the info over. Returns the
  869. * pipe size if successful, or return -ERROR on error.
  870. */
  871. static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
  872. {
  873. struct pipe_buffer *bufs;
  874. if (!nr_pages)
  875. return -EINVAL;
  876. /*
  877. * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
  878. * expect a lot of shrink+grow operations, just free and allocate
  879. * again like we would do for growing. If the pipe currently
  880. * contains more buffers than arg, then return busy.
  881. */
  882. if (nr_pages < pipe->nrbufs)
  883. return -EBUSY;
  884. bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
  885. if (unlikely(!bufs))
  886. return -ENOMEM;
  887. /*
  888. * The pipe array wraps around, so just start the new one at zero
  889. * and adjust the indexes.
  890. */
  891. if (pipe->nrbufs) {
  892. unsigned int tail;
  893. unsigned int head;
  894. tail = pipe->curbuf + pipe->nrbufs;
  895. if (tail < pipe->buffers)
  896. tail = 0;
  897. else
  898. tail &= (pipe->buffers - 1);
  899. head = pipe->nrbufs - tail;
  900. if (head)
  901. memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
  902. if (tail)
  903. memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
  904. }
  905. account_pipe_buffers(pipe, pipe->buffers, nr_pages);
  906. pipe->curbuf = 0;
  907. kfree(pipe->bufs);
  908. pipe->bufs = bufs;
  909. pipe->buffers = nr_pages;
  910. return nr_pages * PAGE_SIZE;
  911. }
  912. /*
  913. * Currently we rely on the pipe array holding a power-of-2 number
  914. * of pages. Returns 0 on error.
  915. */
  916. static inline unsigned int round_pipe_size(unsigned int size)
  917. {
  918. unsigned long nr_pages;
  919. if (size < pipe_min_size)
  920. size = pipe_min_size;
  921. nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  922. if (nr_pages == 0)
  923. return 0;
  924. return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
  925. }
  926. /*
  927. * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
  928. * will return an error.
  929. */
  930. int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
  931. size_t *lenp, loff_t *ppos)
  932. {
  933. unsigned int rounded_pipe_max_size;
  934. int ret;
  935. ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
  936. if (ret < 0 || !write)
  937. return ret;
  938. rounded_pipe_max_size = round_pipe_size(pipe_max_size);
  939. if (rounded_pipe_max_size == 0)
  940. return -EINVAL;
  941. pipe_max_size = rounded_pipe_max_size;
  942. return ret;
  943. }
  944. /*
  945. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  946. * location, so checking ->i_pipe is not enough to verify that this is a
  947. * pipe.
  948. */
  949. struct pipe_inode_info *get_pipe_info(struct file *file)
  950. {
  951. return file->f_op == &pipefifo_fops ? file->private_data : NULL;
  952. }
  953. long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  954. {
  955. struct pipe_inode_info *pipe;
  956. long ret;
  957. pipe = get_pipe_info(file);
  958. if (!pipe)
  959. return -EBADF;
  960. __pipe_lock(pipe);
  961. switch (cmd) {
  962. case F_SETPIPE_SZ: {
  963. unsigned int size, nr_pages;
  964. size = round_pipe_size(arg);
  965. nr_pages = size >> PAGE_SHIFT;
  966. ret = -EINVAL;
  967. if (!nr_pages)
  968. goto out;
  969. if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
  970. ret = -EPERM;
  971. goto out;
  972. } else if ((too_many_pipe_buffers_hard(pipe->user) ||
  973. too_many_pipe_buffers_soft(pipe->user)) &&
  974. !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
  975. ret = -EPERM;
  976. goto out;
  977. }
  978. ret = pipe_set_size(pipe, nr_pages);
  979. break;
  980. }
  981. case F_GETPIPE_SZ:
  982. ret = pipe->buffers * PAGE_SIZE;
  983. break;
  984. default:
  985. ret = -EINVAL;
  986. break;
  987. }
  988. out:
  989. __pipe_unlock(pipe);
  990. return ret;
  991. }
  992. static const struct super_operations pipefs_ops = {
  993. .destroy_inode = free_inode_nonrcu,
  994. .statfs = simple_statfs,
  995. };
  996. /*
  997. * pipefs should _never_ be mounted by userland - too much of security hassle,
  998. * no real gain from having the whole whorehouse mounted. So we don't need
  999. * any operations on the root directory. However, we need a non-trivial
  1000. * d_name - pipe: will go nicely and kill the special-casing in procfs.
  1001. */
  1002. static struct dentry *pipefs_mount(struct file_system_type *fs_type,
  1003. int flags, const char *dev_name, void *data)
  1004. {
  1005. return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
  1006. &pipefs_dentry_operations, PIPEFS_MAGIC);
  1007. }
  1008. static struct file_system_type pipe_fs_type = {
  1009. .name = "pipefs",
  1010. .mount = pipefs_mount,
  1011. .kill_sb = kill_anon_super,
  1012. };
  1013. static int __init init_pipe_fs(void)
  1014. {
  1015. int err = register_filesystem(&pipe_fs_type);
  1016. if (!err) {
  1017. pipe_mnt = kern_mount(&pipe_fs_type);
  1018. if (IS_ERR(pipe_mnt)) {
  1019. err = PTR_ERR(pipe_mnt);
  1020. unregister_filesystem(&pipe_fs_type);
  1021. }
  1022. }
  1023. return err;
  1024. }
  1025. fs_initcall(init_pipe_fs);