pipe.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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/mount.h>
  14. #include <linux/pipe_fs_i.h>
  15. #include <linux/uio.h>
  16. #include <linux/highmem.h>
  17. #include <linux/pagemap.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/ioctls.h>
  20. /*
  21. * We use a start+len construction, which provides full use of the
  22. * allocated memory.
  23. * -- Florian Coosmann (FGC)
  24. *
  25. * Reads with count = 0 should always return 0.
  26. * -- Julian Bradfield 1999-06-07.
  27. *
  28. * FIFOs and Pipes now generate SIGIO for both readers and writers.
  29. * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
  30. *
  31. * pipe_read & write cleanup
  32. * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
  33. */
  34. /* Drop the inode semaphore and wait for a pipe event, atomically */
  35. void pipe_wait(struct pipe_inode_info *pipe)
  36. {
  37. DEFINE_WAIT(wait);
  38. /*
  39. * Pipes are system-local resources, so sleeping on them
  40. * is considered a noninteractive wait:
  41. */
  42. prepare_to_wait(&pipe->wait, &wait,
  43. TASK_INTERRUPTIBLE | TASK_NONINTERACTIVE);
  44. if (pipe->inode)
  45. mutex_unlock(&pipe->inode->i_mutex);
  46. schedule();
  47. finish_wait(&pipe->wait, &wait);
  48. if (pipe->inode)
  49. mutex_lock(&pipe->inode->i_mutex);
  50. }
  51. static int
  52. pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len)
  53. {
  54. unsigned long copy;
  55. while (len > 0) {
  56. while (!iov->iov_len)
  57. iov++;
  58. copy = min_t(unsigned long, len, iov->iov_len);
  59. if (copy_from_user(to, iov->iov_base, copy))
  60. return -EFAULT;
  61. to += copy;
  62. len -= copy;
  63. iov->iov_base += copy;
  64. iov->iov_len -= copy;
  65. }
  66. return 0;
  67. }
  68. static int
  69. pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len)
  70. {
  71. unsigned long copy;
  72. while (len > 0) {
  73. while (!iov->iov_len)
  74. iov++;
  75. copy = min_t(unsigned long, len, iov->iov_len);
  76. if (copy_to_user(iov->iov_base, from, copy))
  77. return -EFAULT;
  78. from += copy;
  79. len -= copy;
  80. iov->iov_base += copy;
  81. iov->iov_len -= copy;
  82. }
  83. return 0;
  84. }
  85. static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
  86. struct pipe_buffer *buf)
  87. {
  88. struct page *page = buf->page;
  89. buf->flags &= ~PIPE_BUF_FLAG_STOLEN;
  90. /*
  91. * If nobody else uses this page, and we don't already have a
  92. * temporary page, let's keep track of it as a one-deep
  93. * allocation cache. (Otherwise just release our reference to it)
  94. */
  95. if (page_count(page) == 1 && !pipe->tmp_page)
  96. pipe->tmp_page = page;
  97. else
  98. page_cache_release(page);
  99. }
  100. static void * anon_pipe_buf_map(struct file *file, struct pipe_inode_info *pipe,
  101. struct pipe_buffer *buf)
  102. {
  103. return kmap(buf->page);
  104. }
  105. static void anon_pipe_buf_unmap(struct pipe_inode_info *pipe,
  106. struct pipe_buffer *buf)
  107. {
  108. kunmap(buf->page);
  109. }
  110. static int anon_pipe_buf_steal(struct pipe_inode_info *pipe,
  111. struct pipe_buffer *buf)
  112. {
  113. struct page *page = buf->page;
  114. if (page_count(page) == 1) {
  115. buf->flags |= PIPE_BUF_FLAG_STOLEN;
  116. lock_page(page);
  117. return 0;
  118. }
  119. return 1;
  120. }
  121. static void anon_pipe_buf_get(struct pipe_inode_info *info,
  122. struct pipe_buffer *buf)
  123. {
  124. page_cache_get(buf->page);
  125. }
  126. static struct pipe_buf_operations anon_pipe_buf_ops = {
  127. .can_merge = 1,
  128. .map = anon_pipe_buf_map,
  129. .unmap = anon_pipe_buf_unmap,
  130. .release = anon_pipe_buf_release,
  131. .steal = anon_pipe_buf_steal,
  132. .get = anon_pipe_buf_get,
  133. };
  134. static ssize_t
  135. pipe_readv(struct file *filp, const struct iovec *_iov,
  136. unsigned long nr_segs, loff_t *ppos)
  137. {
  138. struct inode *inode = filp->f_dentry->d_inode;
  139. struct pipe_inode_info *pipe;
  140. int do_wakeup;
  141. ssize_t ret;
  142. struct iovec *iov = (struct iovec *)_iov;
  143. size_t total_len;
  144. total_len = iov_length(iov, nr_segs);
  145. /* Null read succeeds. */
  146. if (unlikely(total_len == 0))
  147. return 0;
  148. do_wakeup = 0;
  149. ret = 0;
  150. mutex_lock(&inode->i_mutex);
  151. pipe = inode->i_pipe;
  152. for (;;) {
  153. int bufs = pipe->nrbufs;
  154. if (bufs) {
  155. int curbuf = pipe->curbuf;
  156. struct pipe_buffer *buf = pipe->bufs + curbuf;
  157. struct pipe_buf_operations *ops = buf->ops;
  158. void *addr;
  159. size_t chars = buf->len;
  160. int error;
  161. if (chars > total_len)
  162. chars = total_len;
  163. addr = ops->map(filp, pipe, buf);
  164. if (IS_ERR(addr)) {
  165. if (!ret)
  166. ret = PTR_ERR(addr);
  167. break;
  168. }
  169. error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars);
  170. ops->unmap(pipe, buf);
  171. if (unlikely(error)) {
  172. if (!ret)
  173. ret = -EFAULT;
  174. break;
  175. }
  176. ret += chars;
  177. buf->offset += chars;
  178. buf->len -= chars;
  179. if (!buf->len) {
  180. buf->ops = NULL;
  181. ops->release(pipe, buf);
  182. curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
  183. pipe->curbuf = curbuf;
  184. pipe->nrbufs = --bufs;
  185. do_wakeup = 1;
  186. }
  187. total_len -= chars;
  188. if (!total_len)
  189. break; /* common path: read succeeded */
  190. }
  191. if (bufs) /* More to do? */
  192. continue;
  193. if (!pipe->writers)
  194. break;
  195. if (!pipe->waiting_writers) {
  196. /* syscall merging: Usually we must not sleep
  197. * if O_NONBLOCK is set, or if we got some data.
  198. * But if a writer sleeps in kernel space, then
  199. * we can wait for that data without violating POSIX.
  200. */
  201. if (ret)
  202. break;
  203. if (filp->f_flags & O_NONBLOCK) {
  204. ret = -EAGAIN;
  205. break;
  206. }
  207. }
  208. if (signal_pending(current)) {
  209. if (!ret)
  210. ret = -ERESTARTSYS;
  211. break;
  212. }
  213. if (do_wakeup) {
  214. wake_up_interruptible_sync(&pipe->wait);
  215. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  216. }
  217. pipe_wait(pipe);
  218. }
  219. mutex_unlock(&inode->i_mutex);
  220. /* Signal writers asynchronously that there is more room. */
  221. if (do_wakeup) {
  222. wake_up_interruptible(&pipe->wait);
  223. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  224. }
  225. if (ret > 0)
  226. file_accessed(filp);
  227. return ret;
  228. }
  229. static ssize_t
  230. pipe_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
  231. {
  232. struct iovec iov = { .iov_base = buf, .iov_len = count };
  233. return pipe_readv(filp, &iov, 1, ppos);
  234. }
  235. static ssize_t
  236. pipe_writev(struct file *filp, const struct iovec *_iov,
  237. unsigned long nr_segs, loff_t *ppos)
  238. {
  239. struct inode *inode = filp->f_dentry->d_inode;
  240. struct pipe_inode_info *pipe;
  241. ssize_t ret;
  242. int do_wakeup;
  243. struct iovec *iov = (struct iovec *)_iov;
  244. size_t total_len;
  245. ssize_t chars;
  246. total_len = iov_length(iov, nr_segs);
  247. /* Null write succeeds. */
  248. if (unlikely(total_len == 0))
  249. return 0;
  250. do_wakeup = 0;
  251. ret = 0;
  252. mutex_lock(&inode->i_mutex);
  253. pipe = inode->i_pipe;
  254. if (!pipe->readers) {
  255. send_sig(SIGPIPE, current, 0);
  256. ret = -EPIPE;
  257. goto out;
  258. }
  259. /* We try to merge small writes */
  260. chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
  261. if (pipe->nrbufs && chars != 0) {
  262. int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
  263. (PIPE_BUFFERS-1);
  264. struct pipe_buffer *buf = pipe->bufs + lastbuf;
  265. struct pipe_buf_operations *ops = buf->ops;
  266. int offset = buf->offset + buf->len;
  267. if (ops->can_merge && offset + chars <= PAGE_SIZE) {
  268. void *addr;
  269. int error;
  270. addr = ops->map(filp, pipe, buf);
  271. if (IS_ERR(addr)) {
  272. error = PTR_ERR(addr);
  273. goto out;
  274. }
  275. error = pipe_iov_copy_from_user(offset + addr, iov,
  276. chars);
  277. ops->unmap(pipe, buf);
  278. ret = error;
  279. do_wakeup = 1;
  280. if (error)
  281. goto out;
  282. buf->len += chars;
  283. total_len -= chars;
  284. ret = chars;
  285. if (!total_len)
  286. goto out;
  287. }
  288. }
  289. for (;;) {
  290. int bufs;
  291. if (!pipe->readers) {
  292. send_sig(SIGPIPE, current, 0);
  293. if (!ret)
  294. ret = -EPIPE;
  295. break;
  296. }
  297. bufs = pipe->nrbufs;
  298. if (bufs < PIPE_BUFFERS) {
  299. int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
  300. struct pipe_buffer *buf = pipe->bufs + newbuf;
  301. struct page *page = pipe->tmp_page;
  302. int error;
  303. if (!page) {
  304. page = alloc_page(GFP_HIGHUSER);
  305. if (unlikely(!page)) {
  306. ret = ret ? : -ENOMEM;
  307. break;
  308. }
  309. pipe->tmp_page = page;
  310. }
  311. /* Always wake up, even if the copy fails. Otherwise
  312. * we lock up (O_NONBLOCK-)readers that sleep due to
  313. * syscall merging.
  314. * FIXME! Is this really true?
  315. */
  316. do_wakeup = 1;
  317. chars = PAGE_SIZE;
  318. if (chars > total_len)
  319. chars = total_len;
  320. error = pipe_iov_copy_from_user(kmap(page), iov, chars);
  321. kunmap(page);
  322. if (unlikely(error)) {
  323. if (!ret)
  324. ret = -EFAULT;
  325. break;
  326. }
  327. ret += chars;
  328. /* Insert it into the buffer array */
  329. buf->page = page;
  330. buf->ops = &anon_pipe_buf_ops;
  331. buf->offset = 0;
  332. buf->len = chars;
  333. pipe->nrbufs = ++bufs;
  334. pipe->tmp_page = NULL;
  335. total_len -= chars;
  336. if (!total_len)
  337. break;
  338. }
  339. if (bufs < PIPE_BUFFERS)
  340. continue;
  341. if (filp->f_flags & O_NONBLOCK) {
  342. if (!ret)
  343. ret = -EAGAIN;
  344. break;
  345. }
  346. if (signal_pending(current)) {
  347. if (!ret)
  348. ret = -ERESTARTSYS;
  349. break;
  350. }
  351. if (do_wakeup) {
  352. wake_up_interruptible_sync(&pipe->wait);
  353. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  354. do_wakeup = 0;
  355. }
  356. pipe->waiting_writers++;
  357. pipe_wait(pipe);
  358. pipe->waiting_writers--;
  359. }
  360. out:
  361. mutex_unlock(&inode->i_mutex);
  362. if (do_wakeup) {
  363. wake_up_interruptible(&pipe->wait);
  364. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  365. }
  366. if (ret > 0)
  367. file_update_time(filp);
  368. return ret;
  369. }
  370. static ssize_t
  371. pipe_write(struct file *filp, const char __user *buf,
  372. size_t count, loff_t *ppos)
  373. {
  374. struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
  375. return pipe_writev(filp, &iov, 1, ppos);
  376. }
  377. static ssize_t
  378. bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
  379. {
  380. return -EBADF;
  381. }
  382. static ssize_t
  383. bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
  384. loff_t *ppos)
  385. {
  386. return -EBADF;
  387. }
  388. static int
  389. pipe_ioctl(struct inode *pino, struct file *filp,
  390. unsigned int cmd, unsigned long arg)
  391. {
  392. struct inode *inode = filp->f_dentry->d_inode;
  393. struct pipe_inode_info *pipe;
  394. int count, buf, nrbufs;
  395. switch (cmd) {
  396. case FIONREAD:
  397. mutex_lock(&inode->i_mutex);
  398. pipe = inode->i_pipe;
  399. count = 0;
  400. buf = pipe->curbuf;
  401. nrbufs = pipe->nrbufs;
  402. while (--nrbufs >= 0) {
  403. count += pipe->bufs[buf].len;
  404. buf = (buf+1) & (PIPE_BUFFERS-1);
  405. }
  406. mutex_unlock(&inode->i_mutex);
  407. return put_user(count, (int __user *)arg);
  408. default:
  409. return -EINVAL;
  410. }
  411. }
  412. /* No kernel lock held - fine */
  413. static unsigned int
  414. pipe_poll(struct file *filp, poll_table *wait)
  415. {
  416. unsigned int mask;
  417. struct inode *inode = filp->f_dentry->d_inode;
  418. struct pipe_inode_info *pipe = inode->i_pipe;
  419. int nrbufs;
  420. poll_wait(filp, &pipe->wait, wait);
  421. /* Reading only -- no need for acquiring the semaphore. */
  422. nrbufs = pipe->nrbufs;
  423. mask = 0;
  424. if (filp->f_mode & FMODE_READ) {
  425. mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
  426. if (!pipe->writers && filp->f_version != pipe->w_counter)
  427. mask |= POLLHUP;
  428. }
  429. if (filp->f_mode & FMODE_WRITE) {
  430. mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
  431. /*
  432. * Most Unices do not set POLLERR for FIFOs but on Linux they
  433. * behave exactly like pipes for poll().
  434. */
  435. if (!pipe->readers)
  436. mask |= POLLERR;
  437. }
  438. return mask;
  439. }
  440. static int
  441. pipe_release(struct inode *inode, int decr, int decw)
  442. {
  443. struct pipe_inode_info *pipe;
  444. mutex_lock(&inode->i_mutex);
  445. pipe = inode->i_pipe;
  446. pipe->readers -= decr;
  447. pipe->writers -= decw;
  448. if (!pipe->readers && !pipe->writers) {
  449. free_pipe_info(inode);
  450. } else {
  451. wake_up_interruptible(&pipe->wait);
  452. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  453. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  454. }
  455. mutex_unlock(&inode->i_mutex);
  456. return 0;
  457. }
  458. static int
  459. pipe_read_fasync(int fd, struct file *filp, int on)
  460. {
  461. struct inode *inode = filp->f_dentry->d_inode;
  462. int retval;
  463. mutex_lock(&inode->i_mutex);
  464. retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
  465. mutex_unlock(&inode->i_mutex);
  466. if (retval < 0)
  467. return retval;
  468. return 0;
  469. }
  470. static int
  471. pipe_write_fasync(int fd, struct file *filp, int on)
  472. {
  473. struct inode *inode = filp->f_dentry->d_inode;
  474. int retval;
  475. mutex_lock(&inode->i_mutex);
  476. retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
  477. mutex_unlock(&inode->i_mutex);
  478. if (retval < 0)
  479. return retval;
  480. return 0;
  481. }
  482. static int
  483. pipe_rdwr_fasync(int fd, struct file *filp, int on)
  484. {
  485. struct inode *inode = filp->f_dentry->d_inode;
  486. struct pipe_inode_info *pipe = inode->i_pipe;
  487. int retval;
  488. mutex_lock(&inode->i_mutex);
  489. retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
  490. if (retval >= 0)
  491. retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
  492. mutex_unlock(&inode->i_mutex);
  493. if (retval < 0)
  494. return retval;
  495. return 0;
  496. }
  497. static int
  498. pipe_read_release(struct inode *inode, struct file *filp)
  499. {
  500. pipe_read_fasync(-1, filp, 0);
  501. return pipe_release(inode, 1, 0);
  502. }
  503. static int
  504. pipe_write_release(struct inode *inode, struct file *filp)
  505. {
  506. pipe_write_fasync(-1, filp, 0);
  507. return pipe_release(inode, 0, 1);
  508. }
  509. static int
  510. pipe_rdwr_release(struct inode *inode, struct file *filp)
  511. {
  512. int decr, decw;
  513. pipe_rdwr_fasync(-1, filp, 0);
  514. decr = (filp->f_mode & FMODE_READ) != 0;
  515. decw = (filp->f_mode & FMODE_WRITE) != 0;
  516. return pipe_release(inode, decr, decw);
  517. }
  518. static int
  519. pipe_read_open(struct inode *inode, struct file *filp)
  520. {
  521. /* We could have perhaps used atomic_t, but this and friends
  522. below are the only places. So it doesn't seem worthwhile. */
  523. mutex_lock(&inode->i_mutex);
  524. inode->i_pipe->readers++;
  525. mutex_unlock(&inode->i_mutex);
  526. return 0;
  527. }
  528. static int
  529. pipe_write_open(struct inode *inode, struct file *filp)
  530. {
  531. mutex_lock(&inode->i_mutex);
  532. inode->i_pipe->writers++;
  533. mutex_unlock(&inode->i_mutex);
  534. return 0;
  535. }
  536. static int
  537. pipe_rdwr_open(struct inode *inode, struct file *filp)
  538. {
  539. mutex_lock(&inode->i_mutex);
  540. if (filp->f_mode & FMODE_READ)
  541. inode->i_pipe->readers++;
  542. if (filp->f_mode & FMODE_WRITE)
  543. inode->i_pipe->writers++;
  544. mutex_unlock(&inode->i_mutex);
  545. return 0;
  546. }
  547. /*
  548. * The file_operations structs are not static because they
  549. * are also used in linux/fs/fifo.c to do operations on FIFOs.
  550. */
  551. const struct file_operations read_fifo_fops = {
  552. .llseek = no_llseek,
  553. .read = pipe_read,
  554. .readv = pipe_readv,
  555. .write = bad_pipe_w,
  556. .poll = pipe_poll,
  557. .ioctl = pipe_ioctl,
  558. .open = pipe_read_open,
  559. .release = pipe_read_release,
  560. .fasync = pipe_read_fasync,
  561. };
  562. const struct file_operations write_fifo_fops = {
  563. .llseek = no_llseek,
  564. .read = bad_pipe_r,
  565. .write = pipe_write,
  566. .writev = pipe_writev,
  567. .poll = pipe_poll,
  568. .ioctl = pipe_ioctl,
  569. .open = pipe_write_open,
  570. .release = pipe_write_release,
  571. .fasync = pipe_write_fasync,
  572. };
  573. const struct file_operations rdwr_fifo_fops = {
  574. .llseek = no_llseek,
  575. .read = pipe_read,
  576. .readv = pipe_readv,
  577. .write = pipe_write,
  578. .writev = pipe_writev,
  579. .poll = pipe_poll,
  580. .ioctl = pipe_ioctl,
  581. .open = pipe_rdwr_open,
  582. .release = pipe_rdwr_release,
  583. .fasync = pipe_rdwr_fasync,
  584. };
  585. static struct file_operations read_pipe_fops = {
  586. .llseek = no_llseek,
  587. .read = pipe_read,
  588. .readv = pipe_readv,
  589. .write = bad_pipe_w,
  590. .poll = pipe_poll,
  591. .ioctl = pipe_ioctl,
  592. .open = pipe_read_open,
  593. .release = pipe_read_release,
  594. .fasync = pipe_read_fasync,
  595. };
  596. static struct file_operations write_pipe_fops = {
  597. .llseek = no_llseek,
  598. .read = bad_pipe_r,
  599. .write = pipe_write,
  600. .writev = pipe_writev,
  601. .poll = pipe_poll,
  602. .ioctl = pipe_ioctl,
  603. .open = pipe_write_open,
  604. .release = pipe_write_release,
  605. .fasync = pipe_write_fasync,
  606. };
  607. static struct file_operations rdwr_pipe_fops = {
  608. .llseek = no_llseek,
  609. .read = pipe_read,
  610. .readv = pipe_readv,
  611. .write = pipe_write,
  612. .writev = pipe_writev,
  613. .poll = pipe_poll,
  614. .ioctl = pipe_ioctl,
  615. .open = pipe_rdwr_open,
  616. .release = pipe_rdwr_release,
  617. .fasync = pipe_rdwr_fasync,
  618. };
  619. struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
  620. {
  621. struct pipe_inode_info *pipe;
  622. pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
  623. if (pipe) {
  624. init_waitqueue_head(&pipe->wait);
  625. pipe->r_counter = pipe->w_counter = 1;
  626. pipe->inode = inode;
  627. }
  628. return pipe;
  629. }
  630. void __free_pipe_info(struct pipe_inode_info *pipe)
  631. {
  632. int i;
  633. for (i = 0; i < PIPE_BUFFERS; i++) {
  634. struct pipe_buffer *buf = pipe->bufs + i;
  635. if (buf->ops)
  636. buf->ops->release(pipe, buf);
  637. }
  638. if (pipe->tmp_page)
  639. __free_page(pipe->tmp_page);
  640. kfree(pipe);
  641. }
  642. void free_pipe_info(struct inode *inode)
  643. {
  644. __free_pipe_info(inode->i_pipe);
  645. inode->i_pipe = NULL;
  646. }
  647. static struct vfsmount *pipe_mnt __read_mostly;
  648. static int pipefs_delete_dentry(struct dentry *dentry)
  649. {
  650. return 1;
  651. }
  652. static struct dentry_operations pipefs_dentry_operations = {
  653. .d_delete = pipefs_delete_dentry,
  654. };
  655. static struct inode * get_pipe_inode(void)
  656. {
  657. struct inode *inode = new_inode(pipe_mnt->mnt_sb);
  658. struct pipe_inode_info *pipe;
  659. if (!inode)
  660. goto fail_inode;
  661. pipe = alloc_pipe_info(inode);
  662. if (!pipe)
  663. goto fail_iput;
  664. inode->i_pipe = pipe;
  665. pipe->readers = pipe->writers = 1;
  666. inode->i_fop = &rdwr_pipe_fops;
  667. /*
  668. * Mark the inode dirty from the very beginning,
  669. * that way it will never be moved to the dirty
  670. * list because "mark_inode_dirty()" will think
  671. * that it already _is_ on the dirty list.
  672. */
  673. inode->i_state = I_DIRTY;
  674. inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
  675. inode->i_uid = current->fsuid;
  676. inode->i_gid = current->fsgid;
  677. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  678. inode->i_blksize = PAGE_SIZE;
  679. return inode;
  680. fail_iput:
  681. iput(inode);
  682. fail_inode:
  683. return NULL;
  684. }
  685. int do_pipe(int *fd)
  686. {
  687. struct qstr this;
  688. char name[32];
  689. struct dentry *dentry;
  690. struct inode * inode;
  691. struct file *f1, *f2;
  692. int error;
  693. int i, j;
  694. error = -ENFILE;
  695. f1 = get_empty_filp();
  696. if (!f1)
  697. goto no_files;
  698. f2 = get_empty_filp();
  699. if (!f2)
  700. goto close_f1;
  701. inode = get_pipe_inode();
  702. if (!inode)
  703. goto close_f12;
  704. error = get_unused_fd();
  705. if (error < 0)
  706. goto close_f12_inode;
  707. i = error;
  708. error = get_unused_fd();
  709. if (error < 0)
  710. goto close_f12_inode_i;
  711. j = error;
  712. error = -ENOMEM;
  713. sprintf(name, "[%lu]", inode->i_ino);
  714. this.name = name;
  715. this.len = strlen(name);
  716. this.hash = inode->i_ino; /* will go */
  717. dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
  718. if (!dentry)
  719. goto close_f12_inode_i_j;
  720. dentry->d_op = &pipefs_dentry_operations;
  721. d_add(dentry, inode);
  722. f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt));
  723. f1->f_dentry = f2->f_dentry = dget(dentry);
  724. f1->f_mapping = f2->f_mapping = inode->i_mapping;
  725. /* read file */
  726. f1->f_pos = f2->f_pos = 0;
  727. f1->f_flags = O_RDONLY;
  728. f1->f_op = &read_pipe_fops;
  729. f1->f_mode = FMODE_READ;
  730. f1->f_version = 0;
  731. /* write file */
  732. f2->f_flags = O_WRONLY;
  733. f2->f_op = &write_pipe_fops;
  734. f2->f_mode = FMODE_WRITE;
  735. f2->f_version = 0;
  736. fd_install(i, f1);
  737. fd_install(j, f2);
  738. fd[0] = i;
  739. fd[1] = j;
  740. return 0;
  741. close_f12_inode_i_j:
  742. put_unused_fd(j);
  743. close_f12_inode_i:
  744. put_unused_fd(i);
  745. close_f12_inode:
  746. free_pipe_info(inode);
  747. iput(inode);
  748. close_f12:
  749. put_filp(f2);
  750. close_f1:
  751. put_filp(f1);
  752. no_files:
  753. return error;
  754. }
  755. /*
  756. * pipefs should _never_ be mounted by userland - too much of security hassle,
  757. * no real gain from having the whole whorehouse mounted. So we don't need
  758. * any operations on the root directory. However, we need a non-trivial
  759. * d_name - pipe: will go nicely and kill the special-casing in procfs.
  760. */
  761. static struct super_block *
  762. pipefs_get_sb(struct file_system_type *fs_type, int flags,
  763. const char *dev_name, void *data)
  764. {
  765. return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC);
  766. }
  767. static struct file_system_type pipe_fs_type = {
  768. .name = "pipefs",
  769. .get_sb = pipefs_get_sb,
  770. .kill_sb = kill_anon_super,
  771. };
  772. static int __init init_pipe_fs(void)
  773. {
  774. int err = register_filesystem(&pipe_fs_type);
  775. if (!err) {
  776. pipe_mnt = kern_mount(&pipe_fs_type);
  777. if (IS_ERR(pipe_mnt)) {
  778. err = PTR_ERR(pipe_mnt);
  779. unregister_filesystem(&pipe_fs_type);
  780. }
  781. }
  782. return err;
  783. }
  784. static void __exit exit_pipe_fs(void)
  785. {
  786. unregister_filesystem(&pipe_fs_type);
  787. mntput(pipe_mnt);
  788. }
  789. fs_initcall(init_pipe_fs);
  790. module_exit(exit_pipe_fs);