svcsubs.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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/module.h>
  20. #include <linux/mount.h>
  21. #define NLMDBG_FACILITY NLMDBG_SVCSUBS
  22. /*
  23. * Global file hash table
  24. */
  25. #define FILE_HASH_BITS 7
  26. #define FILE_NRHASH (1<<FILE_HASH_BITS)
  27. static struct hlist_head 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_path.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. __be32
  72. nlm_lookup_file(struct svc_rqst *rqstp, struct nlm_file **result,
  73. struct nfs_fh *f)
  74. {
  75. struct hlist_node *pos;
  76. struct nlm_file *file;
  77. unsigned int hash;
  78. __be32 nfserr;
  79. nlm_debug_print_fh("nlm_lookup_file", f);
  80. hash = file_hash(f);
  81. /* Lock file table */
  82. mutex_lock(&nlm_file_mutex);
  83. hlist_for_each_entry(file, pos, &nlm_files[hash], f_list)
  84. if (!nfs_compare_fh(&file->f_handle, f))
  85. goto found;
  86. nlm_debug_print_fh("creating file for", f);
  87. nfserr = nlm_lck_denied_nolocks;
  88. file = kzalloc(sizeof(*file), GFP_KERNEL);
  89. if (!file)
  90. goto out_unlock;
  91. memcpy(&file->f_handle, f, sizeof(struct nfs_fh));
  92. mutex_init(&file->f_mutex);
  93. INIT_HLIST_NODE(&file->f_list);
  94. INIT_LIST_HEAD(&file->f_blocks);
  95. /* Open the file. Note that this must not sleep for too long, else
  96. * we would lock up lockd:-) So no NFS re-exports, folks.
  97. *
  98. * We have to make sure we have the right credential to open
  99. * the file.
  100. */
  101. if ((nfserr = nlmsvc_ops->fopen(rqstp, f, &file->f_file)) != 0) {
  102. dprintk("lockd: open failed (error %d)\n", nfserr);
  103. goto out_free;
  104. }
  105. hlist_add_head(&file->f_list, &nlm_files[hash]);
  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. goto out_unlock;
  117. }
  118. /*
  119. * Delete a file after having released all locks, blocks and shares
  120. */
  121. static inline void
  122. nlm_delete_file(struct nlm_file *file)
  123. {
  124. nlm_debug_print_file("closing file", file);
  125. if (!hlist_unhashed(&file->f_list)) {
  126. hlist_del(&file->f_list);
  127. nlmsvc_ops->fclose(file->f_file);
  128. kfree(file);
  129. } else {
  130. printk(KERN_WARNING "lockd: attempt to release unknown file!\n");
  131. }
  132. }
  133. /*
  134. * Loop over all locks on the given file and perform the specified
  135. * action.
  136. */
  137. static int
  138. nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file,
  139. nlm_host_match_fn_t match)
  140. {
  141. struct inode *inode = nlmsvc_file_inode(file);
  142. struct file_lock *fl;
  143. struct nlm_host *lockhost;
  144. again:
  145. file->f_locks = 0;
  146. for (fl = inode->i_flock; fl; fl = fl->fl_next) {
  147. if (fl->fl_lmops != &nlmsvc_lock_operations)
  148. continue;
  149. /* update current lock count */
  150. file->f_locks++;
  151. lockhost = (struct nlm_host *) fl->fl_owner;
  152. if (match(lockhost, host)) {
  153. struct file_lock lock = *fl;
  154. lock.fl_type = F_UNLCK;
  155. lock.fl_start = 0;
  156. lock.fl_end = OFFSET_MAX;
  157. if (vfs_lock_file(file->f_file, F_SETLK, &lock, NULL) < 0) {
  158. printk("lockd: unlock failure in %s:%d\n",
  159. __FILE__, __LINE__);
  160. return 1;
  161. }
  162. goto again;
  163. }
  164. }
  165. return 0;
  166. }
  167. static int
  168. nlmsvc_always_match(void *dummy1, struct nlm_host *dummy2)
  169. {
  170. return 1;
  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(void *data, nlm_host_match_fn_t match,
  205. int (*is_failover_file)(void *data, struct nlm_file *file))
  206. {
  207. struct hlist_node *pos, *next;
  208. struct nlm_file *file;
  209. int i, ret = 0;
  210. mutex_lock(&nlm_file_mutex);
  211. for (i = 0; i < FILE_NRHASH; i++) {
  212. hlist_for_each_entry_safe(file, pos, next, &nlm_files[i], f_list) {
  213. if (is_failover_file && !is_failover_file(data, file))
  214. continue;
  215. file->f_count++;
  216. mutex_unlock(&nlm_file_mutex);
  217. /* Traverse locks, blocks and shares of this file
  218. * and update file->f_locks count */
  219. if (nlm_inspect_file(data, file, match))
  220. ret = 1;
  221. mutex_lock(&nlm_file_mutex);
  222. file->f_count--;
  223. /* No more references to this file. Let go of it. */
  224. if (list_empty(&file->f_blocks) && !file->f_locks
  225. && !file->f_shares && !file->f_count) {
  226. hlist_del(&file->f_list);
  227. nlmsvc_ops->fclose(file->f_file);
  228. kfree(file);
  229. }
  230. }
  231. }
  232. mutex_unlock(&nlm_file_mutex);
  233. return ret;
  234. }
  235. /*
  236. * Release file. If there are no more remote locks on this file,
  237. * close it and free the handle.
  238. *
  239. * Note that we can't do proper reference counting without major
  240. * contortions because the code in fs/locks.c creates, deletes and
  241. * splits locks without notification. Our only way is to walk the
  242. * entire lock list each time we remove a lock.
  243. */
  244. void
  245. nlm_release_file(struct nlm_file *file)
  246. {
  247. dprintk("lockd: nlm_release_file(%p, ct = %d)\n",
  248. file, file->f_count);
  249. /* Lock file table */
  250. mutex_lock(&nlm_file_mutex);
  251. /* If there are no more locks etc, delete the file */
  252. if (--file->f_count == 0 && !nlm_file_inuse(file))
  253. nlm_delete_file(file);
  254. mutex_unlock(&nlm_file_mutex);
  255. }
  256. /*
  257. * Helpers function for resource traversal
  258. *
  259. * nlmsvc_mark_host:
  260. * used by the garbage collector; simply sets h_inuse.
  261. * Always returns 0.
  262. *
  263. * nlmsvc_same_host:
  264. * returns 1 iff the two hosts match. Used to release
  265. * all resources bound to a specific host.
  266. *
  267. * nlmsvc_is_client:
  268. * returns 1 iff the host is a client.
  269. * Used by nlmsvc_invalidate_all
  270. */
  271. static int
  272. nlmsvc_mark_host(void *data, struct nlm_host *dummy)
  273. {
  274. struct nlm_host *host = data;
  275. host->h_inuse = 1;
  276. return 0;
  277. }
  278. static int
  279. nlmsvc_same_host(void *data, struct nlm_host *other)
  280. {
  281. struct nlm_host *host = data;
  282. return host == other;
  283. }
  284. static int
  285. nlmsvc_is_client(void *data, struct nlm_host *dummy)
  286. {
  287. struct nlm_host *host = data;
  288. if (host->h_server) {
  289. /* we are destroying locks even though the client
  290. * hasn't asked us too, so don't unmonitor the
  291. * client
  292. */
  293. if (host->h_nsmhandle)
  294. host->h_nsmhandle->sm_sticky = 1;
  295. return 1;
  296. } else
  297. return 0;
  298. }
  299. /*
  300. * Mark all hosts that still hold resources
  301. */
  302. void
  303. nlmsvc_mark_resources(void)
  304. {
  305. dprintk("lockd: nlmsvc_mark_resources\n");
  306. nlm_traverse_files(NULL, nlmsvc_mark_host, NULL);
  307. }
  308. /*
  309. * Release all resources held by the given client
  310. */
  311. void
  312. nlmsvc_free_host_resources(struct nlm_host *host)
  313. {
  314. dprintk("lockd: nlmsvc_free_host_resources\n");
  315. if (nlm_traverse_files(host, nlmsvc_same_host, NULL)) {
  316. printk(KERN_WARNING
  317. "lockd: couldn't remove all locks held by %s\n",
  318. host->h_name);
  319. BUG();
  320. }
  321. }
  322. /**
  323. * nlmsvc_invalidate_all - remove all locks held for clients
  324. *
  325. * Release all locks held by NFS clients.
  326. *
  327. */
  328. void
  329. nlmsvc_invalidate_all(void)
  330. {
  331. /*
  332. * Previously, the code would call
  333. * nlmsvc_free_host_resources for each client in
  334. * turn, which is about as inefficient as it gets.
  335. * Now we just do it once in nlm_traverse_files.
  336. */
  337. nlm_traverse_files(NULL, nlmsvc_is_client, NULL);
  338. }
  339. static int
  340. nlmsvc_match_sb(void *datap, struct nlm_file *file)
  341. {
  342. struct super_block *sb = datap;
  343. return sb == file->f_file->f_path.mnt->mnt_sb;
  344. }
  345. /**
  346. * nlmsvc_unlock_all_by_sb - release locks held on this file system
  347. * @sb: super block
  348. *
  349. * Release all locks held by clients accessing this file system.
  350. */
  351. int
  352. nlmsvc_unlock_all_by_sb(struct super_block *sb)
  353. {
  354. int ret;
  355. ret = nlm_traverse_files(sb, nlmsvc_always_match, nlmsvc_match_sb);
  356. return ret ? -EIO : 0;
  357. }
  358. EXPORT_SYMBOL_GPL(nlmsvc_unlock_all_by_sb);
  359. static int
  360. nlmsvc_match_ip(void *datap, struct nlm_host *host)
  361. {
  362. return nlm_cmp_addr(nlm_srcaddr(host), datap);
  363. }
  364. /**
  365. * nlmsvc_unlock_all_by_ip - release local locks by IP address
  366. * @server_addr: server's IP address as seen by clients
  367. *
  368. * Release all locks held by clients accessing this host
  369. * via the passed in IP address.
  370. */
  371. int
  372. nlmsvc_unlock_all_by_ip(struct sockaddr *server_addr)
  373. {
  374. int ret;
  375. ret = nlm_traverse_files(server_addr, nlmsvc_match_ip, NULL);
  376. return ret ? -EIO : 0;
  377. }
  378. EXPORT_SYMBOL_GPL(nlmsvc_unlock_all_by_ip);