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>
42 #include "../internal.h"
43 #include "blocklayout.h"
45 #define NFSDBG_FACILITY NFSDBG_PNFS_LD
47 MODULE_LICENSE("GPL");
48 MODULE_AUTHOR("Andy Adamson <andros@citi.umich.edu>");
49 MODULE_DESCRIPTION("The NFSv4.1 pNFS Block layout driver");
51 static void print_page(struct page *page)
53 dprintk("PRINTPAGE page %p\n", page);
54 dprintk(" PagePrivate %d\n", PagePrivate(page));
55 dprintk(" PageUptodate %d\n", PageUptodate(page));
56 dprintk(" PageError %d\n", PageError(page));
57 dprintk(" PageDirty %d\n", PageDirty(page));
58 dprintk(" PageReferenced %d\n", PageReferenced(page));
59 dprintk(" PageLocked %d\n", PageLocked(page));
60 dprintk(" PageWriteback %d\n", PageWriteback(page));
61 dprintk(" PageMappedToDisk %d\n", PageMappedToDisk(page));
65 /* Given the be associated with isect, determine if page data needs to be
68 static int is_hole(struct pnfs_block_extent *be, sector_t isect)
70 if (be->be_state == PNFS_BLOCK_NONE_DATA)
72 else if (be->be_state != PNFS_BLOCK_INVALID_DATA)
75 return !bl_is_sector_init(be->be_inval, isect);
78 /* Given the be associated with isect, determine if page data can be
81 static int is_writable(struct pnfs_block_extent *be, sector_t isect)
83 return (be->be_state == PNFS_BLOCK_READWRITE_DATA ||
84 be->be_state == PNFS_BLOCK_INVALID_DATA);
87 /* The data we are handed might be spread across several bios. We need
88 * to track when the last one is finished.
92 void (*pnfs_callback) (void *data, int num_se);
97 static inline struct parallel_io *alloc_parallel(void *data)
99 struct parallel_io *rv;
101 rv = kmalloc(sizeof(*rv), GFP_NOFS);
104 kref_init(&rv->refcnt);
110 static inline void get_parallel(struct parallel_io *p)
112 kref_get(&p->refcnt);
115 static void destroy_parallel(struct kref *kref)
117 struct parallel_io *p = container_of(kref, struct parallel_io, refcnt);
119 dprintk("%s enter\n", __func__);
120 p->pnfs_callback(p->data, p->bse_count);
124 static inline void put_parallel(struct parallel_io *p)
126 kref_put(&p->refcnt, destroy_parallel);
130 bl_submit_bio(int rw, struct bio *bio)
133 get_parallel(bio->bi_private);
134 dprintk("%s submitting %s bio %u@%llu\n", __func__,
135 rw == READ ? "read" : "write",
136 bio->bi_size, (unsigned long long)bio->bi_sector);
142 static struct bio *bl_alloc_init_bio(int npg, sector_t isect,
143 struct pnfs_block_extent *be,
144 void (*end_io)(struct bio *, int err),
145 struct parallel_io *par)
149 npg = min(npg, BIO_MAX_PAGES);
150 bio = bio_alloc(GFP_NOIO, npg);
151 if (!bio && (current->flags & PF_MEMALLOC)) {
152 while (!bio && (npg /= 2))
153 bio = bio_alloc(GFP_NOIO, npg);
157 bio->bi_sector = isect - be->be_f_offset + be->be_v_offset;
158 bio->bi_bdev = be->be_mdev;
159 bio->bi_end_io = end_io;
160 bio->bi_private = par;
165 static struct bio *do_add_page_to_bio(struct bio *bio, int npg, int rw,
166 sector_t isect, struct page *page,
167 struct pnfs_block_extent *be,
168 void (*end_io)(struct bio *, int err),
169 struct parallel_io *par,
170 unsigned int offset, int len)
172 isect = isect + (offset >> SECTOR_SHIFT);
173 dprintk("%s: npg %d rw %d isect %llu offset %u len %d\n", __func__,
174 npg, rw, (unsigned long long)isect, offset, len);
177 bio = bl_alloc_init_bio(npg, isect, be, end_io, par);
179 return ERR_PTR(-ENOMEM);
181 if (bio_add_page(bio, page, len, offset) < len) {
182 bio = bl_submit_bio(rw, bio);
188 static struct bio *bl_add_page_to_bio(struct bio *bio, int npg, int rw,
189 sector_t isect, struct page *page,
190 struct pnfs_block_extent *be,
191 void (*end_io)(struct bio *, int err),
192 struct parallel_io *par)
194 return do_add_page_to_bio(bio, npg, rw, isect, page, be,
195 end_io, par, 0, PAGE_CACHE_SIZE);
198 /* This is basically copied from mpage_end_io_read */
199 static void bl_end_io_read(struct bio *bio, int err)
201 struct parallel_io *par = bio->bi_private;
202 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
203 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
206 struct page *page = bvec->bv_page;
208 if (--bvec >= bio->bi_io_vec)
209 prefetchw(&bvec->bv_page->flags);
211 SetPageUptodate(page);
212 } while (bvec >= bio->bi_io_vec);
214 struct nfs_read_data *rdata = par->data;
215 struct nfs_pgio_header *header = rdata->header;
217 if (!header->pnfs_error)
218 header->pnfs_error = -EIO;
219 pnfs_set_lo_fail(header->lseg);
225 static void bl_read_cleanup(struct work_struct *work)
227 struct rpc_task *task;
228 struct nfs_read_data *rdata;
229 dprintk("%s enter\n", __func__);
230 task = container_of(work, struct rpc_task, u.tk_work);
231 rdata = container_of(task, struct nfs_read_data, task);
232 pnfs_ld_read_done(rdata);
236 bl_end_par_io_read(void *data, int unused)
238 struct nfs_read_data *rdata = data;
240 rdata->task.tk_status = rdata->header->pnfs_error;
241 INIT_WORK(&rdata->task.u.tk_work, bl_read_cleanup);
242 schedule_work(&rdata->task.u.tk_work);
245 static enum pnfs_try_status
246 bl_read_pagelist(struct nfs_read_data *rdata)
248 struct nfs_pgio_header *header = rdata->header;
250 struct bio *bio = NULL;
251 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
252 sector_t isect, extent_length = 0;
253 struct parallel_io *par;
254 loff_t f_offset = rdata->args.offset;
255 size_t bytes_left = rdata->args.count;
256 unsigned int pg_offset, pg_len;
257 struct page **pages = rdata->args.pages;
258 int pg_index = rdata->args.pgbase >> PAGE_CACHE_SHIFT;
259 const bool is_dio = (header->dreq != NULL);
261 dprintk("%s enter nr_pages %u offset %lld count %u\n", __func__,
262 rdata->pages.npages, f_offset, (unsigned int)rdata->args.count);
264 par = alloc_parallel(rdata);
267 par->pnfs_callback = bl_end_par_io_read;
268 /* At this point, we can no longer jump to use_mds */
270 isect = (sector_t) (f_offset >> SECTOR_SHIFT);
271 /* Code assumes extents are page-aligned */
272 for (i = pg_index; i < rdata->pages.npages; i++) {
273 if (!extent_length) {
274 /* We've used up the previous extent */
276 bl_put_extent(cow_read);
277 bio = bl_submit_bio(READ, bio);
278 /* Get the next one */
279 be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg),
282 header->pnfs_error = -EIO;
285 extent_length = be->be_length -
286 (isect - be->be_f_offset);
288 sector_t cow_length = cow_read->be_length -
289 (isect - cow_read->be_f_offset);
290 extent_length = min(extent_length, cow_length);
295 pg_offset = f_offset & ~PAGE_CACHE_MASK;
296 if (pg_offset + bytes_left > PAGE_CACHE_SIZE)
297 pg_len = PAGE_CACHE_SIZE - pg_offset;
302 bytes_left -= pg_len;
303 isect += (pg_offset >> SECTOR_SHIFT);
306 pg_len = PAGE_CACHE_SIZE;
309 hole = is_hole(be, isect);
310 if (hole && !cow_read) {
311 bio = bl_submit_bio(READ, bio);
312 /* Fill hole w/ zeroes w/o accessing device */
313 dprintk("%s Zeroing page for hole\n", __func__);
314 zero_user_segment(pages[i], pg_offset, pg_len);
315 print_page(pages[i]);
316 SetPageUptodate(pages[i]);
318 struct pnfs_block_extent *be_read;
320 be_read = (hole && cow_read) ? cow_read : be;
321 bio = do_add_page_to_bio(bio, rdata->pages.npages - i,
323 isect, pages[i], be_read,
327 header->pnfs_error = PTR_ERR(bio);
332 isect += (pg_len >> SECTOR_SHIFT);
333 extent_length -= PAGE_CACHE_SECTORS;
335 if ((isect << SECTOR_SHIFT) >= header->inode->i_size) {
337 rdata->res.count = header->inode->i_size - rdata->args.offset;
339 rdata->res.count = (isect << SECTOR_SHIFT) - rdata->args.offset;
343 bl_put_extent(cow_read);
344 bl_submit_bio(READ, bio);
346 return PNFS_ATTEMPTED;
349 dprintk("Giving up and using normal NFS\n");
350 return PNFS_NOT_ATTEMPTED;
353 static void mark_extents_written(struct pnfs_block_layout *bl,
354 __u64 offset, __u32 count)
357 struct pnfs_block_extent *be;
358 struct pnfs_block_short_extent *se;
360 dprintk("%s(%llu, %u)\n", __func__, offset, count);
363 isect = (offset & (long)(PAGE_CACHE_MASK)) >> SECTOR_SHIFT;
364 end = (offset + count + PAGE_CACHE_SIZE - 1) & (long)(PAGE_CACHE_MASK);
365 end >>= SECTOR_SHIFT;
366 while (isect < end) {
368 be = bl_find_get_extent(bl, isect, NULL);
369 BUG_ON(!be); /* FIXME */
370 len = min(end, be->be_f_offset + be->be_length) - isect;
371 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
372 se = bl_pop_one_short_extent(be->be_inval);
374 bl_mark_for_commit(be, isect, len, se);
381 static void bl_end_io_write_zero(struct bio *bio, int err)
383 struct parallel_io *par = bio->bi_private;
384 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
385 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
388 struct page *page = bvec->bv_page;
390 if (--bvec >= bio->bi_io_vec)
391 prefetchw(&bvec->bv_page->flags);
392 /* This is the zeroing page we added */
393 end_page_writeback(page);
394 page_cache_release(page);
395 } while (bvec >= bio->bi_io_vec);
397 if (unlikely(!uptodate)) {
398 struct nfs_write_data *data = par->data;
399 struct nfs_pgio_header *header = data->header;
401 if (!header->pnfs_error)
402 header->pnfs_error = -EIO;
403 pnfs_set_lo_fail(header->lseg);
409 static void bl_end_io_write(struct bio *bio, int err)
411 struct parallel_io *par = bio->bi_private;
412 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
413 struct nfs_write_data *data = par->data;
414 struct nfs_pgio_header *header = data->header;
417 if (!header->pnfs_error)
418 header->pnfs_error = -EIO;
419 pnfs_set_lo_fail(header->lseg);
425 /* Function scheduled for call during bl_end_par_io_write,
426 * it marks sectors as written and extends the commitlist.
428 static void bl_write_cleanup(struct work_struct *work)
430 struct rpc_task *task;
431 struct nfs_write_data *wdata;
432 dprintk("%s enter\n", __func__);
433 task = container_of(work, struct rpc_task, u.tk_work);
434 wdata = container_of(task, struct nfs_write_data, task);
435 if (likely(!wdata->header->pnfs_error)) {
436 /* Marks for LAYOUTCOMMIT */
437 mark_extents_written(BLK_LSEG2EXT(wdata->header->lseg),
438 wdata->args.offset, wdata->args.count);
440 pnfs_ld_write_done(wdata);
443 /* Called when last of bios associated with a bl_write_pagelist call finishes */
444 static void bl_end_par_io_write(void *data, int num_se)
446 struct nfs_write_data *wdata = data;
448 if (unlikely(wdata->header->pnfs_error)) {
449 bl_free_short_extents(&BLK_LSEG2EXT(wdata->header->lseg)->bl_inval,
453 wdata->task.tk_status = wdata->header->pnfs_error;
454 wdata->verf.committed = NFS_FILE_SYNC;
455 INIT_WORK(&wdata->task.u.tk_work, bl_write_cleanup);
456 schedule_work(&wdata->task.u.tk_work);
459 /* FIXME STUB - mark intersection of layout and page as bad, so is not
462 static void mark_bad_read(void)
468 * map_block: map a requested I/0 block (isect) into an offset in the LVM
472 map_block(struct buffer_head *bh, sector_t isect, struct pnfs_block_extent *be)
474 dprintk("%s enter be=%p\n", __func__, be);
476 set_buffer_mapped(bh);
477 bh->b_bdev = be->be_mdev;
478 bh->b_blocknr = (isect - be->be_f_offset + be->be_v_offset) >>
479 (be->be_mdev->bd_inode->i_blkbits - SECTOR_SHIFT);
481 dprintk("%s isect %llu, bh->b_blocknr %ld, using bsize %Zd\n",
482 __func__, (unsigned long long)isect, (long)bh->b_blocknr,
488 bl_read_single_end_io(struct bio *bio, int error)
490 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
491 struct page *page = bvec->bv_page;
493 /* Only one page in bvec */
498 bl_do_readpage_sync(struct page *page, struct pnfs_block_extent *be,
499 unsigned int offset, unsigned int len)
502 struct page *shadow_page;
504 char *kaddr, *kshadow_addr;
507 dprintk("%s: offset %u len %u\n", __func__, offset, len);
509 shadow_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
510 if (shadow_page == NULL)
513 bio = bio_alloc(GFP_NOIO, 1);
517 isect = (page->index << PAGE_CACHE_SECTOR_SHIFT) +
518 (offset / SECTOR_SIZE);
520 bio->bi_sector = isect - be->be_f_offset + be->be_v_offset;
521 bio->bi_bdev = be->be_mdev;
522 bio->bi_end_io = bl_read_single_end_io;
524 lock_page(shadow_page);
525 if (bio_add_page(bio, shadow_page,
526 SECTOR_SIZE, round_down(offset, SECTOR_SIZE)) == 0) {
527 unlock_page(shadow_page);
532 submit_bio(READ, bio);
533 wait_on_page_locked(shadow_page);
534 if (unlikely(!test_bit(BIO_UPTODATE, &bio->bi_flags))) {
537 kaddr = kmap_atomic(page);
538 kshadow_addr = kmap_atomic(shadow_page);
539 memcpy(kaddr + offset, kshadow_addr + offset, len);
540 kunmap_atomic(kshadow_addr);
541 kunmap_atomic(kaddr);
543 __free_page(shadow_page);
550 bl_read_partial_page_sync(struct page *page, struct pnfs_block_extent *be,
551 unsigned int dirty_offset, unsigned int dirty_len,
555 unsigned int start, end;
559 end = PAGE_CACHE_SIZE;
561 start = round_down(dirty_offset, SECTOR_SIZE);
562 end = round_up(dirty_offset + dirty_len, SECTOR_SIZE);
565 dprintk("%s: offset %u len %d\n", __func__, dirty_offset, dirty_len);
567 zero_user_segments(page, start, dirty_offset,
568 dirty_offset + dirty_len, end);
569 if (start == 0 && end == PAGE_CACHE_SIZE &&
570 trylock_page(page)) {
571 SetPageUptodate(page);
577 if (start != dirty_offset)
578 ret = bl_do_readpage_sync(page, be, start, dirty_offset - start);
580 if (!ret && (dirty_offset + dirty_len < end))
581 ret = bl_do_readpage_sync(page, be, dirty_offset + dirty_len,
582 end - dirty_offset - dirty_len);
587 /* Given an unmapped page, zero it or read in page for COW, page is locked
591 init_page_for_write(struct page *page, struct pnfs_block_extent *cow_read)
593 struct buffer_head *bh = NULL;
597 dprintk("%s enter, %p\n", __func__, page);
598 BUG_ON(PageUptodate(page));
600 zero_user_segment(page, 0, PAGE_SIZE);
601 SetPageUptodate(page);
605 bh = alloc_page_buffers(page, PAGE_CACHE_SIZE, 0);
611 isect = (sector_t) page->index << PAGE_CACHE_SECTOR_SHIFT;
612 map_block(bh, isect, cow_read);
613 if (!bh_uptodate_or_lock(bh))
614 ret = bh_submit_read(bh);
617 SetPageUptodate(page);
621 free_buffer_head(bh);
623 /* Need to mark layout with bad read...should now
624 * just use nfs4 for reads and writes.
631 /* Find or create a zeroing page marked being writeback.
632 * Return ERR_PTR on error, NULL to indicate skip this page and page itself
633 * to indicate write out.
636 bl_find_get_zeroing_page(struct inode *inode, pgoff_t index,
637 struct pnfs_block_extent *cow_read)
641 page = find_get_page(inode->i_mapping, index);
645 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
646 if (unlikely(!page)) {
647 dprintk("%s oom\n", __func__);
648 return ERR_PTR(-ENOMEM);
653 /* PageDirty: Other will write this out
654 * PageWriteback: Other is writing this out
655 * PageUptodate: It was read before
657 if (PageDirty(page) || PageWriteback(page)) {
661 page_cache_release(page);
670 if (!PageUptodate(page)) {
671 /* New page, readin or zero it */
672 init_page_for_write(page, cow_read);
674 set_page_writeback(page);
680 static enum pnfs_try_status
681 bl_write_pagelist(struct nfs_write_data *wdata, int sync)
683 struct nfs_pgio_header *header = wdata->header;
684 int i, ret, npg_zero, pg_index, last = 0;
685 struct bio *bio = NULL;
686 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
687 sector_t isect, last_isect = 0, extent_length = 0;
688 struct parallel_io *par = NULL;
689 loff_t offset = wdata->args.offset;
690 size_t count = wdata->args.count;
691 unsigned int pg_offset, pg_len, saved_len;
692 struct page **pages = wdata->args.pages;
697 NFS_SERVER(header->inode)->pnfs_blksize >> PAGE_CACHE_SHIFT;
699 dprintk("%s enter, %Zu@%lld\n", __func__, count, offset);
701 if (header->dreq != NULL &&
702 (!IS_ALIGNED(offset, NFS_SERVER(header->inode)->pnfs_blksize) ||
703 !IS_ALIGNED(count, NFS_SERVER(header->inode)->pnfs_blksize))) {
704 dprintk("pnfsblock nonblock aligned DIO writes. Resend MDS\n");
707 /* At this point, wdata->pages is a (sequential) list of nfs_pages.
708 * We want to write each, and if there is an error set pnfs_error
709 * to have it redone using nfs.
711 par = alloc_parallel(wdata);
714 par->pnfs_callback = bl_end_par_io_write;
715 /* At this point, have to be more careful with error handling */
717 isect = (sector_t) ((offset & (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
718 be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg), isect, &cow_read);
719 if (!be || !is_writable(be, isect)) {
720 dprintk("%s no matching extents!\n", __func__);
724 /* First page inside INVALID extent */
725 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
726 if (likely(!bl_push_one_short_extent(be->be_inval)))
730 temp = offset >> PAGE_CACHE_SHIFT;
731 npg_zero = do_div(temp, npg_per_block);
732 isect = (sector_t) (((offset - npg_zero * PAGE_CACHE_SIZE) &
733 (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
734 extent_length = be->be_length - (isect - be->be_f_offset);
737 dprintk("%s need to zero %d pages\n", __func__, npg_zero);
738 for (;npg_zero > 0; npg_zero--) {
739 if (bl_is_sector_init(be->be_inval, isect)) {
740 dprintk("isect %llu already init\n",
741 (unsigned long long)isect);
744 /* page ref released in bl_end_io_write_zero */
745 index = isect >> PAGE_CACHE_SECTOR_SHIFT;
746 dprintk("%s zero %dth page: index %lu isect %llu\n",
747 __func__, npg_zero, index,
748 (unsigned long long)isect);
749 page = bl_find_get_zeroing_page(header->inode, index,
751 if (unlikely(IS_ERR(page))) {
752 header->pnfs_error = PTR_ERR(page);
754 } else if (page == NULL)
757 ret = bl_mark_sectors_init(be->be_inval, isect,
760 dprintk("%s bl_mark_sectors_init fail %d\n",
762 end_page_writeback(page);
763 page_cache_release(page);
764 header->pnfs_error = ret;
767 if (likely(!bl_push_one_short_extent(be->be_inval)))
770 end_page_writeback(page);
771 page_cache_release(page);
772 header->pnfs_error = -ENOMEM;
775 /* FIXME: This should be done in bi_end_io */
776 mark_extents_written(BLK_LSEG2EXT(header->lseg),
777 page->index << PAGE_CACHE_SHIFT,
780 bio = bl_add_page_to_bio(bio, npg_zero, WRITE,
782 bl_end_io_write_zero, par);
784 header->pnfs_error = PTR_ERR(bio);
789 isect += PAGE_CACHE_SECTORS;
790 extent_length -= PAGE_CACHE_SECTORS;
795 bio = bl_submit_bio(WRITE, bio);
798 pg_index = wdata->args.pgbase >> PAGE_CACHE_SHIFT;
799 for (i = pg_index; i < wdata->pages.npages; i++) {
800 if (!extent_length) {
801 /* We've used up the previous extent */
803 bl_put_extent(cow_read);
804 bio = bl_submit_bio(WRITE, bio);
805 /* Get the next one */
806 be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg),
808 if (!be || !is_writable(be, isect)) {
809 header->pnfs_error = -EINVAL;
812 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
813 if (likely(!bl_push_one_short_extent(
817 header->pnfs_error = -ENOMEM;
821 extent_length = be->be_length -
822 (isect - be->be_f_offset);
825 dprintk("%s offset %lld count %Zu\n", __func__, offset, count);
826 pg_offset = offset & ~PAGE_CACHE_MASK;
827 if (pg_offset + count > PAGE_CACHE_SIZE)
828 pg_len = PAGE_CACHE_SIZE - pg_offset;
833 if (be->be_state == PNFS_BLOCK_INVALID_DATA &&
834 !bl_is_sector_init(be->be_inval, isect)) {
835 ret = bl_read_partial_page_sync(pages[i], cow_read,
836 pg_offset, pg_len, true);
838 dprintk("%s bl_read_partial_page_sync fail %d\n",
840 header->pnfs_error = ret;
844 ret = bl_mark_sectors_init(be->be_inval, isect,
847 dprintk("%s bl_mark_sectors_init fail %d\n",
849 header->pnfs_error = ret;
853 /* Expand to full page write */
855 pg_len = PAGE_CACHE_SIZE;
856 } else if ((pg_offset & (SECTOR_SIZE - 1)) ||
857 (pg_len & (SECTOR_SIZE - 1))){
858 /* ahh, nasty case. We have to do sync full sector
859 * read-modify-write cycles.
861 unsigned int saved_offset = pg_offset;
862 ret = bl_read_partial_page_sync(pages[i], be, pg_offset,
864 pg_offset = round_down(pg_offset, SECTOR_SIZE);
865 pg_len = round_up(saved_offset + pg_len, SECTOR_SIZE)
870 bio = do_add_page_to_bio(bio, wdata->pages.npages - i, WRITE,
872 bl_end_io_write, par,
875 header->pnfs_error = PTR_ERR(bio);
881 isect += PAGE_CACHE_SECTORS;
883 extent_length -= PAGE_CACHE_SECTORS;
886 /* Last page inside INVALID extent */
887 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
888 bio = bl_submit_bio(WRITE, bio);
889 temp = last_isect >> PAGE_CACHE_SECTOR_SHIFT;
890 npg_zero = npg_per_block - do_div(temp, npg_per_block);
891 if (npg_zero < npg_per_block) {
893 goto fill_invalid_ext;
898 wdata->res.count = wdata->args.count;
901 bl_put_extent(cow_read);
902 bl_submit_bio(WRITE, bio);
904 return PNFS_ATTEMPTED;
907 bl_put_extent(cow_read);
909 return PNFS_NOT_ATTEMPTED;
912 /* FIXME - range ignored */
914 release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range)
917 struct pnfs_block_extent *be;
919 spin_lock(&bl->bl_ext_lock);
920 for (i = 0; i < EXTENT_LISTS; i++) {
921 while (!list_empty(&bl->bl_extents[i])) {
922 be = list_first_entry(&bl->bl_extents[i],
923 struct pnfs_block_extent,
925 list_del(&be->be_node);
929 spin_unlock(&bl->bl_ext_lock);
933 release_inval_marks(struct pnfs_inval_markings *marks)
935 struct pnfs_inval_tracking *pos, *temp;
936 struct pnfs_block_short_extent *se, *stemp;
938 list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) {
939 list_del(&pos->it_link);
943 list_for_each_entry_safe(se, stemp, &marks->im_extents, bse_node) {
944 list_del(&se->bse_node);
950 static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
952 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
954 dprintk("%s enter\n", __func__);
955 release_extents(bl, NULL);
956 release_inval_marks(&bl->bl_inval);
960 static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode,
963 struct pnfs_block_layout *bl;
965 dprintk("%s enter\n", __func__);
966 bl = kzalloc(sizeof(*bl), gfp_flags);
969 spin_lock_init(&bl->bl_ext_lock);
970 INIT_LIST_HEAD(&bl->bl_extents[0]);
971 INIT_LIST_HEAD(&bl->bl_extents[1]);
972 INIT_LIST_HEAD(&bl->bl_commit);
973 INIT_LIST_HEAD(&bl->bl_committing);
975 bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT;
976 BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize);
977 return &bl->bl_layout;
980 static void bl_free_lseg(struct pnfs_layout_segment *lseg)
982 dprintk("%s enter\n", __func__);
986 /* We pretty much ignore lseg, and store all data layout wide, so we
987 * can correctly merge.
989 static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo,
990 struct nfs4_layoutget_res *lgr,
993 struct pnfs_layout_segment *lseg;
996 dprintk("%s enter\n", __func__);
997 lseg = kzalloc(sizeof(*lseg), gfp_flags);
999 return ERR_PTR(-ENOMEM);
1000 status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags);
1002 /* We don't want to call the full-blown bl_free_lseg,
1003 * since on error extents were not touched.
1006 return ERR_PTR(status);
1012 bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr,
1013 const struct nfs4_layoutcommit_args *arg)
1015 dprintk("%s enter\n", __func__);
1016 encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg);
1020 bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
1022 struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout;
1024 dprintk("%s enter\n", __func__);
1025 clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status);
1028 static void free_blk_mountid(struct block_mount_id *mid)
1031 struct pnfs_block_dev *dev, *tmp;
1033 /* No need to take bm_lock as we are last user freeing bm_devlist */
1034 list_for_each_entry_safe(dev, tmp, &mid->bm_devlist, bm_node) {
1035 list_del(&dev->bm_node);
1036 bl_free_block_dev(dev);
1042 /* This is mostly copied from the filelayout_get_device_info function.
1043 * It seems much of this should be at the generic pnfs level.
1045 static struct pnfs_block_dev *
1046 nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh,
1047 struct nfs4_deviceid *d_id)
1049 struct pnfs_device *dev;
1050 struct pnfs_block_dev *rv;
1053 struct page **pages = NULL;
1057 * Use the session max response size as the basis for setting
1058 * GETDEVICEINFO's maxcount
1060 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
1061 max_pages = nfs_page_array_len(0, max_resp_sz);
1062 dprintk("%s max_resp_sz %u max_pages %d\n",
1063 __func__, max_resp_sz, max_pages);
1065 dev = kmalloc(sizeof(*dev), GFP_NOFS);
1067 dprintk("%s kmalloc failed\n", __func__);
1068 return ERR_PTR(-ENOMEM);
1071 pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS);
1072 if (pages == NULL) {
1074 return ERR_PTR(-ENOMEM);
1076 for (i = 0; i < max_pages; i++) {
1077 pages[i] = alloc_page(GFP_NOFS);
1079 rv = ERR_PTR(-ENOMEM);
1084 memcpy(&dev->dev_id, d_id, sizeof(*d_id));
1085 dev->layout_type = LAYOUT_BLOCK_VOLUME;
1088 dev->pglen = PAGE_SIZE * max_pages;
1091 dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data);
1092 rc = nfs4_proc_getdeviceinfo(server, dev);
1093 dprintk("%s getdevice info returns %d\n", __func__, rc);
1099 rv = nfs4_blk_decode_device(server, dev);
1101 for (i = 0; i < max_pages; i++)
1102 __free_page(pages[i]);
1109 bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
1111 struct block_mount_id *b_mt_id = NULL;
1112 struct pnfs_devicelist *dlist = NULL;
1113 struct pnfs_block_dev *bdev;
1114 LIST_HEAD(block_disklist);
1117 dprintk("%s enter\n", __func__);
1119 if (server->pnfs_blksize == 0) {
1120 dprintk("%s Server did not return blksize\n", __func__);
1123 b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS);
1128 /* Initialize nfs4 block layout mount id */
1129 spin_lock_init(&b_mt_id->bm_lock);
1130 INIT_LIST_HEAD(&b_mt_id->bm_devlist);
1132 dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS);
1138 while (!dlist->eof) {
1139 status = nfs4_proc_getdevicelist(server, fh, dlist);
1142 dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n",
1143 __func__, dlist->num_devs, dlist->eof);
1144 for (i = 0; i < dlist->num_devs; i++) {
1145 bdev = nfs4_blk_get_deviceinfo(server, fh,
1148 status = PTR_ERR(bdev);
1151 spin_lock(&b_mt_id->bm_lock);
1152 list_add(&bdev->bm_node, &b_mt_id->bm_devlist);
1153 spin_unlock(&b_mt_id->bm_lock);
1156 dprintk("%s SUCCESS\n", __func__);
1157 server->pnfs_ld_data = b_mt_id;
1164 free_blk_mountid(b_mt_id);
1169 bl_clear_layoutdriver(struct nfs_server *server)
1171 struct block_mount_id *b_mt_id = server->pnfs_ld_data;
1173 dprintk("%s enter\n", __func__);
1174 free_blk_mountid(b_mt_id);
1175 dprintk("%s RETURNS\n", __func__);
1180 is_aligned_req(struct nfs_page *req, unsigned int alignment)
1182 return IS_ALIGNED(req->wb_offset, alignment) &&
1183 IS_ALIGNED(req->wb_bytes, alignment);
1187 bl_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1189 if (pgio->pg_dreq != NULL &&
1190 !is_aligned_req(req, SECTOR_SIZE))
1191 nfs_pageio_reset_read_mds(pgio);
1193 pnfs_generic_pg_init_read(pgio, req);
1197 bl_pg_test_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
1198 struct nfs_page *req)
1200 if (pgio->pg_dreq != NULL &&
1201 !is_aligned_req(req, SECTOR_SIZE))
1204 return pnfs_generic_pg_test(pgio, prev, req);
1208 bl_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1210 if (pgio->pg_dreq != NULL &&
1211 !is_aligned_req(req, PAGE_CACHE_SIZE))
1212 nfs_pageio_reset_write_mds(pgio);
1214 pnfs_generic_pg_init_write(pgio, req);
1218 bl_pg_test_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
1219 struct nfs_page *req)
1221 if (pgio->pg_dreq != NULL &&
1222 !is_aligned_req(req, PAGE_CACHE_SIZE))
1225 return pnfs_generic_pg_test(pgio, prev, req);
1228 static const struct nfs_pageio_ops bl_pg_read_ops = {
1229 .pg_init = bl_pg_init_read,
1230 .pg_test = bl_pg_test_read,
1231 .pg_doio = pnfs_generic_pg_readpages,
1234 static const struct nfs_pageio_ops bl_pg_write_ops = {
1235 .pg_init = bl_pg_init_write,
1236 .pg_test = bl_pg_test_write,
1237 .pg_doio = pnfs_generic_pg_writepages,
1240 static struct pnfs_layoutdriver_type blocklayout_type = {
1241 .id = LAYOUT_BLOCK_VOLUME,
1242 .name = "LAYOUT_BLOCK_VOLUME",
1243 .read_pagelist = bl_read_pagelist,
1244 .write_pagelist = bl_write_pagelist,
1245 .alloc_layout_hdr = bl_alloc_layout_hdr,
1246 .free_layout_hdr = bl_free_layout_hdr,
1247 .alloc_lseg = bl_alloc_lseg,
1248 .free_lseg = bl_free_lseg,
1249 .encode_layoutcommit = bl_encode_layoutcommit,
1250 .cleanup_layoutcommit = bl_cleanup_layoutcommit,
1251 .set_layoutdriver = bl_set_layoutdriver,
1252 .clear_layoutdriver = bl_clear_layoutdriver,
1253 .pg_read_ops = &bl_pg_read_ops,
1254 .pg_write_ops = &bl_pg_write_ops,
1257 static const struct rpc_pipe_ops bl_upcall_ops = {
1258 .upcall = rpc_pipe_generic_upcall,
1259 .downcall = bl_pipe_downcall,
1260 .destroy_msg = bl_pipe_destroy_msg,
1263 static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb,
1264 struct rpc_pipe *pipe)
1266 struct dentry *dir, *dentry;
1268 dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME);
1270 return ERR_PTR(-ENOENT);
1271 dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe);
1276 static void nfs4blocklayout_unregister_sb(struct super_block *sb,
1277 struct rpc_pipe *pipe)
1280 rpc_unlink(pipe->dentry);
1283 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
1286 struct super_block *sb = ptr;
1287 struct net *net = sb->s_fs_info;
1288 struct nfs_net *nn = net_generic(net, nfs_net_id);
1289 struct dentry *dentry;
1292 if (!try_module_get(THIS_MODULE))
1295 if (nn->bl_device_pipe == NULL) {
1296 module_put(THIS_MODULE);
1301 case RPC_PIPEFS_MOUNT:
1302 dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe);
1303 if (IS_ERR(dentry)) {
1304 ret = PTR_ERR(dentry);
1307 nn->bl_device_pipe->dentry = dentry;
1309 case RPC_PIPEFS_UMOUNT:
1310 if (nn->bl_device_pipe->dentry)
1311 nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe);
1317 module_put(THIS_MODULE);
1321 static struct notifier_block nfs4blocklayout_block = {
1322 .notifier_call = rpc_pipefs_event,
1325 static struct dentry *nfs4blocklayout_register_net(struct net *net,
1326 struct rpc_pipe *pipe)
1328 struct super_block *pipefs_sb;
1329 struct dentry *dentry;
1331 pipefs_sb = rpc_get_sb_net(net);
1334 dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe);
1335 rpc_put_sb_net(net);
1339 static void nfs4blocklayout_unregister_net(struct net *net,
1340 struct rpc_pipe *pipe)
1342 struct super_block *pipefs_sb;
1344 pipefs_sb = rpc_get_sb_net(net);
1346 nfs4blocklayout_unregister_sb(pipefs_sb, pipe);
1347 rpc_put_sb_net(net);
1351 static int nfs4blocklayout_net_init(struct net *net)
1353 struct nfs_net *nn = net_generic(net, nfs_net_id);
1354 struct dentry *dentry;
1356 init_waitqueue_head(&nn->bl_wq);
1357 nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0);
1358 if (IS_ERR(nn->bl_device_pipe))
1359 return PTR_ERR(nn->bl_device_pipe);
1360 dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe);
1361 if (IS_ERR(dentry)) {
1362 rpc_destroy_pipe_data(nn->bl_device_pipe);
1363 return PTR_ERR(dentry);
1365 nn->bl_device_pipe->dentry = dentry;
1369 static void nfs4blocklayout_net_exit(struct net *net)
1371 struct nfs_net *nn = net_generic(net, nfs_net_id);
1373 nfs4blocklayout_unregister_net(net, nn->bl_device_pipe);
1374 rpc_destroy_pipe_data(nn->bl_device_pipe);
1375 nn->bl_device_pipe = NULL;
1378 static struct pernet_operations nfs4blocklayout_net_ops = {
1379 .init = nfs4blocklayout_net_init,
1380 .exit = nfs4blocklayout_net_exit,
1383 static int __init nfs4blocklayout_init(void)
1387 dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
1389 ret = pnfs_register_layoutdriver(&blocklayout_type);
1393 ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block);
1396 ret = register_pernet_subsys(&nfs4blocklayout_net_ops);
1403 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
1405 pnfs_unregister_layoutdriver(&blocklayout_type);
1409 static void __exit nfs4blocklayout_exit(void)
1411 dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
1414 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
1415 unregister_pernet_subsys(&nfs4blocklayout_net_ops);
1416 pnfs_unregister_layoutdriver(&blocklayout_type);
1419 MODULE_ALIAS("nfs-layouttype4-3");
1421 module_init(nfs4blocklayout_init);
1422 module_exit(nfs4blocklayout_exit);