Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/lockd/mon.c |
| 3 | * |
| 4 | * The kernel statd client. |
| 5 | * |
| 6 | * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/utsname.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/sunrpc/clnt.h> |
| 13 | #include <linux/sunrpc/svc.h> |
| 14 | #include <linux/lockd/lockd.h> |
| 15 | #include <linux/lockd/sm_inter.h> |
| 16 | |
| 17 | |
| 18 | #define NLMDBG_FACILITY NLMDBG_MONITOR |
| 19 | |
| 20 | static struct rpc_clnt * nsm_create(void); |
| 21 | |
| 22 | static struct rpc_program nsm_program; |
| 23 | |
| 24 | /* |
| 25 | * Local NSM state |
| 26 | */ |
Olaf Kirch | 460f5ca | 2006-10-04 02:16:03 -0700 | [diff] [blame] | 27 | int nsm_local_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * Common procedure for SM_MON/SM_UNMON calls |
| 31 | */ |
| 32 | static int |
Olaf Kirch | 9502c52 | 2006-10-04 02:15:56 -0700 | [diff] [blame] | 33 | nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
| 35 | struct rpc_clnt *clnt; |
| 36 | int status; |
| 37 | struct nsm_args args; |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 38 | struct rpc_message msg = { |
| 39 | .rpc_argp = &args, |
| 40 | .rpc_resp = res, |
| 41 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | clnt = nsm_create(); |
| 44 | if (IS_ERR(clnt)) { |
| 45 | status = PTR_ERR(clnt); |
| 46 | goto out; |
| 47 | } |
| 48 | |
Olaf Kirch | 9502c52 | 2006-10-04 02:15:56 -0700 | [diff] [blame] | 49 | memset(&args, 0, sizeof(args)); |
Olaf Kirch | abd1f50 | 2006-10-04 02:16:01 -0700 | [diff] [blame] | 50 | args.mon_name = nsm->sm_name; |
Olaf Kirch | 9502c52 | 2006-10-04 02:15:56 -0700 | [diff] [blame] | 51 | args.addr = nsm->sm_addr.sin_addr.s_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | args.prog = NLM_PROGRAM; |
Olaf Kirch | 9502c52 | 2006-10-04 02:15:56 -0700 | [diff] [blame] | 53 | args.vers = 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | args.proc = NLMPROC_NSM_NOTIFY; |
| 55 | memset(res, 0, sizeof(*res)); |
| 56 | |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 57 | msg.rpc_proc = &clnt->cl_procinfo[proc]; |
| 58 | status = rpc_call_sync(clnt, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | if (status < 0) |
| 60 | printk(KERN_DEBUG "nsm_mon_unmon: rpc failed, status=%d\n", |
| 61 | status); |
| 62 | else |
| 63 | status = 0; |
| 64 | out: |
| 65 | return status; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Set up monitoring of a remote host |
| 70 | */ |
| 71 | int |
| 72 | nsm_monitor(struct nlm_host *host) |
| 73 | { |
Olaf Kirch | 8dead0d | 2006-10-04 02:15:53 -0700 | [diff] [blame] | 74 | struct nsm_handle *nsm = host->h_nsmhandle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | struct nsm_res res; |
| 76 | int status; |
| 77 | |
| 78 | dprintk("lockd: nsm_monitor(%s)\n", host->h_name); |
Olaf Kirch | 8dead0d | 2006-10-04 02:15:53 -0700 | [diff] [blame] | 79 | BUG_ON(nsm == NULL); |
| 80 | |
| 81 | if (nsm->sm_monitored) |
Olaf Kirch | 977faf3 | 2006-10-04 02:15:51 -0700 | [diff] [blame] | 82 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Olaf Kirch | 9502c52 | 2006-10-04 02:15:56 -0700 | [diff] [blame] | 84 | status = nsm_mon_unmon(nsm, SM_MON, &res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | if (status < 0 || res.status != 0) |
| 87 | printk(KERN_NOTICE "lockd: cannot monitor %s\n", host->h_name); |
| 88 | else |
Olaf Kirch | 8dead0d | 2006-10-04 02:15:53 -0700 | [diff] [blame] | 89 | nsm->sm_monitored = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | return status; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Cease to monitor remote host |
| 95 | */ |
| 96 | int |
| 97 | nsm_unmonitor(struct nlm_host *host) |
| 98 | { |
Olaf Kirch | 8dead0d | 2006-10-04 02:15:53 -0700 | [diff] [blame] | 99 | struct nsm_handle *nsm = host->h_nsmhandle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | struct nsm_res res; |
Olaf Kirch | 977faf3 | 2006-10-04 02:15:51 -0700 | [diff] [blame] | 101 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Olaf Kirch | 8dead0d | 2006-10-04 02:15:53 -0700 | [diff] [blame] | 103 | if (nsm == NULL) |
Olaf Kirch | 977faf3 | 2006-10-04 02:15:51 -0700 | [diff] [blame] | 104 | return 0; |
Olaf Kirch | 8dead0d | 2006-10-04 02:15:53 -0700 | [diff] [blame] | 105 | host->h_nsmhandle = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Olaf Kirch | 9502c52 | 2006-10-04 02:15:56 -0700 | [diff] [blame] | 107 | if (atomic_read(&nsm->sm_count) == 1 |
| 108 | && nsm->sm_monitored && !nsm->sm_sticky) { |
| 109 | dprintk("lockd: nsm_unmonitor(%s)\n", host->h_name); |
| 110 | |
| 111 | status = nsm_mon_unmon(nsm, SM_UNMON, &res); |
Olaf Kirch | 977faf3 | 2006-10-04 02:15:51 -0700 | [diff] [blame] | 112 | if (status < 0) |
Olaf Kirch | 9502c52 | 2006-10-04 02:15:56 -0700 | [diff] [blame] | 113 | printk(KERN_NOTICE "lockd: cannot unmonitor %s\n", |
| 114 | host->h_name); |
| 115 | else |
| 116 | nsm->sm_monitored = 0; |
Olaf Kirch | 977faf3 | 2006-10-04 02:15:51 -0700 | [diff] [blame] | 117 | } |
Olaf Kirch | 8dead0d | 2006-10-04 02:15:53 -0700 | [diff] [blame] | 118 | nsm_release(nsm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | return status; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Create NSM client for the local host |
| 124 | */ |
| 125 | static struct rpc_clnt * |
| 126 | nsm_create(void) |
| 127 | { |
Chuck Lever | e1ec789 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 128 | struct sockaddr_in sin = { |
| 129 | .sin_family = AF_INET, |
| 130 | .sin_addr.s_addr = htonl(INADDR_LOOPBACK), |
| 131 | .sin_port = 0, |
| 132 | }; |
| 133 | struct rpc_create_args args = { |
| 134 | .protocol = IPPROTO_UDP, |
| 135 | .address = (struct sockaddr *)&sin, |
| 136 | .addrsize = sizeof(sin), |
| 137 | .servername = "localhost", |
| 138 | .program = &nsm_program, |
| 139 | .version = SM_VERSION, |
| 140 | .authflavor = RPC_AUTH_NULL, |
| 141 | .flags = (RPC_CLNT_CREATE_ONESHOT), |
| 142 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Chuck Lever | e1ec789 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 144 | return rpc_create(&args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* |
| 148 | * XDR functions for NSM. |
| 149 | */ |
| 150 | |
Al Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 151 | static __be32 * |
| 152 | xdr_encode_common(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
Olaf Kirch | abd1f50 | 2006-10-04 02:16:01 -0700 | [diff] [blame] | 154 | char buffer[20], *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | /* |
| 157 | * Use the dotted-quad IP address of the remote host as |
| 158 | * identifier. Linux statd always looks up the canonical |
| 159 | * hostname first for whatever remote hostname it receives, |
| 160 | * so this works alright. |
| 161 | */ |
Olaf Kirch | abd1f50 | 2006-10-04 02:16:01 -0700 | [diff] [blame] | 162 | if (nsm_use_hostnames) { |
| 163 | name = argp->mon_name; |
| 164 | } else { |
| 165 | sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(argp->addr)); |
| 166 | name = buffer; |
| 167 | } |
| 168 | if (!(p = xdr_encode_string(p, name)) |
Serge E. Hallyn | e9ff399 | 2006-10-02 02:18:11 -0700 | [diff] [blame] | 169 | || !(p = xdr_encode_string(p, utsname()->nodename))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return ERR_PTR(-EIO); |
| 171 | *p++ = htonl(argp->prog); |
| 172 | *p++ = htonl(argp->vers); |
| 173 | *p++ = htonl(argp->proc); |
| 174 | |
| 175 | return p; |
| 176 | } |
| 177 | |
| 178 | static int |
Al Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 179 | xdr_encode_mon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
| 181 | p = xdr_encode_common(rqstp, p, argp); |
| 182 | if (IS_ERR(p)) |
| 183 | return PTR_ERR(p); |
Olaf Kirch | 9502c52 | 2006-10-04 02:15:56 -0700 | [diff] [blame] | 184 | |
| 185 | /* Surprise - there may even be room for an IPv6 address now */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | *p++ = argp->addr; |
Olaf Kirch | 9502c52 | 2006-10-04 02:15:56 -0700 | [diff] [blame] | 187 | *p++ = 0; |
| 188 | *p++ = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | *p++ = 0; |
| 190 | rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p); |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static int |
Al Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 195 | xdr_encode_unmon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
| 197 | p = xdr_encode_common(rqstp, p, argp); |
| 198 | if (IS_ERR(p)) |
| 199 | return PTR_ERR(p); |
| 200 | rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p); |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int |
Al Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 205 | xdr_decode_stat_res(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | { |
| 207 | resp->status = ntohl(*p++); |
| 208 | resp->state = ntohl(*p++); |
| 209 | dprintk("nsm: xdr_decode_stat_res status %d state %d\n", |
| 210 | resp->status, resp->state); |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static int |
Al Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 215 | xdr_decode_stat(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
| 217 | resp->state = ntohl(*p++); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN)) |
| 222 | #define SM_my_id_sz (3+1+SM_my_name_sz) |
| 223 | #define SM_mon_id_sz (1+XDR_QUADLEN(20)+SM_my_id_sz) |
| 224 | #define SM_mon_sz (SM_mon_id_sz+4) |
| 225 | #define SM_monres_sz 2 |
| 226 | #define SM_unmonres_sz 1 |
| 227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | static struct rpc_procinfo nsm_procedures[] = { |
| 229 | [SM_MON] = { |
| 230 | .p_proc = SM_MON, |
| 231 | .p_encode = (kxdrproc_t) xdr_encode_mon, |
| 232 | .p_decode = (kxdrproc_t) xdr_decode_stat_res, |
Chuck Lever | 2bea90d | 2007-03-29 16:47:53 -0400 | [diff] [blame^] | 233 | .p_arglen = SM_mon_sz, |
| 234 | .p_replen = SM_monres_sz, |
Chuck Lever | cc0175c | 2006-03-20 13:44:22 -0500 | [diff] [blame] | 235 | .p_statidx = SM_MON, |
| 236 | .p_name = "MONITOR", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | }, |
| 238 | [SM_UNMON] = { |
| 239 | .p_proc = SM_UNMON, |
| 240 | .p_encode = (kxdrproc_t) xdr_encode_unmon, |
| 241 | .p_decode = (kxdrproc_t) xdr_decode_stat, |
Chuck Lever | 2bea90d | 2007-03-29 16:47:53 -0400 | [diff] [blame^] | 242 | .p_arglen = SM_mon_id_sz, |
| 243 | .p_replen = SM_unmonres_sz, |
Chuck Lever | cc0175c | 2006-03-20 13:44:22 -0500 | [diff] [blame] | 244 | .p_statidx = SM_UNMON, |
| 245 | .p_name = "UNMONITOR", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | }, |
| 247 | }; |
| 248 | |
| 249 | static struct rpc_version nsm_version1 = { |
Tobias Klauser | e8c96f8 | 2006-03-24 03:15:34 -0800 | [diff] [blame] | 250 | .number = 1, |
| 251 | .nrprocs = ARRAY_SIZE(nsm_procedures), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | .procs = nsm_procedures |
| 253 | }; |
| 254 | |
| 255 | static struct rpc_version * nsm_version[] = { |
| 256 | [1] = &nsm_version1, |
| 257 | }; |
| 258 | |
| 259 | static struct rpc_stat nsm_stats; |
| 260 | |
| 261 | static struct rpc_program nsm_program = { |
| 262 | .name = "statd", |
| 263 | .number = SM_PROGRAM, |
Tobias Klauser | e8c96f8 | 2006-03-24 03:15:34 -0800 | [diff] [blame] | 264 | .nrvers = ARRAY_SIZE(nsm_version), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | .version = nsm_version, |
| 266 | .stats = &nsm_stats |
| 267 | }; |