pnfsblock: note written INVAL areas for layoutcommit
[linux-2.6-block.git] / fs / nfs / blocklayout / blocklayout.c
CommitLineData
155e7524
FI
1/*
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 */
9549ec01 32
155e7524
FI
33#include <linux/module.h>
34#include <linux/init.h>
fe0a9b74
JR
35#include <linux/mount.h>
36#include <linux/namei.h>
9549ec01 37#include <linux/bio.h> /* struct bio */
155e7524
FI
38
39#include "blocklayout.h"
40
41#define NFSDBG_FACILITY NFSDBG_PNFS_LD
42
43MODULE_LICENSE("GPL");
44MODULE_AUTHOR("Andy Adamson <andros@citi.umich.edu>");
45MODULE_DESCRIPTION("The NFSv4.1 pNFS Block layout driver");
46
fe0a9b74
JR
47struct dentry *bl_device_pipe;
48wait_queue_head_t bl_wq;
49
9549ec01
FI
50static void print_page(struct page *page)
51{
52 dprintk("PRINTPAGE page %p\n", page);
53 dprintk(" PagePrivate %d\n", PagePrivate(page));
54 dprintk(" PageUptodate %d\n", PageUptodate(page));
55 dprintk(" PageError %d\n", PageError(page));
56 dprintk(" PageDirty %d\n", PageDirty(page));
57 dprintk(" PageReferenced %d\n", PageReferenced(page));
58 dprintk(" PageLocked %d\n", PageLocked(page));
59 dprintk(" PageWriteback %d\n", PageWriteback(page));
60 dprintk(" PageMappedToDisk %d\n", PageMappedToDisk(page));
61 dprintk("\n");
62}
63
64/* Given the be associated with isect, determine if page data needs to be
65 * initialized.
66 */
67static int is_hole(struct pnfs_block_extent *be, sector_t isect)
68{
69 if (be->be_state == PNFS_BLOCK_NONE_DATA)
70 return 1;
71 else if (be->be_state != PNFS_BLOCK_INVALID_DATA)
72 return 0;
73 else
74 return !bl_is_sector_init(be->be_inval, isect);
75}
76
650e2d39
FI
77/* Given the be associated with isect, determine if page data can be
78 * written to disk.
79 */
80static int is_writable(struct pnfs_block_extent *be, sector_t isect)
81{
82 if (be->be_state == PNFS_BLOCK_READWRITE_DATA)
83 return 1;
84 else if (be->be_state != PNFS_BLOCK_INVALID_DATA)
85 return 0;
86 else
87 return bl_is_sector_init(be->be_inval, isect);
88}
89
9549ec01
FI
90/* The data we are handed might be spread across several bios. We need
91 * to track when the last one is finished.
92 */
93struct parallel_io {
94 struct kref refcnt;
95 struct rpc_call_ops call_ops;
96 void (*pnfs_callback) (void *data);
97 void *data;
98};
99
100static inline struct parallel_io *alloc_parallel(void *data)
101{
102 struct parallel_io *rv;
103
104 rv = kmalloc(sizeof(*rv), GFP_NOFS);
105 if (rv) {
106 rv->data = data;
107 kref_init(&rv->refcnt);
108 }
109 return rv;
110}
111
112static inline void get_parallel(struct parallel_io *p)
113{
114 kref_get(&p->refcnt);
115}
116
117static void destroy_parallel(struct kref *kref)
118{
119 struct parallel_io *p = container_of(kref, struct parallel_io, refcnt);
120
121 dprintk("%s enter\n", __func__);
122 p->pnfs_callback(p->data);
123 kfree(p);
124}
125
126static inline void put_parallel(struct parallel_io *p)
127{
128 kref_put(&p->refcnt, destroy_parallel);
129}
130
131static struct bio *
132bl_submit_bio(int rw, struct bio *bio)
133{
134 if (bio) {
135 get_parallel(bio->bi_private);
136 dprintk("%s submitting %s bio %u@%llu\n", __func__,
137 rw == READ ? "read" : "write",
138 bio->bi_size, (unsigned long long)bio->bi_sector);
139 submit_bio(rw, bio);
140 }
141 return NULL;
142}
143
144static struct bio *bl_alloc_init_bio(int npg, sector_t isect,
145 struct pnfs_block_extent *be,
146 void (*end_io)(struct bio *, int err),
147 struct parallel_io *par)
148{
149 struct bio *bio;
150
151 bio = bio_alloc(GFP_NOIO, npg);
152 if (!bio)
153 return NULL;
154
155 bio->bi_sector = isect - be->be_f_offset + be->be_v_offset;
156 bio->bi_bdev = be->be_mdev;
157 bio->bi_end_io = end_io;
158 bio->bi_private = par;
159 return bio;
160}
161
162static struct bio *bl_add_page_to_bio(struct bio *bio, int npg, int rw,
163 sector_t isect, struct page *page,
164 struct pnfs_block_extent *be,
165 void (*end_io)(struct bio *, int err),
166 struct parallel_io *par)
167{
168retry:
169 if (!bio) {
170 bio = bl_alloc_init_bio(npg, isect, be, end_io, par);
171 if (!bio)
172 return ERR_PTR(-ENOMEM);
173 }
174 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
175 bio = bl_submit_bio(rw, bio);
176 goto retry;
177 }
178 return bio;
179}
180
181static void bl_set_lo_fail(struct pnfs_layout_segment *lseg)
182{
183 if (lseg->pls_range.iomode == IOMODE_RW) {
184 dprintk("%s Setting layout IOMODE_RW fail bit\n", __func__);
185 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
186 } else {
187 dprintk("%s Setting layout IOMODE_READ fail bit\n", __func__);
188 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
189 }
190}
191
192/* This is basically copied from mpage_end_io_read */
193static void bl_end_io_read(struct bio *bio, int err)
194{
195 struct parallel_io *par = bio->bi_private;
196 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
197 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
198 struct nfs_read_data *rdata = (struct nfs_read_data *)par->data;
199
200 do {
201 struct page *page = bvec->bv_page;
202
203 if (--bvec >= bio->bi_io_vec)
204 prefetchw(&bvec->bv_page->flags);
205 if (uptodate)
206 SetPageUptodate(page);
207 } while (bvec >= bio->bi_io_vec);
208 if (!uptodate) {
209 if (!rdata->pnfs_error)
210 rdata->pnfs_error = -EIO;
211 bl_set_lo_fail(rdata->lseg);
212 }
213 bio_put(bio);
214 put_parallel(par);
215}
216
217static void bl_read_cleanup(struct work_struct *work)
218{
219 struct rpc_task *task;
220 struct nfs_read_data *rdata;
221 dprintk("%s enter\n", __func__);
222 task = container_of(work, struct rpc_task, u.tk_work);
223 rdata = container_of(task, struct nfs_read_data, task);
224 pnfs_ld_read_done(rdata);
225}
226
227static void
228bl_end_par_io_read(void *data)
229{
230 struct nfs_read_data *rdata = data;
231
232 INIT_WORK(&rdata->task.u.tk_work, bl_read_cleanup);
233 schedule_work(&rdata->task.u.tk_work);
234}
235
236/* We don't want normal .rpc_call_done callback used, so we replace it
237 * with this stub.
238 */
239static void bl_rpc_do_nothing(struct rpc_task *task, void *calldata)
240{
241 return;
242}
243
155e7524
FI
244static enum pnfs_try_status
245bl_read_pagelist(struct nfs_read_data *rdata)
246{
9549ec01
FI
247 int i, hole;
248 struct bio *bio = NULL;
249 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
250 sector_t isect, extent_length = 0;
251 struct parallel_io *par;
252 loff_t f_offset = rdata->args.offset;
253 size_t count = rdata->args.count;
254 struct page **pages = rdata->args.pages;
255 int pg_index = rdata->args.pgbase >> PAGE_CACHE_SHIFT;
256
257 dprintk("%s enter nr_pages %u offset %lld count %Zd\n", __func__,
258 rdata->npages, f_offset, count);
259
260 par = alloc_parallel(rdata);
261 if (!par)
262 goto use_mds;
263 par->call_ops = *rdata->mds_ops;
264 par->call_ops.rpc_call_done = bl_rpc_do_nothing;
265 par->pnfs_callback = bl_end_par_io_read;
266 /* At this point, we can no longer jump to use_mds */
267
268 isect = (sector_t) (f_offset >> SECTOR_SHIFT);
269 /* Code assumes extents are page-aligned */
270 for (i = pg_index; i < rdata->npages; i++) {
271 if (!extent_length) {
272 /* We've used up the previous extent */
273 bl_put_extent(be);
274 bl_put_extent(cow_read);
275 bio = bl_submit_bio(READ, bio);
276 /* Get the next one */
277 be = bl_find_get_extent(BLK_LSEG2EXT(rdata->lseg),
278 isect, &cow_read);
279 if (!be) {
280 rdata->pnfs_error = -EIO;
281 goto out;
282 }
283 extent_length = be->be_length -
284 (isect - be->be_f_offset);
285 if (cow_read) {
286 sector_t cow_length = cow_read->be_length -
287 (isect - cow_read->be_f_offset);
288 extent_length = min(extent_length, cow_length);
289 }
290 }
291 hole = is_hole(be, isect);
292 if (hole && !cow_read) {
293 bio = bl_submit_bio(READ, bio);
294 /* Fill hole w/ zeroes w/o accessing device */
295 dprintk("%s Zeroing page for hole\n", __func__);
296 zero_user_segment(pages[i], 0, PAGE_CACHE_SIZE);
297 print_page(pages[i]);
298 SetPageUptodate(pages[i]);
299 } else {
300 struct pnfs_block_extent *be_read;
301
302 be_read = (hole && cow_read) ? cow_read : be;
303 bio = bl_add_page_to_bio(bio, rdata->npages - i, READ,
304 isect, pages[i], be_read,
305 bl_end_io_read, par);
306 if (IS_ERR(bio)) {
307 rdata->pnfs_error = PTR_ERR(bio);
308 goto out;
309 }
310 }
311 isect += PAGE_CACHE_SECTORS;
312 extent_length -= PAGE_CACHE_SECTORS;
313 }
314 if ((isect << SECTOR_SHIFT) >= rdata->inode->i_size) {
315 rdata->res.eof = 1;
316 rdata->res.count = rdata->inode->i_size - f_offset;
317 } else {
318 rdata->res.count = (isect << SECTOR_SHIFT) - f_offset;
319 }
320out:
321 bl_put_extent(be);
322 bl_put_extent(cow_read);
323 bl_submit_bio(READ, bio);
324 put_parallel(par);
325 return PNFS_ATTEMPTED;
326
327 use_mds:
328 dprintk("Giving up and using normal NFS\n");
155e7524
FI
329 return PNFS_NOT_ATTEMPTED;
330}
331
31e6306a
FI
332static void mark_extents_written(struct pnfs_block_layout *bl,
333 __u64 offset, __u32 count)
334{
335 sector_t isect, end;
336 struct pnfs_block_extent *be;
337
338 dprintk("%s(%llu, %u)\n", __func__, offset, count);
339 if (count == 0)
340 return;
341 isect = (offset & (long)(PAGE_CACHE_MASK)) >> SECTOR_SHIFT;
342 end = (offset + count + PAGE_CACHE_SIZE - 1) & (long)(PAGE_CACHE_MASK);
343 end >>= SECTOR_SHIFT;
344 while (isect < end) {
345 sector_t len;
346 be = bl_find_get_extent(bl, isect, NULL);
347 BUG_ON(!be); /* FIXME */
348 len = min(end, be->be_f_offset + be->be_length) - isect;
349 if (be->be_state == PNFS_BLOCK_INVALID_DATA)
350 bl_mark_for_commit(be, isect, len); /* What if fails? */
351 isect += len;
352 bl_put_extent(be);
353 }
354}
355
650e2d39
FI
356/* This is basically copied from mpage_end_io_read */
357static void bl_end_io_write(struct bio *bio, int err)
358{
359 struct parallel_io *par = bio->bi_private;
360 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
361 struct nfs_write_data *wdata = (struct nfs_write_data *)par->data;
362
363 if (!uptodate) {
364 if (!wdata->pnfs_error)
365 wdata->pnfs_error = -EIO;
366 bl_set_lo_fail(wdata->lseg);
367 }
368 bio_put(bio);
369 put_parallel(par);
370}
371
372/* Function scheduled for call during bl_end_par_io_write,
373 * it marks sectors as written and extends the commitlist.
374 */
375static void bl_write_cleanup(struct work_struct *work)
376{
377 struct rpc_task *task;
378 struct nfs_write_data *wdata;
379 dprintk("%s enter\n", __func__);
380 task = container_of(work, struct rpc_task, u.tk_work);
381 wdata = container_of(task, struct nfs_write_data, task);
31e6306a
FI
382 if (!wdata->task.tk_status) {
383 /* Marks for LAYOUTCOMMIT */
384 /* BUG - this should be called after each bio, not after
385 * all finish, unless have some way of storing success/failure
386 */
387 mark_extents_written(BLK_LSEG2EXT(wdata->lseg),
388 wdata->args.offset, wdata->args.count);
389 }
650e2d39
FI
390 pnfs_ld_write_done(wdata);
391}
392
393/* Called when last of bios associated with a bl_write_pagelist call finishes */
394static void
395bl_end_par_io_write(void *data)
396{
397 struct nfs_write_data *wdata = data;
398
399 /* STUB - ignoring error handling */
400 wdata->task.tk_status = 0;
401 wdata->verf.committed = NFS_FILE_SYNC;
402 INIT_WORK(&wdata->task.u.tk_work, bl_write_cleanup);
403 schedule_work(&wdata->task.u.tk_work);
404}
405
155e7524 406static enum pnfs_try_status
650e2d39 407bl_write_pagelist(struct nfs_write_data *wdata, int sync)
155e7524 408{
650e2d39
FI
409 int i;
410 struct bio *bio = NULL;
411 struct pnfs_block_extent *be = NULL;
412 sector_t isect, extent_length = 0;
413 struct parallel_io *par;
414 loff_t offset = wdata->args.offset;
415 size_t count = wdata->args.count;
416 struct page **pages = wdata->args.pages;
417 int pg_index = wdata->args.pgbase >> PAGE_CACHE_SHIFT;
418
419 dprintk("%s enter, %Zu@%lld\n", __func__, count, offset);
420 /* At this point, wdata->pages is a (sequential) list of nfs_pages.
421 * We want to write each, and if there is an error remove it from
422 * list and call
423 * nfs_retry_request(req) to have it redone using nfs.
424 * QUEST? Do as block or per req? Think have to do per block
425 * as part of end_bio
426 */
427 par = alloc_parallel(wdata);
428 if (!par)
429 return PNFS_NOT_ATTEMPTED;
430 par->call_ops = *wdata->mds_ops;
431 par->call_ops.rpc_call_done = bl_rpc_do_nothing;
432 par->pnfs_callback = bl_end_par_io_write;
433 /* At this point, have to be more careful with error handling */
434
435 isect = (sector_t) ((offset & (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
436 for (i = pg_index; i < wdata->npages ; i++) {
437 if (!extent_length) {
438 /* We've used up the previous extent */
439 bl_put_extent(be);
440 bio = bl_submit_bio(WRITE, bio);
441 /* Get the next one */
442 be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg),
443 isect, NULL);
444 if (!be || !is_writable(be, isect)) {
445 wdata->pnfs_error = -ENOMEM;
446 goto out;
447 }
448 extent_length = be->be_length -
449 (isect - be->be_f_offset);
450 }
451 for (;;) {
452 if (!bio) {
453 bio = bio_alloc(GFP_NOIO, wdata->npages - i);
454 if (!bio) {
455 wdata->pnfs_error = -ENOMEM;
456 goto out;
457 }
458 bio->bi_sector = isect - be->be_f_offset +
459 be->be_v_offset;
460 bio->bi_bdev = be->be_mdev;
461 bio->bi_end_io = bl_end_io_write;
462 bio->bi_private = par;
463 }
464 if (bio_add_page(bio, pages[i], PAGE_SIZE, 0))
465 break;
466 bio = bl_submit_bio(WRITE, bio);
467 }
468 isect += PAGE_CACHE_SECTORS;
469 extent_length -= PAGE_CACHE_SECTORS;
470 }
471 wdata->res.count = (isect << SECTOR_SHIFT) - (offset);
472 if (count < wdata->res.count)
473 wdata->res.count = count;
474out:
475 bl_put_extent(be);
476 bl_submit_bio(WRITE, bio);
477 put_parallel(par);
478 return PNFS_ATTEMPTED;
155e7524
FI
479}
480
9e692969 481/* FIXME - range ignored */
155e7524 482static void
9e692969 483release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range)
155e7524 484{
9e692969
FI
485 int i;
486 struct pnfs_block_extent *be;
487
488 spin_lock(&bl->bl_ext_lock);
489 for (i = 0; i < EXTENT_LISTS; i++) {
490 while (!list_empty(&bl->bl_extents[i])) {
491 be = list_first_entry(&bl->bl_extents[i],
492 struct pnfs_block_extent,
493 be_node);
494 list_del(&be->be_node);
495 bl_put_extent(be);
496 }
497 }
498 spin_unlock(&bl->bl_ext_lock);
155e7524
FI
499}
500
155e7524
FI
501static void
502release_inval_marks(struct pnfs_inval_markings *marks)
503{
c1c2a4cd
FI
504 struct pnfs_inval_tracking *pos, *temp;
505
506 list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) {
507 list_del(&pos->it_link);
508 kfree(pos);
509 }
155e7524
FI
510 return;
511}
512
513static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
514{
515 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
516
517 dprintk("%s enter\n", __func__);
518 release_extents(bl, NULL);
519 release_inval_marks(&bl->bl_inval);
520 kfree(bl);
521}
522
523static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode,
524 gfp_t gfp_flags)
525{
526 struct pnfs_block_layout *bl;
527
528 dprintk("%s enter\n", __func__);
529 bl = kzalloc(sizeof(*bl), gfp_flags);
530 if (!bl)
531 return NULL;
532 spin_lock_init(&bl->bl_ext_lock);
533 INIT_LIST_HEAD(&bl->bl_extents[0]);
534 INIT_LIST_HEAD(&bl->bl_extents[1]);
535 INIT_LIST_HEAD(&bl->bl_commit);
536 INIT_LIST_HEAD(&bl->bl_committing);
537 bl->bl_count = 0;
538 bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT;
539 BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize);
540 return &bl->bl_layout;
541}
542
a60d2ebd 543static void bl_free_lseg(struct pnfs_layout_segment *lseg)
155e7524 544{
a60d2ebd
FI
545 dprintk("%s enter\n", __func__);
546 kfree(lseg);
155e7524
FI
547}
548
a60d2ebd
FI
549/* We pretty much ignore lseg, and store all data layout wide, so we
550 * can correctly merge.
551 */
552static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo,
553 struct nfs4_layoutget_res *lgr,
554 gfp_t gfp_flags)
155e7524 555{
a60d2ebd
FI
556 struct pnfs_layout_segment *lseg;
557 int status;
558
559 dprintk("%s enter\n", __func__);
560 lseg = kzalloc(sizeof(*lseg), gfp_flags);
561 if (!lseg)
562 return ERR_PTR(-ENOMEM);
563 status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags);
564 if (status) {
565 /* We don't want to call the full-blown bl_free_lseg,
566 * since on error extents were not touched.
567 */
568 kfree(lseg);
569 return ERR_PTR(status);
570 }
571 return lseg;
155e7524
FI
572}
573
574static void
575bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr,
576 const struct nfs4_layoutcommit_args *arg)
577{
90ace12a
FI
578 dprintk("%s enter\n", __func__);
579 encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg);
155e7524
FI
580}
581
582static void
583bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
584{
b2be7811
FI
585 struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout;
586
587 dprintk("%s enter\n", __func__);
588 clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status);
155e7524
FI
589}
590
2f9fd182
FI
591static void free_blk_mountid(struct block_mount_id *mid)
592{
593 if (mid) {
594 struct pnfs_block_dev *dev;
595 spin_lock(&mid->bm_lock);
596 while (!list_empty(&mid->bm_devlist)) {
597 dev = list_first_entry(&mid->bm_devlist,
598 struct pnfs_block_dev,
599 bm_node);
600 list_del(&dev->bm_node);
601 bl_free_block_dev(dev);
602 }
603 spin_unlock(&mid->bm_lock);
604 kfree(mid);
605 }
606}
607
608/* This is mostly copied from the filelayout's get_device_info function.
609 * It seems much of this should be at the generic pnfs level.
610 */
611static struct pnfs_block_dev *
612nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh,
613 struct nfs4_deviceid *d_id)
614{
615 struct pnfs_device *dev;
616 struct pnfs_block_dev *rv = NULL;
617 u32 max_resp_sz;
618 int max_pages;
619 struct page **pages = NULL;
620 int i, rc;
621
622 /*
623 * Use the session max response size as the basis for setting
624 * GETDEVICEINFO's maxcount
625 */
626 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
627 max_pages = max_resp_sz >> PAGE_SHIFT;
628 dprintk("%s max_resp_sz %u max_pages %d\n",
629 __func__, max_resp_sz, max_pages);
630
631 dev = kmalloc(sizeof(*dev), GFP_NOFS);
632 if (!dev) {
633 dprintk("%s kmalloc failed\n", __func__);
634 return NULL;
635 }
636
637 pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS);
638 if (pages == NULL) {
639 kfree(dev);
640 return NULL;
641 }
642 for (i = 0; i < max_pages; i++) {
643 pages[i] = alloc_page(GFP_NOFS);
644 if (!pages[i])
645 goto out_free;
646 }
647
648 memcpy(&dev->dev_id, d_id, sizeof(*d_id));
649 dev->layout_type = LAYOUT_BLOCK_VOLUME;
650 dev->pages = pages;
651 dev->pgbase = 0;
652 dev->pglen = PAGE_SIZE * max_pages;
653 dev->mincount = 0;
654
655 dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data);
656 rc = nfs4_proc_getdeviceinfo(server, dev);
657 dprintk("%s getdevice info returns %d\n", __func__, rc);
658 if (rc)
659 goto out_free;
660
661 rv = nfs4_blk_decode_device(server, dev);
662 out_free:
663 for (i = 0; i < max_pages; i++)
664 __free_page(pages[i]);
665 kfree(pages);
666 kfree(dev);
667 return rv;
668}
669
155e7524
FI
670static int
671bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
672{
2f9fd182
FI
673 struct block_mount_id *b_mt_id = NULL;
674 struct pnfs_devicelist *dlist = NULL;
675 struct pnfs_block_dev *bdev;
676 LIST_HEAD(block_disklist);
677 int status = 0, i;
678
155e7524 679 dprintk("%s enter\n", __func__);
2f9fd182
FI
680
681 if (server->pnfs_blksize == 0) {
682 dprintk("%s Server did not return blksize\n", __func__);
683 return -EINVAL;
684 }
685 b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS);
686 if (!b_mt_id) {
687 status = -ENOMEM;
688 goto out_error;
689 }
690 /* Initialize nfs4 block layout mount id */
691 spin_lock_init(&b_mt_id->bm_lock);
692 INIT_LIST_HEAD(&b_mt_id->bm_devlist);
693
694 dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS);
695 if (!dlist) {
696 status = -ENOMEM;
697 goto out_error;
698 }
699 dlist->eof = 0;
700 while (!dlist->eof) {
701 status = nfs4_proc_getdevicelist(server, fh, dlist);
702 if (status)
703 goto out_error;
704 dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n",
705 __func__, dlist->num_devs, dlist->eof);
706 for (i = 0; i < dlist->num_devs; i++) {
707 bdev = nfs4_blk_get_deviceinfo(server, fh,
708 &dlist->dev_id[i]);
709 if (!bdev) {
710 status = -ENODEV;
711 goto out_error;
712 }
713 spin_lock(&b_mt_id->bm_lock);
714 list_add(&bdev->bm_node, &b_mt_id->bm_devlist);
715 spin_unlock(&b_mt_id->bm_lock);
716 }
717 }
718 dprintk("%s SUCCESS\n", __func__);
719 server->pnfs_ld_data = b_mt_id;
720
721 out_return:
722 kfree(dlist);
723 return status;
724
725 out_error:
726 free_blk_mountid(b_mt_id);
727 goto out_return;
155e7524
FI
728}
729
730static int
731bl_clear_layoutdriver(struct nfs_server *server)
732{
2f9fd182
FI
733 struct block_mount_id *b_mt_id = server->pnfs_ld_data;
734
155e7524 735 dprintk("%s enter\n", __func__);
2f9fd182
FI
736 free_blk_mountid(b_mt_id);
737 dprintk("%s RETURNS\n", __func__);
155e7524
FI
738 return 0;
739}
740
e9643fe8
BH
741static const struct nfs_pageio_ops bl_pg_read_ops = {
742 .pg_init = pnfs_generic_pg_init_read,
743 .pg_test = pnfs_generic_pg_test,
744 .pg_doio = pnfs_generic_pg_readpages,
745};
746
747static const struct nfs_pageio_ops bl_pg_write_ops = {
748 .pg_init = pnfs_generic_pg_init_write,
749 .pg_test = pnfs_generic_pg_test,
750 .pg_doio = pnfs_generic_pg_writepages,
751};
752
155e7524
FI
753static struct pnfs_layoutdriver_type blocklayout_type = {
754 .id = LAYOUT_BLOCK_VOLUME,
755 .name = "LAYOUT_BLOCK_VOLUME",
756 .read_pagelist = bl_read_pagelist,
757 .write_pagelist = bl_write_pagelist,
758 .alloc_layout_hdr = bl_alloc_layout_hdr,
759 .free_layout_hdr = bl_free_layout_hdr,
760 .alloc_lseg = bl_alloc_lseg,
761 .free_lseg = bl_free_lseg,
762 .encode_layoutcommit = bl_encode_layoutcommit,
763 .cleanup_layoutcommit = bl_cleanup_layoutcommit,
764 .set_layoutdriver = bl_set_layoutdriver,
765 .clear_layoutdriver = bl_clear_layoutdriver,
e9643fe8
BH
766 .pg_read_ops = &bl_pg_read_ops,
767 .pg_write_ops = &bl_pg_write_ops,
155e7524
FI
768};
769
fe0a9b74
JR
770static const struct rpc_pipe_ops bl_upcall_ops = {
771 .upcall = bl_pipe_upcall,
772 .downcall = bl_pipe_downcall,
773 .destroy_msg = bl_pipe_destroy_msg,
774};
775
155e7524
FI
776static int __init nfs4blocklayout_init(void)
777{
fe0a9b74
JR
778 struct vfsmount *mnt;
779 struct path path;
155e7524
FI
780 int ret;
781
782 dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
783
784 ret = pnfs_register_layoutdriver(&blocklayout_type);
fe0a9b74
JR
785 if (ret)
786 goto out;
787
788 init_waitqueue_head(&bl_wq);
789
790 mnt = rpc_get_mount();
791 if (IS_ERR(mnt)) {
792 ret = PTR_ERR(mnt);
793 goto out_remove;
794 }
795
796 ret = vfs_path_lookup(mnt->mnt_root,
797 mnt,
798 NFS_PIPE_DIRNAME, 0, &path);
799 if (ret)
800 goto out_remove;
801
802 bl_device_pipe = rpc_mkpipe(path.dentry, "blocklayout", NULL,
803 &bl_upcall_ops, 0);
804 if (IS_ERR(bl_device_pipe)) {
805 ret = PTR_ERR(bl_device_pipe);
806 goto out_remove;
807 }
808out:
809 return ret;
810
811out_remove:
812 pnfs_unregister_layoutdriver(&blocklayout_type);
155e7524
FI
813 return ret;
814}
815
816static void __exit nfs4blocklayout_exit(void)
817{
818 dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
819 __func__);
820
821 pnfs_unregister_layoutdriver(&blocklayout_type);
fe0a9b74 822 rpc_unlink(bl_device_pipe);
155e7524
FI
823}
824
825MODULE_ALIAS("nfs-layouttype4-3");
826
827module_init(nfs4blocklayout_init);
828module_exit(nfs4blocklayout_exit);