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