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.
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/mount.h>
36 #include <linux/namei.h>
37 #include <linux/bio.h> /* struct bio */
38 #include <linux/buffer_head.h> /* various write calls */
39 #include <linux/prefetch.h>
41 #include "blocklayout.h"
43 #define NFSDBG_FACILITY NFSDBG_PNFS_LD
45 MODULE_LICENSE("GPL");
46 MODULE_AUTHOR("Andy Adamson <andros@citi.umich.edu>");
47 MODULE_DESCRIPTION("The NFSv4.1 pNFS Block layout driver");
49 static void print_page(struct page *page)
51 dprintk("PRINTPAGE page %p\n", page);
52 dprintk(" PagePrivate %d\n", PagePrivate(page));
53 dprintk(" PageUptodate %d\n", PageUptodate(page));
54 dprintk(" PageError %d\n", PageError(page));
55 dprintk(" PageDirty %d\n", PageDirty(page));
56 dprintk(" PageReferenced %d\n", PageReferenced(page));
57 dprintk(" PageLocked %d\n", PageLocked(page));
58 dprintk(" PageWriteback %d\n", PageWriteback(page));
59 dprintk(" PageMappedToDisk %d\n", PageMappedToDisk(page));
63 /* Given the be associated with isect, determine if page data needs to be
66 static int is_hole(struct pnfs_block_extent *be, sector_t isect)
68 if (be->be_state == PNFS_BLOCK_NONE_DATA)
70 else if (be->be_state != PNFS_BLOCK_INVALID_DATA)
73 return !bl_is_sector_init(be->be_inval, isect);
76 /* Given the be associated with isect, determine if page data can be
79 static int is_writable(struct pnfs_block_extent *be, sector_t isect)
81 return (be->be_state == PNFS_BLOCK_READWRITE_DATA ||
82 be->be_state == PNFS_BLOCK_INVALID_DATA);
85 /* The data we are handed might be spread across several bios. We need
86 * to track when the last one is finished.
90 void (*pnfs_callback) (void *data, int num_se);
95 static inline struct parallel_io *alloc_parallel(void *data)
97 struct parallel_io *rv;
99 rv = kmalloc(sizeof(*rv), GFP_NOFS);
102 kref_init(&rv->refcnt);
108 static inline void get_parallel(struct parallel_io *p)
110 kref_get(&p->refcnt);
113 static void destroy_parallel(struct kref *kref)
115 struct parallel_io *p = container_of(kref, struct parallel_io, refcnt);
117 dprintk("%s enter\n", __func__);
118 p->pnfs_callback(p->data, p->bse_count);
122 static inline void put_parallel(struct parallel_io *p)
124 kref_put(&p->refcnt, destroy_parallel);
128 bl_submit_bio(int rw, struct bio *bio)
131 get_parallel(bio->bi_private);
132 dprintk("%s submitting %s bio %u@%llu\n", __func__,
133 rw == READ ? "read" : "write",
134 bio->bi_size, (unsigned long long)bio->bi_sector);
140 static struct bio *bl_alloc_init_bio(int npg, sector_t isect,
141 struct pnfs_block_extent *be,
142 void (*end_io)(struct bio *, int err),
143 struct parallel_io *par)
147 npg = min(npg, BIO_MAX_PAGES);
148 bio = bio_alloc(GFP_NOIO, npg);
149 if (!bio && (current->flags & PF_MEMALLOC)) {
150 while (!bio && (npg /= 2))
151 bio = bio_alloc(GFP_NOIO, npg);
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;
163 static struct bio *bl_add_page_to_bio(struct bio *bio, int npg, int rw,
164 sector_t isect, struct page *page,
165 struct pnfs_block_extent *be,
166 void (*end_io)(struct bio *, int err),
167 struct parallel_io *par)
171 bio = bl_alloc_init_bio(npg, isect, be, end_io, par);
173 return ERR_PTR(-ENOMEM);
175 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
176 bio = bl_submit_bio(rw, bio);
182 /* This is basically copied from mpage_end_io_read */
183 static void bl_end_io_read(struct bio *bio, int err)
185 struct parallel_io *par = bio->bi_private;
186 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
187 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
188 struct nfs_read_data *rdata = (struct nfs_read_data *)par->data;
191 struct page *page = bvec->bv_page;
193 if (--bvec >= bio->bi_io_vec)
194 prefetchw(&bvec->bv_page->flags);
196 SetPageUptodate(page);
197 } while (bvec >= bio->bi_io_vec);
199 if (!rdata->pnfs_error)
200 rdata->pnfs_error = -EIO;
201 pnfs_set_lo_fail(rdata->lseg);
207 static void bl_read_cleanup(struct work_struct *work)
209 struct rpc_task *task;
210 struct nfs_read_data *rdata;
211 dprintk("%s enter\n", __func__);
212 task = container_of(work, struct rpc_task, u.tk_work);
213 rdata = container_of(task, struct nfs_read_data, task);
214 pnfs_ld_read_done(rdata);
218 bl_end_par_io_read(void *data, int unused)
220 struct nfs_read_data *rdata = data;
222 rdata->task.tk_status = rdata->pnfs_error;
223 INIT_WORK(&rdata->task.u.tk_work, bl_read_cleanup);
224 schedule_work(&rdata->task.u.tk_work);
227 static enum pnfs_try_status
228 bl_read_pagelist(struct nfs_read_data *rdata)
231 struct bio *bio = NULL;
232 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
233 sector_t isect, extent_length = 0;
234 struct parallel_io *par;
235 loff_t f_offset = rdata->args.offset;
236 struct page **pages = rdata->args.pages;
237 int pg_index = rdata->args.pgbase >> PAGE_CACHE_SHIFT;
239 dprintk("%s enter nr_pages %u offset %lld count %u\n", __func__,
240 rdata->npages, f_offset, (unsigned int)rdata->args.count);
242 par = alloc_parallel(rdata);
245 par->pnfs_callback = bl_end_par_io_read;
246 /* At this point, we can no longer jump to use_mds */
248 isect = (sector_t) (f_offset >> SECTOR_SHIFT);
249 /* Code assumes extents are page-aligned */
250 for (i = pg_index; i < rdata->npages; i++) {
251 if (!extent_length) {
252 /* We've used up the previous extent */
254 bl_put_extent(cow_read);
255 bio = bl_submit_bio(READ, bio);
256 /* Get the next one */
257 be = bl_find_get_extent(BLK_LSEG2EXT(rdata->lseg),
260 rdata->pnfs_error = -EIO;
263 extent_length = be->be_length -
264 (isect - be->be_f_offset);
266 sector_t cow_length = cow_read->be_length -
267 (isect - cow_read->be_f_offset);
268 extent_length = min(extent_length, cow_length);
271 hole = is_hole(be, isect);
272 if (hole && !cow_read) {
273 bio = bl_submit_bio(READ, bio);
274 /* Fill hole w/ zeroes w/o accessing device */
275 dprintk("%s Zeroing page for hole\n", __func__);
276 zero_user_segment(pages[i], 0, PAGE_CACHE_SIZE);
277 print_page(pages[i]);
278 SetPageUptodate(pages[i]);
280 struct pnfs_block_extent *be_read;
282 be_read = (hole && cow_read) ? cow_read : be;
283 bio = bl_add_page_to_bio(bio, rdata->npages - i, READ,
284 isect, pages[i], be_read,
285 bl_end_io_read, par);
287 rdata->pnfs_error = PTR_ERR(bio);
292 isect += PAGE_CACHE_SECTORS;
293 extent_length -= PAGE_CACHE_SECTORS;
295 if ((isect << SECTOR_SHIFT) >= rdata->inode->i_size) {
297 rdata->res.count = rdata->inode->i_size - f_offset;
299 rdata->res.count = (isect << SECTOR_SHIFT) - f_offset;
303 bl_put_extent(cow_read);
304 bl_submit_bio(READ, bio);
306 return PNFS_ATTEMPTED;
309 dprintk("Giving up and using normal NFS\n");
310 return PNFS_NOT_ATTEMPTED;
313 static void mark_extents_written(struct pnfs_block_layout *bl,
314 __u64 offset, __u32 count)
317 struct pnfs_block_extent *be;
318 struct pnfs_block_short_extent *se;
320 dprintk("%s(%llu, %u)\n", __func__, offset, count);
323 isect = (offset & (long)(PAGE_CACHE_MASK)) >> SECTOR_SHIFT;
324 end = (offset + count + PAGE_CACHE_SIZE - 1) & (long)(PAGE_CACHE_MASK);
325 end >>= SECTOR_SHIFT;
326 while (isect < end) {
328 be = bl_find_get_extent(bl, isect, NULL);
329 BUG_ON(!be); /* FIXME */
330 len = min(end, be->be_f_offset + be->be_length) - isect;
331 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
332 se = bl_pop_one_short_extent(be->be_inval);
334 bl_mark_for_commit(be, isect, len, se);
341 static void bl_end_io_write_zero(struct bio *bio, int err)
343 struct parallel_io *par = bio->bi_private;
344 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
345 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
346 struct nfs_write_data *wdata = (struct nfs_write_data *)par->data;
349 struct page *page = bvec->bv_page;
351 if (--bvec >= bio->bi_io_vec)
352 prefetchw(&bvec->bv_page->flags);
353 /* This is the zeroing page we added */
354 end_page_writeback(page);
355 page_cache_release(page);
356 } while (bvec >= bio->bi_io_vec);
358 if (unlikely(!uptodate)) {
359 if (!wdata->pnfs_error)
360 wdata->pnfs_error = -EIO;
361 pnfs_set_lo_fail(wdata->lseg);
367 static void bl_end_io_write(struct bio *bio, int err)
369 struct parallel_io *par = bio->bi_private;
370 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
371 struct nfs_write_data *wdata = (struct nfs_write_data *)par->data;
374 if (!wdata->pnfs_error)
375 wdata->pnfs_error = -EIO;
376 pnfs_set_lo_fail(wdata->lseg);
382 /* Function scheduled for call during bl_end_par_io_write,
383 * it marks sectors as written and extends the commitlist.
385 static void bl_write_cleanup(struct work_struct *work)
387 struct rpc_task *task;
388 struct nfs_write_data *wdata;
389 dprintk("%s enter\n", __func__);
390 task = container_of(work, struct rpc_task, u.tk_work);
391 wdata = container_of(task, struct nfs_write_data, task);
392 if (likely(!wdata->pnfs_error)) {
393 /* Marks for LAYOUTCOMMIT */
394 mark_extents_written(BLK_LSEG2EXT(wdata->lseg),
395 wdata->args.offset, wdata->args.count);
397 pnfs_ld_write_done(wdata);
400 /* Called when last of bios associated with a bl_write_pagelist call finishes */
401 static void bl_end_par_io_write(void *data, int num_se)
403 struct nfs_write_data *wdata = data;
405 if (unlikely(wdata->pnfs_error)) {
406 bl_free_short_extents(&BLK_LSEG2EXT(wdata->lseg)->bl_inval,
410 wdata->task.tk_status = wdata->pnfs_error;
411 wdata->verf.committed = NFS_FILE_SYNC;
412 INIT_WORK(&wdata->task.u.tk_work, bl_write_cleanup);
413 schedule_work(&wdata->task.u.tk_work);
416 /* FIXME STUB - mark intersection of layout and page as bad, so is not
419 static void mark_bad_read(void)
425 * map_block: map a requested I/0 block (isect) into an offset in the LVM
429 map_block(struct buffer_head *bh, sector_t isect, struct pnfs_block_extent *be)
431 dprintk("%s enter be=%p\n", __func__, be);
433 set_buffer_mapped(bh);
434 bh->b_bdev = be->be_mdev;
435 bh->b_blocknr = (isect - be->be_f_offset + be->be_v_offset) >>
436 (be->be_mdev->bd_inode->i_blkbits - SECTOR_SHIFT);
438 dprintk("%s isect %llu, bh->b_blocknr %ld, using bsize %Zd\n",
439 __func__, (unsigned long long)isect, (long)bh->b_blocknr,
444 /* Given an unmapped page, zero it or read in page for COW, page is locked
448 init_page_for_write(struct page *page, struct pnfs_block_extent *cow_read)
450 struct buffer_head *bh = NULL;
454 dprintk("%s enter, %p\n", __func__, page);
455 BUG_ON(PageUptodate(page));
457 zero_user_segment(page, 0, PAGE_SIZE);
458 SetPageUptodate(page);
462 bh = alloc_page_buffers(page, PAGE_CACHE_SIZE, 0);
468 isect = (sector_t) page->index << PAGE_CACHE_SECTOR_SHIFT;
469 map_block(bh, isect, cow_read);
470 if (!bh_uptodate_or_lock(bh))
471 ret = bh_submit_read(bh);
474 SetPageUptodate(page);
477 bl_put_extent(cow_read);
479 free_buffer_head(bh);
481 /* Need to mark layout with bad read...should now
482 * just use nfs4 for reads and writes.
489 /* Find or create a zeroing page marked being writeback.
490 * Return ERR_PTR on error, NULL to indicate skip this page and page itself
491 * to indicate write out.
494 bl_find_get_zeroing_page(struct inode *inode, pgoff_t index,
495 struct pnfs_block_extent *cow_read)
499 page = find_get_page(inode->i_mapping, index);
503 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
504 if (unlikely(!page)) {
505 dprintk("%s oom\n", __func__);
506 return ERR_PTR(-ENOMEM);
511 /* PageDirty: Other will write this out
512 * PageWriteback: Other is writing this out
513 * PageUptodate: It was read before
515 if (PageDirty(page) || PageWriteback(page)) {
519 page_cache_release(page);
528 if (!PageUptodate(page)) {
529 /* New page, readin or zero it */
530 init_page_for_write(page, cow_read);
532 set_page_writeback(page);
538 static enum pnfs_try_status
539 bl_write_pagelist(struct nfs_write_data *wdata, int sync)
541 int i, ret, npg_zero, pg_index, last = 0;
542 struct bio *bio = NULL;
543 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
544 sector_t isect, last_isect = 0, extent_length = 0;
545 struct parallel_io *par;
546 loff_t offset = wdata->args.offset;
547 size_t count = wdata->args.count;
548 struct page **pages = wdata->args.pages;
553 NFS_SERVER(wdata->inode)->pnfs_blksize >> PAGE_CACHE_SHIFT;
555 dprintk("%s enter, %Zu@%lld\n", __func__, count, offset);
556 /* At this point, wdata->pages is a (sequential) list of nfs_pages.
557 * We want to write each, and if there is an error set pnfs_error
558 * to have it redone using nfs.
560 par = alloc_parallel(wdata);
563 par->pnfs_callback = bl_end_par_io_write;
564 /* At this point, have to be more careful with error handling */
566 isect = (sector_t) ((offset & (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
567 be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg), isect, &cow_read);
568 if (!be || !is_writable(be, isect)) {
569 dprintk("%s no matching extents!\n", __func__);
573 /* First page inside INVALID extent */
574 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
575 if (likely(!bl_push_one_short_extent(be->be_inval)))
579 temp = offset >> PAGE_CACHE_SHIFT;
580 npg_zero = do_div(temp, npg_per_block);
581 isect = (sector_t) (((offset - npg_zero * PAGE_CACHE_SIZE) &
582 (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
583 extent_length = be->be_length - (isect - be->be_f_offset);
586 dprintk("%s need to zero %d pages\n", __func__, npg_zero);
587 for (;npg_zero > 0; npg_zero--) {
588 if (bl_is_sector_init(be->be_inval, isect)) {
589 dprintk("isect %llu already init\n",
590 (unsigned long long)isect);
593 /* page ref released in bl_end_io_write_zero */
594 index = isect >> PAGE_CACHE_SECTOR_SHIFT;
595 dprintk("%s zero %dth page: index %lu isect %llu\n",
596 __func__, npg_zero, index,
597 (unsigned long long)isect);
598 page = bl_find_get_zeroing_page(wdata->inode, index,
600 if (unlikely(IS_ERR(page))) {
601 wdata->pnfs_error = PTR_ERR(page);
603 } else if (page == NULL)
606 ret = bl_mark_sectors_init(be->be_inval, isect,
609 dprintk("%s bl_mark_sectors_init fail %d\n",
611 end_page_writeback(page);
612 page_cache_release(page);
613 wdata->pnfs_error = ret;
616 if (likely(!bl_push_one_short_extent(be->be_inval)))
619 end_page_writeback(page);
620 page_cache_release(page);
621 wdata->pnfs_error = -ENOMEM;
624 /* FIXME: This should be done in bi_end_io */
625 mark_extents_written(BLK_LSEG2EXT(wdata->lseg),
626 page->index << PAGE_CACHE_SHIFT,
629 bio = bl_add_page_to_bio(bio, npg_zero, WRITE,
631 bl_end_io_write_zero, par);
633 wdata->pnfs_error = PTR_ERR(bio);
638 isect += PAGE_CACHE_SECTORS;
639 extent_length -= PAGE_CACHE_SECTORS;
644 bio = bl_submit_bio(WRITE, bio);
647 pg_index = wdata->args.pgbase >> PAGE_CACHE_SHIFT;
648 for (i = pg_index; i < wdata->npages; i++) {
649 if (!extent_length) {
650 /* We've used up the previous extent */
652 bio = bl_submit_bio(WRITE, bio);
653 /* Get the next one */
654 be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg),
656 if (!be || !is_writable(be, isect)) {
657 wdata->pnfs_error = -EINVAL;
660 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
661 if (likely(!bl_push_one_short_extent(
665 wdata->pnfs_error = -ENOMEM;
669 extent_length = be->be_length -
670 (isect - be->be_f_offset);
672 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
673 ret = bl_mark_sectors_init(be->be_inval, isect,
676 dprintk("%s bl_mark_sectors_init fail %d\n",
678 wdata->pnfs_error = ret;
682 bio = bl_add_page_to_bio(bio, wdata->npages - i, WRITE,
684 bl_end_io_write, par);
686 wdata->pnfs_error = PTR_ERR(bio);
690 isect += PAGE_CACHE_SECTORS;
692 extent_length -= PAGE_CACHE_SECTORS;
695 /* Last page inside INVALID extent */
696 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
697 bio = bl_submit_bio(WRITE, bio);
698 temp = last_isect >> PAGE_CACHE_SECTOR_SHIFT;
699 npg_zero = npg_per_block - do_div(temp, npg_per_block);
700 if (npg_zero < npg_per_block) {
702 goto fill_invalid_ext;
707 wdata->res.count = (last_isect << SECTOR_SHIFT) - (offset);
708 if (count < wdata->res.count) {
709 wdata->res.count = count;
713 bl_submit_bio(WRITE, bio);
715 return PNFS_ATTEMPTED;
719 return PNFS_NOT_ATTEMPTED;
722 /* FIXME - range ignored */
724 release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range)
727 struct pnfs_block_extent *be;
729 spin_lock(&bl->bl_ext_lock);
730 for (i = 0; i < EXTENT_LISTS; i++) {
731 while (!list_empty(&bl->bl_extents[i])) {
732 be = list_first_entry(&bl->bl_extents[i],
733 struct pnfs_block_extent,
735 list_del(&be->be_node);
739 spin_unlock(&bl->bl_ext_lock);
743 release_inval_marks(struct pnfs_inval_markings *marks)
745 struct pnfs_inval_tracking *pos, *temp;
746 struct pnfs_block_short_extent *se, *stemp;
748 list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) {
749 list_del(&pos->it_link);
753 list_for_each_entry_safe(se, stemp, &marks->im_extents, bse_node) {
754 list_del(&se->bse_node);
760 static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
762 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
764 dprintk("%s enter\n", __func__);
765 release_extents(bl, NULL);
766 release_inval_marks(&bl->bl_inval);
770 static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode,
773 struct pnfs_block_layout *bl;
775 dprintk("%s enter\n", __func__);
776 bl = kzalloc(sizeof(*bl), gfp_flags);
779 spin_lock_init(&bl->bl_ext_lock);
780 INIT_LIST_HEAD(&bl->bl_extents[0]);
781 INIT_LIST_HEAD(&bl->bl_extents[1]);
782 INIT_LIST_HEAD(&bl->bl_commit);
783 INIT_LIST_HEAD(&bl->bl_committing);
785 bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT;
786 BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize);
787 return &bl->bl_layout;
790 static void bl_free_lseg(struct pnfs_layout_segment *lseg)
792 dprintk("%s enter\n", __func__);
796 /* We pretty much ignore lseg, and store all data layout wide, so we
797 * can correctly merge.
799 static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo,
800 struct nfs4_layoutget_res *lgr,
803 struct pnfs_layout_segment *lseg;
806 dprintk("%s enter\n", __func__);
807 lseg = kzalloc(sizeof(*lseg), gfp_flags);
809 return ERR_PTR(-ENOMEM);
810 status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags);
812 /* We don't want to call the full-blown bl_free_lseg,
813 * since on error extents were not touched.
816 return ERR_PTR(status);
822 bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr,
823 const struct nfs4_layoutcommit_args *arg)
825 dprintk("%s enter\n", __func__);
826 encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg);
830 bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
832 struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout;
834 dprintk("%s enter\n", __func__);
835 clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status);
838 static void free_blk_mountid(struct block_mount_id *mid)
841 struct pnfs_block_dev *dev, *tmp;
843 /* No need to take bm_lock as we are last user freeing bm_devlist */
844 list_for_each_entry_safe(dev, tmp, &mid->bm_devlist, bm_node) {
845 list_del(&dev->bm_node);
846 bl_free_block_dev(dev);
852 /* This is mostly copied from the filelayout's get_device_info function.
853 * It seems much of this should be at the generic pnfs level.
855 static struct pnfs_block_dev *
856 nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh,
857 struct nfs4_deviceid *d_id)
859 struct pnfs_device *dev;
860 struct pnfs_block_dev *rv;
863 struct page **pages = NULL;
867 * Use the session max response size as the basis for setting
868 * GETDEVICEINFO's maxcount
870 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
871 max_pages = max_resp_sz >> PAGE_SHIFT;
872 dprintk("%s max_resp_sz %u max_pages %d\n",
873 __func__, max_resp_sz, max_pages);
875 dev = kmalloc(sizeof(*dev), GFP_NOFS);
877 dprintk("%s kmalloc failed\n", __func__);
878 return ERR_PTR(-ENOMEM);
881 pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS);
884 return ERR_PTR(-ENOMEM);
886 for (i = 0; i < max_pages; i++) {
887 pages[i] = alloc_page(GFP_NOFS);
889 rv = ERR_PTR(-ENOMEM);
894 memcpy(&dev->dev_id, d_id, sizeof(*d_id));
895 dev->layout_type = LAYOUT_BLOCK_VOLUME;
898 dev->pglen = PAGE_SIZE * max_pages;
901 dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data);
902 rc = nfs4_proc_getdeviceinfo(server, dev);
903 dprintk("%s getdevice info returns %d\n", __func__, rc);
909 rv = nfs4_blk_decode_device(server, dev);
911 for (i = 0; i < max_pages; i++)
912 __free_page(pages[i]);
919 bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
921 struct block_mount_id *b_mt_id = NULL;
922 struct pnfs_devicelist *dlist = NULL;
923 struct pnfs_block_dev *bdev;
924 LIST_HEAD(block_disklist);
927 dprintk("%s enter\n", __func__);
929 if (server->pnfs_blksize == 0) {
930 dprintk("%s Server did not return blksize\n", __func__);
933 b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS);
938 /* Initialize nfs4 block layout mount id */
939 spin_lock_init(&b_mt_id->bm_lock);
940 INIT_LIST_HEAD(&b_mt_id->bm_devlist);
942 dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS);
948 while (!dlist->eof) {
949 status = nfs4_proc_getdevicelist(server, fh, dlist);
952 dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n",
953 __func__, dlist->num_devs, dlist->eof);
954 for (i = 0; i < dlist->num_devs; i++) {
955 bdev = nfs4_blk_get_deviceinfo(server, fh,
958 status = PTR_ERR(bdev);
961 spin_lock(&b_mt_id->bm_lock);
962 list_add(&bdev->bm_node, &b_mt_id->bm_devlist);
963 spin_unlock(&b_mt_id->bm_lock);
966 dprintk("%s SUCCESS\n", __func__);
967 server->pnfs_ld_data = b_mt_id;
974 free_blk_mountid(b_mt_id);
979 bl_clear_layoutdriver(struct nfs_server *server)
981 struct block_mount_id *b_mt_id = server->pnfs_ld_data;
983 dprintk("%s enter\n", __func__);
984 free_blk_mountid(b_mt_id);
985 dprintk("%s RETURNS\n", __func__);
989 static const struct nfs_pageio_ops bl_pg_read_ops = {
990 .pg_init = pnfs_generic_pg_init_read,
991 .pg_test = pnfs_generic_pg_test,
992 .pg_doio = pnfs_generic_pg_readpages,
995 static const struct nfs_pageio_ops bl_pg_write_ops = {
996 .pg_init = pnfs_generic_pg_init_write,
997 .pg_test = pnfs_generic_pg_test,
998 .pg_doio = pnfs_generic_pg_writepages,
1001 static struct pnfs_layoutdriver_type blocklayout_type = {
1002 .id = LAYOUT_BLOCK_VOLUME,
1003 .name = "LAYOUT_BLOCK_VOLUME",
1004 .read_pagelist = bl_read_pagelist,
1005 .write_pagelist = bl_write_pagelist,
1006 .alloc_layout_hdr = bl_alloc_layout_hdr,
1007 .free_layout_hdr = bl_free_layout_hdr,
1008 .alloc_lseg = bl_alloc_lseg,
1009 .free_lseg = bl_free_lseg,
1010 .encode_layoutcommit = bl_encode_layoutcommit,
1011 .cleanup_layoutcommit = bl_cleanup_layoutcommit,
1012 .set_layoutdriver = bl_set_layoutdriver,
1013 .clear_layoutdriver = bl_clear_layoutdriver,
1014 .pg_read_ops = &bl_pg_read_ops,
1015 .pg_write_ops = &bl_pg_write_ops,
1018 static const struct rpc_pipe_ops bl_upcall_ops = {
1019 .upcall = rpc_pipe_generic_upcall,
1020 .downcall = bl_pipe_downcall,
1021 .destroy_msg = bl_pipe_destroy_msg,
1024 static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb,
1025 struct rpc_pipe *pipe)
1027 struct dentry *dir, *dentry;
1029 dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME);
1031 return ERR_PTR(-ENOENT);
1032 dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe);
1037 static void nfs4blocklayout_unregister_sb(struct super_block *sb,
1038 struct rpc_pipe *pipe)
1041 rpc_unlink(pipe->dentry);
1044 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
1047 struct super_block *sb = ptr;
1048 struct net *net = sb->s_fs_info;
1049 struct nfs_net *nn = net_generic(net, nfs_net_id);
1050 struct dentry *dentry;
1053 if (!try_module_get(THIS_MODULE))
1056 if (nn->bl_device_pipe == NULL) {
1057 module_put(THIS_MODULE);
1062 case RPC_PIPEFS_MOUNT:
1063 dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe);
1064 if (IS_ERR(dentry)) {
1065 ret = PTR_ERR(dentry);
1068 nn->bl_device_pipe->dentry = dentry;
1070 case RPC_PIPEFS_UMOUNT:
1071 if (nn->bl_device_pipe->dentry)
1072 nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe);
1078 module_put(THIS_MODULE);
1082 static struct notifier_block nfs4blocklayout_block = {
1083 .notifier_call = rpc_pipefs_event,
1086 static struct dentry *nfs4blocklayout_register_net(struct net *net,
1087 struct rpc_pipe *pipe)
1089 struct super_block *pipefs_sb;
1090 struct dentry *dentry;
1092 pipefs_sb = rpc_get_sb_net(net);
1095 dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe);
1096 rpc_put_sb_net(net);
1100 static void nfs4blocklayout_unregister_net(struct net *net,
1101 struct rpc_pipe *pipe)
1103 struct super_block *pipefs_sb;
1105 pipefs_sb = rpc_get_sb_net(net);
1107 nfs4blocklayout_unregister_sb(pipefs_sb, pipe);
1108 rpc_put_sb_net(net);
1112 static int nfs4blocklayout_net_init(struct net *net)
1114 struct nfs_net *nn = net_generic(net, nfs_net_id);
1115 struct dentry *dentry;
1117 init_waitqueue_head(&nn->bl_wq);
1118 nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0);
1119 if (IS_ERR(nn->bl_device_pipe))
1120 return PTR_ERR(nn->bl_device_pipe);
1121 dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe);
1122 if (IS_ERR(dentry)) {
1123 rpc_destroy_pipe_data(nn->bl_device_pipe);
1124 return PTR_ERR(dentry);
1126 nn->bl_device_pipe->dentry = dentry;
1130 static void nfs4blocklayout_net_exit(struct net *net)
1132 struct nfs_net *nn = net_generic(net, nfs_net_id);
1134 nfs4blocklayout_unregister_net(net, nn->bl_device_pipe);
1135 rpc_destroy_pipe_data(nn->bl_device_pipe);
1136 nn->bl_device_pipe = NULL;
1139 static struct pernet_operations nfs4blocklayout_net_ops = {
1140 .init = nfs4blocklayout_net_init,
1141 .exit = nfs4blocklayout_net_exit,
1144 static int __init nfs4blocklayout_init(void)
1148 dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
1150 ret = pnfs_register_layoutdriver(&blocklayout_type);
1154 ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block);
1157 ret = register_pernet_subsys(&nfs4blocklayout_net_ops);
1164 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
1166 pnfs_unregister_layoutdriver(&blocklayout_type);
1170 static void __exit nfs4blocklayout_exit(void)
1172 dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
1175 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
1176 unregister_pernet_subsys(&nfs4blocklayout_net_ops);
1177 pnfs_unregister_layoutdriver(&blocklayout_type);
1180 MODULE_ALIAS("nfs-layouttype4-3");
1182 module_init(nfs4blocklayout_init);
1183 module_exit(nfs4blocklayout_exit);