rpc_pipe.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * net/sunrpc/rpc_pipe.c
  4. *
  5. * Userland/kernel interface for rpcauth_gss.
  6. * Code shamelessly plagiarized from fs/nfsd/nfsctl.c
  7. * and fs/sysfs/inode.c
  8. *
  9. * Copyright (c) 2002, Trond Myklebust <[email protected]>
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/string.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/mount.h>
  17. #include <linux/fs_context.h>
  18. #include <linux/namei.h>
  19. #include <linux/fsnotify.h>
  20. #include <linux/kernel.h>
  21. #include <linux/rcupdate.h>
  22. #include <linux/utsname.h>
  23. #include <asm/ioctls.h>
  24. #include <linux/poll.h>
  25. #include <linux/wait.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/sunrpc/clnt.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/sunrpc/rpc_pipe_fs.h>
  30. #include <linux/sunrpc/cache.h>
  31. #include <linux/nsproxy.h>
  32. #include <linux/notifier.h>
  33. #include "netns.h"
  34. #include "sunrpc.h"
  35. #define RPCDBG_FACILITY RPCDBG_DEBUG
  36. #define NET_NAME(net) ((net == &init_net) ? " (init_net)" : "")
  37. static struct file_system_type rpc_pipe_fs_type;
  38. static const struct rpc_pipe_ops gssd_dummy_pipe_ops;
  39. static struct kmem_cache *rpc_inode_cachep __read_mostly;
  40. #define RPC_UPCALL_TIMEOUT (30*HZ)
  41. static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list);
  42. int rpc_pipefs_notifier_register(struct notifier_block *nb)
  43. {
  44. return blocking_notifier_chain_register(&rpc_pipefs_notifier_list, nb);
  45. }
  46. EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register);
  47. void rpc_pipefs_notifier_unregister(struct notifier_block *nb)
  48. {
  49. blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list, nb);
  50. }
  51. EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister);
  52. static void rpc_purge_list(wait_queue_head_t *waitq, struct list_head *head,
  53. void (*destroy_msg)(struct rpc_pipe_msg *), int err)
  54. {
  55. struct rpc_pipe_msg *msg;
  56. if (list_empty(head))
  57. return;
  58. do {
  59. msg = list_entry(head->next, struct rpc_pipe_msg, list);
  60. list_del_init(&msg->list);
  61. msg->errno = err;
  62. destroy_msg(msg);
  63. } while (!list_empty(head));
  64. if (waitq)
  65. wake_up(waitq);
  66. }
  67. static void
  68. rpc_timeout_upcall_queue(struct work_struct *work)
  69. {
  70. LIST_HEAD(free_list);
  71. struct rpc_pipe *pipe =
  72. container_of(work, struct rpc_pipe, queue_timeout.work);
  73. void (*destroy_msg)(struct rpc_pipe_msg *);
  74. struct dentry *dentry;
  75. spin_lock(&pipe->lock);
  76. destroy_msg = pipe->ops->destroy_msg;
  77. if (pipe->nreaders == 0) {
  78. list_splice_init(&pipe->pipe, &free_list);
  79. pipe->pipelen = 0;
  80. }
  81. dentry = dget(pipe->dentry);
  82. spin_unlock(&pipe->lock);
  83. rpc_purge_list(dentry ? &RPC_I(d_inode(dentry))->waitq : NULL,
  84. &free_list, destroy_msg, -ETIMEDOUT);
  85. dput(dentry);
  86. }
  87. ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
  88. char __user *dst, size_t buflen)
  89. {
  90. char *data = (char *)msg->data + msg->copied;
  91. size_t mlen = min(msg->len - msg->copied, buflen);
  92. unsigned long left;
  93. left = copy_to_user(dst, data, mlen);
  94. if (left == mlen) {
  95. msg->errno = -EFAULT;
  96. return -EFAULT;
  97. }
  98. mlen -= left;
  99. msg->copied += mlen;
  100. msg->errno = 0;
  101. return mlen;
  102. }
  103. EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
  104. /**
  105. * rpc_queue_upcall - queue an upcall message to userspace
  106. * @pipe: upcall pipe on which to queue given message
  107. * @msg: message to queue
  108. *
  109. * Call with an @inode created by rpc_mkpipe() to queue an upcall.
  110. * A userspace process may then later read the upcall by performing a
  111. * read on an open file for this inode. It is up to the caller to
  112. * initialize the fields of @msg (other than @msg->list) appropriately.
  113. */
  114. int
  115. rpc_queue_upcall(struct rpc_pipe *pipe, struct rpc_pipe_msg *msg)
  116. {
  117. int res = -EPIPE;
  118. struct dentry *dentry;
  119. spin_lock(&pipe->lock);
  120. if (pipe->nreaders) {
  121. list_add_tail(&msg->list, &pipe->pipe);
  122. pipe->pipelen += msg->len;
  123. res = 0;
  124. } else if (pipe->flags & RPC_PIPE_WAIT_FOR_OPEN) {
  125. if (list_empty(&pipe->pipe))
  126. queue_delayed_work(rpciod_workqueue,
  127. &pipe->queue_timeout,
  128. RPC_UPCALL_TIMEOUT);
  129. list_add_tail(&msg->list, &pipe->pipe);
  130. pipe->pipelen += msg->len;
  131. res = 0;
  132. }
  133. dentry = dget(pipe->dentry);
  134. spin_unlock(&pipe->lock);
  135. if (dentry) {
  136. wake_up(&RPC_I(d_inode(dentry))->waitq);
  137. dput(dentry);
  138. }
  139. return res;
  140. }
  141. EXPORT_SYMBOL_GPL(rpc_queue_upcall);
  142. static inline void
  143. rpc_inode_setowner(struct inode *inode, void *private)
  144. {
  145. RPC_I(inode)->private = private;
  146. }
  147. static void
  148. rpc_close_pipes(struct inode *inode)
  149. {
  150. struct rpc_pipe *pipe = RPC_I(inode)->pipe;
  151. int need_release;
  152. LIST_HEAD(free_list);
  153. inode_lock(inode);
  154. spin_lock(&pipe->lock);
  155. need_release = pipe->nreaders != 0 || pipe->nwriters != 0;
  156. pipe->nreaders = 0;
  157. list_splice_init(&pipe->in_upcall, &free_list);
  158. list_splice_init(&pipe->pipe, &free_list);
  159. pipe->pipelen = 0;
  160. pipe->dentry = NULL;
  161. spin_unlock(&pipe->lock);
  162. rpc_purge_list(&RPC_I(inode)->waitq, &free_list, pipe->ops->destroy_msg, -EPIPE);
  163. pipe->nwriters = 0;
  164. if (need_release && pipe->ops->release_pipe)
  165. pipe->ops->release_pipe(inode);
  166. cancel_delayed_work_sync(&pipe->queue_timeout);
  167. rpc_inode_setowner(inode, NULL);
  168. RPC_I(inode)->pipe = NULL;
  169. inode_unlock(inode);
  170. }
  171. static struct inode *
  172. rpc_alloc_inode(struct super_block *sb)
  173. {
  174. struct rpc_inode *rpci;
  175. rpci = alloc_inode_sb(sb, rpc_inode_cachep, GFP_KERNEL);
  176. if (!rpci)
  177. return NULL;
  178. return &rpci->vfs_inode;
  179. }
  180. static void
  181. rpc_free_inode(struct inode *inode)
  182. {
  183. kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
  184. }
  185. static int
  186. rpc_pipe_open(struct inode *inode, struct file *filp)
  187. {
  188. struct rpc_pipe *pipe;
  189. int first_open;
  190. int res = -ENXIO;
  191. inode_lock(inode);
  192. pipe = RPC_I(inode)->pipe;
  193. if (pipe == NULL)
  194. goto out;
  195. first_open = pipe->nreaders == 0 && pipe->nwriters == 0;
  196. if (first_open && pipe->ops->open_pipe) {
  197. res = pipe->ops->open_pipe(inode);
  198. if (res)
  199. goto out;
  200. }
  201. if (filp->f_mode & FMODE_READ)
  202. pipe->nreaders++;
  203. if (filp->f_mode & FMODE_WRITE)
  204. pipe->nwriters++;
  205. res = 0;
  206. out:
  207. inode_unlock(inode);
  208. return res;
  209. }
  210. static int
  211. rpc_pipe_release(struct inode *inode, struct file *filp)
  212. {
  213. struct rpc_pipe *pipe;
  214. struct rpc_pipe_msg *msg;
  215. int last_close;
  216. inode_lock(inode);
  217. pipe = RPC_I(inode)->pipe;
  218. if (pipe == NULL)
  219. goto out;
  220. msg = filp->private_data;
  221. if (msg != NULL) {
  222. spin_lock(&pipe->lock);
  223. msg->errno = -EAGAIN;
  224. list_del_init(&msg->list);
  225. spin_unlock(&pipe->lock);
  226. pipe->ops->destroy_msg(msg);
  227. }
  228. if (filp->f_mode & FMODE_WRITE)
  229. pipe->nwriters --;
  230. if (filp->f_mode & FMODE_READ) {
  231. pipe->nreaders --;
  232. if (pipe->nreaders == 0) {
  233. LIST_HEAD(free_list);
  234. spin_lock(&pipe->lock);
  235. list_splice_init(&pipe->pipe, &free_list);
  236. pipe->pipelen = 0;
  237. spin_unlock(&pipe->lock);
  238. rpc_purge_list(&RPC_I(inode)->waitq, &free_list,
  239. pipe->ops->destroy_msg, -EAGAIN);
  240. }
  241. }
  242. last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
  243. if (last_close && pipe->ops->release_pipe)
  244. pipe->ops->release_pipe(inode);
  245. out:
  246. inode_unlock(inode);
  247. return 0;
  248. }
  249. static ssize_t
  250. rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
  251. {
  252. struct inode *inode = file_inode(filp);
  253. struct rpc_pipe *pipe;
  254. struct rpc_pipe_msg *msg;
  255. int res = 0;
  256. inode_lock(inode);
  257. pipe = RPC_I(inode)->pipe;
  258. if (pipe == NULL) {
  259. res = -EPIPE;
  260. goto out_unlock;
  261. }
  262. msg = filp->private_data;
  263. if (msg == NULL) {
  264. spin_lock(&pipe->lock);
  265. if (!list_empty(&pipe->pipe)) {
  266. msg = list_entry(pipe->pipe.next,
  267. struct rpc_pipe_msg,
  268. list);
  269. list_move(&msg->list, &pipe->in_upcall);
  270. pipe->pipelen -= msg->len;
  271. filp->private_data = msg;
  272. msg->copied = 0;
  273. }
  274. spin_unlock(&pipe->lock);
  275. if (msg == NULL)
  276. goto out_unlock;
  277. }
  278. /* NOTE: it is up to the callback to update msg->copied */
  279. res = pipe->ops->upcall(filp, msg, buf, len);
  280. if (res < 0 || msg->len == msg->copied) {
  281. filp->private_data = NULL;
  282. spin_lock(&pipe->lock);
  283. list_del_init(&msg->list);
  284. spin_unlock(&pipe->lock);
  285. pipe->ops->destroy_msg(msg);
  286. }
  287. out_unlock:
  288. inode_unlock(inode);
  289. return res;
  290. }
  291. static ssize_t
  292. rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
  293. {
  294. struct inode *inode = file_inode(filp);
  295. int res;
  296. inode_lock(inode);
  297. res = -EPIPE;
  298. if (RPC_I(inode)->pipe != NULL)
  299. res = RPC_I(inode)->pipe->ops->downcall(filp, buf, len);
  300. inode_unlock(inode);
  301. return res;
  302. }
  303. static __poll_t
  304. rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
  305. {
  306. struct inode *inode = file_inode(filp);
  307. struct rpc_inode *rpci = RPC_I(inode);
  308. __poll_t mask = EPOLLOUT | EPOLLWRNORM;
  309. poll_wait(filp, &rpci->waitq, wait);
  310. inode_lock(inode);
  311. if (rpci->pipe == NULL)
  312. mask |= EPOLLERR | EPOLLHUP;
  313. else if (filp->private_data || !list_empty(&rpci->pipe->pipe))
  314. mask |= EPOLLIN | EPOLLRDNORM;
  315. inode_unlock(inode);
  316. return mask;
  317. }
  318. static long
  319. rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  320. {
  321. struct inode *inode = file_inode(filp);
  322. struct rpc_pipe *pipe;
  323. int len;
  324. switch (cmd) {
  325. case FIONREAD:
  326. inode_lock(inode);
  327. pipe = RPC_I(inode)->pipe;
  328. if (pipe == NULL) {
  329. inode_unlock(inode);
  330. return -EPIPE;
  331. }
  332. spin_lock(&pipe->lock);
  333. len = pipe->pipelen;
  334. if (filp->private_data) {
  335. struct rpc_pipe_msg *msg;
  336. msg = filp->private_data;
  337. len += msg->len - msg->copied;
  338. }
  339. spin_unlock(&pipe->lock);
  340. inode_unlock(inode);
  341. return put_user(len, (int __user *)arg);
  342. default:
  343. return -EINVAL;
  344. }
  345. }
  346. static const struct file_operations rpc_pipe_fops = {
  347. .owner = THIS_MODULE,
  348. .llseek = no_llseek,
  349. .read = rpc_pipe_read,
  350. .write = rpc_pipe_write,
  351. .poll = rpc_pipe_poll,
  352. .unlocked_ioctl = rpc_pipe_ioctl,
  353. .open = rpc_pipe_open,
  354. .release = rpc_pipe_release,
  355. };
  356. static int
  357. rpc_show_info(struct seq_file *m, void *v)
  358. {
  359. struct rpc_clnt *clnt = m->private;
  360. rcu_read_lock();
  361. seq_printf(m, "RPC server: %s\n",
  362. rcu_dereference(clnt->cl_xprt)->servername);
  363. seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_program->name,
  364. clnt->cl_prog, clnt->cl_vers);
  365. seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
  366. seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
  367. seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
  368. rcu_read_unlock();
  369. return 0;
  370. }
  371. static int
  372. rpc_info_open(struct inode *inode, struct file *file)
  373. {
  374. struct rpc_clnt *clnt = NULL;
  375. int ret = single_open(file, rpc_show_info, NULL);
  376. if (!ret) {
  377. struct seq_file *m = file->private_data;
  378. spin_lock(&file->f_path.dentry->d_lock);
  379. if (!d_unhashed(file->f_path.dentry))
  380. clnt = RPC_I(inode)->private;
  381. if (clnt != NULL && refcount_inc_not_zero(&clnt->cl_count)) {
  382. spin_unlock(&file->f_path.dentry->d_lock);
  383. m->private = clnt;
  384. } else {
  385. spin_unlock(&file->f_path.dentry->d_lock);
  386. single_release(inode, file);
  387. ret = -EINVAL;
  388. }
  389. }
  390. return ret;
  391. }
  392. static int
  393. rpc_info_release(struct inode *inode, struct file *file)
  394. {
  395. struct seq_file *m = file->private_data;
  396. struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
  397. if (clnt)
  398. rpc_release_client(clnt);
  399. return single_release(inode, file);
  400. }
  401. static const struct file_operations rpc_info_operations = {
  402. .owner = THIS_MODULE,
  403. .open = rpc_info_open,
  404. .read = seq_read,
  405. .llseek = seq_lseek,
  406. .release = rpc_info_release,
  407. };
  408. /*
  409. * Description of fs contents.
  410. */
  411. struct rpc_filelist {
  412. const char *name;
  413. const struct file_operations *i_fop;
  414. umode_t mode;
  415. };
  416. static struct inode *
  417. rpc_get_inode(struct super_block *sb, umode_t mode)
  418. {
  419. struct inode *inode = new_inode(sb);
  420. if (!inode)
  421. return NULL;
  422. inode->i_ino = get_next_ino();
  423. inode->i_mode = mode;
  424. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  425. switch (mode & S_IFMT) {
  426. case S_IFDIR:
  427. inode->i_fop = &simple_dir_operations;
  428. inode->i_op = &simple_dir_inode_operations;
  429. inc_nlink(inode);
  430. break;
  431. default:
  432. break;
  433. }
  434. return inode;
  435. }
  436. static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
  437. umode_t mode,
  438. const struct file_operations *i_fop,
  439. void *private)
  440. {
  441. struct inode *inode;
  442. d_drop(dentry);
  443. inode = rpc_get_inode(dir->i_sb, mode);
  444. if (!inode)
  445. goto out_err;
  446. inode->i_ino = iunique(dir->i_sb, 100);
  447. if (i_fop)
  448. inode->i_fop = i_fop;
  449. if (private)
  450. rpc_inode_setowner(inode, private);
  451. d_add(dentry, inode);
  452. return 0;
  453. out_err:
  454. printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %pd\n",
  455. __FILE__, __func__, dentry);
  456. dput(dentry);
  457. return -ENOMEM;
  458. }
  459. static int __rpc_create(struct inode *dir, struct dentry *dentry,
  460. umode_t mode,
  461. const struct file_operations *i_fop,
  462. void *private)
  463. {
  464. int err;
  465. err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
  466. if (err)
  467. return err;
  468. fsnotify_create(dir, dentry);
  469. return 0;
  470. }
  471. static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
  472. umode_t mode,
  473. const struct file_operations *i_fop,
  474. void *private)
  475. {
  476. int err;
  477. err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
  478. if (err)
  479. return err;
  480. inc_nlink(dir);
  481. fsnotify_mkdir(dir, dentry);
  482. return 0;
  483. }
  484. static void
  485. init_pipe(struct rpc_pipe *pipe)
  486. {
  487. pipe->nreaders = 0;
  488. pipe->nwriters = 0;
  489. INIT_LIST_HEAD(&pipe->in_upcall);
  490. INIT_LIST_HEAD(&pipe->in_downcall);
  491. INIT_LIST_HEAD(&pipe->pipe);
  492. pipe->pipelen = 0;
  493. INIT_DELAYED_WORK(&pipe->queue_timeout,
  494. rpc_timeout_upcall_queue);
  495. pipe->ops = NULL;
  496. spin_lock_init(&pipe->lock);
  497. pipe->dentry = NULL;
  498. }
  499. void rpc_destroy_pipe_data(struct rpc_pipe *pipe)
  500. {
  501. kfree(pipe);
  502. }
  503. EXPORT_SYMBOL_GPL(rpc_destroy_pipe_data);
  504. struct rpc_pipe *rpc_mkpipe_data(const struct rpc_pipe_ops *ops, int flags)
  505. {
  506. struct rpc_pipe *pipe;
  507. pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
  508. if (!pipe)
  509. return ERR_PTR(-ENOMEM);
  510. init_pipe(pipe);
  511. pipe->ops = ops;
  512. pipe->flags = flags;
  513. return pipe;
  514. }
  515. EXPORT_SYMBOL_GPL(rpc_mkpipe_data);
  516. static int __rpc_mkpipe_dentry(struct inode *dir, struct dentry *dentry,
  517. umode_t mode,
  518. const struct file_operations *i_fop,
  519. void *private,
  520. struct rpc_pipe *pipe)
  521. {
  522. struct rpc_inode *rpci;
  523. int err;
  524. err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
  525. if (err)
  526. return err;
  527. rpci = RPC_I(d_inode(dentry));
  528. rpci->private = private;
  529. rpci->pipe = pipe;
  530. fsnotify_create(dir, dentry);
  531. return 0;
  532. }
  533. static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
  534. {
  535. int ret;
  536. dget(dentry);
  537. ret = simple_rmdir(dir, dentry);
  538. d_drop(dentry);
  539. if (!ret)
  540. fsnotify_rmdir(dir, dentry);
  541. dput(dentry);
  542. return ret;
  543. }
  544. static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
  545. {
  546. int ret;
  547. dget(dentry);
  548. ret = simple_unlink(dir, dentry);
  549. d_drop(dentry);
  550. if (!ret)
  551. fsnotify_unlink(dir, dentry);
  552. dput(dentry);
  553. return ret;
  554. }
  555. static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
  556. {
  557. struct inode *inode = d_inode(dentry);
  558. rpc_close_pipes(inode);
  559. return __rpc_unlink(dir, dentry);
  560. }
  561. static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
  562. const char *name)
  563. {
  564. struct qstr q = QSTR_INIT(name, strlen(name));
  565. struct dentry *dentry = d_hash_and_lookup(parent, &q);
  566. if (!dentry) {
  567. dentry = d_alloc(parent, &q);
  568. if (!dentry)
  569. return ERR_PTR(-ENOMEM);
  570. }
  571. if (d_really_is_negative(dentry))
  572. return dentry;
  573. dput(dentry);
  574. return ERR_PTR(-EEXIST);
  575. }
  576. /*
  577. * FIXME: This probably has races.
  578. */
  579. static void __rpc_depopulate(struct dentry *parent,
  580. const struct rpc_filelist *files,
  581. int start, int eof)
  582. {
  583. struct inode *dir = d_inode(parent);
  584. struct dentry *dentry;
  585. struct qstr name;
  586. int i;
  587. for (i = start; i < eof; i++) {
  588. name.name = files[i].name;
  589. name.len = strlen(files[i].name);
  590. dentry = d_hash_and_lookup(parent, &name);
  591. if (dentry == NULL)
  592. continue;
  593. if (d_really_is_negative(dentry))
  594. goto next;
  595. switch (d_inode(dentry)->i_mode & S_IFMT) {
  596. default:
  597. BUG();
  598. case S_IFREG:
  599. __rpc_unlink(dir, dentry);
  600. break;
  601. case S_IFDIR:
  602. __rpc_rmdir(dir, dentry);
  603. }
  604. next:
  605. dput(dentry);
  606. }
  607. }
  608. static void rpc_depopulate(struct dentry *parent,
  609. const struct rpc_filelist *files,
  610. int start, int eof)
  611. {
  612. struct inode *dir = d_inode(parent);
  613. inode_lock_nested(dir, I_MUTEX_CHILD);
  614. __rpc_depopulate(parent, files, start, eof);
  615. inode_unlock(dir);
  616. }
  617. static int rpc_populate(struct dentry *parent,
  618. const struct rpc_filelist *files,
  619. int start, int eof,
  620. void *private)
  621. {
  622. struct inode *dir = d_inode(parent);
  623. struct dentry *dentry;
  624. int i, err;
  625. inode_lock(dir);
  626. for (i = start; i < eof; i++) {
  627. dentry = __rpc_lookup_create_exclusive(parent, files[i].name);
  628. err = PTR_ERR(dentry);
  629. if (IS_ERR(dentry))
  630. goto out_bad;
  631. switch (files[i].mode & S_IFMT) {
  632. default:
  633. BUG();
  634. case S_IFREG:
  635. err = __rpc_create(dir, dentry,
  636. files[i].mode,
  637. files[i].i_fop,
  638. private);
  639. break;
  640. case S_IFDIR:
  641. err = __rpc_mkdir(dir, dentry,
  642. files[i].mode,
  643. NULL,
  644. private);
  645. }
  646. if (err != 0)
  647. goto out_bad;
  648. }
  649. inode_unlock(dir);
  650. return 0;
  651. out_bad:
  652. __rpc_depopulate(parent, files, start, eof);
  653. inode_unlock(dir);
  654. printk(KERN_WARNING "%s: %s failed to populate directory %pd\n",
  655. __FILE__, __func__, parent);
  656. return err;
  657. }
  658. static struct dentry *rpc_mkdir_populate(struct dentry *parent,
  659. const char *name, umode_t mode, void *private,
  660. int (*populate)(struct dentry *, void *), void *args_populate)
  661. {
  662. struct dentry *dentry;
  663. struct inode *dir = d_inode(parent);
  664. int error;
  665. inode_lock_nested(dir, I_MUTEX_PARENT);
  666. dentry = __rpc_lookup_create_exclusive(parent, name);
  667. if (IS_ERR(dentry))
  668. goto out;
  669. error = __rpc_mkdir(dir, dentry, mode, NULL, private);
  670. if (error != 0)
  671. goto out_err;
  672. if (populate != NULL) {
  673. error = populate(dentry, args_populate);
  674. if (error)
  675. goto err_rmdir;
  676. }
  677. out:
  678. inode_unlock(dir);
  679. return dentry;
  680. err_rmdir:
  681. __rpc_rmdir(dir, dentry);
  682. out_err:
  683. dentry = ERR_PTR(error);
  684. goto out;
  685. }
  686. static int rpc_rmdir_depopulate(struct dentry *dentry,
  687. void (*depopulate)(struct dentry *))
  688. {
  689. struct dentry *parent;
  690. struct inode *dir;
  691. int error;
  692. parent = dget_parent(dentry);
  693. dir = d_inode(parent);
  694. inode_lock_nested(dir, I_MUTEX_PARENT);
  695. if (depopulate != NULL)
  696. depopulate(dentry);
  697. error = __rpc_rmdir(dir, dentry);
  698. inode_unlock(dir);
  699. dput(parent);
  700. return error;
  701. }
  702. /**
  703. * rpc_mkpipe_dentry - make an rpc_pipefs file for kernel<->userspace
  704. * communication
  705. * @parent: dentry of directory to create new "pipe" in
  706. * @name: name of pipe
  707. * @private: private data to associate with the pipe, for the caller's use
  708. * @pipe: &rpc_pipe containing input parameters
  709. *
  710. * Data is made available for userspace to read by calls to
  711. * rpc_queue_upcall(). The actual reads will result in calls to
  712. * @ops->upcall, which will be called with the file pointer,
  713. * message, and userspace buffer to copy to.
  714. *
  715. * Writes can come at any time, and do not necessarily have to be
  716. * responses to upcalls. They will result in calls to @msg->downcall.
  717. *
  718. * The @private argument passed here will be available to all these methods
  719. * from the file pointer, via RPC_I(file_inode(file))->private.
  720. */
  721. struct dentry *rpc_mkpipe_dentry(struct dentry *parent, const char *name,
  722. void *private, struct rpc_pipe *pipe)
  723. {
  724. struct dentry *dentry;
  725. struct inode *dir = d_inode(parent);
  726. umode_t umode = S_IFIFO | 0600;
  727. int err;
  728. if (pipe->ops->upcall == NULL)
  729. umode &= ~0444;
  730. if (pipe->ops->downcall == NULL)
  731. umode &= ~0222;
  732. inode_lock_nested(dir, I_MUTEX_PARENT);
  733. dentry = __rpc_lookup_create_exclusive(parent, name);
  734. if (IS_ERR(dentry))
  735. goto out;
  736. err = __rpc_mkpipe_dentry(dir, dentry, umode, &rpc_pipe_fops,
  737. private, pipe);
  738. if (err)
  739. goto out_err;
  740. out:
  741. inode_unlock(dir);
  742. return dentry;
  743. out_err:
  744. dentry = ERR_PTR(err);
  745. printk(KERN_WARNING "%s: %s() failed to create pipe %pd/%s (errno = %d)\n",
  746. __FILE__, __func__, parent, name,
  747. err);
  748. goto out;
  749. }
  750. EXPORT_SYMBOL_GPL(rpc_mkpipe_dentry);
  751. /**
  752. * rpc_unlink - remove a pipe
  753. * @dentry: dentry for the pipe, as returned from rpc_mkpipe
  754. *
  755. * After this call, lookups will no longer find the pipe, and any
  756. * attempts to read or write using preexisting opens of the pipe will
  757. * return -EPIPE.
  758. */
  759. int
  760. rpc_unlink(struct dentry *dentry)
  761. {
  762. struct dentry *parent;
  763. struct inode *dir;
  764. int error = 0;
  765. parent = dget_parent(dentry);
  766. dir = d_inode(parent);
  767. inode_lock_nested(dir, I_MUTEX_PARENT);
  768. error = __rpc_rmpipe(dir, dentry);
  769. inode_unlock(dir);
  770. dput(parent);
  771. return error;
  772. }
  773. EXPORT_SYMBOL_GPL(rpc_unlink);
  774. /**
  775. * rpc_init_pipe_dir_head - initialise a struct rpc_pipe_dir_head
  776. * @pdh: pointer to struct rpc_pipe_dir_head
  777. */
  778. void rpc_init_pipe_dir_head(struct rpc_pipe_dir_head *pdh)
  779. {
  780. INIT_LIST_HEAD(&pdh->pdh_entries);
  781. pdh->pdh_dentry = NULL;
  782. }
  783. EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_head);
  784. /**
  785. * rpc_init_pipe_dir_object - initialise a struct rpc_pipe_dir_object
  786. * @pdo: pointer to struct rpc_pipe_dir_object
  787. * @pdo_ops: pointer to const struct rpc_pipe_dir_object_ops
  788. * @pdo_data: pointer to caller-defined data
  789. */
  790. void rpc_init_pipe_dir_object(struct rpc_pipe_dir_object *pdo,
  791. const struct rpc_pipe_dir_object_ops *pdo_ops,
  792. void *pdo_data)
  793. {
  794. INIT_LIST_HEAD(&pdo->pdo_head);
  795. pdo->pdo_ops = pdo_ops;
  796. pdo->pdo_data = pdo_data;
  797. }
  798. EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_object);
  799. static int
  800. rpc_add_pipe_dir_object_locked(struct net *net,
  801. struct rpc_pipe_dir_head *pdh,
  802. struct rpc_pipe_dir_object *pdo)
  803. {
  804. int ret = 0;
  805. if (pdh->pdh_dentry)
  806. ret = pdo->pdo_ops->create(pdh->pdh_dentry, pdo);
  807. if (ret == 0)
  808. list_add_tail(&pdo->pdo_head, &pdh->pdh_entries);
  809. return ret;
  810. }
  811. static void
  812. rpc_remove_pipe_dir_object_locked(struct net *net,
  813. struct rpc_pipe_dir_head *pdh,
  814. struct rpc_pipe_dir_object *pdo)
  815. {
  816. if (pdh->pdh_dentry)
  817. pdo->pdo_ops->destroy(pdh->pdh_dentry, pdo);
  818. list_del_init(&pdo->pdo_head);
  819. }
  820. /**
  821. * rpc_add_pipe_dir_object - associate a rpc_pipe_dir_object to a directory
  822. * @net: pointer to struct net
  823. * @pdh: pointer to struct rpc_pipe_dir_head
  824. * @pdo: pointer to struct rpc_pipe_dir_object
  825. *
  826. */
  827. int
  828. rpc_add_pipe_dir_object(struct net *net,
  829. struct rpc_pipe_dir_head *pdh,
  830. struct rpc_pipe_dir_object *pdo)
  831. {
  832. int ret = 0;
  833. if (list_empty(&pdo->pdo_head)) {
  834. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  835. mutex_lock(&sn->pipefs_sb_lock);
  836. ret = rpc_add_pipe_dir_object_locked(net, pdh, pdo);
  837. mutex_unlock(&sn->pipefs_sb_lock);
  838. }
  839. return ret;
  840. }
  841. EXPORT_SYMBOL_GPL(rpc_add_pipe_dir_object);
  842. /**
  843. * rpc_remove_pipe_dir_object - remove a rpc_pipe_dir_object from a directory
  844. * @net: pointer to struct net
  845. * @pdh: pointer to struct rpc_pipe_dir_head
  846. * @pdo: pointer to struct rpc_pipe_dir_object
  847. *
  848. */
  849. void
  850. rpc_remove_pipe_dir_object(struct net *net,
  851. struct rpc_pipe_dir_head *pdh,
  852. struct rpc_pipe_dir_object *pdo)
  853. {
  854. if (!list_empty(&pdo->pdo_head)) {
  855. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  856. mutex_lock(&sn->pipefs_sb_lock);
  857. rpc_remove_pipe_dir_object_locked(net, pdh, pdo);
  858. mutex_unlock(&sn->pipefs_sb_lock);
  859. }
  860. }
  861. EXPORT_SYMBOL_GPL(rpc_remove_pipe_dir_object);
  862. /**
  863. * rpc_find_or_alloc_pipe_dir_object
  864. * @net: pointer to struct net
  865. * @pdh: pointer to struct rpc_pipe_dir_head
  866. * @match: match struct rpc_pipe_dir_object to data
  867. * @alloc: allocate a new struct rpc_pipe_dir_object
  868. * @data: user defined data for match() and alloc()
  869. *
  870. */
  871. struct rpc_pipe_dir_object *
  872. rpc_find_or_alloc_pipe_dir_object(struct net *net,
  873. struct rpc_pipe_dir_head *pdh,
  874. int (*match)(struct rpc_pipe_dir_object *, void *),
  875. struct rpc_pipe_dir_object *(*alloc)(void *),
  876. void *data)
  877. {
  878. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  879. struct rpc_pipe_dir_object *pdo;
  880. mutex_lock(&sn->pipefs_sb_lock);
  881. list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head) {
  882. if (!match(pdo, data))
  883. continue;
  884. goto out;
  885. }
  886. pdo = alloc(data);
  887. if (!pdo)
  888. goto out;
  889. rpc_add_pipe_dir_object_locked(net, pdh, pdo);
  890. out:
  891. mutex_unlock(&sn->pipefs_sb_lock);
  892. return pdo;
  893. }
  894. EXPORT_SYMBOL_GPL(rpc_find_or_alloc_pipe_dir_object);
  895. static void
  896. rpc_create_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
  897. {
  898. struct rpc_pipe_dir_object *pdo;
  899. struct dentry *dir = pdh->pdh_dentry;
  900. list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
  901. pdo->pdo_ops->create(dir, pdo);
  902. }
  903. static void
  904. rpc_destroy_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
  905. {
  906. struct rpc_pipe_dir_object *pdo;
  907. struct dentry *dir = pdh->pdh_dentry;
  908. list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
  909. pdo->pdo_ops->destroy(dir, pdo);
  910. }
  911. enum {
  912. RPCAUTH_info,
  913. RPCAUTH_EOF
  914. };
  915. static const struct rpc_filelist authfiles[] = {
  916. [RPCAUTH_info] = {
  917. .name = "info",
  918. .i_fop = &rpc_info_operations,
  919. .mode = S_IFREG | 0400,
  920. },
  921. };
  922. static int rpc_clntdir_populate(struct dentry *dentry, void *private)
  923. {
  924. return rpc_populate(dentry,
  925. authfiles, RPCAUTH_info, RPCAUTH_EOF,
  926. private);
  927. }
  928. static void rpc_clntdir_depopulate(struct dentry *dentry)
  929. {
  930. rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
  931. }
  932. /**
  933. * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
  934. * @dentry: the parent of new directory
  935. * @name: the name of new directory
  936. * @rpc_client: rpc client to associate with this directory
  937. *
  938. * This creates a directory at the given @path associated with
  939. * @rpc_clnt, which will contain a file named "info" with some basic
  940. * information about the client, together with any "pipes" that may
  941. * later be created using rpc_mkpipe().
  942. */
  943. struct dentry *rpc_create_client_dir(struct dentry *dentry,
  944. const char *name,
  945. struct rpc_clnt *rpc_client)
  946. {
  947. struct dentry *ret;
  948. ret = rpc_mkdir_populate(dentry, name, 0555, NULL,
  949. rpc_clntdir_populate, rpc_client);
  950. if (!IS_ERR(ret)) {
  951. rpc_client->cl_pipedir_objects.pdh_dentry = ret;
  952. rpc_create_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
  953. }
  954. return ret;
  955. }
  956. /**
  957. * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
  958. * @rpc_client: rpc_client for the pipe
  959. */
  960. int rpc_remove_client_dir(struct rpc_clnt *rpc_client)
  961. {
  962. struct dentry *dentry = rpc_client->cl_pipedir_objects.pdh_dentry;
  963. if (dentry == NULL)
  964. return 0;
  965. rpc_destroy_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
  966. rpc_client->cl_pipedir_objects.pdh_dentry = NULL;
  967. return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
  968. }
  969. static const struct rpc_filelist cache_pipefs_files[3] = {
  970. [0] = {
  971. .name = "channel",
  972. .i_fop = &cache_file_operations_pipefs,
  973. .mode = S_IFREG | 0600,
  974. },
  975. [1] = {
  976. .name = "content",
  977. .i_fop = &content_file_operations_pipefs,
  978. .mode = S_IFREG | 0400,
  979. },
  980. [2] = {
  981. .name = "flush",
  982. .i_fop = &cache_flush_operations_pipefs,
  983. .mode = S_IFREG | 0600,
  984. },
  985. };
  986. static int rpc_cachedir_populate(struct dentry *dentry, void *private)
  987. {
  988. return rpc_populate(dentry,
  989. cache_pipefs_files, 0, 3,
  990. private);
  991. }
  992. static void rpc_cachedir_depopulate(struct dentry *dentry)
  993. {
  994. rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
  995. }
  996. struct dentry *rpc_create_cache_dir(struct dentry *parent, const char *name,
  997. umode_t umode, struct cache_detail *cd)
  998. {
  999. return rpc_mkdir_populate(parent, name, umode, NULL,
  1000. rpc_cachedir_populate, cd);
  1001. }
  1002. void rpc_remove_cache_dir(struct dentry *dentry)
  1003. {
  1004. rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
  1005. }
  1006. /*
  1007. * populate the filesystem
  1008. */
  1009. static const struct super_operations s_ops = {
  1010. .alloc_inode = rpc_alloc_inode,
  1011. .free_inode = rpc_free_inode,
  1012. .statfs = simple_statfs,
  1013. };
  1014. #define RPCAUTH_GSSMAGIC 0x67596969
  1015. /*
  1016. * We have a single directory with 1 node in it.
  1017. */
  1018. enum {
  1019. RPCAUTH_lockd,
  1020. RPCAUTH_mount,
  1021. RPCAUTH_nfs,
  1022. RPCAUTH_portmap,
  1023. RPCAUTH_statd,
  1024. RPCAUTH_nfsd4_cb,
  1025. RPCAUTH_cache,
  1026. RPCAUTH_nfsd,
  1027. RPCAUTH_gssd,
  1028. RPCAUTH_RootEOF
  1029. };
  1030. static const struct rpc_filelist files[] = {
  1031. [RPCAUTH_lockd] = {
  1032. .name = "lockd",
  1033. .mode = S_IFDIR | 0555,
  1034. },
  1035. [RPCAUTH_mount] = {
  1036. .name = "mount",
  1037. .mode = S_IFDIR | 0555,
  1038. },
  1039. [RPCAUTH_nfs] = {
  1040. .name = "nfs",
  1041. .mode = S_IFDIR | 0555,
  1042. },
  1043. [RPCAUTH_portmap] = {
  1044. .name = "portmap",
  1045. .mode = S_IFDIR | 0555,
  1046. },
  1047. [RPCAUTH_statd] = {
  1048. .name = "statd",
  1049. .mode = S_IFDIR | 0555,
  1050. },
  1051. [RPCAUTH_nfsd4_cb] = {
  1052. .name = "nfsd4_cb",
  1053. .mode = S_IFDIR | 0555,
  1054. },
  1055. [RPCAUTH_cache] = {
  1056. .name = "cache",
  1057. .mode = S_IFDIR | 0555,
  1058. },
  1059. [RPCAUTH_nfsd] = {
  1060. .name = "nfsd",
  1061. .mode = S_IFDIR | 0555,
  1062. },
  1063. [RPCAUTH_gssd] = {
  1064. .name = "gssd",
  1065. .mode = S_IFDIR | 0555,
  1066. },
  1067. };
  1068. /*
  1069. * This call can be used only in RPC pipefs mount notification hooks.
  1070. */
  1071. struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
  1072. const unsigned char *dir_name)
  1073. {
  1074. struct qstr dir = QSTR_INIT(dir_name, strlen(dir_name));
  1075. return d_hash_and_lookup(sb->s_root, &dir);
  1076. }
  1077. EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
  1078. int rpc_pipefs_init_net(struct net *net)
  1079. {
  1080. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1081. sn->gssd_dummy = rpc_mkpipe_data(&gssd_dummy_pipe_ops, 0);
  1082. if (IS_ERR(sn->gssd_dummy))
  1083. return PTR_ERR(sn->gssd_dummy);
  1084. mutex_init(&sn->pipefs_sb_lock);
  1085. sn->pipe_version = -1;
  1086. return 0;
  1087. }
  1088. void rpc_pipefs_exit_net(struct net *net)
  1089. {
  1090. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1091. rpc_destroy_pipe_data(sn->gssd_dummy);
  1092. }
  1093. /*
  1094. * This call will be used for per network namespace operations calls.
  1095. * Note: Function will be returned with pipefs_sb_lock taken if superblock was
  1096. * found. This lock have to be released by rpc_put_sb_net() when all operations
  1097. * will be completed.
  1098. */
  1099. struct super_block *rpc_get_sb_net(const struct net *net)
  1100. {
  1101. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1102. mutex_lock(&sn->pipefs_sb_lock);
  1103. if (sn->pipefs_sb)
  1104. return sn->pipefs_sb;
  1105. mutex_unlock(&sn->pipefs_sb_lock);
  1106. return NULL;
  1107. }
  1108. EXPORT_SYMBOL_GPL(rpc_get_sb_net);
  1109. void rpc_put_sb_net(const struct net *net)
  1110. {
  1111. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1112. WARN_ON(sn->pipefs_sb == NULL);
  1113. mutex_unlock(&sn->pipefs_sb_lock);
  1114. }
  1115. EXPORT_SYMBOL_GPL(rpc_put_sb_net);
  1116. static const struct rpc_filelist gssd_dummy_clnt_dir[] = {
  1117. [0] = {
  1118. .name = "clntXX",
  1119. .mode = S_IFDIR | 0555,
  1120. },
  1121. };
  1122. static ssize_t
  1123. dummy_downcall(struct file *filp, const char __user *src, size_t len)
  1124. {
  1125. return -EINVAL;
  1126. }
  1127. static const struct rpc_pipe_ops gssd_dummy_pipe_ops = {
  1128. .upcall = rpc_pipe_generic_upcall,
  1129. .downcall = dummy_downcall,
  1130. };
  1131. /*
  1132. * Here we present a bogus "info" file to keep rpc.gssd happy. We don't expect
  1133. * that it will ever use this info to handle an upcall, but rpc.gssd expects
  1134. * that this file will be there and have a certain format.
  1135. */
  1136. static int
  1137. rpc_dummy_info_show(struct seq_file *m, void *v)
  1138. {
  1139. seq_printf(m, "RPC server: %s\n", utsname()->nodename);
  1140. seq_printf(m, "service: foo (1) version 0\n");
  1141. seq_printf(m, "address: 127.0.0.1\n");
  1142. seq_printf(m, "protocol: tcp\n");
  1143. seq_printf(m, "port: 0\n");
  1144. return 0;
  1145. }
  1146. DEFINE_SHOW_ATTRIBUTE(rpc_dummy_info);
  1147. static const struct rpc_filelist gssd_dummy_info_file[] = {
  1148. [0] = {
  1149. .name = "info",
  1150. .i_fop = &rpc_dummy_info_fops,
  1151. .mode = S_IFREG | 0400,
  1152. },
  1153. };
  1154. /**
  1155. * rpc_gssd_dummy_populate - create a dummy gssd pipe
  1156. * @root: root of the rpc_pipefs filesystem
  1157. * @pipe_data: pipe data created when netns is initialized
  1158. *
  1159. * Create a dummy set of directories and a pipe that gssd can hold open to
  1160. * indicate that it is up and running.
  1161. */
  1162. static struct dentry *
  1163. rpc_gssd_dummy_populate(struct dentry *root, struct rpc_pipe *pipe_data)
  1164. {
  1165. int ret = 0;
  1166. struct dentry *gssd_dentry;
  1167. struct dentry *clnt_dentry = NULL;
  1168. struct dentry *pipe_dentry = NULL;
  1169. struct qstr q = QSTR_INIT(files[RPCAUTH_gssd].name,
  1170. strlen(files[RPCAUTH_gssd].name));
  1171. /* We should never get this far if "gssd" doesn't exist */
  1172. gssd_dentry = d_hash_and_lookup(root, &q);
  1173. if (!gssd_dentry)
  1174. return ERR_PTR(-ENOENT);
  1175. ret = rpc_populate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1, NULL);
  1176. if (ret) {
  1177. pipe_dentry = ERR_PTR(ret);
  1178. goto out;
  1179. }
  1180. q.name = gssd_dummy_clnt_dir[0].name;
  1181. q.len = strlen(gssd_dummy_clnt_dir[0].name);
  1182. clnt_dentry = d_hash_and_lookup(gssd_dentry, &q);
  1183. if (!clnt_dentry) {
  1184. __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
  1185. pipe_dentry = ERR_PTR(-ENOENT);
  1186. goto out;
  1187. }
  1188. ret = rpc_populate(clnt_dentry, gssd_dummy_info_file, 0, 1, NULL);
  1189. if (ret) {
  1190. __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
  1191. pipe_dentry = ERR_PTR(ret);
  1192. goto out;
  1193. }
  1194. pipe_dentry = rpc_mkpipe_dentry(clnt_dentry, "gssd", NULL, pipe_data);
  1195. if (IS_ERR(pipe_dentry)) {
  1196. __rpc_depopulate(clnt_dentry, gssd_dummy_info_file, 0, 1);
  1197. __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
  1198. }
  1199. out:
  1200. dput(clnt_dentry);
  1201. dput(gssd_dentry);
  1202. return pipe_dentry;
  1203. }
  1204. static void
  1205. rpc_gssd_dummy_depopulate(struct dentry *pipe_dentry)
  1206. {
  1207. struct dentry *clnt_dir = pipe_dentry->d_parent;
  1208. struct dentry *gssd_dir = clnt_dir->d_parent;
  1209. dget(pipe_dentry);
  1210. __rpc_rmpipe(d_inode(clnt_dir), pipe_dentry);
  1211. __rpc_depopulate(clnt_dir, gssd_dummy_info_file, 0, 1);
  1212. __rpc_depopulate(gssd_dir, gssd_dummy_clnt_dir, 0, 1);
  1213. dput(pipe_dentry);
  1214. }
  1215. static int
  1216. rpc_fill_super(struct super_block *sb, struct fs_context *fc)
  1217. {
  1218. struct inode *inode;
  1219. struct dentry *root, *gssd_dentry;
  1220. struct net *net = sb->s_fs_info;
  1221. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1222. int err;
  1223. sb->s_blocksize = PAGE_SIZE;
  1224. sb->s_blocksize_bits = PAGE_SHIFT;
  1225. sb->s_magic = RPCAUTH_GSSMAGIC;
  1226. sb->s_op = &s_ops;
  1227. sb->s_d_op = &simple_dentry_operations;
  1228. sb->s_time_gran = 1;
  1229. inode = rpc_get_inode(sb, S_IFDIR | 0555);
  1230. sb->s_root = root = d_make_root(inode);
  1231. if (!root)
  1232. return -ENOMEM;
  1233. if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
  1234. return -ENOMEM;
  1235. gssd_dentry = rpc_gssd_dummy_populate(root, sn->gssd_dummy);
  1236. if (IS_ERR(gssd_dentry)) {
  1237. __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
  1238. return PTR_ERR(gssd_dentry);
  1239. }
  1240. dprintk("RPC: sending pipefs MOUNT notification for net %x%s\n",
  1241. net->ns.inum, NET_NAME(net));
  1242. mutex_lock(&sn->pipefs_sb_lock);
  1243. sn->pipefs_sb = sb;
  1244. err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
  1245. RPC_PIPEFS_MOUNT,
  1246. sb);
  1247. if (err)
  1248. goto err_depopulate;
  1249. mutex_unlock(&sn->pipefs_sb_lock);
  1250. return 0;
  1251. err_depopulate:
  1252. rpc_gssd_dummy_depopulate(gssd_dentry);
  1253. blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
  1254. RPC_PIPEFS_UMOUNT,
  1255. sb);
  1256. sn->pipefs_sb = NULL;
  1257. __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
  1258. mutex_unlock(&sn->pipefs_sb_lock);
  1259. return err;
  1260. }
  1261. bool
  1262. gssd_running(struct net *net)
  1263. {
  1264. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1265. struct rpc_pipe *pipe = sn->gssd_dummy;
  1266. return pipe->nreaders || pipe->nwriters;
  1267. }
  1268. EXPORT_SYMBOL_GPL(gssd_running);
  1269. static int rpc_fs_get_tree(struct fs_context *fc)
  1270. {
  1271. return get_tree_keyed(fc, rpc_fill_super, get_net(fc->net_ns));
  1272. }
  1273. static void rpc_fs_free_fc(struct fs_context *fc)
  1274. {
  1275. if (fc->s_fs_info)
  1276. put_net(fc->s_fs_info);
  1277. }
  1278. static const struct fs_context_operations rpc_fs_context_ops = {
  1279. .free = rpc_fs_free_fc,
  1280. .get_tree = rpc_fs_get_tree,
  1281. };
  1282. static int rpc_init_fs_context(struct fs_context *fc)
  1283. {
  1284. put_user_ns(fc->user_ns);
  1285. fc->user_ns = get_user_ns(fc->net_ns->user_ns);
  1286. fc->ops = &rpc_fs_context_ops;
  1287. return 0;
  1288. }
  1289. static void rpc_kill_sb(struct super_block *sb)
  1290. {
  1291. struct net *net = sb->s_fs_info;
  1292. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1293. mutex_lock(&sn->pipefs_sb_lock);
  1294. if (sn->pipefs_sb != sb) {
  1295. mutex_unlock(&sn->pipefs_sb_lock);
  1296. goto out;
  1297. }
  1298. sn->pipefs_sb = NULL;
  1299. dprintk("RPC: sending pipefs UMOUNT notification for net %x%s\n",
  1300. net->ns.inum, NET_NAME(net));
  1301. blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
  1302. RPC_PIPEFS_UMOUNT,
  1303. sb);
  1304. mutex_unlock(&sn->pipefs_sb_lock);
  1305. out:
  1306. kill_litter_super(sb);
  1307. put_net(net);
  1308. }
  1309. static struct file_system_type rpc_pipe_fs_type = {
  1310. .owner = THIS_MODULE,
  1311. .name = "rpc_pipefs",
  1312. .init_fs_context = rpc_init_fs_context,
  1313. .kill_sb = rpc_kill_sb,
  1314. };
  1315. MODULE_ALIAS_FS("rpc_pipefs");
  1316. MODULE_ALIAS("rpc_pipefs");
  1317. static void
  1318. init_once(void *foo)
  1319. {
  1320. struct rpc_inode *rpci = (struct rpc_inode *) foo;
  1321. inode_init_once(&rpci->vfs_inode);
  1322. rpci->private = NULL;
  1323. rpci->pipe = NULL;
  1324. init_waitqueue_head(&rpci->waitq);
  1325. }
  1326. int register_rpc_pipefs(void)
  1327. {
  1328. int err;
  1329. rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
  1330. sizeof(struct rpc_inode),
  1331. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  1332. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  1333. init_once);
  1334. if (!rpc_inode_cachep)
  1335. return -ENOMEM;
  1336. err = rpc_clients_notifier_register();
  1337. if (err)
  1338. goto err_notifier;
  1339. err = register_filesystem(&rpc_pipe_fs_type);
  1340. if (err)
  1341. goto err_register;
  1342. return 0;
  1343. err_register:
  1344. rpc_clients_notifier_unregister();
  1345. err_notifier:
  1346. kmem_cache_destroy(rpc_inode_cachep);
  1347. return err;
  1348. }
  1349. void unregister_rpc_pipefs(void)
  1350. {
  1351. rpc_clients_notifier_unregister();
  1352. unregister_filesystem(&rpc_pipe_fs_type);
  1353. kmem_cache_destroy(rpc_inode_cachep);
  1354. }