pipe.c 19 KB

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