Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/lockd/clntproc.c |
| 3 | * |
| 4 | * RPC procedures for the client side NLM implementation |
| 5 | * |
| 6 | * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/config.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/fs.h> |
| 14 | #include <linux/nfs_fs.h> |
| 15 | #include <linux/utsname.h> |
| 16 | #include <linux/smp_lock.h> |
| 17 | #include <linux/sunrpc/clnt.h> |
| 18 | #include <linux/sunrpc/svc.h> |
| 19 | #include <linux/lockd/lockd.h> |
| 20 | #include <linux/lockd/sm_inter.h> |
| 21 | |
| 22 | #define NLMDBG_FACILITY NLMDBG_CLIENT |
| 23 | #define NLMCLNT_GRACE_WAIT (5*HZ) |
Trond Myklebust | ecdbf76 | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 24 | #define NLMCLNT_POLL_TIMEOUT (30*HZ) |
Trond Myklebust | aaaa994 | 2006-02-01 12:18:25 -0500 | [diff] [blame] | 25 | #define NLMCLNT_MAX_RETRIES 3 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | static int nlmclnt_test(struct nlm_rqst *, struct file_lock *); |
| 28 | static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *); |
| 29 | static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | static int nlm_stat_to_errno(u32 stat); |
| 31 | static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host); |
Trond Myklebust | 16fb242 | 2006-02-01 12:18:22 -0500 | [diff] [blame] | 32 | static int nlmclnt_cancel(struct nlm_host *, int , struct file_lock *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 34 | static const struct rpc_call_ops nlmclnt_unlock_ops; |
| 35 | static const struct rpc_call_ops nlmclnt_cancel_ops; |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | /* |
| 38 | * Cookie counter for NLM requests |
| 39 | */ |
| 40 | static u32 nlm_cookie = 0x1234; |
| 41 | |
| 42 | static inline void nlmclnt_next_cookie(struct nlm_cookie *c) |
| 43 | { |
| 44 | memcpy(c->data, &nlm_cookie, 4); |
| 45 | memset(c->data+4, 0, 4); |
| 46 | c->len=4; |
| 47 | nlm_cookie++; |
| 48 | } |
| 49 | |
| 50 | static struct nlm_lockowner *nlm_get_lockowner(struct nlm_lockowner *lockowner) |
| 51 | { |
| 52 | atomic_inc(&lockowner->count); |
| 53 | return lockowner; |
| 54 | } |
| 55 | |
| 56 | static void nlm_put_lockowner(struct nlm_lockowner *lockowner) |
| 57 | { |
| 58 | if (!atomic_dec_and_lock(&lockowner->count, &lockowner->host->h_lock)) |
| 59 | return; |
| 60 | list_del(&lockowner->list); |
| 61 | spin_unlock(&lockowner->host->h_lock); |
| 62 | nlm_release_host(lockowner->host); |
| 63 | kfree(lockowner); |
| 64 | } |
| 65 | |
| 66 | static inline int nlm_pidbusy(struct nlm_host *host, uint32_t pid) |
| 67 | { |
| 68 | struct nlm_lockowner *lockowner; |
| 69 | list_for_each_entry(lockowner, &host->h_lockowners, list) { |
| 70 | if (lockowner->pid == pid) |
| 71 | return -EBUSY; |
| 72 | } |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static inline uint32_t __nlm_alloc_pid(struct nlm_host *host) |
| 77 | { |
| 78 | uint32_t res; |
| 79 | do { |
| 80 | res = host->h_pidcount++; |
| 81 | } while (nlm_pidbusy(host, res) < 0); |
| 82 | return res; |
| 83 | } |
| 84 | |
| 85 | static struct nlm_lockowner *__nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner) |
| 86 | { |
| 87 | struct nlm_lockowner *lockowner; |
| 88 | list_for_each_entry(lockowner, &host->h_lockowners, list) { |
| 89 | if (lockowner->owner != owner) |
| 90 | continue; |
| 91 | return nlm_get_lockowner(lockowner); |
| 92 | } |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | static struct nlm_lockowner *nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner) |
| 97 | { |
| 98 | struct nlm_lockowner *res, *new = NULL; |
| 99 | |
| 100 | spin_lock(&host->h_lock); |
| 101 | res = __nlm_find_lockowner(host, owner); |
| 102 | if (res == NULL) { |
| 103 | spin_unlock(&host->h_lock); |
| 104 | new = (struct nlm_lockowner *)kmalloc(sizeof(*new), GFP_KERNEL); |
| 105 | spin_lock(&host->h_lock); |
| 106 | res = __nlm_find_lockowner(host, owner); |
| 107 | if (res == NULL && new != NULL) { |
| 108 | res = new; |
| 109 | atomic_set(&new->count, 1); |
| 110 | new->owner = owner; |
| 111 | new->pid = __nlm_alloc_pid(host); |
| 112 | new->host = nlm_get_host(host); |
| 113 | list_add(&new->list, &host->h_lockowners); |
| 114 | new = NULL; |
| 115 | } |
| 116 | } |
| 117 | spin_unlock(&host->h_lock); |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 118 | kfree(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | return res; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls |
| 124 | */ |
| 125 | static void nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl) |
| 126 | { |
| 127 | struct nlm_args *argp = &req->a_args; |
| 128 | struct nlm_lock *lock = &argp->lock; |
| 129 | |
| 130 | nlmclnt_next_cookie(&argp->cookie); |
| 131 | argp->state = nsm_local_state; |
| 132 | memcpy(&lock->fh, NFS_FH(fl->fl_file->f_dentry->d_inode), sizeof(struct nfs_fh)); |
| 133 | lock->caller = system_utsname.nodename; |
| 134 | lock->oh.data = req->a_owner; |
Trond Myklebust | 7bab377 | 2006-03-20 13:44:06 -0500 | [diff] [blame] | 135 | lock->oh.len = snprintf(req->a_owner, sizeof(req->a_owner), "%u@%s", |
| 136 | (unsigned int)fl->fl_u.nfs_fl.owner->pid, |
| 137 | system_utsname.nodename); |
| 138 | lock->svid = fl->fl_u.nfs_fl.owner->pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | locks_copy_lock(&lock->fl, fl); |
| 140 | } |
| 141 | |
| 142 | static void nlmclnt_release_lockargs(struct nlm_rqst *req) |
| 143 | { |
| 144 | struct file_lock *fl = &req->a_args.lock.fl; |
| 145 | |
| 146 | if (fl->fl_ops && fl->fl_ops->fl_release_private) |
| 147 | fl->fl_ops->fl_release_private(fl); |
| 148 | } |
| 149 | |
| 150 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | * This is the main entry point for the NLM client. |
| 152 | */ |
| 153 | int |
| 154 | nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl) |
| 155 | { |
| 156 | struct nfs_server *nfssrv = NFS_SERVER(inode); |
| 157 | struct nlm_host *host; |
| 158 | struct nlm_rqst reqst, *call = &reqst; |
| 159 | sigset_t oldset; |
| 160 | unsigned long flags; |
| 161 | int status, proto, vers; |
| 162 | |
| 163 | vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1; |
| 164 | if (NFS_PROTO(inode)->version > 3) { |
| 165 | printk(KERN_NOTICE "NFSv4 file locking not implemented!\n"); |
| 166 | return -ENOLCK; |
| 167 | } |
| 168 | |
| 169 | /* Retrieve transport protocol from NFS client */ |
| 170 | proto = NFS_CLIENT(inode)->cl_xprt->prot; |
| 171 | |
| 172 | if (!(host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers))) |
| 173 | return -ENOLCK; |
| 174 | |
| 175 | /* Create RPC client handle if not there, and copy soft |
| 176 | * and intr flags from NFS client. */ |
| 177 | if (host->h_rpcclnt == NULL) { |
| 178 | struct rpc_clnt *clnt; |
| 179 | |
| 180 | /* Bind an rpc client to this host handle (does not |
| 181 | * perform a portmapper lookup) */ |
| 182 | if (!(clnt = nlm_bind_host(host))) { |
| 183 | status = -ENOLCK; |
| 184 | goto done; |
| 185 | } |
| 186 | clnt->cl_softrtry = nfssrv->client->cl_softrtry; |
Chuck Lever | f518e35a | 2006-01-03 09:55:52 +0100 | [diff] [blame] | 187 | clnt->cl_intr = nfssrv->client->cl_intr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* Keep the old signal mask */ |
| 191 | spin_lock_irqsave(¤t->sighand->siglock, flags); |
| 192 | oldset = current->blocked; |
| 193 | |
| 194 | /* If we're cleaning up locks because the process is exiting, |
| 195 | * perform the RPC call asynchronously. */ |
| 196 | if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) |
| 197 | && fl->fl_type == F_UNLCK |
| 198 | && (current->flags & PF_EXITING)) { |
| 199 | sigfillset(¤t->blocked); /* Mask all signals */ |
| 200 | recalc_sigpending(); |
| 201 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
| 202 | |
| 203 | call = nlmclnt_alloc_call(); |
| 204 | if (!call) { |
| 205 | status = -ENOMEM; |
| 206 | goto out_restore; |
| 207 | } |
| 208 | call->a_flags = RPC_TASK_ASYNC; |
| 209 | } else { |
| 210 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
| 211 | memset(call, 0, sizeof(*call)); |
| 212 | locks_init_lock(&call->a_args.lock.fl); |
| 213 | locks_init_lock(&call->a_res.lock.fl); |
| 214 | } |
| 215 | call->a_host = host; |
| 216 | |
| 217 | nlmclnt_locks_init_private(fl, host); |
| 218 | |
| 219 | /* Set up the argument struct */ |
| 220 | nlmclnt_setlockargs(call, fl); |
| 221 | |
| 222 | if (IS_SETLK(cmd) || IS_SETLKW(cmd)) { |
| 223 | if (fl->fl_type != F_UNLCK) { |
| 224 | call->a_args.block = IS_SETLKW(cmd) ? 1 : 0; |
| 225 | status = nlmclnt_lock(call, fl); |
| 226 | } else |
| 227 | status = nlmclnt_unlock(call, fl); |
| 228 | } else if (IS_GETLK(cmd)) |
| 229 | status = nlmclnt_test(call, fl); |
| 230 | else |
| 231 | status = -EINVAL; |
| 232 | |
| 233 | out_restore: |
| 234 | spin_lock_irqsave(¤t->sighand->siglock, flags); |
| 235 | current->blocked = oldset; |
| 236 | recalc_sigpending(); |
| 237 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
| 238 | |
| 239 | done: |
| 240 | dprintk("lockd: clnt proc returns %d\n", status); |
| 241 | nlm_release_host(host); |
| 242 | return status; |
| 243 | } |
| 244 | EXPORT_SYMBOL(nlmclnt_proc); |
| 245 | |
| 246 | /* |
| 247 | * Allocate an NLM RPC call struct |
| 248 | */ |
| 249 | struct nlm_rqst * |
| 250 | nlmclnt_alloc_call(void) |
| 251 | { |
| 252 | struct nlm_rqst *call; |
| 253 | |
Trond Myklebust | 36943fa | 2006-03-20 13:44:05 -0500 | [diff] [blame] | 254 | for(;;) { |
| 255 | call = kzalloc(sizeof(*call), GFP_KERNEL); |
| 256 | if (call != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | locks_init_lock(&call->a_args.lock.fl); |
| 258 | locks_init_lock(&call->a_res.lock.fl); |
| 259 | return call; |
| 260 | } |
Trond Myklebust | 36943fa | 2006-03-20 13:44:05 -0500 | [diff] [blame] | 261 | if (signalled()) |
| 262 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | printk("nlmclnt_alloc_call: failed, waiting for memory\n"); |
Nishanth Aravamudan | 041e0e3 | 2005-09-10 00:27:23 -0700 | [diff] [blame] | 264 | schedule_timeout_interruptible(5*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
| 266 | return NULL; |
| 267 | } |
| 268 | |
| 269 | static int nlm_wait_on_grace(wait_queue_head_t *queue) |
| 270 | { |
| 271 | DEFINE_WAIT(wait); |
| 272 | int status = -EINTR; |
| 273 | |
| 274 | prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE); |
| 275 | if (!signalled ()) { |
| 276 | schedule_timeout(NLMCLNT_GRACE_WAIT); |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 277 | try_to_freeze(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | if (!signalled ()) |
| 279 | status = 0; |
| 280 | } |
| 281 | finish_wait(queue, &wait); |
| 282 | return status; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Generic NLM call |
| 287 | */ |
| 288 | static int |
| 289 | nlmclnt_call(struct nlm_rqst *req, u32 proc) |
| 290 | { |
| 291 | struct nlm_host *host = req->a_host; |
| 292 | struct rpc_clnt *clnt; |
| 293 | struct nlm_args *argp = &req->a_args; |
| 294 | struct nlm_res *resp = &req->a_res; |
| 295 | struct rpc_message msg = { |
| 296 | .rpc_argp = argp, |
| 297 | .rpc_resp = resp, |
| 298 | }; |
| 299 | int status; |
| 300 | |
| 301 | dprintk("lockd: call procedure %d on %s\n", |
| 302 | (int)proc, host->h_name); |
| 303 | |
| 304 | do { |
| 305 | if (host->h_reclaiming && !argp->reclaim) |
| 306 | goto in_grace_period; |
| 307 | |
| 308 | /* If we have no RPC client yet, create one. */ |
| 309 | if ((clnt = nlm_bind_host(host)) == NULL) |
| 310 | return -ENOLCK; |
| 311 | msg.rpc_proc = &clnt->cl_procinfo[proc]; |
| 312 | |
| 313 | /* Perform the RPC call. If an error occurs, try again */ |
| 314 | if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) { |
| 315 | dprintk("lockd: rpc_call returned error %d\n", -status); |
| 316 | switch (status) { |
| 317 | case -EPROTONOSUPPORT: |
| 318 | status = -EINVAL; |
| 319 | break; |
| 320 | case -ECONNREFUSED: |
| 321 | case -ETIMEDOUT: |
| 322 | case -ENOTCONN: |
| 323 | nlm_rebind_host(host); |
| 324 | status = -EAGAIN; |
| 325 | break; |
| 326 | case -ERESTARTSYS: |
| 327 | return signalled () ? -EINTR : status; |
| 328 | default: |
| 329 | break; |
| 330 | } |
| 331 | break; |
| 332 | } else |
| 333 | if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) { |
| 334 | dprintk("lockd: server in grace period\n"); |
| 335 | if (argp->reclaim) { |
| 336 | printk(KERN_WARNING |
| 337 | "lockd: spurious grace period reject?!\n"); |
| 338 | return -ENOLCK; |
| 339 | } |
| 340 | } else { |
| 341 | if (!argp->reclaim) { |
| 342 | /* We appear to be out of the grace period */ |
| 343 | wake_up_all(&host->h_gracewait); |
| 344 | } |
| 345 | dprintk("lockd: server returns status %d\n", resp->status); |
| 346 | return 0; /* Okay, call complete */ |
| 347 | } |
| 348 | |
| 349 | in_grace_period: |
| 350 | /* |
| 351 | * The server has rebooted and appears to be in the grace |
| 352 | * period during which locks are only allowed to be |
| 353 | * reclaimed. |
| 354 | * We can only back off and try again later. |
| 355 | */ |
| 356 | status = nlm_wait_on_grace(&host->h_gracewait); |
| 357 | } while (status == 0); |
| 358 | |
| 359 | return status; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Generic NLM call, async version. |
| 364 | */ |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 365 | int nlmsvc_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | { |
| 367 | struct nlm_host *host = req->a_host; |
| 368 | struct rpc_clnt *clnt; |
| 369 | struct rpc_message msg = { |
| 370 | .rpc_argp = &req->a_args, |
| 371 | .rpc_resp = &req->a_res, |
| 372 | }; |
| 373 | int status; |
| 374 | |
| 375 | dprintk("lockd: call procedure %d on %s (async)\n", |
| 376 | (int)proc, host->h_name); |
| 377 | |
| 378 | /* If we have no RPC client yet, create one. */ |
| 379 | if ((clnt = nlm_bind_host(host)) == NULL) |
| 380 | return -ENOLCK; |
| 381 | msg.rpc_proc = &clnt->cl_procinfo[proc]; |
| 382 | |
| 383 | /* bootstrap and kick off the async RPC call */ |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 384 | status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
| 386 | return status; |
| 387 | } |
| 388 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 389 | static int nlmclnt_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | { |
| 391 | struct nlm_host *host = req->a_host; |
| 392 | struct rpc_clnt *clnt; |
| 393 | struct nlm_args *argp = &req->a_args; |
| 394 | struct nlm_res *resp = &req->a_res; |
| 395 | struct rpc_message msg = { |
| 396 | .rpc_argp = argp, |
| 397 | .rpc_resp = resp, |
| 398 | }; |
| 399 | int status; |
| 400 | |
| 401 | dprintk("lockd: call procedure %d on %s (async)\n", |
| 402 | (int)proc, host->h_name); |
| 403 | |
| 404 | /* If we have no RPC client yet, create one. */ |
| 405 | if ((clnt = nlm_bind_host(host)) == NULL) |
| 406 | return -ENOLCK; |
| 407 | msg.rpc_proc = &clnt->cl_procinfo[proc]; |
| 408 | |
| 409 | /* Increment host refcount */ |
| 410 | nlm_get_host(host); |
| 411 | /* bootstrap and kick off the async RPC call */ |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 412 | status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | if (status < 0) |
| 414 | nlm_release_host(host); |
| 415 | return status; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * TEST for the presence of a conflicting lock |
| 420 | */ |
| 421 | static int |
| 422 | nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl) |
| 423 | { |
| 424 | int status; |
| 425 | |
| 426 | status = nlmclnt_call(req, NLMPROC_TEST); |
| 427 | nlmclnt_release_lockargs(req); |
| 428 | if (status < 0) |
| 429 | return status; |
| 430 | |
| 431 | status = req->a_res.status; |
| 432 | if (status == NLM_LCK_GRANTED) { |
| 433 | fl->fl_type = F_UNLCK; |
| 434 | } if (status == NLM_LCK_DENIED) { |
| 435 | /* |
| 436 | * Report the conflicting lock back to the application. |
| 437 | */ |
| 438 | locks_copy_lock(fl, &req->a_res.lock.fl); |
| 439 | fl->fl_pid = 0; |
| 440 | } else { |
| 441 | return nlm_stat_to_errno(req->a_res.status); |
| 442 | } |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl) |
| 448 | { |
Trond Myklebust | 4c060b5 | 2006-03-20 13:44:41 -0500 | [diff] [blame] | 449 | new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state; |
| 450 | new->fl_u.nfs_fl.owner = nlm_get_lockowner(fl->fl_u.nfs_fl.owner); |
| 451 | list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static void nlmclnt_locks_release_private(struct file_lock *fl) |
| 455 | { |
Trond Myklebust | 4c060b5 | 2006-03-20 13:44:41 -0500 | [diff] [blame] | 456 | list_del(&fl->fl_u.nfs_fl.list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | nlm_put_lockowner(fl->fl_u.nfs_fl.owner); |
| 458 | fl->fl_ops = NULL; |
| 459 | } |
| 460 | |
| 461 | static struct file_lock_operations nlmclnt_lock_ops = { |
| 462 | .fl_copy_lock = nlmclnt_locks_copy_lock, |
| 463 | .fl_release_private = nlmclnt_locks_release_private, |
| 464 | }; |
| 465 | |
| 466 | static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host) |
| 467 | { |
| 468 | BUG_ON(fl->fl_ops != NULL); |
| 469 | fl->fl_u.nfs_fl.state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner); |
Trond Myklebust | 4c060b5 | 2006-03-20 13:44:41 -0500 | [diff] [blame] | 471 | INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | fl->fl_ops = &nlmclnt_lock_ops; |
| 473 | } |
| 474 | |
| 475 | static void do_vfs_lock(struct file_lock *fl) |
| 476 | { |
| 477 | int res = 0; |
| 478 | switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { |
| 479 | case FL_POSIX: |
| 480 | res = posix_lock_file_wait(fl->fl_file, fl); |
| 481 | break; |
| 482 | case FL_FLOCK: |
| 483 | res = flock_lock_file_wait(fl->fl_file, fl); |
| 484 | break; |
| 485 | default: |
| 486 | BUG(); |
| 487 | } |
| 488 | if (res < 0) |
| 489 | printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", |
| 490 | __FUNCTION__); |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * LOCK: Try to create a lock |
| 495 | * |
| 496 | * Programmer Harassment Alert |
| 497 | * |
| 498 | * When given a blocking lock request in a sync RPC call, the HPUX lockd |
| 499 | * will faithfully return LCK_BLOCKED but never cares to notify us when |
| 500 | * the lock could be granted. This way, our local process could hang |
| 501 | * around forever waiting for the callback. |
| 502 | * |
| 503 | * Solution A: Implement busy-waiting |
| 504 | * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES}) |
| 505 | * |
| 506 | * For now I am implementing solution A, because I hate the idea of |
| 507 | * re-implementing lockd for a third time in two months. The async |
| 508 | * calls shouldn't be too hard to do, however. |
| 509 | * |
| 510 | * This is one of the lovely things about standards in the NFS area: |
| 511 | * they're so soft and squishy you can't really blame HP for doing this. |
| 512 | */ |
| 513 | static int |
| 514 | nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl) |
| 515 | { |
| 516 | struct nlm_host *host = req->a_host; |
| 517 | struct nlm_res *resp = &req->a_res; |
Trond Myklebust | ecdbf76 | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 518 | long timeout; |
| 519 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
| 521 | if (!host->h_monitored && nsm_monitor(host) < 0) { |
| 522 | printk(KERN_NOTICE "lockd: failed to monitor %s\n", |
| 523 | host->h_name); |
| 524 | status = -ENOLCK; |
| 525 | goto out; |
| 526 | } |
| 527 | |
Trond Myklebust | ecdbf76 | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 528 | if (req->a_args.block) { |
| 529 | status = nlmclnt_prepare_block(req, host, fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | if (status < 0) |
| 531 | goto out; |
Trond Myklebust | ecdbf76 | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 532 | } |
| 533 | for(;;) { |
| 534 | status = nlmclnt_call(req, NLMPROC_LOCK); |
| 535 | if (status < 0) |
| 536 | goto out_unblock; |
| 537 | if (resp->status != NLM_LCK_BLOCKED) |
| 538 | break; |
| 539 | /* Wait on an NLM blocking lock */ |
| 540 | timeout = nlmclnt_block(req, NLMCLNT_POLL_TIMEOUT); |
| 541 | /* Did a reclaimer thread notify us of a server reboot? */ |
| 542 | if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) |
| 543 | continue; |
| 544 | if (resp->status != NLM_LCK_BLOCKED) |
| 545 | break; |
| 546 | if (timeout >= 0) |
| 547 | continue; |
| 548 | /* We were interrupted. Send a CANCEL request to the server |
| 549 | * and exit |
| 550 | */ |
| 551 | status = (int)timeout; |
| 552 | goto out_unblock; |
| 553 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
| 555 | if (resp->status == NLM_LCK_GRANTED) { |
| 556 | fl->fl_u.nfs_fl.state = host->h_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | fl->fl_flags |= FL_SLEEP; |
Trond Myklebust | 4c060b5 | 2006-03-20 13:44:41 -0500 | [diff] [blame] | 558 | /* Ensure the resulting lock will get added to granted list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | do_vfs_lock(fl); |
| 560 | } |
| 561 | status = nlm_stat_to_errno(resp->status); |
Trond Myklebust | ecdbf76 | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 562 | out_unblock: |
| 563 | nlmclnt_finish_block(req); |
| 564 | /* Cancel the blocked request if it is still pending */ |
| 565 | if (resp->status == NLM_LCK_BLOCKED) |
Trond Myklebust | 16fb242 | 2006-02-01 12:18:22 -0500 | [diff] [blame] | 566 | nlmclnt_cancel(host, req->a_args.block, fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | out: |
| 568 | nlmclnt_release_lockargs(req); |
| 569 | return status; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * RECLAIM: Try to reclaim a lock |
| 574 | */ |
| 575 | int |
| 576 | nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl) |
| 577 | { |
| 578 | struct nlm_rqst reqst, *req; |
| 579 | int status; |
| 580 | |
| 581 | req = &reqst; |
| 582 | memset(req, 0, sizeof(*req)); |
| 583 | locks_init_lock(&req->a_args.lock.fl); |
| 584 | locks_init_lock(&req->a_res.lock.fl); |
| 585 | req->a_host = host; |
| 586 | req->a_flags = 0; |
| 587 | |
| 588 | /* Set up the argument struct */ |
| 589 | nlmclnt_setlockargs(req, fl); |
| 590 | req->a_args.reclaim = 1; |
| 591 | |
| 592 | if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0 |
| 593 | && req->a_res.status == NLM_LCK_GRANTED) |
| 594 | return 0; |
| 595 | |
| 596 | printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d " |
| 597 | "(errno %d, status %d)\n", fl->fl_pid, |
| 598 | status, req->a_res.status); |
| 599 | |
| 600 | /* |
| 601 | * FIXME: This is a serious failure. We can |
| 602 | * |
| 603 | * a. Ignore the problem |
| 604 | * b. Send the owning process some signal (Linux doesn't have |
| 605 | * SIGLOST, though...) |
| 606 | * c. Retry the operation |
| 607 | * |
| 608 | * Until someone comes up with a simple implementation |
| 609 | * for b or c, I'll choose option a. |
| 610 | */ |
| 611 | |
| 612 | return -ENOLCK; |
| 613 | } |
| 614 | |
| 615 | /* |
| 616 | * UNLOCK: remove an existing lock |
| 617 | */ |
| 618 | static int |
| 619 | nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl) |
| 620 | { |
| 621 | struct nlm_res *resp = &req->a_res; |
| 622 | int status; |
| 623 | |
Christoph Hellwig | 26bcbf9 | 2006-03-20 13:44:40 -0500 | [diff] [blame] | 624 | /* |
Trond Myklebust | 30f4e20 | 2006-03-13 21:20:49 -0800 | [diff] [blame] | 625 | * Note: the server is supposed to either grant us the unlock |
| 626 | * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either |
| 627 | * case, we want to unlock. |
| 628 | */ |
| 629 | do_vfs_lock(fl); |
| 630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | if (req->a_flags & RPC_TASK_ASYNC) { |
| 632 | status = nlmclnt_async_call(req, NLMPROC_UNLOCK, |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 633 | &nlmclnt_unlock_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | /* Hrmf... Do the unlock early since locks_remove_posix() |
| 635 | * really expects us to free the lock synchronously */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | if (status < 0) { |
| 637 | nlmclnt_release_lockargs(req); |
| 638 | kfree(req); |
| 639 | } |
| 640 | return status; |
| 641 | } |
| 642 | |
| 643 | status = nlmclnt_call(req, NLMPROC_UNLOCK); |
| 644 | nlmclnt_release_lockargs(req); |
| 645 | if (status < 0) |
| 646 | return status; |
| 647 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | if (resp->status == NLM_LCK_GRANTED) |
| 649 | return 0; |
| 650 | |
| 651 | if (resp->status != NLM_LCK_DENIED_NOLOCKS) |
| 652 | printk("lockd: unexpected unlock status: %d\n", resp->status); |
| 653 | |
| 654 | /* What to do now? I'm out of my depth... */ |
| 655 | |
| 656 | return -ENOLCK; |
| 657 | } |
| 658 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 659 | static void nlmclnt_unlock_callback(struct rpc_task *task, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 661 | struct nlm_rqst *req = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | int status = req->a_res.status; |
| 663 | |
| 664 | if (RPC_ASSASSINATED(task)) |
| 665 | goto die; |
| 666 | |
| 667 | if (task->tk_status < 0) { |
| 668 | dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status); |
| 669 | goto retry_rebind; |
| 670 | } |
| 671 | if (status == NLM_LCK_DENIED_GRACE_PERIOD) { |
| 672 | rpc_delay(task, NLMCLNT_GRACE_WAIT); |
| 673 | goto retry_unlock; |
| 674 | } |
| 675 | if (status != NLM_LCK_GRANTED) |
| 676 | printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status); |
| 677 | die: |
| 678 | nlm_release_host(req->a_host); |
| 679 | nlmclnt_release_lockargs(req); |
| 680 | kfree(req); |
| 681 | return; |
| 682 | retry_rebind: |
| 683 | nlm_rebind_host(req->a_host); |
| 684 | retry_unlock: |
| 685 | rpc_restart_call(task); |
| 686 | } |
| 687 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 688 | static const struct rpc_call_ops nlmclnt_unlock_ops = { |
| 689 | .rpc_call_done = nlmclnt_unlock_callback, |
| 690 | }; |
| 691 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | /* |
| 693 | * Cancel a blocked lock request. |
| 694 | * We always use an async RPC call for this in order not to hang a |
| 695 | * process that has been Ctrl-C'ed. |
| 696 | */ |
Trond Myklebust | 16fb242 | 2006-02-01 12:18:22 -0500 | [diff] [blame] | 697 | static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | { |
| 699 | struct nlm_rqst *req; |
| 700 | unsigned long flags; |
| 701 | sigset_t oldset; |
| 702 | int status; |
| 703 | |
| 704 | /* Block all signals while setting up call */ |
| 705 | spin_lock_irqsave(¤t->sighand->siglock, flags); |
| 706 | oldset = current->blocked; |
| 707 | sigfillset(¤t->blocked); |
| 708 | recalc_sigpending(); |
| 709 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
| 710 | |
| 711 | req = nlmclnt_alloc_call(); |
| 712 | if (!req) |
| 713 | return -ENOMEM; |
| 714 | req->a_host = host; |
| 715 | req->a_flags = RPC_TASK_ASYNC; |
| 716 | |
| 717 | nlmclnt_setlockargs(req, fl); |
Trond Myklebust | 16fb242 | 2006-02-01 12:18:22 -0500 | [diff] [blame] | 718 | req->a_args.block = block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 720 | status = nlmclnt_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | if (status < 0) { |
| 722 | nlmclnt_release_lockargs(req); |
| 723 | kfree(req); |
| 724 | } |
| 725 | |
| 726 | spin_lock_irqsave(¤t->sighand->siglock, flags); |
| 727 | current->blocked = oldset; |
| 728 | recalc_sigpending(); |
| 729 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
| 730 | |
| 731 | return status; |
| 732 | } |
| 733 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 734 | static void nlmclnt_cancel_callback(struct rpc_task *task, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 736 | struct nlm_rqst *req = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | |
| 738 | if (RPC_ASSASSINATED(task)) |
| 739 | goto die; |
| 740 | |
| 741 | if (task->tk_status < 0) { |
| 742 | dprintk("lockd: CANCEL call error %d, retrying.\n", |
| 743 | task->tk_status); |
| 744 | goto retry_cancel; |
| 745 | } |
| 746 | |
| 747 | dprintk("lockd: cancel status %d (task %d)\n", |
| 748 | req->a_res.status, task->tk_pid); |
| 749 | |
| 750 | switch (req->a_res.status) { |
| 751 | case NLM_LCK_GRANTED: |
| 752 | case NLM_LCK_DENIED_GRACE_PERIOD: |
Trond Myklebust | 35576cb | 2006-03-20 13:44:41 -0500 | [diff] [blame^] | 753 | case NLM_LCK_DENIED: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | /* Everything's good */ |
| 755 | break; |
| 756 | case NLM_LCK_DENIED_NOLOCKS: |
| 757 | dprintk("lockd: CANCEL failed (server has no locks)\n"); |
| 758 | goto retry_cancel; |
| 759 | default: |
| 760 | printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n", |
| 761 | req->a_res.status); |
| 762 | } |
| 763 | |
| 764 | die: |
| 765 | nlm_release_host(req->a_host); |
| 766 | nlmclnt_release_lockargs(req); |
| 767 | kfree(req); |
| 768 | return; |
| 769 | |
| 770 | retry_cancel: |
Trond Myklebust | aaaa994 | 2006-02-01 12:18:25 -0500 | [diff] [blame] | 771 | /* Don't ever retry more than 3 times */ |
| 772 | if (req->a_retries++ >= NLMCLNT_MAX_RETRIES) |
| 773 | goto die; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | nlm_rebind_host(req->a_host); |
| 775 | rpc_restart_call(task); |
| 776 | rpc_delay(task, 30 * HZ); |
| 777 | } |
| 778 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 779 | static const struct rpc_call_ops nlmclnt_cancel_ops = { |
| 780 | .rpc_call_done = nlmclnt_cancel_callback, |
| 781 | }; |
| 782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | /* |
| 784 | * Convert an NLM status code to a generic kernel errno |
| 785 | */ |
| 786 | static int |
| 787 | nlm_stat_to_errno(u32 status) |
| 788 | { |
| 789 | switch(status) { |
| 790 | case NLM_LCK_GRANTED: |
| 791 | return 0; |
| 792 | case NLM_LCK_DENIED: |
| 793 | return -EAGAIN; |
| 794 | case NLM_LCK_DENIED_NOLOCKS: |
| 795 | case NLM_LCK_DENIED_GRACE_PERIOD: |
| 796 | return -ENOLCK; |
| 797 | case NLM_LCK_BLOCKED: |
| 798 | printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n"); |
| 799 | return -ENOLCK; |
| 800 | #ifdef CONFIG_LOCKD_V4 |
| 801 | case NLM_DEADLCK: |
| 802 | return -EDEADLK; |
| 803 | case NLM_ROFS: |
| 804 | return -EROFS; |
| 805 | case NLM_STALE_FH: |
| 806 | return -ESTALE; |
| 807 | case NLM_FBIG: |
| 808 | return -EOVERFLOW; |
| 809 | case NLM_FAILED: |
| 810 | return -ENOLCK; |
| 811 | #endif |
| 812 | } |
| 813 | printk(KERN_NOTICE "lockd: unexpected server status %d\n", status); |
| 814 | return -ENOLCK; |
| 815 | } |