svcsubs.c 10 KB

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