HardenedBSD src tree
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nfs_lock.c 10 KiB

Add the new kernel-mode NFS Lock Manager. To use it instead of the user-mode lock manager, build a kernel with the NFSLOCKD option and add '-k' to 'rpc_lockd_flags' in rc.conf. Highlights include: * Thread-safe kernel RPC client - many threads can use the same RPC client handle safely with replies being de-multiplexed at the socket upcall (typically driven directly by the NIC interrupt) and handed off to whichever thread matches the reply. For UDP sockets, many RPC clients can share the same socket. This allows the use of a single privileged UDP port number to talk to an arbitrary number of remote hosts. * Single-threaded kernel RPC server. Adding support for multi-threaded server would be relatively straightforward and would follow approximately the Solaris KPI. A single thread should be sufficient for the NLM since it should rarely block in normal operation. * Kernel mode NLM server supporting cancel requests and granted callbacks. I've tested the NLM server reasonably extensively - it passes both my own tests and the NFS Connectathon locking tests running on Solaris, Mac OS X and Ubuntu Linux. * Userland NLM client supported. While the NLM server doesn't have support for the local NFS client's locking needs, it does have to field async replies and granted callbacks from remote NLMs that the local client has contacted. We relay these replies to the userland rpc.lockd over a local domain RPC socket. * Robust deadlock detection for the local lock manager. In particular it will detect deadlocks caused by a lock request that covers more than one blocking request. As required by the NLM protocol, all deadlock detection happens synchronously - a user is guaranteed that if a lock request isn't rejected immediately, the lock will eventually be granted. The old system allowed for a 'deferred deadlock' condition where a blocked lock request could wake up and find that some other deadlock-causing lock owner had beaten them to the lock. * Since both local and remote locks are managed by the same kernel locking code, local and remote processes can safely use file locks for mutual exclusion. Local processes have no fairness advantage compared to remote processes when contending to lock a region that has just been unlocked - the local lock manager enforces a strict first-come first-served model for both local and remote lockers. Sponsored by: Isilon Systems PR: 95247 107555 115524 116679 MFC after: 2 weeks
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Berkeley Software Design Inc's name may not be used to endorse or
  15. * promote products derived from this software without specific prior
  16. * written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * from BSDI nfs_lock.c,v 2.4 1998/12/14 23:49:56 jch Exp
  31. */
  32. #include <sys/cdefs.h>
  33. __FBSDID("$FreeBSD$");
  34. #include <sys/param.h>
  35. #include <sys/systm.h>
  36. #include <sys/conf.h>
  37. #include <sys/fcntl.h>
  38. #include <sys/kernel.h> /* for hz */
  39. #include <sys/limits.h>
  40. #include <sys/lock.h>
  41. #include <sys/malloc.h>
  42. #include <sys/lockf.h> /* for hz */ /* Must come after sys/malloc.h */
  43. #include <sys/mbuf.h>
  44. #include <sys/mount.h>
  45. #include <sys/namei.h>
  46. #include <sys/priv.h>
  47. #include <sys/proc.h>
  48. #include <sys/resourcevar.h>
  49. #include <sys/socket.h>
  50. #include <sys/socket.h>
  51. #include <sys/unistd.h>
  52. #include <sys/vnode.h>
  53. #include <net/if.h>
  54. #include <nfs/nfsproto.h>
  55. #include <nfs/nfs_lock.h>
  56. #include <nfsclient/nfs.h>
  57. #include <nfsclient/nfsmount.h>
  58. #include <nfsclient/nfsnode.h>
  59. #include <nfsclient/nlminfo.h>
  60. extern void (*nlminfo_release_p)(struct proc *p);
  61. vop_advlock_t *nfs_advlock_p = nfs_dolock;
  62. vop_reclaim_t *nfs_reclaim_p = NULL;
  63. static MALLOC_DEFINE(M_NFSLOCK, "nfsclient_lock", "NFS lock request");
  64. static MALLOC_DEFINE(M_NLMINFO, "nfsclient_nlminfo",
  65. "NFS lock process structure");
  66. static int nfslockdans(struct thread *td, struct lockd_ans *ansp);
  67. static void nlminfo_release(struct proc *p);
  68. /*
  69. * --------------------------------------------------------------------
  70. * A miniature device driver which the userland uses to talk to us.
  71. *
  72. */
  73. static struct cdev *nfslock_dev;
  74. static struct mtx nfslock_mtx;
  75. static int nfslock_isopen;
  76. static TAILQ_HEAD(,__lock_msg) nfslock_list;
  77. static int
  78. nfslock_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
  79. {
  80. int error;
  81. gone_in(13, "uses Giant; replace with NFSLOCKD/nfslockd.ko");
  82. error = priv_check(td, PRIV_NFS_LOCKD);
  83. if (error)
  84. return (error);
  85. mtx_lock(&nfslock_mtx);
  86. if (!nfslock_isopen) {
  87. error = 0;
  88. nfslock_isopen = 1;
  89. } else {
  90. error = EOPNOTSUPP;
  91. }
  92. mtx_unlock(&nfslock_mtx);
  93. return (error);
  94. }
  95. static int
  96. nfslock_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
  97. {
  98. struct __lock_msg *lm;
  99. mtx_lock(&nfslock_mtx);
  100. nfslock_isopen = 0;
  101. while (!TAILQ_EMPTY(&nfslock_list)) {
  102. lm = TAILQ_FIRST(&nfslock_list);
  103. /* XXX: answer request */
  104. TAILQ_REMOVE(&nfslock_list, lm, lm_link);
  105. free(lm, M_NFSLOCK);
  106. }
  107. mtx_unlock(&nfslock_mtx);
  108. return (0);
  109. }
  110. static int
  111. nfslock_read(struct cdev *dev, struct uio *uio, int ioflag)
  112. {
  113. int error;
  114. struct __lock_msg *lm;
  115. if (uio->uio_resid != sizeof *lm)
  116. return (EOPNOTSUPP);
  117. lm = NULL;
  118. error = 0;
  119. mtx_lock(&nfslock_mtx);
  120. while (TAILQ_EMPTY(&nfslock_list)) {
  121. error = msleep(&nfslock_list, &nfslock_mtx, PSOCK | PCATCH,
  122. "nfslockd", 0);
  123. if (error)
  124. break;
  125. }
  126. if (!error) {
  127. lm = TAILQ_FIRST(&nfslock_list);
  128. TAILQ_REMOVE(&nfslock_list, lm, lm_link);
  129. }
  130. mtx_unlock(&nfslock_mtx);
  131. if (!error) {
  132. error = uiomove(lm, sizeof *lm, uio);
  133. free(lm, M_NFSLOCK);
  134. }
  135. return (error);
  136. }
  137. static int
  138. nfslock_write(struct cdev *dev, struct uio *uio, int ioflag)
  139. {
  140. struct lockd_ans la;
  141. int error;
  142. if (uio->uio_resid != sizeof la)
  143. return (EOPNOTSUPP);
  144. error = uiomove(&la, sizeof la, uio);
  145. if (!error)
  146. error = nfslockdans(curthread, &la);
  147. return (error);
  148. }
  149. static int
  150. nfslock_send(struct __lock_msg *lm)
  151. {
  152. struct __lock_msg *lm2;
  153. int error;
  154. error = 0;
  155. lm2 = malloc(sizeof *lm2, M_NFSLOCK, M_WAITOK);
  156. mtx_lock(&nfslock_mtx);
  157. if (nfslock_isopen) {
  158. memcpy(lm2, lm, sizeof *lm2);
  159. TAILQ_INSERT_TAIL(&nfslock_list, lm2, lm_link);
  160. wakeup(&nfslock_list);
  161. } else {
  162. error = EOPNOTSUPP;
  163. }
  164. mtx_unlock(&nfslock_mtx);
  165. if (error)
  166. free(lm2, M_NFSLOCK);
  167. return (error);
  168. }
  169. static struct cdevsw nfslock_cdevsw = {
  170. .d_version = D_VERSION,
  171. .d_open = nfslock_open,
  172. .d_close = nfslock_close,
  173. .d_read = nfslock_read,
  174. .d_write = nfslock_write,
  175. .d_name = "nfslock"
  176. };
  177. static int
  178. nfslock_modevent(module_t mod __unused, int type, void *data __unused)
  179. {
  180. switch (type) {
  181. case MOD_LOAD:
  182. if (bootverbose)
  183. printf("nfslock: pseudo-device\n");
  184. mtx_init(&nfslock_mtx, "nfslock", NULL, MTX_DEF);
  185. TAILQ_INIT(&nfslock_list);
  186. nlminfo_release_p = nlminfo_release;
  187. nfslock_dev = make_dev(&nfslock_cdevsw, 0,
  188. UID_ROOT, GID_KMEM, 0600, _PATH_NFSLCKDEV);
  189. return (0);
  190. default:
  191. return (EOPNOTSUPP);
  192. }
  193. }
  194. DEV_MODULE(nfslock, nfslock_modevent, NULL);
  195. MODULE_VERSION(nfslock, 1);
  196. /*
  197. * XXX
  198. * We have to let the process know if the call succeeded. I'm using an extra
  199. * field in the p_nlminfo field in the proc structure, as it is already for
  200. * lockd stuff.
  201. */
  202. /*
  203. * nfs_advlock --
  204. * NFS advisory byte-level locks.
  205. *
  206. * The vnode shall be (shared) locked on the entry, it is
  207. * unconditionally unlocked after.
  208. */
  209. int
  210. nfs_dolock(struct vop_advlock_args *ap)
  211. {
  212. LOCKD_MSG msg;
  213. struct thread *td;
  214. struct vnode *vp;
  215. int error;
  216. struct flock *fl;
  217. struct proc *p;
  218. struct nfsmount *nmp;
  219. struct timeval boottime;
  220. td = curthread;
  221. p = td->td_proc;
  222. vp = ap->a_vp;
  223. fl = ap->a_fl;
  224. nmp = VFSTONFS(vp->v_mount);
  225. ASSERT_VOP_LOCKED(vp, "nfs_dolock");
  226. nmp->nm_getinfo(vp, msg.lm_fh, &msg.lm_fh_len, &msg.lm_addr,
  227. &msg.lm_nfsv3, NULL, NULL);
  228. VOP_UNLOCK(vp, 0);
  229. /*
  230. * the NLM protocol doesn't allow the server to return an error
  231. * on ranges, so we do it.
  232. */
  233. if (fl->l_whence != SEEK_END) {
  234. if ((fl->l_whence != SEEK_CUR && fl->l_whence != SEEK_SET) ||
  235. fl->l_start < 0 ||
  236. (fl->l_len < 0 &&
  237. (fl->l_start == 0 || fl->l_start + fl->l_len < 0)))
  238. return (EINVAL);
  239. if (fl->l_len > 0 &&
  240. (fl->l_len - 1 > OFF_MAX - fl->l_start))
  241. return (EOVERFLOW);
  242. }
  243. /*
  244. * Fill in the information structure.
  245. */
  246. msg.lm_version = LOCKD_MSG_VERSION;
  247. msg.lm_msg_ident.pid = p->p_pid;
  248. mtx_lock(&Giant);
  249. /*
  250. * if there is no nfsowner table yet, allocate one.
  251. */
  252. if (p->p_nlminfo == NULL) {
  253. p->p_nlminfo = malloc(sizeof(struct nlminfo),
  254. M_NLMINFO, M_WAITOK | M_ZERO);
  255. p->p_nlminfo->pid_start = p->p_stats->p_start;
  256. getboottime(&boottime);
  257. timevaladd(&p->p_nlminfo->pid_start, &boottime);
  258. }
  259. msg.lm_msg_ident.pid_start = p->p_nlminfo->pid_start;
  260. msg.lm_msg_ident.msg_seq = ++(p->p_nlminfo->msg_seq);
  261. msg.lm_fl = *fl;
  262. msg.lm_wait = ap->a_flags & F_WAIT;
  263. msg.lm_getlk = ap->a_op == F_GETLK;
  264. cru2x(td->td_ucred, &msg.lm_cred);
  265. for (;;) {
  266. error = nfslock_send(&msg);
  267. if (error)
  268. goto out;
  269. /* Unlocks succeed immediately. */
  270. if (fl->l_type == F_UNLCK)
  271. goto out;
  272. /*
  273. * Retry after 20 seconds if we haven't gotten a response yet.
  274. * This number was picked out of thin air... but is longer
  275. * then even a reasonably loaded system should take (at least
  276. * on a local network). XXX Probably should use a back-off
  277. * scheme.
  278. *
  279. * XXX: No PCATCH here since we currently have no useful
  280. * way to signal to the userland rpc.lockd that the request
  281. * has been aborted. Once the rpc.lockd implementation
  282. * can handle aborts, and we report them properly,
  283. * PCATCH can be put back. In the mean time, if we did
  284. * permit aborting, the lock attempt would "get lost"
  285. * and the lock would get stuck in the locked state.
  286. */
  287. error = tsleep(p->p_nlminfo, PUSER, "lockd", 20*hz);
  288. if (error != 0) {
  289. if (error == EWOULDBLOCK) {
  290. /*
  291. * We timed out, so we rewrite the request
  292. * to the fifo.
  293. */
  294. continue;
  295. }
  296. break;
  297. }
  298. if (msg.lm_getlk && p->p_nlminfo->retcode == 0) {
  299. if (p->p_nlminfo->set_getlk_pid) {
  300. fl->l_sysid = 0; /* XXX */
  301. fl->l_pid = p->p_nlminfo->getlk_pid;
  302. } else {
  303. fl->l_type = F_UNLCK;
  304. }
  305. }
  306. error = p->p_nlminfo->retcode;
  307. break;
  308. }
  309. out:
  310. mtx_unlock(&Giant);
  311. return (error);
  312. }
  313. /*
  314. * nfslockdans --
  315. * NFS advisory byte-level locks answer from the lock daemon.
  316. */
  317. static int
  318. nfslockdans(struct thread *td, struct lockd_ans *ansp)
  319. {
  320. struct proc *targetp;
  321. /* the version should match, or we're out of sync */
  322. if (ansp->la_vers != LOCKD_ANS_VERSION)
  323. return (EINVAL);
  324. /* Find the process, set its return errno and wake it up. */
  325. if ((targetp = pfind(ansp->la_msg_ident.pid)) == NULL)
  326. return (ESRCH);
  327. /* verify the pid hasn't been reused (if we can), and it isn't waiting
  328. * for an answer from a more recent request. We return an EPIPE if
  329. * the match fails, because we've already used ESRCH above, and this
  330. * is sort of like writing on a pipe after the reader has closed it.
  331. */
  332. if (targetp->p_nlminfo == NULL ||
  333. ((ansp->la_msg_ident.msg_seq != -1) &&
  334. (timevalcmp(&targetp->p_nlminfo->pid_start,
  335. &ansp->la_msg_ident.pid_start, !=) ||
  336. targetp->p_nlminfo->msg_seq != ansp->la_msg_ident.msg_seq))) {
  337. PROC_UNLOCK(targetp);
  338. return (EPIPE);
  339. }
  340. targetp->p_nlminfo->retcode = ansp->la_errno;
  341. targetp->p_nlminfo->set_getlk_pid = ansp->la_set_getlk_pid;
  342. targetp->p_nlminfo->getlk_pid = ansp->la_getlk_pid;
  343. wakeup(targetp->p_nlminfo);
  344. PROC_UNLOCK(targetp);
  345. return (0);
  346. }
  347. /*
  348. * Free nlminfo attached to process.
  349. */
  350. void
  351. nlminfo_release(struct proc *p)
  352. {
  353. free(p->p_nlminfo, M_NLMINFO);
  354. p->p_nlminfo = NULL;
  355. }