blob: 84055d31bfc5e427a87c2412eea17f6ea59e2fc4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/lockd/svclock.c
3 *
4 * Handling of server-side locks, mostly of the blocked variety.
5 * This is the ugliest part of lockd because we tread on very thin ice.
6 * GRANT and CANCEL calls may get stuck, meet in mid-flight, etc.
7 * IMNSHO introducing the grant callback into the NLM protocol was one
8 * of the worst ideas Sun ever had. Except maybe for the idea of doing
9 * NFS file locking at all.
10 *
11 * I'm trying hard to avoid race conditions by protecting most accesses
12 * to a file's list of blocked locks through a semaphore. The global
13 * list of blocked locks is not protected in this fashion however.
14 * Therefore, some functions (such as the RPC callback for the async grant
15 * call) move blocked locks towards the head of the list *while some other
16 * process might be traversing it*. This should not be a problem in
17 * practice, because this will only cause functions traversing the list
18 * to visit some blocks twice.
19 *
20 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/errno.h>
26#include <linux/kernel.h>
27#include <linux/sched.h>
28#include <linux/smp_lock.h>
29#include <linux/sunrpc/clnt.h>
30#include <linux/sunrpc/svc.h>
31#include <linux/lockd/nlm.h>
32#include <linux/lockd/lockd.h>
Jeff Laytond751a7c2008-02-07 16:34:55 -050033#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#define NLMDBG_FACILITY NLMDBG_SVCLOCK
36
37#ifdef CONFIG_LOCKD_V4
38#define nlm_deadlock nlm4_deadlock
39#else
40#define nlm_deadlock nlm_lck_denied
41#endif
42
Trond Myklebust6849c0c2006-03-20 13:44:39 -050043static void nlmsvc_release_block(struct nlm_block *block);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static void nlmsvc_insert_block(struct nlm_block *block, unsigned long);
Olaf Kirch68a2d76c2006-10-04 02:15:57 -070045static void nlmsvc_remove_block(struct nlm_block *block);
Trond Myklebust963d8fe2006-01-03 09:55:04 +010046
Trond Myklebust5e1abf82006-03-20 13:44:39 -050047static int nlmsvc_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock);
48static void nlmsvc_freegrantargs(struct nlm_rqst *call);
Trond Myklebust963d8fe2006-01-03 09:55:04 +010049static const struct rpc_call_ops nlmsvc_grant_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/*
52 * The list of blocked locks to retry
53 */
Olaf Kirch68a2d76c2006-10-04 02:15:57 -070054static LIST_HEAD(nlm_blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
57 * Insert a blocked lock into the global list
58 */
59static void
60nlmsvc_insert_block(struct nlm_block *block, unsigned long when)
61{
Olaf Kirch68a2d76c2006-10-04 02:15:57 -070062 struct nlm_block *b;
63 struct list_head *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 dprintk("lockd: nlmsvc_insert_block(%p, %ld)\n", block, when);
Olaf Kirch68a2d76c2006-10-04 02:15:57 -070066 if (list_empty(&block->b_list)) {
67 kref_get(&block->b_count);
68 } else {
69 list_del_init(&block->b_list);
70 }
71
72 pos = &nlm_blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (when != NLM_NEVER) {
74 if ((when += jiffies) == NLM_NEVER)
75 when ++;
Olaf Kirch68a2d76c2006-10-04 02:15:57 -070076 list_for_each(pos, &nlm_blocked) {
77 b = list_entry(pos, struct nlm_block, b_list);
78 if (time_after(b->b_when,when) || b->b_when == NLM_NEVER)
79 break;
80 }
81 /* On normal exit from the loop, pos == &nlm_blocked,
82 * so we will be adding to the end of the list - good
83 */
84 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Olaf Kirch68a2d76c2006-10-04 02:15:57 -070086 list_add_tail(&block->b_list, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 block->b_when = when;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90/*
91 * Remove a block from the global list
92 */
Olaf Kirch68a2d76c2006-10-04 02:15:57 -070093static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -070094nlmsvc_remove_block(struct nlm_block *block)
95{
Olaf Kirch68a2d76c2006-10-04 02:15:57 -070096 if (!list_empty(&block->b_list)) {
97 list_del_init(&block->b_list);
98 nlmsvc_release_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102/*
Trond Myklebustd9f6eb72006-03-20 13:44:47 -0500103 * Find a block for a given lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
105static struct nlm_block *
Trond Myklebustd9f6eb72006-03-20 13:44:47 -0500106nlmsvc_lookup_block(struct nlm_file *file, struct nlm_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700108 struct nlm_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 struct file_lock *fl;
110
111 dprintk("lockd: nlmsvc_lookup_block f=%p pd=%d %Ld-%Ld ty=%d\n",
112 file, lock->fl.fl_pid,
113 (long long)lock->fl.fl_start,
114 (long long)lock->fl.fl_end, lock->fl.fl_type);
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700115 list_for_each_entry(block, &nlm_blocked, b_list) {
Trond Myklebust92737232006-03-20 13:44:45 -0500116 fl = &block->b_call->a_args.lock.fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 dprintk("lockd: check f=%p pd=%d %Ld-%Ld ty=%d cookie=%s\n",
118 block->b_file, fl->fl_pid,
119 (long long)fl->fl_start,
120 (long long)fl->fl_end, fl->fl_type,
Trond Myklebust92737232006-03-20 13:44:45 -0500121 nlmdbg_cookie2a(&block->b_call->a_args.cookie));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (block->b_file == file && nlm_compare_locks(fl, &lock->fl)) {
Trond Myklebust6849c0c2006-03-20 13:44:39 -0500123 kref_get(&block->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return block;
125 }
126 }
127
128 return NULL;
129}
130
131static inline int nlm_cookie_match(struct nlm_cookie *a, struct nlm_cookie *b)
132{
J. Bruce Fields6d7bbbb2008-07-15 14:38:32 -0400133 if (a->len != b->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return 0;
J. Bruce Fields6d7bbbb2008-07-15 14:38:32 -0400135 if (memcmp(a->data, b->data, a->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return 0;
137 return 1;
138}
139
140/*
141 * Find a block with a given NLM cookie.
142 */
143static inline struct nlm_block *
Olaf Kirch39be4502006-10-04 02:16:03 -0700144nlmsvc_find_block(struct nlm_cookie *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 struct nlm_block *block;
147
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700148 list_for_each_entry(block, &nlm_blocked, b_list) {
Olaf Kirch39be4502006-10-04 02:16:03 -0700149 if (nlm_cookie_match(&block->b_call->a_args.cookie,cookie))
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700150 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700153 return NULL;
154
155found:
Olaf Kirch39be4502006-10-04 02:16:03 -0700156 dprintk("nlmsvc_find_block(%s): block=%p\n", nlmdbg_cookie2a(cookie), block);
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700157 kref_get(&block->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return block;
159}
160
161/*
162 * Create a block and initialize it.
163 *
164 * Note: we explicitly set the cookie of the grant reply to that of
165 * the blocked lock request. The spec explicitly mentions that the client
166 * should _not_ rely on the callback containing the same cookie as the
167 * request, but (as I found out later) that's because some implementations
168 * do just this. Never mind the standards comittees, they support our
169 * logging industries.
Olaf Kirch39be4502006-10-04 02:16:03 -0700170 *
171 * 10 years later: I hope we can safely ignore these old and broken
172 * clients by now. Let's fix this so we can uniquely identify an incoming
173 * GRANTED_RES message by cookie, without having to rely on the client's IP
174 * address. --okir
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 */
Trond Myklebust255129d2007-09-25 15:55:03 -0400176static struct nlm_block *
177nlmsvc_create_block(struct svc_rqst *rqstp, struct nlm_host *host,
178 struct nlm_file *file, struct nlm_lock *lock,
179 struct nlm_cookie *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 struct nlm_block *block;
Trond Myklebust92737232006-03-20 13:44:45 -0500182 struct nlm_rqst *call = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
J. Bruce Fields560de0e2008-07-15 15:05:45 -0400184 nlm_get_host(host);
Trond Myklebust92737232006-03-20 13:44:45 -0500185 call = nlm_alloc_call(host);
186 if (call == NULL)
187 return NULL;
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* Allocate memory for block, and initialize arguments */
Trond Myklebust92737232006-03-20 13:44:45 -0500190 block = kzalloc(sizeof(*block), GFP_KERNEL);
191 if (block == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 goto failed;
Trond Myklebust6849c0c2006-03-20 13:44:39 -0500193 kref_init(&block->b_count);
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700194 INIT_LIST_HEAD(&block->b_list);
195 INIT_LIST_HEAD(&block->b_flist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Trond Myklebust92737232006-03-20 13:44:45 -0500197 if (!nlmsvc_setgrantargs(call, lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 goto failed_free;
199
200 /* Set notifier function for VFS, and init args */
Trond Myklebust92737232006-03-20 13:44:45 -0500201 call->a_args.lock.fl.fl_flags |= FL_SLEEP;
202 call->a_args.lock.fl.fl_lmops = &nlmsvc_lock_operations;
Olaf Kirch39be4502006-10-04 02:16:03 -0700203 nlmclnt_next_cookie(&call->a_args.cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 dprintk("lockd: created block %p...\n", block);
206
207 /* Create and initialize the block */
208 block->b_daemon = rqstp->rq_server;
209 block->b_host = host;
210 block->b_file = file;
Marc Eshel5ea0d752006-11-28 16:27:06 -0500211 block->b_fl = NULL;
Trond Myklebustd9f6eb72006-03-20 13:44:47 -0500212 file->f_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* Add to file's list of blocks */
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700215 list_add(&block->b_flist, &file->f_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 /* Set up RPC arguments for callback */
Trond Myklebust92737232006-03-20 13:44:45 -0500218 block->b_call = call;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 call->a_flags = RPC_TASK_ASYNC;
Trond Myklebust92737232006-03-20 13:44:45 -0500220 call->a_block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 return block;
223
224failed_free:
225 kfree(block);
226failed:
Trond Myklebust92737232006-03-20 13:44:45 -0500227 nlm_release_call(call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return NULL;
229}
230
231/*
J. Bruce Fields3c61eec2008-04-07 13:05:27 -0400232 * Delete a block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 * It is the caller's responsibility to check whether the file
234 * can be closed hereafter.
235 */
Trond Myklebust6849c0c2006-03-20 13:44:39 -0500236static int nlmsvc_unlink_block(struct nlm_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Trond Myklebust09c79382006-03-20 13:44:38 -0500238 int status;
Trond Myklebust6849c0c2006-03-20 13:44:39 -0500239 dprintk("lockd: unlinking block %p...\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 /* Remove block from list */
Trond Myklebust92737232006-03-20 13:44:45 -0500242 status = posix_unblock_lock(block->b_file->f_file, &block->b_call->a_args.lock.fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 nlmsvc_remove_block(block);
Trond Myklebust6849c0c2006-03-20 13:44:39 -0500244 return status;
245}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Trond Myklebust6849c0c2006-03-20 13:44:39 -0500247static void nlmsvc_free_block(struct kref *kref)
248{
249 struct nlm_block *block = container_of(kref, struct nlm_block, b_count);
250 struct nlm_file *file = block->b_file;
Trond Myklebust6849c0c2006-03-20 13:44:39 -0500251
252 dprintk("lockd: freeing block %p...\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 /* Remove block from file's list of blocks */
Neil Brown89e63ef2006-10-04 02:16:06 -0700255 mutex_lock(&file->f_mutex);
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700256 list_del_init(&block->b_flist);
Neil Brown89e63ef2006-10-04 02:16:06 -0700257 mutex_unlock(&file->f_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Trond Myklebust92737232006-03-20 13:44:45 -0500259 nlmsvc_freegrantargs(block->b_call);
260 nlm_release_call(block->b_call);
Trond Myklebustd9f6eb72006-03-20 13:44:47 -0500261 nlm_release_file(block->b_file);
Marc Eshel0e4ac9d2006-11-28 16:26:51 -0500262 kfree(block->b_fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 kfree(block);
Trond Myklebust6849c0c2006-03-20 13:44:39 -0500264}
265
266static void nlmsvc_release_block(struct nlm_block *block)
267{
268 if (block != NULL)
269 kref_put(&block->b_count, nlmsvc_free_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Olaf Kirchf2af7932006-10-04 02:15:59 -0700272/*
273 * Loop over all blocks and delete blocks held by
274 * a matching host.
275 */
276void nlmsvc_traverse_blocks(struct nlm_host *host,
277 struct nlm_file *file,
278 nlm_host_match_fn_t match)
Trond Myklebustd9f6eb72006-03-20 13:44:47 -0500279{
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700280 struct nlm_block *block, *next;
Trond Myklebustd9f6eb72006-03-20 13:44:47 -0500281
282restart:
Neil Brown89e63ef2006-10-04 02:16:06 -0700283 mutex_lock(&file->f_mutex);
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700284 list_for_each_entry_safe(block, next, &file->f_blocks, b_flist) {
Olaf Kirchf2af7932006-10-04 02:15:59 -0700285 if (!match(block->b_host, host))
Trond Myklebustd9f6eb72006-03-20 13:44:47 -0500286 continue;
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700287 /* Do not destroy blocks that are not on
288 * the global retry list - why? */
289 if (list_empty(&block->b_list))
Trond Myklebustd9f6eb72006-03-20 13:44:47 -0500290 continue;
291 kref_get(&block->b_count);
Neil Brown89e63ef2006-10-04 02:16:06 -0700292 mutex_unlock(&file->f_mutex);
Trond Myklebustd9f6eb72006-03-20 13:44:47 -0500293 nlmsvc_unlink_block(block);
294 nlmsvc_release_block(block);
295 goto restart;
296 }
Neil Brown89e63ef2006-10-04 02:16:06 -0700297 mutex_unlock(&file->f_mutex);
Trond Myklebustd9f6eb72006-03-20 13:44:47 -0500298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300/*
Trond Myklebust5e1abf82006-03-20 13:44:39 -0500301 * Initialize arguments for GRANTED call. The nlm_rqst structure
302 * has been cleared already.
303 */
304static int nlmsvc_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock)
305{
306 locks_copy_lock(&call->a_args.lock.fl, &lock->fl);
307 memcpy(&call->a_args.lock.fh, &lock->fh, sizeof(call->a_args.lock.fh));
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700308 call->a_args.lock.caller = utsname()->nodename;
Trond Myklebust5e1abf82006-03-20 13:44:39 -0500309 call->a_args.lock.oh.len = lock->oh.len;
310
311 /* set default data area */
312 call->a_args.lock.oh.data = call->a_owner;
313 call->a_args.lock.svid = lock->fl.fl_pid;
314
315 if (lock->oh.len > NLMCLNT_OHSIZE) {
316 void *data = kmalloc(lock->oh.len, GFP_KERNEL);
Trond Myklebust92737232006-03-20 13:44:45 -0500317 if (!data)
Trond Myklebust5e1abf82006-03-20 13:44:39 -0500318 return 0;
Trond Myklebust5e1abf82006-03-20 13:44:39 -0500319 call->a_args.lock.oh.data = (u8 *) data;
320 }
321
322 memcpy(call->a_args.lock.oh.data, lock->oh.data, lock->oh.len);
323 return 1;
324}
325
326static void nlmsvc_freegrantargs(struct nlm_rqst *call)
327{
Trond Myklebust92737232006-03-20 13:44:45 -0500328 if (call->a_args.lock.oh.data != call->a_owner)
Trond Myklebust5e1abf82006-03-20 13:44:39 -0500329 kfree(call->a_args.lock.oh.data);
Felix Blyakhera9e61e22009-03-31 15:12:56 -0500330
331 locks_release_private(&call->a_args.lock.fl);
Trond Myklebust5e1abf82006-03-20 13:44:39 -0500332}
333
334/*
Marc Eshel2b36f412006-11-28 16:26:47 -0500335 * Deferred lock request handling for non-blocking lock
336 */
Al Viroca5c8cd2007-07-26 17:33:49 +0100337static __be32
Marc Eshel2b36f412006-11-28 16:26:47 -0500338nlmsvc_defer_lock_rqst(struct svc_rqst *rqstp, struct nlm_block *block)
339{
Al Viroca5c8cd2007-07-26 17:33:49 +0100340 __be32 status = nlm_lck_denied_nolocks;
Marc Eshel2b36f412006-11-28 16:26:47 -0500341
342 block->b_flags |= B_QUEUED;
343
344 nlmsvc_insert_block(block, NLM_TIMEOUT);
345
346 block->b_cache_req = &rqstp->rq_chandle;
347 if (rqstp->rq_chandle.defer) {
348 block->b_deferred_req =
349 rqstp->rq_chandle.defer(block->b_cache_req);
350 if (block->b_deferred_req != NULL)
351 status = nlm_drop_reply;
352 }
353 dprintk("lockd: nlmsvc_defer_lock_rqst block %p flags %d status %d\n",
Al Viroca5c8cd2007-07-26 17:33:49 +0100354 block, block->b_flags, ntohl(status));
Marc Eshel2b36f412006-11-28 16:26:47 -0500355
356 return status;
357}
358
359/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * Attempt to establish a lock, and if it can't be granted, block it
361 * if required.
362 */
Al Viro52921e02006-10-19 23:28:46 -0700363__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
Jeff Layton6cde4de2008-07-15 14:26:17 -0400365 struct nlm_host *host, struct nlm_lock *lock, int wait,
J. Bruce Fieldsb2b50282008-02-06 13:59:23 -0500366 struct nlm_cookie *cookie, int reclaim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Marc Eshelf8120482006-12-05 23:48:10 -0500368 struct nlm_block *block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int error;
Al Viro52921e02006-10-19 23:28:46 -0700370 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 dprintk("lockd: nlmsvc_lock(%s/%ld, ty=%d, pi=%d, %Ld-%Ld, bl=%d)\n",
Josef Sipek225a7192006-12-08 02:37:18 -0800373 file->f_file->f_path.dentry->d_inode->i_sb->s_id,
374 file->f_file->f_path.dentry->d_inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 lock->fl.fl_type, lock->fl.fl_pid,
376 (long long)lock->fl.fl_start,
377 (long long)lock->fl.fl_end,
378 wait);
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* Lock file against concurrent access */
Neil Brown89e63ef2006-10-04 02:16:06 -0700381 mutex_lock(&file->f_mutex);
Marc Eshelf8120482006-12-05 23:48:10 -0500382 /* Get existing block (in case client is busy-waiting)
383 * or create new block
384 */
Trond Myklebustd9f6eb72006-03-20 13:44:47 -0500385 block = nlmsvc_lookup_block(file, lock);
Trond Myklebust09c79382006-03-20 13:44:38 -0500386 if (block == NULL) {
J. Bruce Fields560de0e2008-07-15 15:05:45 -0400387 block = nlmsvc_create_block(rqstp, host, file, lock, cookie);
Marc Eshelf8120482006-12-05 23:48:10 -0500388 ret = nlm_lck_denied_nolocks;
389 if (block == NULL)
390 goto out;
Trond Myklebust92737232006-03-20 13:44:45 -0500391 lock = &block->b_call->a_args.lock;
Marc Eshelf8120482006-12-05 23:48:10 -0500392 } else
393 lock->fl.fl_flags &= ~FL_SLEEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Marc Eshel1a8322b2006-11-28 16:27:06 -0500395 if (block->b_flags & B_QUEUED) {
396 dprintk("lockd: nlmsvc_lock deferred block %p flags %d\n",
397 block, block->b_flags);
398 if (block->b_granted) {
399 nlmsvc_unlink_block(block);
400 ret = nlm_granted;
401 goto out;
402 }
403 if (block->b_flags & B_TIMED_OUT) {
404 nlmsvc_unlink_block(block);
405 ret = nlm_lck_denied;
406 goto out;
407 }
408 ret = nlm_drop_reply;
409 goto out;
410 }
411
J. Bruce Fieldsb2b50282008-02-06 13:59:23 -0500412 if (locks_in_grace() && !reclaim) {
413 ret = nlm_lck_denied_grace_period;
414 goto out;
415 }
J. Bruce Fieldsd22b1cf2008-02-06 15:05:12 -0500416 if (reclaim && !locks_in_grace()) {
417 ret = nlm_lck_denied_grace_period;
418 goto out;
419 }
J. Bruce Fieldsb2b50282008-02-06 13:59:23 -0500420
Marc Eshel1a8322b2006-11-28 16:27:06 -0500421 if (!wait)
422 lock->fl.fl_flags &= ~FL_SLEEP;
423 error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL);
Trond Myklebust09c79382006-03-20 13:44:38 -0500424 lock->fl.fl_flags &= ~FL_SLEEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Marc Eshel1a8322b2006-11-28 16:27:06 -0500426 dprintk("lockd: vfs_lock_file returned %d\n", error);
J. Bruce Fields6d7bbbb2008-07-15 14:38:32 -0400427 switch (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 case 0:
Andy Adamson15dadef2006-03-20 13:44:24 -0500429 ret = nlm_granted;
430 goto out;
Trond Myklebust09c79382006-03-20 13:44:38 -0500431 case -EAGAIN:
Miklos Szeredie33d1ea2009-02-09 12:30:43 -0500432 /*
433 * If this is a blocking request for an
434 * already pending lock request then we need
435 * to put it back on lockd's block list
436 */
437 if (wait)
438 break;
Marc Eshel1a8322b2006-11-28 16:27:06 -0500439 ret = nlm_lck_denied;
Miklos Szeredie33d1ea2009-02-09 12:30:43 -0500440 goto out;
Miklos Szeredibde74e42008-07-25 01:48:57 -0700441 case FILE_LOCK_DEFERRED:
Marc Eshel1a8322b2006-11-28 16:27:06 -0500442 if (wait)
443 break;
444 /* Filesystem lock operation is in progress
445 Add it to the queue waiting for callback */
446 ret = nlmsvc_defer_lock_rqst(rqstp, block);
447 goto out;
Trond Myklebust09c79382006-03-20 13:44:38 -0500448 case -EDEADLK:
Andy Adamson15dadef2006-03-20 13:44:24 -0500449 ret = nlm_deadlock;
450 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 default: /* includes ENOLCK */
Andy Adamson15dadef2006-03-20 13:44:24 -0500452 ret = nlm_lck_denied_nolocks;
453 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
Trond Myklebust09c79382006-03-20 13:44:38 -0500456 ret = nlm_lck_blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* Append to list of blocked */
Marc Eshelf8120482006-12-05 23:48:10 -0500459 nlmsvc_insert_block(block, NLM_NEVER);
Andy Adamson15dadef2006-03-20 13:44:24 -0500460out:
Neil Brown89e63ef2006-10-04 02:16:06 -0700461 mutex_unlock(&file->f_mutex);
Trond Myklebust6849c0c2006-03-20 13:44:39 -0500462 nlmsvc_release_block(block);
Andy Adamson15dadef2006-03-20 13:44:24 -0500463 dprintk("lockd: nlmsvc_lock returned %u\n", ret);
464 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
467/*
468 * Test for presence of a conflicting lock.
469 */
Al Viro52921e02006-10-19 23:28:46 -0700470__be32
Marc Eshel85f3f1b32006-11-28 16:27:06 -0500471nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
Jeff Layton8f920d52008-07-15 14:06:48 -0400472 struct nlm_host *host, struct nlm_lock *lock,
473 struct nlm_lock *conflock, struct nlm_cookie *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Marc Eshel5ea0d752006-11-28 16:27:06 -0500475 struct nlm_block *block = NULL;
476 int error;
477 __be32 ret;
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 dprintk("lockd: nlmsvc_testlock(%s/%ld, ty=%d, %Ld-%Ld)\n",
Josef Sipek225a7192006-12-08 02:37:18 -0800480 file->f_file->f_path.dentry->d_inode->i_sb->s_id,
481 file->f_file->f_path.dentry->d_inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 lock->fl.fl_type,
483 (long long)lock->fl.fl_start,
484 (long long)lock->fl.fl_end);
485
Marc Eshel5ea0d752006-11-28 16:27:06 -0500486 /* Get existing block (in case client is busy-waiting) */
487 block = nlmsvc_lookup_block(file, lock);
488
489 if (block == NULL) {
490 struct file_lock *conf = kzalloc(sizeof(*conf), GFP_KERNEL);
491
492 if (conf == NULL)
493 return nlm_granted;
Trond Myklebust255129d2007-09-25 15:55:03 -0400494 block = nlmsvc_create_block(rqstp, host, file, lock, cookie);
Marc Eshel5ea0d752006-11-28 16:27:06 -0500495 if (block == NULL) {
496 kfree(conf);
497 return nlm_granted;
498 }
499 block->b_fl = conf;
500 }
501 if (block->b_flags & B_QUEUED) {
502 dprintk("lockd: nlmsvc_testlock deferred block %p flags %d fl %p\n",
503 block, block->b_flags, block->b_fl);
504 if (block->b_flags & B_TIMED_OUT) {
505 nlmsvc_unlink_block(block);
Oleg Drokin29dbf542007-11-29 14:02:21 -0500506 ret = nlm_lck_denied;
507 goto out;
Marc Eshel5ea0d752006-11-28 16:27:06 -0500508 }
509 if (block->b_flags & B_GOT_CALLBACK) {
Oleg Drokin54ca95e2008-01-11 21:57:35 -0500510 nlmsvc_unlink_block(block);
Marc Eshel5ea0d752006-11-28 16:27:06 -0500511 if (block->b_fl != NULL
512 && block->b_fl->fl_type != F_UNLCK) {
513 lock->fl = *block->b_fl;
514 goto conf_lock;
Oleg Drokin29dbf542007-11-29 14:02:21 -0500515 } else {
Oleg Drokin29dbf542007-11-29 14:02:21 -0500516 ret = nlm_granted;
517 goto out;
Marc Eshel5ea0d752006-11-28 16:27:06 -0500518 }
519 }
Oleg Drokin29dbf542007-11-29 14:02:21 -0500520 ret = nlm_drop_reply;
521 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523
J. Bruce Fieldsb2b50282008-02-06 13:59:23 -0500524 if (locks_in_grace()) {
525 ret = nlm_lck_denied_grace_period;
526 goto out;
527 }
Marc Eshel5ea0d752006-11-28 16:27:06 -0500528 error = vfs_test_lock(file->f_file, &lock->fl);
Miklos Szeredibde74e42008-07-25 01:48:57 -0700529 if (error == FILE_LOCK_DEFERRED) {
Oleg Drokin29dbf542007-11-29 14:02:21 -0500530 ret = nlmsvc_defer_lock_rqst(rqstp, block);
531 goto out;
532 }
Marc Eshel5ea0d752006-11-28 16:27:06 -0500533 if (error) {
534 ret = nlm_lck_denied_nolocks;
535 goto out;
536 }
537 if (lock->fl.fl_type == F_UNLCK) {
538 ret = nlm_granted;
539 goto out;
540 }
541
542conf_lock:
543 dprintk("lockd: conflicting lock(ty=%d, %Ld-%Ld)\n",
544 lock->fl.fl_type, (long long)lock->fl.fl_start,
545 (long long)lock->fl.fl_end);
546 conflock->caller = "somehost"; /* FIXME */
547 conflock->len = strlen(conflock->caller);
548 conflock->oh.len = 0; /* don't return OH info */
549 conflock->svid = lock->fl.fl_pid;
550 conflock->fl.fl_type = lock->fl.fl_type;
551 conflock->fl.fl_start = lock->fl.fl_start;
552 conflock->fl.fl_end = lock->fl.fl_end;
553 ret = nlm_lck_denied;
554out:
555 if (block)
556 nlmsvc_release_block(block);
557 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
560/*
561 * Remove a lock.
562 * This implies a CANCEL call: We send a GRANT_MSG, the client replies
563 * with a GRANT_RES call which gets lost, and calls UNLOCK immediately
564 * afterwards. In this case the block will still be there, and hence
565 * must be removed.
566 */
Al Viro52921e02006-10-19 23:28:46 -0700567__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568nlmsvc_unlock(struct nlm_file *file, struct nlm_lock *lock)
569{
570 int error;
571
572 dprintk("lockd: nlmsvc_unlock(%s/%ld, pi=%d, %Ld-%Ld)\n",
Josef Sipek225a7192006-12-08 02:37:18 -0800573 file->f_file->f_path.dentry->d_inode->i_sb->s_id,
574 file->f_file->f_path.dentry->d_inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 lock->fl.fl_pid,
576 (long long)lock->fl.fl_start,
577 (long long)lock->fl.fl_end);
578
579 /* First, cancel any lock that might be there */
580 nlmsvc_cancel_blocked(file, lock);
581
582 lock->fl.fl_type = F_UNLCK;
Marc Eshel1a8322b2006-11-28 16:27:06 -0500583 error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 return (error < 0)? nlm_lck_denied_nolocks : nlm_granted;
586}
587
588/*
589 * Cancel a previously blocked request.
590 *
591 * A cancel request always overrides any grant that may currently
592 * be in progress.
593 * The calling procedure must check whether the file can be closed.
594 */
Al Viro52921e02006-10-19 23:28:46 -0700595__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596nlmsvc_cancel_blocked(struct nlm_file *file, struct nlm_lock *lock)
597{
598 struct nlm_block *block;
J. Bruce Fields64a318e2006-01-03 09:55:46 +0100599 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 dprintk("lockd: nlmsvc_cancel(%s/%ld, pi=%d, %Ld-%Ld)\n",
Josef Sipek225a7192006-12-08 02:37:18 -0800602 file->f_file->f_path.dentry->d_inode->i_sb->s_id,
603 file->f_file->f_path.dentry->d_inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 lock->fl.fl_pid,
605 (long long)lock->fl.fl_start,
606 (long long)lock->fl.fl_end);
607
J. Bruce Fieldsb2b50282008-02-06 13:59:23 -0500608 if (locks_in_grace())
609 return nlm_lck_denied_grace_period;
610
Neil Brown89e63ef2006-10-04 02:16:06 -0700611 mutex_lock(&file->f_mutex);
Trond Myklebustd9f6eb72006-03-20 13:44:47 -0500612 block = nlmsvc_lookup_block(file, lock);
Neil Brown89e63ef2006-10-04 02:16:06 -0700613 mutex_unlock(&file->f_mutex);
Trond Myklebustd9f6eb72006-03-20 13:44:47 -0500614 if (block != NULL) {
Marc Eshel1a8322b2006-11-28 16:27:06 -0500615 vfs_cancel_lock(block->b_file->f_file,
616 &block->b_call->a_args.lock.fl);
Trond Myklebust6849c0c2006-03-20 13:44:39 -0500617 status = nlmsvc_unlink_block(block);
618 nlmsvc_release_block(block);
619 }
J. Bruce Fields64a318e2006-01-03 09:55:46 +0100620 return status ? nlm_lck_denied : nlm_granted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623/*
Marc Eshel0e4ac9d2006-11-28 16:26:51 -0500624 * This is a callback from the filesystem for VFS file lock requests.
625 * It will be used if fl_grant is defined and the filesystem can not
626 * respond to the request immediately.
627 * For GETLK request it will copy the reply to the nlm_block.
628 * For SETLK or SETLKW request it will get the local posix lock.
629 * In all cases it will move the block to the head of nlm_blocked q where
630 * nlmsvc_retry_blocked() can send back a reply for SETLKW or revisit the
631 * deferred rpc for GETLK and SETLK.
632 */
633static void
634nlmsvc_update_deferred_block(struct nlm_block *block, struct file_lock *conf,
635 int result)
636{
637 block->b_flags |= B_GOT_CALLBACK;
638 if (result == 0)
639 block->b_granted = 1;
640 else
641 block->b_flags |= B_TIMED_OUT;
642 if (conf) {
Marc Eshel0e4ac9d2006-11-28 16:26:51 -0500643 if (block->b_fl)
J. Bruce Fields1a747ee2008-04-24 10:08:22 -0400644 __locks_copy_lock(block->b_fl, conf);
Marc Eshel0e4ac9d2006-11-28 16:26:51 -0500645 }
646}
647
648static int nlmsvc_grant_deferred(struct file_lock *fl, struct file_lock *conf,
649 int result)
650{
651 struct nlm_block *block;
652 int rc = -ENOENT;
653
654 lock_kernel();
655 list_for_each_entry(block, &nlm_blocked, b_list) {
656 if (nlm_compare_locks(&block->b_call->a_args.lock.fl, fl)) {
657 dprintk("lockd: nlmsvc_notify_blocked block %p flags %d\n",
658 block, block->b_flags);
659 if (block->b_flags & B_QUEUED) {
660 if (block->b_flags & B_TIMED_OUT) {
661 rc = -ENOLCK;
662 break;
663 }
664 nlmsvc_update_deferred_block(block, conf, result);
665 } else if (result == 0)
666 block->b_granted = 1;
667
668 nlmsvc_insert_block(block, 0);
669 svc_wake_up(block->b_daemon);
670 rc = 0;
671 break;
672 }
673 }
674 unlock_kernel();
675 if (rc == -ENOENT)
676 printk(KERN_WARNING "lockd: grant for unknown block\n");
677 return rc;
678}
679
680/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 * Unblock a blocked lock request. This is a callback invoked from the
682 * VFS layer when a lock on which we blocked is removed.
683 *
684 * This function doesn't grant the blocked lock instantly, but rather moves
685 * the block to the head of nlm_blocked where it can be picked up by lockd.
686 */
687static void
688nlmsvc_notify_blocked(struct file_lock *fl)
689{
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700690 struct nlm_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 dprintk("lockd: VFS unblock notification for block %p\n", fl);
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700693 list_for_each_entry(block, &nlm_blocked, b_list) {
Trond Myklebust92737232006-03-20 13:44:45 -0500694 if (nlm_compare_locks(&block->b_call->a_args.lock.fl, fl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 nlmsvc_insert_block(block, 0);
696 svc_wake_up(block->b_daemon);
697 return;
698 }
699 }
700
701 printk(KERN_WARNING "lockd: notification for unknown block!\n");
702}
703
704static int nlmsvc_same_owner(struct file_lock *fl1, struct file_lock *fl2)
705{
706 return fl1->fl_owner == fl2->fl_owner && fl1->fl_pid == fl2->fl_pid;
707}
708
Alexey Dobriyan7b021962009-09-21 17:01:12 -0700709const struct lock_manager_operations nlmsvc_lock_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 .fl_compare_owner = nlmsvc_same_owner,
711 .fl_notify = nlmsvc_notify_blocked,
Marc Eshel0e4ac9d2006-11-28 16:26:51 -0500712 .fl_grant = nlmsvc_grant_deferred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713};
714
715/*
716 * Try to claim a lock that was previously blocked.
717 *
718 * Note that we use both the RPC_GRANTED_MSG call _and_ an async
719 * RPC thread when notifying the client. This seems like overkill...
720 * Here's why:
721 * - we don't want to use a synchronous RPC thread, otherwise
722 * we might find ourselves hanging on a dead portmapper.
723 * - Some lockd implementations (e.g. HP) don't react to
724 * RPC_GRANTED calls; they seem to insist on RPC_GRANTED_MSG calls.
725 */
726static void
727nlmsvc_grant_blocked(struct nlm_block *block)
728{
729 struct nlm_file *file = block->b_file;
Trond Myklebust92737232006-03-20 13:44:45 -0500730 struct nlm_lock *lock = &block->b_call->a_args.lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 int error;
732
733 dprintk("lockd: grant blocked lock %p\n", block);
734
Marc Eshel0e4ac9d2006-11-28 16:26:51 -0500735 kref_get(&block->b_count);
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 /* Unlink block request from list */
Trond Myklebust6849c0c2006-03-20 13:44:39 -0500738 nlmsvc_unlink_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 /* If b_granted is true this means we've been here before.
741 * Just retry the grant callback, possibly refreshing the RPC
742 * binding */
743 if (block->b_granted) {
744 nlm_rebind_host(block->b_host);
745 goto callback;
746 }
747
748 /* Try the lock operation again */
Trond Myklebust09c79382006-03-20 13:44:38 -0500749 lock->fl.fl_flags |= FL_SLEEP;
Marc Eshel1a8322b2006-11-28 16:27:06 -0500750 error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL);
Trond Myklebust09c79382006-03-20 13:44:38 -0500751 lock->fl.fl_flags &= ~FL_SLEEP;
752
Andy Adamson5de0e502006-03-20 13:44:25 -0500753 switch (error) {
754 case 0:
755 break;
Miklos Szeredibde74e42008-07-25 01:48:57 -0700756 case FILE_LOCK_DEFERRED:
Marc Eshel1a8322b2006-11-28 16:27:06 -0500757 dprintk("lockd: lock still blocked error %d\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 nlmsvc_insert_block(block, NLM_NEVER);
Marc Eshel0e4ac9d2006-11-28 16:26:51 -0500759 nlmsvc_release_block(block);
Trond Myklebustd9f6eb72006-03-20 13:44:47 -0500760 return;
Andy Adamson5de0e502006-03-20 13:44:25 -0500761 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 printk(KERN_WARNING "lockd: unexpected error %d in %s!\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700763 -error, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 nlmsvc_insert_block(block, 10 * HZ);
Marc Eshel0e4ac9d2006-11-28 16:26:51 -0500765 nlmsvc_release_block(block);
Trond Myklebustd9f6eb72006-03-20 13:44:47 -0500766 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
769callback:
770 /* Lock was granted by VFS. */
771 dprintk("lockd: GRANTing blocked lock.\n");
772 block->b_granted = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Jeff Layton97065012008-02-06 11:34:12 -0500774 /* keep block on the list, but don't reattempt until the RPC
775 * completes or the submission fails
776 */
777 nlmsvc_insert_block(block, NLM_NEVER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Jeff Layton97065012008-02-06 11:34:12 -0500779 /* Call the client -- use a soft RPC task since nlmsvc_retry_blocked
780 * will queue up a new one if this one times out
781 */
782 error = nlm_async_call(block->b_call, NLMPROC_GRANTED_MSG,
783 &nlmsvc_grant_ops);
784
785 /* RPC submission failed, wait a bit and retry */
786 if (error < 0)
787 nlmsvc_insert_block(block, 10 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
790/*
791 * This is the callback from the RPC layer when the NLM_GRANTED_MSG
792 * RPC call has succeeded or timed out.
793 * Like all RPC callbacks, it is invoked by the rpciod process, so it
794 * better not sleep. Therefore, we put the blocked lock on the nlm_blocked
795 * chain once more in order to have it removed by lockd itself (which can
796 * then sleep on the file semaphore without disrupting e.g. the nfs client).
797 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100798static void nlmsvc_grant_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100800 struct nlm_rqst *call = data;
Trond Myklebust92737232006-03-20 13:44:45 -0500801 struct nlm_block *block = call->a_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 dprintk("lockd: GRANT_MSG RPC callback\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Trond Myklebusta86dc492008-06-11 13:37:09 -0400806 lock_kernel();
Jeff Laytonc64e80d2008-02-06 11:34:13 -0500807 /* if the block is not on a list at this point then it has
808 * been invalidated. Don't try to requeue it.
809 *
810 * FIXME: it's possible that the block is removed from the list
811 * after this check but before the nlmsvc_insert_block. In that
812 * case it will be added back. Perhaps we need better locking
813 * for nlm_blocked?
814 */
815 if (list_empty(&block->b_list))
Trond Myklebusta86dc492008-06-11 13:37:09 -0400816 goto out;
Jeff Laytonc64e80d2008-02-06 11:34:13 -0500817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /* Technically, we should down the file semaphore here. Since we
819 * move the block towards the head of the queue only, no harm
820 * can be done, though. */
821 if (task->tk_status < 0) {
822 /* RPC error: Re-insert for retransmission */
823 timeout = 10 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 } else {
825 /* Call was successful, now wait for client callback */
826 timeout = 60 * HZ;
827 }
828 nlmsvc_insert_block(block, timeout);
829 svc_wake_up(block->b_daemon);
Trond Myklebusta86dc492008-06-11 13:37:09 -0400830out:
831 unlock_kernel();
Trond Myklebust5e1abf82006-03-20 13:44:39 -0500832}
833
Adrian Bunkec535ce152006-04-18 13:21:50 -0400834static void nlmsvc_grant_release(void *data)
Trond Myklebust5e1abf82006-03-20 13:44:39 -0500835{
Trond Myklebust6041b792006-03-20 13:44:45 -0500836 struct nlm_rqst *call = data;
837
Trond Myklebusta86dc492008-06-11 13:37:09 -0400838 lock_kernel();
Trond Myklebust6041b792006-03-20 13:44:45 -0500839 nlmsvc_release_block(call->a_block);
Trond Myklebusta86dc492008-06-11 13:37:09 -0400840 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100843static const struct rpc_call_ops nlmsvc_grant_ops = {
844 .rpc_call_done = nlmsvc_grant_callback,
Trond Myklebust5e1abf82006-03-20 13:44:39 -0500845 .rpc_release = nlmsvc_grant_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100846};
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848/*
849 * We received a GRANT_RES callback. Try to find the corresponding
850 * block.
851 */
852void
Al Viroe8c5c042006-12-13 00:35:03 -0800853nlmsvc_grant_reply(struct nlm_cookie *cookie, __be32 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 struct nlm_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Olaf Kirch39be4502006-10-04 02:16:03 -0700857 dprintk("grant_reply: looking for cookie %x, s=%d \n",
858 *(unsigned int *)(cookie->data), status);
859 if (!(block = nlmsvc_find_block(cookie)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
J. Bruce Fieldsf2321422006-01-03 09:55:42 +0100862 if (block) {
Al Viroe8c5c042006-12-13 00:35:03 -0800863 if (status == nlm_lck_denied_grace_period) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 /* Try again in a couple of seconds */
865 nlmsvc_insert_block(block, 10 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 } else {
867 /* Lock is now held by client, or has been rejected.
868 * In both cases, the block should be removed. */
Trond Myklebust6849c0c2006-03-20 13:44:39 -0500869 nlmsvc_unlink_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871 }
Trond Myklebust6849c0c2006-03-20 13:44:39 -0500872 nlmsvc_release_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
Marc Eshel0e4ac9d2006-11-28 16:26:51 -0500875/* Helper function to handle retry of a deferred block.
876 * If it is a blocking lock, call grant_blocked.
877 * For a non-blocking lock or test lock, revisit the request.
878 */
879static void
880retry_deferred_block(struct nlm_block *block)
881{
882 if (!(block->b_flags & B_GOT_CALLBACK))
883 block->b_flags |= B_TIMED_OUT;
884 nlmsvc_insert_block(block, NLM_TIMEOUT);
885 dprintk("revisit block %p flags %d\n", block, block->b_flags);
886 if (block->b_deferred_req) {
887 block->b_deferred_req->revisit(block->b_deferred_req, 0);
888 block->b_deferred_req = NULL;
889 }
890}
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892/*
893 * Retry all blocked locks that have been notified. This is where lockd
894 * picks up locks that can be granted, or grant notifications that must
895 * be retransmitted.
896 */
897unsigned long
898nlmsvc_retry_blocked(void)
899{
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700900 unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
901 struct nlm_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Jeff Laytond751a7c2008-02-07 16:34:55 -0500903 while (!list_empty(&nlm_blocked) && !kthread_should_stop()) {
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700904 block = list_entry(nlm_blocked.next, struct nlm_block, b_list);
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (block->b_when == NLM_NEVER)
907 break;
J. Bruce Fields6d7bbbb2008-07-15 14:38:32 -0400908 if (time_after(block->b_when, jiffies)) {
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700909 timeout = block->b_when - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 break;
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700911 }
912
J. Bruce Fieldsf3d43c72006-08-03 15:07:47 -0400913 dprintk("nlmsvc_retry_blocked(%p, when=%ld)\n",
914 block, block->b_when);
Marc Eshel0e4ac9d2006-11-28 16:26:51 -0500915 if (block->b_flags & B_QUEUED) {
916 dprintk("nlmsvc_retry_blocked delete block (%p, granted=%d, flags=%d)\n",
917 block, block->b_granted, block->b_flags);
918 retry_deferred_block(block);
919 } else
920 nlmsvc_grant_blocked(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922
Olaf Kirch68a2d76c2006-10-04 02:15:57 -0700923 return timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}