pipe.c 19 KB

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