| 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 |
|
| 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 bl_pipe_msg bl_pipe_msg; |
| 59 |
struct rpc_pipe_msg *msg = &bl_pipe_msg.msg; |
| 60 |
struct bl_msg_hdr *bl_msg; |
| 61 |
DECLARE_WAITQUEUE(wq, current); |
| 62 |
dev_t dev = 0; |
| 63 |
int rc; |
| 64 |
|
| 65 |
dprintk("%s CREATING PIPEFS MESSAGE\n", __func__); |
| 66 |
|
| 67 |
mutex_lock(&nn->bl_mutex); |
| 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 |
goto out_unlock; |
| 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_unlock; |
| 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_free_data; |
| 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_free_data; |
| 101 |
} |
| 102 |
|
| 103 |
dev = MKDEV(reply->major, reply->minor); |
| 104 |
out_free_data: |
| 105 |
kfree(msg->data); |
| 106 |
out_unlock: |
| 107 |
mutex_unlock(&nn->bl_mutex); |
| 108 |
return dev; |
| 109 |
} |
| 110 |
|
| 111 |
static ssize_t bl_pipe_downcall(struct file *filp, const char __user *src, |
| 112 |
size_t mlen) |
| 113 |
{ |
| 114 |
struct nfs_net *nn = net_generic(file_inode(filp)->i_sb->s_fs_info, |
| 115 |
nfs_net_id); |
| 116 |
|
| 117 |
if (mlen != sizeof (struct bl_dev_msg)) |
| 118 |
return -EINVAL; |
| 119 |
|
| 120 |
if (copy_from_user(&nn->bl_mount_reply, src, mlen) != 0) |
| 121 |
return -EFAULT; |
| 122 |
|
| 123 |
wake_up(&nn->bl_wq); |
| 124 |
|
| 125 |
return mlen; |
| 126 |
} |
| 127 |
|
| 128 |
static void bl_pipe_destroy_msg(struct rpc_pipe_msg *msg) |
| 129 |
{ |
| 130 |
struct bl_pipe_msg *bl_pipe_msg = |
| 131 |
container_of(msg, struct bl_pipe_msg, msg); |
| 132 |
|
| 133 |
if (msg->errno >= 0) |
| 134 |
return; |
| 135 |
wake_up(bl_pipe_msg->bl_wq); |
| 136 |
} |
| 137 |
|
| 138 |
static const struct rpc_pipe_ops bl_upcall_ops = { |
| 139 |
.upcall = rpc_pipe_generic_upcall, |
| 140 |
.downcall = bl_pipe_downcall, |
| 141 |
.destroy_msg = bl_pipe_destroy_msg, |
| 142 |
}; |
| 143 |
|
| 144 |
static int nfs4blocklayout_register_sb(struct super_block *sb, |
| 145 |
struct rpc_pipe *pipe) |
| 146 |
{ |
| 147 |
struct dentry *dir; |
| 148 |
int err; |
| 149 |
|
| 150 |
dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME); |
| 151 |
if (dir == NULL) |
| 152 |
return -ENOENT; |
| 153 |
err = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe); |
| 154 |
dput(dir); |
| 155 |
return err; |
| 156 |
} |
| 157 |
|
| 158 |
static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event, |
| 159 |
void *ptr) |
| 160 |
{ |
| 161 |
struct super_block *sb = ptr; |
| 162 |
struct net *net = sb->s_fs_info; |
| 163 |
struct nfs_net *nn = net_generic(net, nfs_net_id); |
| 164 |
int ret = 0; |
| 165 |
|
| 166 |
if (!try_module_get(THIS_MODULE)) |
| 167 |
return 0; |
| 168 |
|
| 169 |
if (nn->bl_device_pipe == NULL) { |
| 170 |
module_put(THIS_MODULE); |
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
switch (event) { |
| 175 |
case RPC_PIPEFS_MOUNT: |
| 176 |
ret = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe); |
| 177 |
break; |
| 178 |
case RPC_PIPEFS_UMOUNT: |
| 179 |
rpc_unlink(nn->bl_device_pipe); |
| 180 |
break; |
| 181 |
default: |
| 182 |
ret = -ENOTSUPP; |
| 183 |
break; |
| 184 |
} |
| 185 |
module_put(THIS_MODULE); |
| 186 |
return ret; |
| 187 |
} |
| 188 |
|
| 189 |
static struct notifier_block nfs4blocklayout_block = { |
| 190 |
.notifier_call = rpc_pipefs_event, |
| 191 |
}; |
| 192 |
|
| 193 |
static int nfs4blocklayout_register_net(struct net *net, struct rpc_pipe *pipe) |
| 194 |
{ |
| 195 |
struct super_block *pipefs_sb; |
| 196 |
int ret; |
| 197 |
|
| 198 |
pipefs_sb = rpc_get_sb_net(net); |
| 199 |
if (!pipefs_sb) |
| 200 |
return 0; |
| 201 |
ret = nfs4blocklayout_register_sb(pipefs_sb, pipe); |
| 202 |
rpc_put_sb_net(net); |
| 203 |
return ret; |
| 204 |
} |
| 205 |
|
| 206 |
static void nfs4blocklayout_unregister_net(struct net *net, |
| 207 |
struct rpc_pipe *pipe) |
| 208 |
{ |
| 209 |
struct super_block *pipefs_sb; |
| 210 |
|
| 211 |
pipefs_sb = rpc_get_sb_net(net); |
| 212 |
if (pipefs_sb) { |
| 213 |
rpc_unlink(pipe); |
| 214 |
rpc_put_sb_net(net); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
static int nfs4blocklayout_net_init(struct net *net) |
| 219 |
{ |
| 220 |
struct nfs_net *nn = net_generic(net, nfs_net_id); |
| 221 |
int err; |
| 222 |
|
| 223 |
mutex_init(&nn->bl_mutex); |
| 224 |
init_waitqueue_head(&nn->bl_wq); |
| 225 |
nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0); |
| 226 |
if (IS_ERR(nn->bl_device_pipe)) |
| 227 |
return PTR_ERR(nn->bl_device_pipe); |
| 228 |
err = nfs4blocklayout_register_net(net, nn->bl_device_pipe); |
| 229 |
if (unlikely(err)) |
| 230 |
rpc_destroy_pipe_data(nn->bl_device_pipe); |
| 231 |
return err; |
| 232 |
} |
| 233 |
|
| 234 |
static void nfs4blocklayout_net_exit(struct net *net) |
| 235 |
{ |
| 236 |
struct nfs_net *nn = net_generic(net, nfs_net_id); |
| 237 |
|
| 238 |
nfs4blocklayout_unregister_net(net, nn->bl_device_pipe); |
| 239 |
rpc_destroy_pipe_data(nn->bl_device_pipe); |
| 240 |
nn->bl_device_pipe = NULL; |
| 241 |
} |
| 242 |
|
| 243 |
static struct pernet_operations nfs4blocklayout_net_ops = { |
| 244 |
.init = nfs4blocklayout_net_init, |
| 245 |
.exit = nfs4blocklayout_net_exit, |
| 246 |
}; |
| 247 |
|
| 248 |
int __init bl_init_pipefs(void) |
| 249 |
{ |
| 250 |
int ret; |
| 251 |
|
| 252 |
ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block); |
| 253 |
if (ret) |
| 254 |
goto out; |
| 255 |
ret = register_pernet_subsys(&nfs4blocklayout_net_ops); |
| 256 |
if (ret) |
| 257 |
goto out_unregister_notifier; |
| 258 |
return 0; |
| 259 |
|
| 260 |
out_unregister_notifier: |
| 261 |
rpc_pipefs_notifier_unregister(&nfs4blocklayout_block); |
| 262 |
out: |
| 263 |
return ret; |
| 264 |
} |
| 265 |
|
| 266 |
void bl_cleanup_pipefs(void) |
| 267 |
{ |
| 268 |
rpc_pipefs_notifier_unregister(&nfs4blocklayout_block); |
| 269 |
unregister_pernet_subsys(&nfs4blocklayout_net_ops); |
| 270 |
} |