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/smp_lock.h> 14 #include <linux/lockd/lockd.h> 15 #include <linux/lockd/share.h> 16 17 #define NLMDBG_FACILITY NLMDBG_CLIENT 18 19 #ifdef CONFIG_LOCKD_V4 20 static __be32 21 cast_to_nlm(__be32 status, u32 vers) 22 { 23 /* Note: status is assumed to be in network byte order !!! */ 24 if (vers != 4){ 25 switch (status) { 26 case nlm_granted: 27 case nlm_lck_denied: 28 case nlm_lck_denied_nolocks: 29 case nlm_lck_blocked: 30 case nlm_lck_denied_grace_period: 31 case nlm_drop_reply: 32 break; 33 case nlm4_deadlock: 34 status = nlm_lck_denied; 35 break; 36 default: 37 status = nlm_lck_denied_nolocks; 38 } 39 } 40 41 return (status); 42 } 43 #define cast_status(status) (cast_to_nlm(status, rqstp->rq_vers)) 44 #else 45 #define cast_status(status) (status) 46 #endif 47 48 /* 49 * Obtain client and file from arguments 50 */ 51 static __be32 52 nlmsvc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp, 53 struct nlm_host **hostp, struct nlm_file **filp) 54 { 55 struct nlm_host *host = NULL; 56 struct nlm_file *file = NULL; 57 struct nlm_lock *lock = &argp->lock; 58 __be32 error = 0; 59 60 /* nfsd callbacks must have been installed for this procedure */ 61 if (!nlmsvc_ops) 62 return nlm_lck_denied_nolocks; 63 64 /* Obtain host handle */ 65 if (!(host = nlmsvc_lookup_host(rqstp, lock->caller, lock->len)) 66 || (argp->monitor && nsm_monitor(host) < 0)) 67 goto no_locks; 68 *hostp = host; 69 70 /* Obtain file pointer. Not used by FREE_ALL call. */ 71 if (filp != NULL) { 72 if ((error = nlm_lookup_file(rqstp, &file, &lock->fh)) != 0) 73 goto no_locks; 74 *filp = file; 75 76 /* Set up the missing parts of the file_lock structure */ 77 lock->fl.fl_file = file->f_file; 78 lock->fl.fl_owner = (fl_owner_t) host; 79 lock->fl.fl_lmops = &nlmsvc_lock_operations; 80 } 81 82 return 0; 83 84 no_locks: 85 nlm_release_host(host); 86 if (error) 87 return error; 88 return nlm_lck_denied_nolocks; 89 } 90 91 /* 92 * NULL: Test for presence of service 93 */ 94 static __be32 95 nlmsvc_proc_null(struct svc_rqst *rqstp, void *argp, void *resp) 96 { 97 dprintk("lockd: NULL called\n"); 98 return rpc_success; 99 } 100 101 /* 102 * TEST: Check for conflicting lock 103 */ 104 static __be32 105 nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_args *argp, 106 struct nlm_res *resp) 107 { 108 struct nlm_host *host; 109 struct nlm_file *file; 110 __be32 rc = rpc_success; 111 112 dprintk("lockd: TEST called\n"); 113 resp->cookie = argp->cookie; 114 115 /* Obtain client and file */ 116 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file))) 117 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success; 118 119 /* Now check for conflicting locks */ 120 resp->status = cast_status(nlmsvc_testlock(rqstp, file, host, &argp->lock, &resp->lock, &resp->cookie)); 121 if (resp->status == nlm_drop_reply) 122 rc = rpc_drop_reply; 123 else 124 dprintk("lockd: TEST status %d vers %d\n", 125 ntohl(resp->status), rqstp->rq_vers); 126 127 nlm_release_host(host); 128 nlm_release_file(file); 129 return rc; 130 } 131 132 static __be32 133 nlmsvc_proc_lock(struct svc_rqst *rqstp, struct nlm_args *argp, 134 struct nlm_res *resp) 135 { 136 struct nlm_host *host; 137 struct nlm_file *file; 138 __be32 rc = rpc_success; 139 140 dprintk("lockd: LOCK called\n"); 141 142 resp->cookie = argp->cookie; 143 144 /* Obtain client and file */ 145 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file))) 146 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success; 147 148 #if 0 149 /* If supplied state doesn't match current state, we assume it's 150 * an old request that time-warped somehow. Any error return would 151 * do in this case because it's irrelevant anyway. 152 * 153 * NB: We don't retrieve the remote host's state yet. 154 */ 155 if (host->h_nsmstate && host->h_nsmstate != argp->state) { 156 resp->status = nlm_lck_denied_nolocks; 157 } else 158 #endif 159 160 /* Now try to lock the file */ 161 resp->status = cast_status(nlmsvc_lock(rqstp, file, host, &argp->lock, 162 argp->block, &argp->cookie, 163 argp->reclaim)); 164 if (resp->status == nlm_drop_reply) 165 rc = rpc_drop_reply; 166 else 167 dprintk("lockd: LOCK status %d\n", ntohl(resp->status)); 168 169 nlm_release_host(host); 170 nlm_release_file(file); 171 return rc; 172 } 173 174 static __be32 175 nlmsvc_proc_cancel(struct svc_rqst *rqstp, struct nlm_args *argp, 176 struct nlm_res *resp) 177 { 178 struct nlm_host *host; 179 struct nlm_file *file; 180 181 dprintk("lockd: CANCEL called\n"); 182 183 resp->cookie = argp->cookie; 184 185 /* Don't accept requests during grace period */ 186 if (locks_in_grace()) { 187 resp->status = nlm_lck_denied_grace_period; 188 return rpc_success; 189 } 190 191 /* Obtain client and file */ 192 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file))) 193 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success; 194 195 /* Try to cancel request. */ 196 resp->status = cast_status(nlmsvc_cancel_blocked(file, &argp->lock)); 197 198 dprintk("lockd: CANCEL status %d\n", ntohl(resp->status)); 199 nlm_release_host(host); 200 nlm_release_file(file); 201 return rpc_success; 202 } 203 204 /* 205 * UNLOCK: release a lock 206 */ 207 static __be32 208 nlmsvc_proc_unlock(struct svc_rqst *rqstp, struct nlm_args *argp, 209 struct nlm_res *resp) 210 { 211 struct nlm_host *host; 212 struct nlm_file *file; 213 214 dprintk("lockd: UNLOCK called\n"); 215 216 resp->cookie = argp->cookie; 217 218 /* Don't accept new lock requests during grace period */ 219 if (locks_in_grace()) { 220 resp->status = nlm_lck_denied_grace_period; 221 return rpc_success; 222 } 223 224 /* Obtain client and file */ 225 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file))) 226 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success; 227 228 /* Now try to remove the lock */ 229 resp->status = cast_status(nlmsvc_unlock(file, &argp->lock)); 230 231 dprintk("lockd: UNLOCK status %d\n", ntohl(resp->status)); 232 nlm_release_host(host); 233 nlm_release_file(file); 234 return rpc_success; 235 } 236 237 /* 238 * GRANTED: A server calls us to tell that a process' lock request 239 * was granted 240 */ 241 static __be32 242 nlmsvc_proc_granted(struct svc_rqst *rqstp, struct nlm_args *argp, 243 struct nlm_res *resp) 244 { 245 resp->cookie = argp->cookie; 246 247 dprintk("lockd: GRANTED called\n"); 248 resp->status = nlmclnt_grant(svc_addr(rqstp), &argp->lock); 249 dprintk("lockd: GRANTED status %d\n", ntohl(resp->status)); 250 return rpc_success; 251 } 252 253 /* 254 * This is the generic lockd callback for async RPC calls 255 */ 256 static void nlmsvc_callback_exit(struct rpc_task *task, void *data) 257 { 258 dprintk("lockd: %5u callback returned %d\n", task->tk_pid, 259 -task->tk_status); 260 } 261 262 static void nlmsvc_callback_release(void *data) 263 { 264 lock_kernel(); 265 nlm_release_call(data); 266 unlock_kernel(); 267 } 268 269 static 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 */ 279 static __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 308 static __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 315 static __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 322 static __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 329 static __be32 330 nlmsvc_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 337 static __be32 338 nlmsvc_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 */ 348 static __be32 349 nlmsvc_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 (locks_in_grace() && !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 */ 381 static __be32 382 nlmsvc_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 (locks_in_grace()) { 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 */ 414 static __be32 415 nlmsvc_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 */ 427 static __be32 428 nlmsvc_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 */ 445 static __be32 446 nlmsvc_proc_sm_notify(struct svc_rqst *rqstp, struct nlm_reboot *argp, 447 void *resp) 448 { 449 dprintk("lockd: SM_NOTIFY called\n"); 450 451 if (!nlm_privileged_requester(rqstp)) { 452 char buf[RPC_MAX_ADDRBUFLEN]; 453 printk(KERN_WARNING "lockd: rejected NSM callback from %s\n", 454 svc_print_addr(rqstp, buf, sizeof(buf))); 455 return rpc_system_err; 456 } 457 458 nlm_host_rebooted(argp); 459 return rpc_success; 460 } 461 462 /* 463 * client sent a GRANTED_RES, let's remove the associated block 464 */ 465 static __be32 466 nlmsvc_proc_granted_res(struct svc_rqst *rqstp, struct nlm_res *argp, 467 void *resp) 468 { 469 if (!nlmsvc_ops) 470 return rpc_success; 471 472 dprintk("lockd: GRANTED_RES called\n"); 473 474 nlmsvc_grant_reply(&argp->cookie, argp->status); 475 return rpc_success; 476 } 477 478 /* 479 * NLM Server procedures. 480 */ 481 482 #define nlmsvc_encode_norep nlmsvc_encode_void 483 #define nlmsvc_decode_norep nlmsvc_decode_void 484 #define nlmsvc_decode_testres nlmsvc_decode_void 485 #define nlmsvc_decode_lockres nlmsvc_decode_void 486 #define nlmsvc_decode_unlockres nlmsvc_decode_void 487 #define nlmsvc_decode_cancelres nlmsvc_decode_void 488 #define nlmsvc_decode_grantedres nlmsvc_decode_void 489 490 #define nlmsvc_proc_none nlmsvc_proc_null 491 #define nlmsvc_proc_test_res nlmsvc_proc_null 492 #define nlmsvc_proc_lock_res nlmsvc_proc_null 493 #define nlmsvc_proc_cancel_res nlmsvc_proc_null 494 #define nlmsvc_proc_unlock_res nlmsvc_proc_null 495 496 struct nlm_void { int dummy; }; 497 498 #define PROC(name, xargt, xrest, argt, rest, respsize) \ 499 { .pc_func = (svc_procfunc) nlmsvc_proc_##name, \ 500 .pc_decode = (kxdrproc_t) nlmsvc_decode_##xargt, \ 501 .pc_encode = (kxdrproc_t) nlmsvc_encode_##xrest, \ 502 .pc_release = NULL, \ 503 .pc_argsize = sizeof(struct nlm_##argt), \ 504 .pc_ressize = sizeof(struct nlm_##rest), \ 505 .pc_xdrressize = respsize, \ 506 } 507 508 #define Ck (1+XDR_QUADLEN(NLM_MAXCOOKIELEN)) /* cookie */ 509 #define St 1 /* status */ 510 #define No (1+1024/4) /* Net Obj */ 511 #define Rg 2 /* range - offset + size */ 512 513 struct svc_procedure nlmsvc_procedures[] = { 514 PROC(null, void, void, void, void, 1), 515 PROC(test, testargs, testres, args, res, Ck+St+2+No+Rg), 516 PROC(lock, lockargs, res, args, res, Ck+St), 517 PROC(cancel, cancargs, res, args, res, Ck+St), 518 PROC(unlock, unlockargs, res, args, res, Ck+St), 519 PROC(granted, testargs, res, args, res, Ck+St), 520 PROC(test_msg, testargs, norep, args, void, 1), 521 PROC(lock_msg, lockargs, norep, args, void, 1), 522 PROC(cancel_msg, cancargs, norep, args, void, 1), 523 PROC(unlock_msg, unlockargs, norep, args, void, 1), 524 PROC(granted_msg, testargs, norep, args, void, 1), 525 PROC(test_res, testres, norep, res, void, 1), 526 PROC(lock_res, lockres, norep, res, void, 1), 527 PROC(cancel_res, cancelres, norep, res, void, 1), 528 PROC(unlock_res, unlockres, norep, res, void, 1), 529 PROC(granted_res, res, norep, res, void, 1), 530 /* statd callback */ 531 PROC(sm_notify, reboot, void, reboot, void, 1), 532 PROC(none, void, void, void, void, 1), 533 PROC(none, void, void, void, void, 1), 534 PROC(none, void, void, void, void, 1), 535 PROC(share, shareargs, shareres, args, res, Ck+St+1), 536 PROC(unshare, shareargs, shareres, args, res, Ck+St+1), 537 PROC(nm_lock, lockargs, res, args, res, Ck+St), 538 PROC(free_all, notify, void, args, void, 0), 539 540 }; 541