pipe.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  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/log2.h>
  14. #include <linux/mount.h>
  15. #include <linux/magic.h>
  16. #include <linux/pipe_fs_i.h>
  17. #include <linux/uio.h>
  18. #include <linux/highmem.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/audit.h>
  21. #include <linux/syscalls.h>
  22. #include <linux/fcntl.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/ioctls.h>
  25. /*
  26. * The max size that a non-root user is allowed to grow the pipe. Can
  27. * be set by root in /proc/sys/fs/pipe-max-size
  28. */
  29. unsigned int pipe_max_size = 1048576;
  30. /*
  31. * Minimum pipe size, as required by POSIX
  32. */
  33. unsigned int pipe_min_size = PAGE_SIZE;
  34. /* Maximum allocatable pages per user. Hard limit is unset by default, soft
  35. * matches default values.
  36. */
  37. unsigned long pipe_user_pages_hard;
  38. unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR;
  39. /*
  40. * We use a start+len construction, which provides full use of the
  41. * allocated memory.
  42. * -- Florian Coosmann (FGC)
  43. *
  44. * Reads with count = 0 should always return 0.
  45. * -- Julian Bradfield 1999-06-07.
  46. *
  47. * FIFOs and Pipes now generate SIGIO for both readers and writers.
  48. * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
  49. *
  50. * pipe_read & write cleanup
  51. * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
  52. */
  53. static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
  54. {
  55. if (pipe->inode)
  56. mutex_lock_nested(&pipe->inode->i_mutex, subclass);
  57. }
  58. void pipe_lock(struct pipe_inode_info *pipe)
  59. {
  60. /*
  61. * pipe_lock() nests non-pipe inode locks (for writing to a file)
  62. */
  63. pipe_lock_nested(pipe, I_MUTEX_PARENT);
  64. }
  65. EXPORT_SYMBOL(pipe_lock);
  66. void pipe_unlock(struct pipe_inode_info *pipe)
  67. {
  68. if (pipe->inode)
  69. mutex_unlock(&pipe->inode->i_mutex);
  70. }
  71. EXPORT_SYMBOL(pipe_unlock);
  72. void pipe_double_lock(struct pipe_inode_info *pipe1,
  73. struct pipe_inode_info *pipe2)
  74. {
  75. BUG_ON(pipe1 == pipe2);
  76. if (pipe1 < pipe2) {
  77. pipe_lock_nested(pipe1, I_MUTEX_PARENT);
  78. pipe_lock_nested(pipe2, I_MUTEX_CHILD);
  79. } else {
  80. pipe_lock_nested(pipe2, I_MUTEX_PARENT);
  81. pipe_lock_nested(pipe1, I_MUTEX_CHILD);
  82. }
  83. }
  84. /* Drop the inode semaphore and wait for a pipe event, atomically */
  85. void pipe_wait(struct pipe_inode_info *pipe)
  86. {
  87. DEFINE_WAIT(wait);
  88. /*
  89. * Pipes are system-local resources, so sleeping on them
  90. * is considered a noninteractive wait:
  91. */
  92. prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
  93. pipe_unlock(pipe);
  94. schedule();
  95. finish_wait(&pipe->wait, &wait);
  96. pipe_lock(pipe);
  97. }
  98. static int
  99. pipe_iov_copy_from_user(void *addr, int *offset, struct iovec *iov,
  100. size_t *remaining, int atomic)
  101. {
  102. unsigned long copy;
  103. while (*remaining > 0) {
  104. while (!iov->iov_len)
  105. iov++;
  106. copy = min_t(unsigned long, *remaining, iov->iov_len);
  107. if (atomic) {
  108. if (__copy_from_user_inatomic(addr + *offset,
  109. iov->iov_base, copy))
  110. return -EFAULT;
  111. } else {
  112. if (copy_from_user(addr + *offset,
  113. iov->iov_base, copy))
  114. return -EFAULT;
  115. }
  116. *offset += copy;
  117. *remaining -= copy;
  118. iov->iov_base += copy;
  119. iov->iov_len -= copy;
  120. }
  121. return 0;
  122. }
  123. static int
  124. pipe_iov_copy_to_user(struct iovec *iov, void *addr, int *offset,
  125. size_t *remaining, int atomic)
  126. {
  127. unsigned long copy;
  128. while (*remaining > 0) {
  129. while (!iov->iov_len)
  130. iov++;
  131. copy = min_t(unsigned long, *remaining, iov->iov_len);
  132. if (atomic) {
  133. if (__copy_to_user_inatomic(iov->iov_base,
  134. addr + *offset, copy))
  135. return -EFAULT;
  136. } else {
  137. if (copy_to_user(iov->iov_base,
  138. addr + *offset, copy))
  139. return -EFAULT;
  140. }
  141. *offset += copy;
  142. *remaining -= copy;
  143. iov->iov_base += copy;
  144. iov->iov_len -= copy;
  145. }
  146. return 0;
  147. }
  148. /*
  149. * Attempt to pre-fault in the user memory, so we can use atomic copies.
  150. * Returns the number of bytes not faulted in.
  151. */
  152. static int iov_fault_in_pages_write(struct iovec *iov, unsigned long len)
  153. {
  154. while (!iov->iov_len)
  155. iov++;
  156. while (len > 0) {
  157. unsigned long this_len;
  158. this_len = min_t(unsigned long, len, iov->iov_len);
  159. if (fault_in_pages_writeable(iov->iov_base, this_len))
  160. break;
  161. len -= this_len;
  162. iov++;
  163. }
  164. return len;
  165. }
  166. /*
  167. * Pre-fault in the user memory, so we can use atomic copies.
  168. */
  169. static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len)
  170. {
  171. while (!iov->iov_len)
  172. iov++;
  173. while (len > 0) {
  174. unsigned long this_len;
  175. this_len = min_t(unsigned long, len, iov->iov_len);
  176. fault_in_pages_readable(iov->iov_base, this_len);
  177. len -= this_len;
  178. iov++;
  179. }
  180. }
  181. static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
  182. struct pipe_buffer *buf)
  183. {
  184. struct page *page = buf->page;
  185. /*
  186. * If nobody else uses this page, and we don't already have a
  187. * temporary page, let's keep track of it as a one-deep
  188. * allocation cache. (Otherwise just release our reference to it)
  189. */
  190. if (page_count(page) == 1 && !pipe->tmp_page)
  191. pipe->tmp_page = page;
  192. else
  193. page_cache_release(page);
  194. }
  195. /**
  196. * generic_pipe_buf_map - virtually map a pipe buffer
  197. * @pipe: the pipe that the buffer belongs to
  198. * @buf: the buffer that should be mapped
  199. * @atomic: whether to use an atomic map
  200. *
  201. * Description:
  202. * This function returns a kernel virtual address mapping for the
  203. * pipe_buffer passed in @buf. If @atomic is set, an atomic map is provided
  204. * and the caller has to be careful not to fault before calling
  205. * the unmap function.
  206. *
  207. * Note that this function occupies KM_USER0 if @atomic != 0.
  208. */
  209. void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
  210. struct pipe_buffer *buf, int atomic)
  211. {
  212. if (atomic) {
  213. buf->flags |= PIPE_BUF_FLAG_ATOMIC;
  214. return kmap_atomic(buf->page);
  215. }
  216. return kmap(buf->page);
  217. }
  218. EXPORT_SYMBOL(generic_pipe_buf_map);
  219. /**
  220. * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
  221. * @pipe: the pipe that the buffer belongs to
  222. * @buf: the buffer that should be unmapped
  223. * @map_data: the data that the mapping function returned
  224. *
  225. * Description:
  226. * This function undoes the mapping that ->map() provided.
  227. */
  228. void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
  229. struct pipe_buffer *buf, void *map_data)
  230. {
  231. if (buf->flags & PIPE_BUF_FLAG_ATOMIC) {
  232. buf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
  233. kunmap_atomic(map_data);
  234. } else
  235. kunmap(buf->page);
  236. }
  237. EXPORT_SYMBOL(generic_pipe_buf_unmap);
  238. /**
  239. * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
  240. * @pipe: the pipe that the buffer belongs to
  241. * @buf: the buffer to attempt to steal
  242. *
  243. * Description:
  244. * This function attempts to steal the &struct page attached to
  245. * @buf. If successful, this function returns 0 and returns with
  246. * the page locked. The caller may then reuse the page for whatever
  247. * he wishes; the typical use is insertion into a different file
  248. * page cache.
  249. */
  250. int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
  251. struct pipe_buffer *buf)
  252. {
  253. struct page *page = buf->page;
  254. /*
  255. * A reference of one is golden, that means that the owner of this
  256. * page is the only one holding a reference to it. lock the page
  257. * and return OK.
  258. */
  259. if (page_count(page) == 1) {
  260. lock_page(page);
  261. return 0;
  262. }
  263. return 1;
  264. }
  265. EXPORT_SYMBOL(generic_pipe_buf_steal);
  266. /**
  267. * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
  268. * @pipe: the pipe that the buffer belongs to
  269. * @buf: the buffer to get a reference to
  270. *
  271. * Description:
  272. * This function grabs an extra reference to @buf. It's used in
  273. * in the tee() system call, when we duplicate the buffers in one
  274. * pipe into another.
  275. */
  276. void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
  277. {
  278. page_cache_get(buf->page);
  279. }
  280. EXPORT_SYMBOL(generic_pipe_buf_get);
  281. /**
  282. * generic_pipe_buf_confirm - verify contents of the pipe buffer
  283. * @info: the pipe that the buffer belongs to
  284. * @buf: the buffer to confirm
  285. *
  286. * Description:
  287. * This function does nothing, because the generic pipe code uses
  288. * pages that are always good when inserted into the pipe.
  289. */
  290. int generic_pipe_buf_confirm(struct pipe_inode_info *info,
  291. struct pipe_buffer *buf)
  292. {
  293. return 0;
  294. }
  295. EXPORT_SYMBOL(generic_pipe_buf_confirm);
  296. /**
  297. * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
  298. * @pipe: the pipe that the buffer belongs to
  299. * @buf: the buffer to put a reference to
  300. *
  301. * Description:
  302. * This function releases a reference to @buf.
  303. */
  304. void generic_pipe_buf_release(struct pipe_inode_info *pipe,
  305. struct pipe_buffer *buf)
  306. {
  307. page_cache_release(buf->page);
  308. }
  309. EXPORT_SYMBOL(generic_pipe_buf_release);
  310. static const struct pipe_buf_operations anon_pipe_buf_ops = {
  311. .can_merge = 1,
  312. .map = generic_pipe_buf_map,
  313. .unmap = generic_pipe_buf_unmap,
  314. .confirm = generic_pipe_buf_confirm,
  315. .release = anon_pipe_buf_release,
  316. .steal = generic_pipe_buf_steal,
  317. .get = generic_pipe_buf_get,
  318. };
  319. static const struct pipe_buf_operations anon_pipe_buf_nomerge_ops = {
  320. .can_merge = 0,
  321. .confirm = generic_pipe_buf_confirm,
  322. .release = anon_pipe_buf_release,
  323. .steal = generic_pipe_buf_steal,
  324. .get = generic_pipe_buf_get,
  325. };
  326. static const struct pipe_buf_operations packet_pipe_buf_ops = {
  327. .can_merge = 0,
  328. .map = generic_pipe_buf_map,
  329. .unmap = generic_pipe_buf_unmap,
  330. .confirm = generic_pipe_buf_confirm,
  331. .release = anon_pipe_buf_release,
  332. .steal = generic_pipe_buf_steal,
  333. .get = generic_pipe_buf_get,
  334. };
  335. void pipe_buf_mark_unmergeable(struct pipe_buffer *buf)
  336. {
  337. if (buf->ops == &anon_pipe_buf_ops)
  338. buf->ops = &anon_pipe_buf_nomerge_ops;
  339. }
  340. static ssize_t
  341. pipe_read(struct kiocb *iocb, const struct iovec *_iov,
  342. unsigned long nr_segs, loff_t pos)
  343. {
  344. struct file *filp = iocb->ki_filp;
  345. struct inode *inode = filp->f_path.dentry->d_inode;
  346. struct pipe_inode_info *pipe;
  347. int do_wakeup;
  348. ssize_t ret;
  349. struct iovec *iov = (struct iovec *)_iov;
  350. size_t total_len;
  351. total_len = iov_length(iov, nr_segs);
  352. /* Null read succeeds. */
  353. if (unlikely(total_len == 0))
  354. return 0;
  355. do_wakeup = 0;
  356. ret = 0;
  357. mutex_lock(&inode->i_mutex);
  358. pipe = inode->i_pipe;
  359. for (;;) {
  360. int bufs = pipe->nrbufs;
  361. if (bufs) {
  362. int curbuf = pipe->curbuf;
  363. struct pipe_buffer *buf = pipe->bufs + curbuf;
  364. const struct pipe_buf_operations *ops = buf->ops;
  365. void *addr;
  366. size_t chars = buf->len, remaining;
  367. int error, atomic;
  368. int offset;
  369. if (chars > total_len)
  370. chars = total_len;
  371. error = ops->confirm(pipe, buf);
  372. if (error) {
  373. if (!ret)
  374. ret = error;
  375. break;
  376. }
  377. atomic = !iov_fault_in_pages_write(iov, chars);
  378. remaining = chars;
  379. offset = buf->offset;
  380. redo:
  381. addr = ops->map(pipe, buf, atomic);
  382. error = pipe_iov_copy_to_user(iov, addr, &offset,
  383. &remaining, atomic);
  384. ops->unmap(pipe, buf, addr);
  385. if (unlikely(error)) {
  386. /*
  387. * Just retry with the slow path if we failed.
  388. */
  389. if (atomic) {
  390. atomic = 0;
  391. goto redo;
  392. }
  393. if (!ret)
  394. ret = error;
  395. break;
  396. }
  397. ret += chars;
  398. buf->offset += chars;
  399. buf->len -= chars;
  400. /* Was it a packet buffer? Clean up and exit */
  401. if (buf->flags & PIPE_BUF_FLAG_PACKET) {
  402. total_len = chars;
  403. buf->len = 0;
  404. }
  405. if (!buf->len) {
  406. buf->ops = NULL;
  407. ops->release(pipe, buf);
  408. curbuf = (curbuf + 1) & (pipe->buffers - 1);
  409. pipe->curbuf = curbuf;
  410. pipe->nrbufs = --bufs;
  411. do_wakeup = 1;
  412. }
  413. total_len -= chars;
  414. if (!total_len)
  415. break; /* common path: read succeeded */
  416. }
  417. if (bufs) /* More to do? */
  418. continue;
  419. if (!pipe->writers)
  420. break;
  421. if (!pipe->waiting_writers) {
  422. /* syscall merging: Usually we must not sleep
  423. * if O_NONBLOCK is set, or if we got some data.
  424. * But if a writer sleeps in kernel space, then
  425. * we can wait for that data without violating POSIX.
  426. */
  427. if (ret)
  428. break;
  429. if (filp->f_flags & O_NONBLOCK) {
  430. ret = -EAGAIN;
  431. break;
  432. }
  433. }
  434. if (signal_pending(current)) {
  435. if (!ret)
  436. ret = -ERESTARTSYS;
  437. break;
  438. }
  439. if (do_wakeup) {
  440. wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
  441. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  442. }
  443. pipe_wait(pipe);
  444. }
  445. mutex_unlock(&inode->i_mutex);
  446. /* Signal writers asynchronously that there is more room. */
  447. if (do_wakeup) {
  448. wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
  449. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  450. }
  451. if (ret > 0)
  452. file_accessed(filp);
  453. return ret;
  454. }
  455. static inline int is_packetized(struct file *file)
  456. {
  457. return (file->f_flags & O_DIRECT) != 0;
  458. }
  459. static ssize_t
  460. pipe_write(struct kiocb *iocb, const struct iovec *_iov,
  461. unsigned long nr_segs, loff_t ppos)
  462. {
  463. struct file *filp = iocb->ki_filp;
  464. struct inode *inode = filp->f_path.dentry->d_inode;
  465. struct pipe_inode_info *pipe;
  466. ssize_t ret;
  467. int do_wakeup;
  468. struct iovec *iov = (struct iovec *)_iov;
  469. size_t total_len;
  470. ssize_t chars;
  471. total_len = iov_length(iov, nr_segs);
  472. /* Null write succeeds. */
  473. if (unlikely(total_len == 0))
  474. return 0;
  475. do_wakeup = 0;
  476. ret = 0;
  477. mutex_lock(&inode->i_mutex);
  478. pipe = inode->i_pipe;
  479. if (!pipe->readers) {
  480. send_sig(SIGPIPE, current, 0);
  481. ret = -EPIPE;
  482. goto out;
  483. }
  484. /* We try to merge small writes */
  485. chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
  486. if (pipe->nrbufs && chars != 0) {
  487. int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
  488. (pipe->buffers - 1);
  489. struct pipe_buffer *buf = pipe->bufs + lastbuf;
  490. const struct pipe_buf_operations *ops = buf->ops;
  491. int offset = buf->offset + buf->len;
  492. if (ops->can_merge && offset + chars <= PAGE_SIZE) {
  493. int error, atomic = 1;
  494. void *addr;
  495. size_t remaining = chars;
  496. error = ops->confirm(pipe, buf);
  497. if (error)
  498. goto out;
  499. iov_fault_in_pages_read(iov, chars);
  500. redo1:
  501. addr = ops->map(pipe, buf, atomic);
  502. error = pipe_iov_copy_from_user(addr, &offset, iov,
  503. &remaining, atomic);
  504. ops->unmap(pipe, buf, addr);
  505. ret = error;
  506. do_wakeup = 1;
  507. if (error) {
  508. if (atomic) {
  509. atomic = 0;
  510. goto redo1;
  511. }
  512. goto out;
  513. }
  514. buf->len += chars;
  515. total_len -= chars;
  516. ret = chars;
  517. if (!total_len)
  518. goto out;
  519. }
  520. }
  521. for (;;) {
  522. int bufs;
  523. if (!pipe->readers) {
  524. send_sig(SIGPIPE, current, 0);
  525. if (!ret)
  526. ret = -EPIPE;
  527. break;
  528. }
  529. bufs = pipe->nrbufs;
  530. if (bufs < pipe->buffers) {
  531. int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
  532. struct pipe_buffer *buf = pipe->bufs + newbuf;
  533. struct page *page = pipe->tmp_page;
  534. char *src;
  535. int error, atomic = 1;
  536. int offset = 0;
  537. size_t remaining;
  538. if (!page) {
  539. page = alloc_page(GFP_HIGHUSER);
  540. if (unlikely(!page)) {
  541. ret = ret ? : -ENOMEM;
  542. break;
  543. }
  544. pipe->tmp_page = page;
  545. }
  546. /* Always wake up, even if the copy fails. Otherwise
  547. * we lock up (O_NONBLOCK-)readers that sleep due to
  548. * syscall merging.
  549. * FIXME! Is this really true?
  550. */
  551. do_wakeup = 1;
  552. chars = PAGE_SIZE;
  553. if (chars > total_len)
  554. chars = total_len;
  555. iov_fault_in_pages_read(iov, chars);
  556. remaining = chars;
  557. redo2:
  558. if (atomic)
  559. src = kmap_atomic(page);
  560. else
  561. src = kmap(page);
  562. error = pipe_iov_copy_from_user(src, &offset, iov,
  563. &remaining, atomic);
  564. if (atomic)
  565. kunmap_atomic(src);
  566. else
  567. kunmap(page);
  568. if (unlikely(error)) {
  569. if (atomic) {
  570. atomic = 0;
  571. goto redo2;
  572. }
  573. if (!ret)
  574. ret = error;
  575. break;
  576. }
  577. ret += chars;
  578. /* Insert it into the buffer array */
  579. buf->page = page;
  580. buf->ops = &anon_pipe_buf_ops;
  581. buf->offset = 0;
  582. buf->len = chars;
  583. buf->flags = 0;
  584. if (is_packetized(filp)) {
  585. buf->ops = &packet_pipe_buf_ops;
  586. buf->flags = PIPE_BUF_FLAG_PACKET;
  587. }
  588. pipe->nrbufs = ++bufs;
  589. pipe->tmp_page = NULL;
  590. total_len -= chars;
  591. if (!total_len)
  592. break;
  593. }
  594. if (bufs < pipe->buffers)
  595. continue;
  596. if (filp->f_flags & O_NONBLOCK) {
  597. if (!ret)
  598. ret = -EAGAIN;
  599. break;
  600. }
  601. if (signal_pending(current)) {
  602. if (!ret)
  603. ret = -ERESTARTSYS;
  604. break;
  605. }
  606. if (do_wakeup) {
  607. wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
  608. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  609. do_wakeup = 0;
  610. }
  611. pipe->waiting_writers++;
  612. pipe_wait(pipe);
  613. pipe->waiting_writers--;
  614. }
  615. out:
  616. mutex_unlock(&inode->i_mutex);
  617. if (do_wakeup) {
  618. wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
  619. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  620. }
  621. if (ret > 0) {
  622. int err = file_update_time(filp);
  623. if (err)
  624. ret = err;
  625. }
  626. return ret;
  627. }
  628. static ssize_t
  629. bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
  630. {
  631. return -EBADF;
  632. }
  633. static ssize_t
  634. bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
  635. loff_t *ppos)
  636. {
  637. return -EBADF;
  638. }
  639. static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  640. {
  641. struct inode *inode = filp->f_path.dentry->d_inode;
  642. struct pipe_inode_info *pipe;
  643. int count, buf, nrbufs;
  644. switch (cmd) {
  645. case FIONREAD:
  646. mutex_lock(&inode->i_mutex);
  647. pipe = inode->i_pipe;
  648. count = 0;
  649. buf = pipe->curbuf;
  650. nrbufs = pipe->nrbufs;
  651. while (--nrbufs >= 0) {
  652. count += pipe->bufs[buf].len;
  653. buf = (buf+1) & (pipe->buffers - 1);
  654. }
  655. mutex_unlock(&inode->i_mutex);
  656. return put_user(count, (int __user *)arg);
  657. default:
  658. return -ENOIOCTLCMD;
  659. }
  660. }
  661. /* No kernel lock held - fine */
  662. static unsigned int
  663. pipe_poll(struct file *filp, poll_table *wait)
  664. {
  665. unsigned int mask;
  666. struct inode *inode = filp->f_path.dentry->d_inode;
  667. struct pipe_inode_info *pipe = inode->i_pipe;
  668. int nrbufs;
  669. poll_wait(filp, &pipe->wait, wait);
  670. /* Reading only -- no need for acquiring the semaphore. */
  671. nrbufs = pipe->nrbufs;
  672. mask = 0;
  673. if (filp->f_mode & FMODE_READ) {
  674. mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
  675. if (!pipe->writers && filp->f_version != pipe->w_counter)
  676. mask |= POLLHUP;
  677. }
  678. if (filp->f_mode & FMODE_WRITE) {
  679. mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
  680. /*
  681. * Most Unices do not set POLLERR for FIFOs but on Linux they
  682. * behave exactly like pipes for poll().
  683. */
  684. if (!pipe->readers)
  685. mask |= POLLERR;
  686. }
  687. return mask;
  688. }
  689. static int
  690. pipe_release(struct inode *inode, int decr, int decw)
  691. {
  692. struct pipe_inode_info *pipe;
  693. mutex_lock(&inode->i_mutex);
  694. pipe = inode->i_pipe;
  695. pipe->readers -= decr;
  696. pipe->writers -= decw;
  697. if (!pipe->readers && !pipe->writers) {
  698. free_pipe_info(inode);
  699. } else {
  700. wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
  701. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  702. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  703. }
  704. mutex_unlock(&inode->i_mutex);
  705. return 0;
  706. }
  707. static int
  708. pipe_read_fasync(int fd, struct file *filp, int on)
  709. {
  710. struct inode *inode = filp->f_path.dentry->d_inode;
  711. int retval;
  712. mutex_lock(&inode->i_mutex);
  713. retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
  714. mutex_unlock(&inode->i_mutex);
  715. return retval;
  716. }
  717. static int
  718. pipe_write_fasync(int fd, struct file *filp, int on)
  719. {
  720. struct inode *inode = filp->f_path.dentry->d_inode;
  721. int retval;
  722. mutex_lock(&inode->i_mutex);
  723. retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
  724. mutex_unlock(&inode->i_mutex);
  725. return retval;
  726. }
  727. static int
  728. pipe_rdwr_fasync(int fd, struct file *filp, int on)
  729. {
  730. struct inode *inode = filp->f_path.dentry->d_inode;
  731. struct pipe_inode_info *pipe = inode->i_pipe;
  732. int retval;
  733. mutex_lock(&inode->i_mutex);
  734. retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
  735. if (retval >= 0) {
  736. retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
  737. if (retval < 0) /* this can happen only if on == T */
  738. fasync_helper(-1, filp, 0, &pipe->fasync_readers);
  739. }
  740. mutex_unlock(&inode->i_mutex);
  741. return retval;
  742. }
  743. static int
  744. pipe_read_release(struct inode *inode, struct file *filp)
  745. {
  746. return pipe_release(inode, 1, 0);
  747. }
  748. static int
  749. pipe_write_release(struct inode *inode, struct file *filp)
  750. {
  751. return pipe_release(inode, 0, 1);
  752. }
  753. static int
  754. pipe_rdwr_release(struct inode *inode, struct file *filp)
  755. {
  756. int decr, decw;
  757. decr = (filp->f_mode & FMODE_READ) != 0;
  758. decw = (filp->f_mode & FMODE_WRITE) != 0;
  759. return pipe_release(inode, decr, decw);
  760. }
  761. static int
  762. pipe_read_open(struct inode *inode, struct file *filp)
  763. {
  764. int ret = -ENOENT;
  765. mutex_lock(&inode->i_mutex);
  766. if (inode->i_pipe) {
  767. ret = 0;
  768. inode->i_pipe->readers++;
  769. }
  770. mutex_unlock(&inode->i_mutex);
  771. return ret;
  772. }
  773. static int
  774. pipe_write_open(struct inode *inode, struct file *filp)
  775. {
  776. int ret = -ENOENT;
  777. mutex_lock(&inode->i_mutex);
  778. if (inode->i_pipe) {
  779. ret = 0;
  780. inode->i_pipe->writers++;
  781. }
  782. mutex_unlock(&inode->i_mutex);
  783. return ret;
  784. }
  785. static int
  786. pipe_rdwr_open(struct inode *inode, struct file *filp)
  787. {
  788. int ret = -ENOENT;
  789. if (!(filp->f_mode & (FMODE_READ|FMODE_WRITE)))
  790. return -EINVAL;
  791. mutex_lock(&inode->i_mutex);
  792. if (inode->i_pipe) {
  793. ret = 0;
  794. if (filp->f_mode & FMODE_READ)
  795. inode->i_pipe->readers++;
  796. if (filp->f_mode & FMODE_WRITE)
  797. inode->i_pipe->writers++;
  798. }
  799. mutex_unlock(&inode->i_mutex);
  800. return ret;
  801. }
  802. /*
  803. * The file_operations structs are not static because they
  804. * are also used in linux/fs/fifo.c to do operations on FIFOs.
  805. *
  806. * Pipes reuse fifos' file_operations structs.
  807. */
  808. const struct file_operations read_pipefifo_fops = {
  809. .llseek = no_llseek,
  810. .read = do_sync_read,
  811. .aio_read = pipe_read,
  812. .write = bad_pipe_w,
  813. .poll = pipe_poll,
  814. .unlocked_ioctl = pipe_ioctl,
  815. .open = pipe_read_open,
  816. .release = pipe_read_release,
  817. .fasync = pipe_read_fasync,
  818. };
  819. const struct file_operations write_pipefifo_fops = {
  820. .llseek = no_llseek,
  821. .read = bad_pipe_r,
  822. .write = do_sync_write,
  823. .aio_write = pipe_write,
  824. .poll = pipe_poll,
  825. .unlocked_ioctl = pipe_ioctl,
  826. .open = pipe_write_open,
  827. .release = pipe_write_release,
  828. .fasync = pipe_write_fasync,
  829. };
  830. const struct file_operations rdwr_pipefifo_fops = {
  831. .llseek = no_llseek,
  832. .read = do_sync_read,
  833. .aio_read = pipe_read,
  834. .write = do_sync_write,
  835. .aio_write = pipe_write,
  836. .poll = pipe_poll,
  837. .unlocked_ioctl = pipe_ioctl,
  838. .open = pipe_rdwr_open,
  839. .release = pipe_rdwr_release,
  840. .fasync = pipe_rdwr_fasync,
  841. };
  842. static void account_pipe_buffers(struct pipe_inode_info *pipe,
  843. unsigned long old, unsigned long new)
  844. {
  845. atomic_long_add(new - old, &pipe->user->pipe_bufs);
  846. }
  847. static bool too_many_pipe_buffers_soft(struct user_struct *user)
  848. {
  849. return pipe_user_pages_soft &&
  850. atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_soft;
  851. }
  852. static bool too_many_pipe_buffers_hard(struct user_struct *user)
  853. {
  854. return pipe_user_pages_hard &&
  855. atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_hard;
  856. }
  857. struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
  858. {
  859. struct pipe_inode_info *pipe;
  860. pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
  861. if (pipe) {
  862. unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
  863. struct user_struct *user = get_current_user();
  864. if (!too_many_pipe_buffers_hard(user)) {
  865. if (too_many_pipe_buffers_soft(user))
  866. pipe_bufs = 1;
  867. pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * pipe_bufs, GFP_KERNEL);
  868. }
  869. if (pipe->bufs) {
  870. init_waitqueue_head(&pipe->wait);
  871. pipe->r_counter = pipe->w_counter = 1;
  872. pipe->inode = inode;
  873. pipe->buffers = pipe_bufs;
  874. pipe->user = user;
  875. account_pipe_buffers(pipe, 0, pipe_bufs);
  876. return pipe;
  877. }
  878. free_uid(user);
  879. kfree(pipe);
  880. }
  881. return NULL;
  882. }
  883. void __free_pipe_info(struct pipe_inode_info *pipe)
  884. {
  885. int i;
  886. account_pipe_buffers(pipe, pipe->buffers, 0);
  887. free_uid(pipe->user);
  888. for (i = 0; i < pipe->buffers; i++) {
  889. struct pipe_buffer *buf = pipe->bufs + i;
  890. if (buf->ops)
  891. buf->ops->release(pipe, buf);
  892. }
  893. if (pipe->tmp_page)
  894. __free_page(pipe->tmp_page);
  895. kfree(pipe->bufs);
  896. kfree(pipe);
  897. }
  898. void free_pipe_info(struct inode *inode)
  899. {
  900. __free_pipe_info(inode->i_pipe);
  901. inode->i_pipe = NULL;
  902. }
  903. static struct vfsmount *pipe_mnt __read_mostly;
  904. /*
  905. * pipefs_dname() is called from d_path().
  906. */
  907. static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
  908. {
  909. return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  910. dentry->d_inode->i_ino);
  911. }
  912. static const struct dentry_operations pipefs_dentry_operations = {
  913. .d_dname = pipefs_dname,
  914. };
  915. static struct inode * get_pipe_inode(void)
  916. {
  917. struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
  918. struct pipe_inode_info *pipe;
  919. if (!inode)
  920. goto fail_inode;
  921. inode->i_ino = get_next_ino();
  922. pipe = alloc_pipe_info(inode);
  923. if (!pipe)
  924. goto fail_iput;
  925. inode->i_pipe = pipe;
  926. pipe->readers = pipe->writers = 1;
  927. inode->i_fop = &rdwr_pipefifo_fops;
  928. /*
  929. * Mark the inode dirty from the very beginning,
  930. * that way it will never be moved to the dirty
  931. * list because "mark_inode_dirty()" will think
  932. * that it already _is_ on the dirty list.
  933. */
  934. inode->i_state = I_DIRTY;
  935. inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
  936. inode->i_uid = current_fsuid();
  937. inode->i_gid = current_fsgid();
  938. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  939. return inode;
  940. fail_iput:
  941. iput(inode);
  942. fail_inode:
  943. return NULL;
  944. }
  945. struct file *create_write_pipe(int flags)
  946. {
  947. int err;
  948. struct inode *inode;
  949. struct file *f;
  950. struct path path;
  951. struct qstr name = { .name = "" };
  952. err = -ENFILE;
  953. inode = get_pipe_inode();
  954. if (!inode)
  955. goto err;
  956. err = -ENOMEM;
  957. path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
  958. if (!path.dentry)
  959. goto err_inode;
  960. path.mnt = mntget(pipe_mnt);
  961. d_instantiate(path.dentry, inode);
  962. err = -ENFILE;
  963. f = alloc_file(&path, FMODE_WRITE, &write_pipefifo_fops);
  964. if (!f)
  965. goto err_dentry;
  966. f->f_mapping = inode->i_mapping;
  967. f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
  968. f->f_version = 0;
  969. return f;
  970. err_dentry:
  971. free_pipe_info(inode);
  972. path_put(&path);
  973. return ERR_PTR(err);
  974. err_inode:
  975. free_pipe_info(inode);
  976. iput(inode);
  977. err:
  978. return ERR_PTR(err);
  979. }
  980. void free_write_pipe(struct file *f)
  981. {
  982. free_pipe_info(f->f_dentry->d_inode);
  983. path_put(&f->f_path);
  984. put_filp(f);
  985. }
  986. struct file *create_read_pipe(struct file *wrf, int flags)
  987. {
  988. /* Grab pipe from the writer */
  989. struct file *f = alloc_file(&wrf->f_path, FMODE_READ,
  990. &read_pipefifo_fops);
  991. if (!f)
  992. return ERR_PTR(-ENFILE);
  993. path_get(&wrf->f_path);
  994. f->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  995. return f;
  996. }
  997. int do_pipe_flags(int *fd, int flags)
  998. {
  999. struct file *fw, *fr;
  1000. int error;
  1001. int fdw, fdr;
  1002. if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
  1003. return -EINVAL;
  1004. fw = create_write_pipe(flags);
  1005. if (IS_ERR(fw))
  1006. return PTR_ERR(fw);
  1007. fr = create_read_pipe(fw, flags);
  1008. error = PTR_ERR(fr);
  1009. if (IS_ERR(fr))
  1010. goto err_write_pipe;
  1011. error = get_unused_fd_flags(flags);
  1012. if (error < 0)
  1013. goto err_read_pipe;
  1014. fdr = error;
  1015. error = get_unused_fd_flags(flags);
  1016. if (error < 0)
  1017. goto err_fdr;
  1018. fdw = error;
  1019. audit_fd_pair(fdr, fdw);
  1020. fd_install(fdr, fr);
  1021. fd_install(fdw, fw);
  1022. fd[0] = fdr;
  1023. fd[1] = fdw;
  1024. return 0;
  1025. err_fdr:
  1026. put_unused_fd(fdr);
  1027. err_read_pipe:
  1028. path_put(&fr->f_path);
  1029. put_filp(fr);
  1030. err_write_pipe:
  1031. free_write_pipe(fw);
  1032. return error;
  1033. }
  1034. /*
  1035. * sys_pipe() is the normal C calling standard for creating
  1036. * a pipe. It's not the way Unix traditionally does this, though.
  1037. */
  1038. SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
  1039. {
  1040. int fd[2];
  1041. int error;
  1042. error = do_pipe_flags(fd, flags);
  1043. if (!error) {
  1044. if (copy_to_user(fildes, fd, sizeof(fd))) {
  1045. sys_close(fd[0]);
  1046. sys_close(fd[1]);
  1047. error = -EFAULT;
  1048. }
  1049. }
  1050. return error;
  1051. }
  1052. SYSCALL_DEFINE1(pipe, int __user *, fildes)
  1053. {
  1054. return sys_pipe2(fildes, 0);
  1055. }
  1056. /*
  1057. * Allocate a new array of pipe buffers and copy the info over. Returns the
  1058. * pipe size if successful, or return -ERROR on error.
  1059. */
  1060. static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
  1061. {
  1062. struct pipe_buffer *bufs;
  1063. /*
  1064. * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
  1065. * expect a lot of shrink+grow operations, just free and allocate
  1066. * again like we would do for growing. If the pipe currently
  1067. * contains more buffers than arg, then return busy.
  1068. */
  1069. if (nr_pages < pipe->nrbufs)
  1070. return -EBUSY;
  1071. bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
  1072. if (unlikely(!bufs))
  1073. return -ENOMEM;
  1074. /*
  1075. * The pipe array wraps around, so just start the new one at zero
  1076. * and adjust the indexes.
  1077. */
  1078. if (pipe->nrbufs) {
  1079. unsigned int tail;
  1080. unsigned int head;
  1081. tail = pipe->curbuf + pipe->nrbufs;
  1082. if (tail < pipe->buffers)
  1083. tail = 0;
  1084. else
  1085. tail &= (pipe->buffers - 1);
  1086. head = pipe->nrbufs - tail;
  1087. if (head)
  1088. memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
  1089. if (tail)
  1090. memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
  1091. }
  1092. account_pipe_buffers(pipe, pipe->buffers, nr_pages);
  1093. pipe->curbuf = 0;
  1094. kfree(pipe->bufs);
  1095. pipe->bufs = bufs;
  1096. pipe->buffers = nr_pages;
  1097. return nr_pages * PAGE_SIZE;
  1098. }
  1099. /*
  1100. * Currently we rely on the pipe array holding a power-of-2 number
  1101. * of pages.
  1102. */
  1103. static inline unsigned int round_pipe_size(unsigned int size)
  1104. {
  1105. unsigned long nr_pages;
  1106. nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1107. return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
  1108. }
  1109. /*
  1110. * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
  1111. * will return an error.
  1112. */
  1113. int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
  1114. size_t *lenp, loff_t *ppos)
  1115. {
  1116. int ret;
  1117. ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
  1118. if (ret < 0 || !write)
  1119. return ret;
  1120. pipe_max_size = round_pipe_size(pipe_max_size);
  1121. return ret;
  1122. }
  1123. /*
  1124. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  1125. * location, so checking ->i_pipe is not enough to verify that this is a
  1126. * pipe.
  1127. */
  1128. struct pipe_inode_info *get_pipe_info(struct file *file)
  1129. {
  1130. struct inode *i = file->f_path.dentry->d_inode;
  1131. return S_ISFIFO(i->i_mode) ? i->i_pipe : NULL;
  1132. }
  1133. long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  1134. {
  1135. struct pipe_inode_info *pipe;
  1136. long ret;
  1137. pipe = get_pipe_info(file);
  1138. if (!pipe)
  1139. return -EBADF;
  1140. mutex_lock(&pipe->inode->i_mutex);
  1141. switch (cmd) {
  1142. case F_SETPIPE_SZ: {
  1143. unsigned int size, nr_pages;
  1144. size = round_pipe_size(arg);
  1145. nr_pages = size >> PAGE_SHIFT;
  1146. ret = -EINVAL;
  1147. if (!nr_pages)
  1148. goto out;
  1149. if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
  1150. ret = -EPERM;
  1151. goto out;
  1152. } else if ((too_many_pipe_buffers_hard(pipe->user) ||
  1153. too_many_pipe_buffers_soft(pipe->user)) &&
  1154. !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
  1155. ret = -EPERM;
  1156. goto out;
  1157. }
  1158. ret = pipe_set_size(pipe, nr_pages);
  1159. break;
  1160. }
  1161. case F_GETPIPE_SZ:
  1162. ret = pipe->buffers * PAGE_SIZE;
  1163. break;
  1164. default:
  1165. ret = -EINVAL;
  1166. break;
  1167. }
  1168. out:
  1169. mutex_unlock(&pipe->inode->i_mutex);
  1170. return ret;
  1171. }
  1172. static const struct super_operations pipefs_ops = {
  1173. .destroy_inode = free_inode_nonrcu,
  1174. .statfs = simple_statfs,
  1175. };
  1176. /*
  1177. * pipefs should _never_ be mounted by userland - too much of security hassle,
  1178. * no real gain from having the whole whorehouse mounted. So we don't need
  1179. * any operations on the root directory. However, we need a non-trivial
  1180. * d_name - pipe: will go nicely and kill the special-casing in procfs.
  1181. */
  1182. static struct dentry *pipefs_mount(struct file_system_type *fs_type,
  1183. int flags, const char *dev_name, void *data)
  1184. {
  1185. return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
  1186. &pipefs_dentry_operations, PIPEFS_MAGIC);
  1187. }
  1188. static struct file_system_type pipe_fs_type = {
  1189. .name = "pipefs",
  1190. .mount = pipefs_mount,
  1191. .kill_sb = kill_anon_super,
  1192. };
  1193. static int __init init_pipe_fs(void)
  1194. {
  1195. int err = register_filesystem(&pipe_fs_type);
  1196. if (!err) {
  1197. pipe_mnt = kern_mount(&pipe_fs_type);
  1198. if (IS_ERR(pipe_mnt)) {
  1199. err = PTR_ERR(pipe_mnt);
  1200. unregister_filesystem(&pipe_fs_type);
  1201. }
  1202. }
  1203. return err;
  1204. }
  1205. fs_initcall(init_pipe_fs);