svcsubs.c 9.9 KB

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