svcsubs.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * linux/fs/lockd/svcsubs.c
  3. *
  4. * Various support routines for the NLM server.
  5. *
  6. * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
  7. */
  8. #include <linux/config.h>
  9. #include <linux/types.h>
  10. #include <linux/string.h>
  11. #include <linux/time.h>
  12. #include <linux/in.h>
  13. #include <linux/sunrpc/svc.h>
  14. #include <linux/sunrpc/clnt.h>
  15. #include <linux/nfsd/nfsfh.h>
  16. #include <linux/nfsd/export.h>
  17. #include <linux/lockd/lockd.h>
  18. #include <linux/lockd/share.h>
  19. #include <linux/lockd/sm_inter.h>
  20. #define NLMDBG_FACILITY NLMDBG_SVCSUBS
  21. /*
  22. * Global file hash table
  23. */
  24. #define FILE_HASH_BITS 5
  25. #define FILE_NRHASH (1<<FILE_HASH_BITS)
  26. static struct nlm_file * nlm_files[FILE_NRHASH];
  27. static DECLARE_MUTEX(nlm_file_sema);
  28. static inline unsigned int file_hash(struct nfs_fh *f)
  29. {
  30. unsigned int tmp=0;
  31. int i;
  32. for (i=0; i<NFS2_FHSIZE;i++)
  33. tmp += f->data[i];
  34. return tmp & (FILE_NRHASH - 1);
  35. }
  36. /*
  37. * Lookup file info. If it doesn't exist, create a file info struct
  38. * and open a (VFS) file for the given inode.
  39. *
  40. * FIXME:
  41. * Note that we open the file O_RDONLY even when creating write locks.
  42. * This is not quite right, but for now, we assume the client performs
  43. * the proper R/W checking.
  44. */
  45. u32
  46. nlm_lookup_file(struct svc_rqst *rqstp, struct nlm_file **result,
  47. struct nfs_fh *f)
  48. {
  49. struct nlm_file *file;
  50. unsigned int hash;
  51. u32 nfserr;
  52. u32 *fhp = (u32*)f->data;
  53. dprintk("lockd: nlm_file_lookup(%08x %08x %08x %08x %08x %08x)\n",
  54. fhp[0], fhp[1], fhp[2], fhp[3], fhp[4], fhp[5]);
  55. hash = file_hash(f);
  56. /* Lock file table */
  57. down(&nlm_file_sema);
  58. for (file = nlm_files[hash]; file; file = file->f_next)
  59. if (!nfs_compare_fh(&file->f_handle, f))
  60. goto found;
  61. dprintk("lockd: creating file for (%08x %08x %08x %08x %08x %08x)\n",
  62. fhp[0], fhp[1], fhp[2], fhp[3], fhp[4], fhp[5]);
  63. nfserr = nlm_lck_denied_nolocks;
  64. file = (struct nlm_file *) kmalloc(sizeof(*file), GFP_KERNEL);
  65. if (!file)
  66. goto out_unlock;
  67. memset(file, 0, sizeof(*file));
  68. memcpy(&file->f_handle, f, sizeof(struct nfs_fh));
  69. file->f_hash = hash;
  70. init_MUTEX(&file->f_sema);
  71. /* Open the file. Note that this must not sleep for too long, else
  72. * we would lock up lockd:-) So no NFS re-exports, folks.
  73. *
  74. * We have to make sure we have the right credential to open
  75. * the file.
  76. */
  77. if ((nfserr = nlmsvc_ops->fopen(rqstp, f, &file->f_file)) != 0) {
  78. dprintk("lockd: open failed (nfserr %d)\n", ntohl(nfserr));
  79. goto out_free;
  80. }
  81. file->f_next = nlm_files[hash];
  82. nlm_files[hash] = file;
  83. found:
  84. dprintk("lockd: found file %p (count %d)\n", file, file->f_count);
  85. *result = file;
  86. file->f_count++;
  87. nfserr = 0;
  88. out_unlock:
  89. up(&nlm_file_sema);
  90. return nfserr;
  91. out_free:
  92. kfree(file);
  93. #ifdef CONFIG_LOCKD_V4
  94. if (nfserr == 1)
  95. nfserr = nlm4_stale_fh;
  96. else
  97. #endif
  98. nfserr = nlm_lck_denied;
  99. goto out_unlock;
  100. }
  101. /*
  102. * Delete a file after having released all locks, blocks and shares
  103. */
  104. static inline void
  105. nlm_delete_file(struct nlm_file *file)
  106. {
  107. struct inode *inode = file->f_file->f_dentry->d_inode;
  108. struct nlm_file **fp, *f;
  109. dprintk("lockd: closing file %s/%ld\n",
  110. inode->i_sb->s_id, inode->i_ino);
  111. fp = nlm_files + file->f_hash;
  112. while ((f = *fp) != NULL) {
  113. if (f == file) {
  114. *fp = file->f_next;
  115. nlmsvc_ops->fclose(file->f_file);
  116. kfree(file);
  117. return;
  118. }
  119. fp = &f->f_next;
  120. }
  121. printk(KERN_WARNING "lockd: attempt to release unknown file!\n");
  122. }
  123. /*
  124. * Loop over all locks on the given file and perform the specified
  125. * action.
  126. */
  127. static int
  128. nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file, int action)
  129. {
  130. struct inode *inode = nlmsvc_file_inode(file);
  131. struct file_lock *fl;
  132. struct nlm_host *lockhost;
  133. again:
  134. file->f_locks = 0;
  135. for (fl = inode->i_flock; fl; fl = fl->fl_next) {
  136. if (!(fl->fl_flags & FL_LOCKD))
  137. continue;
  138. /* update current lock count */
  139. file->f_locks++;
  140. lockhost = (struct nlm_host *) fl->fl_owner;
  141. if (action == NLM_ACT_MARK)
  142. lockhost->h_inuse = 1;
  143. else if (action == NLM_ACT_CHECK)
  144. return 1;
  145. else if (action == NLM_ACT_UNLOCK) {
  146. struct file_lock lock = *fl;
  147. if (host && lockhost != host)
  148. continue;
  149. lock.fl_type = F_UNLCK;
  150. lock.fl_start = 0;
  151. lock.fl_end = OFFSET_MAX;
  152. if (posix_lock_file(file->f_file, &lock) < 0) {
  153. printk("lockd: unlock failure in %s:%d\n",
  154. __FILE__, __LINE__);
  155. return 1;
  156. }
  157. goto again;
  158. }
  159. }
  160. return 0;
  161. }
  162. /*
  163. * Operate on a single file
  164. */
  165. static inline int
  166. nlm_inspect_file(struct nlm_host *host, struct nlm_file *file, int action)
  167. {
  168. if (action == NLM_ACT_CHECK) {
  169. /* Fast path for mark and sweep garbage collection */
  170. if (file->f_count || file->f_blocks || file->f_shares)
  171. return 1;
  172. } else {
  173. if (nlmsvc_traverse_blocks(host, file, action)
  174. || nlmsvc_traverse_shares(host, file, action))
  175. return 1;
  176. }
  177. return nlm_traverse_locks(host, file, action);
  178. }
  179. /*
  180. * Loop over all files in the file table.
  181. */
  182. static int
  183. nlm_traverse_files(struct nlm_host *host, int action)
  184. {
  185. struct nlm_file *file, **fp;
  186. int i;
  187. down(&nlm_file_sema);
  188. for (i = 0; i < FILE_NRHASH; i++) {
  189. fp = nlm_files + i;
  190. while ((file = *fp) != NULL) {
  191. /* Traverse locks, blocks and shares of this file
  192. * and update file->f_locks count */
  193. if (nlm_inspect_file(host, file, action)) {
  194. up(&nlm_file_sema);
  195. return 1;
  196. }
  197. /* No more references to this file. Let go of it. */
  198. if (!file->f_blocks && !file->f_locks
  199. && !file->f_shares && !file->f_count) {
  200. *fp = file->f_next;
  201. nlmsvc_ops->fclose(file->f_file);
  202. kfree(file);
  203. } else {
  204. fp = &file->f_next;
  205. }
  206. }
  207. }
  208. up(&nlm_file_sema);
  209. return 0;
  210. }
  211. /*
  212. * Release file. If there are no more remote locks on this file,
  213. * close it and free the handle.
  214. *
  215. * Note that we can't do proper reference counting without major
  216. * contortions because the code in fs/locks.c creates, deletes and
  217. * splits locks without notification. Our only way is to walk the
  218. * entire lock list each time we remove a lock.
  219. */
  220. void
  221. nlm_release_file(struct nlm_file *file)
  222. {
  223. dprintk("lockd: nlm_release_file(%p, ct = %d)\n",
  224. file, file->f_count);
  225. /* Lock file table */
  226. down(&nlm_file_sema);
  227. /* If there are no more locks etc, delete the file */
  228. if(--file->f_count == 0) {
  229. if(!nlm_inspect_file(NULL, file, NLM_ACT_CHECK))
  230. nlm_delete_file(file);
  231. }
  232. up(&nlm_file_sema);
  233. }
  234. /*
  235. * Mark all hosts that still hold resources
  236. */
  237. void
  238. nlmsvc_mark_resources(void)
  239. {
  240. dprintk("lockd: nlmsvc_mark_resources\n");
  241. nlm_traverse_files(NULL, NLM_ACT_MARK);
  242. }
  243. /*
  244. * Release all resources held by the given client
  245. */
  246. void
  247. nlmsvc_free_host_resources(struct nlm_host *host)
  248. {
  249. dprintk("lockd: nlmsvc_free_host_resources\n");
  250. if (nlm_traverse_files(host, NLM_ACT_UNLOCK))
  251. printk(KERN_WARNING
  252. "lockd: couldn't remove all locks held by %s",
  253. host->h_name);
  254. }
  255. /*
  256. * delete all hosts structs for clients
  257. */
  258. void
  259. nlmsvc_invalidate_all(void)
  260. {
  261. struct nlm_host *host;
  262. while ((host = nlm_find_client()) != NULL) {
  263. nlmsvc_free_host_resources(host);
  264. host->h_expires = 0;
  265. host->h_killed = 1;
  266. nlm_release_host(host);
  267. }
  268. }