blocklayoutdev.c 11 KB

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