nfs_node.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* $OpenBSD: nfs_node.c,v 1.62 2015/03/14 03:38:52 jsg Exp $ */
  2. /* $NetBSD: nfs_node.c,v 1.16 1996/02/18 11:53:42 fvdl Exp $ */
  3. /*
  4. * Copyright (c) 1989, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * This code is derived from software contributed to Berkeley by
  8. * Rick Macklem at The University of Guelph.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * @(#)nfs_node.c 8.6 (Berkeley) 5/22/95
  35. */
  36. #include <sys/param.h>
  37. #include <sys/systm.h>
  38. #include <sys/timeout.h>
  39. #include <sys/mount.h>
  40. #include <sys/namei.h>
  41. #include <sys/vnode.h>
  42. #include <sys/lock.h>
  43. #include <sys/kernel.h>
  44. #include <sys/malloc.h>
  45. #include <sys/pool.h>
  46. #include <sys/rwlock.h>
  47. #include <sys/queue.h>
  48. #include <nfs/rpcv2.h>
  49. #include <nfs/nfsproto.h>
  50. #include <nfs/nfsnode.h>
  51. #include <nfs/nfsmount.h>
  52. #include <nfs/nfs_var.h>
  53. struct pool nfs_node_pool;
  54. extern int prtactive;
  55. struct rwlock nfs_hashlock = RWLOCK_INITIALIZER("nfshshlk");
  56. /* XXX */
  57. extern struct vops nfs_vops;
  58. /* filehandle to node lookup. */
  59. static __inline int
  60. nfsnode_cmp(const struct nfsnode *a, const struct nfsnode *b)
  61. {
  62. if (a->n_fhsize != b->n_fhsize)
  63. return (a->n_fhsize - b->n_fhsize);
  64. return (memcmp(a->n_fhp, b->n_fhp, a->n_fhsize));
  65. }
  66. RB_PROTOTYPE(nfs_nodetree, nfsnode, n_entry, nfsnode_cmp);
  67. RB_GENERATE(nfs_nodetree, nfsnode, n_entry, nfsnode_cmp);
  68. /*
  69. * Look up a vnode/nfsnode by file handle.
  70. * Callers must check for mount points!!
  71. * In all cases, a pointer to a
  72. * nfsnode structure is returned.
  73. */
  74. int
  75. nfs_nget(struct mount *mnt, nfsfh_t *fh, int fhsize, struct nfsnode **npp)
  76. {
  77. struct nfsmount *nmp;
  78. struct nfsnode *np, find, *np2;
  79. struct vnode *vp, *nvp;
  80. struct proc *p = curproc; /* XXX */
  81. int error;
  82. nmp = VFSTONFS(mnt);
  83. loop:
  84. rw_enter_write(&nfs_hashlock);
  85. find.n_fhp = fh;
  86. find.n_fhsize = fhsize;
  87. np = RB_FIND(nfs_nodetree, &nmp->nm_ntree, &find);
  88. if (np != NULL) {
  89. rw_exit_write(&nfs_hashlock);
  90. vp = NFSTOV(np);
  91. error = vget(vp, LK_EXCLUSIVE, p);
  92. if (error)
  93. goto loop;
  94. *npp = np;
  95. return (0);
  96. }
  97. /*
  98. * getnewvnode() could recycle a vnode, potentially formerly
  99. * owned by NFS. This will cause a VOP_RECLAIM() to happen,
  100. * which will cause recursive locking, so we unlock before
  101. * calling getnewvnode() lock again afterwards, but must check
  102. * to see if this nfsnode has been added while we did not hold
  103. * the lock.
  104. */
  105. rw_exit_write(&nfs_hashlock);
  106. error = getnewvnode(VT_NFS, mnt, &nfs_vops, &nvp);
  107. /* note that we don't have this vnode set up completely yet */
  108. rw_enter_write(&nfs_hashlock);
  109. if (error) {
  110. *npp = NULL;
  111. rw_exit_write(&nfs_hashlock);
  112. return (error);
  113. }
  114. nvp->v_flag |= VLARVAL;
  115. np = RB_FIND(nfs_nodetree, &nmp->nm_ntree, &find);
  116. if (np != NULL) {
  117. vgone(nvp);
  118. rw_exit_write(&nfs_hashlock);
  119. goto loop;
  120. }
  121. vp = nvp;
  122. np = pool_get(&nfs_node_pool, PR_WAITOK | PR_ZERO);
  123. vp->v_data = np;
  124. /* we now have an nfsnode on this vnode */
  125. vp->v_flag &= ~VLARVAL;
  126. np->n_vnode = vp;
  127. rw_init(&np->n_commitlock, "nfs_commitlk");
  128. /*
  129. * Are we getting the root? If so, make sure the vnode flags
  130. * are correct
  131. */
  132. if ((fhsize == nmp->nm_fhsize) && !bcmp(fh, nmp->nm_fh, fhsize)) {
  133. if (vp->v_type == VNON)
  134. vp->v_type = VDIR;
  135. vp->v_flag |= VROOT;
  136. }
  137. np->n_fhp = &np->n_fh;
  138. bcopy(fh, np->n_fhp, fhsize);
  139. np->n_fhsize = fhsize;
  140. np2 = RB_INSERT(nfs_nodetree, &nmp->nm_ntree, np);
  141. KASSERT(np2 == NULL);
  142. np->n_accstamp = -1;
  143. rw_exit(&nfs_hashlock);
  144. *npp = np;
  145. return (0);
  146. }
  147. int
  148. nfs_inactive(void *v)
  149. {
  150. struct vop_inactive_args *ap = v;
  151. struct nfsnode *np;
  152. struct sillyrename *sp;
  153. #ifdef DIAGNOSTIC
  154. if (prtactive && ap->a_vp->v_usecount != 0)
  155. vprint("nfs_inactive: pushing active", ap->a_vp);
  156. #endif
  157. if (ap->a_vp->v_flag & VLARVAL)
  158. /*
  159. * vnode was incompletely set up, just return
  160. * as we are throwing it away.
  161. */
  162. return(0);
  163. #ifdef DIAGNOSTIC
  164. if (ap->a_vp->v_data == NULL)
  165. panic("NULL v_data (no nfsnode set up?) in vnode %p",
  166. ap->a_vp);
  167. #endif
  168. np = VTONFS(ap->a_vp);
  169. if (ap->a_vp->v_type != VDIR) {
  170. sp = np->n_sillyrename;
  171. np->n_sillyrename = NULL;
  172. } else
  173. sp = NULL;
  174. if (sp) {
  175. /*
  176. * Remove the silly file that was rename'd earlier
  177. */
  178. nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, curproc);
  179. nfs_removeit(sp);
  180. crfree(sp->s_cred);
  181. vrele(sp->s_dvp);
  182. free(sp, M_NFSREQ, sizeof(*sp));
  183. }
  184. np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT);
  185. VOP_UNLOCK(ap->a_vp, 0, ap->a_p);
  186. return (0);
  187. }
  188. /*
  189. * Reclaim an nfsnode so that it can be used for other purposes.
  190. */
  191. int
  192. nfs_reclaim(void *v)
  193. {
  194. struct vop_reclaim_args *ap = v;
  195. struct vnode *vp = ap->a_vp;
  196. struct nfsmount *nmp;
  197. struct nfsnode *np = VTONFS(vp);
  198. #ifdef DIAGNOSTIC
  199. if (prtactive && vp->v_usecount != 0)
  200. vprint("nfs_reclaim: pushing active", vp);
  201. #endif
  202. if (ap->a_vp->v_flag & VLARVAL)
  203. /*
  204. * vnode was incompletely set up, just return
  205. * as we are throwing it away.
  206. */
  207. return(0);
  208. #ifdef DIAGNOSTIC
  209. if (ap->a_vp->v_data == NULL)
  210. panic("NULL v_data (no nfsnode set up?) in vnode %p",
  211. ap->a_vp);
  212. #endif
  213. nmp = VFSTONFS(vp->v_mount);
  214. rw_enter_write(&nfs_hashlock);
  215. RB_REMOVE(nfs_nodetree, &nmp->nm_ntree, np);
  216. rw_exit_write(&nfs_hashlock);
  217. if (np->n_rcred)
  218. crfree(np->n_rcred);
  219. if (np->n_wcred)
  220. crfree(np->n_wcred);
  221. cache_purge(vp);
  222. pool_put(&nfs_node_pool, vp->v_data);
  223. vp->v_data = NULL;
  224. return (0);
  225. }