pipe.c 19 KB

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