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/blkdev.h> 31 32 #include "blocklayout.h" 33 34 #define NFSDBG_FACILITY NFSDBG_PNFS_LD 35 36 static void 37 nfs4_encode_simple(__be32 *p, struct pnfs_block_volume *b) 38 { 39 int i; 40 41 *p++ = cpu_to_be32(1); 42 *p++ = cpu_to_be32(b->type); 43 *p++ = cpu_to_be32(b->simple.nr_sigs); 44 for (i = 0; i < b->simple.nr_sigs; i++) { 45 p = xdr_encode_hyper(p, b->simple.sigs[i].offset); 46 p = xdr_encode_opaque(p, b->simple.sigs[i].sig, 47 b->simple.sigs[i].sig_len); 48 } 49 } 50 51 dev_t 52 bl_resolve_deviceid(struct nfs_server *server, struct pnfs_block_volume *b, 53 gfp_t gfp_mask) 54 { 55 struct net *net = server->nfs_client->cl_net; 56 struct nfs_net *nn = net_generic(net, nfs_net_id); 57 struct bl_dev_msg *reply = &nn->bl_mount_reply; 58 struct rpc_pipe_msg *msg = &nn->bl_pipe_msg; 59 struct rpc_pipe *pipe = nn->bl_device_pipe; 60 struct bl_msg_hdr *bl_msg; 61 dev_t dev = 0; 62 int rc; 63 64 dprintk("%s CREATING PIPEFS MESSAGE\n", __func__); 65 66 mutex_lock(&nn->bl_mutex); 67 68 b->simple.len += 4; /* single volume */ 69 if (b->simple.len > PAGE_SIZE) 70 goto out_unlock; 71 72 memset(msg, 0, sizeof(*msg)); 73 msg->len = sizeof(*bl_msg) + b->simple.len; 74 msg->data = kzalloc(msg->len, gfp_mask); 75 if (!msg->data) 76 goto out_unlock; 77 78 bl_msg = msg->data; 79 bl_msg->type = BL_DEVICE_MOUNT; 80 bl_msg->totallen = b->simple.len; 81 nfs4_encode_simple(msg->data + sizeof(*bl_msg), b); 82 83 dprintk("%s CALLING USERSPACE DAEMON\n", __func__); 84 reinit_completion(&nn->bl_done); 85 rc = rpc_queue_upcall(pipe, msg); 86 if (rc < 0) 87 goto out_free_data; 88 89 wait_for_completion(&nn->bl_done); 90 91 if (msg->errno < 0) 92 goto out_free_data; 93 if (reply->status != BL_DEVICE_REQUEST_PROC) { 94 printk(KERN_WARNING "%s failed to decode device: %d\n", 95 __func__, reply->status); 96 goto out_free_data; 97 } 98 99 dev = MKDEV(reply->major, reply->minor); 100 out_free_data: 101 kfree(msg->data); 102 out_unlock: 103 mutex_unlock(&nn->bl_mutex); 104 return dev; 105 } 106 107 static ssize_t bl_pipe_downcall(struct file *filp, const char __user *src, 108 size_t mlen) 109 { 110 struct nfs_net *nn = net_generic(file_inode(filp)->i_sb->s_fs_info, 111 nfs_net_id); 112 struct rpc_pipe *pipe = nn->bl_device_pipe; 113 struct bl_dev_msg reply; 114 bool accepted; 115 116 if (mlen != sizeof(reply)) 117 return -EINVAL; 118 if (copy_from_user(&reply, src, mlen) != 0) 119 return -EFAULT; 120 121 /* 122 * Accept a reply only after blkmapd has read the whole upcall. 123 * Completion retires the message, here and in 124 * bl_pipe_destroy_msg(), so a later reply finds nothing in 125 * flight. 126 */ 127 spin_lock(&pipe->lock); 128 accepted = rpc_msg_is_inflight(&nn->bl_pipe_msg); 129 if (accepted) { 130 nn->bl_pipe_msg.copied = 0; 131 nn->bl_mount_reply = reply; 132 complete(&nn->bl_done); 133 } 134 spin_unlock(&pipe->lock); 135 if (!accepted) 136 return -EINVAL; 137 return mlen; 138 } 139 140 static void bl_pipe_destroy_msg(struct rpc_pipe_msg *msg) 141 { 142 struct nfs_net *nn = container_of(msg, struct nfs_net, bl_pipe_msg); 143 struct rpc_pipe *pipe = nn->bl_device_pipe; 144 145 if (msg->errno >= 0) 146 return; 147 148 spin_lock(&pipe->lock); 149 msg->copied = 0; 150 spin_unlock(&pipe->lock); 151 complete(&nn->bl_done); 152 } 153 154 /* 155 * An upcall blkmapd has consumed is off every pipe list, so the 156 * purges in rpc_pipe_release() and rpc_close_pipes() cannot reach it. 157 * No downcall can arrive once the pipe is closed; release its waiter 158 * here. 159 */ 160 static void bl_release_pipe(struct inode *inode) 161 { 162 struct nfs_net *nn = net_generic(inode->i_sb->s_fs_info, nfs_net_id); 163 struct rpc_pipe *pipe = nn->bl_device_pipe; 164 165 spin_lock(&pipe->lock); 166 if (rpc_msg_is_inflight(&nn->bl_pipe_msg)) { 167 nn->bl_pipe_msg.copied = 0; 168 nn->bl_pipe_msg.errno = -EPIPE; 169 complete(&nn->bl_done); 170 } 171 spin_unlock(&pipe->lock); 172 } 173 174 static const struct rpc_pipe_ops bl_upcall_ops = { 175 .upcall = rpc_pipe_generic_upcall, 176 .downcall = bl_pipe_downcall, 177 .destroy_msg = bl_pipe_destroy_msg, 178 .release_pipe = bl_release_pipe, 179 }; 180 181 static int nfs4blocklayout_register_sb(struct super_block *sb, 182 struct rpc_pipe *pipe) 183 { 184 struct dentry *dir; 185 int err; 186 187 dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME); 188 if (dir == NULL) 189 return -ENOENT; 190 err = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe); 191 dput(dir); 192 return err; 193 } 194 195 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event, 196 void *ptr) 197 { 198 struct super_block *sb = ptr; 199 struct net *net = sb->s_fs_info; 200 struct nfs_net *nn = net_generic(net, nfs_net_id); 201 int ret = 0; 202 203 if (!try_module_get(THIS_MODULE)) 204 return 0; 205 206 if (nn->bl_device_pipe == NULL) { 207 module_put(THIS_MODULE); 208 return 0; 209 } 210 211 switch (event) { 212 case RPC_PIPEFS_MOUNT: 213 ret = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe); 214 break; 215 case RPC_PIPEFS_UMOUNT: 216 rpc_unlink(nn->bl_device_pipe); 217 break; 218 default: 219 ret = -ENOTSUPP; 220 break; 221 } 222 module_put(THIS_MODULE); 223 return ret; 224 } 225 226 static struct notifier_block nfs4blocklayout_block = { 227 .notifier_call = rpc_pipefs_event, 228 }; 229 230 static int nfs4blocklayout_register_net(struct net *net, struct rpc_pipe *pipe) 231 { 232 struct super_block *pipefs_sb; 233 int ret; 234 235 pipefs_sb = rpc_get_sb_net(net); 236 if (!pipefs_sb) 237 return 0; 238 ret = nfs4blocklayout_register_sb(pipefs_sb, pipe); 239 rpc_put_sb_net(net); 240 return ret; 241 } 242 243 static void nfs4blocklayout_unregister_net(struct net *net, 244 struct rpc_pipe *pipe) 245 { 246 struct super_block *pipefs_sb; 247 248 pipefs_sb = rpc_get_sb_net(net); 249 if (pipefs_sb) { 250 rpc_unlink(pipe); 251 rpc_put_sb_net(net); 252 } 253 } 254 255 static int nfs4blocklayout_net_init(struct net *net) 256 { 257 struct nfs_net *nn = net_generic(net, nfs_net_id); 258 int err; 259 260 mutex_init(&nn->bl_mutex); 261 init_completion(&nn->bl_done); 262 nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0); 263 if (IS_ERR(nn->bl_device_pipe)) 264 return PTR_ERR(nn->bl_device_pipe); 265 err = nfs4blocklayout_register_net(net, nn->bl_device_pipe); 266 if (unlikely(err)) 267 rpc_destroy_pipe_data(nn->bl_device_pipe); 268 return err; 269 } 270 271 static void nfs4blocklayout_net_exit(struct net *net) 272 { 273 struct nfs_net *nn = net_generic(net, nfs_net_id); 274 275 nfs4blocklayout_unregister_net(net, nn->bl_device_pipe); 276 rpc_destroy_pipe_data(nn->bl_device_pipe); 277 nn->bl_device_pipe = NULL; 278 } 279 280 static struct pernet_operations nfs4blocklayout_net_ops = { 281 .init = nfs4blocklayout_net_init, 282 .exit = nfs4blocklayout_net_exit, 283 }; 284 285 int __init bl_init_pipefs(void) 286 { 287 int ret; 288 289 ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block); 290 if (ret) 291 goto out; 292 ret = register_pernet_subsys(&nfs4blocklayout_net_ops); 293 if (ret) 294 goto out_unregister_notifier; 295 return 0; 296 297 out_unregister_notifier: 298 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block); 299 out: 300 return ret; 301 } 302 303 void bl_cleanup_pipefs(void) 304 { 305 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block); 306 unregister_pernet_subsys(&nfs4blocklayout_net_ops); 307 } 308