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. ret = ops->confirm(pipe, buf);
  329. if (ret)
  330. goto out;
  331. ret = copy_page_from_iter(buf->page, offset, chars, from);
  332. if (unlikely(ret < chars)) {
  333. ret = -EFAULT;
  334. goto out;
  335. }
  336. do_wakeup = 1;
  337. buf->len += ret;
  338. if (!iov_iter_count(from))
  339. goto out;
  340. }
  341. }
  342. for (;;) {
  343. int bufs;
  344. if (!pipe->readers) {
  345. send_sig(SIGPIPE, current, 0);
  346. if (!ret)
  347. ret = -EPIPE;
  348. break;
  349. }
  350. bufs = pipe->nrbufs;
  351. if (bufs < pipe->buffers) {
  352. int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
  353. struct pipe_buffer *buf = pipe->bufs + newbuf;
  354. struct page *page = pipe->tmp_page;
  355. int copied;
  356. if (!page) {
  357. page = alloc_page(GFP_HIGHUSER);
  358. if (unlikely(!page)) {
  359. ret = ret ? : -ENOMEM;
  360. break;
  361. }
  362. pipe->tmp_page = page;
  363. }
  364. /* Always wake up, even if the copy fails. Otherwise
  365. * we lock up (O_NONBLOCK-)readers that sleep due to
  366. * syscall merging.
  367. * FIXME! Is this really true?
  368. */
  369. do_wakeup = 1;
  370. copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
  371. if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) {
  372. if (!ret)
  373. ret = -EFAULT;
  374. break;
  375. }
  376. ret += copied;
  377. /* Insert it into the buffer array */
  378. buf->page = page;
  379. buf->ops = &anon_pipe_buf_ops;
  380. buf->offset = 0;
  381. buf->len = copied;
  382. buf->flags = 0;
  383. if (is_packetized(filp)) {
  384. buf->ops = &packet_pipe_buf_ops;
  385. buf->flags = PIPE_BUF_FLAG_PACKET;
  386. }
  387. pipe->nrbufs = ++bufs;
  388. pipe->tmp_page = NULL;
  389. if (!iov_iter_count(from))
  390. break;
  391. }
  392. if (bufs < pipe->buffers)
  393. continue;
  394. if (filp->f_flags & O_NONBLOCK) {
  395. if (!ret)
  396. ret = -EAGAIN;
  397. break;
  398. }
  399. if (signal_pending(current)) {
  400. if (!ret)
  401. ret = -ERESTARTSYS;
  402. break;
  403. }
  404. if (do_wakeup) {
  405. wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
  406. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  407. do_wakeup = 0;
  408. }
  409. pipe->waiting_writers++;
  410. pipe_wait(pipe);
  411. pipe->waiting_writers--;
  412. }
  413. out:
  414. __pipe_unlock(pipe);
  415. if (do_wakeup) {
  416. wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
  417. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  418. }
  419. if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
  420. int err = file_update_time(filp);
  421. if (err)
  422. ret = err;
  423. sb_end_write(file_inode(filp)->i_sb);
  424. }
  425. return ret;
  426. }
  427. static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  428. {
  429. struct pipe_inode_info *pipe = filp->private_data;
  430. int count, buf, nrbufs;
  431. switch (cmd) {
  432. case FIONREAD:
  433. __pipe_lock(pipe);
  434. count = 0;
  435. buf = pipe->curbuf;
  436. nrbufs = pipe->nrbufs;
  437. while (--nrbufs >= 0) {
  438. count += pipe->bufs[buf].len;
  439. buf = (buf+1) & (pipe->buffers - 1);
  440. }
  441. __pipe_unlock(pipe);
  442. return put_user(count, (int __user *)arg);
  443. default:
  444. return -ENOIOCTLCMD;
  445. }
  446. }
  447. /* No kernel lock held - fine */
  448. static unsigned int
  449. pipe_poll(struct file *filp, poll_table *wait)
  450. {
  451. unsigned int mask;
  452. struct pipe_inode_info *pipe = filp->private_data;
  453. int nrbufs;
  454. poll_wait(filp, &pipe->wait, wait);
  455. /* Reading only -- no need for acquiring the semaphore. */
  456. nrbufs = pipe->nrbufs;
  457. mask = 0;
  458. if (filp->f_mode & FMODE_READ) {
  459. mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
  460. if (!pipe->writers && filp->f_version != pipe->w_counter)
  461. mask |= POLLHUP;
  462. }
  463. if (filp->f_mode & FMODE_WRITE) {
  464. mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
  465. /*
  466. * Most Unices do not set POLLERR for FIFOs but on Linux they
  467. * behave exactly like pipes for poll().
  468. */
  469. if (!pipe->readers)
  470. mask |= POLLERR;
  471. }
  472. return mask;
  473. }
  474. static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
  475. {
  476. int kill = 0;
  477. spin_lock(&inode->i_lock);
  478. if (!--pipe->files) {
  479. inode->i_pipe = NULL;
  480. kill = 1;
  481. }
  482. spin_unlock(&inode->i_lock);
  483. if (kill)
  484. free_pipe_info(pipe);
  485. }
  486. static int
  487. pipe_release(struct inode *inode, struct file *file)
  488. {
  489. struct pipe_inode_info *pipe = file->private_data;
  490. __pipe_lock(pipe);
  491. if (file->f_mode & FMODE_READ)
  492. pipe->readers--;
  493. if (file->f_mode & FMODE_WRITE)
  494. pipe->writers--;
  495. if (pipe->readers || pipe->writers) {
  496. wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
  497. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  498. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  499. }
  500. __pipe_unlock(pipe);
  501. put_pipe_info(inode, pipe);
  502. return 0;
  503. }
  504. static int
  505. pipe_fasync(int fd, struct file *filp, int on)
  506. {
  507. struct pipe_inode_info *pipe = filp->private_data;
  508. int retval = 0;
  509. __pipe_lock(pipe);
  510. if (filp->f_mode & FMODE_READ)
  511. retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
  512. if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
  513. retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
  514. if (retval < 0 && (filp->f_mode & FMODE_READ))
  515. /* this can happen only if on == T */
  516. fasync_helper(-1, filp, 0, &pipe->fasync_readers);
  517. }
  518. __pipe_unlock(pipe);
  519. return retval;
  520. }
  521. struct pipe_inode_info *alloc_pipe_info(void)
  522. {
  523. struct pipe_inode_info *pipe;
  524. pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
  525. if (pipe) {
  526. pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * PIPE_DEF_BUFFERS, GFP_KERNEL);
  527. if (pipe->bufs) {
  528. init_waitqueue_head(&pipe->wait);
  529. pipe->r_counter = pipe->w_counter = 1;
  530. pipe->buffers = PIPE_DEF_BUFFERS;
  531. mutex_init(&pipe->mutex);
  532. return pipe;
  533. }
  534. kfree(pipe);
  535. }
  536. return NULL;
  537. }
  538. void free_pipe_info(struct pipe_inode_info *pipe)
  539. {
  540. int i;
  541. for (i = 0; i < pipe->buffers; i++) {
  542. struct pipe_buffer *buf = pipe->bufs + i;
  543. if (buf->ops)
  544. buf->ops->release(pipe, buf);
  545. }
  546. if (pipe->tmp_page)
  547. __free_page(pipe->tmp_page);
  548. kfree(pipe->bufs);
  549. kfree(pipe);
  550. }
  551. static struct vfsmount *pipe_mnt __read_mostly;
  552. /*
  553. * pipefs_dname() is called from d_path().
  554. */
  555. static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
  556. {
  557. return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  558. d_inode(dentry)->i_ino);
  559. }
  560. static const struct dentry_operations pipefs_dentry_operations = {
  561. .d_dname = pipefs_dname,
  562. };
  563. static struct inode * get_pipe_inode(void)
  564. {
  565. struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
  566. struct pipe_inode_info *pipe;
  567. if (!inode)
  568. goto fail_inode;
  569. inode->i_ino = get_next_ino();
  570. pipe = alloc_pipe_info();
  571. if (!pipe)
  572. goto fail_iput;
  573. inode->i_pipe = pipe;
  574. pipe->files = 2;
  575. pipe->readers = pipe->writers = 1;
  576. inode->i_fop = &pipefifo_fops;
  577. /*
  578. * Mark the inode dirty from the very beginning,
  579. * that way it will never be moved to the dirty
  580. * list because "mark_inode_dirty()" will think
  581. * that it already _is_ on the dirty list.
  582. */
  583. inode->i_state = I_DIRTY;
  584. inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
  585. inode->i_uid = current_fsuid();
  586. inode->i_gid = current_fsgid();
  587. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  588. return inode;
  589. fail_iput:
  590. iput(inode);
  591. fail_inode:
  592. return NULL;
  593. }
  594. int create_pipe_files(struct file **res, int flags)
  595. {
  596. int err;
  597. struct inode *inode = get_pipe_inode();
  598. struct file *f;
  599. struct path path;
  600. static struct qstr name = { .name = "" };
  601. if (!inode)
  602. return -ENFILE;
  603. err = -ENOMEM;
  604. path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
  605. if (!path.dentry)
  606. goto err_inode;
  607. path.mnt = mntget(pipe_mnt);
  608. d_instantiate(path.dentry, inode);
  609. f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops);
  610. if (IS_ERR(f)) {
  611. err = PTR_ERR(f);
  612. goto err_dentry;
  613. }
  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. err = PTR_ERR(res[0]);
  619. goto err_file;
  620. }
  621. path_get(&path);
  622. res[0]->private_data = inode->i_pipe;
  623. res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  624. res[1] = f;
  625. return 0;
  626. err_file:
  627. put_filp(f);
  628. err_dentry:
  629. free_pipe_info(inode->i_pipe);
  630. path_put(&path);
  631. return err;
  632. err_inode:
  633. free_pipe_info(inode->i_pipe);
  634. iput(inode);
  635. return err;
  636. }
  637. static int __do_pipe_flags(int *fd, struct file **files, int flags)
  638. {
  639. int error;
  640. int fdw, fdr;
  641. if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
  642. return -EINVAL;
  643. error = create_pipe_files(files, flags);
  644. if (error)
  645. return error;
  646. error = get_unused_fd_flags(flags);
  647. if (error < 0)
  648. goto err_read_pipe;
  649. fdr = error;
  650. error = get_unused_fd_flags(flags);
  651. if (error < 0)
  652. goto err_fdr;
  653. fdw = error;
  654. audit_fd_pair(fdr, fdw);
  655. fd[0] = fdr;
  656. fd[1] = fdw;
  657. return 0;
  658. err_fdr:
  659. put_unused_fd(fdr);
  660. err_read_pipe:
  661. fput(files[0]);
  662. fput(files[1]);
  663. return error;
  664. }
  665. int do_pipe_flags(int *fd, int flags)
  666. {
  667. struct file *files[2];
  668. int error = __do_pipe_flags(fd, files, flags);
  669. if (!error) {
  670. fd_install(fd[0], files[0]);
  671. fd_install(fd[1], files[1]);
  672. }
  673. return error;
  674. }
  675. /*
  676. * sys_pipe() is the normal C calling standard for creating
  677. * a pipe. It's not the way Unix traditionally does this, though.
  678. */
  679. SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
  680. {
  681. struct file *files[2];
  682. int fd[2];
  683. int error;
  684. error = __do_pipe_flags(fd, files, flags);
  685. if (!error) {
  686. if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
  687. fput(files[0]);
  688. fput(files[1]);
  689. put_unused_fd(fd[0]);
  690. put_unused_fd(fd[1]);
  691. error = -EFAULT;
  692. } else {
  693. fd_install(fd[0], files[0]);
  694. fd_install(fd[1], files[1]);
  695. }
  696. }
  697. return error;
  698. }
  699. SYSCALL_DEFINE1(pipe, int __user *, fildes)
  700. {
  701. return sys_pipe2(fildes, 0);
  702. }
  703. static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
  704. {
  705. int cur = *cnt;
  706. while (cur == *cnt) {
  707. pipe_wait(pipe);
  708. if (signal_pending(current))
  709. break;
  710. }
  711. return cur == *cnt ? -ERESTARTSYS : 0;
  712. }
  713. static void wake_up_partner(struct pipe_inode_info *pipe)
  714. {
  715. wake_up_interruptible(&pipe->wait);
  716. }
  717. static int fifo_open(struct inode *inode, struct file *filp)
  718. {
  719. struct pipe_inode_info *pipe;
  720. bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
  721. int ret;
  722. filp->f_version = 0;
  723. spin_lock(&inode->i_lock);
  724. if (inode->i_pipe) {
  725. pipe = inode->i_pipe;
  726. pipe->files++;
  727. spin_unlock(&inode->i_lock);
  728. } else {
  729. spin_unlock(&inode->i_lock);
  730. pipe = alloc_pipe_info();
  731. if (!pipe)
  732. return -ENOMEM;
  733. pipe->files = 1;
  734. spin_lock(&inode->i_lock);
  735. if (unlikely(inode->i_pipe)) {
  736. inode->i_pipe->files++;
  737. spin_unlock(&inode->i_lock);
  738. free_pipe_info(pipe);
  739. pipe = inode->i_pipe;
  740. } else {
  741. inode->i_pipe = pipe;
  742. spin_unlock(&inode->i_lock);
  743. }
  744. }
  745. filp->private_data = pipe;
  746. /* OK, we have a pipe and it's pinned down */
  747. __pipe_lock(pipe);
  748. /* We can only do regular read/write on fifos */
  749. filp->f_mode &= (FMODE_READ | FMODE_WRITE);
  750. switch (filp->f_mode) {
  751. case FMODE_READ:
  752. /*
  753. * O_RDONLY
  754. * POSIX.1 says that O_NONBLOCK means return with the FIFO
  755. * opened, even when there is no process writing the FIFO.
  756. */
  757. pipe->r_counter++;
  758. if (pipe->readers++ == 0)
  759. wake_up_partner(pipe);
  760. if (!is_pipe && !pipe->writers) {
  761. if ((filp->f_flags & O_NONBLOCK)) {
  762. /* suppress POLLHUP until we have
  763. * seen a writer */
  764. filp->f_version = pipe->w_counter;
  765. } else {
  766. if (wait_for_partner(pipe, &pipe->w_counter))
  767. goto err_rd;
  768. }
  769. }
  770. break;
  771. case FMODE_WRITE:
  772. /*
  773. * O_WRONLY
  774. * POSIX.1 says that O_NONBLOCK means return -1 with
  775. * errno=ENXIO when there is no process reading the FIFO.
  776. */
  777. ret = -ENXIO;
  778. if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
  779. goto err;
  780. pipe->w_counter++;
  781. if (!pipe->writers++)
  782. wake_up_partner(pipe);
  783. if (!is_pipe && !pipe->readers) {
  784. if (wait_for_partner(pipe, &pipe->r_counter))
  785. goto err_wr;
  786. }
  787. break;
  788. case FMODE_READ | FMODE_WRITE:
  789. /*
  790. * O_RDWR
  791. * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
  792. * This implementation will NEVER block on a O_RDWR open, since
  793. * the process can at least talk to itself.
  794. */
  795. pipe->readers++;
  796. pipe->writers++;
  797. pipe->r_counter++;
  798. pipe->w_counter++;
  799. if (pipe->readers == 1 || pipe->writers == 1)
  800. wake_up_partner(pipe);
  801. break;
  802. default:
  803. ret = -EINVAL;
  804. goto err;
  805. }
  806. /* Ok! */
  807. __pipe_unlock(pipe);
  808. return 0;
  809. err_rd:
  810. if (!--pipe->readers)
  811. wake_up_interruptible(&pipe->wait);
  812. ret = -ERESTARTSYS;
  813. goto err;
  814. err_wr:
  815. if (!--pipe->writers)
  816. wake_up_interruptible(&pipe->wait);
  817. ret = -ERESTARTSYS;
  818. goto err;
  819. err:
  820. __pipe_unlock(pipe);
  821. put_pipe_info(inode, pipe);
  822. return ret;
  823. }
  824. const struct file_operations pipefifo_fops = {
  825. .open = fifo_open,
  826. .llseek = no_llseek,
  827. .read_iter = pipe_read,
  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);