pipe.c 19 KB

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