NFS: track direct IO left bytes
[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 */
71cdd40f 38#include <linux/buffer_head.h> /* various write calls */
88c9e421 39#include <linux/prefetch.h>
155e7524 40
10bd295a
JR
41#include "../pnfs.h"
42#include "../internal.h"
155e7524
FI
43#include "blocklayout.h"
44
45#define NFSDBG_FACILITY NFSDBG_PNFS_LD
46
47MODULE_LICENSE("GPL");
48MODULE_AUTHOR("Andy Adamson <andros@citi.umich.edu>");
49MODULE_DESCRIPTION("The NFSv4.1 pNFS Block layout driver");
50
9549ec01
FI
51static void print_page(struct page *page)
52{
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));
62 dprintk("\n");
63}
64
65/* Given the be associated with isect, determine if page data needs to be
66 * initialized.
67 */
68static int is_hole(struct pnfs_block_extent *be, sector_t isect)
69{
70 if (be->be_state == PNFS_BLOCK_NONE_DATA)
71 return 1;
72 else if (be->be_state != PNFS_BLOCK_INVALID_DATA)
73 return 0;
74 else
75 return !bl_is_sector_init(be->be_inval, isect);
76}
77
650e2d39
FI
78/* Given the be associated with isect, determine if page data can be
79 * written to disk.
80 */
81static int is_writable(struct pnfs_block_extent *be, sector_t isect)
82{
71cdd40f
PT
83 return (be->be_state == PNFS_BLOCK_READWRITE_DATA ||
84 be->be_state == PNFS_BLOCK_INVALID_DATA);
650e2d39
FI
85}
86
9549ec01
FI
87/* The data we are handed might be spread across several bios. We need
88 * to track when the last one is finished.
89 */
90struct parallel_io {
91 struct kref refcnt;
7c5465d6 92 void (*pnfs_callback) (void *data, int num_se);
9549ec01 93 void *data;
7c5465d6 94 int bse_count;
9549ec01
FI
95};
96
97static inline struct parallel_io *alloc_parallel(void *data)
98{
99 struct parallel_io *rv;
100
101 rv = kmalloc(sizeof(*rv), GFP_NOFS);
102 if (rv) {
103 rv->data = data;
104 kref_init(&rv->refcnt);
7c5465d6 105 rv->bse_count = 0;
9549ec01
FI
106 }
107 return rv;
108}
109
110static inline void get_parallel(struct parallel_io *p)
111{
112 kref_get(&p->refcnt);
113}
114
115static void destroy_parallel(struct kref *kref)
116{
117 struct parallel_io *p = container_of(kref, struct parallel_io, refcnt);
118
119 dprintk("%s enter\n", __func__);
7c5465d6 120 p->pnfs_callback(p->data, p->bse_count);
9549ec01
FI
121 kfree(p);
122}
123
124static inline void put_parallel(struct parallel_io *p)
125{
126 kref_put(&p->refcnt, destroy_parallel);
127}
128
129static struct bio *
130bl_submit_bio(int rw, struct bio *bio)
131{
132 if (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);
137 submit_bio(rw, bio);
138 }
139 return NULL;
140}
141
142static 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)
146{
147 struct bio *bio;
148
74a6eeb4 149 npg = min(npg, BIO_MAX_PAGES);
9549ec01 150 bio = bio_alloc(GFP_NOIO, npg);
74a6eeb4
PT
151 if (!bio && (current->flags & PF_MEMALLOC)) {
152 while (!bio && (npg /= 2))
153 bio = bio_alloc(GFP_NOIO, npg);
154 }
9549ec01 155
74a6eeb4
PT
156 if (bio) {
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;
161 }
9549ec01
FI
162 return bio;
163}
164
fe6e1e8d 165static struct bio *do_add_page_to_bio(struct bio *bio, int npg, int rw,
9549ec01
FI
166 sector_t isect, struct page *page,
167 struct pnfs_block_extent *be,
168 void (*end_io)(struct bio *, int err),
fe6e1e8d
PT
169 struct parallel_io *par,
170 unsigned int offset, int len)
9549ec01 171{
fe6e1e8d
PT
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);
9549ec01
FI
175retry:
176 if (!bio) {
177 bio = bl_alloc_init_bio(npg, isect, be, end_io, par);
178 if (!bio)
179 return ERR_PTR(-ENOMEM);
180 }
fe6e1e8d 181 if (bio_add_page(bio, page, len, offset) < len) {
9549ec01
FI
182 bio = bl_submit_bio(rw, bio);
183 goto retry;
184 }
185 return bio;
186}
187
fe6e1e8d
PT
188static 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)
193{
194 return do_add_page_to_bio(bio, npg, rw, isect, page, be,
195 end_io, par, 0, PAGE_CACHE_SIZE);
196}
197
9549ec01
FI
198/* This is basically copied from mpage_end_io_read */
199static void bl_end_io_read(struct bio *bio, int err)
200{
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;
9549ec01
FI
204
205 do {
206 struct page *page = bvec->bv_page;
207
208 if (--bvec >= bio->bi_io_vec)
209 prefetchw(&bvec->bv_page->flags);
210 if (uptodate)
211 SetPageUptodate(page);
212 } while (bvec >= bio->bi_io_vec);
213 if (!uptodate) {
cd841605
FI
214 struct nfs_read_data *rdata = par->data;
215 struct nfs_pgio_header *header = rdata->header;
216
217 if (!header->pnfs_error)
218 header->pnfs_error = -EIO;
219 pnfs_set_lo_fail(header->lseg);
9549ec01
FI
220 }
221 bio_put(bio);
222 put_parallel(par);
223}
224
225static void bl_read_cleanup(struct work_struct *work)
226{
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);
233}
234
235static void
7c5465d6 236bl_end_par_io_read(void *data, int unused)
9549ec01
FI
237{
238 struct nfs_read_data *rdata = data;
239
cd841605 240 rdata->task.tk_status = rdata->header->pnfs_error;
9549ec01
FI
241 INIT_WORK(&rdata->task.u.tk_work, bl_read_cleanup);
242 schedule_work(&rdata->task.u.tk_work);
243}
244
155e7524
FI
245static enum pnfs_try_status
246bl_read_pagelist(struct nfs_read_data *rdata)
247{
cd841605 248 struct nfs_pgio_header *header = rdata->header;
9549ec01
FI
249 int i, hole;
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;
f742dc4a
PT
255 size_t bytes_left = rdata->args.count;
256 unsigned int pg_offset, pg_len;
9549ec01
FI
257 struct page **pages = rdata->args.pages;
258 int pg_index = rdata->args.pgbase >> PAGE_CACHE_SHIFT;
f742dc4a 259 const bool is_dio = (header->dreq != NULL);
9549ec01 260
6f00866d 261 dprintk("%s enter nr_pages %u offset %lld count %u\n", __func__,
30dd374f 262 rdata->pages.npages, f_offset, (unsigned int)rdata->args.count);
9549ec01
FI
263
264 par = alloc_parallel(rdata);
265 if (!par)
266 goto use_mds;
9549ec01
FI
267 par->pnfs_callback = bl_end_par_io_read;
268 /* At this point, we can no longer jump to use_mds */
269
270 isect = (sector_t) (f_offset >> SECTOR_SHIFT);
271 /* Code assumes extents are page-aligned */
30dd374f 272 for (i = pg_index; i < rdata->pages.npages; i++) {
9549ec01
FI
273 if (!extent_length) {
274 /* We've used up the previous extent */
275 bl_put_extent(be);
276 bl_put_extent(cow_read);
277 bio = bl_submit_bio(READ, bio);
278 /* Get the next one */
cd841605 279 be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg),
9549ec01
FI
280 isect, &cow_read);
281 if (!be) {
cd841605 282 header->pnfs_error = -EIO;
9549ec01
FI
283 goto out;
284 }
285 extent_length = be->be_length -
286 (isect - be->be_f_offset);
287 if (cow_read) {
288 sector_t cow_length = cow_read->be_length -
289 (isect - cow_read->be_f_offset);
290 extent_length = min(extent_length, cow_length);
291 }
292 }
f742dc4a
PT
293
294 if (is_dio) {
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;
298 else
299 pg_len = bytes_left;
300
301 f_offset += pg_len;
302 bytes_left -= pg_len;
303 isect += (pg_offset >> SECTOR_SHIFT);
304 } else {
305 pg_offset = 0;
306 pg_len = PAGE_CACHE_SIZE;
307 }
308
9549ec01
FI
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__);
f742dc4a 314 zero_user_segment(pages[i], pg_offset, pg_len);
9549ec01
FI
315 print_page(pages[i]);
316 SetPageUptodate(pages[i]);
317 } else {
318 struct pnfs_block_extent *be_read;
319
320 be_read = (hole && cow_read) ? cow_read : be;
f742dc4a 321 bio = do_add_page_to_bio(bio, rdata->pages.npages - i,
30dd374f 322 READ,
9549ec01 323 isect, pages[i], be_read,
f742dc4a
PT
324 bl_end_io_read, par,
325 pg_offset, pg_len);
9549ec01 326 if (IS_ERR(bio)) {
cd841605 327 header->pnfs_error = PTR_ERR(bio);
e6d05a75 328 bio = NULL;
9549ec01
FI
329 goto out;
330 }
331 }
f742dc4a 332 isect += (pg_len >> SECTOR_SHIFT);
9549ec01
FI
333 extent_length -= PAGE_CACHE_SECTORS;
334 }
cd841605 335 if ((isect << SECTOR_SHIFT) >= header->inode->i_size) {
9549ec01 336 rdata->res.eof = 1;
f742dc4a 337 rdata->res.count = header->inode->i_size - rdata->args.offset;
9549ec01 338 } else {
f742dc4a 339 rdata->res.count = (isect << SECTOR_SHIFT) - rdata->args.offset;
9549ec01
FI
340 }
341out:
342 bl_put_extent(be);
343 bl_put_extent(cow_read);
344 bl_submit_bio(READ, bio);
345 put_parallel(par);
346 return PNFS_ATTEMPTED;
347
348 use_mds:
349 dprintk("Giving up and using normal NFS\n");
155e7524
FI
350 return PNFS_NOT_ATTEMPTED;
351}
352
31e6306a
FI
353static void mark_extents_written(struct pnfs_block_layout *bl,
354 __u64 offset, __u32 count)
355{
356 sector_t isect, end;
357 struct pnfs_block_extent *be;
7c5465d6 358 struct pnfs_block_short_extent *se;
31e6306a
FI
359
360 dprintk("%s(%llu, %u)\n", __func__, offset, count);
361 if (count == 0)
362 return;
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) {
367 sector_t len;
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;
7c5465d6
PT
371 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
372 se = bl_pop_one_short_extent(be->be_inval);
373 BUG_ON(!se);
374 bl_mark_for_commit(be, isect, len, se);
375 }
31e6306a
FI
376 isect += len;
377 bl_put_extent(be);
378 }
379}
380
71cdd40f
PT
381static void bl_end_io_write_zero(struct bio *bio, int err)
382{
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;
71cdd40f
PT
386
387 do {
388 struct page *page = bvec->bv_page;
389
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);
7c5465d6
PT
396
397 if (unlikely(!uptodate)) {
cd841605
FI
398 struct nfs_write_data *data = par->data;
399 struct nfs_pgio_header *header = data->header;
400
401 if (!header->pnfs_error)
402 header->pnfs_error = -EIO;
403 pnfs_set_lo_fail(header->lseg);
71cdd40f
PT
404 }
405 bio_put(bio);
406 put_parallel(par);
407}
408
650e2d39
FI
409static void bl_end_io_write(struct bio *bio, int err)
410{
411 struct parallel_io *par = bio->bi_private;
412 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
cd841605
FI
413 struct nfs_write_data *data = par->data;
414 struct nfs_pgio_header *header = data->header;
650e2d39
FI
415
416 if (!uptodate) {
cd841605
FI
417 if (!header->pnfs_error)
418 header->pnfs_error = -EIO;
419 pnfs_set_lo_fail(header->lseg);
650e2d39
FI
420 }
421 bio_put(bio);
422 put_parallel(par);
423}
424
425/* Function scheduled for call during bl_end_par_io_write,
426 * it marks sectors as written and extends the commitlist.
427 */
428static void bl_write_cleanup(struct work_struct *work)
429{
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);
cd841605 435 if (likely(!wdata->header->pnfs_error)) {
31e6306a 436 /* Marks for LAYOUTCOMMIT */
cd841605 437 mark_extents_written(BLK_LSEG2EXT(wdata->header->lseg),
31e6306a
FI
438 wdata->args.offset, wdata->args.count);
439 }
650e2d39
FI
440 pnfs_ld_write_done(wdata);
441}
442
443/* Called when last of bios associated with a bl_write_pagelist call finishes */
7c5465d6 444static void bl_end_par_io_write(void *data, int num_se)
650e2d39
FI
445{
446 struct nfs_write_data *wdata = data;
447
cd841605
FI
448 if (unlikely(wdata->header->pnfs_error)) {
449 bl_free_short_extents(&BLK_LSEG2EXT(wdata->header->lseg)->bl_inval,
7c5465d6
PT
450 num_se);
451 }
452
cd841605 453 wdata->task.tk_status = wdata->header->pnfs_error;
650e2d39
FI
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);
457}
458
71cdd40f
PT
459/* FIXME STUB - mark intersection of layout and page as bad, so is not
460 * used again.
461 */
462static void mark_bad_read(void)
463{
464 return;
465}
466
467/*
468 * map_block: map a requested I/0 block (isect) into an offset in the LVM
469 * block_device
470 */
471static void
472map_block(struct buffer_head *bh, sector_t isect, struct pnfs_block_extent *be)
473{
474 dprintk("%s enter be=%p\n", __func__, be);
475
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);
480
481 dprintk("%s isect %llu, bh->b_blocknr %ld, using bsize %Zd\n",
482 __func__, (unsigned long long)isect, (long)bh->b_blocknr,
483 bh->b_size);
484 return;
485}
486
fe6e1e8d
PT
487static void
488bl_read_single_end_io(struct bio *bio, int error)
489{
490 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
491 struct page *page = bvec->bv_page;
492
493 /* Only one page in bvec */
494 unlock_page(page);
495}
496
497static int
498bl_do_readpage_sync(struct page *page, struct pnfs_block_extent *be,
499 unsigned int offset, unsigned int len)
500{
501 struct bio *bio;
502 struct page *shadow_page;
503 sector_t isect;
504 char *kaddr, *kshadow_addr;
505 int ret = 0;
506
507 dprintk("%s: offset %u len %u\n", __func__, offset, len);
508
509 shadow_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
510 if (shadow_page == NULL)
511 return -ENOMEM;
512
513 bio = bio_alloc(GFP_NOIO, 1);
514 if (bio == NULL)
515 return -ENOMEM;
516
517 isect = (page->index << PAGE_CACHE_SECTOR_SHIFT) +
518 (offset / SECTOR_SIZE);
519
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;
523
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);
528 bio_put(bio);
529 return -EIO;
530 }
531
532 submit_bio(READ, bio);
533 wait_on_page_locked(shadow_page);
534 if (unlikely(!test_bit(BIO_UPTODATE, &bio->bi_flags))) {
535 ret = -EIO;
536 } else {
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);
542 }
543 __free_page(shadow_page);
544 bio_put(bio);
545
546 return ret;
547}
548
549static int
550bl_read_partial_page_sync(struct page *page, struct pnfs_block_extent *be,
551 unsigned int dirty_offset, unsigned int dirty_len,
552 bool full_page)
553{
554 int ret = 0;
555 unsigned int start, end;
556
557 if (full_page) {
558 start = 0;
559 end = PAGE_CACHE_SIZE;
560 } else {
561 start = round_down(dirty_offset, SECTOR_SIZE);
562 end = round_up(dirty_offset + dirty_len, SECTOR_SIZE);
563 }
564
565 dprintk("%s: offset %u len %d\n", __func__, dirty_offset, dirty_len);
566 if (!be) {
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);
572 unlock_page(page);
573 }
574 return ret;
575 }
576
577 if (start != dirty_offset)
578 ret = bl_do_readpage_sync(page, be, start, dirty_offset - start);
579
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);
583
584 return ret;
585}
586
71cdd40f
PT
587/* Given an unmapped page, zero it or read in page for COW, page is locked
588 * by caller.
589 */
590static int
591init_page_for_write(struct page *page, struct pnfs_block_extent *cow_read)
592{
593 struct buffer_head *bh = NULL;
594 int ret = 0;
595 sector_t isect;
596
597 dprintk("%s enter, %p\n", __func__, page);
598 BUG_ON(PageUptodate(page));
599 if (!cow_read) {
600 zero_user_segment(page, 0, PAGE_SIZE);
601 SetPageUptodate(page);
602 goto cleanup;
603 }
604
605 bh = alloc_page_buffers(page, PAGE_CACHE_SIZE, 0);
606 if (!bh) {
607 ret = -ENOMEM;
608 goto cleanup;
609 }
610
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);
615 if (ret)
616 goto cleanup;
617 SetPageUptodate(page);
618
619cleanup:
71cdd40f
PT
620 if (bh)
621 free_buffer_head(bh);
622 if (ret) {
623 /* Need to mark layout with bad read...should now
624 * just use nfs4 for reads and writes.
625 */
626 mark_bad_read();
627 }
628 return ret;
629}
630
72c50887
PT
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.
634 */
635static struct page *
636bl_find_get_zeroing_page(struct inode *inode, pgoff_t index,
637 struct pnfs_block_extent *cow_read)
638{
639 struct page *page;
640 int locked = 0;
641 page = find_get_page(inode->i_mapping, index);
642 if (page)
643 goto check_page;
644
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);
649 }
650 locked = 1;
651
652check_page:
653 /* PageDirty: Other will write this out
654 * PageWriteback: Other is writing this out
655 * PageUptodate: It was read before
656 */
657 if (PageDirty(page) || PageWriteback(page)) {
658 print_page(page);
659 if (locked)
660 unlock_page(page);
661 page_cache_release(page);
662 return NULL;
663 }
664
665 if (!locked) {
666 lock_page(page);
667 locked = 1;
668 goto check_page;
669 }
670 if (!PageUptodate(page)) {
671 /* New page, readin or zero it */
672 init_page_for_write(page, cow_read);
673 }
674 set_page_writeback(page);
675 unlock_page(page);
676
677 return page;
678}
679
155e7524 680static enum pnfs_try_status
650e2d39 681bl_write_pagelist(struct nfs_write_data *wdata, int sync)
155e7524 682{
cd841605 683 struct nfs_pgio_header *header = wdata->header;
71cdd40f 684 int i, ret, npg_zero, pg_index, last = 0;
650e2d39 685 struct bio *bio = NULL;
71cdd40f
PT
686 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
687 sector_t isect, last_isect = 0, extent_length = 0;
96c9eae6 688 struct parallel_io *par = NULL;
650e2d39
FI
689 loff_t offset = wdata->args.offset;
690 size_t count = wdata->args.count;
fe6e1e8d 691 unsigned int pg_offset, pg_len, saved_len;
650e2d39 692 struct page **pages = wdata->args.pages;
71cdd40f
PT
693 struct page *page;
694 pgoff_t index;
695 u64 temp;
696 int npg_per_block =
cd841605 697 NFS_SERVER(header->inode)->pnfs_blksize >> PAGE_CACHE_SHIFT;
650e2d39
FI
698
699 dprintk("%s enter, %Zu@%lld\n", __func__, count, offset);
96c9eae6
PT
700
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");
705 goto out_mds;
706 }
650e2d39 707 /* At this point, wdata->pages is a (sequential) list of nfs_pages.
71cdd40f
PT
708 * We want to write each, and if there is an error set pnfs_error
709 * to have it redone using nfs.
650e2d39
FI
710 */
711 par = alloc_parallel(wdata);
712 if (!par)
7c5465d6 713 goto out_mds;
650e2d39
FI
714 par->pnfs_callback = bl_end_par_io_write;
715 /* At this point, have to be more careful with error handling */
716
717 isect = (sector_t) ((offset & (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
cd841605 718 be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg), isect, &cow_read);
71cdd40f
PT
719 if (!be || !is_writable(be, isect)) {
720 dprintk("%s no matching extents!\n", __func__);
7c5465d6 721 goto out_mds;
71cdd40f
PT
722 }
723
724 /* First page inside INVALID extent */
725 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
7c5465d6
PT
726 if (likely(!bl_push_one_short_extent(be->be_inval)))
727 par->bse_count++;
728 else
729 goto out_mds;
71cdd40f
PT
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);
735
736fill_invalid_ext:
737 dprintk("%s need to zero %d pages\n", __func__, npg_zero);
738 for (;npg_zero > 0; npg_zero--) {
75422745
PT
739 if (bl_is_sector_init(be->be_inval, isect)) {
740 dprintk("isect %llu already init\n",
741 (unsigned long long)isect);
742 goto next_page;
743 }
71cdd40f
PT
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);
cd841605 749 page = bl_find_get_zeroing_page(header->inode, index,
72c50887
PT
750 cow_read);
751 if (unlikely(IS_ERR(page))) {
cd841605 752 header->pnfs_error = PTR_ERR(page);
71cdd40f 753 goto out;
72c50887 754 } else if (page == NULL)
71cdd40f 755 goto next_page;
71cdd40f
PT
756
757 ret = bl_mark_sectors_init(be->be_inval, isect,
60c52e3a 758 PAGE_CACHE_SECTORS);
71cdd40f
PT
759 if (unlikely(ret)) {
760 dprintk("%s bl_mark_sectors_init fail %d\n",
761 __func__, ret);
762 end_page_writeback(page);
763 page_cache_release(page);
cd841605 764 header->pnfs_error = ret;
71cdd40f
PT
765 goto out;
766 }
7c5465d6
PT
767 if (likely(!bl_push_one_short_extent(be->be_inval)))
768 par->bse_count++;
769 else {
770 end_page_writeback(page);
771 page_cache_release(page);
cd841605 772 header->pnfs_error = -ENOMEM;
7c5465d6
PT
773 goto out;
774 }
775 /* FIXME: This should be done in bi_end_io */
cd841605 776 mark_extents_written(BLK_LSEG2EXT(header->lseg),
7c5465d6
PT
777 page->index << PAGE_CACHE_SHIFT,
778 PAGE_CACHE_SIZE);
779
71cdd40f
PT
780 bio = bl_add_page_to_bio(bio, npg_zero, WRITE,
781 isect, page, be,
782 bl_end_io_write_zero, par);
783 if (IS_ERR(bio)) {
cd841605 784 header->pnfs_error = PTR_ERR(bio);
e6d05a75 785 bio = NULL;
71cdd40f
PT
786 goto out;
787 }
71cdd40f
PT
788next_page:
789 isect += PAGE_CACHE_SECTORS;
790 extent_length -= PAGE_CACHE_SECTORS;
791 }
792 if (last)
793 goto write_done;
794 }
795 bio = bl_submit_bio(WRITE, bio);
796
797 /* Middle pages */
798 pg_index = wdata->args.pgbase >> PAGE_CACHE_SHIFT;
30dd374f 799 for (i = pg_index; i < wdata->pages.npages; i++) {
650e2d39
FI
800 if (!extent_length) {
801 /* We've used up the previous extent */
802 bl_put_extent(be);
fe6e1e8d 803 bl_put_extent(cow_read);
650e2d39
FI
804 bio = bl_submit_bio(WRITE, bio);
805 /* Get the next one */
cd841605 806 be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg),
fe6e1e8d 807 isect, &cow_read);
650e2d39 808 if (!be || !is_writable(be, isect)) {
cd841605 809 header->pnfs_error = -EINVAL;
650e2d39
FI
810 goto out;
811 }
7c5465d6
PT
812 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
813 if (likely(!bl_push_one_short_extent(
814 be->be_inval)))
815 par->bse_count++;
816 else {
cd841605 817 header->pnfs_error = -ENOMEM;
7c5465d6
PT
818 goto out;
819 }
820 }
650e2d39 821 extent_length = be->be_length -
71cdd40f 822 (isect - be->be_f_offset);
650e2d39 823 }
fe6e1e8d
PT
824
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;
829 else
830 pg_len = count;
831
832 saved_len = pg_len;
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);
837 if (ret) {
838 dprintk("%s bl_read_partial_page_sync fail %d\n",
839 __func__, ret);
840 header->pnfs_error = ret;
841 goto out;
842 }
843
71cdd40f 844 ret = bl_mark_sectors_init(be->be_inval, isect,
60c52e3a 845 PAGE_CACHE_SECTORS);
71cdd40f
PT
846 if (unlikely(ret)) {
847 dprintk("%s bl_mark_sectors_init fail %d\n",
848 __func__, ret);
cd841605 849 header->pnfs_error = ret;
71cdd40f 850 goto out;
650e2d39 851 }
fe6e1e8d
PT
852
853 /* Expand to full page write */
854 pg_offset = 0;
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.
860 */
861 unsigned int saved_offset = pg_offset;
862 ret = bl_read_partial_page_sync(pages[i], be, pg_offset,
863 pg_len, false);
864 pg_offset = round_down(pg_offset, SECTOR_SIZE);
865 pg_len = round_up(saved_offset + pg_len, SECTOR_SIZE)
866 - pg_offset;
71cdd40f 867 }
fe6e1e8d
PT
868
869
870 bio = do_add_page_to_bio(bio, wdata->pages.npages - i, WRITE,
71cdd40f 871 isect, pages[i], be,
fe6e1e8d
PT
872 bl_end_io_write, par,
873 pg_offset, pg_len);
71cdd40f 874 if (IS_ERR(bio)) {
cd841605 875 header->pnfs_error = PTR_ERR(bio);
e6d05a75 876 bio = NULL;
71cdd40f 877 goto out;
650e2d39 878 }
fe6e1e8d
PT
879 offset += saved_len;
880 count -= saved_len;
650e2d39 881 isect += PAGE_CACHE_SECTORS;
71cdd40f 882 last_isect = isect;
650e2d39
FI
883 extent_length -= PAGE_CACHE_SECTORS;
884 }
71cdd40f
PT
885
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) {
892 last = 1;
893 goto fill_invalid_ext;
894 }
895 }
896
897write_done:
fe6e1e8d 898 wdata->res.count = wdata->args.count;
650e2d39
FI
899out:
900 bl_put_extent(be);
fe6e1e8d 901 bl_put_extent(cow_read);
650e2d39
FI
902 bl_submit_bio(WRITE, bio);
903 put_parallel(par);
904 return PNFS_ATTEMPTED;
7c5465d6
PT
905out_mds:
906 bl_put_extent(be);
fe6e1e8d 907 bl_put_extent(cow_read);
7c5465d6
PT
908 kfree(par);
909 return PNFS_NOT_ATTEMPTED;
155e7524
FI
910}
911
9e692969 912/* FIXME - range ignored */
155e7524 913static void
9e692969 914release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range)
155e7524 915{
9e692969
FI
916 int i;
917 struct pnfs_block_extent *be;
918
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,
924 be_node);
925 list_del(&be->be_node);
926 bl_put_extent(be);
927 }
928 }
929 spin_unlock(&bl->bl_ext_lock);
155e7524
FI
930}
931
155e7524
FI
932static void
933release_inval_marks(struct pnfs_inval_markings *marks)
934{
c1c2a4cd 935 struct pnfs_inval_tracking *pos, *temp;
7c5465d6 936 struct pnfs_block_short_extent *se, *stemp;
c1c2a4cd
FI
937
938 list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) {
939 list_del(&pos->it_link);
940 kfree(pos);
941 }
7c5465d6
PT
942
943 list_for_each_entry_safe(se, stemp, &marks->im_extents, bse_node) {
944 list_del(&se->bse_node);
945 kfree(se);
946 }
155e7524
FI
947 return;
948}
949
950static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
951{
952 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
953
954 dprintk("%s enter\n", __func__);
955 release_extents(bl, NULL);
956 release_inval_marks(&bl->bl_inval);
957 kfree(bl);
958}
959
960static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode,
961 gfp_t gfp_flags)
962{
963 struct pnfs_block_layout *bl;
964
965 dprintk("%s enter\n", __func__);
966 bl = kzalloc(sizeof(*bl), gfp_flags);
967 if (!bl)
968 return NULL;
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);
974 bl->bl_count = 0;
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;
978}
979
a60d2ebd 980static void bl_free_lseg(struct pnfs_layout_segment *lseg)
155e7524 981{
a60d2ebd
FI
982 dprintk("%s enter\n", __func__);
983 kfree(lseg);
155e7524
FI
984}
985
a60d2ebd
FI
986/* We pretty much ignore lseg, and store all data layout wide, so we
987 * can correctly merge.
988 */
989static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo,
990 struct nfs4_layoutget_res *lgr,
991 gfp_t gfp_flags)
155e7524 992{
a60d2ebd
FI
993 struct pnfs_layout_segment *lseg;
994 int status;
995
996 dprintk("%s enter\n", __func__);
997 lseg = kzalloc(sizeof(*lseg), gfp_flags);
998 if (!lseg)
999 return ERR_PTR(-ENOMEM);
1000 status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags);
1001 if (status) {
1002 /* We don't want to call the full-blown bl_free_lseg,
1003 * since on error extents were not touched.
1004 */
1005 kfree(lseg);
1006 return ERR_PTR(status);
1007 }
1008 return lseg;
155e7524
FI
1009}
1010
1011static void
1012bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr,
1013 const struct nfs4_layoutcommit_args *arg)
1014{
90ace12a
FI
1015 dprintk("%s enter\n", __func__);
1016 encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg);
155e7524
FI
1017}
1018
1019static void
1020bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
1021{
b2be7811
FI
1022 struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout;
1023
1024 dprintk("%s enter\n", __func__);
1025 clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status);
155e7524
FI
1026}
1027
2f9fd182
FI
1028static void free_blk_mountid(struct block_mount_id *mid)
1029{
1030 if (mid) {
93a3844e
PT
1031 struct pnfs_block_dev *dev, *tmp;
1032
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) {
2f9fd182
FI
1035 list_del(&dev->bm_node);
1036 bl_free_block_dev(dev);
1037 }
2f9fd182
FI
1038 kfree(mid);
1039 }
1040}
1041
78e4e05c 1042/* This is mostly copied from the filelayout_get_device_info function.
2f9fd182
FI
1043 * It seems much of this should be at the generic pnfs level.
1044 */
1045static struct pnfs_block_dev *
1046nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh,
1047 struct nfs4_deviceid *d_id)
1048{
1049 struct pnfs_device *dev;
516f2e24 1050 struct pnfs_block_dev *rv;
2f9fd182
FI
1051 u32 max_resp_sz;
1052 int max_pages;
1053 struct page **pages = NULL;
1054 int i, rc;
1055
1056 /*
1057 * Use the session max response size as the basis for setting
1058 * GETDEVICEINFO's maxcount
1059 */
1060 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
10bd295a 1061 max_pages = nfs_page_array_len(0, max_resp_sz);
2f9fd182
FI
1062 dprintk("%s max_resp_sz %u max_pages %d\n",
1063 __func__, max_resp_sz, max_pages);
1064
1065 dev = kmalloc(sizeof(*dev), GFP_NOFS);
1066 if (!dev) {
1067 dprintk("%s kmalloc failed\n", __func__);
516f2e24 1068 return ERR_PTR(-ENOMEM);
2f9fd182
FI
1069 }
1070
1071 pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS);
1072 if (pages == NULL) {
1073 kfree(dev);
516f2e24 1074 return ERR_PTR(-ENOMEM);
2f9fd182
FI
1075 }
1076 for (i = 0; i < max_pages; i++) {
1077 pages[i] = alloc_page(GFP_NOFS);
516f2e24
JR
1078 if (!pages[i]) {
1079 rv = ERR_PTR(-ENOMEM);
2f9fd182 1080 goto out_free;
516f2e24 1081 }
2f9fd182
FI
1082 }
1083
1084 memcpy(&dev->dev_id, d_id, sizeof(*d_id));
1085 dev->layout_type = LAYOUT_BLOCK_VOLUME;
1086 dev->pages = pages;
1087 dev->pgbase = 0;
1088 dev->pglen = PAGE_SIZE * max_pages;
1089 dev->mincount = 0;
1090
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);
516f2e24
JR
1094 if (rc) {
1095 rv = ERR_PTR(rc);
2f9fd182 1096 goto out_free;
516f2e24 1097 }
2f9fd182
FI
1098
1099 rv = nfs4_blk_decode_device(server, dev);
1100 out_free:
1101 for (i = 0; i < max_pages; i++)
1102 __free_page(pages[i]);
1103 kfree(pages);
1104 kfree(dev);
1105 return rv;
1106}
1107
155e7524
FI
1108static int
1109bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
1110{
2f9fd182
FI
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);
516f2e24 1115 int status, i;
2f9fd182 1116
155e7524 1117 dprintk("%s enter\n", __func__);
2f9fd182
FI
1118
1119 if (server->pnfs_blksize == 0) {
1120 dprintk("%s Server did not return blksize\n", __func__);
1121 return -EINVAL;
1122 }
1123 b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS);
1124 if (!b_mt_id) {
1125 status = -ENOMEM;
1126 goto out_error;
1127 }
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);
1131
1132 dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS);
1133 if (!dlist) {
1134 status = -ENOMEM;
1135 goto out_error;
1136 }
1137 dlist->eof = 0;
1138 while (!dlist->eof) {
1139 status = nfs4_proc_getdevicelist(server, fh, dlist);
1140 if (status)
1141 goto out_error;
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,
1146 &dlist->dev_id[i]);
516f2e24
JR
1147 if (IS_ERR(bdev)) {
1148 status = PTR_ERR(bdev);
2f9fd182
FI
1149 goto out_error;
1150 }
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);
1154 }
1155 }
1156 dprintk("%s SUCCESS\n", __func__);
1157 server->pnfs_ld_data = b_mt_id;
1158
1159 out_return:
1160 kfree(dlist);
1161 return status;
1162
1163 out_error:
1164 free_blk_mountid(b_mt_id);
1165 goto out_return;
155e7524
FI
1166}
1167
1168static int
1169bl_clear_layoutdriver(struct nfs_server *server)
1170{
2f9fd182
FI
1171 struct block_mount_id *b_mt_id = server->pnfs_ld_data;
1172
155e7524 1173 dprintk("%s enter\n", __func__);
2f9fd182
FI
1174 free_blk_mountid(b_mt_id);
1175 dprintk("%s RETURNS\n", __func__);
155e7524
FI
1176 return 0;
1177}
1178
f742dc4a
PT
1179static bool
1180is_aligned_req(struct nfs_page *req, unsigned int alignment)
1181{
1182 return IS_ALIGNED(req->wb_offset, alignment) &&
1183 IS_ALIGNED(req->wb_bytes, alignment);
1184}
1185
1186static void
1187bl_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1188{
1189 if (pgio->pg_dreq != NULL &&
1190 !is_aligned_req(req, SECTOR_SIZE))
1191 nfs_pageio_reset_read_mds(pgio);
1192 else
1193 pnfs_generic_pg_init_read(pgio, req);
1194}
1195
1196static bool
1197bl_pg_test_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
1198 struct nfs_page *req)
1199{
1200 if (pgio->pg_dreq != NULL &&
1201 !is_aligned_req(req, SECTOR_SIZE))
1202 return false;
1203
1204 return pnfs_generic_pg_test(pgio, prev, req);
1205}
1206
6f018efa 1207static void
96c9eae6
PT
1208bl_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1209{
1210 if (pgio->pg_dreq != NULL &&
1211 !is_aligned_req(req, PAGE_CACHE_SIZE))
1212 nfs_pageio_reset_write_mds(pgio);
1213 else
1214 pnfs_generic_pg_init_write(pgio, req);
1215}
1216
1217static bool
1218bl_pg_test_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
1219 struct nfs_page *req)
1220{
1221 if (pgio->pg_dreq != NULL &&
1222 !is_aligned_req(req, PAGE_CACHE_SIZE))
1223 return false;
1224
1225 return pnfs_generic_pg_test(pgio, prev, req);
1226}
1227
e9643fe8 1228static const struct nfs_pageio_ops bl_pg_read_ops = {
f742dc4a
PT
1229 .pg_init = bl_pg_init_read,
1230 .pg_test = bl_pg_test_read,
e9643fe8
BH
1231 .pg_doio = pnfs_generic_pg_readpages,
1232};
1233
1234static const struct nfs_pageio_ops bl_pg_write_ops = {
96c9eae6
PT
1235 .pg_init = bl_pg_init_write,
1236 .pg_test = bl_pg_test_write,
e9643fe8
BH
1237 .pg_doio = pnfs_generic_pg_writepages,
1238};
1239
155e7524
FI
1240static 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,
e9643fe8
BH
1253 .pg_read_ops = &bl_pg_read_ops,
1254 .pg_write_ops = &bl_pg_write_ops,
155e7524
FI
1255};
1256
fe0a9b74 1257static const struct rpc_pipe_ops bl_upcall_ops = {
c1225158 1258 .upcall = rpc_pipe_generic_upcall,
fe0a9b74
JR
1259 .downcall = bl_pipe_downcall,
1260 .destroy_msg = bl_pipe_destroy_msg,
1261};
1262
332dfab6
SK
1263static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb,
1264 struct rpc_pipe *pipe)
1265{
1266 struct dentry *dir, *dentry;
1267
1268 dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME);
1269 if (dir == NULL)
1270 return ERR_PTR(-ENOENT);
1271 dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe);
1272 dput(dir);
1273 return dentry;
1274}
1275
1276static void nfs4blocklayout_unregister_sb(struct super_block *sb,
1277 struct rpc_pipe *pipe)
1278{
1279 if (pipe->dentry)
1280 rpc_unlink(pipe->dentry);
1281}
1282
627f3066
SK
1283static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
1284 void *ptr)
1285{
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;
1290 int ret = 0;
1291
1292 if (!try_module_get(THIS_MODULE))
1293 return 0;
1294
1295 if (nn->bl_device_pipe == NULL) {
1296 module_put(THIS_MODULE);
1297 return 0;
1298 }
1299
1300 switch (event) {
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);
1305 break;
1306 }
1307 nn->bl_device_pipe->dentry = dentry;
1308 break;
1309 case RPC_PIPEFS_UMOUNT:
1310 if (nn->bl_device_pipe->dentry)
1311 nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe);
1312 break;
1313 default:
1314 ret = -ENOTSUPP;
1315 break;
1316 }
1317 module_put(THIS_MODULE);
1318 return ret;
1319}
1320
1321static struct notifier_block nfs4blocklayout_block = {
1322 .notifier_call = rpc_pipefs_event,
1323};
1324
332dfab6
SK
1325static struct dentry *nfs4blocklayout_register_net(struct net *net,
1326 struct rpc_pipe *pipe)
1327{
1328 struct super_block *pipefs_sb;
1329 struct dentry *dentry;
1330
1331 pipefs_sb = rpc_get_sb_net(net);
1332 if (!pipefs_sb)
2561d618 1333 return NULL;
332dfab6
SK
1334 dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe);
1335 rpc_put_sb_net(net);
1336 return dentry;
1337}
1338
1339static void nfs4blocklayout_unregister_net(struct net *net,
1340 struct rpc_pipe *pipe)
1341{
1342 struct super_block *pipefs_sb;
1343
1344 pipefs_sb = rpc_get_sb_net(net);
1345 if (pipefs_sb) {
1346 nfs4blocklayout_unregister_sb(pipefs_sb, pipe);
1347 rpc_put_sb_net(net);
1348 }
1349}
1350
9e2e74db
SK
1351static int nfs4blocklayout_net_init(struct net *net)
1352{
1353 struct nfs_net *nn = net_generic(net, nfs_net_id);
1354 struct dentry *dentry;
1355
5ffaf855 1356 init_waitqueue_head(&nn->bl_wq);
9e2e74db
SK
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);
1364 }
1365 nn->bl_device_pipe->dentry = dentry;
1366 return 0;
1367}
1368
1369static void nfs4blocklayout_net_exit(struct net *net)
1370{
1371 struct nfs_net *nn = net_generic(net, nfs_net_id);
1372
1373 nfs4blocklayout_unregister_net(net, nn->bl_device_pipe);
1374 rpc_destroy_pipe_data(nn->bl_device_pipe);
1375 nn->bl_device_pipe = NULL;
1376}
1377
1378static struct pernet_operations nfs4blocklayout_net_ops = {
1379 .init = nfs4blocklayout_net_init,
1380 .exit = nfs4blocklayout_net_exit,
1381};
1382
155e7524
FI
1383static int __init nfs4blocklayout_init(void)
1384{
1385 int ret;
1386
1387 dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
1388
1389 ret = pnfs_register_layoutdriver(&blocklayout_type);
fe0a9b74
JR
1390 if (ret)
1391 goto out;
1392
627f3066 1393 ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block);
9e2e74db
SK
1394 if (ret)
1395 goto out_remove;
627f3066
SK
1396 ret = register_pernet_subsys(&nfs4blocklayout_net_ops);
1397 if (ret)
1398 goto out_notifier;
fe0a9b74
JR
1399out:
1400 return ret;
1401
627f3066
SK
1402out_notifier:
1403 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
fe0a9b74
JR
1404out_remove:
1405 pnfs_unregister_layoutdriver(&blocklayout_type);
155e7524
FI
1406 return ret;
1407}
1408
1409static void __exit nfs4blocklayout_exit(void)
1410{
1411 dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
1412 __func__);
1413
627f3066 1414 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
9e2e74db 1415 unregister_pernet_subsys(&nfs4blocklayout_net_ops);
155e7524
FI
1416 pnfs_unregister_layoutdriver(&blocklayout_type);
1417}
1418
1419MODULE_ALIAS("nfs-layouttype4-3");
1420
1421module_init(nfs4blocklayout_init);
1422module_exit(nfs4blocklayout_exit);