| 1 | |
| 2 | |
| 3 | * linux/fs/lockd/svclock.c
|
| 4 | *
|
| 5 | * Handling of server-side locks, mostly of the blocked variety.
|
| 6 | * This is the ugliest part of lockd because we tread on very thin ice.
|
| 7 | * GRANT and CANCEL calls may get stuck, meet in mid-flight, etc.
|
| 8 | * IMNSHO introducing the grant callback into the NLM protocol was one
|
| 9 | * of the worst ideas Sun ever had. Except maybe for the idea of doing
|
| 10 | * NFS file locking at all.
|
| 11 | *
|
| 12 | * I'm trying hard to avoid race conditions by protecting most accesses
|
| 13 | * to a file's list of blocked locks through a semaphore. The global
|
| 14 | * list of blocked locks is not protected in this fashion however.
|
| 15 | * Therefore, some functions (such as the RPC callback for the async grant
|
| 16 | * call) move blocked locks towards the head of the list *while some other
|
| 17 | * process might be traversing it*. This should not be a problem in
|
| 18 | * practice, because this will only cause functions traversing the list
|
| 19 | * to visit some blocks twice.
|
| 20 | *
|
| 21 | * Copyright (C) 1996, Olaf Kirch <[email protected]>
|
| 22 |
|
| 23 |
|
| 24 | #include <linux/types.h>
|
| 25 | #include <linux/slab.h>
|
| 26 | #include <linux/errno.h>
|
| 27 | #include <linux/kernel.h>
|
| 28 | #include <linux/sched.h>
|
| 29 | #include <linux/sunrpc/clnt.h>
|
| 30 | #include <linux/sunrpc/svc_xprt.h>
|
| 31 |
|
| 32 | #include "lockd.h"
|
| 33 |
|
| 34 | #define NLMDBG_FACILITY NLMDBG_SVCLOCK
|
| 35 |
|
| 36 | static void nlmsvc_release_block(struct nlm_block *block);
|
| 37 | static void nlmsvc_insert_block(struct nlm_block *block, unsigned long);
|
| 38 | static void nlmsvc_remove_block(struct nlm_block *block);
|
| 39 |
|
| 40 | static int nlmsvc_setgrantargs(struct nlm_rqst *call, struct lockd_lock *lock);
|
| 41 | static void nlmsvc_freegrantargs(struct nlm_rqst *call);
|
| 42 | static const struct rpc_call_ops nlmsvc_grant_ops;
|
| 43 |
|
| 44 | |
| 45 | * The list of blocked locks to retry
|
| 46 |
|
| 47 | static LIST_HEAD(nlm_blocked);
|
| 48 | static DEFINE_SPINLOCK(nlm_blocked_lock);
|
| 49 |
|
| 50 | |
| 51 | * Insert a blocked lock into the global list
|
| 52 |
|
| 53 | static void
|
| 54 | nlmsvc_insert_block_locked(struct nlm_block *block, unsigned long when)
|
| 55 | {
|
| 56 | struct nlm_block *b;
|
| 57 | struct list_head *pos;
|
| 58 |
|
| 59 | dprintk("lockd: nlmsvc_insert_block(%p, %ld)\n", block, when);
|
| 60 | if (list_empty(&block->b_list)) {
|
| 61 | kref_get(&block->b_count);
|
| 62 | } else {
|
| 63 | list_del_init(&block->b_list);
|
| 64 | }
|
| 65 |
|
| 66 | pos = &nlm_blocked;
|
| 67 | if (when != NLM_NEVER) {
|
| 68 | if ((when += jiffies) == NLM_NEVER)
|
| 69 | when ++;
|
| 70 | list_for_each(pos, &nlm_blocked) {
|
| 71 | b = list_entry(pos, struct nlm_block, b_list);
|
| 72 | if (time_after(b->b_when,when) || b->b_when == NLM_NEVER)
|
| 73 | break;
|
| 74 | }
|
| 75 | |
| 76 | * so we will be adding to the end of the list - good
|
| 77 |
|
| 78 | }
|
| 79 |
|
| 80 | list_add_tail(&block->b_list, pos);
|
| 81 | block->b_when = when;
|
| 82 | }
|
| 83 |
|
| 84 | static void nlmsvc_insert_block(struct nlm_block *block, unsigned long when)
|
| 85 | {
|
| 86 | spin_lock(&nlm_blocked_lock);
|
| 87 | nlmsvc_insert_block_locked(block, when);
|
| 88 | spin_unlock(&nlm_blocked_lock);
|
| 89 | }
|
| 90 |
|
| 91 | |
| 92 | * Remove a block from the global list
|
| 93 |
|
| 94 | static inline void
|
| 95 | nlmsvc_remove_block(struct nlm_block *block)
|
| 96 | {
|
| 97 | spin_lock(&nlm_blocked_lock);
|
| 98 | if (!list_empty(&block->b_list)) {
|
| 99 | list_del_init(&block->b_list);
|
| 100 | spin_unlock(&nlm_blocked_lock);
|
| 101 | nlmsvc_release_block(block);
|
| 102 | return;
|
| 103 | }
|
| 104 | spin_unlock(&nlm_blocked_lock);
|
| 105 | }
|
| 106 |
|
| 107 | |
| 108 | * Find a block for a given lock
|
| 109 |
|
| 110 | static struct nlm_block *
|
| 111 | nlmsvc_lookup_block(struct nlm_file *file, struct lockd_lock *lock)
|
| 112 | {
|
| 113 | struct nlm_block *block;
|
| 114 | struct file_lock *fl;
|
| 115 |
|
| 116 | dprintk("lockd: nlmsvc_lookup_block f=%p pd=%d %Ld-%Ld ty=%d\n",
|
| 117 | file, lock->fl.c.flc_pid,
|
| 118 | (long long)lock->fl.fl_start,
|
| 119 | (long long)lock->fl.fl_end,
|
| 120 | lock->fl.c.flc_type);
|
| 121 | spin_lock(&nlm_blocked_lock);
|
| 122 | list_for_each_entry(block, &nlm_blocked, b_list) {
|
| 123 | fl = &block->b_call->a_args.lock.fl;
|
| 124 | dprintk("lockd: check f=%p pd=%d %Ld-%Ld ty=%d cookie=%*phN\n",
|
| 125 | block->b_file, fl->c.flc_pid,
|
| 126 | (long long)fl->fl_start,
|
| 127 | (long long)fl->fl_end, fl->c.flc_type,
|
| 128 | block->b_call->a_args.cookie.len,
|
| 129 | block->b_call->a_args.cookie.data);
|
| 130 | if (block->b_file == file && nlm_compare_locks(fl, &lock->fl)) {
|
| 131 | kref_get(&block->b_count);
|
| 132 | spin_unlock(&nlm_blocked_lock);
|
| 133 | return block;
|
| 134 | }
|
| 135 | }
|
| 136 | spin_unlock(&nlm_blocked_lock);
|
| 137 |
|
| 138 | return NULL;
|
| 139 | }
|
| 140 |
|
| 141 | static int lockd_cookie_match(struct lockd_cookie *a, struct lockd_cookie *b)
|
| 142 | {
|
| 143 | if (a->len != b->len)
|
| 144 | return 0;
|
| 145 | if (memcmp(a->data, b->data, a->len))
|
| 146 | return 0;
|
| 147 | return 1;
|
| 148 | }
|
| 149 |
|
| 150 | |
| 151 | * Find a block with a given NLM cookie.
|
| 152 |
|
| 153 | static inline struct nlm_block *
|
| 154 | nlmsvc_find_block(struct lockd_cookie *cookie)
|
| 155 | {
|
| 156 | struct nlm_block *block;
|
| 157 |
|
| 158 | spin_lock(&nlm_blocked_lock);
|
| 159 | list_for_each_entry(block, &nlm_blocked, b_list) {
|
| 160 | if (lockd_cookie_match(&block->b_call->a_args.cookie, cookie))
|
| 161 | goto found;
|
| 162 | }
|
| 163 | spin_unlock(&nlm_blocked_lock);
|
| 164 |
|
| 165 | return NULL;
|
| 166 |
|
| 167 | found:
|
| 168 | dprintk("nlmsvc_find_block(%*phN): block=%p\n",
|
| 169 | cookie->len, cookie->data, block);
|
| 170 | kref_get(&block->b_count);
|
| 171 | spin_unlock(&nlm_blocked_lock);
|
| 172 | return block;
|
| 173 | }
|
| 174 |
|
| 175 | |
| 176 | * Create a block and initialize it.
|
| 177 | *
|
| 178 | * Note: we explicitly set the cookie of the grant reply to that of
|
| 179 | * the blocked lock request. The spec explicitly mentions that the client
|
| 180 | * should _not_ rely on the callback containing the same cookie as the
|
| 181 | * request, but (as I found out later) that's because some implementations
|
| 182 | * do just this. Never mind the standards comittees, they support our
|
| 183 | * logging industries.
|
| 184 | *
|
| 185 | * 10 years later: I hope we can safely ignore these old and broken
|
| 186 | * clients by now. Let's fix this so we can uniquely identify an incoming
|
| 187 | * GRANTED_RES message by cookie, without having to rely on the client's IP
|
| 188 | * address. --okir
|
| 189 |
|
| 190 | static struct nlm_block *
|
| 191 | nlmsvc_create_block(struct svc_rqst *rqstp, struct nlm_host *host,
|
| 192 | struct nlm_file *file, struct lockd_lock *lock,
|
| 193 | struct lockd_cookie *cookie)
|
| 194 | {
|
| 195 | struct nlm_block *block;
|
| 196 | struct nlm_rqst *call = NULL;
|
| 197 |
|
| 198 | call = nlm_alloc_call(host);
|
| 199 | if (call == NULL)
|
| 200 | return NULL;
|
| 201 |
|
| 202 |
|
| 203 | block = kzalloc_obj(*block);
|
| 204 | if (block == NULL)
|
| 205 | goto failed;
|
| 206 | kref_init(&block->b_count);
|
| 207 | INIT_LIST_HEAD(&block->b_list);
|
| 208 | INIT_LIST_HEAD(&block->b_flist);
|
| 209 |
|
| 210 | if (!nlmsvc_setgrantargs(call, lock))
|
| 211 | goto failed_free;
|
| 212 |
|
| 213 |
|
| 214 | call->a_args.lock.fl.c.flc_flags |= FL_SLEEP;
|
| 215 | call->a_args.lock.fl.fl_lmops = &nlmsvc_lock_operations;
|
| 216 | nlmclnt_next_cookie(&call->a_args.cookie);
|
| 217 |
|
| 218 | dprintk("lockd: created block %p...\n", block);
|
| 219 |
|
| 220 |
|
| 221 | block->b_daemon = rqstp->rq_server;
|
| 222 | block->b_host = host;
|
| 223 | block->b_file = file;
|
| 224 | file->f_count++;
|
| 225 |
|
| 226 |
|
| 227 | list_add(&block->b_flist, &file->f_blocks);
|
| 228 |
|
| 229 |
|
| 230 | block->b_call = call;
|
| 231 | call->a_flags = RPC_TASK_ASYNC;
|
| 232 | call->a_block = block;
|
| 233 |
|
| 234 | return block;
|
| 235 |
|
| 236 | failed_free:
|
| 237 | kfree(block);
|
| 238 | failed:
|
| 239 | nlmsvc_release_call(call);
|
| 240 | return NULL;
|
| 241 | }
|
| 242 |
|
| 243 | |
| 244 | * Delete a block.
|
| 245 | * It is the caller's responsibility to check whether the file
|
| 246 | * can be closed hereafter.
|
| 247 |
|
| 248 | static int nlmsvc_unlink_block(struct nlm_block *block)
|
| 249 | {
|
| 250 | int status;
|
| 251 | dprintk("lockd: unlinking block %p...\n", block);
|
| 252 |
|
| 253 |
|
| 254 | status = locks_delete_block(&block->b_call->a_args.lock.fl);
|
| 255 | nlmsvc_remove_block(block);
|
| 256 | return status;
|
| 257 | }
|
| 258 |
|
| 259 | static void nlmsvc_free_block(struct kref *kref)
|
| 260 | {
|
| 261 | struct nlm_block *block = container_of(kref, struct nlm_block, b_count);
|
| 262 | struct nlm_file *file = block->b_file;
|
| 263 |
|
| 264 | dprintk("lockd: freeing block %p...\n", block);
|
| 265 |
|
| 266 |
|
| 267 | list_del_init(&block->b_flist);
|
| 268 | mutex_unlock(&file->f_mutex);
|
| 269 |
|
| 270 | nlmsvc_freegrantargs(block->b_call);
|
| 271 | nlmsvc_release_call(block->b_call);
|
| 272 | nlm_release_file(block->b_file);
|
| 273 | kfree(block);
|
| 274 | }
|
| 275 |
|
| 276 | static void nlmsvc_release_block(struct nlm_block *block)
|
| 277 | {
|
| 278 | if (block != NULL)
|
| 279 | kref_put_mutex(&block->b_count, nlmsvc_free_block, &block->b_file->f_mutex);
|
| 280 | }
|
| 281 |
|
| 282 | |
| 283 | * Loop over all blocks and delete blocks held by
|
| 284 | * a matching host.
|
| 285 |
|
| 286 | void nlmsvc_traverse_blocks(struct nlm_host *host,
|
| 287 | struct nlm_file *file,
|
| 288 | nlm_host_match_fn_t match)
|
| 289 | {
|
| 290 | struct nlm_block *block, *next;
|
| 291 |
|
| 292 | restart:
|
| 293 | mutex_lock(&file->f_mutex);
|
| 294 | spin_lock(&nlm_blocked_lock);
|
| 295 | list_for_each_entry_safe(block, next, &file->f_blocks, b_flist) {
|
| 296 | if (!match(block->b_host, host))
|
| 297 | continue;
|
| 298 | |
| 299 | * the global retry list - why?
|
| 300 | if (list_empty(&block->b_list))
|
| 301 | continue;
|
| 302 | kref_get(&block->b_count);
|
| 303 | spin_unlock(&nlm_blocked_lock);
|
| 304 | mutex_unlock(&file->f_mutex);
|
| 305 | nlmsvc_unlink_block(block);
|
| 306 | nlmsvc_release_block(block);
|
| 307 | goto restart;
|
| 308 | }
|
| 309 | spin_unlock(&nlm_blocked_lock);
|
| 310 | mutex_unlock(&file->f_mutex);
|
| 311 | }
|
| 312 |
|
| 313 | static struct nlm_lockowner *
|
| 314 | nlmsvc_get_lockowner(struct nlm_lockowner *lockowner)
|
| 315 | {
|
| 316 | refcount_inc(&lockowner->count);
|
| 317 | return lockowner;
|
| 318 | }
|
| 319 |
|
| 320 | void nlmsvc_put_lockowner(struct nlm_lockowner *lockowner)
|
| 321 | {
|
| 322 | if (!refcount_dec_and_lock(&lockowner->count, &lockowner->host->h_lock))
|
| 323 | return;
|
| 324 | list_del(&lockowner->list);
|
| 325 | spin_unlock(&lockowner->host->h_lock);
|
| 326 | nlmsvc_release_host(lockowner->host);
|
| 327 | kfree(lockowner);
|
| 328 | }
|
| 329 |
|
| 330 | static struct nlm_lockowner *__nlmsvc_find_lockowner(struct nlm_host *host, pid_t pid)
|
| 331 | {
|
| 332 | struct nlm_lockowner *lockowner;
|
| 333 | list_for_each_entry(lockowner, &host->h_lockowners, list) {
|
| 334 | if (lockowner->pid != pid)
|
| 335 | continue;
|
| 336 | return nlmsvc_get_lockowner(lockowner);
|
| 337 | }
|
| 338 | return NULL;
|
| 339 | }
|
| 340 |
|
| 341 | static struct nlm_lockowner *nlmsvc_find_lockowner(struct nlm_host *host, pid_t pid)
|
| 342 | {
|
| 343 | struct nlm_lockowner *res, *new = NULL;
|
| 344 |
|
| 345 | spin_lock(&host->h_lock);
|
| 346 | res = __nlmsvc_find_lockowner(host, pid);
|
| 347 |
|
| 348 | if (res == NULL) {
|
| 349 | spin_unlock(&host->h_lock);
|
| 350 | new = kmalloc_obj(*res);
|
| 351 | spin_lock(&host->h_lock);
|
| 352 | res = __nlmsvc_find_lockowner(host, pid);
|
| 353 | if (res == NULL && new != NULL) {
|
| 354 | res = new;
|
| 355 |
|
| 356 | refcount_set(&new->count, 1);
|
| 357 | new->pid = pid;
|
| 358 | new->host = nlm_get_host(host);
|
| 359 | list_add(&new->list, &host->h_lockowners);
|
| 360 | new = NULL;
|
| 361 | }
|
| 362 | }
|
| 363 |
|
| 364 | spin_unlock(&host->h_lock);
|
| 365 | kfree(new);
|
| 366 | return res;
|
| 367 | }
|
| 368 |
|
| 369 | void
|
| 370 | nlmsvc_release_lockowner(struct lockd_lock *lock)
|
| 371 | {
|
| 372 | if (lock->fl.c.flc_owner)
|
| 373 | nlmsvc_put_lockowner(lock->fl.c.flc_owner);
|
| 374 | }
|
| 375 |
|
| 376 | void nlmsvc_locks_init_private(struct file_lock *fl, struct nlm_host *host,
|
| 377 | pid_t pid)
|
| 378 | {
|
| 379 | fl->c.flc_owner = nlmsvc_find_lockowner(host, pid);
|
| 380 | }
|
| 381 |
|
| 382 | |
| 383 | * Initialize arguments for GRANTED call. The nlm_rqst structure
|
| 384 | * has been cleared already.
|
| 385 |
|
| 386 | static int nlmsvc_setgrantargs(struct nlm_rqst *call, struct lockd_lock *lock)
|
| 387 | {
|
| 388 | locks_copy_lock(&call->a_args.lock.fl, &lock->fl);
|
| 389 | memcpy(&call->a_args.lock.fh, &lock->fh, sizeof(call->a_args.lock.fh));
|
| 390 | call->a_args.lock.caller = utsname()->nodename;
|
| 391 | call->a_args.lock.oh.len = lock->oh.len;
|
| 392 |
|
| 393 |
|
| 394 | call->a_args.lock.oh.data = call->a_owner;
|
| 395 | call->a_args.lock.svid = ((struct nlm_lockowner *) lock->fl.c.flc_owner)->pid;
|
| 396 |
|
| 397 | if (lock->oh.len > NLMCLNT_OHSIZE) {
|
| 398 | void *data = kmalloc(lock->oh.len, GFP_KERNEL);
|
| 399 | if (!data)
|
| 400 | return 0;
|
| 401 | call->a_args.lock.oh.data = (u8 *) data;
|
| 402 | }
|
| 403 |
|
| 404 | memcpy(call->a_args.lock.oh.data, lock->oh.data, lock->oh.len);
|
| 405 | return 1;
|
| 406 | }
|
| 407 |
|
| 408 | static void nlmsvc_freegrantargs(struct nlm_rqst *call)
|
| 409 | {
|
| 410 | if (call->a_args.lock.oh.data != call->a_owner)
|
| 411 | kfree(call->a_args.lock.oh.data);
|
| 412 |
|
| 413 | locks_release_private(&call->a_args.lock.fl);
|
| 414 | }
|
| 415 |
|
| 416 | |
| 417 | * Deferred lock request handling for non-blocking lock
|
| 418 |
|
| 419 | static __be32
|
| 420 | nlmsvc_defer_lock_rqst(struct svc_rqst *rqstp, struct nlm_block *block)
|
| 421 | {
|
| 422 | __be32 status = nlm_lck_denied_nolocks;
|
| 423 |
|
| 424 | block->b_flags |= B_QUEUED;
|
| 425 |
|
| 426 | nlmsvc_insert_block(block, NLM_TIMEOUT);
|
| 427 |
|
| 428 | block->b_cache_req = &rqstp->rq_chandle;
|
| 429 | if (rqstp->rq_chandle.defer) {
|
| 430 | block->b_deferred_req =
|
| 431 | rqstp->rq_chandle.defer(block->b_cache_req);
|
| 432 | if (block->b_deferred_req != NULL)
|
| 433 | status = nlm__int__drop_reply;
|
| 434 | }
|
| 435 | dprintk("lockd: nlmsvc_defer_lock_rqst block %p flags %d status %d\n",
|
| 436 | block, block->b_flags, ntohl(status));
|
| 437 |
|
| 438 | return status;
|
| 439 | }
|
| 440 |
|
| 441 | |
| 442 | * Attempt to establish a lock, and if it can't be granted, block it
|
| 443 | * if required.
|
| 444 |
|
| 445 | __be32
|
| 446 | nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
|
| 447 | struct nlm_host *host, struct lockd_lock *lock, int wait,
|
| 448 | struct lockd_cookie *cookie, int reclaim)
|
| 449 | {
|
| 450 | struct inode *inode __maybe_unused = nlmsvc_file_inode(file);
|
| 451 | struct nlm_block *block = NULL;
|
| 452 | int error;
|
| 453 | int mode;
|
| 454 | int async_block = 0;
|
| 455 | __be32 ret;
|
| 456 |
|
| 457 | dprintk("lockd: nlmsvc_lock(%s/%llu, ty=%d, pi=%d, %Ld-%Ld, bl=%d)\n",
|
| 458 | inode->i_sb->s_id, inode->i_ino,
|
| 459 | lock->fl.c.flc_type,
|
| 460 | lock->fl.c.flc_pid,
|
| 461 | (long long)lock->fl.fl_start,
|
| 462 | (long long)lock->fl.fl_end,
|
| 463 | wait);
|
| 464 |
|
| 465 | if (nlmsvc_file_cannot_lock(file))
|
| 466 | return nlm_lck_denied_nolocks;
|
| 467 |
|
| 468 | if (!locks_can_async_lock(nlmsvc_file_file(file)->f_op)) {
|
| 469 | async_block = wait;
|
| 470 | wait = 0;
|
| 471 | }
|
| 472 |
|
| 473 |
|
| 474 | mutex_lock(&file->f_mutex);
|
| 475 | |
| 476 | * or create new block
|
| 477 |
|
| 478 | block = nlmsvc_lookup_block(file, lock);
|
| 479 | if (block == NULL) {
|
| 480 | block = nlmsvc_create_block(rqstp, host, file, lock, cookie);
|
| 481 | ret = nlm_lck_denied_nolocks;
|
| 482 | if (block == NULL)
|
| 483 | goto out;
|
| 484 | lock = &block->b_call->a_args.lock;
|
| 485 | } else
|
| 486 | lock->fl.c.flc_flags &= ~FL_SLEEP;
|
| 487 |
|
| 488 | if (block->b_flags & B_QUEUED) {
|
| 489 | dprintk("lockd: nlmsvc_lock deferred block %p flags %d\n",
|
| 490 | block, block->b_flags);
|
| 491 | if (block->b_granted) {
|
| 492 | nlmsvc_unlink_block(block);
|
| 493 | ret = nlm_granted;
|
| 494 | goto out;
|
| 495 | }
|
| 496 | if (block->b_flags & B_TIMED_OUT) {
|
| 497 | nlmsvc_unlink_block(block);
|
| 498 | ret = nlm_lck_denied;
|
| 499 | goto out;
|
| 500 | }
|
| 501 | ret = nlm__int__drop_reply;
|
| 502 | goto out;
|
| 503 | }
|
| 504 |
|
| 505 | if (locks_in_grace(SVC_NET(rqstp)) && !reclaim) {
|
| 506 | ret = nlm_lck_denied_grace_period;
|
| 507 | goto out;
|
| 508 | }
|
| 509 | if (reclaim && !locks_in_grace(SVC_NET(rqstp))) {
|
| 510 | ret = nlm_lck_denied_grace_period;
|
| 511 | goto out;
|
| 512 | }
|
| 513 |
|
| 514 | spin_lock(&nlm_blocked_lock);
|
| 515 | |
| 516 | * If this is a lock request for an already pending
|
| 517 | * lock request we return nlm_lck_blocked without calling
|
| 518 | * vfs_lock_file() again. Otherwise we have two pending
|
| 519 | * requests on the underlaying ->lock() implementation but
|
| 520 | * only one nlm_block to being granted by lm_grant().
|
| 521 |
|
| 522 | if (locks_can_async_lock(nlmsvc_file_file(file)->f_op) &&
|
| 523 | !list_empty(&block->b_list)) {
|
| 524 | spin_unlock(&nlm_blocked_lock);
|
| 525 | ret = nlm_lck_blocked;
|
| 526 | goto out;
|
| 527 | }
|
| 528 |
|
| 529 |
|
| 530 | nlmsvc_insert_block_locked(block, NLM_NEVER);
|
| 531 | spin_unlock(&nlm_blocked_lock);
|
| 532 |
|
| 533 | if (!wait)
|
| 534 | lock->fl.c.flc_flags &= ~FL_SLEEP;
|
| 535 | mode = lock_to_openmode(&lock->fl);
|
| 536 | error = vfs_lock_file(file->f_file[mode], F_SETLK, &lock->fl, NULL);
|
| 537 | lock->fl.c.flc_flags &= ~FL_SLEEP;
|
| 538 |
|
| 539 | dprintk("lockd: vfs_lock_file returned %d\n", error);
|
| 540 | switch (error) {
|
| 541 | case 0:
|
| 542 | nlmsvc_remove_block(block);
|
| 543 | ret = nlm_granted;
|
| 544 | goto out;
|
| 545 | case -EAGAIN:
|
| 546 | if (!wait)
|
| 547 | nlmsvc_remove_block(block);
|
| 548 | ret = async_block ? nlm_lck_blocked : nlm_lck_denied;
|
| 549 | goto out;
|
| 550 | case FILE_LOCK_DEFERRED:
|
| 551 | if (wait)
|
| 552 | break;
|
| 553 | |
| 554 | Add it to the queue waiting for callback
|
| 555 | ret = nlmsvc_defer_lock_rqst(rqstp, block);
|
| 556 | goto out;
|
| 557 | case -EDEADLK:
|
| 558 | nlmsvc_remove_block(block);
|
| 559 | ret = nlm__int__deadlock;
|
| 560 | goto out;
|
| 561 | default:
|
| 562 | nlmsvc_remove_block(block);
|
| 563 | ret = nlm_lck_denied_nolocks;
|
| 564 | goto out;
|
| 565 | }
|
| 566 |
|
| 567 | ret = nlm_lck_blocked;
|
| 568 | out:
|
| 569 | mutex_unlock(&file->f_mutex);
|
| 570 | nlmsvc_release_block(block);
|
| 571 | dprintk("lockd: nlmsvc_lock returned %u\n", ret);
|
| 572 | return ret;
|
| 573 | }
|
| 574 |
|
| 575 | |
| 576 | * Test for presence of a conflicting lock.
|
| 577 |
|
| 578 | __be32
|
| 579 | nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
|
| 580 | struct nlm_host *host, struct lockd_lock *lock,
|
| 581 | struct lockd_lock *conflock)
|
| 582 | {
|
| 583 | int error;
|
| 584 | __be32 ret;
|
| 585 |
|
| 586 | dprintk("lockd: nlmsvc_testlock(%s/%llu, ty=%d, %Ld-%Ld)\n",
|
| 587 | nlmsvc_file_inode(file)->i_sb->s_id,
|
| 588 | nlmsvc_file_inode(file)->i_ino,
|
| 589 | lock->fl.c.flc_type,
|
| 590 | (long long)lock->fl.fl_start,
|
| 591 | (long long)lock->fl.fl_end);
|
| 592 |
|
| 593 | if (nlmsvc_file_cannot_lock(file))
|
| 594 | return nlm_lck_denied_nolocks;
|
| 595 |
|
| 596 | if (locks_in_grace(SVC_NET(rqstp))) {
|
| 597 | ret = nlm_lck_denied_grace_period;
|
| 598 | goto out;
|
| 599 | }
|
| 600 |
|
| 601 | locks_init_lock(&conflock->fl);
|
| 602 |
|
| 603 | conflock->fl.c.flc_file = lock->fl.c.flc_file;
|
| 604 | conflock->fl.fl_start = lock->fl.fl_start;
|
| 605 | conflock->fl.fl_end = lock->fl.fl_end;
|
| 606 | conflock->fl.c.flc_owner = lock->fl.c.flc_owner;
|
| 607 | error = vfs_test_lock(lock->fl.c.flc_file, &conflock->fl);
|
| 608 | if (error) {
|
| 609 | ret = nlm_lck_denied_nolocks;
|
| 610 | goto out;
|
| 611 | }
|
| 612 |
|
| 613 | if (conflock->fl.c.flc_type == F_UNLCK) {
|
| 614 | ret = nlm_granted;
|
| 615 | goto out;
|
| 616 | }
|
| 617 |
|
| 618 | dprintk("lockd: conflicting lock(ty=%d, %Ld-%Ld)\n",
|
| 619 | conflock->fl.c.flc_type, (long long)conflock->fl.fl_start,
|
| 620 | (long long)conflock->fl.fl_end);
|
| 621 | conflock->caller = "somehost";
|
| 622 | conflock->len = strlen(conflock->caller);
|
| 623 | conflock->oh.len = 0;
|
| 624 | conflock->svid = conflock->fl.c.flc_pid;
|
| 625 | locks_release_private(&conflock->fl);
|
| 626 |
|
| 627 | ret = nlm_lck_denied;
|
| 628 | out:
|
| 629 | return ret;
|
| 630 | }
|
| 631 |
|
| 632 | |
| 633 | * Remove a lock.
|
| 634 | * This implies a CANCEL call: We send a GRANT_MSG, the client replies
|
| 635 | * with a GRANT_RES call which gets lost, and calls UNLOCK immediately
|
| 636 | * afterwards. In this case the block will still be there, and hence
|
| 637 | * must be removed.
|
| 638 |
|
| 639 | __be32
|
| 640 | nlmsvc_unlock(struct net *net, struct nlm_file *file, struct lockd_lock *lock)
|
| 641 | {
|
| 642 | int error = 0;
|
| 643 |
|
| 644 | dprintk("lockd: nlmsvc_unlock(%s/%llu, pi=%d, %Ld-%Ld)\n",
|
| 645 | nlmsvc_file_inode(file)->i_sb->s_id,
|
| 646 | nlmsvc_file_inode(file)->i_ino,
|
| 647 | lock->fl.c.flc_pid,
|
| 648 | (long long)lock->fl.fl_start,
|
| 649 | (long long)lock->fl.fl_end);
|
| 650 |
|
| 651 | if (nlmsvc_file_cannot_lock(file))
|
| 652 | return nlm_lck_denied_nolocks;
|
| 653 |
|
| 654 |
|
| 655 | nlmsvc_cancel_blocked(net, file, lock);
|
| 656 |
|
| 657 | lock->fl.c.flc_type = F_UNLCK;
|
| 658 | lock->fl.c.flc_file = file->f_file[O_RDONLY];
|
| 659 | if (lock->fl.c.flc_file)
|
| 660 | error = vfs_lock_file(lock->fl.c.flc_file, F_SETLK,
|
| 661 | &lock->fl, NULL);
|
| 662 | lock->fl.c.flc_file = file->f_file[O_WRONLY];
|
| 663 | if (lock->fl.c.flc_file)
|
| 664 | error |= vfs_lock_file(lock->fl.c.flc_file, F_SETLK,
|
| 665 | &lock->fl, NULL);
|
| 666 |
|
| 667 | return (error < 0)? nlm_lck_denied_nolocks : nlm_granted;
|
| 668 | }
|
| 669 |
|
| 670 | |
| 671 | * Cancel a previously blocked request.
|
| 672 | *
|
| 673 | * A cancel request always overrides any grant that may currently
|
| 674 | * be in progress.
|
| 675 | * The calling procedure must check whether the file can be closed.
|
| 676 |
|
| 677 | __be32
|
| 678 | nlmsvc_cancel_blocked(struct net *net, struct nlm_file *file, struct lockd_lock *lock)
|
| 679 | {
|
| 680 | struct nlm_block *block;
|
| 681 | int status = 0;
|
| 682 | int mode;
|
| 683 |
|
| 684 | dprintk("lockd: nlmsvc_cancel(%s/%llu, pi=%d, %Ld-%Ld)\n",
|
| 685 | nlmsvc_file_inode(file)->i_sb->s_id,
|
| 686 | nlmsvc_file_inode(file)->i_ino,
|
| 687 | lock->fl.c.flc_pid,
|
| 688 | (long long)lock->fl.fl_start,
|
| 689 | (long long)lock->fl.fl_end);
|
| 690 |
|
| 691 | if (nlmsvc_file_cannot_lock(file))
|
| 692 | return nlm_lck_denied_nolocks;
|
| 693 |
|
| 694 | if (locks_in_grace(net))
|
| 695 | return nlm_lck_denied_grace_period;
|
| 696 |
|
| 697 | mutex_lock(&file->f_mutex);
|
| 698 | block = nlmsvc_lookup_block(file, lock);
|
| 699 | mutex_unlock(&file->f_mutex);
|
| 700 | if (block != NULL) {
|
| 701 | struct file_lock *fl = &block->b_call->a_args.lock.fl;
|
| 702 |
|
| 703 | mode = lock_to_openmode(fl);
|
| 704 | vfs_cancel_lock(block->b_file->f_file[mode], fl);
|
| 705 | status = nlmsvc_unlink_block(block);
|
| 706 | nlmsvc_release_block(block);
|
| 707 | }
|
| 708 | return status ? nlm_lck_denied : nlm_granted;
|
| 709 | }
|
| 710 |
|
| 711 | |
| 712 | * This is a callback from the filesystem for VFS file lock requests.
|
| 713 | * It will be used if lm_grant is defined and the filesystem can not
|
| 714 | * respond to the request immediately.
|
| 715 | * For SETLK or SETLKW request it will get the local posix lock.
|
| 716 | * In all cases it will move the block to the head of nlm_blocked q where
|
| 717 | * nlmsvc_retry_blocked() can send back a reply for SETLKW or revisit the
|
| 718 | * deferred rpc for GETLK and SETLK.
|
| 719 |
|
| 720 | static void
|
| 721 | nlmsvc_update_deferred_block(struct nlm_block *block, int result)
|
| 722 | {
|
| 723 | block->b_flags |= B_GOT_CALLBACK;
|
| 724 | if (result == 0)
|
| 725 | block->b_granted = 1;
|
| 726 | else
|
| 727 | block->b_flags |= B_TIMED_OUT;
|
| 728 | }
|
| 729 |
|
| 730 | static int nlmsvc_grant_deferred(struct file_lock *fl, int result)
|
| 731 | {
|
| 732 | struct nlm_block *block;
|
| 733 | int rc = -ENOENT;
|
| 734 |
|
| 735 | spin_lock(&nlm_blocked_lock);
|
| 736 | list_for_each_entry(block, &nlm_blocked, b_list) {
|
| 737 | if (nlm_compare_locks(&block->b_call->a_args.lock.fl, fl)) {
|
| 738 | dprintk("lockd: nlmsvc_notify_blocked block %p flags %d\n",
|
| 739 | block, block->b_flags);
|
| 740 | if (block->b_flags & B_QUEUED) {
|
| 741 | if (block->b_flags & B_TIMED_OUT) {
|
| 742 | rc = -ENOLCK;
|
| 743 | break;
|
| 744 | }
|
| 745 | nlmsvc_update_deferred_block(block, result);
|
| 746 | } else if (result == 0)
|
| 747 | block->b_granted = 1;
|
| 748 |
|
| 749 | nlmsvc_insert_block_locked(block, 0);
|
| 750 | svc_wake_up(block->b_daemon);
|
| 751 | rc = 0;
|
| 752 | break;
|
| 753 | }
|
| 754 | }
|
| 755 | spin_unlock(&nlm_blocked_lock);
|
| 756 | if (rc == -ENOENT)
|
| 757 | printk(KERN_WARNING "lockd: grant for unknown block\n");
|
| 758 | return rc;
|
| 759 | }
|
| 760 |
|
| 761 | |
| 762 | * Unblock a blocked lock request. This is a callback invoked from the
|
| 763 | * VFS layer when a lock on which we blocked is removed.
|
| 764 | *
|
| 765 | * This function doesn't grant the blocked lock instantly, but rather moves
|
| 766 | * the block to the head of nlm_blocked where it can be picked up by lockd.
|
| 767 |
|
| 768 | static void
|
| 769 | nlmsvc_notify_blocked(struct file_lock *fl)
|
| 770 | {
|
| 771 | struct nlm_block *block;
|
| 772 |
|
| 773 | dprintk("lockd: VFS unblock notification for block %p\n", fl);
|
| 774 | spin_lock(&nlm_blocked_lock);
|
| 775 | list_for_each_entry(block, &nlm_blocked, b_list) {
|
| 776 | if (nlm_compare_locks(&block->b_call->a_args.lock.fl, fl)) {
|
| 777 | nlmsvc_insert_block_locked(block, 0);
|
| 778 | spin_unlock(&nlm_blocked_lock);
|
| 779 | svc_wake_up(block->b_daemon);
|
| 780 | return;
|
| 781 | }
|
| 782 | }
|
| 783 | spin_unlock(&nlm_blocked_lock);
|
| 784 | printk(KERN_WARNING "lockd: notification for unknown block!\n");
|
| 785 | }
|
| 786 |
|
| 787 | static fl_owner_t nlmsvc_get_owner(fl_owner_t owner)
|
| 788 | {
|
| 789 | return nlmsvc_get_lockowner(owner);
|
| 790 | }
|
| 791 |
|
| 792 | static void nlmsvc_put_owner(fl_owner_t owner)
|
| 793 | {
|
| 794 | nlmsvc_put_lockowner(owner);
|
| 795 | }
|
| 796 |
|
| 797 | const struct lock_manager_operations nlmsvc_lock_operations = {
|
| 798 | .lm_notify = nlmsvc_notify_blocked,
|
| 799 | .lm_grant = nlmsvc_grant_deferred,
|
| 800 | .lm_get_owner = nlmsvc_get_owner,
|
| 801 | .lm_put_owner = nlmsvc_put_owner,
|
| 802 | };
|
| 803 |
|
| 804 | |
| 805 | * Try to claim a lock that was previously blocked.
|
| 806 | *
|
| 807 | * Note that we use both the RPC_GRANTED_MSG call _and_ an async
|
| 808 | * RPC thread when notifying the client. This seems like overkill...
|
| 809 | * Here's why:
|
| 810 | * - we don't want to use a synchronous RPC thread, otherwise
|
| 811 | * we might find ourselves hanging on a dead portmapper.
|
| 812 | * - Some lockd implementations (e.g. HP) don't react to
|
| 813 | * RPC_GRANTED calls; they seem to insist on RPC_GRANTED_MSG calls.
|
| 814 |
|
| 815 | static void
|
| 816 | nlmsvc_grant_blocked(struct nlm_block *block)
|
| 817 | {
|
| 818 | struct nlm_file *file = block->b_file;
|
| 819 | struct lockd_lock *lock = &block->b_call->a_args.lock;
|
| 820 | int mode;
|
| 821 | int error;
|
| 822 | loff_t fl_start, fl_end;
|
| 823 |
|
| 824 | dprintk("lockd: grant blocked lock %p\n", block);
|
| 825 |
|
| 826 | kref_get(&block->b_count);
|
| 827 |
|
| 828 |
|
| 829 | nlmsvc_unlink_block(block);
|
| 830 |
|
| 831 | |
| 832 | * Just retry the grant callback, possibly refreshing the RPC
|
| 833 | * binding
|
| 834 | if (block->b_granted) {
|
| 835 | nlm_rebind_host(block->b_host);
|
| 836 | goto callback;
|
| 837 | }
|
| 838 |
|
| 839 |
|
| 840 | |
| 841 | * them unchanged for the GRANT_MSG
|
| 842 |
|
| 843 | lock->fl.c.flc_flags |= FL_SLEEP;
|
| 844 | fl_start = lock->fl.fl_start;
|
| 845 | fl_end = lock->fl.fl_end;
|
| 846 | mode = lock_to_openmode(&lock->fl);
|
| 847 | error = vfs_lock_file(file->f_file[mode], F_SETLK, &lock->fl, NULL);
|
| 848 | lock->fl.c.flc_flags &= ~FL_SLEEP;
|
| 849 | lock->fl.fl_start = fl_start;
|
| 850 | lock->fl.fl_end = fl_end;
|
| 851 |
|
| 852 | switch (error) {
|
| 853 | case 0:
|
| 854 | break;
|
| 855 | case FILE_LOCK_DEFERRED:
|
| 856 | dprintk("lockd: lock still blocked error %d\n", error);
|
| 857 | nlmsvc_insert_block(block, NLM_NEVER);
|
| 858 | nlmsvc_release_block(block);
|
| 859 | return;
|
| 860 | default:
|
| 861 | printk(KERN_WARNING "lockd: unexpected error %d in %s!\n",
|
| 862 | -error, __func__);
|
| 863 | nlmsvc_insert_block(block, 10 * HZ);
|
| 864 | nlmsvc_release_block(block);
|
| 865 | return;
|
| 866 | }
|
| 867 |
|
| 868 | callback:
|
| 869 |
|
| 870 | dprintk("lockd: GRANTing blocked lock.\n");
|
| 871 | block->b_granted = 1;
|
| 872 |
|
| 873 | |
| 874 | * completes or the submission fails
|
| 875 |
|
| 876 | nlmsvc_insert_block(block, NLM_NEVER);
|
| 877 |
|
| 878 | |
| 879 | * will queue up a new one if this one times out
|
| 880 |
|
| 881 | error = nlm_async_call(block->b_call, NLMPROC_GRANTED_MSG,
|
| 882 | &nlmsvc_grant_ops);
|
| 883 |
|
| 884 |
|
| 885 | if (error < 0)
|
| 886 | nlmsvc_insert_block(block, 10 * HZ);
|
| 887 | }
|
| 888 |
|
| 889 | |
| 890 | * This is the callback from the RPC layer when the NLM_GRANTED_MSG
|
| 891 | * RPC call has succeeded or timed out.
|
| 892 | * Like all RPC callbacks, it is invoked by the rpciod process, so it
|
| 893 | * better not sleep. Therefore, we put the blocked lock on the nlm_blocked
|
| 894 | * chain once more in order to have it removed by lockd itself (which can
|
| 895 | * then sleep on the file semaphore without disrupting e.g. the nfs client).
|
| 896 |
|
| 897 | static void nlmsvc_grant_callback(struct rpc_task *task, void *data)
|
| 898 | {
|
| 899 | struct nlm_rqst *call = data;
|
| 900 | struct nlm_block *block = call->a_block;
|
| 901 | unsigned long timeout;
|
| 902 |
|
| 903 | dprintk("lockd: GRANT_MSG RPC callback\n");
|
| 904 |
|
| 905 | spin_lock(&nlm_blocked_lock);
|
| 906 | |
| 907 | * been invalidated. Don't try to requeue it.
|
| 908 | *
|
| 909 | * FIXME: it's possible that the block is removed from the list
|
| 910 | * after this check but before the nlmsvc_insert_block. In that
|
| 911 | * case it will be added back. Perhaps we need better locking
|
| 912 | * for nlm_blocked?
|
| 913 |
|
| 914 | if (list_empty(&block->b_list))
|
| 915 | goto out;
|
| 916 |
|
| 917 | |
| 918 | * move the block towards the head of the queue only, no harm
|
| 919 | * can be done, though.
|
| 920 | if (task->tk_status < 0) {
|
| 921 |
|
| 922 | timeout = 10 * HZ;
|
| 923 | } else {
|
| 924 |
|
| 925 | timeout = 60 * HZ;
|
| 926 | }
|
| 927 | nlmsvc_insert_block_locked(block, timeout);
|
| 928 | svc_wake_up(block->b_daemon);
|
| 929 | out:
|
| 930 | spin_unlock(&nlm_blocked_lock);
|
| 931 | }
|
| 932 |
|
| 933 | |
| 934 | * FIXME: nlmsvc_release_block() grabs a mutex. This is not allowed for an
|
| 935 | * .rpc_release rpc_call_op
|
| 936 |
|
| 937 | static void nlmsvc_grant_release(void *data)
|
| 938 | {
|
| 939 | struct nlm_rqst *call = data;
|
| 940 | nlmsvc_release_block(call->a_block);
|
| 941 | }
|
| 942 |
|
| 943 | static const struct rpc_call_ops nlmsvc_grant_ops = {
|
| 944 | .rpc_call_done = nlmsvc_grant_callback,
|
| 945 | .rpc_release = nlmsvc_grant_release,
|
| 946 | };
|
| 947 |
|
| 948 | |
| 949 | * We received a GRANT_RES callback. Try to find the corresponding
|
| 950 | * block.
|
| 951 |
|
| 952 | void
|
| 953 | nlmsvc_grant_reply(struct lockd_cookie *cookie, __be32 status)
|
| 954 | {
|
| 955 | struct nlm_block *block;
|
| 956 | struct file_lock *fl;
|
| 957 | int error;
|
| 958 |
|
| 959 | dprintk("grant_reply: looking for cookie %x, s=%d\n",
|
| 960 | *(unsigned int *)(cookie->data), status);
|
| 961 | if (!(block = nlmsvc_find_block(cookie)))
|
| 962 | return;
|
| 963 |
|
| 964 | switch (status) {
|
| 965 | case nlm_lck_denied_grace_period:
|
| 966 |
|
| 967 | nlmsvc_insert_block(block, 10 * HZ);
|
| 968 | break;
|
| 969 | case nlm_lck_denied:
|
| 970 |
|
| 971 | nlmsvc_unlink_block(block);
|
| 972 | fl = &block->b_call->a_args.lock.fl;
|
| 973 | fl->c.flc_type = F_UNLCK;
|
| 974 | error = vfs_lock_file(fl->c.flc_file, F_SETLK, fl, NULL);
|
| 975 | if (error)
|
| 976 | pr_warn("lockd: unable to unlock lock rejected by client!\n");
|
| 977 | break;
|
| 978 | default:
|
| 979 | |
| 980 | * Either it was accepted or the status makes no sense
|
| 981 | * just unlink it either way.
|
| 982 |
|
| 983 | nlmsvc_unlink_block(block);
|
| 984 | }
|
| 985 | nlmsvc_release_block(block);
|
| 986 | }
|
| 987 |
|
| 988 | |
| 989 | * If it is a blocking lock, call grant_blocked.
|
| 990 | * For a non-blocking lock or test lock, revisit the request.
|
| 991 |
|
| 992 | static void
|
| 993 | retry_deferred_block(struct nlm_block *block)
|
| 994 | {
|
| 995 | if (!(block->b_flags & B_GOT_CALLBACK))
|
| 996 | block->b_flags |= B_TIMED_OUT;
|
| 997 | nlmsvc_insert_block(block, NLM_TIMEOUT);
|
| 998 | dprintk("revisit block %p flags %d\n", block, block->b_flags);
|
| 999 | if (block->b_deferred_req) {
|
| 1000 | block->b_deferred_req->revisit(block->b_deferred_req, 0);
|
| 1001 | block->b_deferred_req = NULL;
|
| 1002 | }
|
| 1003 | }
|
| 1004 |
|
| 1005 | |
| 1006 | * Retry all blocked locks that have been notified. This is where lockd
|
| 1007 | * picks up locks that can be granted, or grant notifications that must
|
| 1008 | * be retransmitted.
|
| 1009 |
|
| 1010 | void
|
| 1011 | nlmsvc_retry_blocked(struct svc_rqst *rqstp)
|
| 1012 | {
|
| 1013 | unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
|
| 1014 | struct nlm_block *block;
|
| 1015 |
|
| 1016 | spin_lock(&nlm_blocked_lock);
|
| 1017 | while (!list_empty(&nlm_blocked) && !svc_thread_should_stop(rqstp)) {
|
| 1018 | block = list_entry(nlm_blocked.next, struct nlm_block, b_list);
|
| 1019 |
|
| 1020 | if (block->b_when == NLM_NEVER)
|
| 1021 | break;
|
| 1022 | if (time_after(block->b_when, jiffies)) {
|
| 1023 | timeout = block->b_when - jiffies;
|
| 1024 | break;
|
| 1025 | }
|
| 1026 | spin_unlock(&nlm_blocked_lock);
|
| 1027 |
|
| 1028 | dprintk("nlmsvc_retry_blocked(%p, when=%ld)\n",
|
| 1029 | block, block->b_when);
|
| 1030 | if (block->b_flags & B_QUEUED) {
|
| 1031 | dprintk("nlmsvc_retry_blocked delete block (%p, granted=%d, flags=%d)\n",
|
| 1032 | block, block->b_granted, block->b_flags);
|
| 1033 | retry_deferred_block(block);
|
| 1034 | } else
|
| 1035 | nlmsvc_grant_blocked(block);
|
| 1036 | spin_lock(&nlm_blocked_lock);
|
| 1037 | }
|
| 1038 | spin_unlock(&nlm_blocked_lock);
|
| 1039 |
|
| 1040 | if (timeout < MAX_SCHEDULE_TIMEOUT)
|
| 1041 | mod_timer(&nlmsvc_retry, jiffies + timeout);
|
| 1042 | }
|