svcsubs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/lockd/svcsubs.c
  4. *
  5. * Various support routines for the NLM server.
  6. *
  7. * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
  8. */
  9. #include <linux/types.h>
  10. #include <linux/string.h>
  11. #include <linux/time.h>
  12. #include <linux/in.h>
  13. #include <linux/slab.h>
  14. #include <linux/mutex.h>
  15. #include <linux/sunrpc/svc.h>
  16. #include <linux/sunrpc/addr.h>
  17. #include <linux/lockd/lockd.h>
  18. #include <linux/lockd/share.h>
  19. #include <linux/module.h>
  20. #include <linux/mount.h>
  21. #include <uapi/linux/nfs2.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 CONFIG_SUNRPC_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 = nlmsvc_file_inode(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. int lock_to_openmode(struct file_lock *lock)
  64. {
  65. return lock_is_write(lock) ? O_WRONLY : O_RDONLY;
  66. }
  67. /*
  68. * Open the file. Note that if we're reexporting, for example,
  69. * this could block the lockd thread for a while.
  70. *
  71. * We have to make sure we have the right credential to open
  72. * the file.
  73. */
  74. static __be32 nlm_do_fopen(struct svc_rqst *rqstp,
  75. struct nlm_file *file, int mode)
  76. {
  77. struct file **fp = &file->f_file[mode];
  78. __be32 nfserr;
  79. if (*fp)
  80. return 0;
  81. nfserr = nlmsvc_ops->fopen(rqstp, &file->f_handle, fp, mode);
  82. if (nfserr)
  83. dprintk("lockd: open failed (error %d)\n", nfserr);
  84. return nfserr;
  85. }
  86. /*
  87. * Lookup file info. If it doesn't exist, create a file info struct
  88. * and open a (VFS) file for the given inode.
  89. */
  90. __be32
  91. nlm_lookup_file(struct svc_rqst *rqstp, struct nlm_file **result,
  92. struct nlm_lock *lock)
  93. {
  94. struct nlm_file *file;
  95. unsigned int hash;
  96. __be32 nfserr;
  97. int mode;
  98. nlm_debug_print_fh("nlm_lookup_file", &lock->fh);
  99. hash = file_hash(&lock->fh);
  100. mode = lock_to_openmode(&lock->fl);
  101. /* Lock file table */
  102. mutex_lock(&nlm_file_mutex);
  103. hlist_for_each_entry(file, &nlm_files[hash], f_list)
  104. if (!nfs_compare_fh(&file->f_handle, &lock->fh)) {
  105. mutex_lock(&file->f_mutex);
  106. nfserr = nlm_do_fopen(rqstp, file, mode);
  107. mutex_unlock(&file->f_mutex);
  108. goto found;
  109. }
  110. nlm_debug_print_fh("creating file for", &lock->fh);
  111. nfserr = nlm_lck_denied_nolocks;
  112. file = kzalloc(sizeof(*file), GFP_KERNEL);
  113. if (!file)
  114. goto out_free;
  115. memcpy(&file->f_handle, &lock->fh, sizeof(struct nfs_fh));
  116. mutex_init(&file->f_mutex);
  117. INIT_HLIST_NODE(&file->f_list);
  118. INIT_LIST_HEAD(&file->f_blocks);
  119. nfserr = nlm_do_fopen(rqstp, file, mode);
  120. if (nfserr)
  121. goto out_unlock;
  122. hlist_add_head(&file->f_list, &nlm_files[hash]);
  123. found:
  124. dprintk("lockd: found file %p (count %d)\n", file, file->f_count);
  125. *result = file;
  126. file->f_count++;
  127. out_unlock:
  128. mutex_unlock(&nlm_file_mutex);
  129. return nfserr;
  130. out_free:
  131. kfree(file);
  132. goto out_unlock;
  133. }
  134. /*
  135. * Delete a file after having released all locks, blocks and shares
  136. */
  137. static inline void
  138. nlm_delete_file(struct nlm_file *file)
  139. {
  140. nlm_debug_print_file("closing file", file);
  141. if (!hlist_unhashed(&file->f_list)) {
  142. hlist_del(&file->f_list);
  143. if (file->f_file[O_RDONLY])
  144. nlmsvc_ops->fclose(file->f_file[O_RDONLY]);
  145. if (file->f_file[O_WRONLY])
  146. nlmsvc_ops->fclose(file->f_file[O_WRONLY]);
  147. kfree(file);
  148. } else {
  149. printk(KERN_WARNING "lockd: attempt to release unknown file!\n");
  150. }
  151. }
  152. static int nlm_unlock_files(struct nlm_file *file, const struct file_lock *fl)
  153. {
  154. struct file_lock lock;
  155. locks_init_lock(&lock);
  156. lock.c.flc_type = F_UNLCK;
  157. lock.fl_start = 0;
  158. lock.fl_end = OFFSET_MAX;
  159. lock.c.flc_owner = fl->c.flc_owner;
  160. lock.c.flc_pid = fl->c.flc_pid;
  161. lock.c.flc_flags = FL_POSIX;
  162. lock.c.flc_file = file->f_file[O_RDONLY];
  163. if (lock.c.flc_file && vfs_lock_file(lock.c.flc_file, F_SETLK, &lock, NULL))
  164. goto out_err;
  165. lock.c.flc_file = file->f_file[O_WRONLY];
  166. if (lock.c.flc_file && vfs_lock_file(lock.c.flc_file, F_SETLK, &lock, NULL))
  167. goto out_err;
  168. return 0;
  169. out_err:
  170. pr_warn("lockd: unlock failure in %s:%d\n", __FILE__, __LINE__);
  171. return 1;
  172. }
  173. /*
  174. * Loop over all locks on the given file and perform the specified
  175. * action.
  176. */
  177. static int
  178. nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file,
  179. nlm_host_match_fn_t match)
  180. {
  181. struct inode *inode = nlmsvc_file_inode(file);
  182. struct file_lock *fl;
  183. struct file_lock_context *flctx = locks_inode_context(inode);
  184. struct nlm_host *lockhost;
  185. if (!flctx || list_empty_careful(&flctx->flc_posix))
  186. return 0;
  187. again:
  188. file->f_locks = 0;
  189. spin_lock(&flctx->flc_lock);
  190. for_each_file_lock(fl, &flctx->flc_posix) {
  191. if (fl->fl_lmops != &nlmsvc_lock_operations)
  192. continue;
  193. /* update current lock count */
  194. file->f_locks++;
  195. lockhost = ((struct nlm_lockowner *) fl->c.flc_owner)->host;
  196. if (match(lockhost, host)) {
  197. spin_unlock(&flctx->flc_lock);
  198. if (nlm_unlock_files(file, fl))
  199. return 1;
  200. goto again;
  201. }
  202. }
  203. spin_unlock(&flctx->flc_lock);
  204. return 0;
  205. }
  206. static int
  207. nlmsvc_always_match(void *dummy1, struct nlm_host *dummy2)
  208. {
  209. return 1;
  210. }
  211. /*
  212. * Inspect a single file
  213. */
  214. static inline int
  215. nlm_inspect_file(struct nlm_host *host, struct nlm_file *file, nlm_host_match_fn_t match)
  216. {
  217. nlmsvc_traverse_blocks(host, file, match);
  218. nlmsvc_traverse_shares(host, file, match);
  219. return nlm_traverse_locks(host, file, match);
  220. }
  221. /*
  222. * Quick check whether there are still any locks, blocks or
  223. * shares on a given file.
  224. */
  225. static inline int
  226. nlm_file_inuse(struct nlm_file *file)
  227. {
  228. struct inode *inode = nlmsvc_file_inode(file);
  229. struct file_lock *fl;
  230. struct file_lock_context *flctx = locks_inode_context(inode);
  231. if (file->f_count || !list_empty(&file->f_blocks) || file->f_shares)
  232. return 1;
  233. if (flctx && !list_empty_careful(&flctx->flc_posix)) {
  234. spin_lock(&flctx->flc_lock);
  235. for_each_file_lock(fl, &flctx->flc_posix) {
  236. if (fl->fl_lmops == &nlmsvc_lock_operations) {
  237. spin_unlock(&flctx->flc_lock);
  238. return 1;
  239. }
  240. }
  241. spin_unlock(&flctx->flc_lock);
  242. }
  243. file->f_locks = 0;
  244. return 0;
  245. }
  246. static void nlm_close_files(struct nlm_file *file)
  247. {
  248. if (file->f_file[O_RDONLY])
  249. nlmsvc_ops->fclose(file->f_file[O_RDONLY]);
  250. if (file->f_file[O_WRONLY])
  251. nlmsvc_ops->fclose(file->f_file[O_WRONLY]);
  252. }
  253. /*
  254. * Loop over all files in the file table.
  255. */
  256. static int
  257. nlm_traverse_files(void *data, nlm_host_match_fn_t match,
  258. int (*is_failover_file)(void *data, struct nlm_file *file))
  259. {
  260. struct hlist_node *next;
  261. struct nlm_file *file;
  262. int i, ret = 0;
  263. mutex_lock(&nlm_file_mutex);
  264. for (i = 0; i < FILE_NRHASH; i++) {
  265. hlist_for_each_entry_safe(file, next, &nlm_files[i], f_list) {
  266. if (is_failover_file && !is_failover_file(data, file))
  267. continue;
  268. file->f_count++;
  269. mutex_unlock(&nlm_file_mutex);
  270. /* Traverse locks, blocks and shares of this file
  271. * and update file->f_locks count */
  272. if (nlm_inspect_file(data, file, match))
  273. ret = 1;
  274. mutex_lock(&nlm_file_mutex);
  275. file->f_count--;
  276. /* No more references to this file. Let go of it. */
  277. if (list_empty(&file->f_blocks) && !file->f_locks
  278. && !file->f_shares && !file->f_count) {
  279. hlist_del(&file->f_list);
  280. nlm_close_files(file);
  281. kfree(file);
  282. }
  283. }
  284. }
  285. mutex_unlock(&nlm_file_mutex);
  286. return ret;
  287. }
  288. /*
  289. * Release file. If there are no more remote locks on this file,
  290. * close it and free the handle.
  291. *
  292. * Note that we can't do proper reference counting without major
  293. * contortions because the code in fs/locks.c creates, deletes and
  294. * splits locks without notification. Our only way is to walk the
  295. * entire lock list each time we remove a lock.
  296. */
  297. void
  298. nlm_release_file(struct nlm_file *file)
  299. {
  300. dprintk("lockd: nlm_release_file(%p, ct = %d)\n",
  301. file, file->f_count);
  302. /* Lock file table */
  303. mutex_lock(&nlm_file_mutex);
  304. /* If there are no more locks etc, delete the file */
  305. if (--file->f_count == 0 && !nlm_file_inuse(file))
  306. nlm_delete_file(file);
  307. mutex_unlock(&nlm_file_mutex);
  308. }
  309. /*
  310. * Helpers function for resource traversal
  311. *
  312. * nlmsvc_mark_host:
  313. * used by the garbage collector; simply sets h_inuse only for those
  314. * hosts, which passed network check.
  315. * Always returns 0.
  316. *
  317. * nlmsvc_same_host:
  318. * returns 1 iff the two hosts match. Used to release
  319. * all resources bound to a specific host.
  320. *
  321. * nlmsvc_is_client:
  322. * returns 1 iff the host is a client.
  323. * Used by nlmsvc_invalidate_all
  324. */
  325. static int
  326. nlmsvc_mark_host(void *data, struct nlm_host *hint)
  327. {
  328. struct nlm_host *host = data;
  329. if ((hint->net == NULL) ||
  330. (host->net == hint->net))
  331. host->h_inuse = 1;
  332. return 0;
  333. }
  334. static int
  335. nlmsvc_same_host(void *data, struct nlm_host *other)
  336. {
  337. struct nlm_host *host = data;
  338. return host == other;
  339. }
  340. static int
  341. nlmsvc_is_client(void *data, struct nlm_host *dummy)
  342. {
  343. struct nlm_host *host = data;
  344. if (host->h_server) {
  345. /* we are destroying locks even though the client
  346. * hasn't asked us too, so don't unmonitor the
  347. * client
  348. */
  349. if (host->h_nsmhandle)
  350. host->h_nsmhandle->sm_sticky = 1;
  351. return 1;
  352. } else
  353. return 0;
  354. }
  355. /*
  356. * Mark all hosts that still hold resources
  357. */
  358. void
  359. nlmsvc_mark_resources(struct net *net)
  360. {
  361. struct nlm_host hint;
  362. dprintk("lockd: %s for net %x\n", __func__, net ? net->ns.inum : 0);
  363. hint.net = net;
  364. nlm_traverse_files(&hint, nlmsvc_mark_host, NULL);
  365. }
  366. /*
  367. * Release all resources held by the given client
  368. */
  369. void
  370. nlmsvc_free_host_resources(struct nlm_host *host)
  371. {
  372. dprintk("lockd: nlmsvc_free_host_resources\n");
  373. if (nlm_traverse_files(host, nlmsvc_same_host, NULL)) {
  374. printk(KERN_WARNING
  375. "lockd: couldn't remove all locks held by %s\n",
  376. host->h_name);
  377. BUG();
  378. }
  379. }
  380. /**
  381. * nlmsvc_invalidate_all - remove all locks held for clients
  382. *
  383. * Release all locks held by NFS clients.
  384. *
  385. */
  386. void
  387. nlmsvc_invalidate_all(void)
  388. {
  389. /*
  390. * Previously, the code would call
  391. * nlmsvc_free_host_resources for each client in
  392. * turn, which is about as inefficient as it gets.
  393. * Now we just do it once in nlm_traverse_files.
  394. */
  395. nlm_traverse_files(NULL, nlmsvc_is_client, NULL);
  396. }
  397. static int
  398. nlmsvc_match_sb(void *datap, struct nlm_file *file)
  399. {
  400. struct super_block *sb = datap;
  401. return sb == nlmsvc_file_inode(file)->i_sb;
  402. }
  403. /**
  404. * nlmsvc_unlock_all_by_sb - release locks held on this file system
  405. * @sb: super block
  406. *
  407. * Release all locks held by clients accessing this file system.
  408. */
  409. int
  410. nlmsvc_unlock_all_by_sb(struct super_block *sb)
  411. {
  412. int ret;
  413. ret = nlm_traverse_files(sb, nlmsvc_always_match, nlmsvc_match_sb);
  414. return ret ? -EIO : 0;
  415. }
  416. EXPORT_SYMBOL_GPL(nlmsvc_unlock_all_by_sb);
  417. static int
  418. nlmsvc_match_ip(void *datap, struct nlm_host *host)
  419. {
  420. return rpc_cmp_addr(nlm_srcaddr(host), datap);
  421. }
  422. /**
  423. * nlmsvc_unlock_all_by_ip - release local locks by IP address
  424. * @server_addr: server's IP address as seen by clients
  425. *
  426. * Release all locks held by clients accessing this host
  427. * via the passed in IP address.
  428. */
  429. int
  430. nlmsvc_unlock_all_by_ip(struct sockaddr *server_addr)
  431. {
  432. int ret;
  433. ret = nlm_traverse_files(server_addr, nlmsvc_match_ip, NULL);
  434. return ret ? -EIO : 0;
  435. }
  436. EXPORT_SYMBOL_GPL(nlmsvc_unlock_all_by_ip);