rpc_pipe.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  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. mutex_lock(&inode->i_mutex);
  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. mutex_unlock(&inode->i_mutex);
  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. mutex_lock(&inode->i_mutex);
  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. mutex_unlock(&inode->i_mutex);
  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. mutex_lock(&inode->i_mutex);
  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. mutex_unlock(&inode->i_mutex);
  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. mutex_lock(&inode->i_mutex);
  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. mutex_unlock(&inode->i_mutex);
  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. mutex_lock(&inode->i_mutex);
  301. res = -EPIPE;
  302. if (RPC_I(inode)->pipe != NULL)
  303. res = RPC_I(inode)->pipe->ops->downcall(filp, buf, len);
  304. mutex_unlock(&inode->i_mutex);
  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. mutex_lock(&inode->i_mutex);
  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. mutex_unlock(&inode->i_mutex);
  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. mutex_lock(&inode->i_mutex);
  331. pipe = RPC_I(inode)->pipe;
  332. if (pipe == NULL) {
  333. mutex_unlock(&inode->i_mutex);
  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. mutex_unlock(&inode->i_mutex);
  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;
  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. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  553. error = __rpc_rmdir(dir, dentry);
  554. mutex_unlock(&dir->i_mutex);
  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. mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
  627. __rpc_depopulate(parent, files, start, eof);
  628. mutex_unlock(&dir->i_mutex);
  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. mutex_lock(&dir->i_mutex);
  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. mutex_unlock(&dir->i_mutex);
  663. return 0;
  664. out_bad:
  665. __rpc_depopulate(parent, files, start, eof);
  666. mutex_unlock(&dir->i_mutex);
  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. mutex_lock_nested(&dir->i_mutex, 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. mutex_unlock(&dir->i_mutex);
  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. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  708. if (depopulate != NULL)
  709. depopulate(dentry);
  710. error = __rpc_rmdir(dir, dentry);
  711. mutex_unlock(&dir->i_mutex);
  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. mutex_lock_nested(&dir->i_mutex, 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. mutex_unlock(&dir->i_mutex);
  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. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  780. error = __rpc_rmpipe(dir, dentry);
  781. mutex_unlock(&dir->i_mutex);
  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. pipe_dentry = ERR_PTR(-ENOENT);
  1208. goto out;
  1209. }
  1210. ret = rpc_populate(clnt_dentry, gssd_dummy_info_file, 0, 1, NULL);
  1211. if (ret) {
  1212. __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
  1213. pipe_dentry = ERR_PTR(ret);
  1214. goto out;
  1215. }
  1216. pipe_dentry = rpc_mkpipe_dentry(clnt_dentry, "gssd", NULL, pipe_data);
  1217. if (IS_ERR(pipe_dentry)) {
  1218. __rpc_depopulate(clnt_dentry, gssd_dummy_info_file, 0, 1);
  1219. __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
  1220. }
  1221. out:
  1222. dput(clnt_dentry);
  1223. dput(gssd_dentry);
  1224. return pipe_dentry;
  1225. }
  1226. static void
  1227. rpc_gssd_dummy_depopulate(struct dentry *pipe_dentry)
  1228. {
  1229. struct dentry *clnt_dir = pipe_dentry->d_parent;
  1230. struct dentry *gssd_dir = clnt_dir->d_parent;
  1231. __rpc_rmpipe(d_inode(clnt_dir), pipe_dentry);
  1232. __rpc_depopulate(clnt_dir, gssd_dummy_info_file, 0, 1);
  1233. __rpc_depopulate(gssd_dir, gssd_dummy_clnt_dir, 0, 1);
  1234. dput(pipe_dentry);
  1235. }
  1236. static int
  1237. rpc_fill_super(struct super_block *sb, void *data, int silent)
  1238. {
  1239. struct inode *inode;
  1240. struct dentry *root, *gssd_dentry;
  1241. struct net *net = data;
  1242. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1243. int err;
  1244. sb->s_blocksize = PAGE_CACHE_SIZE;
  1245. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  1246. sb->s_magic = RPCAUTH_GSSMAGIC;
  1247. sb->s_op = &s_ops;
  1248. sb->s_d_op = &simple_dentry_operations;
  1249. sb->s_time_gran = 1;
  1250. inode = rpc_get_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO);
  1251. sb->s_root = root = d_make_root(inode);
  1252. if (!root)
  1253. return -ENOMEM;
  1254. if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
  1255. return -ENOMEM;
  1256. gssd_dentry = rpc_gssd_dummy_populate(root, sn->gssd_dummy);
  1257. if (IS_ERR(gssd_dentry)) {
  1258. __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
  1259. return PTR_ERR(gssd_dentry);
  1260. }
  1261. dprintk("RPC: sending pipefs MOUNT notification for net %p%s\n",
  1262. net, NET_NAME(net));
  1263. mutex_lock(&sn->pipefs_sb_lock);
  1264. sn->pipefs_sb = sb;
  1265. err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
  1266. RPC_PIPEFS_MOUNT,
  1267. sb);
  1268. if (err)
  1269. goto err_depopulate;
  1270. sb->s_fs_info = get_net(net);
  1271. mutex_unlock(&sn->pipefs_sb_lock);
  1272. return 0;
  1273. err_depopulate:
  1274. rpc_gssd_dummy_depopulate(gssd_dentry);
  1275. blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
  1276. RPC_PIPEFS_UMOUNT,
  1277. sb);
  1278. sn->pipefs_sb = NULL;
  1279. __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
  1280. mutex_unlock(&sn->pipefs_sb_lock);
  1281. return err;
  1282. }
  1283. bool
  1284. gssd_running(struct net *net)
  1285. {
  1286. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1287. struct rpc_pipe *pipe = sn->gssd_dummy;
  1288. return pipe->nreaders || pipe->nwriters;
  1289. }
  1290. EXPORT_SYMBOL_GPL(gssd_running);
  1291. static struct dentry *
  1292. rpc_mount(struct file_system_type *fs_type,
  1293. int flags, const char *dev_name, void *data)
  1294. {
  1295. return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
  1296. }
  1297. static void rpc_kill_sb(struct super_block *sb)
  1298. {
  1299. struct net *net = sb->s_fs_info;
  1300. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1301. mutex_lock(&sn->pipefs_sb_lock);
  1302. if (sn->pipefs_sb != sb) {
  1303. mutex_unlock(&sn->pipefs_sb_lock);
  1304. goto out;
  1305. }
  1306. sn->pipefs_sb = NULL;
  1307. dprintk("RPC: sending pipefs UMOUNT notification for net %p%s\n",
  1308. net, NET_NAME(net));
  1309. blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
  1310. RPC_PIPEFS_UMOUNT,
  1311. sb);
  1312. mutex_unlock(&sn->pipefs_sb_lock);
  1313. put_net(net);
  1314. out:
  1315. kill_litter_super(sb);
  1316. }
  1317. static struct file_system_type rpc_pipe_fs_type = {
  1318. .owner = THIS_MODULE,
  1319. .name = "rpc_pipefs",
  1320. .mount = rpc_mount,
  1321. .kill_sb = rpc_kill_sb,
  1322. };
  1323. MODULE_ALIAS_FS("rpc_pipefs");
  1324. MODULE_ALIAS("rpc_pipefs");
  1325. static void
  1326. init_once(void *foo)
  1327. {
  1328. struct rpc_inode *rpci = (struct rpc_inode *) foo;
  1329. inode_init_once(&rpci->vfs_inode);
  1330. rpci->private = NULL;
  1331. rpci->pipe = NULL;
  1332. init_waitqueue_head(&rpci->waitq);
  1333. }
  1334. int register_rpc_pipefs(void)
  1335. {
  1336. int err;
  1337. rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
  1338. sizeof(struct rpc_inode),
  1339. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  1340. SLAB_MEM_SPREAD),
  1341. init_once);
  1342. if (!rpc_inode_cachep)
  1343. return -ENOMEM;
  1344. err = rpc_clients_notifier_register();
  1345. if (err)
  1346. goto err_notifier;
  1347. err = register_filesystem(&rpc_pipe_fs_type);
  1348. if (err)
  1349. goto err_register;
  1350. return 0;
  1351. err_register:
  1352. rpc_clients_notifier_unregister();
  1353. err_notifier:
  1354. kmem_cache_destroy(rpc_inode_cachep);
  1355. return err;
  1356. }
  1357. void unregister_rpc_pipefs(void)
  1358. {
  1359. rpc_clients_notifier_unregister();
  1360. kmem_cache_destroy(rpc_inode_cachep);
  1361. unregister_filesystem(&rpc_pipe_fs_type);
  1362. }