rpc_pipefs.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. mutex_lock(&nn->bl_mutex);
  61. bl_pipe_msg.bl_wq = &nn->bl_wq;
  62. b->simple.len += 4; /* single volume */
  63. if (b->simple.len > PAGE_SIZE)
  64. goto out_unlock;
  65. memset(msg, 0, sizeof(*msg));
  66. msg->len = sizeof(*bl_msg) + b->simple.len;
  67. msg->data = kzalloc(msg->len, gfp_mask);
  68. if (!msg->data)
  69. goto out_free_data;
  70. bl_msg = msg->data;
  71. bl_msg->type = BL_DEVICE_MOUNT;
  72. bl_msg->totallen = b->simple.len;
  73. nfs4_encode_simple(msg->data + sizeof(*bl_msg), b);
  74. dprintk("%s CALLING USERSPACE DAEMON\n", __func__);
  75. add_wait_queue(&nn->bl_wq, &wq);
  76. rc = rpc_queue_upcall(nn->bl_device_pipe, msg);
  77. if (rc < 0) {
  78. remove_wait_queue(&nn->bl_wq, &wq);
  79. goto out_free_data;
  80. }
  81. set_current_state(TASK_UNINTERRUPTIBLE);
  82. schedule();
  83. remove_wait_queue(&nn->bl_wq, &wq);
  84. if (reply->status != BL_DEVICE_REQUEST_PROC) {
  85. printk(KERN_WARNING "%s failed to decode device: %d\n",
  86. __func__, reply->status);
  87. goto out_free_data;
  88. }
  89. dev = MKDEV(reply->major, reply->minor);
  90. out_free_data:
  91. kfree(msg->data);
  92. out_unlock:
  93. mutex_unlock(&nn->bl_mutex);
  94. return dev;
  95. }
  96. static ssize_t bl_pipe_downcall(struct file *filp, const char __user *src,
  97. size_t mlen)
  98. {
  99. struct nfs_net *nn = net_generic(file_inode(filp)->i_sb->s_fs_info,
  100. nfs_net_id);
  101. if (mlen != sizeof (struct bl_dev_msg))
  102. return -EINVAL;
  103. if (copy_from_user(&nn->bl_mount_reply, src, mlen) != 0)
  104. return -EFAULT;
  105. wake_up(&nn->bl_wq);
  106. return mlen;
  107. }
  108. static void bl_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  109. {
  110. struct bl_pipe_msg *bl_pipe_msg =
  111. container_of(msg, struct bl_pipe_msg, msg);
  112. if (msg->errno >= 0)
  113. return;
  114. wake_up(bl_pipe_msg->bl_wq);
  115. }
  116. static const struct rpc_pipe_ops bl_upcall_ops = {
  117. .upcall = rpc_pipe_generic_upcall,
  118. .downcall = bl_pipe_downcall,
  119. .destroy_msg = bl_pipe_destroy_msg,
  120. };
  121. static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb,
  122. struct rpc_pipe *pipe)
  123. {
  124. struct dentry *dir, *dentry;
  125. dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME);
  126. if (dir == NULL)
  127. return ERR_PTR(-ENOENT);
  128. dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe);
  129. dput(dir);
  130. return dentry;
  131. }
  132. static void nfs4blocklayout_unregister_sb(struct super_block *sb,
  133. struct rpc_pipe *pipe)
  134. {
  135. if (pipe->dentry)
  136. rpc_unlink(pipe->dentry);
  137. }
  138. static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
  139. void *ptr)
  140. {
  141. struct super_block *sb = ptr;
  142. struct net *net = sb->s_fs_info;
  143. struct nfs_net *nn = net_generic(net, nfs_net_id);
  144. struct dentry *dentry;
  145. int ret = 0;
  146. if (!try_module_get(THIS_MODULE))
  147. return 0;
  148. if (nn->bl_device_pipe == NULL) {
  149. module_put(THIS_MODULE);
  150. return 0;
  151. }
  152. switch (event) {
  153. case RPC_PIPEFS_MOUNT:
  154. dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe);
  155. if (IS_ERR(dentry)) {
  156. ret = PTR_ERR(dentry);
  157. break;
  158. }
  159. nn->bl_device_pipe->dentry = dentry;
  160. break;
  161. case RPC_PIPEFS_UMOUNT:
  162. if (nn->bl_device_pipe->dentry)
  163. nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe);
  164. break;
  165. default:
  166. ret = -ENOTSUPP;
  167. break;
  168. }
  169. module_put(THIS_MODULE);
  170. return ret;
  171. }
  172. static struct notifier_block nfs4blocklayout_block = {
  173. .notifier_call = rpc_pipefs_event,
  174. };
  175. static struct dentry *nfs4blocklayout_register_net(struct net *net,
  176. struct rpc_pipe *pipe)
  177. {
  178. struct super_block *pipefs_sb;
  179. struct dentry *dentry;
  180. pipefs_sb = rpc_get_sb_net(net);
  181. if (!pipefs_sb)
  182. return NULL;
  183. dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe);
  184. rpc_put_sb_net(net);
  185. return dentry;
  186. }
  187. static void nfs4blocklayout_unregister_net(struct net *net,
  188. struct rpc_pipe *pipe)
  189. {
  190. struct super_block *pipefs_sb;
  191. pipefs_sb = rpc_get_sb_net(net);
  192. if (pipefs_sb) {
  193. nfs4blocklayout_unregister_sb(pipefs_sb, pipe);
  194. rpc_put_sb_net(net);
  195. }
  196. }
  197. static int nfs4blocklayout_net_init(struct net *net)
  198. {
  199. struct nfs_net *nn = net_generic(net, nfs_net_id);
  200. struct dentry *dentry;
  201. mutex_init(&nn->bl_mutex);
  202. init_waitqueue_head(&nn->bl_wq);
  203. nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0);
  204. if (IS_ERR(nn->bl_device_pipe))
  205. return PTR_ERR(nn->bl_device_pipe);
  206. dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe);
  207. if (IS_ERR(dentry)) {
  208. rpc_destroy_pipe_data(nn->bl_device_pipe);
  209. return PTR_ERR(dentry);
  210. }
  211. nn->bl_device_pipe->dentry = dentry;
  212. return 0;
  213. }
  214. static void nfs4blocklayout_net_exit(struct net *net)
  215. {
  216. struct nfs_net *nn = net_generic(net, nfs_net_id);
  217. nfs4blocklayout_unregister_net(net, nn->bl_device_pipe);
  218. rpc_destroy_pipe_data(nn->bl_device_pipe);
  219. nn->bl_device_pipe = NULL;
  220. }
  221. static struct pernet_operations nfs4blocklayout_net_ops = {
  222. .init = nfs4blocklayout_net_init,
  223. .exit = nfs4blocklayout_net_exit,
  224. };
  225. int __init bl_init_pipefs(void)
  226. {
  227. int ret;
  228. ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block);
  229. if (ret)
  230. goto out;
  231. ret = register_pernet_subsys(&nfs4blocklayout_net_ops);
  232. if (ret)
  233. goto out_unregister_notifier;
  234. return 0;
  235. out_unregister_notifier:
  236. rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
  237. out:
  238. return ret;
  239. }
  240. void bl_cleanup_pipefs(void)
  241. {
  242. rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
  243. unregister_pernet_subsys(&nfs4blocklayout_net_ops);
  244. }