blocklayout.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * linux/fs/nfs/blocklayout/blocklayout.c
  3. *
  4. * Module for the NFSv4.1 pNFS block layout driver.
  5. *
  6. * Copyright (c) 2006 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Andy Adamson <andros@citi.umich.edu>
  10. * Fred Isaman <iisaman@umich.edu>
  11. *
  12. * permission is granted to use, copy, create derivative works and
  13. * redistribute this software and such derivative works for any purpose,
  14. * so long as the name of the university of michigan is not used in
  15. * any advertising or publicity pertaining to the use or distribution
  16. * of this software without specific, written prior authorization. if
  17. * the above copyright notice or any other identification of the
  18. * university of michigan is included in any copy of any portion of
  19. * this software, then the disclaimer below must also be included.
  20. *
  21. * this software is provided as is, without representation from the
  22. * university of michigan as to its fitness for any purpose, and without
  23. * warranty by the university of michigan of any kind, either express
  24. * or implied, including without limitation the implied warranties of
  25. * merchantability and fitness for a particular purpose. the regents
  26. * of the university of michigan shall not be liable for any damages,
  27. * including special, indirect, incidental, or consequential damages,
  28. * with respect to any claim arising out or in connection with the use
  29. * of the software, even if it has been or is hereafter advised of the
  30. * possibility of such damages.
  31. */
  32. #include <linux/module.h>
  33. #include <linux/init.h>
  34. #include <linux/mount.h>
  35. #include <linux/namei.h>
  36. #include "blocklayout.h"
  37. #define NFSDBG_FACILITY NFSDBG_PNFS_LD
  38. MODULE_LICENSE("GPL");
  39. MODULE_AUTHOR("Andy Adamson <andros@citi.umich.edu>");
  40. MODULE_DESCRIPTION("The NFSv4.1 pNFS Block layout driver");
  41. struct dentry *bl_device_pipe;
  42. wait_queue_head_t bl_wq;
  43. static enum pnfs_try_status
  44. bl_read_pagelist(struct nfs_read_data *rdata)
  45. {
  46. return PNFS_NOT_ATTEMPTED;
  47. }
  48. static enum pnfs_try_status
  49. bl_write_pagelist(struct nfs_write_data *wdata,
  50. int sync)
  51. {
  52. return PNFS_NOT_ATTEMPTED;
  53. }
  54. /* FIXME - range ignored */
  55. static void
  56. release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range)
  57. {
  58. int i;
  59. struct pnfs_block_extent *be;
  60. spin_lock(&bl->bl_ext_lock);
  61. for (i = 0; i < EXTENT_LISTS; i++) {
  62. while (!list_empty(&bl->bl_extents[i])) {
  63. be = list_first_entry(&bl->bl_extents[i],
  64. struct pnfs_block_extent,
  65. be_node);
  66. list_del(&be->be_node);
  67. bl_put_extent(be);
  68. }
  69. }
  70. spin_unlock(&bl->bl_ext_lock);
  71. }
  72. static void
  73. release_inval_marks(struct pnfs_inval_markings *marks)
  74. {
  75. struct pnfs_inval_tracking *pos, *temp;
  76. list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) {
  77. list_del(&pos->it_link);
  78. kfree(pos);
  79. }
  80. return;
  81. }
  82. static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
  83. {
  84. struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
  85. dprintk("%s enter\n", __func__);
  86. release_extents(bl, NULL);
  87. release_inval_marks(&bl->bl_inval);
  88. kfree(bl);
  89. }
  90. static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode,
  91. gfp_t gfp_flags)
  92. {
  93. struct pnfs_block_layout *bl;
  94. dprintk("%s enter\n", __func__);
  95. bl = kzalloc(sizeof(*bl), gfp_flags);
  96. if (!bl)
  97. return NULL;
  98. spin_lock_init(&bl->bl_ext_lock);
  99. INIT_LIST_HEAD(&bl->bl_extents[0]);
  100. INIT_LIST_HEAD(&bl->bl_extents[1]);
  101. INIT_LIST_HEAD(&bl->bl_commit);
  102. INIT_LIST_HEAD(&bl->bl_committing);
  103. bl->bl_count = 0;
  104. bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT;
  105. BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize);
  106. return &bl->bl_layout;
  107. }
  108. static void bl_free_lseg(struct pnfs_layout_segment *lseg)
  109. {
  110. dprintk("%s enter\n", __func__);
  111. kfree(lseg);
  112. }
  113. /* We pretty much ignore lseg, and store all data layout wide, so we
  114. * can correctly merge.
  115. */
  116. static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo,
  117. struct nfs4_layoutget_res *lgr,
  118. gfp_t gfp_flags)
  119. {
  120. struct pnfs_layout_segment *lseg;
  121. int status;
  122. dprintk("%s enter\n", __func__);
  123. lseg = kzalloc(sizeof(*lseg), gfp_flags);
  124. if (!lseg)
  125. return ERR_PTR(-ENOMEM);
  126. status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags);
  127. if (status) {
  128. /* We don't want to call the full-blown bl_free_lseg,
  129. * since on error extents were not touched.
  130. */
  131. kfree(lseg);
  132. return ERR_PTR(status);
  133. }
  134. return lseg;
  135. }
  136. static void
  137. bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr,
  138. const struct nfs4_layoutcommit_args *arg)
  139. {
  140. dprintk("%s enter\n", __func__);
  141. encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg);
  142. }
  143. static void
  144. bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
  145. {
  146. struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout;
  147. dprintk("%s enter\n", __func__);
  148. clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status);
  149. }
  150. static void free_blk_mountid(struct block_mount_id *mid)
  151. {
  152. if (mid) {
  153. struct pnfs_block_dev *dev;
  154. spin_lock(&mid->bm_lock);
  155. while (!list_empty(&mid->bm_devlist)) {
  156. dev = list_first_entry(&mid->bm_devlist,
  157. struct pnfs_block_dev,
  158. bm_node);
  159. list_del(&dev->bm_node);
  160. bl_free_block_dev(dev);
  161. }
  162. spin_unlock(&mid->bm_lock);
  163. kfree(mid);
  164. }
  165. }
  166. /* This is mostly copied from the filelayout's get_device_info function.
  167. * It seems much of this should be at the generic pnfs level.
  168. */
  169. static struct pnfs_block_dev *
  170. nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh,
  171. struct nfs4_deviceid *d_id)
  172. {
  173. struct pnfs_device *dev;
  174. struct pnfs_block_dev *rv = NULL;
  175. u32 max_resp_sz;
  176. int max_pages;
  177. struct page **pages = NULL;
  178. int i, rc;
  179. /*
  180. * Use the session max response size as the basis for setting
  181. * GETDEVICEINFO's maxcount
  182. */
  183. max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  184. max_pages = max_resp_sz >> PAGE_SHIFT;
  185. dprintk("%s max_resp_sz %u max_pages %d\n",
  186. __func__, max_resp_sz, max_pages);
  187. dev = kmalloc(sizeof(*dev), GFP_NOFS);
  188. if (!dev) {
  189. dprintk("%s kmalloc failed\n", __func__);
  190. return NULL;
  191. }
  192. pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS);
  193. if (pages == NULL) {
  194. kfree(dev);
  195. return NULL;
  196. }
  197. for (i = 0; i < max_pages; i++) {
  198. pages[i] = alloc_page(GFP_NOFS);
  199. if (!pages[i])
  200. goto out_free;
  201. }
  202. memcpy(&dev->dev_id, d_id, sizeof(*d_id));
  203. dev->layout_type = LAYOUT_BLOCK_VOLUME;
  204. dev->pages = pages;
  205. dev->pgbase = 0;
  206. dev->pglen = PAGE_SIZE * max_pages;
  207. dev->mincount = 0;
  208. dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data);
  209. rc = nfs4_proc_getdeviceinfo(server, dev);
  210. dprintk("%s getdevice info returns %d\n", __func__, rc);
  211. if (rc)
  212. goto out_free;
  213. rv = nfs4_blk_decode_device(server, dev);
  214. out_free:
  215. for (i = 0; i < max_pages; i++)
  216. __free_page(pages[i]);
  217. kfree(pages);
  218. kfree(dev);
  219. return rv;
  220. }
  221. static int
  222. bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
  223. {
  224. struct block_mount_id *b_mt_id = NULL;
  225. struct pnfs_devicelist *dlist = NULL;
  226. struct pnfs_block_dev *bdev;
  227. LIST_HEAD(block_disklist);
  228. int status = 0, i;
  229. dprintk("%s enter\n", __func__);
  230. if (server->pnfs_blksize == 0) {
  231. dprintk("%s Server did not return blksize\n", __func__);
  232. return -EINVAL;
  233. }
  234. b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS);
  235. if (!b_mt_id) {
  236. status = -ENOMEM;
  237. goto out_error;
  238. }
  239. /* Initialize nfs4 block layout mount id */
  240. spin_lock_init(&b_mt_id->bm_lock);
  241. INIT_LIST_HEAD(&b_mt_id->bm_devlist);
  242. dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS);
  243. if (!dlist) {
  244. status = -ENOMEM;
  245. goto out_error;
  246. }
  247. dlist->eof = 0;
  248. while (!dlist->eof) {
  249. status = nfs4_proc_getdevicelist(server, fh, dlist);
  250. if (status)
  251. goto out_error;
  252. dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n",
  253. __func__, dlist->num_devs, dlist->eof);
  254. for (i = 0; i < dlist->num_devs; i++) {
  255. bdev = nfs4_blk_get_deviceinfo(server, fh,
  256. &dlist->dev_id[i]);
  257. if (!bdev) {
  258. status = -ENODEV;
  259. goto out_error;
  260. }
  261. spin_lock(&b_mt_id->bm_lock);
  262. list_add(&bdev->bm_node, &b_mt_id->bm_devlist);
  263. spin_unlock(&b_mt_id->bm_lock);
  264. }
  265. }
  266. dprintk("%s SUCCESS\n", __func__);
  267. server->pnfs_ld_data = b_mt_id;
  268. out_return:
  269. kfree(dlist);
  270. return status;
  271. out_error:
  272. free_blk_mountid(b_mt_id);
  273. goto out_return;
  274. }
  275. static int
  276. bl_clear_layoutdriver(struct nfs_server *server)
  277. {
  278. struct block_mount_id *b_mt_id = server->pnfs_ld_data;
  279. dprintk("%s enter\n", __func__);
  280. free_blk_mountid(b_mt_id);
  281. dprintk("%s RETURNS\n", __func__);
  282. return 0;
  283. }
  284. static const struct nfs_pageio_ops bl_pg_read_ops = {
  285. .pg_init = pnfs_generic_pg_init_read,
  286. .pg_test = pnfs_generic_pg_test,
  287. .pg_doio = pnfs_generic_pg_readpages,
  288. };
  289. static const struct nfs_pageio_ops bl_pg_write_ops = {
  290. .pg_init = pnfs_generic_pg_init_write,
  291. .pg_test = pnfs_generic_pg_test,
  292. .pg_doio = pnfs_generic_pg_writepages,
  293. };
  294. static struct pnfs_layoutdriver_type blocklayout_type = {
  295. .id = LAYOUT_BLOCK_VOLUME,
  296. .name = "LAYOUT_BLOCK_VOLUME",
  297. .read_pagelist = bl_read_pagelist,
  298. .write_pagelist = bl_write_pagelist,
  299. .alloc_layout_hdr = bl_alloc_layout_hdr,
  300. .free_layout_hdr = bl_free_layout_hdr,
  301. .alloc_lseg = bl_alloc_lseg,
  302. .free_lseg = bl_free_lseg,
  303. .encode_layoutcommit = bl_encode_layoutcommit,
  304. .cleanup_layoutcommit = bl_cleanup_layoutcommit,
  305. .set_layoutdriver = bl_set_layoutdriver,
  306. .clear_layoutdriver = bl_clear_layoutdriver,
  307. .pg_read_ops = &bl_pg_read_ops,
  308. .pg_write_ops = &bl_pg_write_ops,
  309. };
  310. static const struct rpc_pipe_ops bl_upcall_ops = {
  311. .upcall = bl_pipe_upcall,
  312. .downcall = bl_pipe_downcall,
  313. .destroy_msg = bl_pipe_destroy_msg,
  314. };
  315. static int __init nfs4blocklayout_init(void)
  316. {
  317. struct vfsmount *mnt;
  318. struct path path;
  319. int ret;
  320. dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
  321. ret = pnfs_register_layoutdriver(&blocklayout_type);
  322. if (ret)
  323. goto out;
  324. init_waitqueue_head(&bl_wq);
  325. mnt = rpc_get_mount();
  326. if (IS_ERR(mnt)) {
  327. ret = PTR_ERR(mnt);
  328. goto out_remove;
  329. }
  330. ret = vfs_path_lookup(mnt->mnt_root,
  331. mnt,
  332. NFS_PIPE_DIRNAME, 0, &path);
  333. if (ret)
  334. goto out_remove;
  335. bl_device_pipe = rpc_mkpipe(path.dentry, "blocklayout", NULL,
  336. &bl_upcall_ops, 0);
  337. if (IS_ERR(bl_device_pipe)) {
  338. ret = PTR_ERR(bl_device_pipe);
  339. goto out_remove;
  340. }
  341. out:
  342. return ret;
  343. out_remove:
  344. pnfs_unregister_layoutdriver(&blocklayout_type);
  345. return ret;
  346. }
  347. static void __exit nfs4blocklayout_exit(void)
  348. {
  349. dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
  350. __func__);
  351. pnfs_unregister_layoutdriver(&blocklayout_type);
  352. rpc_unlink(bl_device_pipe);
  353. }
  354. MODULE_ALIAS("nfs-layouttype4-3");
  355. module_init(nfs4blocklayout_init);
  356. module_exit(nfs4blocklayout_exit);