blocklayoutdev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * linux/fs/nfs/blocklayout/blocklayoutdev.c
  3. *
  4. * Device operations for the pnfs nfs4 file 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/buffer_head.h> /* __bread */
  34. #include <linux/genhd.h>
  35. #include <linux/blkdev.h>
  36. #include <linux/hash.h>
  37. #include "blocklayout.h"
  38. #define NFSDBG_FACILITY NFSDBG_PNFS_LD
  39. static int decode_sector_number(__be32 **rp, sector_t *sp)
  40. {
  41. uint64_t s;
  42. *rp = xdr_decode_hyper(*rp, &s);
  43. if (s & 0x1ff) {
  44. printk(KERN_WARNING "%s: sector not aligned\n", __func__);
  45. return -1;
  46. }
  47. *sp = s >> SECTOR_SHIFT;
  48. return 0;
  49. }
  50. /* Open a block_device by device number. */
  51. struct block_device *nfs4_blkdev_get(dev_t dev)
  52. {
  53. struct block_device *bd;
  54. dprintk("%s enter\n", __func__);
  55. bd = blkdev_get_by_dev(dev, FMODE_READ, NULL);
  56. if (IS_ERR(bd))
  57. goto fail;
  58. return bd;
  59. fail:
  60. dprintk("%s failed to open device : %ld\n",
  61. __func__, PTR_ERR(bd));
  62. return NULL;
  63. }
  64. /*
  65. * Release the block device
  66. */
  67. int nfs4_blkdev_put(struct block_device *bdev)
  68. {
  69. dprintk("%s for device %d:%d\n", __func__, MAJOR(bdev->bd_dev),
  70. MINOR(bdev->bd_dev));
  71. return blkdev_put(bdev, FMODE_READ);
  72. }
  73. static struct bl_dev_msg bl_mount_reply;
  74. ssize_t bl_pipe_downcall(struct file *filp, const char __user *src,
  75. size_t mlen)
  76. {
  77. if (mlen != sizeof (struct bl_dev_msg))
  78. return -EINVAL;
  79. if (copy_from_user(&bl_mount_reply, src, mlen) != 0)
  80. return -EFAULT;
  81. wake_up(&bl_wq);
  82. return mlen;
  83. }
  84. void bl_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  85. {
  86. if (msg->errno >= 0)
  87. return;
  88. wake_up(&bl_wq);
  89. }
  90. /*
  91. * Decodes pnfs_block_deviceaddr4 which is XDR encoded in dev->dev_addr_buf.
  92. */
  93. struct pnfs_block_dev *
  94. nfs4_blk_decode_device(struct nfs_server *server,
  95. struct pnfs_device *dev)
  96. {
  97. struct pnfs_block_dev *rv;
  98. struct block_device *bd = NULL;
  99. struct rpc_pipe_msg msg;
  100. struct bl_msg_hdr bl_msg = {
  101. .type = BL_DEVICE_MOUNT,
  102. .totallen = dev->mincount,
  103. };
  104. uint8_t *dataptr;
  105. DECLARE_WAITQUEUE(wq, current);
  106. struct bl_dev_msg *reply = &bl_mount_reply;
  107. int offset, len, i, rc;
  108. dprintk("%s CREATING PIPEFS MESSAGE\n", __func__);
  109. dprintk("%s: deviceid: %s, mincount: %d\n", __func__, dev->dev_id.data,
  110. dev->mincount);
  111. memset(&msg, 0, sizeof(msg));
  112. msg.data = kzalloc(sizeof(bl_msg) + dev->mincount, GFP_NOFS);
  113. if (!msg.data) {
  114. rv = ERR_PTR(-ENOMEM);
  115. goto out;
  116. }
  117. memcpy(msg.data, &bl_msg, sizeof(bl_msg));
  118. dataptr = (uint8_t *) msg.data;
  119. len = dev->mincount;
  120. offset = sizeof(bl_msg);
  121. for (i = 0; len > 0; i++) {
  122. memcpy(&dataptr[offset], page_address(dev->pages[i]),
  123. len < PAGE_CACHE_SIZE ? len : PAGE_CACHE_SIZE);
  124. len -= PAGE_CACHE_SIZE;
  125. offset += PAGE_CACHE_SIZE;
  126. }
  127. msg.len = sizeof(bl_msg) + dev->mincount;
  128. dprintk("%s CALLING USERSPACE DAEMON\n", __func__);
  129. add_wait_queue(&bl_wq, &wq);
  130. rc = rpc_queue_upcall(bl_device_pipe->d_inode, &msg);
  131. if (rc < 0) {
  132. remove_wait_queue(&bl_wq, &wq);
  133. rv = ERR_PTR(rc);
  134. goto out;
  135. }
  136. set_current_state(TASK_UNINTERRUPTIBLE);
  137. schedule();
  138. __set_current_state(TASK_RUNNING);
  139. remove_wait_queue(&bl_wq, &wq);
  140. if (reply->status != BL_DEVICE_REQUEST_PROC) {
  141. dprintk("%s failed to open device: %d\n",
  142. __func__, reply->status);
  143. rv = ERR_PTR(-EINVAL);
  144. goto out;
  145. }
  146. bd = nfs4_blkdev_get(MKDEV(reply->major, reply->minor));
  147. if (IS_ERR(bd)) {
  148. rc = PTR_ERR(bd);
  149. dprintk("%s failed to open device : %d\n", __func__, rc);
  150. rv = ERR_PTR(rc);
  151. goto out;
  152. }
  153. rv = kzalloc(sizeof(*rv), GFP_NOFS);
  154. if (!rv) {
  155. rv = ERR_PTR(-ENOMEM);
  156. goto out;
  157. }
  158. rv->bm_mdev = bd;
  159. memcpy(&rv->bm_mdevid, &dev->dev_id, sizeof(struct nfs4_deviceid));
  160. dprintk("%s Created device %s with bd_block_size %u\n",
  161. __func__,
  162. bd->bd_disk->disk_name,
  163. bd->bd_block_size);
  164. out:
  165. kfree(msg.data);
  166. return rv;
  167. }
  168. /* Map deviceid returned by the server to constructed block_device */
  169. static struct block_device *translate_devid(struct pnfs_layout_hdr *lo,
  170. struct nfs4_deviceid *id)
  171. {
  172. struct block_device *rv = NULL;
  173. struct block_mount_id *mid;
  174. struct pnfs_block_dev *dev;
  175. dprintk("%s enter, lo=%p, id=%p\n", __func__, lo, id);
  176. mid = BLK_ID(lo);
  177. spin_lock(&mid->bm_lock);
  178. list_for_each_entry(dev, &mid->bm_devlist, bm_node) {
  179. if (memcmp(id->data, dev->bm_mdevid.data,
  180. NFS4_DEVICEID4_SIZE) == 0) {
  181. rv = dev->bm_mdev;
  182. goto out;
  183. }
  184. }
  185. out:
  186. spin_unlock(&mid->bm_lock);
  187. dprintk("%s returning %p\n", __func__, rv);
  188. return rv;
  189. }
  190. /* Tracks info needed to ensure extents in layout obey constraints of spec */
  191. struct layout_verification {
  192. u32 mode; /* R or RW */
  193. u64 start; /* Expected start of next non-COW extent */
  194. u64 inval; /* Start of INVAL coverage */
  195. u64 cowread; /* End of COW read coverage */
  196. };
  197. /* Verify the extent meets the layout requirements of the pnfs-block draft,
  198. * section 2.3.1.
  199. */
  200. static int verify_extent(struct pnfs_block_extent *be,
  201. struct layout_verification *lv)
  202. {
  203. if (lv->mode == IOMODE_READ) {
  204. if (be->be_state == PNFS_BLOCK_READWRITE_DATA ||
  205. be->be_state == PNFS_BLOCK_INVALID_DATA)
  206. return -EIO;
  207. if (be->be_f_offset != lv->start)
  208. return -EIO;
  209. lv->start += be->be_length;
  210. return 0;
  211. }
  212. /* lv->mode == IOMODE_RW */
  213. if (be->be_state == PNFS_BLOCK_READWRITE_DATA) {
  214. if (be->be_f_offset != lv->start)
  215. return -EIO;
  216. if (lv->cowread > lv->start)
  217. return -EIO;
  218. lv->start += be->be_length;
  219. lv->inval = lv->start;
  220. return 0;
  221. } else if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
  222. if (be->be_f_offset != lv->start)
  223. return -EIO;
  224. lv->start += be->be_length;
  225. return 0;
  226. } else if (be->be_state == PNFS_BLOCK_READ_DATA) {
  227. if (be->be_f_offset > lv->start)
  228. return -EIO;
  229. if (be->be_f_offset < lv->inval)
  230. return -EIO;
  231. if (be->be_f_offset < lv->cowread)
  232. return -EIO;
  233. /* It looks like you might want to min this with lv->start,
  234. * but you really don't.
  235. */
  236. lv->inval = lv->inval + be->be_length;
  237. lv->cowread = be->be_f_offset + be->be_length;
  238. return 0;
  239. } else
  240. return -EIO;
  241. }
  242. /* XDR decode pnfs_block_layout4 structure */
  243. int
  244. nfs4_blk_process_layoutget(struct pnfs_layout_hdr *lo,
  245. struct nfs4_layoutget_res *lgr, gfp_t gfp_flags)
  246. {
  247. struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
  248. int i, status = -EIO;
  249. uint32_t count;
  250. struct pnfs_block_extent *be = NULL, *save;
  251. struct xdr_stream stream;
  252. struct xdr_buf buf;
  253. struct page *scratch;
  254. __be32 *p;
  255. struct layout_verification lv = {
  256. .mode = lgr->range.iomode,
  257. .start = lgr->range.offset >> SECTOR_SHIFT,
  258. .inval = lgr->range.offset >> SECTOR_SHIFT,
  259. .cowread = lgr->range.offset >> SECTOR_SHIFT,
  260. };
  261. LIST_HEAD(extents);
  262. dprintk("---> %s\n", __func__);
  263. scratch = alloc_page(gfp_flags);
  264. if (!scratch)
  265. return -ENOMEM;
  266. xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
  267. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  268. p = xdr_inline_decode(&stream, 4);
  269. if (unlikely(!p))
  270. goto out_err;
  271. count = be32_to_cpup(p++);
  272. dprintk("%s enter, number of extents %i\n", __func__, count);
  273. p = xdr_inline_decode(&stream, (28 + NFS4_DEVICEID4_SIZE) * count);
  274. if (unlikely(!p))
  275. goto out_err;
  276. /* Decode individual extents, putting them in temporary
  277. * staging area until whole layout is decoded to make error
  278. * recovery easier.
  279. */
  280. for (i = 0; i < count; i++) {
  281. be = bl_alloc_extent();
  282. if (!be) {
  283. status = -ENOMEM;
  284. goto out_err;
  285. }
  286. memcpy(&be->be_devid, p, NFS4_DEVICEID4_SIZE);
  287. p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
  288. be->be_mdev = translate_devid(lo, &be->be_devid);
  289. if (!be->be_mdev)
  290. goto out_err;
  291. /* The next three values are read in as bytes,
  292. * but stored as 512-byte sector lengths
  293. */
  294. if (decode_sector_number(&p, &be->be_f_offset) < 0)
  295. goto out_err;
  296. if (decode_sector_number(&p, &be->be_length) < 0)
  297. goto out_err;
  298. if (decode_sector_number(&p, &be->be_v_offset) < 0)
  299. goto out_err;
  300. be->be_state = be32_to_cpup(p++);
  301. if (be->be_state == PNFS_BLOCK_INVALID_DATA)
  302. be->be_inval = &bl->bl_inval;
  303. if (verify_extent(be, &lv)) {
  304. dprintk("%s verify failed\n", __func__);
  305. goto out_err;
  306. }
  307. list_add_tail(&be->be_node, &extents);
  308. }
  309. if (lgr->range.offset + lgr->range.length !=
  310. lv.start << SECTOR_SHIFT) {
  311. dprintk("%s Final length mismatch\n", __func__);
  312. be = NULL;
  313. goto out_err;
  314. }
  315. if (lv.start < lv.cowread) {
  316. dprintk("%s Final uncovered COW extent\n", __func__);
  317. be = NULL;
  318. goto out_err;
  319. }
  320. /* Extents decoded properly, now try to merge them in to
  321. * existing layout extents.
  322. */
  323. spin_lock(&bl->bl_ext_lock);
  324. list_for_each_entry_safe(be, save, &extents, be_node) {
  325. list_del(&be->be_node);
  326. status = bl_add_merge_extent(bl, be);
  327. if (status) {
  328. spin_unlock(&bl->bl_ext_lock);
  329. /* This is a fairly catastrophic error, as the
  330. * entire layout extent lists are now corrupted.
  331. * We should have some way to distinguish this.
  332. */
  333. be = NULL;
  334. goto out_err;
  335. }
  336. }
  337. spin_unlock(&bl->bl_ext_lock);
  338. status = 0;
  339. out:
  340. __free_page(scratch);
  341. dprintk("%s returns %i\n", __func__, status);
  342. return status;
  343. out_err:
  344. bl_put_extent(be);
  345. while (!list_empty(&extents)) {
  346. be = list_first_entry(&extents, struct pnfs_block_extent,
  347. be_node);
  348. list_del(&be->be_node);
  349. bl_put_extent(be);
  350. }
  351. goto out;
  352. }