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