pnfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * pNFS functions to call and manage layout drivers.
  3. *
  4. * Copyright (c) 2002 [year of first publication]
  5. * The Regents of the University of Michigan
  6. * All Rights Reserved
  7. *
  8. * Dean Hildebrand <dhildebz@umich.edu>
  9. *
  10. * Permission is granted to use, copy, create derivative works, and
  11. * redistribute this software and such derivative works for any purpose,
  12. * so long as the name of the University of Michigan is not used in
  13. * any advertising or publicity pertaining to the use or distribution
  14. * of this software without specific, written prior authorization. If
  15. * the above copyright notice or any other identification of the
  16. * University of Michigan is included in any copy of any portion of
  17. * this software, then the disclaimer below must also be included.
  18. *
  19. * This software is provided as is, without representation or warranty
  20. * of any kind either express or implied, including without limitation
  21. * the implied warranties of merchantability, fitness for a particular
  22. * purpose, or noninfringement. The Regents of the University of
  23. * Michigan shall not be liable for any damages, including special,
  24. * indirect, incidental, or consequential damages, with respect to any
  25. * claim arising out of or in connection with the use of the software,
  26. * even if it has been or is hereafter advised of the possibility of
  27. * such damages.
  28. */
  29. #include <linux/nfs_fs.h>
  30. #include "internal.h"
  31. #include "pnfs.h"
  32. #define NFSDBG_FACILITY NFSDBG_PNFS
  33. /* Locking:
  34. *
  35. * pnfs_spinlock:
  36. * protects pnfs_modules_tbl.
  37. */
  38. static DEFINE_SPINLOCK(pnfs_spinlock);
  39. /*
  40. * pnfs_modules_tbl holds all pnfs modules
  41. */
  42. static LIST_HEAD(pnfs_modules_tbl);
  43. /* Return the registered pnfs layout driver module matching given id */
  44. static struct pnfs_layoutdriver_type *
  45. find_pnfs_driver_locked(u32 id)
  46. {
  47. struct pnfs_layoutdriver_type *local;
  48. list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
  49. if (local->id == id)
  50. goto out;
  51. local = NULL;
  52. out:
  53. dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
  54. return local;
  55. }
  56. static struct pnfs_layoutdriver_type *
  57. find_pnfs_driver(u32 id)
  58. {
  59. struct pnfs_layoutdriver_type *local;
  60. spin_lock(&pnfs_spinlock);
  61. local = find_pnfs_driver_locked(id);
  62. spin_unlock(&pnfs_spinlock);
  63. return local;
  64. }
  65. void
  66. unset_pnfs_layoutdriver(struct nfs_server *nfss)
  67. {
  68. if (nfss->pnfs_curr_ld) {
  69. nfss->pnfs_curr_ld->uninitialize_mountpoint(nfss);
  70. module_put(nfss->pnfs_curr_ld->owner);
  71. }
  72. nfss->pnfs_curr_ld = NULL;
  73. }
  74. /*
  75. * Try to set the server's pnfs module to the pnfs layout type specified by id.
  76. * Currently only one pNFS layout driver per filesystem is supported.
  77. *
  78. * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
  79. */
  80. void
  81. set_pnfs_layoutdriver(struct nfs_server *server, u32 id)
  82. {
  83. struct pnfs_layoutdriver_type *ld_type = NULL;
  84. if (id == 0)
  85. goto out_no_driver;
  86. if (!(server->nfs_client->cl_exchange_flags &
  87. (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
  88. printk(KERN_ERR "%s: id %u cl_exchange_flags 0x%x\n", __func__,
  89. id, server->nfs_client->cl_exchange_flags);
  90. goto out_no_driver;
  91. }
  92. ld_type = find_pnfs_driver(id);
  93. if (!ld_type) {
  94. request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
  95. ld_type = find_pnfs_driver(id);
  96. if (!ld_type) {
  97. dprintk("%s: No pNFS module found for %u.\n",
  98. __func__, id);
  99. goto out_no_driver;
  100. }
  101. }
  102. if (!try_module_get(ld_type->owner)) {
  103. dprintk("%s: Could not grab reference on module\n", __func__);
  104. goto out_no_driver;
  105. }
  106. server->pnfs_curr_ld = ld_type;
  107. if (ld_type->initialize_mountpoint(server)) {
  108. printk(KERN_ERR
  109. "%s: Error initializing mount point for layout driver %u.\n",
  110. __func__, id);
  111. module_put(ld_type->owner);
  112. goto out_no_driver;
  113. }
  114. dprintk("%s: pNFS module for %u set\n", __func__, id);
  115. return;
  116. out_no_driver:
  117. dprintk("%s: Using NFSv4 I/O\n", __func__);
  118. server->pnfs_curr_ld = NULL;
  119. }
  120. int
  121. pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
  122. {
  123. int status = -EINVAL;
  124. struct pnfs_layoutdriver_type *tmp;
  125. if (ld_type->id == 0) {
  126. printk(KERN_ERR "%s id 0 is reserved\n", __func__);
  127. return status;
  128. }
  129. spin_lock(&pnfs_spinlock);
  130. tmp = find_pnfs_driver_locked(ld_type->id);
  131. if (!tmp) {
  132. list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
  133. status = 0;
  134. dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
  135. ld_type->name);
  136. } else {
  137. printk(KERN_ERR "%s Module with id %d already loaded!\n",
  138. __func__, ld_type->id);
  139. }
  140. spin_unlock(&pnfs_spinlock);
  141. return status;
  142. }
  143. EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
  144. void
  145. pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
  146. {
  147. dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
  148. spin_lock(&pnfs_spinlock);
  149. list_del(&ld_type->pnfs_tblid);
  150. spin_unlock(&pnfs_spinlock);
  151. }
  152. EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
  153. static void
  154. get_layout_hdr_locked(struct pnfs_layout_hdr *lo)
  155. {
  156. assert_spin_locked(&lo->inode->i_lock);
  157. lo->refcount++;
  158. }
  159. static void
  160. put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
  161. {
  162. assert_spin_locked(&lo->inode->i_lock);
  163. BUG_ON(lo->refcount == 0);
  164. lo->refcount--;
  165. if (!lo->refcount) {
  166. dprintk("%s: freeing layout cache %p\n", __func__, lo);
  167. BUG_ON(!list_empty(&lo->layouts));
  168. NFS_I(lo->inode)->layout = NULL;
  169. kfree(lo);
  170. }
  171. }
  172. static void
  173. put_layout_hdr(struct inode *inode)
  174. {
  175. spin_lock(&inode->i_lock);
  176. put_layout_hdr_locked(NFS_I(inode)->layout);
  177. spin_unlock(&inode->i_lock);
  178. }
  179. static void
  180. init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
  181. {
  182. INIT_LIST_HEAD(&lseg->fi_list);
  183. kref_init(&lseg->kref);
  184. lseg->layout = lo;
  185. }
  186. /* Called without i_lock held, as the free_lseg call may sleep */
  187. static void
  188. destroy_lseg(struct kref *kref)
  189. {
  190. struct pnfs_layout_segment *lseg =
  191. container_of(kref, struct pnfs_layout_segment, kref);
  192. struct inode *ino = lseg->layout->inode;
  193. dprintk("--> %s\n", __func__);
  194. kfree(lseg);
  195. /* Matched by get_layout_hdr_locked in pnfs_insert_layout */
  196. put_layout_hdr(ino);
  197. }
  198. static void
  199. put_lseg(struct pnfs_layout_segment *lseg)
  200. {
  201. if (!lseg)
  202. return;
  203. dprintk("%s: lseg %p ref %d\n", __func__, lseg,
  204. atomic_read(&lseg->kref.refcount));
  205. kref_put(&lseg->kref, destroy_lseg);
  206. }
  207. static void
  208. pnfs_clear_lseg_list(struct pnfs_layout_hdr *lo, struct list_head *tmp_list)
  209. {
  210. struct pnfs_layout_segment *lseg, *next;
  211. struct nfs_client *clp;
  212. dprintk("%s:Begin lo %p\n", __func__, lo);
  213. assert_spin_locked(&lo->inode->i_lock);
  214. list_for_each_entry_safe(lseg, next, &lo->segs, fi_list) {
  215. dprintk("%s: freeing lseg %p\n", __func__, lseg);
  216. list_move(&lseg->fi_list, tmp_list);
  217. }
  218. clp = NFS_SERVER(lo->inode)->nfs_client;
  219. spin_lock(&clp->cl_lock);
  220. /* List does not take a reference, so no need for put here */
  221. list_del_init(&lo->layouts);
  222. spin_unlock(&clp->cl_lock);
  223. dprintk("%s:Return\n", __func__);
  224. }
  225. static void
  226. pnfs_free_lseg_list(struct list_head *tmp_list)
  227. {
  228. struct pnfs_layout_segment *lseg;
  229. while (!list_empty(tmp_list)) {
  230. lseg = list_entry(tmp_list->next, struct pnfs_layout_segment,
  231. fi_list);
  232. dprintk("%s calling put_lseg on %p\n", __func__, lseg);
  233. list_del(&lseg->fi_list);
  234. put_lseg(lseg);
  235. }
  236. }
  237. void
  238. pnfs_destroy_layout(struct nfs_inode *nfsi)
  239. {
  240. struct pnfs_layout_hdr *lo;
  241. LIST_HEAD(tmp_list);
  242. spin_lock(&nfsi->vfs_inode.i_lock);
  243. lo = nfsi->layout;
  244. if (lo) {
  245. pnfs_clear_lseg_list(lo, &tmp_list);
  246. /* Matched by refcount set to 1 in alloc_init_layout_hdr */
  247. put_layout_hdr_locked(lo);
  248. }
  249. spin_unlock(&nfsi->vfs_inode.i_lock);
  250. pnfs_free_lseg_list(&tmp_list);
  251. }
  252. /*
  253. * Called by the state manger to remove all layouts established under an
  254. * expired lease.
  255. */
  256. void
  257. pnfs_destroy_all_layouts(struct nfs_client *clp)
  258. {
  259. struct pnfs_layout_hdr *lo;
  260. LIST_HEAD(tmp_list);
  261. spin_lock(&clp->cl_lock);
  262. list_splice_init(&clp->cl_layouts, &tmp_list);
  263. spin_unlock(&clp->cl_lock);
  264. while (!list_empty(&tmp_list)) {
  265. lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
  266. layouts);
  267. dprintk("%s freeing layout for inode %lu\n", __func__,
  268. lo->inode->i_ino);
  269. pnfs_destroy_layout(NFS_I(lo->inode));
  270. }
  271. }
  272. static void pnfs_insert_layout(struct pnfs_layout_hdr *lo,
  273. struct pnfs_layout_segment *lseg);
  274. /* Get layout from server. */
  275. static struct pnfs_layout_segment *
  276. send_layoutget(struct pnfs_layout_hdr *lo,
  277. struct nfs_open_context *ctx,
  278. u32 iomode)
  279. {
  280. struct inode *ino = lo->inode;
  281. struct pnfs_layout_segment *lseg;
  282. /* Lets pretend we sent LAYOUTGET and got a response */
  283. lseg = kzalloc(sizeof(*lseg), GFP_KERNEL);
  284. if (!lseg) {
  285. set_bit(lo_fail_bit(iomode), &lo->state);
  286. spin_lock(&ino->i_lock);
  287. put_layout_hdr_locked(lo);
  288. spin_unlock(&ino->i_lock);
  289. return NULL;
  290. }
  291. init_lseg(lo, lseg);
  292. lseg->iomode = IOMODE_RW;
  293. spin_lock(&ino->i_lock);
  294. pnfs_insert_layout(lo, lseg);
  295. put_layout_hdr_locked(lo);
  296. spin_unlock(&ino->i_lock);
  297. return lseg;
  298. }
  299. static void
  300. pnfs_insert_layout(struct pnfs_layout_hdr *lo,
  301. struct pnfs_layout_segment *lseg)
  302. {
  303. dprintk("%s:Begin\n", __func__);
  304. assert_spin_locked(&lo->inode->i_lock);
  305. if (list_empty(&lo->segs)) {
  306. struct nfs_client *clp = NFS_SERVER(lo->inode)->nfs_client;
  307. spin_lock(&clp->cl_lock);
  308. BUG_ON(!list_empty(&lo->layouts));
  309. list_add_tail(&lo->layouts, &clp->cl_layouts);
  310. spin_unlock(&clp->cl_lock);
  311. }
  312. get_layout_hdr_locked(lo);
  313. /* STUB - add the constructed lseg if necessary */
  314. if (list_empty(&lo->segs)) {
  315. list_add_tail(&lseg->fi_list, &lo->segs);
  316. dprintk("%s: inserted lseg %p iomode %d at tail\n",
  317. __func__, lseg, lseg->iomode);
  318. } else {
  319. /* There is no harm for the moment in calling this
  320. * with the lock held, and the call will be removed
  321. * with the STUB.
  322. */
  323. put_lseg(lseg);
  324. }
  325. dprintk("%s:Return\n", __func__);
  326. }
  327. static struct pnfs_layout_hdr *
  328. alloc_init_layout_hdr(struct inode *ino)
  329. {
  330. struct pnfs_layout_hdr *lo;
  331. lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL);
  332. if (!lo)
  333. return NULL;
  334. lo->refcount = 1;
  335. INIT_LIST_HEAD(&lo->layouts);
  336. INIT_LIST_HEAD(&lo->segs);
  337. lo->inode = ino;
  338. return lo;
  339. }
  340. static struct pnfs_layout_hdr *
  341. pnfs_find_alloc_layout(struct inode *ino)
  342. {
  343. struct nfs_inode *nfsi = NFS_I(ino);
  344. struct pnfs_layout_hdr *new = NULL;
  345. dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
  346. assert_spin_locked(&ino->i_lock);
  347. if (nfsi->layout)
  348. return nfsi->layout;
  349. spin_unlock(&ino->i_lock);
  350. new = alloc_init_layout_hdr(ino);
  351. spin_lock(&ino->i_lock);
  352. if (likely(nfsi->layout == NULL)) /* Won the race? */
  353. nfsi->layout = new;
  354. else
  355. kfree(new);
  356. return nfsi->layout;
  357. }
  358. /* STUB - LAYOUTGET never succeeds, so cache is empty */
  359. static struct pnfs_layout_segment *
  360. pnfs_has_layout(struct pnfs_layout_hdr *lo, u32 iomode)
  361. {
  362. return NULL;
  363. }
  364. /*
  365. * Layout segment is retreived from the server if not cached.
  366. * The appropriate layout segment is referenced and returned to the caller.
  367. */
  368. struct pnfs_layout_segment *
  369. pnfs_update_layout(struct inode *ino,
  370. struct nfs_open_context *ctx,
  371. enum pnfs_iomode iomode)
  372. {
  373. struct nfs_inode *nfsi = NFS_I(ino);
  374. struct pnfs_layout_hdr *lo;
  375. struct pnfs_layout_segment *lseg = NULL;
  376. if (!pnfs_enabled_sb(NFS_SERVER(ino)))
  377. return NULL;
  378. spin_lock(&ino->i_lock);
  379. lo = pnfs_find_alloc_layout(ino);
  380. if (lo == NULL) {
  381. dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
  382. goto out_unlock;
  383. }
  384. /* Check to see if the layout for the given range already exists */
  385. lseg = pnfs_has_layout(lo, iomode);
  386. if (lseg) {
  387. dprintk("%s: Using cached lseg %p for iomode %d)\n",
  388. __func__, lseg, iomode);
  389. goto out_unlock;
  390. }
  391. /* if LAYOUTGET already failed once we don't try again */
  392. if (test_bit(lo_fail_bit(iomode), &nfsi->layout->state))
  393. goto out_unlock;
  394. get_layout_hdr_locked(lo);
  395. spin_unlock(&ino->i_lock);
  396. lseg = send_layoutget(lo, ctx, iomode);
  397. out:
  398. dprintk("%s end, state 0x%lx lseg %p\n", __func__,
  399. nfsi->layout->state, lseg);
  400. return lseg;
  401. out_unlock:
  402. spin_unlock(&ino->i_lock);
  403. goto out;
  404. }