pipe.c 19 KB

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