rpc_pipe.c 36 KB

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