2 * linux/fs/nfs/blocklayout/blocklayout.c
4 * Module for the NFSv4.1 pNFS block 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/init.h>
34 #include <linux/mount.h>
35 #include <linux/namei.h>
37 #include "blocklayout.h"
39 #define NFSDBG_FACILITY NFSDBG_PNFS_LD
41 MODULE_LICENSE("GPL");
42 MODULE_AUTHOR("Andy Adamson <andros@citi.umich.edu>");
43 MODULE_DESCRIPTION("The NFSv4.1 pNFS Block layout driver");
45 struct dentry
*bl_device_pipe
;
46 wait_queue_head_t bl_wq
;
48 static enum pnfs_try_status
49 bl_read_pagelist(struct nfs_read_data
*rdata
)
51 return PNFS_NOT_ATTEMPTED
;
54 static enum pnfs_try_status
55 bl_write_pagelist(struct nfs_write_data
*wdata
,
58 return PNFS_NOT_ATTEMPTED
;
61 /* FIXME - range ignored */
63 release_extents(struct pnfs_block_layout
*bl
, struct pnfs_layout_range
*range
)
66 struct pnfs_block_extent
*be
;
68 spin_lock(&bl
->bl_ext_lock
);
69 for (i
= 0; i
< EXTENT_LISTS
; i
++) {
70 while (!list_empty(&bl
->bl_extents
[i
])) {
71 be
= list_first_entry(&bl
->bl_extents
[i
],
72 struct pnfs_block_extent
,
74 list_del(&be
->be_node
);
78 spin_unlock(&bl
->bl_ext_lock
);
82 release_inval_marks(struct pnfs_inval_markings
*marks
)
84 struct pnfs_inval_tracking
*pos
, *temp
;
86 list_for_each_entry_safe(pos
, temp
, &marks
->im_tree
.mtt_stub
, it_link
) {
87 list_del(&pos
->it_link
);
93 static void bl_free_layout_hdr(struct pnfs_layout_hdr
*lo
)
95 struct pnfs_block_layout
*bl
= BLK_LO2EXT(lo
);
97 dprintk("%s enter\n", __func__
);
98 release_extents(bl
, NULL
);
99 release_inval_marks(&bl
->bl_inval
);
103 static struct pnfs_layout_hdr
*bl_alloc_layout_hdr(struct inode
*inode
,
106 struct pnfs_block_layout
*bl
;
108 dprintk("%s enter\n", __func__
);
109 bl
= kzalloc(sizeof(*bl
), gfp_flags
);
112 spin_lock_init(&bl
->bl_ext_lock
);
113 INIT_LIST_HEAD(&bl
->bl_extents
[0]);
114 INIT_LIST_HEAD(&bl
->bl_extents
[1]);
115 INIT_LIST_HEAD(&bl
->bl_commit
);
116 INIT_LIST_HEAD(&bl
->bl_committing
);
118 bl
->bl_blocksize
= NFS_SERVER(inode
)->pnfs_blksize
>> SECTOR_SHIFT
;
119 BL_INIT_INVAL_MARKS(&bl
->bl_inval
, bl
->bl_blocksize
);
120 return &bl
->bl_layout
;
123 static void bl_free_lseg(struct pnfs_layout_segment
*lseg
)
125 dprintk("%s enter\n", __func__
);
129 /* We pretty much ignore lseg, and store all data layout wide, so we
130 * can correctly merge.
132 static struct pnfs_layout_segment
*bl_alloc_lseg(struct pnfs_layout_hdr
*lo
,
133 struct nfs4_layoutget_res
*lgr
,
136 struct pnfs_layout_segment
*lseg
;
139 dprintk("%s enter\n", __func__
);
140 lseg
= kzalloc(sizeof(*lseg
), gfp_flags
);
142 return ERR_PTR(-ENOMEM
);
143 status
= nfs4_blk_process_layoutget(lo
, lgr
, gfp_flags
);
145 /* We don't want to call the full-blown bl_free_lseg,
146 * since on error extents were not touched.
149 return ERR_PTR(status
);
155 bl_encode_layoutcommit(struct pnfs_layout_hdr
*lo
, struct xdr_stream
*xdr
,
156 const struct nfs4_layoutcommit_args
*arg
)
161 bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data
*lcdata
)
165 static void free_blk_mountid(struct block_mount_id
*mid
)
168 struct pnfs_block_dev
*dev
;
169 spin_lock(&mid
->bm_lock
);
170 while (!list_empty(&mid
->bm_devlist
)) {
171 dev
= list_first_entry(&mid
->bm_devlist
,
172 struct pnfs_block_dev
,
174 list_del(&dev
->bm_node
);
175 bl_free_block_dev(dev
);
177 spin_unlock(&mid
->bm_lock
);
182 /* This is mostly copied from the filelayout's get_device_info function.
183 * It seems much of this should be at the generic pnfs level.
185 static struct pnfs_block_dev
*
186 nfs4_blk_get_deviceinfo(struct nfs_server
*server
, const struct nfs_fh
*fh
,
187 struct nfs4_deviceid
*d_id
)
189 struct pnfs_device
*dev
;
190 struct pnfs_block_dev
*rv
= NULL
;
193 struct page
**pages
= NULL
;
197 * Use the session max response size as the basis for setting
198 * GETDEVICEINFO's maxcount
200 max_resp_sz
= server
->nfs_client
->cl_session
->fc_attrs
.max_resp_sz
;
201 max_pages
= max_resp_sz
>> PAGE_SHIFT
;
202 dprintk("%s max_resp_sz %u max_pages %d\n",
203 __func__
, max_resp_sz
, max_pages
);
205 dev
= kmalloc(sizeof(*dev
), GFP_NOFS
);
207 dprintk("%s kmalloc failed\n", __func__
);
211 pages
= kzalloc(max_pages
* sizeof(struct page
*), GFP_NOFS
);
216 for (i
= 0; i
< max_pages
; i
++) {
217 pages
[i
] = alloc_page(GFP_NOFS
);
222 memcpy(&dev
->dev_id
, d_id
, sizeof(*d_id
));
223 dev
->layout_type
= LAYOUT_BLOCK_VOLUME
;
226 dev
->pglen
= PAGE_SIZE
* max_pages
;
229 dprintk("%s: dev_id: %s\n", __func__
, dev
->dev_id
.data
);
230 rc
= nfs4_proc_getdeviceinfo(server
, dev
);
231 dprintk("%s getdevice info returns %d\n", __func__
, rc
);
235 rv
= nfs4_blk_decode_device(server
, dev
);
237 for (i
= 0; i
< max_pages
; i
++)
238 __free_page(pages
[i
]);
245 bl_set_layoutdriver(struct nfs_server
*server
, const struct nfs_fh
*fh
)
247 struct block_mount_id
*b_mt_id
= NULL
;
248 struct pnfs_devicelist
*dlist
= NULL
;
249 struct pnfs_block_dev
*bdev
;
250 LIST_HEAD(block_disklist
);
253 dprintk("%s enter\n", __func__
);
255 if (server
->pnfs_blksize
== 0) {
256 dprintk("%s Server did not return blksize\n", __func__
);
259 b_mt_id
= kzalloc(sizeof(struct block_mount_id
), GFP_NOFS
);
264 /* Initialize nfs4 block layout mount id */
265 spin_lock_init(&b_mt_id
->bm_lock
);
266 INIT_LIST_HEAD(&b_mt_id
->bm_devlist
);
268 dlist
= kmalloc(sizeof(struct pnfs_devicelist
), GFP_NOFS
);
274 while (!dlist
->eof
) {
275 status
= nfs4_proc_getdevicelist(server
, fh
, dlist
);
278 dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n",
279 __func__
, dlist
->num_devs
, dlist
->eof
);
280 for (i
= 0; i
< dlist
->num_devs
; i
++) {
281 bdev
= nfs4_blk_get_deviceinfo(server
, fh
,
287 spin_lock(&b_mt_id
->bm_lock
);
288 list_add(&bdev
->bm_node
, &b_mt_id
->bm_devlist
);
289 spin_unlock(&b_mt_id
->bm_lock
);
292 dprintk("%s SUCCESS\n", __func__
);
293 server
->pnfs_ld_data
= b_mt_id
;
300 free_blk_mountid(b_mt_id
);
305 bl_clear_layoutdriver(struct nfs_server
*server
)
307 struct block_mount_id
*b_mt_id
= server
->pnfs_ld_data
;
309 dprintk("%s enter\n", __func__
);
310 free_blk_mountid(b_mt_id
);
311 dprintk("%s RETURNS\n", __func__
);
315 static const struct nfs_pageio_ops bl_pg_read_ops
= {
316 .pg_init
= pnfs_generic_pg_init_read
,
317 .pg_test
= pnfs_generic_pg_test
,
318 .pg_doio
= pnfs_generic_pg_readpages
,
321 static const struct nfs_pageio_ops bl_pg_write_ops
= {
322 .pg_init
= pnfs_generic_pg_init_write
,
323 .pg_test
= pnfs_generic_pg_test
,
324 .pg_doio
= pnfs_generic_pg_writepages
,
327 static struct pnfs_layoutdriver_type blocklayout_type
= {
328 .id
= LAYOUT_BLOCK_VOLUME
,
329 .name
= "LAYOUT_BLOCK_VOLUME",
330 .read_pagelist
= bl_read_pagelist
,
331 .write_pagelist
= bl_write_pagelist
,
332 .alloc_layout_hdr
= bl_alloc_layout_hdr
,
333 .free_layout_hdr
= bl_free_layout_hdr
,
334 .alloc_lseg
= bl_alloc_lseg
,
335 .free_lseg
= bl_free_lseg
,
336 .encode_layoutcommit
= bl_encode_layoutcommit
,
337 .cleanup_layoutcommit
= bl_cleanup_layoutcommit
,
338 .set_layoutdriver
= bl_set_layoutdriver
,
339 .clear_layoutdriver
= bl_clear_layoutdriver
,
340 .pg_read_ops
= &bl_pg_read_ops
,
341 .pg_write_ops
= &bl_pg_write_ops
,
344 static const struct rpc_pipe_ops bl_upcall_ops
= {
345 .upcall
= bl_pipe_upcall
,
346 .downcall
= bl_pipe_downcall
,
347 .destroy_msg
= bl_pipe_destroy_msg
,
350 static int __init
nfs4blocklayout_init(void)
352 struct vfsmount
*mnt
;
356 dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__
);
358 ret
= pnfs_register_layoutdriver(&blocklayout_type
);
362 init_waitqueue_head(&bl_wq
);
364 mnt
= rpc_get_mount();
370 ret
= vfs_path_lookup(mnt
->mnt_root
,
372 NFS_PIPE_DIRNAME
, 0, &path
);
376 bl_device_pipe
= rpc_mkpipe(path
.dentry
, "blocklayout", NULL
,
378 if (IS_ERR(bl_device_pipe
)) {
379 ret
= PTR_ERR(bl_device_pipe
);
386 pnfs_unregister_layoutdriver(&blocklayout_type
);
390 static void __exit
nfs4blocklayout_exit(void)
392 dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
395 pnfs_unregister_layoutdriver(&blocklayout_type
);
396 rpc_unlink(bl_device_pipe
);
399 MODULE_ALIAS("nfs-layouttype4-3");
401 module_init(nfs4blocklayout_init
);
402 module_exit(nfs4blocklayout_exit
);