pipe.c 19 KB

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