pipe.c 19 KB

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