1 1.2 pgoyette /* $NetBSD: nlm_advlock.c,v 1.2 2016/12/13 21:58:17 pgoyette Exp $ */ 2 1.1 dholland /*- 3 1.1 dholland * Copyright (c) 2008 Isilon Inc http://www.isilon.com/ 4 1.1 dholland * Authors: Doug Rabson <dfr (at) rabson.org> 5 1.1 dholland * Developed with Red Inc: Alfred Perlstein <alfred (at) freebsd.org> 6 1.1 dholland * 7 1.1 dholland * Redistribution and use in source and binary forms, with or without 8 1.1 dholland * modification, are permitted provided that the following conditions 9 1.1 dholland * are met: 10 1.1 dholland * 1. Redistributions of source code must retain the above copyright 11 1.1 dholland * notice, this list of conditions and the following disclaimer. 12 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 dholland * notice, this list of conditions and the following disclaimer in the 14 1.1 dholland * documentation and/or other materials provided with the distribution. 15 1.1 dholland * 16 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 1.1 dholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 1.1 dholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 1.1 dholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 1.1 dholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.1 dholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 1.1 dholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 dholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 dholland * SUCH DAMAGE. 27 1.1 dholland */ 28 1.1 dholland 29 1.1 dholland #include <sys/cdefs.h> 30 1.2 pgoyette /* __FBSDID("FreeBSD: head/sys/nlm/nlm_advlock.c 302216 2016-06-26 20:08:42Z kib "); */ 31 1.2 pgoyette __RCSID("$NetBSD: nlm_advlock.c,v 1.2 2016/12/13 21:58:17 pgoyette Exp $"); 32 1.1 dholland 33 1.1 dholland #include <sys/param.h> 34 1.1 dholland #include <sys/fcntl.h> 35 1.1 dholland #include <sys/jail.h> 36 1.1 dholland #include <sys/kernel.h> 37 1.1 dholland #include <sys/limits.h> 38 1.1 dholland #include <sys/lock.h> 39 1.1 dholland #include <sys/lockf.h> 40 1.1 dholland #include <sys/malloc.h> 41 1.1 dholland #include <sys/mbuf.h> 42 1.1 dholland #include <sys/mount.h> 43 1.1 dholland #include <sys/mutex.h> 44 1.1 dholland #include <sys/proc.h> 45 1.1 dholland #include <sys/socket.h> 46 1.1 dholland #include <sys/syslog.h> 47 1.1 dholland #include <sys/systm.h> 48 1.1 dholland #include <sys/unistd.h> 49 1.1 dholland #include <sys/vnode.h> 50 1.1 dholland 51 1.2 pgoyette #include <fs/nfs/common/nfsproto.h> 52 1.2 pgoyette #include <fs/nfs/client/nfs.h> 53 1.2 pgoyette #include <fs/nfs/client/nfsmount.h> 54 1.1 dholland 55 1.2 pgoyette #include <fs/nfs/nlm/nlm_prot.h> 56 1.2 pgoyette #include <fs/nfs/nlm/nlm.h> 57 1.1 dholland 58 1.1 dholland /* 59 1.1 dholland * We need to keep track of the svid values used for F_FLOCK locks. 60 1.1 dholland */ 61 1.1 dholland struct nlm_file_svid { 62 1.1 dholland int ns_refs; /* thread count + 1 if active */ 63 1.1 dholland int ns_svid; /* on-the-wire SVID for this file */ 64 1.1 dholland struct ucred *ns_ucred; /* creds to use for lock recovery */ 65 1.1 dholland void *ns_id; /* local struct file pointer */ 66 1.1 dholland bool_t ns_active; /* TRUE if we own a lock */ 67 1.1 dholland LIST_ENTRY(nlm_file_svid) ns_link; 68 1.1 dholland }; 69 1.1 dholland LIST_HEAD(nlm_file_svid_list, nlm_file_svid); 70 1.1 dholland 71 1.1 dholland #define NLM_SVID_HASH_SIZE 256 72 1.1 dholland struct nlm_file_svid_list nlm_file_svids[NLM_SVID_HASH_SIZE]; 73 1.1 dholland 74 1.1 dholland struct mtx nlm_svid_lock; 75 1.1 dholland static struct unrhdr *nlm_svid_allocator; 76 1.1 dholland static volatile u_int nlm_xid = 1; 77 1.1 dholland 78 1.1 dholland static int nlm_setlock(struct nlm_host *host, struct rpc_callextra *ext, 79 1.1 dholland rpcvers_t vers, struct timeval *timo, int retries, 80 1.1 dholland struct vnode *vp, int op, struct flock *fl, int flags, 81 1.1 dholland int svid, size_t fhlen, void *fh, off_t size, bool_t reclaim); 82 1.1 dholland static int nlm_clearlock(struct nlm_host *host, struct rpc_callextra *ext, 83 1.1 dholland rpcvers_t vers, struct timeval *timo, int retries, 84 1.1 dholland struct vnode *vp, int op, struct flock *fl, int flags, 85 1.1 dholland int svid, size_t fhlen, void *fh, off_t size); 86 1.1 dholland static int nlm_getlock(struct nlm_host *host, struct rpc_callextra *ext, 87 1.1 dholland rpcvers_t vers, struct timeval *timo, int retries, 88 1.1 dholland struct vnode *vp, int op, struct flock *fl, int flags, 89 1.1 dholland int svid, size_t fhlen, void *fh, off_t size); 90 1.1 dholland static int nlm_map_status(nlm4_stats stat); 91 1.1 dholland static struct nlm_file_svid *nlm_find_svid(void *id); 92 1.1 dholland static void nlm_free_svid(struct nlm_file_svid *nf); 93 1.1 dholland static int nlm_init_lock(struct flock *fl, int flags, int svid, 94 1.1 dholland rpcvers_t vers, size_t fhlen, void *fh, off_t size, 95 1.1 dholland struct nlm4_lock *lock, char oh_space[32]); 96 1.1 dholland 97 1.1 dholland static void 98 1.1 dholland nlm_client_init(void *dummy) 99 1.1 dholland { 100 1.1 dholland int i; 101 1.1 dholland 102 1.1 dholland mtx_init(&nlm_svid_lock, "NLM svid lock", NULL, MTX_DEF); 103 1.1 dholland /* pid_max cannot be greater than PID_MAX */ 104 1.1 dholland nlm_svid_allocator = new_unrhdr(PID_MAX + 2, INT_MAX, &nlm_svid_lock); 105 1.1 dholland for (i = 0; i < NLM_SVID_HASH_SIZE; i++) 106 1.1 dholland LIST_INIT(&nlm_file_svids[i]); 107 1.1 dholland } 108 1.1 dholland SYSINIT(nlm_client_init, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_client_init, NULL); 109 1.1 dholland 110 1.1 dholland static int 111 1.1 dholland nlm_msg(struct thread *td, const char *server, const char *msg, int error) 112 1.1 dholland { 113 1.1 dholland struct proc *p; 114 1.1 dholland 115 1.1 dholland p = td ? td->td_proc : NULL; 116 1.1 dholland if (error) { 117 1.1 dholland tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n", server, 118 1.1 dholland msg, error); 119 1.1 dholland } else { 120 1.1 dholland tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg); 121 1.1 dholland } 122 1.1 dholland return (0); 123 1.1 dholland } 124 1.1 dholland 125 1.1 dholland struct nlm_feedback_arg { 126 1.1 dholland bool_t nf_printed; 127 1.1 dholland struct nfsmount *nf_nmp; 128 1.1 dholland }; 129 1.1 dholland 130 1.1 dholland static void 131 1.1 dholland nlm_down(struct nlm_feedback_arg *nf, struct thread *td, 132 1.1 dholland const char *msg, int error) 133 1.1 dholland { 134 1.1 dholland struct nfsmount *nmp = nf->nf_nmp; 135 1.1 dholland 136 1.1 dholland if (nmp == NULL) 137 1.1 dholland return; 138 1.1 dholland mtx_lock(&nmp->nm_mtx); 139 1.1 dholland if (!(nmp->nm_state & NFSSTA_LOCKTIMEO)) { 140 1.1 dholland nmp->nm_state |= NFSSTA_LOCKTIMEO; 141 1.1 dholland mtx_unlock(&nmp->nm_mtx); 142 1.1 dholland vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid, 143 1.1 dholland VQ_NOTRESPLOCK, 0); 144 1.1 dholland } else { 145 1.1 dholland mtx_unlock(&nmp->nm_mtx); 146 1.1 dholland } 147 1.1 dholland 148 1.1 dholland nf->nf_printed = TRUE; 149 1.1 dholland nlm_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error); 150 1.1 dholland } 151 1.1 dholland 152 1.1 dholland static void 153 1.1 dholland nlm_up(struct nlm_feedback_arg *nf, struct thread *td, 154 1.1 dholland const char *msg) 155 1.1 dholland { 156 1.1 dholland struct nfsmount *nmp = nf->nf_nmp; 157 1.1 dholland 158 1.1 dholland if (!nf->nf_printed) 159 1.1 dholland return; 160 1.1 dholland 161 1.1 dholland nlm_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0); 162 1.1 dholland 163 1.1 dholland mtx_lock(&nmp->nm_mtx); 164 1.1 dholland if (nmp->nm_state & NFSSTA_LOCKTIMEO) { 165 1.1 dholland nmp->nm_state &= ~NFSSTA_LOCKTIMEO; 166 1.1 dholland mtx_unlock(&nmp->nm_mtx); 167 1.1 dholland vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid, 168 1.1 dholland VQ_NOTRESPLOCK, 1); 169 1.1 dholland } else { 170 1.1 dholland mtx_unlock(&nmp->nm_mtx); 171 1.1 dholland } 172 1.1 dholland } 173 1.1 dholland 174 1.1 dholland static void 175 1.1 dholland nlm_feedback(int type, int proc, void *arg) 176 1.1 dholland { 177 1.1 dholland struct thread *td = curthread; 178 1.1 dholland struct nlm_feedback_arg *nf = (struct nlm_feedback_arg *) arg; 179 1.1 dholland 180 1.1 dholland switch (type) { 181 1.1 dholland case FEEDBACK_REXMIT2: 182 1.1 dholland case FEEDBACK_RECONNECT: 183 1.1 dholland nlm_down(nf, td, "lockd not responding", 0); 184 1.1 dholland break; 185 1.1 dholland 186 1.1 dholland case FEEDBACK_OK: 187 1.1 dholland nlm_up(nf, td, "lockd is alive again"); 188 1.1 dholland break; 189 1.1 dholland } 190 1.1 dholland } 191 1.1 dholland 192 1.1 dholland /* 193 1.1 dholland * nlm_advlock -- 194 1.1 dholland * NFS advisory byte-level locks. 195 1.1 dholland */ 196 1.1 dholland static int 197 1.1 dholland nlm_advlock_internal(struct vnode *vp, void *id, int op, struct flock *fl, 198 1.1 dholland int flags, bool_t reclaim, bool_t unlock_vp) 199 1.1 dholland { 200 1.1 dholland struct thread *td = curthread; 201 1.1 dholland struct nfsmount *nmp; 202 1.1 dholland off_t size; 203 1.1 dholland size_t fhlen; 204 1.1 dholland union nfsfh fh; 205 1.1 dholland struct sockaddr *sa; 206 1.1 dholland struct sockaddr_storage ss; 207 1.1 dholland char servername[MNAMELEN]; 208 1.1 dholland struct timeval timo; 209 1.1 dholland int retries; 210 1.1 dholland rpcvers_t vers; 211 1.1 dholland struct nlm_host *host; 212 1.1 dholland struct rpc_callextra ext; 213 1.1 dholland struct nlm_feedback_arg nf; 214 1.1 dholland AUTH *auth; 215 1.2 pgoyette struct ucred *cred, *cred1; 216 1.1 dholland struct nlm_file_svid *ns; 217 1.1 dholland int svid; 218 1.1 dholland int error; 219 1.1 dholland int is_v3; 220 1.1 dholland 221 1.1 dholland ASSERT_VOP_LOCKED(vp, "nlm_advlock_1"); 222 1.1 dholland 223 1.1 dholland nmp = VFSTONFS(vp->v_mount); 224 1.1 dholland /* 225 1.1 dholland * Push any pending writes to the server and flush our cache 226 1.1 dholland * so that if we are contending with another machine for a 227 1.1 dholland * file, we get whatever they wrote and vice-versa. 228 1.1 dholland */ 229 1.1 dholland if (op == F_SETLK || op == F_UNLCK) 230 1.1 dholland nmp->nm_vinvalbuf(vp, V_SAVE, td, 1); 231 1.1 dholland 232 1.1 dholland strcpy(servername, nmp->nm_hostname); 233 1.1 dholland nmp->nm_getinfo(vp, fh.fh_bytes, &fhlen, &ss, &is_v3, &size, &timo); 234 1.1 dholland sa = (struct sockaddr *) &ss; 235 1.1 dholland if (is_v3 != 0) 236 1.1 dholland vers = NLM_VERS4; 237 1.1 dholland else 238 1.1 dholland vers = NLM_VERS; 239 1.1 dholland 240 1.1 dholland if (nmp->nm_flag & NFSMNT_SOFT) 241 1.1 dholland retries = nmp->nm_retry; 242 1.1 dholland else 243 1.1 dholland retries = INT_MAX; 244 1.1 dholland 245 1.1 dholland /* 246 1.1 dholland * We need to switch to mount-point creds so that we can send 247 1.2 pgoyette * packets from a privileged port. Reference mnt_cred and 248 1.2 pgoyette * switch to them before unlocking the vnode, since mount 249 1.2 pgoyette * point could be unmounted right after unlock. 250 1.1 dholland */ 251 1.1 dholland cred = td->td_ucred; 252 1.1 dholland td->td_ucred = vp->v_mount->mnt_cred; 253 1.2 pgoyette crhold(td->td_ucred); 254 1.2 pgoyette if (unlock_vp) 255 1.2 pgoyette VOP_UNLOCK(vp, 0); 256 1.1 dholland 257 1.1 dholland host = nlm_find_host_by_name(servername, sa, vers); 258 1.1 dholland auth = authunix_create(cred); 259 1.1 dholland memset(&ext, 0, sizeof(ext)); 260 1.1 dholland 261 1.1 dholland nf.nf_printed = FALSE; 262 1.1 dholland nf.nf_nmp = nmp; 263 1.1 dholland ext.rc_auth = auth; 264 1.1 dholland 265 1.1 dholland ext.rc_feedback = nlm_feedback; 266 1.1 dholland ext.rc_feedback_arg = &nf; 267 1.1 dholland ext.rc_timers = NULL; 268 1.1 dholland 269 1.1 dholland ns = NULL; 270 1.1 dholland if (flags & F_FLOCK) { 271 1.1 dholland ns = nlm_find_svid(id); 272 1.1 dholland KASSERT(fl->l_start == 0 && fl->l_len == 0, 273 1.1 dholland ("F_FLOCK lock requests must be whole-file locks")); 274 1.1 dholland if (!ns->ns_ucred) { 275 1.1 dholland /* 276 1.1 dholland * Remember the creds used for locking in case 277 1.1 dholland * we need to recover the lock later. 278 1.1 dholland */ 279 1.1 dholland ns->ns_ucred = crdup(cred); 280 1.1 dholland } 281 1.1 dholland svid = ns->ns_svid; 282 1.1 dholland } else if (flags & F_REMOTE) { 283 1.1 dholland /* 284 1.1 dholland * If we are recovering after a server restart or 285 1.1 dholland * trashing locks on a force unmount, use the same 286 1.1 dholland * svid as last time. 287 1.1 dholland */ 288 1.1 dholland svid = fl->l_pid; 289 1.1 dholland } else { 290 1.1 dholland svid = ((struct proc *) id)->p_pid; 291 1.1 dholland } 292 1.1 dholland 293 1.1 dholland switch(op) { 294 1.1 dholland case F_SETLK: 295 1.1 dholland if ((flags & (F_FLOCK|F_WAIT)) == (F_FLOCK|F_WAIT) 296 1.1 dholland && fl->l_type == F_WRLCK) { 297 1.1 dholland /* 298 1.1 dholland * The semantics for flock(2) require that any 299 1.1 dholland * shared lock on the file must be released 300 1.1 dholland * before an exclusive lock is granted. The 301 1.1 dholland * local locking code interprets this by 302 1.1 dholland * unlocking the file before sleeping on a 303 1.1 dholland * blocked exclusive lock request. We 304 1.1 dholland * approximate this by first attempting 305 1.1 dholland * non-blocking and if that fails, we unlock 306 1.1 dholland * the file and block. 307 1.1 dholland */ 308 1.1 dholland error = nlm_setlock(host, &ext, vers, &timo, retries, 309 1.1 dholland vp, F_SETLK, fl, flags & ~F_WAIT, 310 1.1 dholland svid, fhlen, &fh.fh_bytes, size, reclaim); 311 1.1 dholland if (error == EAGAIN) { 312 1.1 dholland fl->l_type = F_UNLCK; 313 1.1 dholland error = nlm_clearlock(host, &ext, vers, &timo, 314 1.1 dholland retries, vp, F_UNLCK, fl, flags, 315 1.1 dholland svid, fhlen, &fh.fh_bytes, size); 316 1.1 dholland fl->l_type = F_WRLCK; 317 1.1 dholland if (!error) { 318 1.1 dholland mtx_lock(&nlm_svid_lock); 319 1.1 dholland if (ns->ns_active) { 320 1.1 dholland ns->ns_refs--; 321 1.1 dholland ns->ns_active = FALSE; 322 1.1 dholland } 323 1.1 dholland mtx_unlock(&nlm_svid_lock); 324 1.1 dholland flags |= F_WAIT; 325 1.1 dholland error = nlm_setlock(host, &ext, vers, 326 1.1 dholland &timo, retries, vp, F_SETLK, fl, 327 1.1 dholland flags, svid, fhlen, &fh.fh_bytes, 328 1.1 dholland size, reclaim); 329 1.1 dholland } 330 1.1 dholland } 331 1.1 dholland } else { 332 1.1 dholland error = nlm_setlock(host, &ext, vers, &timo, retries, 333 1.1 dholland vp, op, fl, flags, svid, fhlen, &fh.fh_bytes, 334 1.1 dholland size, reclaim); 335 1.1 dholland } 336 1.1 dholland if (!error && ns) { 337 1.1 dholland mtx_lock(&nlm_svid_lock); 338 1.1 dholland if (!ns->ns_active) { 339 1.1 dholland /* 340 1.1 dholland * Add one to the reference count to 341 1.1 dholland * hold onto the SVID for the lifetime 342 1.1 dholland * of the lock. Note that since 343 1.1 dholland * F_FLOCK only supports whole-file 344 1.1 dholland * locks, there can only be one active 345 1.1 dholland * lock for this SVID. 346 1.1 dholland */ 347 1.1 dholland ns->ns_refs++; 348 1.1 dholland ns->ns_active = TRUE; 349 1.1 dholland } 350 1.1 dholland mtx_unlock(&nlm_svid_lock); 351 1.1 dholland } 352 1.1 dholland break; 353 1.1 dholland 354 1.1 dholland case F_UNLCK: 355 1.1 dholland error = nlm_clearlock(host, &ext, vers, &timo, retries, 356 1.1 dholland vp, op, fl, flags, svid, fhlen, &fh.fh_bytes, size); 357 1.1 dholland if (!error && ns) { 358 1.1 dholland mtx_lock(&nlm_svid_lock); 359 1.1 dholland if (ns->ns_active) { 360 1.1 dholland ns->ns_refs--; 361 1.1 dholland ns->ns_active = FALSE; 362 1.1 dholland } 363 1.1 dholland mtx_unlock(&nlm_svid_lock); 364 1.1 dholland } 365 1.1 dholland break; 366 1.1 dholland 367 1.1 dholland case F_GETLK: 368 1.1 dholland error = nlm_getlock(host, &ext, vers, &timo, retries, 369 1.1 dholland vp, op, fl, flags, svid, fhlen, &fh.fh_bytes, size); 370 1.1 dholland break; 371 1.1 dholland 372 1.1 dholland default: 373 1.1 dholland error = EINVAL; 374 1.1 dholland break; 375 1.1 dholland } 376 1.1 dholland 377 1.1 dholland if (ns) 378 1.1 dholland nlm_free_svid(ns); 379 1.1 dholland 380 1.2 pgoyette cred1 = td->td_ucred; 381 1.1 dholland td->td_ucred = cred; 382 1.2 pgoyette crfree(cred1); 383 1.1 dholland AUTH_DESTROY(auth); 384 1.1 dholland 385 1.1 dholland nlm_host_release(host); 386 1.1 dholland 387 1.1 dholland return (error); 388 1.1 dholland } 389 1.1 dholland 390 1.1 dholland int 391 1.1 dholland nlm_advlock(struct vop_advlock_args *ap) 392 1.1 dholland { 393 1.1 dholland 394 1.1 dholland return (nlm_advlock_internal(ap->a_vp, ap->a_id, ap->a_op, ap->a_fl, 395 1.1 dholland ap->a_flags, FALSE, TRUE)); 396 1.1 dholland } 397 1.1 dholland 398 1.1 dholland /* 399 1.1 dholland * Set the creds of td to the creds of the given lock's owner. The new 400 1.1 dholland * creds reference count will be incremented via crhold. The caller is 401 1.1 dholland * responsible for calling crfree and restoring td's original creds. 402 1.1 dholland */ 403 1.1 dholland static void 404 1.1 dholland nlm_set_creds_for_lock(struct thread *td, struct flock *fl) 405 1.1 dholland { 406 1.1 dholland int i; 407 1.1 dholland struct nlm_file_svid *ns; 408 1.1 dholland struct proc *p; 409 1.1 dholland struct ucred *cred; 410 1.1 dholland 411 1.1 dholland cred = NULL; 412 1.1 dholland if (fl->l_pid > PID_MAX) { 413 1.1 dholland /* 414 1.1 dholland * If this was originally a F_FLOCK-style lock, we 415 1.1 dholland * recorded the creds used when it was originally 416 1.1 dholland * locked in the nlm_file_svid structure. 417 1.1 dholland */ 418 1.1 dholland mtx_lock(&nlm_svid_lock); 419 1.1 dholland for (i = 0; i < NLM_SVID_HASH_SIZE; i++) { 420 1.1 dholland for (ns = LIST_FIRST(&nlm_file_svids[i]); ns; 421 1.1 dholland ns = LIST_NEXT(ns, ns_link)) { 422 1.1 dholland if (ns->ns_svid == fl->l_pid) { 423 1.1 dholland cred = crhold(ns->ns_ucred); 424 1.1 dholland break; 425 1.1 dholland } 426 1.1 dholland } 427 1.1 dholland } 428 1.1 dholland mtx_unlock(&nlm_svid_lock); 429 1.1 dholland } else { 430 1.1 dholland /* 431 1.1 dholland * This lock is owned by a process. Get a reference to 432 1.1 dholland * the process creds. 433 1.1 dholland */ 434 1.1 dholland p = pfind(fl->l_pid); 435 1.1 dholland if (p) { 436 1.1 dholland cred = crhold(p->p_ucred); 437 1.1 dholland PROC_UNLOCK(p); 438 1.1 dholland } 439 1.1 dholland } 440 1.1 dholland 441 1.1 dholland /* 442 1.1 dholland * If we can't find a cred, fall back on the recovery 443 1.1 dholland * thread's cred. 444 1.1 dholland */ 445 1.1 dholland if (!cred) { 446 1.1 dholland cred = crhold(td->td_ucred); 447 1.1 dholland } 448 1.1 dholland 449 1.1 dholland td->td_ucred = cred; 450 1.1 dholland } 451 1.1 dholland 452 1.1 dholland static int 453 1.1 dholland nlm_reclaim_free_lock(struct vnode *vp, struct flock *fl, void *arg) 454 1.1 dholland { 455 1.1 dholland struct flock newfl; 456 1.1 dholland struct thread *td = curthread; 457 1.1 dholland struct ucred *oldcred; 458 1.1 dholland int error; 459 1.1 dholland 460 1.1 dholland newfl = *fl; 461 1.1 dholland newfl.l_type = F_UNLCK; 462 1.1 dholland 463 1.1 dholland oldcred = td->td_ucred; 464 1.1 dholland nlm_set_creds_for_lock(td, &newfl); 465 1.1 dholland 466 1.1 dholland error = nlm_advlock_internal(vp, NULL, F_UNLCK, &newfl, F_REMOTE, 467 1.1 dholland FALSE, FALSE); 468 1.1 dholland 469 1.1 dholland crfree(td->td_ucred); 470 1.1 dholland td->td_ucred = oldcred; 471 1.1 dholland 472 1.1 dholland return (error); 473 1.1 dholland } 474 1.1 dholland 475 1.1 dholland int 476 1.1 dholland nlm_reclaim(struct vop_reclaim_args *ap) 477 1.1 dholland { 478 1.1 dholland 479 1.1 dholland nlm_cancel_wait(ap->a_vp); 480 1.1 dholland lf_iteratelocks_vnode(ap->a_vp, nlm_reclaim_free_lock, NULL); 481 1.1 dholland return (0); 482 1.1 dholland } 483 1.1 dholland 484 1.1 dholland struct nlm_recovery_context { 485 1.1 dholland struct nlm_host *nr_host; /* host we are recovering */ 486 1.1 dholland int nr_state; /* remote NSM state for recovery */ 487 1.1 dholland }; 488 1.1 dholland 489 1.1 dholland static int 490 1.1 dholland nlm_client_recover_lock(struct vnode *vp, struct flock *fl, void *arg) 491 1.1 dholland { 492 1.1 dholland struct nlm_recovery_context *nr = (struct nlm_recovery_context *) arg; 493 1.1 dholland struct thread *td = curthread; 494 1.1 dholland struct ucred *oldcred; 495 1.1 dholland int state, error; 496 1.1 dholland 497 1.1 dholland /* 498 1.1 dholland * If the remote NSM state changes during recovery, the host 499 1.1 dholland * must have rebooted a second time. In that case, we must 500 1.1 dholland * restart the recovery. 501 1.1 dholland */ 502 1.1 dholland state = nlm_host_get_state(nr->nr_host); 503 1.1 dholland if (nr->nr_state != state) 504 1.1 dholland return (ERESTART); 505 1.1 dholland 506 1.1 dholland error = vn_lock(vp, LK_SHARED); 507 1.1 dholland if (error) 508 1.1 dholland return (error); 509 1.1 dholland 510 1.1 dholland oldcred = td->td_ucred; 511 1.1 dholland nlm_set_creds_for_lock(td, fl); 512 1.1 dholland 513 1.1 dholland error = nlm_advlock_internal(vp, NULL, F_SETLK, fl, F_REMOTE, 514 1.1 dholland TRUE, TRUE); 515 1.1 dholland 516 1.1 dholland crfree(td->td_ucred); 517 1.1 dholland td->td_ucred = oldcred; 518 1.1 dholland 519 1.1 dholland return (error); 520 1.1 dholland } 521 1.1 dholland 522 1.1 dholland void 523 1.1 dholland nlm_client_recovery(struct nlm_host *host) 524 1.1 dholland { 525 1.1 dholland struct nlm_recovery_context nr; 526 1.1 dholland int sysid, error; 527 1.1 dholland 528 1.1 dholland sysid = NLM_SYSID_CLIENT | nlm_host_get_sysid(host); 529 1.1 dholland do { 530 1.1 dholland nr.nr_host = host; 531 1.1 dholland nr.nr_state = nlm_host_get_state(host); 532 1.1 dholland error = lf_iteratelocks_sysid(sysid, 533 1.1 dholland nlm_client_recover_lock, &nr); 534 1.1 dholland } while (error == ERESTART); 535 1.1 dholland } 536 1.1 dholland 537 1.1 dholland static void 538 1.1 dholland nlm_convert_to_nlm_lock(struct nlm_lock *dst, struct nlm4_lock *src) 539 1.1 dholland { 540 1.1 dholland 541 1.1 dholland dst->caller_name = src->caller_name; 542 1.1 dholland dst->fh = src->fh; 543 1.1 dholland dst->oh = src->oh; 544 1.1 dholland dst->svid = src->svid; 545 1.1 dholland dst->l_offset = src->l_offset; 546 1.1 dholland dst->l_len = src->l_len; 547 1.1 dholland } 548 1.1 dholland 549 1.1 dholland static void 550 1.1 dholland nlm_convert_to_nlm4_holder(struct nlm4_holder *dst, struct nlm_holder *src) 551 1.1 dholland { 552 1.1 dholland 553 1.1 dholland dst->exclusive = src->exclusive; 554 1.1 dholland dst->svid = src->svid; 555 1.1 dholland dst->oh = src->oh; 556 1.1 dholland dst->l_offset = src->l_offset; 557 1.1 dholland dst->l_len = src->l_len; 558 1.1 dholland } 559 1.1 dholland 560 1.1 dholland static void 561 1.1 dholland nlm_convert_to_nlm4_res(struct nlm4_res *dst, struct nlm_res *src) 562 1.1 dholland { 563 1.1 dholland dst->cookie = src->cookie; 564 1.1 dholland dst->stat.stat = (enum nlm4_stats) src->stat.stat; 565 1.1 dholland } 566 1.1 dholland 567 1.1 dholland static enum clnt_stat 568 1.1 dholland nlm_test_rpc(rpcvers_t vers, nlm4_testargs *args, nlm4_testres *res, CLIENT *client, 569 1.1 dholland struct rpc_callextra *ext, struct timeval timo) 570 1.1 dholland { 571 1.1 dholland if (vers == NLM_VERS4) { 572 1.1 dholland return nlm4_test_4(args, res, client, ext, timo); 573 1.1 dholland } else { 574 1.1 dholland nlm_testargs args1; 575 1.1 dholland nlm_testres res1; 576 1.1 dholland enum clnt_stat stat; 577 1.1 dholland 578 1.1 dholland args1.cookie = args->cookie; 579 1.1 dholland args1.exclusive = args->exclusive; 580 1.1 dholland nlm_convert_to_nlm_lock(&args1.alock, &args->alock); 581 1.1 dholland memset(&res1, 0, sizeof(res1)); 582 1.1 dholland 583 1.1 dholland stat = nlm_test_1(&args1, &res1, client, ext, timo); 584 1.1 dholland 585 1.1 dholland if (stat == RPC_SUCCESS) { 586 1.1 dholland res->cookie = res1.cookie; 587 1.1 dholland res->stat.stat = (enum nlm4_stats) res1.stat.stat; 588 1.1 dholland if (res1.stat.stat == nlm_denied) 589 1.1 dholland nlm_convert_to_nlm4_holder( 590 1.1 dholland &res->stat.nlm4_testrply_u.holder, 591 1.1 dholland &res1.stat.nlm_testrply_u.holder); 592 1.1 dholland } 593 1.1 dholland 594 1.1 dholland return (stat); 595 1.1 dholland } 596 1.1 dholland } 597 1.1 dholland 598 1.1 dholland static enum clnt_stat 599 1.1 dholland nlm_lock_rpc(rpcvers_t vers, nlm4_lockargs *args, nlm4_res *res, CLIENT *client, 600 1.1 dholland struct rpc_callextra *ext, struct timeval timo) 601 1.1 dholland { 602 1.1 dholland if (vers == NLM_VERS4) { 603 1.1 dholland return nlm4_lock_4(args, res, client, ext, timo); 604 1.1 dholland } else { 605 1.1 dholland nlm_lockargs args1; 606 1.1 dholland nlm_res res1; 607 1.1 dholland enum clnt_stat stat; 608 1.1 dholland 609 1.1 dholland args1.cookie = args->cookie; 610 1.1 dholland args1.block = args->block; 611 1.1 dholland args1.exclusive = args->exclusive; 612 1.1 dholland nlm_convert_to_nlm_lock(&args1.alock, &args->alock); 613 1.1 dholland args1.reclaim = args->reclaim; 614 1.1 dholland args1.state = args->state; 615 1.1 dholland memset(&res1, 0, sizeof(res1)); 616 1.1 dholland 617 1.1 dholland stat = nlm_lock_1(&args1, &res1, client, ext, timo); 618 1.1 dholland 619 1.1 dholland if (stat == RPC_SUCCESS) { 620 1.1 dholland nlm_convert_to_nlm4_res(res, &res1); 621 1.1 dholland } 622 1.1 dholland 623 1.1 dholland return (stat); 624 1.1 dholland } 625 1.1 dholland } 626 1.1 dholland 627 1.1 dholland static enum clnt_stat 628 1.1 dholland nlm_cancel_rpc(rpcvers_t vers, nlm4_cancargs *args, nlm4_res *res, CLIENT *client, 629 1.1 dholland struct rpc_callextra *ext, struct timeval timo) 630 1.1 dholland { 631 1.1 dholland if (vers == NLM_VERS4) { 632 1.1 dholland return nlm4_cancel_4(args, res, client, ext, timo); 633 1.1 dholland } else { 634 1.1 dholland nlm_cancargs args1; 635 1.1 dholland nlm_res res1; 636 1.1 dholland enum clnt_stat stat; 637 1.1 dholland 638 1.1 dholland args1.cookie = args->cookie; 639 1.1 dholland args1.block = args->block; 640 1.1 dholland args1.exclusive = args->exclusive; 641 1.1 dholland nlm_convert_to_nlm_lock(&args1.alock, &args->alock); 642 1.1 dholland memset(&res1, 0, sizeof(res1)); 643 1.1 dholland 644 1.1 dholland stat = nlm_cancel_1(&args1, &res1, client, ext, timo); 645 1.1 dholland 646 1.1 dholland if (stat == RPC_SUCCESS) { 647 1.1 dholland nlm_convert_to_nlm4_res(res, &res1); 648 1.1 dholland } 649 1.1 dholland 650 1.1 dholland return (stat); 651 1.1 dholland } 652 1.1 dholland } 653 1.1 dholland 654 1.1 dholland static enum clnt_stat 655 1.1 dholland nlm_unlock_rpc(rpcvers_t vers, nlm4_unlockargs *args, nlm4_res *res, CLIENT *client, 656 1.1 dholland struct rpc_callextra *ext, struct timeval timo) 657 1.1 dholland { 658 1.1 dholland if (vers == NLM_VERS4) { 659 1.1 dholland return nlm4_unlock_4(args, res, client, ext, timo); 660 1.1 dholland } else { 661 1.1 dholland nlm_unlockargs args1; 662 1.1 dholland nlm_res res1; 663 1.1 dholland enum clnt_stat stat; 664 1.1 dholland 665 1.1 dholland args1.cookie = args->cookie; 666 1.1 dholland nlm_convert_to_nlm_lock(&args1.alock, &args->alock); 667 1.1 dholland memset(&res1, 0, sizeof(res1)); 668 1.1 dholland 669 1.1 dholland stat = nlm_unlock_1(&args1, &res1, client, ext, timo); 670 1.1 dholland 671 1.1 dholland if (stat == RPC_SUCCESS) { 672 1.1 dholland nlm_convert_to_nlm4_res(res, &res1); 673 1.1 dholland } 674 1.1 dholland 675 1.1 dholland return (stat); 676 1.1 dholland } 677 1.1 dholland } 678 1.1 dholland 679 1.1 dholland /* 680 1.1 dholland * Called after a lock request (set or clear) succeeded. We record the 681 1.1 dholland * details in the local lock manager. Note that since the remote 682 1.1 dholland * server has granted the lock, we can be sure that it doesn't 683 1.1 dholland * conflict with any other locks we have in the local lock manager. 684 1.1 dholland * 685 1.1 dholland * Since it is possible that host may also make NLM client requests to 686 1.1 dholland * our NLM server, we use a different sysid value to record our own 687 1.1 dholland * client locks. 688 1.1 dholland * 689 1.1 dholland * Note that since it is possible for us to receive replies from the 690 1.1 dholland * server in a different order than the locks were granted (e.g. if 691 1.1 dholland * many local threads are contending for the same lock), we must use a 692 1.1 dholland * blocking operation when registering with the local lock manager. 693 1.1 dholland * We expect that any actual wait will be rare and short hence we 694 1.1 dholland * ignore signals for this. 695 1.1 dholland */ 696 1.1 dholland static void 697 1.1 dholland nlm_record_lock(struct vnode *vp, int op, struct flock *fl, 698 1.1 dholland int svid, int sysid, off_t size) 699 1.1 dholland { 700 1.1 dholland struct vop_advlockasync_args a; 701 1.1 dholland struct flock newfl; 702 1.2 pgoyette struct proc *p; 703 1.2 pgoyette int error, stops_deferred; 704 1.1 dholland 705 1.1 dholland a.a_vp = vp; 706 1.1 dholland a.a_id = NULL; 707 1.1 dholland a.a_op = op; 708 1.1 dholland a.a_fl = &newfl; 709 1.1 dholland a.a_flags = F_REMOTE|F_WAIT|F_NOINTR; 710 1.1 dholland a.a_task = NULL; 711 1.1 dholland a.a_cookiep = NULL; 712 1.1 dholland newfl.l_start = fl->l_start; 713 1.1 dholland newfl.l_len = fl->l_len; 714 1.1 dholland newfl.l_type = fl->l_type; 715 1.1 dholland newfl.l_whence = fl->l_whence; 716 1.1 dholland newfl.l_pid = svid; 717 1.1 dholland newfl.l_sysid = NLM_SYSID_CLIENT | sysid; 718 1.1 dholland 719 1.2 pgoyette for (;;) { 720 1.2 pgoyette error = lf_advlockasync(&a, &vp->v_lockf, size); 721 1.2 pgoyette if (error == EDEADLK) { 722 1.2 pgoyette /* 723 1.2 pgoyette * Locks are associated with the processes and 724 1.2 pgoyette * not with threads. Suppose we have two 725 1.2 pgoyette * threads A1 A2 in one process, A1 locked 726 1.2 pgoyette * file f1, A2 is locking file f2, and A1 is 727 1.2 pgoyette * unlocking f1. Then remote server may 728 1.2 pgoyette * already unlocked f1, while local still not 729 1.2 pgoyette * yet scheduled A1 to make the call to local 730 1.2 pgoyette * advlock manager. The process B owns lock on 731 1.2 pgoyette * f2 and issued the lock on f1. Remote would 732 1.2 pgoyette * grant B the request on f1, but local would 733 1.2 pgoyette * return EDEADLK. 734 1.2 pgoyette */ 735 1.2 pgoyette pause("nlmdlk", 1); 736 1.2 pgoyette p = curproc; 737 1.2 pgoyette stops_deferred = sigdeferstop(SIGDEFERSTOP_OFF); 738 1.2 pgoyette PROC_LOCK(p); 739 1.2 pgoyette thread_suspend_check(0); 740 1.2 pgoyette PROC_UNLOCK(p); 741 1.2 pgoyette sigallowstop(stops_deferred); 742 1.2 pgoyette } else if (error == EINTR) { 743 1.2 pgoyette /* 744 1.2 pgoyette * lf_purgelocks() might wake up the lock 745 1.2 pgoyette * waiter and removed our lock graph edges. 746 1.2 pgoyette * There is no sense in re-trying recording 747 1.2 pgoyette * the lock to the local manager after 748 1.2 pgoyette * reclaim. 749 1.2 pgoyette */ 750 1.2 pgoyette error = 0; 751 1.2 pgoyette break; 752 1.2 pgoyette } else 753 1.2 pgoyette break; 754 1.2 pgoyette } 755 1.1 dholland KASSERT(error == 0 || error == ENOENT, 756 1.1 dholland ("Failed to register NFS lock locally - error=%d", error)); 757 1.1 dholland } 758 1.1 dholland 759 1.1 dholland static int 760 1.1 dholland nlm_setlock(struct nlm_host *host, struct rpc_callextra *ext, 761 1.1 dholland rpcvers_t vers, struct timeval *timo, int retries, 762 1.1 dholland struct vnode *vp, int op, struct flock *fl, int flags, 763 1.1 dholland int svid, size_t fhlen, void *fh, off_t size, bool_t reclaim) 764 1.1 dholland { 765 1.1 dholland struct nlm4_lockargs args; 766 1.1 dholland char oh_space[32]; 767 1.1 dholland struct nlm4_res res; 768 1.1 dholland u_int xid; 769 1.1 dholland CLIENT *client; 770 1.1 dholland enum clnt_stat stat; 771 1.1 dholland int retry, block, exclusive; 772 1.1 dholland void *wait_handle = NULL; 773 1.1 dholland int error; 774 1.1 dholland 775 1.1 dholland memset(&args, 0, sizeof(args)); 776 1.1 dholland memset(&res, 0, sizeof(res)); 777 1.1 dholland 778 1.1 dholland block = (flags & F_WAIT) ? TRUE : FALSE; 779 1.1 dholland exclusive = (fl->l_type == F_WRLCK); 780 1.1 dholland 781 1.1 dholland error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size, 782 1.1 dholland &args.alock, oh_space); 783 1.1 dholland if (error) 784 1.1 dholland return (error); 785 1.1 dholland args.block = block; 786 1.1 dholland args.exclusive = exclusive; 787 1.1 dholland args.reclaim = reclaim; 788 1.1 dholland args.state = nlm_nsm_state; 789 1.1 dholland 790 1.1 dholland retry = 5*hz; 791 1.1 dholland for (;;) { 792 1.1 dholland client = nlm_host_get_rpc(host, FALSE); 793 1.1 dholland if (!client) 794 1.1 dholland return (ENOLCK); /* XXX retry? */ 795 1.1 dholland 796 1.1 dholland if (block) 797 1.1 dholland wait_handle = nlm_register_wait_lock(&args.alock, vp); 798 1.1 dholland 799 1.1 dholland xid = atomic_fetchadd_int(&nlm_xid, 1); 800 1.1 dholland args.cookie.n_len = sizeof(xid); 801 1.1 dholland args.cookie.n_bytes = (char*) &xid; 802 1.1 dholland 803 1.1 dholland stat = nlm_lock_rpc(vers, &args, &res, client, ext, *timo); 804 1.1 dholland 805 1.1 dholland CLNT_RELEASE(client); 806 1.1 dholland 807 1.1 dholland if (stat != RPC_SUCCESS) { 808 1.1 dholland if (block) 809 1.1 dholland nlm_deregister_wait_lock(wait_handle); 810 1.1 dholland if (retries) { 811 1.1 dholland retries--; 812 1.1 dholland continue; 813 1.1 dholland } 814 1.1 dholland return (EINVAL); 815 1.1 dholland } 816 1.1 dholland 817 1.1 dholland /* 818 1.1 dholland * Free res.cookie. 819 1.1 dholland */ 820 1.1 dholland xdr_free((xdrproc_t) xdr_nlm4_res, &res); 821 1.1 dholland 822 1.1 dholland if (block && res.stat.stat != nlm4_blocked) 823 1.1 dholland nlm_deregister_wait_lock(wait_handle); 824 1.1 dholland 825 1.1 dholland if (res.stat.stat == nlm4_denied_grace_period) { 826 1.1 dholland /* 827 1.1 dholland * The server has recently rebooted and is 828 1.1 dholland * giving old clients a change to reclaim 829 1.1 dholland * their locks. Wait for a few seconds and try 830 1.1 dholland * again. 831 1.1 dholland */ 832 1.1 dholland error = tsleep(&args, PCATCH, "nlmgrace", retry); 833 1.1 dholland if (error && error != EWOULDBLOCK) 834 1.1 dholland return (error); 835 1.1 dholland retry = 2*retry; 836 1.1 dholland if (retry > 30*hz) 837 1.1 dholland retry = 30*hz; 838 1.1 dholland continue; 839 1.1 dholland } 840 1.1 dholland 841 1.1 dholland if (block && res.stat.stat == nlm4_blocked) { 842 1.1 dholland /* 843 1.1 dholland * The server should call us back with a 844 1.1 dholland * granted message when the lock succeeds. In 845 1.1 dholland * order to deal with broken servers, lost 846 1.1 dholland * granted messages and server reboots, we 847 1.1 dholland * will also re-try every few seconds. 848 1.1 dholland */ 849 1.1 dholland error = nlm_wait_lock(wait_handle, retry); 850 1.1 dholland if (error == EWOULDBLOCK) { 851 1.1 dholland retry = 2*retry; 852 1.1 dholland if (retry > 30*hz) 853 1.1 dholland retry = 30*hz; 854 1.1 dholland continue; 855 1.1 dholland } 856 1.1 dholland if (error) { 857 1.1 dholland /* 858 1.1 dholland * We need to call the server to 859 1.1 dholland * cancel our lock request. 860 1.1 dholland */ 861 1.1 dholland nlm4_cancargs cancel; 862 1.1 dholland 863 1.1 dholland memset(&cancel, 0, sizeof(cancel)); 864 1.1 dholland 865 1.1 dholland xid = atomic_fetchadd_int(&nlm_xid, 1); 866 1.1 dholland cancel.cookie.n_len = sizeof(xid); 867 1.1 dholland cancel.cookie.n_bytes = (char*) &xid; 868 1.1 dholland cancel.block = block; 869 1.1 dholland cancel.exclusive = exclusive; 870 1.1 dholland cancel.alock = args.alock; 871 1.1 dholland 872 1.1 dholland do { 873 1.1 dholland client = nlm_host_get_rpc(host, FALSE); 874 1.1 dholland if (!client) 875 1.1 dholland /* XXX retry? */ 876 1.1 dholland return (ENOLCK); 877 1.1 dholland 878 1.1 dholland stat = nlm_cancel_rpc(vers, &cancel, 879 1.1 dholland &res, client, ext, *timo); 880 1.1 dholland 881 1.1 dholland CLNT_RELEASE(client); 882 1.1 dholland 883 1.1 dholland if (stat != RPC_SUCCESS) { 884 1.1 dholland /* 885 1.1 dholland * We need to cope 886 1.1 dholland * with temporary 887 1.1 dholland * network partitions 888 1.1 dholland * as well as server 889 1.1 dholland * reboots. This means 890 1.1 dholland * we have to keep 891 1.1 dholland * trying to cancel 892 1.1 dholland * until the server 893 1.1 dholland * wakes up again. 894 1.1 dholland */ 895 1.1 dholland pause("nlmcancel", 10*hz); 896 1.1 dholland } 897 1.1 dholland } while (stat != RPC_SUCCESS); 898 1.1 dholland 899 1.1 dholland /* 900 1.1 dholland * Free res.cookie. 901 1.1 dholland */ 902 1.1 dholland xdr_free((xdrproc_t) xdr_nlm4_res, &res); 903 1.1 dholland 904 1.1 dholland switch (res.stat.stat) { 905 1.1 dholland case nlm_denied: 906 1.1 dholland /* 907 1.1 dholland * There was nothing 908 1.1 dholland * to cancel. We are 909 1.1 dholland * going to go ahead 910 1.1 dholland * and assume we got 911 1.1 dholland * the lock. 912 1.1 dholland */ 913 1.1 dholland error = 0; 914 1.1 dholland break; 915 1.1 dholland 916 1.1 dholland case nlm4_denied_grace_period: 917 1.1 dholland /* 918 1.1 dholland * The server has 919 1.1 dholland * recently rebooted - 920 1.1 dholland * treat this as a 921 1.1 dholland * successful 922 1.1 dholland * cancellation. 923 1.1 dholland */ 924 1.1 dholland break; 925 1.1 dholland 926 1.1 dholland case nlm4_granted: 927 1.1 dholland /* 928 1.1 dholland * We managed to 929 1.1 dholland * cancel. 930 1.1 dholland */ 931 1.1 dholland break; 932 1.1 dholland 933 1.1 dholland default: 934 1.1 dholland /* 935 1.1 dholland * Broken server 936 1.1 dholland * implementation - 937 1.1 dholland * can't really do 938 1.1 dholland * anything here. 939 1.1 dholland */ 940 1.1 dholland break; 941 1.1 dholland } 942 1.1 dholland 943 1.1 dholland } 944 1.1 dholland } else { 945 1.1 dholland error = nlm_map_status(res.stat.stat); 946 1.1 dholland } 947 1.1 dholland 948 1.1 dholland if (!error && !reclaim) { 949 1.1 dholland nlm_record_lock(vp, op, fl, args.alock.svid, 950 1.1 dholland nlm_host_get_sysid(host), size); 951 1.1 dholland nlm_host_monitor(host, 0); 952 1.1 dholland } 953 1.1 dholland 954 1.1 dholland return (error); 955 1.1 dholland } 956 1.1 dholland } 957 1.1 dholland 958 1.1 dholland static int 959 1.1 dholland nlm_clearlock(struct nlm_host *host, struct rpc_callextra *ext, 960 1.1 dholland rpcvers_t vers, struct timeval *timo, int retries, 961 1.1 dholland struct vnode *vp, int op, struct flock *fl, int flags, 962 1.1 dholland int svid, size_t fhlen, void *fh, off_t size) 963 1.1 dholland { 964 1.1 dholland struct nlm4_unlockargs args; 965 1.1 dholland char oh_space[32]; 966 1.1 dholland struct nlm4_res res; 967 1.1 dholland u_int xid; 968 1.1 dholland CLIENT *client; 969 1.1 dholland enum clnt_stat stat; 970 1.1 dholland int error; 971 1.1 dholland 972 1.1 dholland memset(&args, 0, sizeof(args)); 973 1.1 dholland memset(&res, 0, sizeof(res)); 974 1.1 dholland 975 1.1 dholland error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size, 976 1.1 dholland &args.alock, oh_space); 977 1.1 dholland if (error) 978 1.1 dholland return (error); 979 1.1 dholland 980 1.1 dholland for (;;) { 981 1.1 dholland client = nlm_host_get_rpc(host, FALSE); 982 1.1 dholland if (!client) 983 1.1 dholland return (ENOLCK); /* XXX retry? */ 984 1.1 dholland 985 1.1 dholland xid = atomic_fetchadd_int(&nlm_xid, 1); 986 1.1 dholland args.cookie.n_len = sizeof(xid); 987 1.1 dholland args.cookie.n_bytes = (char*) &xid; 988 1.1 dholland 989 1.1 dholland stat = nlm_unlock_rpc(vers, &args, &res, client, ext, *timo); 990 1.1 dholland 991 1.1 dholland CLNT_RELEASE(client); 992 1.1 dholland 993 1.1 dholland if (stat != RPC_SUCCESS) { 994 1.1 dholland if (retries) { 995 1.1 dholland retries--; 996 1.1 dholland continue; 997 1.1 dholland } 998 1.1 dholland return (EINVAL); 999 1.1 dholland } 1000 1.1 dholland 1001 1.1 dholland /* 1002 1.1 dholland * Free res.cookie. 1003 1.1 dholland */ 1004 1.1 dholland xdr_free((xdrproc_t) xdr_nlm4_res, &res); 1005 1.1 dholland 1006 1.1 dholland if (res.stat.stat == nlm4_denied_grace_period) { 1007 1.1 dholland /* 1008 1.1 dholland * The server has recently rebooted and is 1009 1.1 dholland * giving old clients a change to reclaim 1010 1.1 dholland * their locks. Wait for a few seconds and try 1011 1.1 dholland * again. 1012 1.1 dholland */ 1013 1.1 dholland error = tsleep(&args, PCATCH, "nlmgrace", 5*hz); 1014 1.1 dholland if (error && error != EWOULDBLOCK) 1015 1.1 dholland return (error); 1016 1.1 dholland continue; 1017 1.1 dholland } 1018 1.1 dholland 1019 1.1 dholland /* 1020 1.1 dholland * If we are being called via nlm_reclaim (which will 1021 1.1 dholland * use the F_REMOTE flag), don't record the lock 1022 1.1 dholland * operation in the local lock manager since the vnode 1023 1.1 dholland * is going away. 1024 1.1 dholland */ 1025 1.1 dholland if (!(flags & F_REMOTE)) 1026 1.1 dholland nlm_record_lock(vp, op, fl, args.alock.svid, 1027 1.1 dholland nlm_host_get_sysid(host), size); 1028 1.1 dholland 1029 1.1 dholland return (0); 1030 1.1 dholland } 1031 1.1 dholland } 1032 1.1 dholland 1033 1.1 dholland static int 1034 1.1 dholland nlm_getlock(struct nlm_host *host, struct rpc_callextra *ext, 1035 1.1 dholland rpcvers_t vers, struct timeval *timo, int retries, 1036 1.1 dholland struct vnode *vp, int op, struct flock *fl, int flags, 1037 1.1 dholland int svid, size_t fhlen, void *fh, off_t size) 1038 1.1 dholland { 1039 1.1 dholland struct nlm4_testargs args; 1040 1.1 dholland char oh_space[32]; 1041 1.1 dholland struct nlm4_testres res; 1042 1.1 dholland u_int xid; 1043 1.1 dholland CLIENT *client; 1044 1.1 dholland enum clnt_stat stat; 1045 1.1 dholland int exclusive; 1046 1.1 dholland int error; 1047 1.1 dholland 1048 1.1 dholland KASSERT(!(flags & F_FLOCK), ("unexpected F_FLOCK for F_GETLK")); 1049 1.1 dholland 1050 1.1 dholland memset(&args, 0, sizeof(args)); 1051 1.1 dholland memset(&res, 0, sizeof(res)); 1052 1.1 dholland 1053 1.1 dholland exclusive = (fl->l_type == F_WRLCK); 1054 1.1 dholland 1055 1.1 dholland error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size, 1056 1.1 dholland &args.alock, oh_space); 1057 1.1 dholland if (error) 1058 1.1 dholland return (error); 1059 1.1 dholland args.exclusive = exclusive; 1060 1.1 dholland 1061 1.1 dholland for (;;) { 1062 1.1 dholland client = nlm_host_get_rpc(host, FALSE); 1063 1.1 dholland if (!client) 1064 1.1 dholland return (ENOLCK); /* XXX retry? */ 1065 1.1 dholland 1066 1.1 dholland xid = atomic_fetchadd_int(&nlm_xid, 1); 1067 1.1 dholland args.cookie.n_len = sizeof(xid); 1068 1.1 dholland args.cookie.n_bytes = (char*) &xid; 1069 1.1 dholland 1070 1.1 dholland stat = nlm_test_rpc(vers, &args, &res, client, ext, *timo); 1071 1.1 dholland 1072 1.1 dholland CLNT_RELEASE(client); 1073 1.1 dholland 1074 1.1 dholland if (stat != RPC_SUCCESS) { 1075 1.1 dholland if (retries) { 1076 1.1 dholland retries--; 1077 1.1 dholland continue; 1078 1.1 dholland } 1079 1.1 dholland return (EINVAL); 1080 1.1 dholland } 1081 1.1 dholland 1082 1.1 dholland if (res.stat.stat == nlm4_denied_grace_period) { 1083 1.1 dholland /* 1084 1.1 dholland * The server has recently rebooted and is 1085 1.1 dholland * giving old clients a change to reclaim 1086 1.1 dholland * their locks. Wait for a few seconds and try 1087 1.1 dholland * again. 1088 1.1 dholland */ 1089 1.1 dholland xdr_free((xdrproc_t) xdr_nlm4_testres, &res); 1090 1.1 dholland error = tsleep(&args, PCATCH, "nlmgrace", 5*hz); 1091 1.1 dholland if (error && error != EWOULDBLOCK) 1092 1.1 dholland return (error); 1093 1.1 dholland continue; 1094 1.1 dholland } 1095 1.1 dholland 1096 1.1 dholland if (res.stat.stat == nlm4_denied) { 1097 1.1 dholland struct nlm4_holder *h = 1098 1.1 dholland &res.stat.nlm4_testrply_u.holder; 1099 1.1 dholland fl->l_start = h->l_offset; 1100 1.1 dholland fl->l_len = h->l_len; 1101 1.1 dholland fl->l_pid = h->svid; 1102 1.1 dholland if (h->exclusive) 1103 1.1 dholland fl->l_type = F_WRLCK; 1104 1.1 dholland else 1105 1.1 dholland fl->l_type = F_RDLCK; 1106 1.1 dholland fl->l_whence = SEEK_SET; 1107 1.1 dholland fl->l_sysid = 0; 1108 1.1 dholland } else { 1109 1.1 dholland fl->l_type = F_UNLCK; 1110 1.1 dholland } 1111 1.1 dholland 1112 1.1 dholland xdr_free((xdrproc_t) xdr_nlm4_testres, &res); 1113 1.1 dholland 1114 1.1 dholland return (0); 1115 1.1 dholland } 1116 1.1 dholland } 1117 1.1 dholland 1118 1.1 dholland static int 1119 1.1 dholland nlm_map_status(nlm4_stats stat) 1120 1.1 dholland { 1121 1.1 dholland switch (stat) { 1122 1.1 dholland case nlm4_granted: 1123 1.1 dholland return (0); 1124 1.1 dholland 1125 1.1 dholland case nlm4_denied: 1126 1.1 dholland return (EAGAIN); 1127 1.1 dholland 1128 1.1 dholland case nlm4_denied_nolocks: 1129 1.1 dholland return (ENOLCK); 1130 1.1 dholland 1131 1.1 dholland case nlm4_deadlck: 1132 1.1 dholland return (EDEADLK); 1133 1.1 dholland 1134 1.1 dholland case nlm4_rofs: 1135 1.1 dholland return (EROFS); 1136 1.1 dholland 1137 1.1 dholland case nlm4_stale_fh: 1138 1.1 dholland return (ESTALE); 1139 1.1 dholland 1140 1.1 dholland case nlm4_fbig: 1141 1.1 dholland return (EFBIG); 1142 1.1 dholland 1143 1.1 dholland case nlm4_failed: 1144 1.1 dholland return (EACCES); 1145 1.1 dholland 1146 1.1 dholland default: 1147 1.1 dholland return (EINVAL); 1148 1.1 dholland } 1149 1.1 dholland } 1150 1.1 dholland 1151 1.1 dholland static struct nlm_file_svid * 1152 1.1 dholland nlm_find_svid(void *id) 1153 1.1 dholland { 1154 1.1 dholland struct nlm_file_svid *ns, *newns; 1155 1.1 dholland int h; 1156 1.1 dholland 1157 1.1 dholland h = (((uintptr_t) id) >> 7) % NLM_SVID_HASH_SIZE; 1158 1.1 dholland 1159 1.1 dholland mtx_lock(&nlm_svid_lock); 1160 1.1 dholland LIST_FOREACH(ns, &nlm_file_svids[h], ns_link) { 1161 1.1 dholland if (ns->ns_id == id) { 1162 1.1 dholland ns->ns_refs++; 1163 1.1 dholland break; 1164 1.1 dholland } 1165 1.1 dholland } 1166 1.1 dholland mtx_unlock(&nlm_svid_lock); 1167 1.1 dholland if (!ns) { 1168 1.1 dholland int svid = alloc_unr(nlm_svid_allocator); 1169 1.1 dholland newns = malloc(sizeof(struct nlm_file_svid), M_NLM, 1170 1.1 dholland M_WAITOK); 1171 1.1 dholland newns->ns_refs = 1; 1172 1.1 dholland newns->ns_id = id; 1173 1.1 dholland newns->ns_svid = svid; 1174 1.1 dholland newns->ns_ucred = NULL; 1175 1.1 dholland newns->ns_active = FALSE; 1176 1.1 dholland 1177 1.1 dholland /* 1178 1.1 dholland * We need to check for a race with some other 1179 1.1 dholland * thread allocating a svid for this file. 1180 1.1 dholland */ 1181 1.1 dholland mtx_lock(&nlm_svid_lock); 1182 1.1 dholland LIST_FOREACH(ns, &nlm_file_svids[h], ns_link) { 1183 1.1 dholland if (ns->ns_id == id) { 1184 1.1 dholland ns->ns_refs++; 1185 1.1 dholland break; 1186 1.1 dholland } 1187 1.1 dholland } 1188 1.1 dholland if (ns) { 1189 1.1 dholland mtx_unlock(&nlm_svid_lock); 1190 1.1 dholland free_unr(nlm_svid_allocator, newns->ns_svid); 1191 1.1 dholland free(newns, M_NLM); 1192 1.1 dholland } else { 1193 1.1 dholland LIST_INSERT_HEAD(&nlm_file_svids[h], newns, 1194 1.1 dholland ns_link); 1195 1.1 dholland ns = newns; 1196 1.1 dholland mtx_unlock(&nlm_svid_lock); 1197 1.1 dholland } 1198 1.1 dholland } 1199 1.1 dholland 1200 1.1 dholland return (ns); 1201 1.1 dholland } 1202 1.1 dholland 1203 1.1 dholland static void 1204 1.1 dholland nlm_free_svid(struct nlm_file_svid *ns) 1205 1.1 dholland { 1206 1.1 dholland 1207 1.1 dholland mtx_lock(&nlm_svid_lock); 1208 1.1 dholland ns->ns_refs--; 1209 1.1 dholland if (!ns->ns_refs) { 1210 1.1 dholland KASSERT(!ns->ns_active, ("Freeing active SVID")); 1211 1.1 dholland LIST_REMOVE(ns, ns_link); 1212 1.1 dholland mtx_unlock(&nlm_svid_lock); 1213 1.1 dholland free_unr(nlm_svid_allocator, ns->ns_svid); 1214 1.1 dholland if (ns->ns_ucred) 1215 1.1 dholland crfree(ns->ns_ucred); 1216 1.1 dholland free(ns, M_NLM); 1217 1.1 dholland } else { 1218 1.1 dholland mtx_unlock(&nlm_svid_lock); 1219 1.1 dholland } 1220 1.1 dholland } 1221 1.1 dholland 1222 1.1 dholland static int 1223 1.1 dholland nlm_init_lock(struct flock *fl, int flags, int svid, 1224 1.1 dholland rpcvers_t vers, size_t fhlen, void *fh, off_t size, 1225 1.1 dholland struct nlm4_lock *lock, char oh_space[32]) 1226 1.1 dholland { 1227 1.1 dholland size_t oh_len; 1228 1.1 dholland off_t start, len; 1229 1.1 dholland 1230 1.1 dholland if (fl->l_whence == SEEK_END) { 1231 1.1 dholland if (size > OFF_MAX 1232 1.1 dholland || (fl->l_start > 0 && size > OFF_MAX - fl->l_start)) 1233 1.1 dholland return (EOVERFLOW); 1234 1.1 dholland start = size + fl->l_start; 1235 1.1 dholland } else if (fl->l_whence == SEEK_SET || fl->l_whence == SEEK_CUR) { 1236 1.1 dholland start = fl->l_start; 1237 1.1 dholland } else { 1238 1.1 dholland return (EINVAL); 1239 1.1 dholland } 1240 1.1 dholland if (start < 0) 1241 1.1 dholland return (EINVAL); 1242 1.1 dholland if (fl->l_len < 0) { 1243 1.1 dholland len = -fl->l_len; 1244 1.1 dholland start -= len; 1245 1.1 dholland if (start < 0) 1246 1.1 dholland return (EINVAL); 1247 1.1 dholland } else { 1248 1.1 dholland len = fl->l_len; 1249 1.1 dholland } 1250 1.1 dholland 1251 1.1 dholland if (vers == NLM_VERS) { 1252 1.1 dholland /* 1253 1.1 dholland * Enforce range limits on V1 locks 1254 1.1 dholland */ 1255 1.1 dholland if (start > 0xffffffffLL || len > 0xffffffffLL) 1256 1.1 dholland return (EOVERFLOW); 1257 1.1 dholland } 1258 1.1 dholland 1259 1.1 dholland snprintf(oh_space, 32, "%d@", svid); 1260 1.1 dholland oh_len = strlen(oh_space); 1261 1.1 dholland getcredhostname(NULL, oh_space + oh_len, 32 - oh_len); 1262 1.1 dholland oh_len = strlen(oh_space); 1263 1.1 dholland 1264 1.1 dholland memset(lock, 0, sizeof(*lock)); 1265 1.1 dholland lock->caller_name = prison0.pr_hostname; 1266 1.1 dholland lock->fh.n_len = fhlen; 1267 1.1 dholland lock->fh.n_bytes = fh; 1268 1.1 dholland lock->oh.n_len = oh_len; 1269 1.1 dholland lock->oh.n_bytes = oh_space; 1270 1.1 dholland lock->svid = svid; 1271 1.1 dholland lock->l_offset = start; 1272 1.1 dholland lock->l_len = len; 1273 1.1 dholland 1274 1.1 dholland return (0); 1275 1.1 dholland } 1276