pipe.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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. dentry->d_inode->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 = new_sync_read,
  826. .read_iter = pipe_read,
  827. .write = new_sync_write,
  828. .write_iter = pipe_write,
  829. .poll = pipe_poll,
  830. .unlocked_ioctl = pipe_ioctl,
  831. .release = pipe_release,
  832. .fasync = pipe_fasync,
  833. };
  834. /*
  835. * Allocate a new array of pipe buffers and copy the info over. Returns the
  836. * pipe size if successful, or return -ERROR on error.
  837. */
  838. static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
  839. {
  840. struct pipe_buffer *bufs;
  841. /*
  842. * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
  843. * expect a lot of shrink+grow operations, just free and allocate
  844. * again like we would do for growing. If the pipe currently
  845. * contains more buffers than arg, then return busy.
  846. */
  847. if (nr_pages < pipe->nrbufs)
  848. return -EBUSY;
  849. bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
  850. if (unlikely(!bufs))
  851. return -ENOMEM;
  852. /*
  853. * The pipe array wraps around, so just start the new one at zero
  854. * and adjust the indexes.
  855. */
  856. if (pipe->nrbufs) {
  857. unsigned int tail;
  858. unsigned int head;
  859. tail = pipe->curbuf + pipe->nrbufs;
  860. if (tail < pipe->buffers)
  861. tail = 0;
  862. else
  863. tail &= (pipe->buffers - 1);
  864. head = pipe->nrbufs - tail;
  865. if (head)
  866. memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
  867. if (tail)
  868. memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
  869. }
  870. pipe->curbuf = 0;
  871. kfree(pipe->bufs);
  872. pipe->bufs = bufs;
  873. pipe->buffers = nr_pages;
  874. return nr_pages * PAGE_SIZE;
  875. }
  876. /*
  877. * Currently we rely on the pipe array holding a power-of-2 number
  878. * of pages.
  879. */
  880. static inline unsigned int round_pipe_size(unsigned int size)
  881. {
  882. unsigned long nr_pages;
  883. nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  884. return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
  885. }
  886. /*
  887. * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
  888. * will return an error.
  889. */
  890. int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
  891. size_t *lenp, loff_t *ppos)
  892. {
  893. int ret;
  894. ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
  895. if (ret < 0 || !write)
  896. return ret;
  897. pipe_max_size = round_pipe_size(pipe_max_size);
  898. return ret;
  899. }
  900. /*
  901. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  902. * location, so checking ->i_pipe is not enough to verify that this is a
  903. * pipe.
  904. */
  905. struct pipe_inode_info *get_pipe_info(struct file *file)
  906. {
  907. return file->f_op == &pipefifo_fops ? file->private_data : NULL;
  908. }
  909. long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  910. {
  911. struct pipe_inode_info *pipe;
  912. long ret;
  913. pipe = get_pipe_info(file);
  914. if (!pipe)
  915. return -EBADF;
  916. __pipe_lock(pipe);
  917. switch (cmd) {
  918. case F_SETPIPE_SZ: {
  919. unsigned int size, nr_pages;
  920. size = round_pipe_size(arg);
  921. nr_pages = size >> PAGE_SHIFT;
  922. ret = -EINVAL;
  923. if (!nr_pages)
  924. goto out;
  925. if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
  926. ret = -EPERM;
  927. goto out;
  928. }
  929. ret = pipe_set_size(pipe, nr_pages);
  930. break;
  931. }
  932. case F_GETPIPE_SZ:
  933. ret = pipe->buffers * PAGE_SIZE;
  934. break;
  935. default:
  936. ret = -EINVAL;
  937. break;
  938. }
  939. out:
  940. __pipe_unlock(pipe);
  941. return ret;
  942. }
  943. static const struct super_operations pipefs_ops = {
  944. .destroy_inode = free_inode_nonrcu,
  945. .statfs = simple_statfs,
  946. };
  947. /*
  948. * pipefs should _never_ be mounted by userland - too much of security hassle,
  949. * no real gain from having the whole whorehouse mounted. So we don't need
  950. * any operations on the root directory. However, we need a non-trivial
  951. * d_name - pipe: will go nicely and kill the special-casing in procfs.
  952. */
  953. static struct dentry *pipefs_mount(struct file_system_type *fs_type,
  954. int flags, const char *dev_name, void *data)
  955. {
  956. return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
  957. &pipefs_dentry_operations, PIPEFS_MAGIC);
  958. }
  959. static struct file_system_type pipe_fs_type = {
  960. .name = "pipefs",
  961. .mount = pipefs_mount,
  962. .kill_sb = kill_anon_super,
  963. };
  964. static int __init init_pipe_fs(void)
  965. {
  966. int err = register_filesystem(&pipe_fs_type);
  967. if (!err) {
  968. pipe_mnt = kern_mount(&pipe_fs_type);
  969. if (IS_ERR(pipe_mnt)) {
  970. err = PTR_ERR(pipe_mnt);
  971. unregister_filesystem(&pipe_fs_type);
  972. }
  973. }
  974. return err;
  975. }
  976. fs_initcall(init_pipe_fs);