svcsubs.c 10 KB

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