11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/lockd/clntproc.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * RPC procedures for the client side NLM implementation 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de> 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #include <linux/config.h> 101da177e4SLinus Torvalds #include <linux/module.h> 111da177e4SLinus Torvalds #include <linux/types.h> 121da177e4SLinus Torvalds #include <linux/errno.h> 131da177e4SLinus Torvalds #include <linux/fs.h> 141da177e4SLinus Torvalds #include <linux/nfs_fs.h> 151da177e4SLinus Torvalds #include <linux/utsname.h> 161da177e4SLinus Torvalds #include <linux/smp_lock.h> 171da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 181da177e4SLinus Torvalds #include <linux/sunrpc/svc.h> 191da177e4SLinus Torvalds #include <linux/lockd/lockd.h> 201da177e4SLinus Torvalds #include <linux/lockd/sm_inter.h> 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds #define NLMDBG_FACILITY NLMDBG_CLIENT 231da177e4SLinus Torvalds #define NLMCLNT_GRACE_WAIT (5*HZ) 24ecdbf769STrond Myklebust #define NLMCLNT_POLL_TIMEOUT (30*HZ) 25aaaa9942STrond Myklebust #define NLMCLNT_MAX_RETRIES 3 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds static int nlmclnt_test(struct nlm_rqst *, struct file_lock *); 281da177e4SLinus Torvalds static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *); 291da177e4SLinus Torvalds static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *); 301da177e4SLinus Torvalds static int nlm_stat_to_errno(u32 stat); 311da177e4SLinus Torvalds static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host); 3216fb2425STrond Myklebust static int nlmclnt_cancel(struct nlm_host *, int , struct file_lock *); 331da177e4SLinus Torvalds 34963d8fe5STrond Myklebust static const struct rpc_call_ops nlmclnt_unlock_ops; 35963d8fe5STrond Myklebust static const struct rpc_call_ops nlmclnt_cancel_ops; 36963d8fe5STrond Myklebust 371da177e4SLinus Torvalds /* 381da177e4SLinus Torvalds * Cookie counter for NLM requests 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds static u32 nlm_cookie = 0x1234; 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds static inline void nlmclnt_next_cookie(struct nlm_cookie *c) 431da177e4SLinus Torvalds { 441da177e4SLinus Torvalds memcpy(c->data, &nlm_cookie, 4); 451da177e4SLinus Torvalds memset(c->data+4, 0, 4); 461da177e4SLinus Torvalds c->len=4; 471da177e4SLinus Torvalds nlm_cookie++; 481da177e4SLinus Torvalds } 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds static struct nlm_lockowner *nlm_get_lockowner(struct nlm_lockowner *lockowner) 511da177e4SLinus Torvalds { 521da177e4SLinus Torvalds atomic_inc(&lockowner->count); 531da177e4SLinus Torvalds return lockowner; 541da177e4SLinus Torvalds } 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds static void nlm_put_lockowner(struct nlm_lockowner *lockowner) 571da177e4SLinus Torvalds { 581da177e4SLinus Torvalds if (!atomic_dec_and_lock(&lockowner->count, &lockowner->host->h_lock)) 591da177e4SLinus Torvalds return; 601da177e4SLinus Torvalds list_del(&lockowner->list); 611da177e4SLinus Torvalds spin_unlock(&lockowner->host->h_lock); 621da177e4SLinus Torvalds nlm_release_host(lockowner->host); 631da177e4SLinus Torvalds kfree(lockowner); 641da177e4SLinus Torvalds } 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds static inline int nlm_pidbusy(struct nlm_host *host, uint32_t pid) 671da177e4SLinus Torvalds { 681da177e4SLinus Torvalds struct nlm_lockowner *lockowner; 691da177e4SLinus Torvalds list_for_each_entry(lockowner, &host->h_lockowners, list) { 701da177e4SLinus Torvalds if (lockowner->pid == pid) 711da177e4SLinus Torvalds return -EBUSY; 721da177e4SLinus Torvalds } 731da177e4SLinus Torvalds return 0; 741da177e4SLinus Torvalds } 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds static inline uint32_t __nlm_alloc_pid(struct nlm_host *host) 771da177e4SLinus Torvalds { 781da177e4SLinus Torvalds uint32_t res; 791da177e4SLinus Torvalds do { 801da177e4SLinus Torvalds res = host->h_pidcount++; 811da177e4SLinus Torvalds } while (nlm_pidbusy(host, res) < 0); 821da177e4SLinus Torvalds return res; 831da177e4SLinus Torvalds } 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds static struct nlm_lockowner *__nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner) 861da177e4SLinus Torvalds { 871da177e4SLinus Torvalds struct nlm_lockowner *lockowner; 881da177e4SLinus Torvalds list_for_each_entry(lockowner, &host->h_lockowners, list) { 891da177e4SLinus Torvalds if (lockowner->owner != owner) 901da177e4SLinus Torvalds continue; 911da177e4SLinus Torvalds return nlm_get_lockowner(lockowner); 921da177e4SLinus Torvalds } 931da177e4SLinus Torvalds return NULL; 941da177e4SLinus Torvalds } 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds static struct nlm_lockowner *nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner) 971da177e4SLinus Torvalds { 981da177e4SLinus Torvalds struct nlm_lockowner *res, *new = NULL; 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds spin_lock(&host->h_lock); 1011da177e4SLinus Torvalds res = __nlm_find_lockowner(host, owner); 1021da177e4SLinus Torvalds if (res == NULL) { 1031da177e4SLinus Torvalds spin_unlock(&host->h_lock); 1041da177e4SLinus Torvalds new = (struct nlm_lockowner *)kmalloc(sizeof(*new), GFP_KERNEL); 1051da177e4SLinus Torvalds spin_lock(&host->h_lock); 1061da177e4SLinus Torvalds res = __nlm_find_lockowner(host, owner); 1071da177e4SLinus Torvalds if (res == NULL && new != NULL) { 1081da177e4SLinus Torvalds res = new; 1091da177e4SLinus Torvalds atomic_set(&new->count, 1); 1101da177e4SLinus Torvalds new->owner = owner; 1111da177e4SLinus Torvalds new->pid = __nlm_alloc_pid(host); 1121da177e4SLinus Torvalds new->host = nlm_get_host(host); 1131da177e4SLinus Torvalds list_add(&new->list, &host->h_lockowners); 1141da177e4SLinus Torvalds new = NULL; 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds spin_unlock(&host->h_lock); 1181da177e4SLinus Torvalds kfree(new); 1191da177e4SLinus Torvalds return res; 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds /* 1231da177e4SLinus Torvalds * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls 1241da177e4SLinus Torvalds */ 1251da177e4SLinus Torvalds static void nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl) 1261da177e4SLinus Torvalds { 1271da177e4SLinus Torvalds struct nlm_args *argp = &req->a_args; 1281da177e4SLinus Torvalds struct nlm_lock *lock = &argp->lock; 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds nlmclnt_next_cookie(&argp->cookie); 1311da177e4SLinus Torvalds argp->state = nsm_local_state; 1321da177e4SLinus Torvalds memcpy(&lock->fh, NFS_FH(fl->fl_file->f_dentry->d_inode), sizeof(struct nfs_fh)); 1331da177e4SLinus Torvalds lock->caller = system_utsname.nodename; 1341da177e4SLinus Torvalds lock->oh.data = req->a_owner; 1351da177e4SLinus Torvalds lock->oh.len = sprintf(req->a_owner, "%d@%s", 1361da177e4SLinus Torvalds current->pid, system_utsname.nodename); 1371da177e4SLinus Torvalds locks_copy_lock(&lock->fl, fl); 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds static void nlmclnt_release_lockargs(struct nlm_rqst *req) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds struct file_lock *fl = &req->a_args.lock.fl; 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds if (fl->fl_ops && fl->fl_ops->fl_release_private) 1451da177e4SLinus Torvalds fl->fl_ops->fl_release_private(fl); 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds /* 1491da177e4SLinus Torvalds * Initialize arguments for GRANTED call. The nlm_rqst structure 1501da177e4SLinus Torvalds * has been cleared already. 1511da177e4SLinus Torvalds */ 1521da177e4SLinus Torvalds int 1531da177e4SLinus Torvalds nlmclnt_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock) 1541da177e4SLinus Torvalds { 1551da177e4SLinus Torvalds locks_copy_lock(&call->a_args.lock.fl, &lock->fl); 1561da177e4SLinus Torvalds memcpy(&call->a_args.lock.fh, &lock->fh, sizeof(call->a_args.lock.fh)); 1571da177e4SLinus Torvalds call->a_args.lock.caller = system_utsname.nodename; 1581da177e4SLinus Torvalds call->a_args.lock.oh.len = lock->oh.len; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds /* set default data area */ 1611da177e4SLinus Torvalds call->a_args.lock.oh.data = call->a_owner; 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds if (lock->oh.len > NLMCLNT_OHSIZE) { 1641da177e4SLinus Torvalds void *data = kmalloc(lock->oh.len, GFP_KERNEL); 1651da177e4SLinus Torvalds if (!data) { 1661da177e4SLinus Torvalds nlmclnt_freegrantargs(call); 1671da177e4SLinus Torvalds return 0; 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds call->a_args.lock.oh.data = (u8 *) data; 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds memcpy(call->a_args.lock.oh.data, lock->oh.data, lock->oh.len); 1731da177e4SLinus Torvalds return 1; 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds void 1771da177e4SLinus Torvalds nlmclnt_freegrantargs(struct nlm_rqst *call) 1781da177e4SLinus Torvalds { 1791da177e4SLinus Torvalds struct file_lock *fl = &call->a_args.lock.fl; 1801da177e4SLinus Torvalds /* 1811da177e4SLinus Torvalds * Check whether we allocated memory for the owner. 1821da177e4SLinus Torvalds */ 1831da177e4SLinus Torvalds if (call->a_args.lock.oh.data != (u8 *) call->a_owner) { 1841da177e4SLinus Torvalds kfree(call->a_args.lock.oh.data); 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds if (fl->fl_ops && fl->fl_ops->fl_release_private) 1871da177e4SLinus Torvalds fl->fl_ops->fl_release_private(fl); 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds /* 1911da177e4SLinus Torvalds * This is the main entry point for the NLM client. 1921da177e4SLinus Torvalds */ 1931da177e4SLinus Torvalds int 1941da177e4SLinus Torvalds nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl) 1951da177e4SLinus Torvalds { 1961da177e4SLinus Torvalds struct nfs_server *nfssrv = NFS_SERVER(inode); 1971da177e4SLinus Torvalds struct nlm_host *host; 1981da177e4SLinus Torvalds struct nlm_rqst reqst, *call = &reqst; 1991da177e4SLinus Torvalds sigset_t oldset; 2001da177e4SLinus Torvalds unsigned long flags; 2011da177e4SLinus Torvalds int status, proto, vers; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1; 2041da177e4SLinus Torvalds if (NFS_PROTO(inode)->version > 3) { 2051da177e4SLinus Torvalds printk(KERN_NOTICE "NFSv4 file locking not implemented!\n"); 2061da177e4SLinus Torvalds return -ENOLCK; 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds /* Retrieve transport protocol from NFS client */ 2101da177e4SLinus Torvalds proto = NFS_CLIENT(inode)->cl_xprt->prot; 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds if (!(host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers))) 2131da177e4SLinus Torvalds return -ENOLCK; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds /* Create RPC client handle if not there, and copy soft 2161da177e4SLinus Torvalds * and intr flags from NFS client. */ 2171da177e4SLinus Torvalds if (host->h_rpcclnt == NULL) { 2181da177e4SLinus Torvalds struct rpc_clnt *clnt; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds /* Bind an rpc client to this host handle (does not 2211da177e4SLinus Torvalds * perform a portmapper lookup) */ 2221da177e4SLinus Torvalds if (!(clnt = nlm_bind_host(host))) { 2231da177e4SLinus Torvalds status = -ENOLCK; 2241da177e4SLinus Torvalds goto done; 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds clnt->cl_softrtry = nfssrv->client->cl_softrtry; 2271da177e4SLinus Torvalds clnt->cl_intr = nfssrv->client->cl_intr; 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds /* Keep the old signal mask */ 2311da177e4SLinus Torvalds spin_lock_irqsave(¤t->sighand->siglock, flags); 2321da177e4SLinus Torvalds oldset = current->blocked; 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds /* If we're cleaning up locks because the process is exiting, 2351da177e4SLinus Torvalds * perform the RPC call asynchronously. */ 2361da177e4SLinus Torvalds if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) 2371da177e4SLinus Torvalds && fl->fl_type == F_UNLCK 2381da177e4SLinus Torvalds && (current->flags & PF_EXITING)) { 2391da177e4SLinus Torvalds sigfillset(¤t->blocked); /* Mask all signals */ 2401da177e4SLinus Torvalds recalc_sigpending(); 2411da177e4SLinus Torvalds spin_unlock_irqrestore(¤t->sighand->siglock, flags); 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds call = nlmclnt_alloc_call(); 2441da177e4SLinus Torvalds if (!call) { 2451da177e4SLinus Torvalds status = -ENOMEM; 2461da177e4SLinus Torvalds goto out_restore; 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds call->a_flags = RPC_TASK_ASYNC; 2491da177e4SLinus Torvalds } else { 2501da177e4SLinus Torvalds spin_unlock_irqrestore(¤t->sighand->siglock, flags); 2511da177e4SLinus Torvalds memset(call, 0, sizeof(*call)); 2521da177e4SLinus Torvalds locks_init_lock(&call->a_args.lock.fl); 2531da177e4SLinus Torvalds locks_init_lock(&call->a_res.lock.fl); 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds call->a_host = host; 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds nlmclnt_locks_init_private(fl, host); 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds /* Set up the argument struct */ 2601da177e4SLinus Torvalds nlmclnt_setlockargs(call, fl); 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds if (IS_SETLK(cmd) || IS_SETLKW(cmd)) { 2631da177e4SLinus Torvalds if (fl->fl_type != F_UNLCK) { 2641da177e4SLinus Torvalds call->a_args.block = IS_SETLKW(cmd) ? 1 : 0; 2651da177e4SLinus Torvalds status = nlmclnt_lock(call, fl); 2661da177e4SLinus Torvalds } else 2671da177e4SLinus Torvalds status = nlmclnt_unlock(call, fl); 2681da177e4SLinus Torvalds } else if (IS_GETLK(cmd)) 2691da177e4SLinus Torvalds status = nlmclnt_test(call, fl); 2701da177e4SLinus Torvalds else 2711da177e4SLinus Torvalds status = -EINVAL; 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds out_restore: 2741da177e4SLinus Torvalds spin_lock_irqsave(¤t->sighand->siglock, flags); 2751da177e4SLinus Torvalds current->blocked = oldset; 2761da177e4SLinus Torvalds recalc_sigpending(); 2771da177e4SLinus Torvalds spin_unlock_irqrestore(¤t->sighand->siglock, flags); 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds done: 2801da177e4SLinus Torvalds dprintk("lockd: clnt proc returns %d\n", status); 2811da177e4SLinus Torvalds nlm_release_host(host); 2821da177e4SLinus Torvalds return status; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds EXPORT_SYMBOL(nlmclnt_proc); 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds /* 2871da177e4SLinus Torvalds * Allocate an NLM RPC call struct 2881da177e4SLinus Torvalds */ 2891da177e4SLinus Torvalds struct nlm_rqst * 2901da177e4SLinus Torvalds nlmclnt_alloc_call(void) 2911da177e4SLinus Torvalds { 2921da177e4SLinus Torvalds struct nlm_rqst *call; 2931da177e4SLinus Torvalds 294*36943fa4STrond Myklebust for(;;) { 295*36943fa4STrond Myklebust call = kzalloc(sizeof(*call), GFP_KERNEL); 296*36943fa4STrond Myklebust if (call != NULL) { 2971da177e4SLinus Torvalds locks_init_lock(&call->a_args.lock.fl); 2981da177e4SLinus Torvalds locks_init_lock(&call->a_res.lock.fl); 2991da177e4SLinus Torvalds return call; 3001da177e4SLinus Torvalds } 301*36943fa4STrond Myklebust if (signalled()) 302*36943fa4STrond Myklebust break; 3031da177e4SLinus Torvalds printk("nlmclnt_alloc_call: failed, waiting for memory\n"); 304041e0e3bSNishanth Aravamudan schedule_timeout_interruptible(5*HZ); 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds return NULL; 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds static int nlm_wait_on_grace(wait_queue_head_t *queue) 3101da177e4SLinus Torvalds { 3111da177e4SLinus Torvalds DEFINE_WAIT(wait); 3121da177e4SLinus Torvalds int status = -EINTR; 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE); 3151da177e4SLinus Torvalds if (!signalled ()) { 3161da177e4SLinus Torvalds schedule_timeout(NLMCLNT_GRACE_WAIT); 3173e1d1d28SChristoph Lameter try_to_freeze(); 3181da177e4SLinus Torvalds if (!signalled ()) 3191da177e4SLinus Torvalds status = 0; 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds finish_wait(queue, &wait); 3221da177e4SLinus Torvalds return status; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds /* 3261da177e4SLinus Torvalds * Generic NLM call 3271da177e4SLinus Torvalds */ 3281da177e4SLinus Torvalds static int 3291da177e4SLinus Torvalds nlmclnt_call(struct nlm_rqst *req, u32 proc) 3301da177e4SLinus Torvalds { 3311da177e4SLinus Torvalds struct nlm_host *host = req->a_host; 3321da177e4SLinus Torvalds struct rpc_clnt *clnt; 3331da177e4SLinus Torvalds struct nlm_args *argp = &req->a_args; 3341da177e4SLinus Torvalds struct nlm_res *resp = &req->a_res; 3351da177e4SLinus Torvalds struct rpc_message msg = { 3361da177e4SLinus Torvalds .rpc_argp = argp, 3371da177e4SLinus Torvalds .rpc_resp = resp, 3381da177e4SLinus Torvalds }; 3391da177e4SLinus Torvalds int status; 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds dprintk("lockd: call procedure %d on %s\n", 3421da177e4SLinus Torvalds (int)proc, host->h_name); 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds do { 3451da177e4SLinus Torvalds if (host->h_reclaiming && !argp->reclaim) 3461da177e4SLinus Torvalds goto in_grace_period; 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds /* If we have no RPC client yet, create one. */ 3491da177e4SLinus Torvalds if ((clnt = nlm_bind_host(host)) == NULL) 3501da177e4SLinus Torvalds return -ENOLCK; 3511da177e4SLinus Torvalds msg.rpc_proc = &clnt->cl_procinfo[proc]; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds /* Perform the RPC call. If an error occurs, try again */ 3541da177e4SLinus Torvalds if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) { 3551da177e4SLinus Torvalds dprintk("lockd: rpc_call returned error %d\n", -status); 3561da177e4SLinus Torvalds switch (status) { 3571da177e4SLinus Torvalds case -EPROTONOSUPPORT: 3581da177e4SLinus Torvalds status = -EINVAL; 3591da177e4SLinus Torvalds break; 3601da177e4SLinus Torvalds case -ECONNREFUSED: 3611da177e4SLinus Torvalds case -ETIMEDOUT: 3621da177e4SLinus Torvalds case -ENOTCONN: 3631da177e4SLinus Torvalds nlm_rebind_host(host); 3641da177e4SLinus Torvalds status = -EAGAIN; 3651da177e4SLinus Torvalds break; 3661da177e4SLinus Torvalds case -ERESTARTSYS: 3671da177e4SLinus Torvalds return signalled () ? -EINTR : status; 3681da177e4SLinus Torvalds default: 3691da177e4SLinus Torvalds break; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds break; 3721da177e4SLinus Torvalds } else 3731da177e4SLinus Torvalds if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) { 3741da177e4SLinus Torvalds dprintk("lockd: server in grace period\n"); 3751da177e4SLinus Torvalds if (argp->reclaim) { 3761da177e4SLinus Torvalds printk(KERN_WARNING 3771da177e4SLinus Torvalds "lockd: spurious grace period reject?!\n"); 3781da177e4SLinus Torvalds return -ENOLCK; 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds } else { 3811da177e4SLinus Torvalds if (!argp->reclaim) { 3821da177e4SLinus Torvalds /* We appear to be out of the grace period */ 3831da177e4SLinus Torvalds wake_up_all(&host->h_gracewait); 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds dprintk("lockd: server returns status %d\n", resp->status); 3861da177e4SLinus Torvalds return 0; /* Okay, call complete */ 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds in_grace_period: 3901da177e4SLinus Torvalds /* 3911da177e4SLinus Torvalds * The server has rebooted and appears to be in the grace 3921da177e4SLinus Torvalds * period during which locks are only allowed to be 3931da177e4SLinus Torvalds * reclaimed. 3941da177e4SLinus Torvalds * We can only back off and try again later. 3951da177e4SLinus Torvalds */ 3961da177e4SLinus Torvalds status = nlm_wait_on_grace(&host->h_gracewait); 3971da177e4SLinus Torvalds } while (status == 0); 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds return status; 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds /* 4031da177e4SLinus Torvalds * Generic NLM call, async version. 4041da177e4SLinus Torvalds */ 405963d8fe5STrond Myklebust int nlmsvc_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops) 4061da177e4SLinus Torvalds { 4071da177e4SLinus Torvalds struct nlm_host *host = req->a_host; 4081da177e4SLinus Torvalds struct rpc_clnt *clnt; 4091da177e4SLinus Torvalds struct rpc_message msg = { 4101da177e4SLinus Torvalds .rpc_argp = &req->a_args, 4111da177e4SLinus Torvalds .rpc_resp = &req->a_res, 4121da177e4SLinus Torvalds }; 4131da177e4SLinus Torvalds int status; 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds dprintk("lockd: call procedure %d on %s (async)\n", 4161da177e4SLinus Torvalds (int)proc, host->h_name); 4171da177e4SLinus Torvalds 4181da177e4SLinus Torvalds /* If we have no RPC client yet, create one. */ 4191da177e4SLinus Torvalds if ((clnt = nlm_bind_host(host)) == NULL) 4201da177e4SLinus Torvalds return -ENOLCK; 4211da177e4SLinus Torvalds msg.rpc_proc = &clnt->cl_procinfo[proc]; 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds /* bootstrap and kick off the async RPC call */ 424963d8fe5STrond Myklebust status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req); 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds return status; 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 429963d8fe5STrond Myklebust static int nlmclnt_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops) 4301da177e4SLinus Torvalds { 4311da177e4SLinus Torvalds struct nlm_host *host = req->a_host; 4321da177e4SLinus Torvalds struct rpc_clnt *clnt; 4331da177e4SLinus Torvalds struct nlm_args *argp = &req->a_args; 4341da177e4SLinus Torvalds struct nlm_res *resp = &req->a_res; 4351da177e4SLinus Torvalds struct rpc_message msg = { 4361da177e4SLinus Torvalds .rpc_argp = argp, 4371da177e4SLinus Torvalds .rpc_resp = resp, 4381da177e4SLinus Torvalds }; 4391da177e4SLinus Torvalds int status; 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds dprintk("lockd: call procedure %d on %s (async)\n", 4421da177e4SLinus Torvalds (int)proc, host->h_name); 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds /* If we have no RPC client yet, create one. */ 4451da177e4SLinus Torvalds if ((clnt = nlm_bind_host(host)) == NULL) 4461da177e4SLinus Torvalds return -ENOLCK; 4471da177e4SLinus Torvalds msg.rpc_proc = &clnt->cl_procinfo[proc]; 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds /* Increment host refcount */ 4501da177e4SLinus Torvalds nlm_get_host(host); 4511da177e4SLinus Torvalds /* bootstrap and kick off the async RPC call */ 452963d8fe5STrond Myklebust status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req); 4531da177e4SLinus Torvalds if (status < 0) 4541da177e4SLinus Torvalds nlm_release_host(host); 4551da177e4SLinus Torvalds return status; 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds /* 4591da177e4SLinus Torvalds * TEST for the presence of a conflicting lock 4601da177e4SLinus Torvalds */ 4611da177e4SLinus Torvalds static int 4621da177e4SLinus Torvalds nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl) 4631da177e4SLinus Torvalds { 4641da177e4SLinus Torvalds int status; 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds status = nlmclnt_call(req, NLMPROC_TEST); 4671da177e4SLinus Torvalds nlmclnt_release_lockargs(req); 4681da177e4SLinus Torvalds if (status < 0) 4691da177e4SLinus Torvalds return status; 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds status = req->a_res.status; 4721da177e4SLinus Torvalds if (status == NLM_LCK_GRANTED) { 4731da177e4SLinus Torvalds fl->fl_type = F_UNLCK; 4741da177e4SLinus Torvalds } if (status == NLM_LCK_DENIED) { 4751da177e4SLinus Torvalds /* 4761da177e4SLinus Torvalds * Report the conflicting lock back to the application. 4771da177e4SLinus Torvalds */ 4781da177e4SLinus Torvalds locks_copy_lock(fl, &req->a_res.lock.fl); 4791da177e4SLinus Torvalds fl->fl_pid = 0; 4801da177e4SLinus Torvalds } else { 4811da177e4SLinus Torvalds return nlm_stat_to_errno(req->a_res.status); 4821da177e4SLinus Torvalds } 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds return 0; 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl) 4881da177e4SLinus Torvalds { 4891da177e4SLinus Torvalds memcpy(&new->fl_u.nfs_fl, &fl->fl_u.nfs_fl, sizeof(new->fl_u.nfs_fl)); 4901da177e4SLinus Torvalds nlm_get_lockowner(new->fl_u.nfs_fl.owner); 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds static void nlmclnt_locks_release_private(struct file_lock *fl) 4941da177e4SLinus Torvalds { 4951da177e4SLinus Torvalds nlm_put_lockowner(fl->fl_u.nfs_fl.owner); 4961da177e4SLinus Torvalds fl->fl_ops = NULL; 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds static struct file_lock_operations nlmclnt_lock_ops = { 5001da177e4SLinus Torvalds .fl_copy_lock = nlmclnt_locks_copy_lock, 5011da177e4SLinus Torvalds .fl_release_private = nlmclnt_locks_release_private, 5021da177e4SLinus Torvalds }; 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host) 5051da177e4SLinus Torvalds { 5061da177e4SLinus Torvalds BUG_ON(fl->fl_ops != NULL); 5071da177e4SLinus Torvalds fl->fl_u.nfs_fl.state = 0; 5081da177e4SLinus Torvalds fl->fl_u.nfs_fl.flags = 0; 5091da177e4SLinus Torvalds fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner); 5101da177e4SLinus Torvalds fl->fl_ops = &nlmclnt_lock_ops; 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds static void do_vfs_lock(struct file_lock *fl) 5141da177e4SLinus Torvalds { 5151da177e4SLinus Torvalds int res = 0; 5161da177e4SLinus Torvalds switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { 5171da177e4SLinus Torvalds case FL_POSIX: 5181da177e4SLinus Torvalds res = posix_lock_file_wait(fl->fl_file, fl); 5191da177e4SLinus Torvalds break; 5201da177e4SLinus Torvalds case FL_FLOCK: 5211da177e4SLinus Torvalds res = flock_lock_file_wait(fl->fl_file, fl); 5221da177e4SLinus Torvalds break; 5231da177e4SLinus Torvalds default: 5241da177e4SLinus Torvalds BUG(); 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds if (res < 0) 5271da177e4SLinus Torvalds printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", 5281da177e4SLinus Torvalds __FUNCTION__); 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds /* 5321da177e4SLinus Torvalds * LOCK: Try to create a lock 5331da177e4SLinus Torvalds * 5341da177e4SLinus Torvalds * Programmer Harassment Alert 5351da177e4SLinus Torvalds * 5361da177e4SLinus Torvalds * When given a blocking lock request in a sync RPC call, the HPUX lockd 5371da177e4SLinus Torvalds * will faithfully return LCK_BLOCKED but never cares to notify us when 5381da177e4SLinus Torvalds * the lock could be granted. This way, our local process could hang 5391da177e4SLinus Torvalds * around forever waiting for the callback. 5401da177e4SLinus Torvalds * 5411da177e4SLinus Torvalds * Solution A: Implement busy-waiting 5421da177e4SLinus Torvalds * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES}) 5431da177e4SLinus Torvalds * 5441da177e4SLinus Torvalds * For now I am implementing solution A, because I hate the idea of 5451da177e4SLinus Torvalds * re-implementing lockd for a third time in two months. The async 5461da177e4SLinus Torvalds * calls shouldn't be too hard to do, however. 5471da177e4SLinus Torvalds * 5481da177e4SLinus Torvalds * This is one of the lovely things about standards in the NFS area: 5491da177e4SLinus Torvalds * they're so soft and squishy you can't really blame HP for doing this. 5501da177e4SLinus Torvalds */ 5511da177e4SLinus Torvalds static int 5521da177e4SLinus Torvalds nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl) 5531da177e4SLinus Torvalds { 5541da177e4SLinus Torvalds struct nlm_host *host = req->a_host; 5551da177e4SLinus Torvalds struct nlm_res *resp = &req->a_res; 556ecdbf769STrond Myklebust long timeout; 5571da177e4SLinus Torvalds int status; 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds if (!host->h_monitored && nsm_monitor(host) < 0) { 5601da177e4SLinus Torvalds printk(KERN_NOTICE "lockd: failed to monitor %s\n", 5611da177e4SLinus Torvalds host->h_name); 5621da177e4SLinus Torvalds status = -ENOLCK; 5631da177e4SLinus Torvalds goto out; 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds 566ecdbf769STrond Myklebust if (req->a_args.block) { 567ecdbf769STrond Myklebust status = nlmclnt_prepare_block(req, host, fl); 5681da177e4SLinus Torvalds if (status < 0) 5691da177e4SLinus Torvalds goto out; 570ecdbf769STrond Myklebust } 571ecdbf769STrond Myklebust for(;;) { 572ecdbf769STrond Myklebust status = nlmclnt_call(req, NLMPROC_LOCK); 573ecdbf769STrond Myklebust if (status < 0) 574ecdbf769STrond Myklebust goto out_unblock; 575ecdbf769STrond Myklebust if (resp->status != NLM_LCK_BLOCKED) 576ecdbf769STrond Myklebust break; 577ecdbf769STrond Myklebust /* Wait on an NLM blocking lock */ 578ecdbf769STrond Myklebust timeout = nlmclnt_block(req, NLMCLNT_POLL_TIMEOUT); 579ecdbf769STrond Myklebust /* Did a reclaimer thread notify us of a server reboot? */ 580ecdbf769STrond Myklebust if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) 581ecdbf769STrond Myklebust continue; 582ecdbf769STrond Myklebust if (resp->status != NLM_LCK_BLOCKED) 583ecdbf769STrond Myklebust break; 584ecdbf769STrond Myklebust if (timeout >= 0) 585ecdbf769STrond Myklebust continue; 586ecdbf769STrond Myklebust /* We were interrupted. Send a CANCEL request to the server 587ecdbf769STrond Myklebust * and exit 588ecdbf769STrond Myklebust */ 589ecdbf769STrond Myklebust status = (int)timeout; 590ecdbf769STrond Myklebust goto out_unblock; 591ecdbf769STrond Myklebust } 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds if (resp->status == NLM_LCK_GRANTED) { 5941da177e4SLinus Torvalds fl->fl_u.nfs_fl.state = host->h_state; 5951da177e4SLinus Torvalds fl->fl_u.nfs_fl.flags |= NFS_LCK_GRANTED; 5961da177e4SLinus Torvalds fl->fl_flags |= FL_SLEEP; 5971da177e4SLinus Torvalds do_vfs_lock(fl); 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds status = nlm_stat_to_errno(resp->status); 600ecdbf769STrond Myklebust out_unblock: 601ecdbf769STrond Myklebust nlmclnt_finish_block(req); 602ecdbf769STrond Myklebust /* Cancel the blocked request if it is still pending */ 603ecdbf769STrond Myklebust if (resp->status == NLM_LCK_BLOCKED) 60416fb2425STrond Myklebust nlmclnt_cancel(host, req->a_args.block, fl); 6051da177e4SLinus Torvalds out: 6061da177e4SLinus Torvalds nlmclnt_release_lockargs(req); 6071da177e4SLinus Torvalds return status; 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds /* 6111da177e4SLinus Torvalds * RECLAIM: Try to reclaim a lock 6121da177e4SLinus Torvalds */ 6131da177e4SLinus Torvalds int 6141da177e4SLinus Torvalds nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl) 6151da177e4SLinus Torvalds { 6161da177e4SLinus Torvalds struct nlm_rqst reqst, *req; 6171da177e4SLinus Torvalds int status; 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds req = &reqst; 6201da177e4SLinus Torvalds memset(req, 0, sizeof(*req)); 6211da177e4SLinus Torvalds locks_init_lock(&req->a_args.lock.fl); 6221da177e4SLinus Torvalds locks_init_lock(&req->a_res.lock.fl); 6231da177e4SLinus Torvalds req->a_host = host; 6241da177e4SLinus Torvalds req->a_flags = 0; 6251da177e4SLinus Torvalds 6261da177e4SLinus Torvalds /* Set up the argument struct */ 6271da177e4SLinus Torvalds nlmclnt_setlockargs(req, fl); 6281da177e4SLinus Torvalds req->a_args.reclaim = 1; 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0 6311da177e4SLinus Torvalds && req->a_res.status == NLM_LCK_GRANTED) 6321da177e4SLinus Torvalds return 0; 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d " 6351da177e4SLinus Torvalds "(errno %d, status %d)\n", fl->fl_pid, 6361da177e4SLinus Torvalds status, req->a_res.status); 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds /* 6391da177e4SLinus Torvalds * FIXME: This is a serious failure. We can 6401da177e4SLinus Torvalds * 6411da177e4SLinus Torvalds * a. Ignore the problem 6421da177e4SLinus Torvalds * b. Send the owning process some signal (Linux doesn't have 6431da177e4SLinus Torvalds * SIGLOST, though...) 6441da177e4SLinus Torvalds * c. Retry the operation 6451da177e4SLinus Torvalds * 6461da177e4SLinus Torvalds * Until someone comes up with a simple implementation 6471da177e4SLinus Torvalds * for b or c, I'll choose option a. 6481da177e4SLinus Torvalds */ 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds return -ENOLCK; 6511da177e4SLinus Torvalds } 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds /* 6541da177e4SLinus Torvalds * UNLOCK: remove an existing lock 6551da177e4SLinus Torvalds */ 6561da177e4SLinus Torvalds static int 6571da177e4SLinus Torvalds nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl) 6581da177e4SLinus Torvalds { 6591da177e4SLinus Torvalds struct nlm_res *resp = &req->a_res; 6601da177e4SLinus Torvalds int status; 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds /* Clean the GRANTED flag now so the lock doesn't get 6631da177e4SLinus Torvalds * reclaimed while we're stuck in the unlock call. */ 6641da177e4SLinus Torvalds fl->fl_u.nfs_fl.flags &= ~NFS_LCK_GRANTED; 6651da177e4SLinus Torvalds 66630f4e20aSTrond Myklebust /* 66730f4e20aSTrond Myklebust * Note: the server is supposed to either grant us the unlock 66830f4e20aSTrond Myklebust * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either 66930f4e20aSTrond Myklebust * case, we want to unlock. 67030f4e20aSTrond Myklebust */ 67130f4e20aSTrond Myklebust do_vfs_lock(fl); 67230f4e20aSTrond Myklebust 6731da177e4SLinus Torvalds if (req->a_flags & RPC_TASK_ASYNC) { 6741da177e4SLinus Torvalds status = nlmclnt_async_call(req, NLMPROC_UNLOCK, 675963d8fe5STrond Myklebust &nlmclnt_unlock_ops); 6761da177e4SLinus Torvalds /* Hrmf... Do the unlock early since locks_remove_posix() 6771da177e4SLinus Torvalds * really expects us to free the lock synchronously */ 6781da177e4SLinus Torvalds if (status < 0) { 6791da177e4SLinus Torvalds nlmclnt_release_lockargs(req); 6801da177e4SLinus Torvalds kfree(req); 6811da177e4SLinus Torvalds } 6821da177e4SLinus Torvalds return status; 6831da177e4SLinus Torvalds } 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds status = nlmclnt_call(req, NLMPROC_UNLOCK); 6861da177e4SLinus Torvalds nlmclnt_release_lockargs(req); 6871da177e4SLinus Torvalds if (status < 0) 6881da177e4SLinus Torvalds return status; 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds if (resp->status == NLM_LCK_GRANTED) 6911da177e4SLinus Torvalds return 0; 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds if (resp->status != NLM_LCK_DENIED_NOLOCKS) 6941da177e4SLinus Torvalds printk("lockd: unexpected unlock status: %d\n", resp->status); 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds /* What to do now? I'm out of my depth... */ 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds return -ENOLCK; 6991da177e4SLinus Torvalds } 7001da177e4SLinus Torvalds 701963d8fe5STrond Myklebust static void nlmclnt_unlock_callback(struct rpc_task *task, void *data) 7021da177e4SLinus Torvalds { 703963d8fe5STrond Myklebust struct nlm_rqst *req = data; 7041da177e4SLinus Torvalds int status = req->a_res.status; 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds if (RPC_ASSASSINATED(task)) 7071da177e4SLinus Torvalds goto die; 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds if (task->tk_status < 0) { 7101da177e4SLinus Torvalds dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status); 7111da177e4SLinus Torvalds goto retry_rebind; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds if (status == NLM_LCK_DENIED_GRACE_PERIOD) { 7141da177e4SLinus Torvalds rpc_delay(task, NLMCLNT_GRACE_WAIT); 7151da177e4SLinus Torvalds goto retry_unlock; 7161da177e4SLinus Torvalds } 7171da177e4SLinus Torvalds if (status != NLM_LCK_GRANTED) 7181da177e4SLinus Torvalds printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status); 7191da177e4SLinus Torvalds die: 7201da177e4SLinus Torvalds nlm_release_host(req->a_host); 7211da177e4SLinus Torvalds nlmclnt_release_lockargs(req); 7221da177e4SLinus Torvalds kfree(req); 7231da177e4SLinus Torvalds return; 7241da177e4SLinus Torvalds retry_rebind: 7251da177e4SLinus Torvalds nlm_rebind_host(req->a_host); 7261da177e4SLinus Torvalds retry_unlock: 7271da177e4SLinus Torvalds rpc_restart_call(task); 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 730963d8fe5STrond Myklebust static const struct rpc_call_ops nlmclnt_unlock_ops = { 731963d8fe5STrond Myklebust .rpc_call_done = nlmclnt_unlock_callback, 732963d8fe5STrond Myklebust }; 733963d8fe5STrond Myklebust 7341da177e4SLinus Torvalds /* 7351da177e4SLinus Torvalds * Cancel a blocked lock request. 7361da177e4SLinus Torvalds * We always use an async RPC call for this in order not to hang a 7371da177e4SLinus Torvalds * process that has been Ctrl-C'ed. 7381da177e4SLinus Torvalds */ 73916fb2425STrond Myklebust static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl) 7401da177e4SLinus Torvalds { 7411da177e4SLinus Torvalds struct nlm_rqst *req; 7421da177e4SLinus Torvalds unsigned long flags; 7431da177e4SLinus Torvalds sigset_t oldset; 7441da177e4SLinus Torvalds int status; 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds /* Block all signals while setting up call */ 7471da177e4SLinus Torvalds spin_lock_irqsave(¤t->sighand->siglock, flags); 7481da177e4SLinus Torvalds oldset = current->blocked; 7491da177e4SLinus Torvalds sigfillset(¤t->blocked); 7501da177e4SLinus Torvalds recalc_sigpending(); 7511da177e4SLinus Torvalds spin_unlock_irqrestore(¤t->sighand->siglock, flags); 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds req = nlmclnt_alloc_call(); 7541da177e4SLinus Torvalds if (!req) 7551da177e4SLinus Torvalds return -ENOMEM; 7561da177e4SLinus Torvalds req->a_host = host; 7571da177e4SLinus Torvalds req->a_flags = RPC_TASK_ASYNC; 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds nlmclnt_setlockargs(req, fl); 76016fb2425STrond Myklebust req->a_args.block = block; 7611da177e4SLinus Torvalds 762963d8fe5STrond Myklebust status = nlmclnt_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops); 7631da177e4SLinus Torvalds if (status < 0) { 7641da177e4SLinus Torvalds nlmclnt_release_lockargs(req); 7651da177e4SLinus Torvalds kfree(req); 7661da177e4SLinus Torvalds } 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds spin_lock_irqsave(¤t->sighand->siglock, flags); 7691da177e4SLinus Torvalds current->blocked = oldset; 7701da177e4SLinus Torvalds recalc_sigpending(); 7711da177e4SLinus Torvalds spin_unlock_irqrestore(¤t->sighand->siglock, flags); 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds return status; 7741da177e4SLinus Torvalds } 7751da177e4SLinus Torvalds 776963d8fe5STrond Myklebust static void nlmclnt_cancel_callback(struct rpc_task *task, void *data) 7771da177e4SLinus Torvalds { 778963d8fe5STrond Myklebust struct nlm_rqst *req = data; 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds if (RPC_ASSASSINATED(task)) 7811da177e4SLinus Torvalds goto die; 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds if (task->tk_status < 0) { 7841da177e4SLinus Torvalds dprintk("lockd: CANCEL call error %d, retrying.\n", 7851da177e4SLinus Torvalds task->tk_status); 7861da177e4SLinus Torvalds goto retry_cancel; 7871da177e4SLinus Torvalds } 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds dprintk("lockd: cancel status %d (task %d)\n", 7901da177e4SLinus Torvalds req->a_res.status, task->tk_pid); 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds switch (req->a_res.status) { 7931da177e4SLinus Torvalds case NLM_LCK_GRANTED: 7941da177e4SLinus Torvalds case NLM_LCK_DENIED_GRACE_PERIOD: 7951da177e4SLinus Torvalds /* Everything's good */ 7961da177e4SLinus Torvalds break; 7971da177e4SLinus Torvalds case NLM_LCK_DENIED_NOLOCKS: 7981da177e4SLinus Torvalds dprintk("lockd: CANCEL failed (server has no locks)\n"); 7991da177e4SLinus Torvalds goto retry_cancel; 8001da177e4SLinus Torvalds default: 8011da177e4SLinus Torvalds printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n", 8021da177e4SLinus Torvalds req->a_res.status); 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds die: 8061da177e4SLinus Torvalds nlm_release_host(req->a_host); 8071da177e4SLinus Torvalds nlmclnt_release_lockargs(req); 8081da177e4SLinus Torvalds kfree(req); 8091da177e4SLinus Torvalds return; 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds retry_cancel: 812aaaa9942STrond Myklebust /* Don't ever retry more than 3 times */ 813aaaa9942STrond Myklebust if (req->a_retries++ >= NLMCLNT_MAX_RETRIES) 814aaaa9942STrond Myklebust goto die; 8151da177e4SLinus Torvalds nlm_rebind_host(req->a_host); 8161da177e4SLinus Torvalds rpc_restart_call(task); 8171da177e4SLinus Torvalds rpc_delay(task, 30 * HZ); 8181da177e4SLinus Torvalds } 8191da177e4SLinus Torvalds 820963d8fe5STrond Myklebust static const struct rpc_call_ops nlmclnt_cancel_ops = { 821963d8fe5STrond Myklebust .rpc_call_done = nlmclnt_cancel_callback, 822963d8fe5STrond Myklebust }; 823963d8fe5STrond Myklebust 8241da177e4SLinus Torvalds /* 8251da177e4SLinus Torvalds * Convert an NLM status code to a generic kernel errno 8261da177e4SLinus Torvalds */ 8271da177e4SLinus Torvalds static int 8281da177e4SLinus Torvalds nlm_stat_to_errno(u32 status) 8291da177e4SLinus Torvalds { 8301da177e4SLinus Torvalds switch(status) { 8311da177e4SLinus Torvalds case NLM_LCK_GRANTED: 8321da177e4SLinus Torvalds return 0; 8331da177e4SLinus Torvalds case NLM_LCK_DENIED: 8341da177e4SLinus Torvalds return -EAGAIN; 8351da177e4SLinus Torvalds case NLM_LCK_DENIED_NOLOCKS: 8361da177e4SLinus Torvalds case NLM_LCK_DENIED_GRACE_PERIOD: 8371da177e4SLinus Torvalds return -ENOLCK; 8381da177e4SLinus Torvalds case NLM_LCK_BLOCKED: 8391da177e4SLinus Torvalds printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n"); 8401da177e4SLinus Torvalds return -ENOLCK; 8411da177e4SLinus Torvalds #ifdef CONFIG_LOCKD_V4 8421da177e4SLinus Torvalds case NLM_DEADLCK: 8431da177e4SLinus Torvalds return -EDEADLK; 8441da177e4SLinus Torvalds case NLM_ROFS: 8451da177e4SLinus Torvalds return -EROFS; 8461da177e4SLinus Torvalds case NLM_STALE_FH: 8471da177e4SLinus Torvalds return -ESTALE; 8481da177e4SLinus Torvalds case NLM_FBIG: 8491da177e4SLinus Torvalds return -EOVERFLOW; 8501da177e4SLinus Torvalds case NLM_FAILED: 8511da177e4SLinus Torvalds return -ENOLCK; 8521da177e4SLinus Torvalds #endif 8531da177e4SLinus Torvalds } 8541da177e4SLinus Torvalds printk(KERN_NOTICE "lockd: unexpected server status %d\n", status); 8551da177e4SLinus Torvalds return -ENOLCK; 8561da177e4SLinus Torvalds } 857