svcsubs.c 8.8 KB

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