svcsubs.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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/types.h>
  9. #include <linux/string.h>
  10. #include <linux/time.h>
  11. #include <linux/in.h>
  12. #include <linux/mutex.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 7
  25. #define FILE_NRHASH (1<<FILE_HASH_BITS)
  26. static struct hlist_head nlm_files[FILE_NRHASH];
  27. static DEFINE_MUTEX(nlm_file_mutex);
  28. #ifdef NFSD_DEBUG
  29. static inline void nlm_debug_print_fh(char *msg, struct nfs_fh *f)
  30. {
  31. u32 *fhp = (u32*)f->data;
  32. /* print the first 32 bytes of the fh */
  33. dprintk("lockd: %s (%08x %08x %08x %08x %08x %08x %08x %08x)\n",
  34. msg, fhp[0], fhp[1], fhp[2], fhp[3],
  35. fhp[4], fhp[5], fhp[6], fhp[7]);
  36. }
  37. static inline void nlm_debug_print_file(char *msg, struct nlm_file *file)
  38. {
  39. struct inode *inode = file->f_file->f_dentry->d_inode;
  40. dprintk("lockd: %s %s/%ld\n",
  41. msg, inode->i_sb->s_id, inode->i_ino);
  42. }
  43. #else
  44. static inline void nlm_debug_print_fh(char *msg, struct nfs_fh *f)
  45. {
  46. return;
  47. }
  48. static inline void nlm_debug_print_file(char *msg, struct nlm_file *file)
  49. {
  50. return;
  51. }
  52. #endif
  53. static inline unsigned int file_hash(struct nfs_fh *f)
  54. {
  55. unsigned int tmp=0;
  56. int i;
  57. for (i=0; i<NFS2_FHSIZE;i++)
  58. tmp += f->data[i];
  59. return tmp & (FILE_NRHASH - 1);
  60. }
  61. /*
  62. * Lookup file info. If it doesn't exist, create a file info struct
  63. * and open a (VFS) file for the given inode.
  64. *
  65. * FIXME:
  66. * Note that we open the file O_RDONLY even when creating write locks.
  67. * This is not quite right, but for now, we assume the client performs
  68. * the proper R/W checking.
  69. */
  70. u32
  71. nlm_lookup_file(struct svc_rqst *rqstp, struct nlm_file **result,
  72. struct nfs_fh *f)
  73. {
  74. struct hlist_node *pos;
  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. hlist_for_each_entry(file, pos, &nlm_files[hash], f_list)
  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 = kzalloc(sizeof(*file), GFP_KERNEL);
  88. if (!file)
  89. goto out_unlock;
  90. memcpy(&file->f_handle, f, sizeof(struct nfs_fh));
  91. mutex_init(&file->f_mutex);
  92. INIT_HLIST_NODE(&file->f_list);
  93. INIT_LIST_HEAD(&file->f_blocks);
  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 (error %d)\n", nfserr);
  102. goto out_free;
  103. }
  104. hlist_add_head(&file->f_list, &nlm_files[hash]);
  105. found:
  106. dprintk("lockd: found file %p (count %d)\n", file, file->f_count);
  107. *result = file;
  108. file->f_count++;
  109. nfserr = 0;
  110. out_unlock:
  111. mutex_unlock(&nlm_file_mutex);
  112. return nfserr;
  113. out_free:
  114. kfree(file);
  115. #ifdef CONFIG_LOCKD_V4
  116. if (nfserr == 1)
  117. nfserr = nlm4_stale_fh;
  118. else
  119. #endif
  120. nfserr = nlm_lck_denied;
  121. goto out_unlock;
  122. }
  123. /*
  124. * Delete a file after having released all locks, blocks and shares
  125. */
  126. static inline void
  127. nlm_delete_file(struct nlm_file *file)
  128. {
  129. nlm_debug_print_file("closing file", file);
  130. if (!hlist_unhashed(&file->f_list)) {
  131. hlist_del(&file->f_list);
  132. nlmsvc_ops->fclose(file->f_file);
  133. kfree(file);
  134. } else {
  135. printk(KERN_WARNING "lockd: attempt to release unknown file!\n");
  136. }
  137. }
  138. /*
  139. * Loop over all locks on the given file and perform the specified
  140. * action.
  141. */
  142. static int
  143. nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file,
  144. nlm_host_match_fn_t match)
  145. {
  146. struct inode *inode = nlmsvc_file_inode(file);
  147. struct file_lock *fl;
  148. struct nlm_host *lockhost;
  149. again:
  150. file->f_locks = 0;
  151. for (fl = inode->i_flock; fl; fl = fl->fl_next) {
  152. if (fl->fl_lmops != &nlmsvc_lock_operations)
  153. continue;
  154. /* update current lock count */
  155. file->f_locks++;
  156. lockhost = (struct nlm_host *) fl->fl_owner;
  157. if (match(lockhost, host)) {
  158. struct file_lock lock = *fl;
  159. lock.fl_type = F_UNLCK;
  160. lock.fl_start = 0;
  161. lock.fl_end = OFFSET_MAX;
  162. if (posix_lock_file(file->f_file, &lock) < 0) {
  163. printk("lockd: unlock failure in %s:%d\n",
  164. __FILE__, __LINE__);
  165. return 1;
  166. }
  167. goto again;
  168. }
  169. }
  170. return 0;
  171. }
  172. /*
  173. * Inspect a single file
  174. */
  175. static inline int
  176. nlm_inspect_file(struct nlm_host *host, struct nlm_file *file, nlm_host_match_fn_t match)
  177. {
  178. nlmsvc_traverse_blocks(host, file, match);
  179. nlmsvc_traverse_shares(host, file, match);
  180. return nlm_traverse_locks(host, file, match);
  181. }
  182. /*
  183. * Quick check whether there are still any locks, blocks or
  184. * shares on a given file.
  185. */
  186. static inline int
  187. nlm_file_inuse(struct nlm_file *file)
  188. {
  189. struct inode *inode = nlmsvc_file_inode(file);
  190. struct file_lock *fl;
  191. if (file->f_count || !list_empty(&file->f_blocks) || file->f_shares)
  192. return 1;
  193. for (fl = inode->i_flock; fl; fl = fl->fl_next) {
  194. if (fl->fl_lmops == &nlmsvc_lock_operations)
  195. return 1;
  196. }
  197. file->f_locks = 0;
  198. return 0;
  199. }
  200. /*
  201. * Loop over all files in the file table.
  202. */
  203. static int
  204. nlm_traverse_files(struct nlm_host *host, nlm_host_match_fn_t match)
  205. {
  206. struct hlist_node *pos, *next;
  207. struct nlm_file *file;
  208. int i, ret = 0;
  209. mutex_lock(&nlm_file_mutex);
  210. for (i = 0; i < FILE_NRHASH; i++) {
  211. hlist_for_each_entry_safe(file, pos, next, &nlm_files[i], f_list) {
  212. file->f_count++;
  213. mutex_unlock(&nlm_file_mutex);
  214. /* Traverse locks, blocks and shares of this file
  215. * and update file->f_locks count */
  216. if (nlm_inspect_file(host, file, match))
  217. ret = 1;
  218. mutex_lock(&nlm_file_mutex);
  219. file->f_count--;
  220. /* No more references to this file. Let go of it. */
  221. if (list_empty(&file->f_blocks) && !file->f_locks
  222. && !file->f_shares && !file->f_count) {
  223. hlist_del(&file->f_list);
  224. nlmsvc_ops->fclose(file->f_file);
  225. kfree(file);
  226. }
  227. }
  228. }
  229. mutex_unlock(&nlm_file_mutex);
  230. return ret;
  231. }
  232. /*
  233. * Release file. If there are no more remote locks on this file,
  234. * close it and free the handle.
  235. *
  236. * Note that we can't do proper reference counting without major
  237. * contortions because the code in fs/locks.c creates, deletes and
  238. * splits locks without notification. Our only way is to walk the
  239. * entire lock list each time we remove a lock.
  240. */
  241. void
  242. nlm_release_file(struct nlm_file *file)
  243. {
  244. dprintk("lockd: nlm_release_file(%p, ct = %d)\n",
  245. file, file->f_count);
  246. /* Lock file table */
  247. mutex_lock(&nlm_file_mutex);
  248. /* If there are no more locks etc, delete the file */
  249. if (--file->f_count == 0 && !nlm_file_inuse(file))
  250. nlm_delete_file(file);
  251. mutex_unlock(&nlm_file_mutex);
  252. }
  253. /*
  254. * Helpers function for resource traversal
  255. *
  256. * nlmsvc_mark_host:
  257. * used by the garbage collector; simply sets h_inuse.
  258. * Always returns 0.
  259. *
  260. * nlmsvc_same_host:
  261. * returns 1 iff the two hosts match. Used to release
  262. * all resources bound to a specific host.
  263. *
  264. * nlmsvc_is_client:
  265. * returns 1 iff the host is a client.
  266. * Used by nlmsvc_invalidate_all
  267. */
  268. static int
  269. nlmsvc_mark_host(struct nlm_host *host, struct nlm_host *dummy)
  270. {
  271. host->h_inuse = 1;
  272. return 0;
  273. }
  274. static int
  275. nlmsvc_same_host(struct nlm_host *host, struct nlm_host *other)
  276. {
  277. return host == other;
  278. }
  279. static int
  280. nlmsvc_is_client(struct nlm_host *host, struct nlm_host *dummy)
  281. {
  282. return host->h_server;
  283. }
  284. /*
  285. * Mark all hosts that still hold resources
  286. */
  287. void
  288. nlmsvc_mark_resources(void)
  289. {
  290. dprintk("lockd: nlmsvc_mark_resources\n");
  291. nlm_traverse_files(NULL, nlmsvc_mark_host);
  292. }
  293. /*
  294. * Release all resources held by the given client
  295. */
  296. void
  297. nlmsvc_free_host_resources(struct nlm_host *host)
  298. {
  299. dprintk("lockd: nlmsvc_free_host_resources\n");
  300. if (nlm_traverse_files(host, nlmsvc_same_host)) {
  301. printk(KERN_WARNING
  302. "lockd: couldn't remove all locks held by %s\n",
  303. host->h_name);
  304. BUG();
  305. }
  306. }
  307. /*
  308. * Remove all locks held for clients
  309. */
  310. void
  311. nlmsvc_invalidate_all(void)
  312. {
  313. /* Release all locks held by NFS clients.
  314. * Previously, the code would call
  315. * nlmsvc_free_host_resources for each client in
  316. * turn, which is about as inefficient as it gets.
  317. * Now we just do it once in nlm_traverse_files.
  318. */
  319. nlm_traverse_files(NULL, nlmsvc_is_client);
  320. }