rpc_pipe.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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 <trond.myklebust@fys.uio.no>
  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 = kmem_cache_alloc(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 && atomic_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. default:
  431. break;
  432. }
  433. return inode;
  434. }
  435. static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
  436. umode_t mode,
  437. const struct file_operations *i_fop,
  438. void *private)
  439. {
  440. struct inode *inode;
  441. d_drop(dentry);
  442. inode = rpc_get_inode(dir->i_sb, mode);
  443. if (!inode)
  444. goto out_err;
  445. inode->i_ino = iunique(dir->i_sb, 100);
  446. if (i_fop)
  447. inode->i_fop = i_fop;
  448. if (private)
  449. rpc_inode_setowner(inode, private);
  450. d_add(dentry, inode);
  451. return 0;
  452. out_err:
  453. printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %pd\n",
  454. __FILE__, __func__, dentry);
  455. dput(dentry);
  456. return -ENOMEM;
  457. }
  458. static int __rpc_create(struct inode *dir, struct dentry *dentry,
  459. umode_t mode,
  460. const struct file_operations *i_fop,
  461. void *private)
  462. {
  463. int err;
  464. err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
  465. if (err)
  466. return err;
  467. fsnotify_create(dir, dentry);
  468. return 0;
  469. }
  470. static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
  471. umode_t mode,
  472. const struct file_operations *i_fop,
  473. void *private)
  474. {
  475. int err;
  476. err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
  477. if (err)
  478. return err;
  479. inc_nlink(dir);
  480. fsnotify_mkdir(dir, dentry);
  481. return 0;
  482. }
  483. static void
  484. init_pipe(struct rpc_pipe *pipe)
  485. {
  486. pipe->nreaders = 0;
  487. pipe->nwriters = 0;
  488. INIT_LIST_HEAD(&pipe->in_upcall);
  489. INIT_LIST_HEAD(&pipe->in_downcall);
  490. INIT_LIST_HEAD(&pipe->pipe);
  491. pipe->pipelen = 0;
  492. INIT_DELAYED_WORK(&pipe->queue_timeout,
  493. rpc_timeout_upcall_queue);
  494. pipe->ops = NULL;
  495. spin_lock_init(&pipe->lock);
  496. pipe->dentry = NULL;
  497. }
  498. void rpc_destroy_pipe_data(struct rpc_pipe *pipe)
  499. {
  500. kfree(pipe);
  501. }
  502. EXPORT_SYMBOL_GPL(rpc_destroy_pipe_data);
  503. struct rpc_pipe *rpc_mkpipe_data(const struct rpc_pipe_ops *ops, int flags)
  504. {
  505. struct rpc_pipe *pipe;
  506. pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
  507. if (!pipe)
  508. return ERR_PTR(-ENOMEM);
  509. init_pipe(pipe);
  510. pipe->ops = ops;
  511. pipe->flags = flags;
  512. return pipe;
  513. }
  514. EXPORT_SYMBOL_GPL(rpc_mkpipe_data);
  515. static int __rpc_mkpipe_dentry(struct inode *dir, struct dentry *dentry,
  516. umode_t mode,
  517. const struct file_operations *i_fop,
  518. void *private,
  519. struct rpc_pipe *pipe)
  520. {
  521. struct rpc_inode *rpci;
  522. int err;
  523. err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
  524. if (err)
  525. return err;
  526. rpci = RPC_I(d_inode(dentry));
  527. rpci->private = private;
  528. rpci->pipe = pipe;
  529. fsnotify_create(dir, dentry);
  530. return 0;
  531. }
  532. static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
  533. {
  534. int ret;
  535. dget(dentry);
  536. ret = simple_rmdir(dir, dentry);
  537. d_drop(dentry);
  538. if (!ret)
  539. fsnotify_rmdir(dir, dentry);
  540. dput(dentry);
  541. return ret;
  542. }
  543. static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
  544. {
  545. int ret;
  546. dget(dentry);
  547. ret = simple_unlink(dir, dentry);
  548. d_drop(dentry);
  549. if (!ret)
  550. fsnotify_unlink(dir, dentry);
  551. dput(dentry);
  552. return ret;
  553. }
  554. static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
  555. {
  556. struct inode *inode = d_inode(dentry);
  557. rpc_close_pipes(inode);
  558. return __rpc_unlink(dir, dentry);
  559. }
  560. static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
  561. const char *name)
  562. {
  563. struct qstr q = QSTR_INIT(name, strlen(name));
  564. struct dentry *dentry = d_hash_and_lookup(parent, &q);
  565. if (!dentry) {
  566. dentry = d_alloc(parent, &q);
  567. if (!dentry)
  568. return ERR_PTR(-ENOMEM);
  569. }
  570. if (d_really_is_negative(dentry))
  571. return dentry;
  572. dput(dentry);
  573. return ERR_PTR(-EEXIST);
  574. }
  575. /*
  576. * FIXME: This probably has races.
  577. */
  578. static void __rpc_depopulate(struct dentry *parent,
  579. const struct rpc_filelist *files,
  580. int start, int eof)
  581. {
  582. struct inode *dir = d_inode(parent);
  583. struct dentry *dentry;
  584. struct qstr name;
  585. int i;
  586. for (i = start; i < eof; i++) {
  587. name.name = files[i].name;
  588. name.len = strlen(files[i].name);
  589. dentry = d_hash_and_lookup(parent, &name);
  590. if (dentry == NULL)
  591. continue;
  592. if (d_really_is_negative(dentry))
  593. goto next;
  594. switch (d_inode(dentry)->i_mode & S_IFMT) {
  595. default:
  596. BUG();
  597. case S_IFREG:
  598. __rpc_unlink(dir, dentry);
  599. break;
  600. case S_IFDIR:
  601. __rpc_rmdir(dir, dentry);
  602. }
  603. next:
  604. dput(dentry);
  605. }
  606. }
  607. static void rpc_depopulate(struct dentry *parent,
  608. const struct rpc_filelist *files,
  609. int start, int eof)
  610. {
  611. struct inode *dir = d_inode(parent);
  612. inode_lock_nested(dir, I_MUTEX_CHILD);
  613. __rpc_depopulate(parent, files, start, eof);
  614. inode_unlock(dir);
  615. }
  616. static int rpc_populate(struct dentry *parent,
  617. const struct rpc_filelist *files,
  618. int start, int eof,
  619. void *private)
  620. {
  621. struct inode *dir = d_inode(parent);
  622. struct dentry *dentry;
  623. int i, err;
  624. inode_lock(dir);
  625. for (i = start; i < eof; i++) {
  626. dentry = __rpc_lookup_create_exclusive(parent, files[i].name);
  627. err = PTR_ERR(dentry);
  628. if (IS_ERR(dentry))
  629. goto out_bad;
  630. switch (files[i].mode & S_IFMT) {
  631. default:
  632. BUG();
  633. case S_IFREG:
  634. err = __rpc_create(dir, dentry,
  635. files[i].mode,
  636. files[i].i_fop,
  637. private);
  638. break;
  639. case S_IFDIR:
  640. err = __rpc_mkdir(dir, dentry,
  641. files[i].mode,
  642. NULL,
  643. private);
  644. }
  645. if (err != 0)
  646. goto out_bad;
  647. }
  648. inode_unlock(dir);
  649. return 0;
  650. out_bad:
  651. __rpc_depopulate(parent, files, start, eof);
  652. inode_unlock(dir);
  653. printk(KERN_WARNING "%s: %s failed to populate directory %pd\n",
  654. __FILE__, __func__, parent);
  655. return err;
  656. }
  657. static struct dentry *rpc_mkdir_populate(struct dentry *parent,
  658. const char *name, umode_t mode, void *private,
  659. int (*populate)(struct dentry *, void *), void *args_populate)
  660. {
  661. struct dentry *dentry;
  662. struct inode *dir = d_inode(parent);
  663. int error;
  664. inode_lock_nested(dir, I_MUTEX_PARENT);
  665. dentry = __rpc_lookup_create_exclusive(parent, name);
  666. if (IS_ERR(dentry))
  667. goto out;
  668. error = __rpc_mkdir(dir, dentry, mode, NULL, private);
  669. if (error != 0)
  670. goto out_err;
  671. if (populate != NULL) {
  672. error = populate(dentry, args_populate);
  673. if (error)
  674. goto err_rmdir;
  675. }
  676. out:
  677. inode_unlock(dir);
  678. return dentry;
  679. err_rmdir:
  680. __rpc_rmdir(dir, dentry);
  681. out_err:
  682. dentry = ERR_PTR(error);
  683. goto out;
  684. }
  685. static int rpc_rmdir_depopulate(struct dentry *dentry,
  686. void (*depopulate)(struct dentry *))
  687. {
  688. struct dentry *parent;
  689. struct inode *dir;
  690. int error;
  691. parent = dget_parent(dentry);
  692. dir = d_inode(parent);
  693. inode_lock_nested(dir, I_MUTEX_PARENT);
  694. if (depopulate != NULL)
  695. depopulate(dentry);
  696. error = __rpc_rmdir(dir, dentry);
  697. inode_unlock(dir);
  698. dput(parent);
  699. return error;
  700. }
  701. /**
  702. * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
  703. * @parent: dentry of directory to create new "pipe" in
  704. * @name: name of pipe
  705. * @private: private data to associate with the pipe, for the caller's use
  706. * @pipe: &rpc_pipe containing input parameters
  707. *
  708. * Data is made available for userspace to read by calls to
  709. * rpc_queue_upcall(). The actual reads will result in calls to
  710. * @ops->upcall, which will be called with the file pointer,
  711. * message, and userspace buffer to copy to.
  712. *
  713. * Writes can come at any time, and do not necessarily have to be
  714. * responses to upcalls. They will result in calls to @msg->downcall.
  715. *
  716. * The @private argument passed here will be available to all these methods
  717. * from the file pointer, via RPC_I(file_inode(file))->private.
  718. */
  719. struct dentry *rpc_mkpipe_dentry(struct dentry *parent, const char *name,
  720. void *private, struct rpc_pipe *pipe)
  721. {
  722. struct dentry *dentry;
  723. struct inode *dir = d_inode(parent);
  724. umode_t umode = S_IFIFO | 0600;
  725. int err;
  726. if (pipe->ops->upcall == NULL)
  727. umode &= ~0444;
  728. if (pipe->ops->downcall == NULL)
  729. umode &= ~0222;
  730. inode_lock_nested(dir, I_MUTEX_PARENT);
  731. dentry = __rpc_lookup_create_exclusive(parent, name);
  732. if (IS_ERR(dentry))
  733. goto out;
  734. err = __rpc_mkpipe_dentry(dir, dentry, umode, &rpc_pipe_fops,
  735. private, pipe);
  736. if (err)
  737. goto out_err;
  738. out:
  739. inode_unlock(dir);
  740. return dentry;
  741. out_err:
  742. dentry = ERR_PTR(err);
  743. printk(KERN_WARNING "%s: %s() failed to create pipe %pd/%s (errno = %d)\n",
  744. __FILE__, __func__, parent, name,
  745. err);
  746. goto out;
  747. }
  748. EXPORT_SYMBOL_GPL(rpc_mkpipe_dentry);
  749. /**
  750. * rpc_unlink - remove a pipe
  751. * @dentry: dentry for the pipe, as returned from rpc_mkpipe
  752. *
  753. * After this call, lookups will no longer find the pipe, and any
  754. * attempts to read or write using preexisting opens of the pipe will
  755. * return -EPIPE.
  756. */
  757. int
  758. rpc_unlink(struct dentry *dentry)
  759. {
  760. struct dentry *parent;
  761. struct inode *dir;
  762. int error = 0;
  763. parent = dget_parent(dentry);
  764. dir = d_inode(parent);
  765. inode_lock_nested(dir, I_MUTEX_PARENT);
  766. error = __rpc_rmpipe(dir, dentry);
  767. inode_unlock(dir);
  768. dput(parent);
  769. return error;
  770. }
  771. EXPORT_SYMBOL_GPL(rpc_unlink);
  772. /**
  773. * rpc_init_pipe_dir_head - initialise a struct rpc_pipe_dir_head
  774. * @pdh: pointer to struct rpc_pipe_dir_head
  775. */
  776. void rpc_init_pipe_dir_head(struct rpc_pipe_dir_head *pdh)
  777. {
  778. INIT_LIST_HEAD(&pdh->pdh_entries);
  779. pdh->pdh_dentry = NULL;
  780. }
  781. EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_head);
  782. /**
  783. * rpc_init_pipe_dir_object - initialise a struct rpc_pipe_dir_object
  784. * @pdo: pointer to struct rpc_pipe_dir_object
  785. * @pdo_ops: pointer to const struct rpc_pipe_dir_object_ops
  786. * @pdo_data: pointer to caller-defined data
  787. */
  788. void rpc_init_pipe_dir_object(struct rpc_pipe_dir_object *pdo,
  789. const struct rpc_pipe_dir_object_ops *pdo_ops,
  790. void *pdo_data)
  791. {
  792. INIT_LIST_HEAD(&pdo->pdo_head);
  793. pdo->pdo_ops = pdo_ops;
  794. pdo->pdo_data = pdo_data;
  795. }
  796. EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_object);
  797. static int
  798. rpc_add_pipe_dir_object_locked(struct net *net,
  799. struct rpc_pipe_dir_head *pdh,
  800. struct rpc_pipe_dir_object *pdo)
  801. {
  802. int ret = 0;
  803. if (pdh->pdh_dentry)
  804. ret = pdo->pdo_ops->create(pdh->pdh_dentry, pdo);
  805. if (ret == 0)
  806. list_add_tail(&pdo->pdo_head, &pdh->pdh_entries);
  807. return ret;
  808. }
  809. static void
  810. rpc_remove_pipe_dir_object_locked(struct net *net,
  811. struct rpc_pipe_dir_head *pdh,
  812. struct rpc_pipe_dir_object *pdo)
  813. {
  814. if (pdh->pdh_dentry)
  815. pdo->pdo_ops->destroy(pdh->pdh_dentry, pdo);
  816. list_del_init(&pdo->pdo_head);
  817. }
  818. /**
  819. * rpc_add_pipe_dir_object - associate a rpc_pipe_dir_object to a directory
  820. * @net: pointer to struct net
  821. * @pdh: pointer to struct rpc_pipe_dir_head
  822. * @pdo: pointer to struct rpc_pipe_dir_object
  823. *
  824. */
  825. int
  826. rpc_add_pipe_dir_object(struct net *net,
  827. struct rpc_pipe_dir_head *pdh,
  828. struct rpc_pipe_dir_object *pdo)
  829. {
  830. int ret = 0;
  831. if (list_empty(&pdo->pdo_head)) {
  832. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  833. mutex_lock(&sn->pipefs_sb_lock);
  834. ret = rpc_add_pipe_dir_object_locked(net, pdh, pdo);
  835. mutex_unlock(&sn->pipefs_sb_lock);
  836. }
  837. return ret;
  838. }
  839. EXPORT_SYMBOL_GPL(rpc_add_pipe_dir_object);
  840. /**
  841. * rpc_remove_pipe_dir_object - remove a rpc_pipe_dir_object from a directory
  842. * @net: pointer to struct net
  843. * @pdh: pointer to struct rpc_pipe_dir_head
  844. * @pdo: pointer to struct rpc_pipe_dir_object
  845. *
  846. */
  847. void
  848. rpc_remove_pipe_dir_object(struct net *net,
  849. struct rpc_pipe_dir_head *pdh,
  850. struct rpc_pipe_dir_object *pdo)
  851. {
  852. if (!list_empty(&pdo->pdo_head)) {
  853. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  854. mutex_lock(&sn->pipefs_sb_lock);
  855. rpc_remove_pipe_dir_object_locked(net, pdh, pdo);
  856. mutex_unlock(&sn->pipefs_sb_lock);
  857. }
  858. }
  859. EXPORT_SYMBOL_GPL(rpc_remove_pipe_dir_object);
  860. /**
  861. * rpc_find_or_alloc_pipe_dir_object
  862. * @net: pointer to struct net
  863. * @pdh: pointer to struct rpc_pipe_dir_head
  864. * @match: match struct rpc_pipe_dir_object to data
  865. * @alloc: allocate a new struct rpc_pipe_dir_object
  866. * @data: user defined data for match() and alloc()
  867. *
  868. */
  869. struct rpc_pipe_dir_object *
  870. rpc_find_or_alloc_pipe_dir_object(struct net *net,
  871. struct rpc_pipe_dir_head *pdh,
  872. int (*match)(struct rpc_pipe_dir_object *, void *),
  873. struct rpc_pipe_dir_object *(*alloc)(void *),
  874. void *data)
  875. {
  876. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  877. struct rpc_pipe_dir_object *pdo;
  878. mutex_lock(&sn->pipefs_sb_lock);
  879. list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head) {
  880. if (!match(pdo, data))
  881. continue;
  882. goto out;
  883. }
  884. pdo = alloc(data);
  885. if (!pdo)
  886. goto out;
  887. rpc_add_pipe_dir_object_locked(net, pdh, pdo);
  888. out:
  889. mutex_unlock(&sn->pipefs_sb_lock);
  890. return pdo;
  891. }
  892. EXPORT_SYMBOL_GPL(rpc_find_or_alloc_pipe_dir_object);
  893. static void
  894. rpc_create_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
  895. {
  896. struct rpc_pipe_dir_object *pdo;
  897. struct dentry *dir = pdh->pdh_dentry;
  898. list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
  899. pdo->pdo_ops->create(dir, pdo);
  900. }
  901. static void
  902. rpc_destroy_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
  903. {
  904. struct rpc_pipe_dir_object *pdo;
  905. struct dentry *dir = pdh->pdh_dentry;
  906. list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
  907. pdo->pdo_ops->destroy(dir, pdo);
  908. }
  909. enum {
  910. RPCAUTH_info,
  911. RPCAUTH_EOF
  912. };
  913. static const struct rpc_filelist authfiles[] = {
  914. [RPCAUTH_info] = {
  915. .name = "info",
  916. .i_fop = &rpc_info_operations,
  917. .mode = S_IFREG | 0400,
  918. },
  919. };
  920. static int rpc_clntdir_populate(struct dentry *dentry, void *private)
  921. {
  922. return rpc_populate(dentry,
  923. authfiles, RPCAUTH_info, RPCAUTH_EOF,
  924. private);
  925. }
  926. static void rpc_clntdir_depopulate(struct dentry *dentry)
  927. {
  928. rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
  929. }
  930. /**
  931. * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
  932. * @dentry: the parent of new directory
  933. * @name: the name of new directory
  934. * @rpc_client: rpc client to associate with this directory
  935. *
  936. * This creates a directory at the given @path associated with
  937. * @rpc_clnt, which will contain a file named "info" with some basic
  938. * information about the client, together with any "pipes" that may
  939. * later be created using rpc_mkpipe().
  940. */
  941. struct dentry *rpc_create_client_dir(struct dentry *dentry,
  942. const char *name,
  943. struct rpc_clnt *rpc_client)
  944. {
  945. struct dentry *ret;
  946. ret = rpc_mkdir_populate(dentry, name, 0555, NULL,
  947. rpc_clntdir_populate, rpc_client);
  948. if (!IS_ERR(ret)) {
  949. rpc_client->cl_pipedir_objects.pdh_dentry = ret;
  950. rpc_create_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
  951. }
  952. return ret;
  953. }
  954. /**
  955. * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
  956. * @rpc_client: rpc_client for the pipe
  957. */
  958. int rpc_remove_client_dir(struct rpc_clnt *rpc_client)
  959. {
  960. struct dentry *dentry = rpc_client->cl_pipedir_objects.pdh_dentry;
  961. if (dentry == NULL)
  962. return 0;
  963. rpc_destroy_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
  964. rpc_client->cl_pipedir_objects.pdh_dentry = NULL;
  965. return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
  966. }
  967. static const struct rpc_filelist cache_pipefs_files[3] = {
  968. [0] = {
  969. .name = "channel",
  970. .i_fop = &cache_file_operations_pipefs,
  971. .mode = S_IFREG | 0600,
  972. },
  973. [1] = {
  974. .name = "content",
  975. .i_fop = &content_file_operations_pipefs,
  976. .mode = S_IFREG | 0400,
  977. },
  978. [2] = {
  979. .name = "flush",
  980. .i_fop = &cache_flush_operations_pipefs,
  981. .mode = S_IFREG | 0600,
  982. },
  983. };
  984. static int rpc_cachedir_populate(struct dentry *dentry, void *private)
  985. {
  986. return rpc_populate(dentry,
  987. cache_pipefs_files, 0, 3,
  988. private);
  989. }
  990. static void rpc_cachedir_depopulate(struct dentry *dentry)
  991. {
  992. rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
  993. }
  994. struct dentry *rpc_create_cache_dir(struct dentry *parent, const char *name,
  995. umode_t umode, struct cache_detail *cd)
  996. {
  997. return rpc_mkdir_populate(parent, name, umode, NULL,
  998. rpc_cachedir_populate, cd);
  999. }
  1000. void rpc_remove_cache_dir(struct dentry *dentry)
  1001. {
  1002. rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
  1003. }
  1004. /*
  1005. * populate the filesystem
  1006. */
  1007. static const struct super_operations s_ops = {
  1008. .alloc_inode = rpc_alloc_inode,
  1009. .free_inode = rpc_free_inode,
  1010. .statfs = simple_statfs,
  1011. };
  1012. #define RPCAUTH_GSSMAGIC 0x67596969
  1013. /*
  1014. * We have a single directory with 1 node in it.
  1015. */
  1016. enum {
  1017. RPCAUTH_lockd,
  1018. RPCAUTH_mount,
  1019. RPCAUTH_nfs,
  1020. RPCAUTH_portmap,
  1021. RPCAUTH_statd,
  1022. RPCAUTH_nfsd4_cb,
  1023. RPCAUTH_cache,
  1024. RPCAUTH_nfsd,
  1025. RPCAUTH_gssd,
  1026. RPCAUTH_RootEOF
  1027. };
  1028. static const struct rpc_filelist files[] = {
  1029. [RPCAUTH_lockd] = {
  1030. .name = "lockd",
  1031. .mode = S_IFDIR | 0555,
  1032. },
  1033. [RPCAUTH_mount] = {
  1034. .name = "mount",
  1035. .mode = S_IFDIR | 0555,
  1036. },
  1037. [RPCAUTH_nfs] = {
  1038. .name = "nfs",
  1039. .mode = S_IFDIR | 0555,
  1040. },
  1041. [RPCAUTH_portmap] = {
  1042. .name = "portmap",
  1043. .mode = S_IFDIR | 0555,
  1044. },
  1045. [RPCAUTH_statd] = {
  1046. .name = "statd",
  1047. .mode = S_IFDIR | 0555,
  1048. },
  1049. [RPCAUTH_nfsd4_cb] = {
  1050. .name = "nfsd4_cb",
  1051. .mode = S_IFDIR | 0555,
  1052. },
  1053. [RPCAUTH_cache] = {
  1054. .name = "cache",
  1055. .mode = S_IFDIR | 0555,
  1056. },
  1057. [RPCAUTH_nfsd] = {
  1058. .name = "nfsd",
  1059. .mode = S_IFDIR | 0555,
  1060. },
  1061. [RPCAUTH_gssd] = {
  1062. .name = "gssd",
  1063. .mode = S_IFDIR | 0555,
  1064. },
  1065. };
  1066. /*
  1067. * This call can be used only in RPC pipefs mount notification hooks.
  1068. */
  1069. struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
  1070. const unsigned char *dir_name)
  1071. {
  1072. struct qstr dir = QSTR_INIT(dir_name, strlen(dir_name));
  1073. return d_hash_and_lookup(sb->s_root, &dir);
  1074. }
  1075. EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
  1076. int rpc_pipefs_init_net(struct net *net)
  1077. {
  1078. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1079. sn->gssd_dummy = rpc_mkpipe_data(&gssd_dummy_pipe_ops, 0);
  1080. if (IS_ERR(sn->gssd_dummy))
  1081. return PTR_ERR(sn->gssd_dummy);
  1082. mutex_init(&sn->pipefs_sb_lock);
  1083. sn->pipe_version = -1;
  1084. return 0;
  1085. }
  1086. void rpc_pipefs_exit_net(struct net *net)
  1087. {
  1088. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1089. rpc_destroy_pipe_data(sn->gssd_dummy);
  1090. }
  1091. /*
  1092. * This call will be used for per network namespace operations calls.
  1093. * Note: Function will be returned with pipefs_sb_lock taken if superblock was
  1094. * found. This lock have to be released by rpc_put_sb_net() when all operations
  1095. * will be completed.
  1096. */
  1097. struct super_block *rpc_get_sb_net(const struct net *net)
  1098. {
  1099. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1100. mutex_lock(&sn->pipefs_sb_lock);
  1101. if (sn->pipefs_sb)
  1102. return sn->pipefs_sb;
  1103. mutex_unlock(&sn->pipefs_sb_lock);
  1104. return NULL;
  1105. }
  1106. EXPORT_SYMBOL_GPL(rpc_get_sb_net);
  1107. void rpc_put_sb_net(const struct net *net)
  1108. {
  1109. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1110. WARN_ON(sn->pipefs_sb == NULL);
  1111. mutex_unlock(&sn->pipefs_sb_lock);
  1112. }
  1113. EXPORT_SYMBOL_GPL(rpc_put_sb_net);
  1114. static const struct rpc_filelist gssd_dummy_clnt_dir[] = {
  1115. [0] = {
  1116. .name = "clntXX",
  1117. .mode = S_IFDIR | 0555,
  1118. },
  1119. };
  1120. static ssize_t
  1121. dummy_downcall(struct file *filp, const char __user *src, size_t len)
  1122. {
  1123. return -EINVAL;
  1124. }
  1125. static const struct rpc_pipe_ops gssd_dummy_pipe_ops = {
  1126. .upcall = rpc_pipe_generic_upcall,
  1127. .downcall = dummy_downcall,
  1128. };
  1129. /*
  1130. * Here we present a bogus "info" file to keep rpc.gssd happy. We don't expect
  1131. * that it will ever use this info to handle an upcall, but rpc.gssd expects
  1132. * that this file will be there and have a certain format.
  1133. */
  1134. static int
  1135. rpc_dummy_info_show(struct seq_file *m, void *v)
  1136. {
  1137. seq_printf(m, "RPC server: %s\n", utsname()->nodename);
  1138. seq_printf(m, "service: foo (1) version 0\n");
  1139. seq_printf(m, "address: 127.0.0.1\n");
  1140. seq_printf(m, "protocol: tcp\n");
  1141. seq_printf(m, "port: 0\n");
  1142. return 0;
  1143. }
  1144. DEFINE_SHOW_ATTRIBUTE(rpc_dummy_info);
  1145. static const struct rpc_filelist gssd_dummy_info_file[] = {
  1146. [0] = {
  1147. .name = "info",
  1148. .i_fop = &rpc_dummy_info_fops,
  1149. .mode = S_IFREG | 0400,
  1150. },
  1151. };
  1152. /**
  1153. * rpc_gssd_dummy_populate - create a dummy gssd pipe
  1154. * @root: root of the rpc_pipefs filesystem
  1155. * @pipe_data: pipe data created when netns is initialized
  1156. *
  1157. * Create a dummy set of directories and a pipe that gssd can hold open to
  1158. * indicate that it is up and running.
  1159. */
  1160. static struct dentry *
  1161. rpc_gssd_dummy_populate(struct dentry *root, struct rpc_pipe *pipe_data)
  1162. {
  1163. int ret = 0;
  1164. struct dentry *gssd_dentry;
  1165. struct dentry *clnt_dentry = NULL;
  1166. struct dentry *pipe_dentry = NULL;
  1167. struct qstr q = QSTR_INIT(files[RPCAUTH_gssd].name,
  1168. strlen(files[RPCAUTH_gssd].name));
  1169. /* We should never get this far if "gssd" doesn't exist */
  1170. gssd_dentry = d_hash_and_lookup(root, &q);
  1171. if (!gssd_dentry)
  1172. return ERR_PTR(-ENOENT);
  1173. ret = rpc_populate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1, NULL);
  1174. if (ret) {
  1175. pipe_dentry = ERR_PTR(ret);
  1176. goto out;
  1177. }
  1178. q.name = gssd_dummy_clnt_dir[0].name;
  1179. q.len = strlen(gssd_dummy_clnt_dir[0].name);
  1180. clnt_dentry = d_hash_and_lookup(gssd_dentry, &q);
  1181. if (!clnt_dentry) {
  1182. __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
  1183. pipe_dentry = ERR_PTR(-ENOENT);
  1184. goto out;
  1185. }
  1186. ret = rpc_populate(clnt_dentry, gssd_dummy_info_file, 0, 1, NULL);
  1187. if (ret) {
  1188. __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
  1189. pipe_dentry = ERR_PTR(ret);
  1190. goto out;
  1191. }
  1192. pipe_dentry = rpc_mkpipe_dentry(clnt_dentry, "gssd", NULL, pipe_data);
  1193. if (IS_ERR(pipe_dentry)) {
  1194. __rpc_depopulate(clnt_dentry, gssd_dummy_info_file, 0, 1);
  1195. __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
  1196. }
  1197. out:
  1198. dput(clnt_dentry);
  1199. dput(gssd_dentry);
  1200. return pipe_dentry;
  1201. }
  1202. static void
  1203. rpc_gssd_dummy_depopulate(struct dentry *pipe_dentry)
  1204. {
  1205. struct dentry *clnt_dir = pipe_dentry->d_parent;
  1206. struct dentry *gssd_dir = clnt_dir->d_parent;
  1207. dget(pipe_dentry);
  1208. __rpc_rmpipe(d_inode(clnt_dir), pipe_dentry);
  1209. __rpc_depopulate(clnt_dir, gssd_dummy_info_file, 0, 1);
  1210. __rpc_depopulate(gssd_dir, gssd_dummy_clnt_dir, 0, 1);
  1211. dput(pipe_dentry);
  1212. }
  1213. static int
  1214. rpc_fill_super(struct super_block *sb, struct fs_context *fc)
  1215. {
  1216. struct inode *inode;
  1217. struct dentry *root, *gssd_dentry;
  1218. struct net *net = sb->s_fs_info;
  1219. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1220. int err;
  1221. sb->s_blocksize = PAGE_SIZE;
  1222. sb->s_blocksize_bits = PAGE_SHIFT;
  1223. sb->s_magic = RPCAUTH_GSSMAGIC;
  1224. sb->s_op = &s_ops;
  1225. sb->s_d_op = &simple_dentry_operations;
  1226. sb->s_time_gran = 1;
  1227. inode = rpc_get_inode(sb, S_IFDIR | 0555);
  1228. sb->s_root = root = d_make_root(inode);
  1229. if (!root)
  1230. return -ENOMEM;
  1231. if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
  1232. return -ENOMEM;
  1233. gssd_dentry = rpc_gssd_dummy_populate(root, sn->gssd_dummy);
  1234. if (IS_ERR(gssd_dentry)) {
  1235. __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
  1236. return PTR_ERR(gssd_dentry);
  1237. }
  1238. dprintk("RPC: sending pipefs MOUNT notification for net %x%s\n",
  1239. net->ns.inum, NET_NAME(net));
  1240. mutex_lock(&sn->pipefs_sb_lock);
  1241. sn->pipefs_sb = sb;
  1242. err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
  1243. RPC_PIPEFS_MOUNT,
  1244. sb);
  1245. if (err)
  1246. goto err_depopulate;
  1247. mutex_unlock(&sn->pipefs_sb_lock);
  1248. return 0;
  1249. err_depopulate:
  1250. rpc_gssd_dummy_depopulate(gssd_dentry);
  1251. blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
  1252. RPC_PIPEFS_UMOUNT,
  1253. sb);
  1254. sn->pipefs_sb = NULL;
  1255. __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
  1256. mutex_unlock(&sn->pipefs_sb_lock);
  1257. return err;
  1258. }
  1259. bool
  1260. gssd_running(struct net *net)
  1261. {
  1262. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1263. struct rpc_pipe *pipe = sn->gssd_dummy;
  1264. return pipe->nreaders || pipe->nwriters;
  1265. }
  1266. EXPORT_SYMBOL_GPL(gssd_running);
  1267. static int rpc_fs_get_tree(struct fs_context *fc)
  1268. {
  1269. return get_tree_keyed(fc, rpc_fill_super, get_net(fc->net_ns));
  1270. }
  1271. static void rpc_fs_free_fc(struct fs_context *fc)
  1272. {
  1273. if (fc->s_fs_info)
  1274. put_net(fc->s_fs_info);
  1275. }
  1276. static const struct fs_context_operations rpc_fs_context_ops = {
  1277. .free = rpc_fs_free_fc,
  1278. .get_tree = rpc_fs_get_tree,
  1279. };
  1280. static int rpc_init_fs_context(struct fs_context *fc)
  1281. {
  1282. put_user_ns(fc->user_ns);
  1283. fc->user_ns = get_user_ns(fc->net_ns->user_ns);
  1284. fc->ops = &rpc_fs_context_ops;
  1285. return 0;
  1286. }
  1287. static void rpc_kill_sb(struct super_block *sb)
  1288. {
  1289. struct net *net = sb->s_fs_info;
  1290. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1291. mutex_lock(&sn->pipefs_sb_lock);
  1292. if (sn->pipefs_sb != sb) {
  1293. mutex_unlock(&sn->pipefs_sb_lock);
  1294. goto out;
  1295. }
  1296. sn->pipefs_sb = NULL;
  1297. dprintk("RPC: sending pipefs UMOUNT notification for net %x%s\n",
  1298. net->ns.inum, NET_NAME(net));
  1299. blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
  1300. RPC_PIPEFS_UMOUNT,
  1301. sb);
  1302. mutex_unlock(&sn->pipefs_sb_lock);
  1303. out:
  1304. kill_litter_super(sb);
  1305. put_net(net);
  1306. }
  1307. static struct file_system_type rpc_pipe_fs_type = {
  1308. .owner = THIS_MODULE,
  1309. .name = "rpc_pipefs",
  1310. .init_fs_context = rpc_init_fs_context,
  1311. .kill_sb = rpc_kill_sb,
  1312. };
  1313. MODULE_ALIAS_FS("rpc_pipefs");
  1314. MODULE_ALIAS("rpc_pipefs");
  1315. static void
  1316. init_once(void *foo)
  1317. {
  1318. struct rpc_inode *rpci = (struct rpc_inode *) foo;
  1319. inode_init_once(&rpci->vfs_inode);
  1320. rpci->private = NULL;
  1321. rpci->pipe = NULL;
  1322. init_waitqueue_head(&rpci->waitq);
  1323. }
  1324. int register_rpc_pipefs(void)
  1325. {
  1326. int err;
  1327. rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
  1328. sizeof(struct rpc_inode),
  1329. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  1330. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  1331. init_once);
  1332. if (!rpc_inode_cachep)
  1333. return -ENOMEM;
  1334. err = rpc_clients_notifier_register();
  1335. if (err)
  1336. goto err_notifier;
  1337. err = register_filesystem(&rpc_pipe_fs_type);
  1338. if (err)
  1339. goto err_register;
  1340. return 0;
  1341. err_register:
  1342. rpc_clients_notifier_unregister();
  1343. err_notifier:
  1344. kmem_cache_destroy(rpc_inode_cachep);
  1345. return err;
  1346. }
  1347. void unregister_rpc_pipefs(void)
  1348. {
  1349. rpc_clients_notifier_unregister();
  1350. unregister_filesystem(&rpc_pipe_fs_type);
  1351. kmem_cache_destroy(rpc_inode_cachep);
  1352. }