xref: /openbmc/linux/fs/nfs/blocklayout/rpc_pipefs.c (revision 3c6a73cc)
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 
29 #include <linux/module.h>
30 #include <linux/genhd.h>
31 #include <linux/blkdev.h>
32 
33 #include "blocklayout.h"
34 
35 #define NFSDBG_FACILITY         NFSDBG_PNFS_LD
36 
37 static void
38 nfs4_encode_simple(__be32 *p, struct pnfs_block_volume *b)
39 {
40 	int i;
41 
42 	*p++ = cpu_to_be32(1);
43 	*p++ = cpu_to_be32(b->type);
44 	*p++ = cpu_to_be32(b->simple.nr_sigs);
45 	for (i = 0; i < b->simple.nr_sigs; i++) {
46 		p = xdr_encode_hyper(p, b->simple.sigs[i].offset);
47 		p = xdr_encode_opaque(p, b->simple.sigs[i].sig,
48 					 b->simple.sigs[i].sig_len);
49 	}
50 }
51 
52 dev_t
53 bl_resolve_deviceid(struct nfs_server *server, struct pnfs_block_volume *b,
54 		gfp_t gfp_mask)
55 {
56 	struct net *net = server->nfs_client->cl_net;
57 	struct nfs_net *nn = net_generic(net, nfs_net_id);
58 	struct bl_dev_msg *reply = &nn->bl_mount_reply;
59 	struct bl_pipe_msg bl_pipe_msg;
60 	struct rpc_pipe_msg *msg = &bl_pipe_msg.msg;
61 	struct bl_msg_hdr *bl_msg;
62 	DECLARE_WAITQUEUE(wq, current);
63 	dev_t dev = 0;
64 	int rc;
65 
66 	dprintk("%s CREATING PIPEFS MESSAGE\n", __func__);
67 
68 	bl_pipe_msg.bl_wq = &nn->bl_wq;
69 
70 	b->simple.len += 4;	/* single volume */
71 	if (b->simple.len > PAGE_SIZE)
72 		return -EIO;
73 
74 	memset(msg, 0, sizeof(*msg));
75 	msg->len = sizeof(*bl_msg) + b->simple.len;
76 	msg->data = kzalloc(msg->len, gfp_mask);
77 	if (!msg->data)
78 		goto out;
79 
80 	bl_msg = msg->data;
81 	bl_msg->type = BL_DEVICE_MOUNT,
82 	bl_msg->totallen = b->simple.len;
83 	nfs4_encode_simple(msg->data + sizeof(*bl_msg), b);
84 
85 	dprintk("%s CALLING USERSPACE DAEMON\n", __func__);
86 	add_wait_queue(&nn->bl_wq, &wq);
87 	rc = rpc_queue_upcall(nn->bl_device_pipe, msg);
88 	if (rc < 0) {
89 		remove_wait_queue(&nn->bl_wq, &wq);
90 		goto out;
91 	}
92 
93 	set_current_state(TASK_UNINTERRUPTIBLE);
94 	schedule();
95 	remove_wait_queue(&nn->bl_wq, &wq);
96 
97 	if (reply->status != BL_DEVICE_REQUEST_PROC) {
98 		printk(KERN_WARNING "%s failed to decode device: %d\n",
99 			__func__, reply->status);
100 		goto out;
101 	}
102 
103 	dev = MKDEV(reply->major, reply->minor);
104 out:
105 	kfree(msg->data);
106 	return dev;
107 }
108 
109 static ssize_t bl_pipe_downcall(struct file *filp, const char __user *src,
110 			 size_t mlen)
111 {
112 	struct nfs_net *nn = net_generic(filp->f_dentry->d_sb->s_fs_info,
113 					 nfs_net_id);
114 
115 	if (mlen != sizeof (struct bl_dev_msg))
116 		return -EINVAL;
117 
118 	if (copy_from_user(&nn->bl_mount_reply, src, mlen) != 0)
119 		return -EFAULT;
120 
121 	wake_up(&nn->bl_wq);
122 
123 	return mlen;
124 }
125 
126 static void bl_pipe_destroy_msg(struct rpc_pipe_msg *msg)
127 {
128 	struct bl_pipe_msg *bl_pipe_msg =
129 		container_of(msg, struct bl_pipe_msg, msg);
130 
131 	if (msg->errno >= 0)
132 		return;
133 	wake_up(bl_pipe_msg->bl_wq);
134 }
135 
136 static const struct rpc_pipe_ops bl_upcall_ops = {
137 	.upcall		= rpc_pipe_generic_upcall,
138 	.downcall	= bl_pipe_downcall,
139 	.destroy_msg	= bl_pipe_destroy_msg,
140 };
141 
142 static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb,
143 					    struct rpc_pipe *pipe)
144 {
145 	struct dentry *dir, *dentry;
146 
147 	dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME);
148 	if (dir == NULL)
149 		return ERR_PTR(-ENOENT);
150 	dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe);
151 	dput(dir);
152 	return dentry;
153 }
154 
155 static void nfs4blocklayout_unregister_sb(struct super_block *sb,
156 					  struct rpc_pipe *pipe)
157 {
158 	if (pipe->dentry)
159 		rpc_unlink(pipe->dentry);
160 }
161 
162 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
163 			   void *ptr)
164 {
165 	struct super_block *sb = ptr;
166 	struct net *net = sb->s_fs_info;
167 	struct nfs_net *nn = net_generic(net, nfs_net_id);
168 	struct dentry *dentry;
169 	int ret = 0;
170 
171 	if (!try_module_get(THIS_MODULE))
172 		return 0;
173 
174 	if (nn->bl_device_pipe == NULL) {
175 		module_put(THIS_MODULE);
176 		return 0;
177 	}
178 
179 	switch (event) {
180 	case RPC_PIPEFS_MOUNT:
181 		dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe);
182 		if (IS_ERR(dentry)) {
183 			ret = PTR_ERR(dentry);
184 			break;
185 		}
186 		nn->bl_device_pipe->dentry = dentry;
187 		break;
188 	case RPC_PIPEFS_UMOUNT:
189 		if (nn->bl_device_pipe->dentry)
190 			nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe);
191 		break;
192 	default:
193 		ret = -ENOTSUPP;
194 		break;
195 	}
196 	module_put(THIS_MODULE);
197 	return ret;
198 }
199 
200 static struct notifier_block nfs4blocklayout_block = {
201 	.notifier_call = rpc_pipefs_event,
202 };
203 
204 static struct dentry *nfs4blocklayout_register_net(struct net *net,
205 						   struct rpc_pipe *pipe)
206 {
207 	struct super_block *pipefs_sb;
208 	struct dentry *dentry;
209 
210 	pipefs_sb = rpc_get_sb_net(net);
211 	if (!pipefs_sb)
212 		return NULL;
213 	dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe);
214 	rpc_put_sb_net(net);
215 	return dentry;
216 }
217 
218 static void nfs4blocklayout_unregister_net(struct net *net,
219 					   struct rpc_pipe *pipe)
220 {
221 	struct super_block *pipefs_sb;
222 
223 	pipefs_sb = rpc_get_sb_net(net);
224 	if (pipefs_sb) {
225 		nfs4blocklayout_unregister_sb(pipefs_sb, pipe);
226 		rpc_put_sb_net(net);
227 	}
228 }
229 
230 static int nfs4blocklayout_net_init(struct net *net)
231 {
232 	struct nfs_net *nn = net_generic(net, nfs_net_id);
233 	struct dentry *dentry;
234 
235 	init_waitqueue_head(&nn->bl_wq);
236 	nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0);
237 	if (IS_ERR(nn->bl_device_pipe))
238 		return PTR_ERR(nn->bl_device_pipe);
239 	dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe);
240 	if (IS_ERR(dentry)) {
241 		rpc_destroy_pipe_data(nn->bl_device_pipe);
242 		return PTR_ERR(dentry);
243 	}
244 	nn->bl_device_pipe->dentry = dentry;
245 	return 0;
246 }
247 
248 static void nfs4blocklayout_net_exit(struct net *net)
249 {
250 	struct nfs_net *nn = net_generic(net, nfs_net_id);
251 
252 	nfs4blocklayout_unregister_net(net, nn->bl_device_pipe);
253 	rpc_destroy_pipe_data(nn->bl_device_pipe);
254 	nn->bl_device_pipe = NULL;
255 }
256 
257 static struct pernet_operations nfs4blocklayout_net_ops = {
258 	.init = nfs4blocklayout_net_init,
259 	.exit = nfs4blocklayout_net_exit,
260 };
261 
262 int __init bl_init_pipefs(void)
263 {
264 	int ret;
265 
266 	ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block);
267 	if (ret)
268 		goto out;
269 	ret = register_pernet_subsys(&nfs4blocklayout_net_ops);
270 	if (ret)
271 		goto out_unregister_notifier;
272 	return 0;
273 
274 out_unregister_notifier:
275 	rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
276 out:
277 	return ret;
278 }
279 
280 void __exit bl_cleanup_pipefs(void)
281 {
282 	rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
283 	unregister_pernet_subsys(&nfs4blocklayout_net_ops);
284 }
285