blob: 1d58642b15300adeef6c0c0fb24c12ae0e3fd5a7 [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) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -050049 printk(KERN_WARNING "NFS: %s: sector not aligned\n", __func__);
Fred Isamane9437cc2011-07-30 20:52:47 -040050 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
Jim Reesfe0a9b72011-07-30 20:52:42 -040082ssize_t bl_pipe_downcall(struct file *filp, const char __user *src,
83 size_t mlen)
84{
Stanislav Kinsburskycb9c1c42012-03-11 18:20:23 +040085 struct nfs_net *nn = net_generic(filp->f_dentry->d_sb->s_fs_info,
86 nfs_net_id);
87
Jim Reesfe0a9b72011-07-30 20:52:42 -040088 if (mlen != sizeof (struct bl_dev_msg))
89 return -EINVAL;
90
Stanislav Kinsburskycb9c1c42012-03-11 18:20:23 +040091 if (copy_from_user(&nn->bl_mount_reply, src, mlen) != 0)
Jim Reesfe0a9b72011-07-30 20:52:42 -040092 return -EFAULT;
93
94 wake_up(&bl_wq);
95
96 return mlen;
97}
98
99void bl_pipe_destroy_msg(struct rpc_pipe_msg *msg)
100{
101 if (msg->errno >= 0)
102 return;
103 wake_up(&bl_wq);
104}
105
106/*
107 * Decodes pnfs_block_deviceaddr4 which is XDR encoded in dev->dev_addr_buf.
108 */
109struct pnfs_block_dev *
110nfs4_blk_decode_device(struct nfs_server *server,
Fred Isaman2f9fd182011-07-30 20:52:46 -0400111 struct pnfs_device *dev)
Jim Reesfe0a9b72011-07-30 20:52:42 -0400112{
Jim Rees516f2e22011-09-22 21:50:08 -0400113 struct pnfs_block_dev *rv;
Jim Reesfe0a9b72011-07-30 20:52:42 -0400114 struct block_device *bd = NULL;
115 struct rpc_pipe_msg msg;
116 struct bl_msg_hdr bl_msg = {
117 .type = BL_DEVICE_MOUNT,
118 .totallen = dev->mincount,
119 };
120 uint8_t *dataptr;
121 DECLARE_WAITQUEUE(wq, current);
Jim Rees516f2e22011-09-22 21:50:08 -0400122 int offset, len, i, rc;
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +0400123 struct net *net = server->nfs_client->net;
124 struct nfs_net *nn = net_generic(net, nfs_net_id);
Stanislav Kinsburskycb9c1c42012-03-11 18:20:23 +0400125 struct bl_dev_msg *reply = &nn->bl_mount_reply;
Jim Reesfe0a9b72011-07-30 20:52:42 -0400126
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
131 memset(&msg, 0, sizeof(msg));
132 msg.data = kzalloc(sizeof(bl_msg) + dev->mincount, GFP_NOFS);
133 if (!msg.data) {
134 rv = ERR_PTR(-ENOMEM);
135 goto out;
136 }
137
138 memcpy(msg.data, &bl_msg, sizeof(bl_msg));
139 dataptr = (uint8_t *) msg.data;
Fred Isaman2f9fd182011-07-30 20:52:46 -0400140 len = dev->mincount;
141 offset = sizeof(bl_msg);
142 for (i = 0; len > 0; i++) {
143 memcpy(&dataptr[offset], page_address(dev->pages[i]),
144 len < PAGE_CACHE_SIZE ? len : PAGE_CACHE_SIZE);
145 len -= PAGE_CACHE_SIZE;
146 offset += PAGE_CACHE_SIZE;
147 }
Jim Reesfe0a9b72011-07-30 20:52:42 -0400148 msg.len = sizeof(bl_msg) + dev->mincount;
149
150 dprintk("%s CALLING USERSPACE DAEMON\n", __func__);
151 add_wait_queue(&bl_wq, &wq);
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +0400152 rc = rpc_queue_upcall(nn->bl_device_pipe, &msg);
Jim Rees516f2e22011-09-22 21:50:08 -0400153 if (rc < 0) {
Jim Reesfe0a9b72011-07-30 20:52:42 -0400154 remove_wait_queue(&bl_wq, &wq);
Jim Rees516f2e22011-09-22 21:50:08 -0400155 rv = ERR_PTR(rc);
Jim Reesfe0a9b72011-07-30 20:52:42 -0400156 goto out;
157 }
158
159 set_current_state(TASK_UNINTERRUPTIBLE);
160 schedule();
161 __set_current_state(TASK_RUNNING);
162 remove_wait_queue(&bl_wq, &wq);
163
164 if (reply->status != BL_DEVICE_REQUEST_PROC) {
165 dprintk("%s failed to open device: %d\n",
166 __func__, reply->status);
167 rv = ERR_PTR(-EINVAL);
168 goto out;
169 }
170
171 bd = nfs4_blkdev_get(MKDEV(reply->major, reply->minor));
172 if (IS_ERR(bd)) {
Jim Rees516f2e22011-09-22 21:50:08 -0400173 rc = PTR_ERR(bd);
174 dprintk("%s failed to open device : %d\n", __func__, rc);
175 rv = ERR_PTR(rc);
Jim Reesfe0a9b72011-07-30 20:52:42 -0400176 goto out;
177 }
178
179 rv = kzalloc(sizeof(*rv), GFP_NOFS);
180 if (!rv) {
181 rv = ERR_PTR(-ENOMEM);
182 goto out;
183 }
184
185 rv->bm_mdev = bd;
186 memcpy(&rv->bm_mdevid, &dev->dev_id, sizeof(struct nfs4_deviceid));
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +0400187 rv->net = net;
Jim Reesfe0a9b72011-07-30 20:52:42 -0400188 dprintk("%s Created device %s with bd_block_size %u\n",
189 __func__,
190 bd->bd_disk->disk_name,
191 bd->bd_block_size);
192
193out:
194 kfree(msg.data);
195 return rv;
196}
Fred Isamana60d2eb2011-07-30 20:52:44 -0400197
Fred Isamane9437cc2011-07-30 20:52:47 -0400198/* Map deviceid returned by the server to constructed block_device */
199static struct block_device *translate_devid(struct pnfs_layout_hdr *lo,
200 struct nfs4_deviceid *id)
201{
202 struct block_device *rv = NULL;
203 struct block_mount_id *mid;
204 struct pnfs_block_dev *dev;
205
206 dprintk("%s enter, lo=%p, id=%p\n", __func__, lo, id);
207 mid = BLK_ID(lo);
208 spin_lock(&mid->bm_lock);
209 list_for_each_entry(dev, &mid->bm_devlist, bm_node) {
210 if (memcmp(id->data, dev->bm_mdevid.data,
211 NFS4_DEVICEID4_SIZE) == 0) {
212 rv = dev->bm_mdev;
213 goto out;
214 }
215 }
216 out:
217 spin_unlock(&mid->bm_lock);
218 dprintk("%s returning %p\n", __func__, rv);
219 return rv;
220}
221
222/* Tracks info needed to ensure extents in layout obey constraints of spec */
223struct layout_verification {
224 u32 mode; /* R or RW */
225 u64 start; /* Expected start of next non-COW extent */
226 u64 inval; /* Start of INVAL coverage */
227 u64 cowread; /* End of COW read coverage */
228};
229
230/* Verify the extent meets the layout requirements of the pnfs-block draft,
231 * section 2.3.1.
232 */
233static int verify_extent(struct pnfs_block_extent *be,
234 struct layout_verification *lv)
235{
236 if (lv->mode == IOMODE_READ) {
237 if (be->be_state == PNFS_BLOCK_READWRITE_DATA ||
238 be->be_state == PNFS_BLOCK_INVALID_DATA)
239 return -EIO;
240 if (be->be_f_offset != lv->start)
241 return -EIO;
242 lv->start += be->be_length;
243 return 0;
244 }
245 /* lv->mode == IOMODE_RW */
246 if (be->be_state == PNFS_BLOCK_READWRITE_DATA) {
247 if (be->be_f_offset != lv->start)
248 return -EIO;
249 if (lv->cowread > lv->start)
250 return -EIO;
251 lv->start += be->be_length;
252 lv->inval = lv->start;
253 return 0;
254 } else if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
255 if (be->be_f_offset != lv->start)
256 return -EIO;
257 lv->start += be->be_length;
258 return 0;
259 } else if (be->be_state == PNFS_BLOCK_READ_DATA) {
260 if (be->be_f_offset > lv->start)
261 return -EIO;
262 if (be->be_f_offset < lv->inval)
263 return -EIO;
264 if (be->be_f_offset < lv->cowread)
265 return -EIO;
266 /* It looks like you might want to min this with lv->start,
267 * but you really don't.
268 */
269 lv->inval = lv->inval + be->be_length;
270 lv->cowread = be->be_f_offset + be->be_length;
271 return 0;
272 } else
273 return -EIO;
274}
275
276/* XDR decode pnfs_block_layout4 structure */
Fred Isamana60d2eb2011-07-30 20:52:44 -0400277int
278nfs4_blk_process_layoutget(struct pnfs_layout_hdr *lo,
279 struct nfs4_layoutget_res *lgr, gfp_t gfp_flags)
280{
Fred Isamane9437cc2011-07-30 20:52:47 -0400281 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
282 int i, status = -EIO;
283 uint32_t count;
284 struct pnfs_block_extent *be = NULL, *save;
285 struct xdr_stream stream;
286 struct xdr_buf buf;
287 struct page *scratch;
288 __be32 *p;
289 struct layout_verification lv = {
290 .mode = lgr->range.iomode,
291 .start = lgr->range.offset >> SECTOR_SHIFT,
292 .inval = lgr->range.offset >> SECTOR_SHIFT,
293 .cowread = lgr->range.offset >> SECTOR_SHIFT,
294 };
295 LIST_HEAD(extents);
296
297 dprintk("---> %s\n", __func__);
298
299 scratch = alloc_page(gfp_flags);
300 if (!scratch)
301 return -ENOMEM;
302
303 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
304 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
305
306 p = xdr_inline_decode(&stream, 4);
307 if (unlikely(!p))
308 goto out_err;
309
310 count = be32_to_cpup(p++);
311
312 dprintk("%s enter, number of extents %i\n", __func__, count);
313 p = xdr_inline_decode(&stream, (28 + NFS4_DEVICEID4_SIZE) * count);
314 if (unlikely(!p))
315 goto out_err;
316
317 /* Decode individual extents, putting them in temporary
318 * staging area until whole layout is decoded to make error
319 * recovery easier.
320 */
321 for (i = 0; i < count; i++) {
322 be = bl_alloc_extent();
323 if (!be) {
324 status = -ENOMEM;
325 goto out_err;
326 }
327 memcpy(&be->be_devid, p, NFS4_DEVICEID4_SIZE);
328 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
329 be->be_mdev = translate_devid(lo, &be->be_devid);
330 if (!be->be_mdev)
331 goto out_err;
332
333 /* The next three values are read in as bytes,
334 * but stored as 512-byte sector lengths
335 */
336 if (decode_sector_number(&p, &be->be_f_offset) < 0)
337 goto out_err;
338 if (decode_sector_number(&p, &be->be_length) < 0)
339 goto out_err;
340 if (decode_sector_number(&p, &be->be_v_offset) < 0)
341 goto out_err;
342 be->be_state = be32_to_cpup(p++);
343 if (be->be_state == PNFS_BLOCK_INVALID_DATA)
344 be->be_inval = &bl->bl_inval;
345 if (verify_extent(be, &lv)) {
346 dprintk("%s verify failed\n", __func__);
347 goto out_err;
348 }
349 list_add_tail(&be->be_node, &extents);
350 }
351 if (lgr->range.offset + lgr->range.length !=
352 lv.start << SECTOR_SHIFT) {
353 dprintk("%s Final length mismatch\n", __func__);
354 be = NULL;
355 goto out_err;
356 }
357 if (lv.start < lv.cowread) {
358 dprintk("%s Final uncovered COW extent\n", __func__);
359 be = NULL;
360 goto out_err;
361 }
362 /* Extents decoded properly, now try to merge them in to
363 * existing layout extents.
364 */
365 spin_lock(&bl->bl_ext_lock);
366 list_for_each_entry_safe(be, save, &extents, be_node) {
367 list_del(&be->be_node);
368 status = bl_add_merge_extent(bl, be);
369 if (status) {
370 spin_unlock(&bl->bl_ext_lock);
371 /* This is a fairly catastrophic error, as the
372 * entire layout extent lists are now corrupted.
373 * We should have some way to distinguish this.
374 */
375 be = NULL;
376 goto out_err;
377 }
378 }
379 spin_unlock(&bl->bl_ext_lock);
380 status = 0;
381 out:
382 __free_page(scratch);
383 dprintk("%s returns %i\n", __func__, status);
384 return status;
385
386 out_err:
387 bl_put_extent(be);
388 while (!list_empty(&extents)) {
389 be = list_first_entry(&extents, struct pnfs_block_extent,
390 be_node);
391 list_del(&be->be_node);
392 bl_put_extent(be);
393 }
394 goto out;
Fred Isamana60d2eb2011-07-30 20:52:44 -0400395}