2 * linux/fs/nfs/blocklayout/blocklayoutdev.c
4 * Device operations for the pnfs nfs4 file layout driver.
6 * Copyright (c) 2006 The Regents of the University of Michigan.
9 * Andy Adamson <andros@citi.umich.edu>
10 * Fred Isaman <iisaman@umich.edu>
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.
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.
32 #include <linux/module.h>
33 #include <linux/buffer_head.h> /* __bread */
35 #include <linux/genhd.h>
36 #include <linux/blkdev.h>
37 #include <linux/hash.h>
39 #include "blocklayout.h"
41 #define NFSDBG_FACILITY NFSDBG_PNFS_LD
43 static int decode_sector_number(__be32 **rp, sector_t *sp)
47 *rp = xdr_decode_hyper(*rp, &s);
49 printk(KERN_WARNING "NFS: %s: sector not aligned\n", __func__);
52 *sp = s >> SECTOR_SHIFT;
56 /* Open a block_device by device number. */
57 struct block_device *nfs4_blkdev_get(dev_t dev)
59 struct block_device *bd;
61 dprintk("%s enter\n", __func__);
62 bd = blkdev_get_by_dev(dev, FMODE_READ, NULL);
67 dprintk("%s failed to open device : %ld\n",
68 __func__, PTR_ERR(bd));
73 * Release the block device
75 int nfs4_blkdev_put(struct block_device *bdev)
77 dprintk("%s for device %d:%d\n", __func__, MAJOR(bdev->bd_dev),
79 return blkdev_put(bdev, FMODE_READ);
82 ssize_t bl_pipe_downcall(struct file *filp, const char __user *src,
85 struct nfs_net *nn = net_generic(filp->f_dentry->d_sb->s_fs_info,
88 if (mlen != sizeof (struct bl_dev_msg))
91 if (copy_from_user(&nn->bl_mount_reply, src, mlen) != 0)
99 void bl_pipe_destroy_msg(struct rpc_pipe_msg *msg)
101 struct bl_pipe_msg *bl_pipe_msg = container_of(msg, struct bl_pipe_msg, msg);
105 wake_up(bl_pipe_msg->bl_wq);
109 * Decodes pnfs_block_deviceaddr4 which is XDR encoded in dev->dev_addr_buf.
111 struct pnfs_block_dev *
112 nfs4_blk_decode_device(struct nfs_server *server,
113 struct pnfs_device *dev)
115 struct pnfs_block_dev *rv;
116 struct block_device *bd = NULL;
117 struct bl_pipe_msg bl_pipe_msg;
118 struct rpc_pipe_msg *msg = &bl_pipe_msg.msg;
119 struct bl_msg_hdr bl_msg = {
120 .type = BL_DEVICE_MOUNT,
121 .totallen = dev->mincount,
124 DECLARE_WAITQUEUE(wq, current);
125 int offset, len, i, rc;
126 struct net *net = server->nfs_client->cl_net;
127 struct nfs_net *nn = net_generic(net, nfs_net_id);
128 struct bl_dev_msg *reply = &nn->bl_mount_reply;
130 dprintk("%s CREATING PIPEFS MESSAGE\n", __func__);
131 dprintk("%s: deviceid: %s, mincount: %d\n", __func__, dev->dev_id.data,
134 bl_pipe_msg.bl_wq = &nn->bl_wq;
135 memset(msg, 0, sizeof(*msg));
136 msg->data = kzalloc(sizeof(bl_msg) + dev->mincount, GFP_NOFS);
138 rv = ERR_PTR(-ENOMEM);
142 memcpy(msg->data, &bl_msg, sizeof(bl_msg));
143 dataptr = (uint8_t *) msg->data;
145 offset = sizeof(bl_msg);
146 for (i = 0; len > 0; i++) {
147 memcpy(&dataptr[offset], page_address(dev->pages[i]),
148 len < PAGE_CACHE_SIZE ? len : PAGE_CACHE_SIZE);
149 len -= PAGE_CACHE_SIZE;
150 offset += PAGE_CACHE_SIZE;
152 msg->len = sizeof(bl_msg) + dev->mincount;
154 dprintk("%s CALLING USERSPACE DAEMON\n", __func__);
155 add_wait_queue(&nn->bl_wq, &wq);
156 rc = rpc_queue_upcall(nn->bl_device_pipe, msg);
158 remove_wait_queue(&nn->bl_wq, &wq);
163 set_current_state(TASK_UNINTERRUPTIBLE);
165 __set_current_state(TASK_RUNNING);
166 remove_wait_queue(&nn->bl_wq, &wq);
168 if (reply->status != BL_DEVICE_REQUEST_PROC) {
169 dprintk("%s failed to open device: %d\n",
170 __func__, reply->status);
171 rv = ERR_PTR(-EINVAL);
175 bd = nfs4_blkdev_get(MKDEV(reply->major, reply->minor));
178 dprintk("%s failed to open device : %d\n", __func__, rc);
183 rv = kzalloc(sizeof(*rv), GFP_NOFS);
185 rv = ERR_PTR(-ENOMEM);
190 memcpy(&rv->bm_mdevid, &dev->dev_id, sizeof(struct nfs4_deviceid));
192 dprintk("%s Created device %s with bd_block_size %u\n",
194 bd->bd_disk->disk_name,
202 /* Map deviceid returned by the server to constructed block_device */
203 static struct block_device *translate_devid(struct pnfs_layout_hdr *lo,
204 struct nfs4_deviceid *id)
206 struct block_device *rv = NULL;
207 struct block_mount_id *mid;
208 struct pnfs_block_dev *dev;
210 dprintk("%s enter, lo=%p, id=%p\n", __func__, lo, id);
212 spin_lock(&mid->bm_lock);
213 list_for_each_entry(dev, &mid->bm_devlist, bm_node) {
214 if (memcmp(id->data, dev->bm_mdevid.data,
215 NFS4_DEVICEID4_SIZE) == 0) {
221 spin_unlock(&mid->bm_lock);
222 dprintk("%s returning %p\n", __func__, rv);
226 /* Tracks info needed to ensure extents in layout obey constraints of spec */
227 struct layout_verification {
228 u32 mode; /* R or RW */
229 u64 start; /* Expected start of next non-COW extent */
230 u64 inval; /* Start of INVAL coverage */
231 u64 cowread; /* End of COW read coverage */
234 /* Verify the extent meets the layout requirements of the pnfs-block draft,
237 static int verify_extent(struct pnfs_block_extent *be,
238 struct layout_verification *lv)
240 if (lv->mode == IOMODE_READ) {
241 if (be->be_state == PNFS_BLOCK_READWRITE_DATA ||
242 be->be_state == PNFS_BLOCK_INVALID_DATA)
244 if (be->be_f_offset != lv->start)
246 lv->start += be->be_length;
249 /* lv->mode == IOMODE_RW */
250 if (be->be_state == PNFS_BLOCK_READWRITE_DATA) {
251 if (be->be_f_offset != lv->start)
253 if (lv->cowread > lv->start)
255 lv->start += be->be_length;
256 lv->inval = lv->start;
258 } else if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
259 if (be->be_f_offset != lv->start)
261 lv->start += be->be_length;
263 } else if (be->be_state == PNFS_BLOCK_READ_DATA) {
264 if (be->be_f_offset > lv->start)
266 if (be->be_f_offset < lv->inval)
268 if (be->be_f_offset < lv->cowread)
270 /* It looks like you might want to min this with lv->start,
271 * but you really don't.
273 lv->inval = lv->inval + be->be_length;
274 lv->cowread = be->be_f_offset + be->be_length;
280 /* XDR decode pnfs_block_layout4 structure */
282 nfs4_blk_process_layoutget(struct pnfs_layout_hdr *lo,
283 struct nfs4_layoutget_res *lgr, gfp_t gfp_flags)
285 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
286 int i, status = -EIO;
288 struct pnfs_block_extent *be = NULL, *save;
289 struct xdr_stream stream;
291 struct page *scratch;
293 struct layout_verification lv = {
294 .mode = lgr->range.iomode,
295 .start = lgr->range.offset >> SECTOR_SHIFT,
296 .inval = lgr->range.offset >> SECTOR_SHIFT,
297 .cowread = lgr->range.offset >> SECTOR_SHIFT,
301 dprintk("---> %s\n", __func__);
303 scratch = alloc_page(gfp_flags);
307 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
308 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
310 p = xdr_inline_decode(&stream, 4);
314 count = be32_to_cpup(p++);
316 dprintk("%s enter, number of extents %i\n", __func__, count);
317 p = xdr_inline_decode(&stream, (28 + NFS4_DEVICEID4_SIZE) * count);
321 /* Decode individual extents, putting them in temporary
322 * staging area until whole layout is decoded to make error
325 for (i = 0; i < count; i++) {
326 be = bl_alloc_extent();
331 memcpy(&be->be_devid, p, NFS4_DEVICEID4_SIZE);
332 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
333 be->be_mdev = translate_devid(lo, &be->be_devid);
337 /* The next three values are read in as bytes,
338 * but stored as 512-byte sector lengths
340 if (decode_sector_number(&p, &be->be_f_offset) < 0)
342 if (decode_sector_number(&p, &be->be_length) < 0)
344 if (decode_sector_number(&p, &be->be_v_offset) < 0)
346 be->be_state = be32_to_cpup(p++);
347 if (be->be_state == PNFS_BLOCK_INVALID_DATA)
348 be->be_inval = &bl->bl_inval;
349 if (verify_extent(be, &lv)) {
350 dprintk("%s verify failed\n", __func__);
353 list_add_tail(&be->be_node, &extents);
355 if (lgr->range.offset + lgr->range.length !=
356 lv.start << SECTOR_SHIFT) {
357 dprintk("%s Final length mismatch\n", __func__);
361 if (lv.start < lv.cowread) {
362 dprintk("%s Final uncovered COW extent\n", __func__);
366 /* Extents decoded properly, now try to merge them in to
367 * existing layout extents.
369 spin_lock(&bl->bl_ext_lock);
370 list_for_each_entry_safe(be, save, &extents, be_node) {
371 list_del(&be->be_node);
372 status = bl_add_merge_extent(bl, be);
374 spin_unlock(&bl->bl_ext_lock);
375 /* This is a fairly catastrophic error, as the
376 * entire layout extent lists are now corrupted.
377 * We should have some way to distinguish this.
383 spin_unlock(&bl->bl_ext_lock);
386 __free_page(scratch);
387 dprintk("%s returns %i\n", __func__, status);
392 while (!list_empty(&extents)) {
393 be = list_first_entry(&extents, struct pnfs_block_extent,
395 list_del(&be->be_node);