svcsubs.c 7.4 KB

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