blocklayoutdev.c 10 KB

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