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