1/* 2 * linux/fs/lockd/svcproc.c 3 * 4 * Lockd server procedures. We don't implement the NLM_*_RES 5 * procedures because we don't use the async procedures. 6 * 7 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de> 8 */ 9 10#include <linux/types.h> 11#include <linux/time.h> 12#include <linux/slab.h> 13#include <linux/in.h> 14#include <linux/sunrpc/svc.h> 15#include <linux/sunrpc/clnt.h> 16#include <linux/nfsd/nfsd.h> 17#include <linux/lockd/lockd.h> 18#include <linux/lockd/share.h> 19#include <linux/lockd/sm_inter.h> 20 21 22#define NLMDBG_FACILITY NLMDBG_CLIENT 23 24#ifdef CONFIG_LOCKD_V4 25static __be32 26cast_to_nlm(__be32 status, u32 vers) 27{ 28 /* Note: status is assumed to be in network byte order !!! */ 29 if (vers != 4){ 30 switch (status) { 31 case nlm_granted: 32 case nlm_lck_denied: 33 case nlm_lck_denied_nolocks: 34 case nlm_lck_blocked: 35 case nlm_lck_denied_grace_period: 36 case nlm_drop_reply: 37 break; 38 case nlm4_deadlock: 39 status = nlm_lck_denied; 40 break; 41 default: 42 status = nlm_lck_denied_nolocks; 43 } 44 } 45 46 return (status); 47} 48#define cast_status(status) (cast_to_nlm(status, rqstp->rq_vers)) 49#else 50#define cast_status(status) (status) 51#endif 52 53/* 54 * Obtain client and file from arguments 55 */ 56static __be32 57nlmsvc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp, 58 struct nlm_host **hostp, struct nlm_file **filp) 59{ 60 struct nlm_host *host = NULL; 61 struct nlm_file *file = NULL; 62 struct nlm_lock *lock = &argp->lock; 63 __be32 error = 0; 64 65 /* nfsd callbacks must have been installed for this procedure */ 66 if (!nlmsvc_ops) 67 return nlm_lck_denied_nolocks; 68 69 /* Obtain host handle */ 70 if (!(host = nlmsvc_lookup_host(rqstp, lock->caller, lock->len)) 71 || (argp->monitor && nsm_monitor(host) < 0)) 72 goto no_locks; 73 *hostp = host; 74 75 /* Obtain file pointer. Not used by FREE_ALL call. */ 76 if (filp != NULL) { 77 if ((error = nlm_lookup_file(rqstp, &file, &lock->fh)) != 0) 78 goto no_locks; 79 *filp = file; 80 81 /* Set up the missing parts of the file_lock structure */ 82 lock->fl.fl_file = file->f_file; 83 lock->fl.fl_owner = (fl_owner_t) host; 84 lock->fl.fl_lmops = &nlmsvc_lock_operations; 85 } 86 87 return 0; 88 89no_locks: 90 if (host) 91 nlm_release_host(host); 92 if (error) 93 return error; 94 return nlm_lck_denied_nolocks; 95} 96 97/* 98 * NULL: Test for presence of service 99 */ 100static __be32 101nlmsvc_proc_null(struct svc_rqst *rqstp, void *argp, void *resp) 102{ 103 dprintk("lockd: NULL called\n"); 104 return rpc_success; 105} 106 107/* 108 * TEST: Check for conflicting lock 109 */ 110static __be32 111nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_args *argp, 112 struct nlm_res *resp) 113{ 114 struct nlm_host *host; 115 struct nlm_file *file; 116 117 dprintk("lockd: TEST called\n"); 118 resp->cookie = argp->cookie; 119 120 /* Don't accept test requests during grace period */ 121 if (nlmsvc_grace_period) { 122 resp->status = nlm_lck_denied_grace_period; 123 return rpc_success; 124 } 125 126 /* Obtain client and file */ 127 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file))) 128 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success; 129 130 /* Now check for conflicting locks */ 131 resp->status = cast_status(nlmsvc_testlock(rqstp, file, &argp->lock, &resp->lock, &resp->cookie)); 132 if (resp->status == nlm_drop_reply) 133 return rpc_drop_reply; 134 135 dprintk("lockd: TEST status %d vers %d\n", 136 ntohl(resp->status), rqstp->rq_vers); 137 nlm_release_host(host); 138 nlm_release_file(file); 139 return rpc_success; 140} 141 142static __be32 143nlmsvc_proc_lock(struct svc_rqst *rqstp, struct nlm_args *argp, 144 struct nlm_res *resp) 145{ 146 struct nlm_host *host; 147 struct nlm_file *file; 148 149 dprintk("lockd: LOCK called\n"); 150 151 resp->cookie = argp->cookie; 152 153 /* Don't accept new lock requests during grace period */ 154 if (nlmsvc_grace_period && !argp->reclaim) { 155 resp->status = nlm_lck_denied_grace_period; 156 return rpc_success; 157 } 158 159 /* Obtain client and file */ 160 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file))) 161 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success; 162 163 164 /* Now try to lock the file */ 165 resp->status = cast_status(nlmsvc_lock(rqstp, file, &argp->lock, 166 argp->block, &argp->cookie)); 167 if (resp->status == nlm_drop_reply) 168 return rpc_drop_reply; 169 170 dprintk("lockd: LOCK status %d\n", ntohl(resp->status)); 171 nlm_release_host(host); 172 nlm_release_file(file); 173 return rpc_success; 174} 175 176static __be32 177nlmsvc_proc_cancel(struct svc_rqst *rqstp, struct nlm_args *argp, 178 struct nlm_res *resp) 179{ 180 struct nlm_host *host; 181 struct nlm_file *file; 182 183 dprintk("lockd: CANCEL called\n"); 184 185 resp->cookie = argp->cookie; 186 187 /* Don't accept requests during grace period */ 188 if (nlmsvc_grace_period) { 189 resp->status = nlm_lck_denied_grace_period; 190 return rpc_success; 191 } 192 193 /* Obtain client and file */ 194 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file))) 195 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success; 196 197 /* Try to cancel request. */ 198 resp->status = cast_status(nlmsvc_cancel_blocked(file, &argp->lock)); 199 200 dprintk("lockd: CANCEL status %d\n", ntohl(resp->status)); 201 nlm_release_host(host); 202 nlm_release_file(file); 203 return rpc_success; 204} 205 206/* 207 * UNLOCK: release a lock 208 */ 209static __be32 210nlmsvc_proc_unlock(struct svc_rqst *rqstp, struct nlm_args *argp, 211 struct nlm_res *resp) 212{ 213 struct nlm_host *host; 214 struct nlm_file *file; 215 216 dprintk("lockd: UNLOCK called\n"); 217 218 resp->cookie = argp->cookie; 219 220 /* Don't accept new lock requests during grace period */ 221 if (nlmsvc_grace_period) { 222 resp->status = nlm_lck_denied_grace_period; 223 return rpc_success; 224 } 225 226 /* Obtain client and file */ 227 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file))) 228 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success; 229 230 /* Now try to remove the lock */ 231 resp->status = cast_status(nlmsvc_unlock(file, &argp->lock)); 232 233 dprintk("lockd: UNLOCK status %d\n", ntohl(resp->status)); 234 nlm_release_host(host); 235 nlm_release_file(file); 236 return rpc_success; 237} 238 239/* 240 * GRANTED: A server calls us to tell that a process' lock request 241 * was granted 242 */ 243static __be32 244nlmsvc_proc_granted(struct svc_rqst *rqstp, struct nlm_args *argp, 245 struct nlm_res *resp) 246{ 247 resp->cookie = argp->cookie; 248 249 dprintk("lockd: GRANTED called\n"); 250 resp->status = nlmclnt_grant(svc_addr_in(rqstp), &argp->lock); 251 dprintk("lockd: GRANTED status %d\n", ntohl(resp->status)); 252 return rpc_success; 253} 254 255/* 256 * This is the generic lockd callback for async RPC calls 257 */ 258static void nlmsvc_callback_exit(struct rpc_task *task, void *data) 259{ 260 dprintk("lockd: %5u callback returned %d\n", task->tk_pid, 261 -task->tk_status); 262} 263 264static void nlmsvc_callback_release(void *data) 265{ 266 nlm_release_call(data); 267} 268 269static const struct rpc_call_ops nlmsvc_callback_ops = { 270 .rpc_call_done = nlmsvc_callback_exit, 271 .rpc_release = nlmsvc_callback_release, 272}; 273 274/* 275 * `Async' versions of the above service routines. They aren't really, 276 * because we send the callback before the reply proper. I hope this 277 * doesn't break any clients. 278 */ 279static __be32 nlmsvc_callback(struct svc_rqst *rqstp, u32 proc, struct nlm_args *argp, 280 __be32 (*func)(struct svc_rqst *, struct nlm_args *, struct nlm_res *)) 281{ 282 struct nlm_host *host; 283 struct nlm_rqst *call; 284 __be32 stat; 285 286 host = nlmsvc_lookup_host(rqstp, 287 argp->lock.caller, 288 argp->lock.len); 289 if (host == NULL) 290 return rpc_system_err; 291 292 call = nlm_alloc_call(host); 293 if (call == NULL) 294 return rpc_system_err; 295 296 stat = func(rqstp, argp, &call->a_res); 297 if (stat != 0) { 298 nlm_release_call(call); 299 return stat; 300 } 301 302 call->a_flags = RPC_TASK_ASYNC; 303 if (nlm_async_reply(call, proc, &nlmsvc_callback_ops) < 0) 304 return rpc_system_err; 305 return rpc_success; 306} 307 308static __be32 nlmsvc_proc_test_msg(struct svc_rqst *rqstp, struct nlm_args *argp, 309 void *resp) 310{ 311 dprintk("lockd: TEST_MSG called\n"); 312 return nlmsvc_callback(rqstp, NLMPROC_TEST_RES, argp, nlmsvc_proc_test); 313} 314 315static __be32 nlmsvc_proc_lock_msg(struct svc_rqst *rqstp, struct nlm_args *argp, 316 void *resp) 317{ 318 dprintk("lockd: LOCK_MSG called\n"); 319 return nlmsvc_callback(rqstp, NLMPROC_LOCK_RES, argp, nlmsvc_proc_lock); 320} 321 322static __be32 nlmsvc_proc_cancel_msg(struct svc_rqst *rqstp, struct nlm_args *argp, 323 void *resp) 324{ 325 dprintk("lockd: CANCEL_MSG called\n"); 326 return nlmsvc_callback(rqstp, NLMPROC_CANCEL_RES, argp, nlmsvc_proc_cancel); 327} 328 329static __be32 330nlmsvc_proc_unlock_msg(struct svc_rqst *rqstp, struct nlm_args *argp, 331 void *resp) 332{ 333 dprintk("lockd: UNLOCK_MSG called\n"); 334 return nlmsvc_callback(rqstp, NLMPROC_UNLOCK_RES, argp, nlmsvc_proc_unlock); 335} 336 337static __be32 338nlmsvc_proc_granted_msg(struct svc_rqst *rqstp, struct nlm_args *argp, 339 void *resp) 340{ 341 dprintk("lockd: GRANTED_MSG called\n"); 342 return nlmsvc_callback(rqstp, NLMPROC_GRANTED_RES, argp, nlmsvc_proc_granted); 343} 344 345/* 346 * SHARE: create a DOS share or alter existing share. 347 */ 348static __be32 349nlmsvc_proc_share(struct svc_rqst *rqstp, struct nlm_args *argp, 350 struct nlm_res *resp) 351{ 352 struct nlm_host *host; 353 struct nlm_file *file; 354 355 dprintk("lockd: SHARE called\n"); 356 357 resp->cookie = argp->cookie; 358 359 /* Don't accept new lock requests during grace period */ 360 if (nlmsvc_grace_period && !argp->reclaim) { 361 resp->status = nlm_lck_denied_grace_period; 362 return rpc_success; 363 } 364 365 /* Obtain client and file */ 366 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file))) 367 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success; 368 369 /* Now try to create the share */ 370 resp->status = cast_status(nlmsvc_share_file(host, file, argp)); 371 372 dprintk("lockd: SHARE status %d\n", ntohl(resp->status)); 373 nlm_release_host(host); 374 nlm_release_file(file); 375 return rpc_success; 376} 377 378/* 379 * UNSHARE: Release a DOS share. 380 */ 381static __be32 382nlmsvc_proc_unshare(struct svc_rqst *rqstp, struct nlm_args *argp, 383 struct nlm_res *resp) 384{ 385 struct nlm_host *host; 386 struct nlm_file *file; 387 388 dprintk("lockd: UNSHARE called\n"); 389 390 resp->cookie = argp->cookie; 391 392 /* Don't accept requests during grace period */ 393 if (nlmsvc_grace_period) { 394 resp->status = nlm_lck_denied_grace_period; 395 return rpc_success; 396 } 397 398 /* Obtain client and file */ 399 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file))) 400 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success; 401 402 /* Now try to unshare the file */ 403 resp->status = cast_status(nlmsvc_unshare_file(host, file, argp)); 404 405 dprintk("lockd: UNSHARE status %d\n", ntohl(resp->status)); 406 nlm_release_host(host); 407 nlm_release_file(file); 408 return rpc_success; 409} 410 411/* 412 * NM_LOCK: Create an unmonitored lock 413 */ 414static __be32 415nlmsvc_proc_nm_lock(struct svc_rqst *rqstp, struct nlm_args *argp, 416 struct nlm_res *resp) 417{ 418 dprintk("lockd: NM_LOCK called\n"); 419 420 argp->monitor = 0; /* just clean the monitor flag */ 421 return nlmsvc_proc_lock(rqstp, argp, resp); 422} 423 424/* 425 * FREE_ALL: Release all locks and shares held by client 426 */ 427static __be32 428nlmsvc_proc_free_all(struct svc_rqst *rqstp, struct nlm_args *argp, 429 void *resp) 430{ 431 struct nlm_host *host; 432 433 /* Obtain client */ 434 if (nlmsvc_retrieve_args(rqstp, argp, &host, NULL)) 435 return rpc_success; 436 437 nlmsvc_free_host_resources(host); 438 nlm_release_host(host); 439 return rpc_success; 440} 441 442/* 443 * SM_NOTIFY: private callback from statd (not part of official NLM proto) 444 */ 445static __be32 446nlmsvc_proc_sm_notify(struct svc_rqst *rqstp, struct nlm_reboot *argp, 447 void *resp) 448{ 449 struct sockaddr_in saddr; 450 451 memcpy(&saddr, svc_addr_in(rqstp), sizeof(saddr)); 452 453 dprintk("lockd: SM_NOTIFY called\n"); 454 if (saddr.sin_addr.s_addr != htonl(INADDR_LOOPBACK) 455 || ntohs(saddr.sin_port) >= 1024) { 456 char buf[RPC_MAX_ADDRBUFLEN]; 457 printk(KERN_WARNING "lockd: rejected NSM callback from %s\n", 458 svc_print_addr(rqstp, buf, sizeof(buf))); 459 return rpc_system_err; 460 } 461 462 /* Obtain the host pointer for this NFS server and try to 463 * reclaim all locks we hold on this server. 464 */ 465 memset(&saddr, 0, sizeof(saddr)); 466 saddr.sin_addr.s_addr = argp->addr; 467 nlm_host_rebooted(&saddr, argp->mon, argp->len, argp->state); 468 469 return rpc_success; 470} 471 472/* 473 * client sent a GRANTED_RES, let's remove the associated block 474 */ 475static __be32 476nlmsvc_proc_granted_res(struct svc_rqst *rqstp, struct nlm_res *argp, 477 void *resp) 478{ 479 if (!nlmsvc_ops) 480 return rpc_success; 481 482 dprintk("lockd: GRANTED_RES called\n"); 483 484 nlmsvc_grant_reply(&argp->cookie, argp->status); 485 return rpc_success; 486} 487 488/* 489 * NLM Server procedures. 490 */ 491 492#define nlmsvc_encode_norep nlmsvc_encode_void 493#define nlmsvc_decode_norep nlmsvc_decode_void 494#define nlmsvc_decode_testres nlmsvc_decode_void 495#define nlmsvc_decode_lockres nlmsvc_decode_void 496#define nlmsvc_decode_unlockres nlmsvc_decode_void 497#define nlmsvc_decode_cancelres nlmsvc_decode_void 498#define nlmsvc_decode_grantedres nlmsvc_decode_void 499 500#define nlmsvc_proc_none nlmsvc_proc_null 501#define nlmsvc_proc_test_res nlmsvc_proc_null 502#define nlmsvc_proc_lock_res nlmsvc_proc_null 503#define nlmsvc_proc_cancel_res nlmsvc_proc_null 504#define nlmsvc_proc_unlock_res nlmsvc_proc_null 505 506struct nlm_void { int dummy; }; 507 508#define PROC(name, xargt, xrest, argt, rest, respsize) \ 509 { .pc_func = (svc_procfunc) nlmsvc_proc_##name, \ 510 .pc_decode = (kxdrproc_t) nlmsvc_decode_##xargt, \ 511 .pc_encode = (kxdrproc_t) nlmsvc_encode_##xrest, \ 512 .pc_release = NULL, \ 513 .pc_argsize = sizeof(struct nlm_##argt), \ 514 .pc_ressize = sizeof(struct nlm_##rest), \ 515 .pc_xdrressize = respsize, \ 516 } 517 518#define Ck (1+XDR_QUADLEN(NLM_MAXCOOKIELEN)) /* cookie */ 519#define St 1 /* status */ 520#define No (1+1024/4) /* Net Obj */ 521#define Rg 2 /* range - offset + size */ 522 523struct svc_procedure nlmsvc_procedures[] = { 524 PROC(null, void, void, void, void, 1), 525 PROC(test, testargs, testres, args, res, Ck+St+2+No+Rg), 526 PROC(lock, lockargs, res, args, res, Ck+St), 527 PROC(cancel, cancargs, res, args, res, Ck+St), 528 PROC(unlock, unlockargs, res, args, res, Ck+St), 529 PROC(granted, testargs, res, args, res, Ck+St), 530 PROC(test_msg, testargs, norep, args, void, 1), 531 PROC(lock_msg, lockargs, norep, args, void, 1), 532 PROC(cancel_msg, cancargs, norep, args, void, 1), 533 PROC(unlock_msg, unlockargs, norep, args, void, 1), 534 PROC(granted_msg, testargs, norep, args, void, 1), 535 PROC(test_res, testres, norep, res, void, 1), 536 PROC(lock_res, lockres, norep, res, void, 1), 537 PROC(cancel_res, cancelres, norep, res, void, 1), 538 PROC(unlock_res, unlockres, norep, res, void, 1), 539 PROC(granted_res, res, norep, res, void, 1), 540 /* statd callback */ 541 PROC(sm_notify, reboot, void, reboot, void, 1), 542 PROC(none, void, void, void, void, 1), 543 PROC(none, void, void, void, void, 1), 544 PROC(none, void, void, void, void, 1), 545 PROC(share, shareargs, shareres, args, res, Ck+St+1), 546 PROC(unshare, shareargs, shareres, args, res, Ck+St+1), 547 PROC(nm_lock, lockargs, res, args, res, Ck+St), 548 PROC(free_all, notify, void, args, void, 0), 549 550}; 551