blob: 6cd7f4f3acdb9b831eb7a31c7cfc0d9b860ccb9a [file] [log] [blame]
Fred Isaman155e7522011-07-30 20:52:39 -04001/*
2 * linux/fs/nfs/blocklayout/blocklayout.c
3 *
4 * Module for the NFSv4.1 pNFS block 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/init.h>
Jim Reesfe0a9b72011-07-30 20:52:42 -040034#include <linux/mount.h>
35#include <linux/namei.h>
Fred Isaman155e7522011-07-30 20:52:39 -040036
37#include "blocklayout.h"
38
39#define NFSDBG_FACILITY NFSDBG_PNFS_LD
40
41MODULE_LICENSE("GPL");
42MODULE_AUTHOR("Andy Adamson <andros@citi.umich.edu>");
43MODULE_DESCRIPTION("The NFSv4.1 pNFS Block layout driver");
44
Jim Reesfe0a9b72011-07-30 20:52:42 -040045struct dentry *bl_device_pipe;
46wait_queue_head_t bl_wq;
47
Fred Isaman155e7522011-07-30 20:52:39 -040048static enum pnfs_try_status
49bl_read_pagelist(struct nfs_read_data *rdata)
50{
51 return PNFS_NOT_ATTEMPTED;
52}
53
54static enum pnfs_try_status
55bl_write_pagelist(struct nfs_write_data *wdata,
56 int sync)
57{
58 return PNFS_NOT_ATTEMPTED;
59}
60
Fred Isaman9e692962011-07-30 20:52:41 -040061/* FIXME - range ignored */
Fred Isaman155e7522011-07-30 20:52:39 -040062static void
Fred Isaman9e692962011-07-30 20:52:41 -040063release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range)
Fred Isaman155e7522011-07-30 20:52:39 -040064{
Fred Isaman9e692962011-07-30 20:52:41 -040065 int i;
66 struct pnfs_block_extent *be;
67
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,
73 be_node);
74 list_del(&be->be_node);
75 bl_put_extent(be);
76 }
77 }
78 spin_unlock(&bl->bl_ext_lock);
Fred Isaman155e7522011-07-30 20:52:39 -040079}
80
81/* STUB */
82static void
83release_inval_marks(struct pnfs_inval_markings *marks)
84{
85 return;
86}
87
88static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
89{
90 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
91
92 dprintk("%s enter\n", __func__);
93 release_extents(bl, NULL);
94 release_inval_marks(&bl->bl_inval);
95 kfree(bl);
96}
97
98static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode,
99 gfp_t gfp_flags)
100{
101 struct pnfs_block_layout *bl;
102
103 dprintk("%s enter\n", __func__);
104 bl = kzalloc(sizeof(*bl), gfp_flags);
105 if (!bl)
106 return NULL;
107 spin_lock_init(&bl->bl_ext_lock);
108 INIT_LIST_HEAD(&bl->bl_extents[0]);
109 INIT_LIST_HEAD(&bl->bl_extents[1]);
110 INIT_LIST_HEAD(&bl->bl_commit);
111 INIT_LIST_HEAD(&bl->bl_committing);
112 bl->bl_count = 0;
113 bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT;
114 BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize);
115 return &bl->bl_layout;
116}
117
Fred Isamana60d2eb2011-07-30 20:52:44 -0400118static void bl_free_lseg(struct pnfs_layout_segment *lseg)
Fred Isaman155e7522011-07-30 20:52:39 -0400119{
Fred Isamana60d2eb2011-07-30 20:52:44 -0400120 dprintk("%s enter\n", __func__);
121 kfree(lseg);
Fred Isaman155e7522011-07-30 20:52:39 -0400122}
123
Fred Isamana60d2eb2011-07-30 20:52:44 -0400124/* We pretty much ignore lseg, and store all data layout wide, so we
125 * can correctly merge.
126 */
127static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo,
128 struct nfs4_layoutget_res *lgr,
129 gfp_t gfp_flags)
Fred Isaman155e7522011-07-30 20:52:39 -0400130{
Fred Isamana60d2eb2011-07-30 20:52:44 -0400131 struct pnfs_layout_segment *lseg;
132 int status;
133
134 dprintk("%s enter\n", __func__);
135 lseg = kzalloc(sizeof(*lseg), gfp_flags);
136 if (!lseg)
137 return ERR_PTR(-ENOMEM);
138 status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags);
139 if (status) {
140 /* We don't want to call the full-blown bl_free_lseg,
141 * since on error extents were not touched.
142 */
143 kfree(lseg);
144 return ERR_PTR(status);
145 }
146 return lseg;
Fred Isaman155e7522011-07-30 20:52:39 -0400147}
148
149static void
150bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr,
151 const struct nfs4_layoutcommit_args *arg)
152{
153}
154
155static void
156bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
157{
158}
159
Fred Isaman2f9fd182011-07-30 20:52:46 -0400160static void free_blk_mountid(struct block_mount_id *mid)
161{
162 if (mid) {
163 struct pnfs_block_dev *dev;
164 spin_lock(&mid->bm_lock);
165 while (!list_empty(&mid->bm_devlist)) {
166 dev = list_first_entry(&mid->bm_devlist,
167 struct pnfs_block_dev,
168 bm_node);
169 list_del(&dev->bm_node);
170 bl_free_block_dev(dev);
171 }
172 spin_unlock(&mid->bm_lock);
173 kfree(mid);
174 }
175}
176
177/* This is mostly copied from the filelayout's get_device_info function.
178 * It seems much of this should be at the generic pnfs level.
179 */
180static struct pnfs_block_dev *
181nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh,
182 struct nfs4_deviceid *d_id)
183{
184 struct pnfs_device *dev;
185 struct pnfs_block_dev *rv = NULL;
186 u32 max_resp_sz;
187 int max_pages;
188 struct page **pages = NULL;
189 int i, rc;
190
191 /*
192 * Use the session max response size as the basis for setting
193 * GETDEVICEINFO's maxcount
194 */
195 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
196 max_pages = max_resp_sz >> PAGE_SHIFT;
197 dprintk("%s max_resp_sz %u max_pages %d\n",
198 __func__, max_resp_sz, max_pages);
199
200 dev = kmalloc(sizeof(*dev), GFP_NOFS);
201 if (!dev) {
202 dprintk("%s kmalloc failed\n", __func__);
203 return NULL;
204 }
205
206 pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS);
207 if (pages == NULL) {
208 kfree(dev);
209 return NULL;
210 }
211 for (i = 0; i < max_pages; i++) {
212 pages[i] = alloc_page(GFP_NOFS);
213 if (!pages[i])
214 goto out_free;
215 }
216
217 memcpy(&dev->dev_id, d_id, sizeof(*d_id));
218 dev->layout_type = LAYOUT_BLOCK_VOLUME;
219 dev->pages = pages;
220 dev->pgbase = 0;
221 dev->pglen = PAGE_SIZE * max_pages;
222 dev->mincount = 0;
223
224 dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data);
225 rc = nfs4_proc_getdeviceinfo(server, dev);
226 dprintk("%s getdevice info returns %d\n", __func__, rc);
227 if (rc)
228 goto out_free;
229
230 rv = nfs4_blk_decode_device(server, dev);
231 out_free:
232 for (i = 0; i < max_pages; i++)
233 __free_page(pages[i]);
234 kfree(pages);
235 kfree(dev);
236 return rv;
237}
238
Fred Isaman155e7522011-07-30 20:52:39 -0400239static int
240bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
241{
Fred Isaman2f9fd182011-07-30 20:52:46 -0400242 struct block_mount_id *b_mt_id = NULL;
243 struct pnfs_devicelist *dlist = NULL;
244 struct pnfs_block_dev *bdev;
245 LIST_HEAD(block_disklist);
246 int status = 0, i;
247
Fred Isaman155e7522011-07-30 20:52:39 -0400248 dprintk("%s enter\n", __func__);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400249
250 if (server->pnfs_blksize == 0) {
251 dprintk("%s Server did not return blksize\n", __func__);
252 return -EINVAL;
253 }
254 b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS);
255 if (!b_mt_id) {
256 status = -ENOMEM;
257 goto out_error;
258 }
259 /* Initialize nfs4 block layout mount id */
260 spin_lock_init(&b_mt_id->bm_lock);
261 INIT_LIST_HEAD(&b_mt_id->bm_devlist);
262
263 dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS);
264 if (!dlist) {
265 status = -ENOMEM;
266 goto out_error;
267 }
268 dlist->eof = 0;
269 while (!dlist->eof) {
270 status = nfs4_proc_getdevicelist(server, fh, dlist);
271 if (status)
272 goto out_error;
273 dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n",
274 __func__, dlist->num_devs, dlist->eof);
275 for (i = 0; i < dlist->num_devs; i++) {
276 bdev = nfs4_blk_get_deviceinfo(server, fh,
277 &dlist->dev_id[i]);
278 if (!bdev) {
279 status = -ENODEV;
280 goto out_error;
281 }
282 spin_lock(&b_mt_id->bm_lock);
283 list_add(&bdev->bm_node, &b_mt_id->bm_devlist);
284 spin_unlock(&b_mt_id->bm_lock);
285 }
286 }
287 dprintk("%s SUCCESS\n", __func__);
288 server->pnfs_ld_data = b_mt_id;
289
290 out_return:
291 kfree(dlist);
292 return status;
293
294 out_error:
295 free_blk_mountid(b_mt_id);
296 goto out_return;
Fred Isaman155e7522011-07-30 20:52:39 -0400297}
298
299static int
300bl_clear_layoutdriver(struct nfs_server *server)
301{
Fred Isaman2f9fd182011-07-30 20:52:46 -0400302 struct block_mount_id *b_mt_id = server->pnfs_ld_data;
303
Fred Isaman155e7522011-07-30 20:52:39 -0400304 dprintk("%s enter\n", __func__);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400305 free_blk_mountid(b_mt_id);
306 dprintk("%s RETURNS\n", __func__);
Fred Isaman155e7522011-07-30 20:52:39 -0400307 return 0;
308}
309
Benny Halevye9643fe2011-07-30 20:52:40 -0400310static const struct nfs_pageio_ops bl_pg_read_ops = {
311 .pg_init = pnfs_generic_pg_init_read,
312 .pg_test = pnfs_generic_pg_test,
313 .pg_doio = pnfs_generic_pg_readpages,
314};
315
316static const struct nfs_pageio_ops bl_pg_write_ops = {
317 .pg_init = pnfs_generic_pg_init_write,
318 .pg_test = pnfs_generic_pg_test,
319 .pg_doio = pnfs_generic_pg_writepages,
320};
321
Fred Isaman155e7522011-07-30 20:52:39 -0400322static struct pnfs_layoutdriver_type blocklayout_type = {
323 .id = LAYOUT_BLOCK_VOLUME,
324 .name = "LAYOUT_BLOCK_VOLUME",
325 .read_pagelist = bl_read_pagelist,
326 .write_pagelist = bl_write_pagelist,
327 .alloc_layout_hdr = bl_alloc_layout_hdr,
328 .free_layout_hdr = bl_free_layout_hdr,
329 .alloc_lseg = bl_alloc_lseg,
330 .free_lseg = bl_free_lseg,
331 .encode_layoutcommit = bl_encode_layoutcommit,
332 .cleanup_layoutcommit = bl_cleanup_layoutcommit,
333 .set_layoutdriver = bl_set_layoutdriver,
334 .clear_layoutdriver = bl_clear_layoutdriver,
Benny Halevye9643fe2011-07-30 20:52:40 -0400335 .pg_read_ops = &bl_pg_read_ops,
336 .pg_write_ops = &bl_pg_write_ops,
Fred Isaman155e7522011-07-30 20:52:39 -0400337};
338
Jim Reesfe0a9b72011-07-30 20:52:42 -0400339static const struct rpc_pipe_ops bl_upcall_ops = {
340 .upcall = bl_pipe_upcall,
341 .downcall = bl_pipe_downcall,
342 .destroy_msg = bl_pipe_destroy_msg,
343};
344
Fred Isaman155e7522011-07-30 20:52:39 -0400345static int __init nfs4blocklayout_init(void)
346{
Jim Reesfe0a9b72011-07-30 20:52:42 -0400347 struct vfsmount *mnt;
348 struct path path;
Fred Isaman155e7522011-07-30 20:52:39 -0400349 int ret;
350
351 dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
352
353 ret = pnfs_register_layoutdriver(&blocklayout_type);
Jim Reesfe0a9b72011-07-30 20:52:42 -0400354 if (ret)
355 goto out;
356
357 init_waitqueue_head(&bl_wq);
358
359 mnt = rpc_get_mount();
360 if (IS_ERR(mnt)) {
361 ret = PTR_ERR(mnt);
362 goto out_remove;
363 }
364
365 ret = vfs_path_lookup(mnt->mnt_root,
366 mnt,
367 NFS_PIPE_DIRNAME, 0, &path);
368 if (ret)
369 goto out_remove;
370
371 bl_device_pipe = rpc_mkpipe(path.dentry, "blocklayout", NULL,
372 &bl_upcall_ops, 0);
373 if (IS_ERR(bl_device_pipe)) {
374 ret = PTR_ERR(bl_device_pipe);
375 goto out_remove;
376 }
377out:
378 return ret;
379
380out_remove:
381 pnfs_unregister_layoutdriver(&blocklayout_type);
Fred Isaman155e7522011-07-30 20:52:39 -0400382 return ret;
383}
384
385static void __exit nfs4blocklayout_exit(void)
386{
387 dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
388 __func__);
389
390 pnfs_unregister_layoutdriver(&blocklayout_type);
Jim Reesfe0a9b72011-07-30 20:52:42 -0400391 rpc_unlink(bl_device_pipe);
Fred Isaman155e7522011-07-30 20:52:39 -0400392}
393
394MODULE_ALIAS("nfs-layouttype4-3");
395
396module_init(nfs4blocklayout_init);
397module_exit(nfs4blocklayout_exit);