| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/types.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #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 Layton | d751a7c | 2008-02-07 16:34:55 -0500 | [diff] [blame] | 33 | #include <linux/kthread.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 |  | 
|  | 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 Myklebust | 6849c0c | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 43 | static void nlmsvc_release_block(struct nlm_block *block); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | static void	nlmsvc_insert_block(struct nlm_block *block, unsigned long); | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 45 | static void	nlmsvc_remove_block(struct nlm_block *block); | 
| Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 46 |  | 
| Trond Myklebust | 5e1abf8 | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 47 | static int nlmsvc_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock); | 
|  | 48 | static void nlmsvc_freegrantargs(struct nlm_rqst *call); | 
| Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 49 | static const struct rpc_call_ops nlmsvc_grant_ops; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 |  | 
|  | 51 | /* | 
|  | 52 | * The list of blocked locks to retry | 
|  | 53 | */ | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 54 | static LIST_HEAD(nlm_blocked); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 |  | 
|  | 56 | /* | 
|  | 57 | * Insert a blocked lock into the global list | 
|  | 58 | */ | 
|  | 59 | static void | 
|  | 60 | nlmsvc_insert_block(struct nlm_block *block, unsigned long when) | 
|  | 61 | { | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 62 | struct nlm_block *b; | 
|  | 63 | struct list_head *pos; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 |  | 
|  | 65 | dprintk("lockd: nlmsvc_insert_block(%p, %ld)\n", block, when); | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 66 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | if (when != NLM_NEVER) { | 
|  | 74 | if ((when += jiffies) == NLM_NEVER) | 
|  | 75 | when ++; | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 76 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 |  | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 86 | list_add_tail(&block->b_list, pos); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | block->b_when = when; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } | 
|  | 89 |  | 
|  | 90 | /* | 
|  | 91 | * Remove a block from the global list | 
|  | 92 | */ | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 93 | static inline void | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | nlmsvc_remove_block(struct nlm_block *block) | 
|  | 95 | { | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 96 | if (!list_empty(&block->b_list)) { | 
|  | 97 | list_del_init(&block->b_list); | 
|  | 98 | nlmsvc_release_block(block); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
|  | 102 | /* | 
| Trond Myklebust | d9f6eb7 | 2006-03-20 13:44:47 -0500 | [diff] [blame] | 103 | * Find a block for a given lock | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | */ | 
|  | 105 | static struct nlm_block * | 
| Trond Myklebust | d9f6eb7 | 2006-03-20 13:44:47 -0500 | [diff] [blame] | 106 | nlmsvc_lookup_block(struct nlm_file *file, struct nlm_lock *lock) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 108 | struct nlm_block	*block; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | 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 Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 115 | list_for_each_entry(block, &nlm_blocked, b_list) { | 
| Trond Myklebust | 9273723 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 116 | fl = &block->b_call->a_args.lock.fl; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | 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 Myklebust | 9273723 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 121 | nlmdbg_cookie2a(&block->b_call->a_args.cookie)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | if (block->b_file == file && nlm_compare_locks(fl, &lock->fl)) { | 
| Trond Myklebust | 6849c0c | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 123 | kref_get(&block->b_count); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | return block; | 
|  | 125 | } | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | return NULL; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | static inline int nlm_cookie_match(struct nlm_cookie *a, struct nlm_cookie *b) | 
|  | 132 | { | 
| J. Bruce Fields | 6d7bbbb | 2008-07-15 14:38:32 -0400 | [diff] [blame] | 133 | if (a->len != b->len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | return 0; | 
| J. Bruce Fields | 6d7bbbb | 2008-07-15 14:38:32 -0400 | [diff] [blame] | 135 | if (memcmp(a->data, b->data, a->len)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | return 0; | 
|  | 137 | return 1; | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | /* | 
|  | 141 | * Find a block with a given NLM cookie. | 
|  | 142 | */ | 
|  | 143 | static inline struct nlm_block * | 
| Olaf Kirch | 39be450 | 2006-10-04 02:16:03 -0700 | [diff] [blame] | 144 | nlmsvc_find_block(struct nlm_cookie *cookie) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { | 
|  | 146 | struct nlm_block *block; | 
|  | 147 |  | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 148 | list_for_each_entry(block, &nlm_blocked, b_list) { | 
| Olaf Kirch | 39be450 | 2006-10-04 02:16:03 -0700 | [diff] [blame] | 149 | if (nlm_cookie_match(&block->b_call->a_args.cookie,cookie)) | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 150 | goto found; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } | 
|  | 152 |  | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 153 | return NULL; | 
|  | 154 |  | 
|  | 155 | found: | 
| Olaf Kirch | 39be450 | 2006-10-04 02:16:03 -0700 | [diff] [blame] | 156 | dprintk("nlmsvc_find_block(%s): block=%p\n", nlmdbg_cookie2a(cookie), block); | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 157 | kref_get(&block->b_count); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | 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 Kirch | 39be450 | 2006-10-04 02:16:03 -0700 | [diff] [blame] | 170 | * | 
|  | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | */ | 
| Trond Myklebust | 255129d | 2007-09-25 15:55:03 -0400 | [diff] [blame] | 176 | static struct nlm_block * | 
|  | 177 | nlmsvc_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { | 
|  | 181 | struct nlm_block	*block; | 
| Trond Myklebust | 9273723 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 182 | struct nlm_rqst		*call = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 |  | 
| J. Bruce Fields | 560de0e | 2008-07-15 15:05:45 -0400 | [diff] [blame] | 184 | nlm_get_host(host); | 
| Trond Myklebust | 9273723 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 185 | call = nlm_alloc_call(host); | 
|  | 186 | if (call == NULL) | 
|  | 187 | return NULL; | 
|  | 188 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* Allocate memory for block, and initialize arguments */ | 
| Trond Myklebust | 9273723 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 190 | block = kzalloc(sizeof(*block), GFP_KERNEL); | 
|  | 191 | if (block == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | goto failed; | 
| Trond Myklebust | 6849c0c | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 193 | kref_init(&block->b_count); | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 194 | INIT_LIST_HEAD(&block->b_list); | 
|  | 195 | INIT_LIST_HEAD(&block->b_flist); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 |  | 
| Trond Myklebust | 9273723 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 197 | if (!nlmsvc_setgrantargs(call, lock)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | goto failed_free; | 
|  | 199 |  | 
|  | 200 | /* Set notifier function for VFS, and init args */ | 
| Trond Myklebust | 9273723 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 201 | call->a_args.lock.fl.fl_flags |= FL_SLEEP; | 
|  | 202 | call->a_args.lock.fl.fl_lmops = &nlmsvc_lock_operations; | 
| Olaf Kirch | 39be450 | 2006-10-04 02:16:03 -0700 | [diff] [blame] | 203 | nlmclnt_next_cookie(&call->a_args.cookie); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 |  | 
|  | 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 Eshel | 5ea0d75 | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 211 | block->b_fl = NULL; | 
| Trond Myklebust | d9f6eb7 | 2006-03-20 13:44:47 -0500 | [diff] [blame] | 212 | file->f_count++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 |  | 
|  | 214 | /* Add to file's list of blocks */ | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 215 | list_add(&block->b_flist, &file->f_blocks); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 |  | 
|  | 217 | /* Set up RPC arguments for callback */ | 
| Trond Myklebust | 9273723 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 218 | block->b_call = call; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | call->a_flags   = RPC_TASK_ASYNC; | 
| Trond Myklebust | 9273723 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 220 | call->a_block = block; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 |  | 
|  | 222 | return block; | 
|  | 223 |  | 
|  | 224 | failed_free: | 
|  | 225 | kfree(block); | 
|  | 226 | failed: | 
| Trond Myklebust | 9273723 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 227 | nlm_release_call(call); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | return NULL; | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | /* | 
| J. Bruce Fields | 3c61eec | 2008-04-07 13:05:27 -0400 | [diff] [blame] | 232 | * Delete a block. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | * It is the caller's responsibility to check whether the file | 
|  | 234 | * can be closed hereafter. | 
|  | 235 | */ | 
| Trond Myklebust | 6849c0c | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 236 | static int nlmsvc_unlink_block(struct nlm_block *block) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { | 
| Trond Myklebust | 09c7938 | 2006-03-20 13:44:38 -0500 | [diff] [blame] | 238 | int status; | 
| Trond Myklebust | 6849c0c | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 239 | dprintk("lockd: unlinking block %p...\n", block); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 |  | 
|  | 241 | /* Remove block from list */ | 
| Trond Myklebust | 9273723 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 242 | status = posix_unblock_lock(block->b_file->f_file, &block->b_call->a_args.lock.fl); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | nlmsvc_remove_block(block); | 
| Trond Myklebust | 6849c0c | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 244 | return status; | 
|  | 245 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 |  | 
| Trond Myklebust | 6849c0c | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 247 | static 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 Myklebust | 6849c0c | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 251 |  | 
|  | 252 | dprintk("lockd: freeing block %p...\n", block); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 |  | 
|  | 254 | /* Remove block from file's list of blocks */ | 
| Neil Brown | 89e63ef | 2006-10-04 02:16:06 -0700 | [diff] [blame] | 255 | mutex_lock(&file->f_mutex); | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 256 | list_del_init(&block->b_flist); | 
| Neil Brown | 89e63ef | 2006-10-04 02:16:06 -0700 | [diff] [blame] | 257 | mutex_unlock(&file->f_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 |  | 
| Trond Myklebust | 9273723 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 259 | nlmsvc_freegrantargs(block->b_call); | 
|  | 260 | nlm_release_call(block->b_call); | 
| Trond Myklebust | d9f6eb7 | 2006-03-20 13:44:47 -0500 | [diff] [blame] | 261 | nlm_release_file(block->b_file); | 
| Marc Eshel | 0e4ac9d | 2006-11-28 16:26:51 -0500 | [diff] [blame] | 262 | kfree(block->b_fl); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | kfree(block); | 
| Trond Myklebust | 6849c0c | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 264 | } | 
|  | 265 |  | 
|  | 266 | static void nlmsvc_release_block(struct nlm_block *block) | 
|  | 267 | { | 
|  | 268 | if (block != NULL) | 
|  | 269 | kref_put(&block->b_count, nlmsvc_free_block); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } | 
|  | 271 |  | 
| Olaf Kirch | f2af793 | 2006-10-04 02:15:59 -0700 | [diff] [blame] | 272 | /* | 
|  | 273 | * Loop over all blocks and delete blocks held by | 
|  | 274 | * a matching host. | 
|  | 275 | */ | 
|  | 276 | void nlmsvc_traverse_blocks(struct nlm_host *host, | 
|  | 277 | struct nlm_file *file, | 
|  | 278 | nlm_host_match_fn_t match) | 
| Trond Myklebust | d9f6eb7 | 2006-03-20 13:44:47 -0500 | [diff] [blame] | 279 | { | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 280 | struct nlm_block *block, *next; | 
| Trond Myklebust | d9f6eb7 | 2006-03-20 13:44:47 -0500 | [diff] [blame] | 281 |  | 
|  | 282 | restart: | 
| Neil Brown | 89e63ef | 2006-10-04 02:16:06 -0700 | [diff] [blame] | 283 | mutex_lock(&file->f_mutex); | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 284 | list_for_each_entry_safe(block, next, &file->f_blocks, b_flist) { | 
| Olaf Kirch | f2af793 | 2006-10-04 02:15:59 -0700 | [diff] [blame] | 285 | if (!match(block->b_host, host)) | 
| Trond Myklebust | d9f6eb7 | 2006-03-20 13:44:47 -0500 | [diff] [blame] | 286 | continue; | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 287 | /* Do not destroy blocks that are not on | 
|  | 288 | * the global retry list - why? */ | 
|  | 289 | if (list_empty(&block->b_list)) | 
| Trond Myklebust | d9f6eb7 | 2006-03-20 13:44:47 -0500 | [diff] [blame] | 290 | continue; | 
|  | 291 | kref_get(&block->b_count); | 
| Neil Brown | 89e63ef | 2006-10-04 02:16:06 -0700 | [diff] [blame] | 292 | mutex_unlock(&file->f_mutex); | 
| Trond Myklebust | d9f6eb7 | 2006-03-20 13:44:47 -0500 | [diff] [blame] | 293 | nlmsvc_unlink_block(block); | 
|  | 294 | nlmsvc_release_block(block); | 
|  | 295 | goto restart; | 
|  | 296 | } | 
| Neil Brown | 89e63ef | 2006-10-04 02:16:06 -0700 | [diff] [blame] | 297 | mutex_unlock(&file->f_mutex); | 
| Trond Myklebust | d9f6eb7 | 2006-03-20 13:44:47 -0500 | [diff] [blame] | 298 | } | 
|  | 299 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | /* | 
| Trond Myklebust | 5e1abf8 | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 301 | * Initialize arguments for GRANTED call. The nlm_rqst structure | 
|  | 302 | * has been cleared already. | 
|  | 303 | */ | 
|  | 304 | static 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. Hallyn | e9ff399 | 2006-10-02 02:18:11 -0700 | [diff] [blame] | 308 | call->a_args.lock.caller = utsname()->nodename; | 
| Trond Myklebust | 5e1abf8 | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 309 | 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 Myklebust | 9273723 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 317 | if (!data) | 
| Trond Myklebust | 5e1abf8 | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 318 | return 0; | 
| Trond Myklebust | 5e1abf8 | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 319 | 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 |  | 
|  | 326 | static void nlmsvc_freegrantargs(struct nlm_rqst *call) | 
|  | 327 | { | 
| Trond Myklebust | 9273723 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 328 | if (call->a_args.lock.oh.data != call->a_owner) | 
| Trond Myklebust | 5e1abf8 | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 329 | kfree(call->a_args.lock.oh.data); | 
| Felix Blyakher | a9e61e2 | 2009-03-31 15:12:56 -0500 | [diff] [blame] | 330 |  | 
|  | 331 | locks_release_private(&call->a_args.lock.fl); | 
| Trond Myklebust | 5e1abf8 | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 332 | } | 
|  | 333 |  | 
|  | 334 | /* | 
| Marc Eshel | 2b36f41 | 2006-11-28 16:26:47 -0500 | [diff] [blame] | 335 | * Deferred lock request handling for non-blocking lock | 
|  | 336 | */ | 
| Al Viro | ca5c8cd | 2007-07-26 17:33:49 +0100 | [diff] [blame] | 337 | static __be32 | 
| Marc Eshel | 2b36f41 | 2006-11-28 16:26:47 -0500 | [diff] [blame] | 338 | nlmsvc_defer_lock_rqst(struct svc_rqst *rqstp, struct nlm_block *block) | 
|  | 339 | { | 
| Al Viro | ca5c8cd | 2007-07-26 17:33:49 +0100 | [diff] [blame] | 340 | __be32 status = nlm_lck_denied_nolocks; | 
| Marc Eshel | 2b36f41 | 2006-11-28 16:26:47 -0500 | [diff] [blame] | 341 |  | 
|  | 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 Viro | ca5c8cd | 2007-07-26 17:33:49 +0100 | [diff] [blame] | 354 | block, block->b_flags, ntohl(status)); | 
| Marc Eshel | 2b36f41 | 2006-11-28 16:26:47 -0500 | [diff] [blame] | 355 |  | 
|  | 356 | return status; | 
|  | 357 | } | 
|  | 358 |  | 
|  | 359 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | * Attempt to establish a lock, and if it can't be granted, block it | 
|  | 361 | * if required. | 
|  | 362 | */ | 
| Al Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 363 | __be32 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file, | 
| Jeff Layton | 6cde4de | 2008-07-15 14:26:17 -0400 | [diff] [blame] | 365 | struct nlm_host *host, struct nlm_lock *lock, int wait, | 
| J. Bruce Fields | b2b5028 | 2008-02-06 13:59:23 -0500 | [diff] [blame] | 366 | struct nlm_cookie *cookie, int reclaim) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { | 
| Marc Eshel | f812048 | 2006-12-05 23:48:10 -0500 | [diff] [blame] | 368 | struct nlm_block	*block = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | int			error; | 
| Al Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 370 | __be32			ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 |  | 
|  | 372 | dprintk("lockd: nlmsvc_lock(%s/%ld, ty=%d, pi=%d, %Ld-%Ld, bl=%d)\n", | 
| Josef Sipek | 225a719 | 2006-12-08 02:37:18 -0800 | [diff] [blame] | 373 | file->f_file->f_path.dentry->d_inode->i_sb->s_id, | 
|  | 374 | file->f_file->f_path.dentry->d_inode->i_ino, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | /* Lock file against concurrent access */ | 
| Neil Brown | 89e63ef | 2006-10-04 02:16:06 -0700 | [diff] [blame] | 381 | mutex_lock(&file->f_mutex); | 
| Marc Eshel | f812048 | 2006-12-05 23:48:10 -0500 | [diff] [blame] | 382 | /* Get existing block (in case client is busy-waiting) | 
|  | 383 | * or create new block | 
|  | 384 | */ | 
| Trond Myklebust | d9f6eb7 | 2006-03-20 13:44:47 -0500 | [diff] [blame] | 385 | block = nlmsvc_lookup_block(file, lock); | 
| Trond Myklebust | 09c7938 | 2006-03-20 13:44:38 -0500 | [diff] [blame] | 386 | if (block == NULL) { | 
| J. Bruce Fields | 560de0e | 2008-07-15 15:05:45 -0400 | [diff] [blame] | 387 | block = nlmsvc_create_block(rqstp, host, file, lock, cookie); | 
| Marc Eshel | f812048 | 2006-12-05 23:48:10 -0500 | [diff] [blame] | 388 | ret = nlm_lck_denied_nolocks; | 
|  | 389 | if (block == NULL) | 
|  | 390 | goto out; | 
| Trond Myklebust | 9273723 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 391 | lock = &block->b_call->a_args.lock; | 
| Marc Eshel | f812048 | 2006-12-05 23:48:10 -0500 | [diff] [blame] | 392 | } else | 
|  | 393 | lock->fl.fl_flags &= ~FL_SLEEP; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 |  | 
| Marc Eshel | 1a8322b | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 395 | 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 Fields | b2b5028 | 2008-02-06 13:59:23 -0500 | [diff] [blame] | 412 | if (locks_in_grace() && !reclaim) { | 
|  | 413 | ret = nlm_lck_denied_grace_period; | 
|  | 414 | goto out; | 
|  | 415 | } | 
| J. Bruce Fields | d22b1cf | 2008-02-06 15:05:12 -0500 | [diff] [blame] | 416 | if (reclaim && !locks_in_grace()) { | 
|  | 417 | ret = nlm_lck_denied_grace_period; | 
|  | 418 | goto out; | 
|  | 419 | } | 
| J. Bruce Fields | b2b5028 | 2008-02-06 13:59:23 -0500 | [diff] [blame] | 420 |  | 
| Marc Eshel | 1a8322b | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 421 | if (!wait) | 
|  | 422 | lock->fl.fl_flags &= ~FL_SLEEP; | 
|  | 423 | error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL); | 
| Trond Myklebust | 09c7938 | 2006-03-20 13:44:38 -0500 | [diff] [blame] | 424 | lock->fl.fl_flags &= ~FL_SLEEP; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 |  | 
| Marc Eshel | 1a8322b | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 426 | dprintk("lockd: vfs_lock_file returned %d\n", error); | 
| J. Bruce Fields | 6d7bbbb | 2008-07-15 14:38:32 -0400 | [diff] [blame] | 427 | switch (error) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | case 0: | 
| Andy Adamson | 15dadef | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 429 | ret = nlm_granted; | 
|  | 430 | goto out; | 
| Trond Myklebust | 09c7938 | 2006-03-20 13:44:38 -0500 | [diff] [blame] | 431 | case -EAGAIN: | 
| Miklos Szeredi | e33d1ea | 2009-02-09 12:30:43 -0500 | [diff] [blame] | 432 | /* | 
|  | 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 Eshel | 1a8322b | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 439 | ret = nlm_lck_denied; | 
| Miklos Szeredi | e33d1ea | 2009-02-09 12:30:43 -0500 | [diff] [blame] | 440 | goto out; | 
| Miklos Szeredi | bde74e4 | 2008-07-25 01:48:57 -0700 | [diff] [blame] | 441 | case FILE_LOCK_DEFERRED: | 
| Marc Eshel | 1a8322b | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 442 | 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 Myklebust | 09c7938 | 2006-03-20 13:44:38 -0500 | [diff] [blame] | 448 | case -EDEADLK: | 
| Andy Adamson | 15dadef | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 449 | ret = nlm_deadlock; | 
|  | 450 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | default:			/* includes ENOLCK */ | 
| Andy Adamson | 15dadef | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 452 | ret = nlm_lck_denied_nolocks; | 
|  | 453 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } | 
|  | 455 |  | 
| Trond Myklebust | 09c7938 | 2006-03-20 13:44:38 -0500 | [diff] [blame] | 456 | ret = nlm_lck_blocked; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 |  | 
|  | 458 | /* Append to list of blocked */ | 
| Marc Eshel | f812048 | 2006-12-05 23:48:10 -0500 | [diff] [blame] | 459 | nlmsvc_insert_block(block, NLM_NEVER); | 
| Andy Adamson | 15dadef | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 460 | out: | 
| Neil Brown | 89e63ef | 2006-10-04 02:16:06 -0700 | [diff] [blame] | 461 | mutex_unlock(&file->f_mutex); | 
| Trond Myklebust | 6849c0c | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 462 | nlmsvc_release_block(block); | 
| Andy Adamson | 15dadef | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 463 | dprintk("lockd: nlmsvc_lock returned %u\n", ret); | 
|  | 464 | return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } | 
|  | 466 |  | 
|  | 467 | /* | 
|  | 468 | * Test for presence of a conflicting lock. | 
|  | 469 | */ | 
| Al Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 470 | __be32 | 
| Marc Eshel | 85f3f1b3 | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 471 | nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, | 
| Jeff Layton | 8f920d5 | 2008-07-15 14:06:48 -0400 | [diff] [blame] | 472 | struct nlm_host *host, struct nlm_lock *lock, | 
|  | 473 | struct nlm_lock *conflock, struct nlm_cookie *cookie) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | { | 
| Marc Eshel | 5ea0d75 | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 475 | struct nlm_block 	*block = NULL; | 
|  | 476 | int			error; | 
|  | 477 | __be32			ret; | 
|  | 478 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | dprintk("lockd: nlmsvc_testlock(%s/%ld, ty=%d, %Ld-%Ld)\n", | 
| Josef Sipek | 225a719 | 2006-12-08 02:37:18 -0800 | [diff] [blame] | 480 | file->f_file->f_path.dentry->d_inode->i_sb->s_id, | 
|  | 481 | file->f_file->f_path.dentry->d_inode->i_ino, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | lock->fl.fl_type, | 
|  | 483 | (long long)lock->fl.fl_start, | 
|  | 484 | (long long)lock->fl.fl_end); | 
|  | 485 |  | 
| Marc Eshel | 5ea0d75 | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 486 | /* 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 Myklebust | 255129d | 2007-09-25 15:55:03 -0400 | [diff] [blame] | 494 | block = nlmsvc_create_block(rqstp, host, file, lock, cookie); | 
| Marc Eshel | 5ea0d75 | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 495 | 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 Drokin | 29dbf54 | 2007-11-29 14:02:21 -0500 | [diff] [blame] | 506 | ret = nlm_lck_denied; | 
|  | 507 | goto out; | 
| Marc Eshel | 5ea0d75 | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 508 | } | 
|  | 509 | if (block->b_flags & B_GOT_CALLBACK) { | 
| Oleg Drokin | 54ca95e | 2008-01-11 21:57:35 -0500 | [diff] [blame] | 510 | nlmsvc_unlink_block(block); | 
| Marc Eshel | 5ea0d75 | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 511 | if (block->b_fl != NULL | 
|  | 512 | && block->b_fl->fl_type != F_UNLCK) { | 
|  | 513 | lock->fl = *block->b_fl; | 
|  | 514 | goto conf_lock; | 
| Oleg Drokin | 29dbf54 | 2007-11-29 14:02:21 -0500 | [diff] [blame] | 515 | } else { | 
| Oleg Drokin | 29dbf54 | 2007-11-29 14:02:21 -0500 | [diff] [blame] | 516 | ret = nlm_granted; | 
|  | 517 | goto out; | 
| Marc Eshel | 5ea0d75 | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 518 | } | 
|  | 519 | } | 
| Oleg Drokin | 29dbf54 | 2007-11-29 14:02:21 -0500 | [diff] [blame] | 520 | ret = nlm_drop_reply; | 
|  | 521 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | } | 
|  | 523 |  | 
| J. Bruce Fields | b2b5028 | 2008-02-06 13:59:23 -0500 | [diff] [blame] | 524 | if (locks_in_grace()) { | 
|  | 525 | ret = nlm_lck_denied_grace_period; | 
|  | 526 | goto out; | 
|  | 527 | } | 
| Marc Eshel | 5ea0d75 | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 528 | error = vfs_test_lock(file->f_file, &lock->fl); | 
| Miklos Szeredi | bde74e4 | 2008-07-25 01:48:57 -0700 | [diff] [blame] | 529 | if (error == FILE_LOCK_DEFERRED) { | 
| Oleg Drokin | 29dbf54 | 2007-11-29 14:02:21 -0500 | [diff] [blame] | 530 | ret = nlmsvc_defer_lock_rqst(rqstp, block); | 
|  | 531 | goto out; | 
|  | 532 | } | 
| Marc Eshel | 5ea0d75 | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 533 | 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 |  | 
|  | 542 | conf_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; | 
|  | 554 | out: | 
|  | 555 | if (block) | 
|  | 556 | nlmsvc_release_block(block); | 
|  | 557 | return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } | 
|  | 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 Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 567 | __be32 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | nlmsvc_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 Sipek | 225a719 | 2006-12-08 02:37:18 -0800 | [diff] [blame] | 573 | file->f_file->f_path.dentry->d_inode->i_sb->s_id, | 
|  | 574 | file->f_file->f_path.dentry->d_inode->i_ino, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | 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 Eshel | 1a8322b | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 583 | error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 |  | 
|  | 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 Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 595 | __be32 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | nlmsvc_cancel_blocked(struct nlm_file *file, struct nlm_lock *lock) | 
|  | 597 | { | 
|  | 598 | struct nlm_block	*block; | 
| J. Bruce Fields | 64a318e | 2006-01-03 09:55:46 +0100 | [diff] [blame] | 599 | int status = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 |  | 
|  | 601 | dprintk("lockd: nlmsvc_cancel(%s/%ld, pi=%d, %Ld-%Ld)\n", | 
| Josef Sipek | 225a719 | 2006-12-08 02:37:18 -0800 | [diff] [blame] | 602 | file->f_file->f_path.dentry->d_inode->i_sb->s_id, | 
|  | 603 | file->f_file->f_path.dentry->d_inode->i_ino, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | lock->fl.fl_pid, | 
|  | 605 | (long long)lock->fl.fl_start, | 
|  | 606 | (long long)lock->fl.fl_end); | 
|  | 607 |  | 
| J. Bruce Fields | b2b5028 | 2008-02-06 13:59:23 -0500 | [diff] [blame] | 608 | if (locks_in_grace()) | 
|  | 609 | return nlm_lck_denied_grace_period; | 
|  | 610 |  | 
| Neil Brown | 89e63ef | 2006-10-04 02:16:06 -0700 | [diff] [blame] | 611 | mutex_lock(&file->f_mutex); | 
| Trond Myklebust | d9f6eb7 | 2006-03-20 13:44:47 -0500 | [diff] [blame] | 612 | block = nlmsvc_lookup_block(file, lock); | 
| Neil Brown | 89e63ef | 2006-10-04 02:16:06 -0700 | [diff] [blame] | 613 | mutex_unlock(&file->f_mutex); | 
| Trond Myklebust | d9f6eb7 | 2006-03-20 13:44:47 -0500 | [diff] [blame] | 614 | if (block != NULL) { | 
| Marc Eshel | 1a8322b | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 615 | vfs_cancel_lock(block->b_file->f_file, | 
|  | 616 | &block->b_call->a_args.lock.fl); | 
| Trond Myklebust | 6849c0c | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 617 | status = nlmsvc_unlink_block(block); | 
|  | 618 | nlmsvc_release_block(block); | 
|  | 619 | } | 
| J. Bruce Fields | 64a318e | 2006-01-03 09:55:46 +0100 | [diff] [blame] | 620 | return status ? nlm_lck_denied : nlm_granted; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | } | 
|  | 622 |  | 
|  | 623 | /* | 
| Marc Eshel | 0e4ac9d | 2006-11-28 16:26:51 -0500 | [diff] [blame] | 624 | * 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 | */ | 
|  | 633 | static void | 
|  | 634 | nlmsvc_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 Eshel | 0e4ac9d | 2006-11-28 16:26:51 -0500 | [diff] [blame] | 643 | if (block->b_fl) | 
| J. Bruce Fields | 1a747ee | 2008-04-24 10:08:22 -0400 | [diff] [blame] | 644 | __locks_copy_lock(block->b_fl, conf); | 
| Marc Eshel | 0e4ac9d | 2006-11-28 16:26:51 -0500 | [diff] [blame] | 645 | } | 
|  | 646 | } | 
|  | 647 |  | 
|  | 648 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | * 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 | */ | 
|  | 687 | static void | 
|  | 688 | nlmsvc_notify_blocked(struct file_lock *fl) | 
|  | 689 | { | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 690 | struct nlm_block	*block; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 |  | 
|  | 692 | dprintk("lockd: VFS unblock notification for block %p\n", fl); | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 693 | list_for_each_entry(block, &nlm_blocked, b_list) { | 
| Trond Myklebust | 9273723 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 694 | if (nlm_compare_locks(&block->b_call->a_args.lock.fl, fl)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | 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 |  | 
|  | 704 | static 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 Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 709 | const struct lock_manager_operations nlmsvc_lock_operations = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | .fl_compare_owner = nlmsvc_same_owner, | 
|  | 711 | .fl_notify = nlmsvc_notify_blocked, | 
| Marc Eshel | 0e4ac9d | 2006-11-28 16:26:51 -0500 | [diff] [blame] | 712 | .fl_grant = nlmsvc_grant_deferred, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | }; | 
|  | 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 | */ | 
|  | 726 | static void | 
|  | 727 | nlmsvc_grant_blocked(struct nlm_block *block) | 
|  | 728 | { | 
|  | 729 | struct nlm_file		*file = block->b_file; | 
| Trond Myklebust | 9273723 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 730 | struct nlm_lock		*lock = &block->b_call->a_args.lock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | int			error; | 
|  | 732 |  | 
|  | 733 | dprintk("lockd: grant blocked lock %p\n", block); | 
|  | 734 |  | 
| Marc Eshel | 0e4ac9d | 2006-11-28 16:26:51 -0500 | [diff] [blame] | 735 | kref_get(&block->b_count); | 
|  | 736 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | /* Unlink block request from list */ | 
| Trond Myklebust | 6849c0c | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 738 | nlmsvc_unlink_block(block); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 |  | 
|  | 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 Myklebust | 09c7938 | 2006-03-20 13:44:38 -0500 | [diff] [blame] | 749 | lock->fl.fl_flags |= FL_SLEEP; | 
| Marc Eshel | 1a8322b | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 750 | error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL); | 
| Trond Myklebust | 09c7938 | 2006-03-20 13:44:38 -0500 | [diff] [blame] | 751 | lock->fl.fl_flags &= ~FL_SLEEP; | 
|  | 752 |  | 
| Andy Adamson | 5de0e50 | 2006-03-20 13:44:25 -0500 | [diff] [blame] | 753 | switch (error) { | 
|  | 754 | case 0: | 
|  | 755 | break; | 
| Miklos Szeredi | bde74e4 | 2008-07-25 01:48:57 -0700 | [diff] [blame] | 756 | case FILE_LOCK_DEFERRED: | 
| Marc Eshel | 1a8322b | 2006-11-28 16:27:06 -0500 | [diff] [blame] | 757 | dprintk("lockd: lock still blocked error %d\n", error); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | nlmsvc_insert_block(block, NLM_NEVER); | 
| Marc Eshel | 0e4ac9d | 2006-11-28 16:26:51 -0500 | [diff] [blame] | 759 | nlmsvc_release_block(block); | 
| Trond Myklebust | d9f6eb7 | 2006-03-20 13:44:47 -0500 | [diff] [blame] | 760 | return; | 
| Andy Adamson | 5de0e50 | 2006-03-20 13:44:25 -0500 | [diff] [blame] | 761 | default: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | printk(KERN_WARNING "lockd: unexpected error %d in %s!\n", | 
| Harvey Harrison | 8e24eea | 2008-04-30 00:55:09 -0700 | [diff] [blame] | 763 | -error, __func__); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | nlmsvc_insert_block(block, 10 * HZ); | 
| Marc Eshel | 0e4ac9d | 2006-11-28 16:26:51 -0500 | [diff] [blame] | 765 | nlmsvc_release_block(block); | 
| Trond Myklebust | d9f6eb7 | 2006-03-20 13:44:47 -0500 | [diff] [blame] | 766 | return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | } | 
|  | 768 |  | 
|  | 769 | callback: | 
|  | 770 | /* Lock was granted by VFS. */ | 
|  | 771 | dprintk("lockd: GRANTing blocked lock.\n"); | 
|  | 772 | block->b_granted = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 |  | 
| Jeff Layton | 9706501 | 2008-02-06 11:34:12 -0500 | [diff] [blame] | 774 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 |  | 
| Jeff Layton | 9706501 | 2008-02-06 11:34:12 -0500 | [diff] [blame] | 779 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | } | 
|  | 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 Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 798 | static void nlmsvc_grant_callback(struct rpc_task *task, void *data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | { | 
| Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 800 | struct nlm_rqst		*call = data; | 
| Trond Myklebust | 9273723 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 801 | struct nlm_block	*block = call->a_block; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | unsigned long		timeout; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 |  | 
|  | 804 | dprintk("lockd: GRANT_MSG RPC callback\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 |  | 
| Trond Myklebust | a86dc49 | 2008-06-11 13:37:09 -0400 | [diff] [blame] | 806 | lock_kernel(); | 
| Jeff Layton | c64e80d | 2008-02-06 11:34:13 -0500 | [diff] [blame] | 807 | /* 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 Myklebust | a86dc49 | 2008-06-11 13:37:09 -0400 | [diff] [blame] | 816 | goto out; | 
| Jeff Layton | c64e80d | 2008-02-06 11:34:13 -0500 | [diff] [blame] | 817 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | } 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 Myklebust | a86dc49 | 2008-06-11 13:37:09 -0400 | [diff] [blame] | 830 | out: | 
|  | 831 | unlock_kernel(); | 
| Trond Myklebust | 5e1abf8 | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 832 | } | 
|  | 833 |  | 
| Adrian Bunk | ec535ce | 2006-04-18 13:21:50 -0400 | [diff] [blame] | 834 | static void nlmsvc_grant_release(void *data) | 
| Trond Myklebust | 5e1abf8 | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 835 | { | 
| Trond Myklebust | 6041b79 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 836 | struct nlm_rqst		*call = data; | 
|  | 837 |  | 
| Trond Myklebust | a86dc49 | 2008-06-11 13:37:09 -0400 | [diff] [blame] | 838 | lock_kernel(); | 
| Trond Myklebust | 6041b79 | 2006-03-20 13:44:45 -0500 | [diff] [blame] | 839 | nlmsvc_release_block(call->a_block); | 
| Trond Myklebust | a86dc49 | 2008-06-11 13:37:09 -0400 | [diff] [blame] | 840 | unlock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | } | 
|  | 842 |  | 
| Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 843 | static const struct rpc_call_ops nlmsvc_grant_ops = { | 
|  | 844 | .rpc_call_done = nlmsvc_grant_callback, | 
| Trond Myklebust | 5e1abf8 | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 845 | .rpc_release = nlmsvc_grant_release, | 
| Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 846 | }; | 
|  | 847 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | /* | 
|  | 849 | * We received a GRANT_RES callback. Try to find the corresponding | 
|  | 850 | * block. | 
|  | 851 | */ | 
|  | 852 | void | 
| Al Viro | e8c5c04 | 2006-12-13 00:35:03 -0800 | [diff] [blame] | 853 | nlmsvc_grant_reply(struct nlm_cookie *cookie, __be32 status) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | { | 
|  | 855 | struct nlm_block	*block; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 |  | 
| Olaf Kirch | 39be450 | 2006-10-04 02:16:03 -0700 | [diff] [blame] | 857 | dprintk("grant_reply: looking for cookie %x, s=%d \n", | 
|  | 858 | *(unsigned int *)(cookie->data), status); | 
|  | 859 | if (!(block = nlmsvc_find_block(cookie))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 |  | 
| J. Bruce Fields | f232142 | 2006-01-03 09:55:42 +0100 | [diff] [blame] | 862 | if (block) { | 
| Al Viro | e8c5c04 | 2006-12-13 00:35:03 -0800 | [diff] [blame] | 863 | if (status == nlm_lck_denied_grace_period) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | /* Try again in a couple of seconds */ | 
|  | 865 | nlmsvc_insert_block(block, 10 * HZ); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | } else { | 
|  | 867 | /* Lock is now held by client, or has been rejected. | 
|  | 868 | * In both cases, the block should be removed. */ | 
| Trond Myklebust | 6849c0c | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 869 | nlmsvc_unlink_block(block); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | } | 
|  | 871 | } | 
| Trond Myklebust | 6849c0c | 2006-03-20 13:44:39 -0500 | [diff] [blame] | 872 | nlmsvc_release_block(block); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } | 
|  | 874 |  | 
| Marc Eshel | 0e4ac9d | 2006-11-28 16:26:51 -0500 | [diff] [blame] | 875 | /* 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 | */ | 
|  | 879 | static void | 
|  | 880 | retry_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | /* | 
|  | 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 | */ | 
|  | 897 | unsigned long | 
|  | 898 | nlmsvc_retry_blocked(void) | 
|  | 899 | { | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 900 | unsigned long	timeout = MAX_SCHEDULE_TIMEOUT; | 
|  | 901 | struct nlm_block *block; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 |  | 
| Jeff Layton | d751a7c | 2008-02-07 16:34:55 -0500 | [diff] [blame] | 903 | while (!list_empty(&nlm_blocked) && !kthread_should_stop()) { | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 904 | block = list_entry(nlm_blocked.next, struct nlm_block, b_list); | 
|  | 905 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | if (block->b_when == NLM_NEVER) | 
|  | 907 | break; | 
| J. Bruce Fields | 6d7bbbb | 2008-07-15 14:38:32 -0400 | [diff] [blame] | 908 | if (time_after(block->b_when, jiffies)) { | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 909 | timeout = block->b_when - jiffies; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | break; | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 911 | } | 
|  | 912 |  | 
| J. Bruce Fields | f3d43c7 | 2006-08-03 15:07:47 -0400 | [diff] [blame] | 913 | dprintk("nlmsvc_retry_blocked(%p, when=%ld)\n", | 
|  | 914 | block, block->b_when); | 
| Marc Eshel | 0e4ac9d | 2006-11-28 16:26:51 -0500 | [diff] [blame] | 915 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | } | 
|  | 922 |  | 
| Olaf Kirch | 68a2d76c | 2006-10-04 02:15:57 -0700 | [diff] [blame] | 923 | return timeout; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | } |