svcsubs.c 10 KB

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