rpc_pipefs.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright (c) 2006,2007 The Regents of the University of Michigan.
  3. * All rights reserved.
  4. *
  5. * Andy Adamson <andros@citi.umich.edu>
  6. * Fred Isaman <iisaman@umich.edu>
  7. *
  8. * permission is granted to use, copy, create derivative works and
  9. * redistribute this software and such derivative works for any purpose,
  10. * so long as the name of the university of michigan is not used in
  11. * any advertising or publicity pertaining to the use or distribution
  12. * of this software without specific, written prior authorization. if
  13. * the above copyright notice or any other identification of the
  14. * university of michigan is included in any copy of any portion of
  15. * this software, then the disclaimer below must also be included.
  16. *
  17. * this software is provided as is, without representation from the
  18. * university of michigan as to its fitness for any purpose, and without
  19. * warranty by the university of michigan of any kind, either express
  20. * or implied, including without limitation the implied warranties of
  21. * merchantability and fitness for a particular purpose. the regents
  22. * of the university of michigan shall not be liable for any damages,
  23. * including special, indirect, incidental, or consequential damages,
  24. * with respect to any claim arising out or in connection with the use
  25. * of the software, even if it has been or is hereafter advised of the
  26. * possibility of such damages.
  27. */
  28. #include <linux/module.h>
  29. #include <linux/genhd.h>
  30. #include <linux/blkdev.h>
  31. #include "blocklayout.h"
  32. #define NFSDBG_FACILITY NFSDBG_PNFS_LD
  33. static void
  34. nfs4_encode_simple(__be32 *p, struct pnfs_block_volume *b)
  35. {
  36. int i;
  37. *p++ = cpu_to_be32(1);
  38. *p++ = cpu_to_be32(b->type);
  39. *p++ = cpu_to_be32(b->simple.nr_sigs);
  40. for (i = 0; i < b->simple.nr_sigs; i++) {
  41. p = xdr_encode_hyper(p, b->simple.sigs[i].offset);
  42. p = xdr_encode_opaque(p, b->simple.sigs[i].sig,
  43. b->simple.sigs[i].sig_len);
  44. }
  45. }
  46. dev_t
  47. bl_resolve_deviceid(struct nfs_server *server, struct pnfs_block_volume *b,
  48. gfp_t gfp_mask)
  49. {
  50. struct net *net = server->nfs_client->cl_net;
  51. struct nfs_net *nn = net_generic(net, nfs_net_id);
  52. struct bl_dev_msg *reply = &nn->bl_mount_reply;
  53. struct bl_pipe_msg bl_pipe_msg;
  54. struct rpc_pipe_msg *msg = &bl_pipe_msg.msg;
  55. struct bl_msg_hdr *bl_msg;
  56. DECLARE_WAITQUEUE(wq, current);
  57. dev_t dev = 0;
  58. int rc;
  59. dprintk("%s CREATING PIPEFS MESSAGE\n", __func__);
  60. bl_pipe_msg.bl_wq = &nn->bl_wq;
  61. b->simple.len += 4; /* single volume */
  62. if (b->simple.len > PAGE_SIZE)
  63. return -EIO;
  64. memset(msg, 0, sizeof(*msg));
  65. msg->len = sizeof(*bl_msg) + b->simple.len;
  66. msg->data = kzalloc(msg->len, gfp_mask);
  67. if (!msg->data)
  68. goto out;
  69. bl_msg = msg->data;
  70. bl_msg->type = BL_DEVICE_MOUNT,
  71. bl_msg->totallen = b->simple.len;
  72. nfs4_encode_simple(msg->data + sizeof(*bl_msg), b);
  73. dprintk("%s CALLING USERSPACE DAEMON\n", __func__);
  74. add_wait_queue(&nn->bl_wq, &wq);
  75. rc = rpc_queue_upcall(nn->bl_device_pipe, msg);
  76. if (rc < 0) {
  77. remove_wait_queue(&nn->bl_wq, &wq);
  78. goto out;
  79. }
  80. set_current_state(TASK_UNINTERRUPTIBLE);
  81. schedule();
  82. remove_wait_queue(&nn->bl_wq, &wq);
  83. if (reply->status != BL_DEVICE_REQUEST_PROC) {
  84. printk(KERN_WARNING "%s failed to decode device: %d\n",
  85. __func__, reply->status);
  86. goto out;
  87. }
  88. dev = MKDEV(reply->major, reply->minor);
  89. out:
  90. kfree(msg->data);
  91. return dev;
  92. }
  93. static ssize_t bl_pipe_downcall(struct file *filp, const char __user *src,
  94. size_t mlen)
  95. {
  96. struct nfs_net *nn = net_generic(filp->f_dentry->d_sb->s_fs_info,
  97. nfs_net_id);
  98. if (mlen != sizeof (struct bl_dev_msg))
  99. return -EINVAL;
  100. if (copy_from_user(&nn->bl_mount_reply, src, mlen) != 0)
  101. return -EFAULT;
  102. wake_up(&nn->bl_wq);
  103. return mlen;
  104. }
  105. static void bl_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  106. {
  107. struct bl_pipe_msg *bl_pipe_msg =
  108. container_of(msg, struct bl_pipe_msg, msg);
  109. if (msg->errno >= 0)
  110. return;
  111. wake_up(bl_pipe_msg->bl_wq);
  112. }
  113. static const struct rpc_pipe_ops bl_upcall_ops = {
  114. .upcall = rpc_pipe_generic_upcall,
  115. .downcall = bl_pipe_downcall,
  116. .destroy_msg = bl_pipe_destroy_msg,
  117. };
  118. static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb,
  119. struct rpc_pipe *pipe)
  120. {
  121. struct dentry *dir, *dentry;
  122. dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME);
  123. if (dir == NULL)
  124. return ERR_PTR(-ENOENT);
  125. dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe);
  126. dput(dir);
  127. return dentry;
  128. }
  129. static void nfs4blocklayout_unregister_sb(struct super_block *sb,
  130. struct rpc_pipe *pipe)
  131. {
  132. if (pipe->dentry)
  133. rpc_unlink(pipe->dentry);
  134. }
  135. static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
  136. void *ptr)
  137. {
  138. struct super_block *sb = ptr;
  139. struct net *net = sb->s_fs_info;
  140. struct nfs_net *nn = net_generic(net, nfs_net_id);
  141. struct dentry *dentry;
  142. int ret = 0;
  143. if (!try_module_get(THIS_MODULE))
  144. return 0;
  145. if (nn->bl_device_pipe == NULL) {
  146. module_put(THIS_MODULE);
  147. return 0;
  148. }
  149. switch (event) {
  150. case RPC_PIPEFS_MOUNT:
  151. dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe);
  152. if (IS_ERR(dentry)) {
  153. ret = PTR_ERR(dentry);
  154. break;
  155. }
  156. nn->bl_device_pipe->dentry = dentry;
  157. break;
  158. case RPC_PIPEFS_UMOUNT:
  159. if (nn->bl_device_pipe->dentry)
  160. nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe);
  161. break;
  162. default:
  163. ret = -ENOTSUPP;
  164. break;
  165. }
  166. module_put(THIS_MODULE);
  167. return ret;
  168. }
  169. static struct notifier_block nfs4blocklayout_block = {
  170. .notifier_call = rpc_pipefs_event,
  171. };
  172. static struct dentry *nfs4blocklayout_register_net(struct net *net,
  173. struct rpc_pipe *pipe)
  174. {
  175. struct super_block *pipefs_sb;
  176. struct dentry *dentry;
  177. pipefs_sb = rpc_get_sb_net(net);
  178. if (!pipefs_sb)
  179. return NULL;
  180. dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe);
  181. rpc_put_sb_net(net);
  182. return dentry;
  183. }
  184. static void nfs4blocklayout_unregister_net(struct net *net,
  185. struct rpc_pipe *pipe)
  186. {
  187. struct super_block *pipefs_sb;
  188. pipefs_sb = rpc_get_sb_net(net);
  189. if (pipefs_sb) {
  190. nfs4blocklayout_unregister_sb(pipefs_sb, pipe);
  191. rpc_put_sb_net(net);
  192. }
  193. }
  194. static int nfs4blocklayout_net_init(struct net *net)
  195. {
  196. struct nfs_net *nn = net_generic(net, nfs_net_id);
  197. struct dentry *dentry;
  198. init_waitqueue_head(&nn->bl_wq);
  199. nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0);
  200. if (IS_ERR(nn->bl_device_pipe))
  201. return PTR_ERR(nn->bl_device_pipe);
  202. dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe);
  203. if (IS_ERR(dentry)) {
  204. rpc_destroy_pipe_data(nn->bl_device_pipe);
  205. return PTR_ERR(dentry);
  206. }
  207. nn->bl_device_pipe->dentry = dentry;
  208. return 0;
  209. }
  210. static void nfs4blocklayout_net_exit(struct net *net)
  211. {
  212. struct nfs_net *nn = net_generic(net, nfs_net_id);
  213. nfs4blocklayout_unregister_net(net, nn->bl_device_pipe);
  214. rpc_destroy_pipe_data(nn->bl_device_pipe);
  215. nn->bl_device_pipe = NULL;
  216. }
  217. static struct pernet_operations nfs4blocklayout_net_ops = {
  218. .init = nfs4blocklayout_net_init,
  219. .exit = nfs4blocklayout_net_exit,
  220. };
  221. int __init bl_init_pipefs(void)
  222. {
  223. int ret;
  224. ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block);
  225. if (ret)
  226. goto out;
  227. ret = register_pernet_subsys(&nfs4blocklayout_net_ops);
  228. if (ret)
  229. goto out_unregister_notifier;
  230. return 0;
  231. out_unregister_notifier:
  232. rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
  233. out:
  234. return ret;
  235. }
  236. void __exit bl_cleanup_pipefs(void)
  237. {
  238. rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
  239. unregister_pernet_subsys(&nfs4blocklayout_net_ops);
  240. }