rpc_pipefs.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (c) 2006,2007 The Regents of the University of Michigan.
  3. * All rights reserved.
  4. *
  5. * Andy Adamson <[email protected]>
  6. * Fred Isaman <[email protected]>
  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/blkdev.h>
  30. #include "blocklayout.h"
  31. #define NFSDBG_FACILITY NFSDBG_PNFS_LD
  32. static void
  33. nfs4_encode_simple(__be32 *p, struct pnfs_block_volume *b)
  34. {
  35. int i;
  36. *p++ = cpu_to_be32(1);
  37. *p++ = cpu_to_be32(b->type);
  38. *p++ = cpu_to_be32(b->simple.nr_sigs);
  39. for (i = 0; i < b->simple.nr_sigs; i++) {
  40. p = xdr_encode_hyper(p, b->simple.sigs[i].offset);
  41. p = xdr_encode_opaque(p, b->simple.sigs[i].sig,
  42. b->simple.sigs[i].sig_len);
  43. }
  44. }
  45. dev_t
  46. bl_resolve_deviceid(struct nfs_server *server, struct pnfs_block_volume *b,
  47. gfp_t gfp_mask)
  48. {
  49. struct net *net = server->nfs_client->cl_net;
  50. struct nfs_net *nn = net_generic(net, nfs_net_id);
  51. struct bl_dev_msg *reply = &nn->bl_mount_reply;
  52. struct bl_pipe_msg bl_pipe_msg;
  53. struct rpc_pipe_msg *msg = &bl_pipe_msg.msg;
  54. struct bl_msg_hdr *bl_msg;
  55. DECLARE_WAITQUEUE(wq, current);
  56. dev_t dev = 0;
  57. int rc;
  58. dprintk("%s CREATING PIPEFS MESSAGE\n", __func__);
  59. mutex_lock(&nn->bl_mutex);
  60. bl_pipe_msg.bl_wq = &nn->bl_wq;
  61. b->simple.len += 4; /* single volume */
  62. if (b->simple.len > PAGE_SIZE)
  63. goto out_unlock;
  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_free_data;
  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_free_data;
  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_free_data;
  87. }
  88. dev = MKDEV(reply->major, reply->minor);
  89. out_free_data:
  90. kfree(msg->data);
  91. out_unlock:
  92. mutex_unlock(&nn->bl_mutex);
  93. return dev;
  94. }
  95. static ssize_t bl_pipe_downcall(struct file *filp, const char __user *src,
  96. size_t mlen)
  97. {
  98. struct nfs_net *nn = net_generic(file_inode(filp)->i_sb->s_fs_info,
  99. nfs_net_id);
  100. if (mlen != sizeof (struct bl_dev_msg))
  101. return -EINVAL;
  102. if (copy_from_user(&nn->bl_mount_reply, src, mlen) != 0)
  103. return -EFAULT;
  104. wake_up(&nn->bl_wq);
  105. return mlen;
  106. }
  107. static void bl_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  108. {
  109. struct bl_pipe_msg *bl_pipe_msg =
  110. container_of(msg, struct bl_pipe_msg, msg);
  111. if (msg->errno >= 0)
  112. return;
  113. wake_up(bl_pipe_msg->bl_wq);
  114. }
  115. static const struct rpc_pipe_ops bl_upcall_ops = {
  116. .upcall = rpc_pipe_generic_upcall,
  117. .downcall = bl_pipe_downcall,
  118. .destroy_msg = bl_pipe_destroy_msg,
  119. };
  120. static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb,
  121. struct rpc_pipe *pipe)
  122. {
  123. struct dentry *dir, *dentry;
  124. dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME);
  125. if (dir == NULL)
  126. return ERR_PTR(-ENOENT);
  127. dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe);
  128. dput(dir);
  129. return dentry;
  130. }
  131. static void nfs4blocklayout_unregister_sb(struct super_block *sb,
  132. struct rpc_pipe *pipe)
  133. {
  134. if (pipe->dentry)
  135. rpc_unlink(pipe->dentry);
  136. }
  137. static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
  138. void *ptr)
  139. {
  140. struct super_block *sb = ptr;
  141. struct net *net = sb->s_fs_info;
  142. struct nfs_net *nn = net_generic(net, nfs_net_id);
  143. struct dentry *dentry;
  144. int ret = 0;
  145. if (!try_module_get(THIS_MODULE))
  146. return 0;
  147. if (nn->bl_device_pipe == NULL) {
  148. module_put(THIS_MODULE);
  149. return 0;
  150. }
  151. switch (event) {
  152. case RPC_PIPEFS_MOUNT:
  153. dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe);
  154. if (IS_ERR(dentry)) {
  155. ret = PTR_ERR(dentry);
  156. break;
  157. }
  158. nn->bl_device_pipe->dentry = dentry;
  159. break;
  160. case RPC_PIPEFS_UMOUNT:
  161. if (nn->bl_device_pipe->dentry)
  162. nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe);
  163. break;
  164. default:
  165. ret = -ENOTSUPP;
  166. break;
  167. }
  168. module_put(THIS_MODULE);
  169. return ret;
  170. }
  171. static struct notifier_block nfs4blocklayout_block = {
  172. .notifier_call = rpc_pipefs_event,
  173. };
  174. static struct dentry *nfs4blocklayout_register_net(struct net *net,
  175. struct rpc_pipe *pipe)
  176. {
  177. struct super_block *pipefs_sb;
  178. struct dentry *dentry;
  179. pipefs_sb = rpc_get_sb_net(net);
  180. if (!pipefs_sb)
  181. return NULL;
  182. dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe);
  183. rpc_put_sb_net(net);
  184. return dentry;
  185. }
  186. static void nfs4blocklayout_unregister_net(struct net *net,
  187. struct rpc_pipe *pipe)
  188. {
  189. struct super_block *pipefs_sb;
  190. pipefs_sb = rpc_get_sb_net(net);
  191. if (pipefs_sb) {
  192. nfs4blocklayout_unregister_sb(pipefs_sb, pipe);
  193. rpc_put_sb_net(net);
  194. }
  195. }
  196. static int nfs4blocklayout_net_init(struct net *net)
  197. {
  198. struct nfs_net *nn = net_generic(net, nfs_net_id);
  199. struct dentry *dentry;
  200. mutex_init(&nn->bl_mutex);
  201. init_waitqueue_head(&nn->bl_wq);
  202. nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0);
  203. if (IS_ERR(nn->bl_device_pipe))
  204. return PTR_ERR(nn->bl_device_pipe);
  205. dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe);
  206. if (IS_ERR(dentry)) {
  207. rpc_destroy_pipe_data(nn->bl_device_pipe);
  208. return PTR_ERR(dentry);
  209. }
  210. nn->bl_device_pipe->dentry = dentry;
  211. return 0;
  212. }
  213. static void nfs4blocklayout_net_exit(struct net *net)
  214. {
  215. struct nfs_net *nn = net_generic(net, nfs_net_id);
  216. nfs4blocklayout_unregister_net(net, nn->bl_device_pipe);
  217. rpc_destroy_pipe_data(nn->bl_device_pipe);
  218. nn->bl_device_pipe = NULL;
  219. }
  220. static struct pernet_operations nfs4blocklayout_net_ops = {
  221. .init = nfs4blocklayout_net_init,
  222. .exit = nfs4blocklayout_net_exit,
  223. };
  224. int __init bl_init_pipefs(void)
  225. {
  226. int ret;
  227. ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block);
  228. if (ret)
  229. goto out;
  230. ret = register_pernet_subsys(&nfs4blocklayout_net_ops);
  231. if (ret)
  232. goto out_unregister_notifier;
  233. return 0;
  234. out_unregister_notifier:
  235. rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
  236. out:
  237. return ret;
  238. }
  239. void bl_cleanup_pipefs(void)
  240. {
  241. rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
  242. unregister_pernet_subsys(&nfs4blocklayout_net_ops);
  243. }