svcsubs.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. static int
  169. nlmsvc_always_match(void *dummy1, struct nlm_host *dummy2)
  170. {
  171. return 1;
  172. }
  173. /*
  174. * Inspect a single file
  175. */
  176. static inline int
  177. nlm_inspect_file(struct nlm_host *host, struct nlm_file *file, nlm_host_match_fn_t match)
  178. {
  179. nlmsvc_traverse_blocks(host, file, match);
  180. nlmsvc_traverse_shares(host, file, match);
  181. return nlm_traverse_locks(host, file, match);
  182. }
  183. /*
  184. * Quick check whether there are still any locks, blocks or
  185. * shares on a given file.
  186. */
  187. static inline int
  188. nlm_file_inuse(struct nlm_file *file)
  189. {
  190. struct inode *inode = nlmsvc_file_inode(file);
  191. struct file_lock *fl;
  192. if (file->f_count || !list_empty(&file->f_blocks) || file->f_shares)
  193. return 1;
  194. for (fl = inode->i_flock; fl; fl = fl->fl_next) {
  195. if (fl->fl_lmops == &nlmsvc_lock_operations)
  196. return 1;
  197. }
  198. file->f_locks = 0;
  199. return 0;
  200. }
  201. /*
  202. * Loop over all files in the file table.
  203. */
  204. static int
  205. nlm_traverse_files(void *data, nlm_host_match_fn_t match,
  206. int (*is_failover_file)(void *data, struct nlm_file *file))
  207. {
  208. struct hlist_node *pos, *next;
  209. struct nlm_file *file;
  210. int i, ret = 0;
  211. mutex_lock(&nlm_file_mutex);
  212. for (i = 0; i < FILE_NRHASH; i++) {
  213. hlist_for_each_entry_safe(file, pos, next, &nlm_files[i], f_list) {
  214. if (is_failover_file && !is_failover_file(data, file))
  215. continue;
  216. file->f_count++;
  217. mutex_unlock(&nlm_file_mutex);
  218. /* Traverse locks, blocks and shares of this file
  219. * and update file->f_locks count */
  220. if (nlm_inspect_file(data, file, match))
  221. ret = 1;
  222. mutex_lock(&nlm_file_mutex);
  223. file->f_count--;
  224. /* No more references to this file. Let go of it. */
  225. if (list_empty(&file->f_blocks) && !file->f_locks
  226. && !file->f_shares && !file->f_count) {
  227. hlist_del(&file->f_list);
  228. nlmsvc_ops->fclose(file->f_file);
  229. kfree(file);
  230. }
  231. }
  232. }
  233. mutex_unlock(&nlm_file_mutex);
  234. return ret;
  235. }
  236. /*
  237. * Release file. If there are no more remote locks on this file,
  238. * close it and free the handle.
  239. *
  240. * Note that we can't do proper reference counting without major
  241. * contortions because the code in fs/locks.c creates, deletes and
  242. * splits locks without notification. Our only way is to walk the
  243. * entire lock list each time we remove a lock.
  244. */
  245. void
  246. nlm_release_file(struct nlm_file *file)
  247. {
  248. dprintk("lockd: nlm_release_file(%p, ct = %d)\n",
  249. file, file->f_count);
  250. /* Lock file table */
  251. mutex_lock(&nlm_file_mutex);
  252. /* If there are no more locks etc, delete the file */
  253. if (--file->f_count == 0 && !nlm_file_inuse(file))
  254. nlm_delete_file(file);
  255. mutex_unlock(&nlm_file_mutex);
  256. }
  257. /*
  258. * Helpers function for resource traversal
  259. *
  260. * nlmsvc_mark_host:
  261. * used by the garbage collector; simply sets h_inuse.
  262. * Always returns 0.
  263. *
  264. * nlmsvc_same_host:
  265. * returns 1 iff the two hosts match. Used to release
  266. * all resources bound to a specific host.
  267. *
  268. * nlmsvc_is_client:
  269. * returns 1 iff the host is a client.
  270. * Used by nlmsvc_invalidate_all
  271. */
  272. static int
  273. nlmsvc_mark_host(void *data, struct nlm_host *dummy)
  274. {
  275. struct nlm_host *host = data;
  276. host->h_inuse = 1;
  277. return 0;
  278. }
  279. static int
  280. nlmsvc_same_host(void *data, struct nlm_host *other)
  281. {
  282. struct nlm_host *host = data;
  283. return host == other;
  284. }
  285. static int
  286. nlmsvc_is_client(void *data, struct nlm_host *dummy)
  287. {
  288. struct nlm_host *host = data;
  289. if (host->h_server) {
  290. /* we are destroying locks even though the client
  291. * hasn't asked us too, so don't unmonitor the
  292. * client
  293. */
  294. if (host->h_nsmhandle)
  295. host->h_nsmhandle->sm_sticky = 1;
  296. return 1;
  297. } else
  298. return 0;
  299. }
  300. /*
  301. * Mark all hosts that still hold resources
  302. */
  303. void
  304. nlmsvc_mark_resources(void)
  305. {
  306. dprintk("lockd: nlmsvc_mark_resources\n");
  307. nlm_traverse_files(NULL, nlmsvc_mark_host, NULL);
  308. }
  309. /*
  310. * Release all resources held by the given client
  311. */
  312. void
  313. nlmsvc_free_host_resources(struct nlm_host *host)
  314. {
  315. dprintk("lockd: nlmsvc_free_host_resources\n");
  316. if (nlm_traverse_files(host, nlmsvc_same_host, NULL)) {
  317. printk(KERN_WARNING
  318. "lockd: couldn't remove all locks held by %s\n",
  319. host->h_name);
  320. BUG();
  321. }
  322. }
  323. /**
  324. * nlmsvc_invalidate_all - remove all locks held for clients
  325. *
  326. * Release all locks held by NFS clients.
  327. *
  328. */
  329. void
  330. nlmsvc_invalidate_all(void)
  331. {
  332. /*
  333. * Previously, the code would call
  334. * nlmsvc_free_host_resources for each client in
  335. * turn, which is about as inefficient as it gets.
  336. * Now we just do it once in nlm_traverse_files.
  337. */
  338. nlm_traverse_files(NULL, nlmsvc_is_client, NULL);
  339. }
  340. static int
  341. nlmsvc_match_sb(void *datap, struct nlm_file *file)
  342. {
  343. struct super_block *sb = datap;
  344. return sb == file->f_file->f_path.mnt->mnt_sb;
  345. }
  346. /**
  347. * nlmsvc_unlock_all_by_sb - release locks held on this file system
  348. * @sb: super block
  349. *
  350. * Release all locks held by clients accessing this file system.
  351. */
  352. int
  353. nlmsvc_unlock_all_by_sb(struct super_block *sb)
  354. {
  355. int ret;
  356. ret = nlm_traverse_files(sb, nlmsvc_always_match, nlmsvc_match_sb);
  357. return ret ? -EIO : 0;
  358. }
  359. EXPORT_SYMBOL_GPL(nlmsvc_unlock_all_by_sb);
  360. static int
  361. nlmsvc_match_ip(void *datap, struct nlm_host *host)
  362. {
  363. return nlm_cmp_addr(nlm_srcaddr(host), datap);
  364. }
  365. /**
  366. * nlmsvc_unlock_all_by_ip - release local locks by IP address
  367. * @server_addr: server's IP address as seen by clients
  368. *
  369. * Release all locks held by clients accessing this host
  370. * via the passed in IP address.
  371. */
  372. int
  373. nlmsvc_unlock_all_by_ip(struct sockaddr *server_addr)
  374. {
  375. int ret;
  376. ret = nlm_traverse_files(server_addr, nlmsvc_match_ip, NULL);
  377. return ret ? -EIO : 0;
  378. }
  379. EXPORT_SYMBOL_GPL(nlmsvc_unlock_all_by_ip);