pnfs/blocklayout: refactor extent processing
[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 */
88c9e421 38#include <linux/prefetch.h>
6296556f 39#include <linux/pagevec.h>
155e7524 40
10bd295a 41#include "../pnfs.h"
76e697ba 42#include "../nfs4session.h"
10bd295a 43#include "../internal.h"
155e7524
FI
44#include "blocklayout.h"
45
46#define NFSDBG_FACILITY NFSDBG_PNFS_LD
47
48MODULE_LICENSE("GPL");
49MODULE_AUTHOR("Andy Adamson <andros@citi.umich.edu>");
50MODULE_DESCRIPTION("The NFSv4.1 pNFS Block layout driver");
51
8067253c 52static bool is_hole(struct pnfs_block_extent *be)
9549ec01 53{
8067253c
CH
54 switch (be->be_state) {
55 case PNFS_BLOCK_NONE_DATA:
56 return true;
57 case PNFS_BLOCK_INVALID_DATA:
58 return be->be_tag ? false : true;
59 default:
60 return false;
61 }
650e2d39
FI
62}
63
9549ec01
FI
64/* The data we are handed might be spread across several bios. We need
65 * to track when the last one is finished.
66 */
67struct parallel_io {
68 struct kref refcnt;
8067253c 69 void (*pnfs_callback) (void *data);
9549ec01
FI
70 void *data;
71};
72
73static inline struct parallel_io *alloc_parallel(void *data)
74{
75 struct parallel_io *rv;
76
77 rv = kmalloc(sizeof(*rv), GFP_NOFS);
78 if (rv) {
79 rv->data = data;
80 kref_init(&rv->refcnt);
81 }
82 return rv;
83}
84
85static inline void get_parallel(struct parallel_io *p)
86{
87 kref_get(&p->refcnt);
88}
89
90static void destroy_parallel(struct kref *kref)
91{
92 struct parallel_io *p = container_of(kref, struct parallel_io, refcnt);
93
94 dprintk("%s enter\n", __func__);
8067253c 95 p->pnfs_callback(p->data);
9549ec01
FI
96 kfree(p);
97}
98
99static inline void put_parallel(struct parallel_io *p)
100{
101 kref_put(&p->refcnt, destroy_parallel);
102}
103
104static struct bio *
105bl_submit_bio(int rw, struct bio *bio)
106{
107 if (bio) {
108 get_parallel(bio->bi_private);
109 dprintk("%s submitting %s bio %u@%llu\n", __func__,
4f024f37
KO
110 rw == READ ? "read" : "write", bio->bi_iter.bi_size,
111 (unsigned long long)bio->bi_iter.bi_sector);
9549ec01
FI
112 submit_bio(rw, bio);
113 }
114 return NULL;
115}
116
117static struct bio *bl_alloc_init_bio(int npg, sector_t isect,
118 struct pnfs_block_extent *be,
119 void (*end_io)(struct bio *, int err),
120 struct parallel_io *par)
121{
20d655d6
CH
122 struct pnfs_block_dev *dev =
123 container_of(be->be_device, struct pnfs_block_dev, d_node);
9549ec01
FI
124 struct bio *bio;
125
74a6eeb4 126 npg = min(npg, BIO_MAX_PAGES);
9549ec01 127 bio = bio_alloc(GFP_NOIO, npg);
74a6eeb4
PT
128 if (!bio && (current->flags & PF_MEMALLOC)) {
129 while (!bio && (npg /= 2))
130 bio = bio_alloc(GFP_NOIO, npg);
131 }
9549ec01 132
74a6eeb4 133 if (bio) {
4f024f37
KO
134 bio->bi_iter.bi_sector = isect - be->be_f_offset +
135 be->be_v_offset;
20d655d6 136 bio->bi_bdev = dev->d_bdev;
74a6eeb4
PT
137 bio->bi_end_io = end_io;
138 bio->bi_private = par;
139 }
9549ec01
FI
140 return bio;
141}
142
fe6e1e8d 143static struct bio *do_add_page_to_bio(struct bio *bio, int npg, int rw,
9549ec01
FI
144 sector_t isect, struct page *page,
145 struct pnfs_block_extent *be,
146 void (*end_io)(struct bio *, int err),
fe6e1e8d
PT
147 struct parallel_io *par,
148 unsigned int offset, int len)
9549ec01 149{
fe6e1e8d
PT
150 isect = isect + (offset >> SECTOR_SHIFT);
151 dprintk("%s: npg %d rw %d isect %llu offset %u len %d\n", __func__,
152 npg, rw, (unsigned long long)isect, offset, len);
9549ec01
FI
153retry:
154 if (!bio) {
155 bio = bl_alloc_init_bio(npg, isect, be, end_io, par);
156 if (!bio)
157 return ERR_PTR(-ENOMEM);
158 }
fe6e1e8d 159 if (bio_add_page(bio, page, len, offset) < len) {
9549ec01
FI
160 bio = bl_submit_bio(rw, bio);
161 goto retry;
162 }
163 return bio;
164}
165
9549ec01
FI
166static void bl_end_io_read(struct bio *bio, int err)
167{
168 struct parallel_io *par = bio->bi_private;
9549ec01 169
2c30c71b 170 if (err) {
d45f60c6 171 struct nfs_pgio_header *header = par->data;
cd841605
FI
172
173 if (!header->pnfs_error)
174 header->pnfs_error = -EIO;
175 pnfs_set_lo_fail(header->lseg);
9549ec01 176 }
8c792ea9 177
9549ec01
FI
178 bio_put(bio);
179 put_parallel(par);
180}
181
182static void bl_read_cleanup(struct work_struct *work)
183{
184 struct rpc_task *task;
d45f60c6 185 struct nfs_pgio_header *hdr;
9549ec01
FI
186 dprintk("%s enter\n", __func__);
187 task = container_of(work, struct rpc_task, u.tk_work);
d45f60c6
WAA
188 hdr = container_of(task, struct nfs_pgio_header, task);
189 pnfs_ld_read_done(hdr);
9549ec01
FI
190}
191
192static void
8067253c 193bl_end_par_io_read(void *data)
9549ec01 194{
d45f60c6 195 struct nfs_pgio_header *hdr = data;
9549ec01 196
d45f60c6
WAA
197 hdr->task.tk_status = hdr->pnfs_error;
198 INIT_WORK(&hdr->task.u.tk_work, bl_read_cleanup);
199 schedule_work(&hdr->task.u.tk_work);
9549ec01
FI
200}
201
155e7524 202static enum pnfs_try_status
8067253c 203bl_read_pagelist(struct nfs_pgio_header *header)
155e7524 204{
8067253c 205 struct pnfs_block_layout *bl = BLK_LSEG2EXT(header->lseg);
9549ec01 206 struct bio *bio = NULL;
8067253c 207 struct pnfs_block_extent be;
9549ec01
FI
208 sector_t isect, extent_length = 0;
209 struct parallel_io *par;
8067253c
CH
210 loff_t f_offset = header->args.offset;
211 size_t bytes_left = header->args.count;
f742dc4a 212 unsigned int pg_offset, pg_len;
8067253c
CH
213 struct page **pages = header->args.pages;
214 int pg_index = header->args.pgbase >> PAGE_CACHE_SHIFT;
f742dc4a 215 const bool is_dio = (header->dreq != NULL);
be98fd0a 216 struct blk_plug plug;
8067253c 217 int i;
9549ec01 218
6f00866d 219 dprintk("%s enter nr_pages %u offset %lld count %u\n", __func__,
8067253c
CH
220 header->page_array.npages, f_offset,
221 (unsigned int)header->args.count);
9549ec01 222
8067253c 223 par = alloc_parallel(header);
9549ec01 224 if (!par)
8067253c 225 return PNFS_NOT_ATTEMPTED;
9549ec01 226 par->pnfs_callback = bl_end_par_io_read;
9549ec01 227
be98fd0a
CH
228 blk_start_plug(&plug);
229
9549ec01
FI
230 isect = (sector_t) (f_offset >> SECTOR_SHIFT);
231 /* Code assumes extents are page-aligned */
8067253c 232 for (i = pg_index; i < header->page_array.npages; i++) {
921b81a8 233 if (extent_length <= 0) {
9549ec01 234 /* We've used up the previous extent */
9549ec01 235 bio = bl_submit_bio(READ, bio);
8067253c 236
9549ec01 237 /* Get the next one */
8067253c 238 if (!ext_tree_lookup(bl, isect, &be, false)) {
cd841605 239 header->pnfs_error = -EIO;
9549ec01
FI
240 goto out;
241 }
8067253c 242 extent_length = be.be_length - (isect - be.be_f_offset);
9549ec01 243 }
f742dc4a 244
3a6fd1f0 245 pg_offset = f_offset & ~PAGE_CACHE_MASK;
f742dc4a 246 if (is_dio) {
f742dc4a
PT
247 if (pg_offset + bytes_left > PAGE_CACHE_SIZE)
248 pg_len = PAGE_CACHE_SIZE - pg_offset;
249 else
250 pg_len = bytes_left;
251
252 f_offset += pg_len;
253 bytes_left -= pg_len;
254 isect += (pg_offset >> SECTOR_SHIFT);
921b81a8 255 extent_length -= (pg_offset >> SECTOR_SHIFT);
f742dc4a 256 } else {
3a6fd1f0 257 BUG_ON(pg_offset != 0);
f742dc4a
PT
258 pg_len = PAGE_CACHE_SIZE;
259 }
260
8067253c 261 if (is_hole(&be)) {
9549ec01
FI
262 bio = bl_submit_bio(READ, bio);
263 /* Fill hole w/ zeroes w/o accessing device */
264 dprintk("%s Zeroing page for hole\n", __func__);
f742dc4a 265 zero_user_segment(pages[i], pg_offset, pg_len);
9549ec01 266 } else {
823b0c9d 267 bio = do_add_page_to_bio(bio,
8067253c 268 header->page_array.npages - i,
30dd374f 269 READ,
8067253c 270 isect, pages[i], &be,
f742dc4a
PT
271 bl_end_io_read, par,
272 pg_offset, pg_len);
9549ec01 273 if (IS_ERR(bio)) {
cd841605 274 header->pnfs_error = PTR_ERR(bio);
e6d05a75 275 bio = NULL;
9549ec01
FI
276 goto out;
277 }
278 }
f742dc4a 279 isect += (pg_len >> SECTOR_SHIFT);
921b81a8 280 extent_length -= (pg_len >> SECTOR_SHIFT);
9549ec01 281 }
cd841605 282 if ((isect << SECTOR_SHIFT) >= header->inode->i_size) {
8067253c
CH
283 header->res.eof = 1;
284 header->res.count = header->inode->i_size - header->args.offset;
9549ec01 285 } else {
8067253c 286 header->res.count = (isect << SECTOR_SHIFT) - header->args.offset;
9549ec01
FI
287 }
288out:
9549ec01 289 bl_submit_bio(READ, bio);
be98fd0a 290 blk_finish_plug(&plug);
9549ec01
FI
291 put_parallel(par);
292 return PNFS_ATTEMPTED;
31e6306a
FI
293}
294
650e2d39
FI
295static void bl_end_io_write(struct bio *bio, int err)
296{
297 struct parallel_io *par = bio->bi_private;
298 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
d45f60c6 299 struct nfs_pgio_header *header = par->data;
650e2d39
FI
300
301 if (!uptodate) {
cd841605
FI
302 if (!header->pnfs_error)
303 header->pnfs_error = -EIO;
304 pnfs_set_lo_fail(header->lseg);
650e2d39
FI
305 }
306 bio_put(bio);
307 put_parallel(par);
308}
309
310/* Function scheduled for call during bl_end_par_io_write,
311 * it marks sectors as written and extends the commitlist.
312 */
313static void bl_write_cleanup(struct work_struct *work)
314{
8067253c
CH
315 struct rpc_task *task = container_of(work, struct rpc_task, u.tk_work);
316 struct nfs_pgio_header *hdr =
317 container_of(task, struct nfs_pgio_header, task);
318
650e2d39 319 dprintk("%s enter\n", __func__);
8067253c 320
d45f60c6 321 if (likely(!hdr->pnfs_error)) {
8067253c
CH
322 struct pnfs_block_layout *bl = BLK_LSEG2EXT(hdr->lseg);
323 u64 start = hdr->args.offset & (loff_t)PAGE_CACHE_MASK;
324 u64 end = (hdr->args.offset + hdr->args.count +
325 PAGE_CACHE_SIZE - 1) & (loff_t)PAGE_CACHE_MASK;
326
327 ext_tree_mark_written(bl, start >> SECTOR_SHIFT,
328 (end - start) >> SECTOR_SHIFT);
31e6306a 329 }
8067253c 330
d45f60c6 331 pnfs_ld_write_done(hdr);
650e2d39
FI
332}
333
334/* Called when last of bios associated with a bl_write_pagelist call finishes */
8067253c 335static void bl_end_par_io_write(void *data)
650e2d39 336{
d45f60c6 337 struct nfs_pgio_header *hdr = data;
650e2d39 338
d45f60c6 339 hdr->task.tk_status = hdr->pnfs_error;
c65e6254 340 hdr->verf.committed = NFS_FILE_SYNC;
d45f60c6
WAA
341 INIT_WORK(&hdr->task.u.tk_work, bl_write_cleanup);
342 schedule_work(&hdr->task.u.tk_work);
650e2d39
FI
343}
344
155e7524 345static enum pnfs_try_status
d45f60c6 346bl_write_pagelist(struct nfs_pgio_header *header, int sync)
155e7524 347{
8067253c 348 struct pnfs_block_layout *bl = BLK_LSEG2EXT(header->lseg);
650e2d39 349 struct bio *bio = NULL;
8067253c 350 struct pnfs_block_extent be;
3a6fd1f0 351 sector_t isect, extent_length = 0;
96c9eae6 352 struct parallel_io *par = NULL;
d45f60c6
WAA
353 loff_t offset = header->args.offset;
354 size_t count = header->args.count;
d45f60c6 355 struct page **pages = header->args.pages;
3a6fd1f0 356 int pg_index = pg_index = header->args.pgbase >> PAGE_CACHE_SHIFT;
be98fd0a 357 struct blk_plug plug;
8067253c 358 int i;
650e2d39
FI
359
360 dprintk("%s enter, %Zu@%lld\n", __func__, count, offset);
96c9eae6 361
d45f60c6 362 /* At this point, header->page_aray is a (sequential) list of nfs_pages.
71cdd40f
PT
363 * We want to write each, and if there is an error set pnfs_error
364 * to have it redone using nfs.
650e2d39 365 */
d45f60c6 366 par = alloc_parallel(header);
650e2d39 367 if (!par)
8067253c 368 return PNFS_NOT_ATTEMPTED;
650e2d39 369 par->pnfs_callback = bl_end_par_io_write;
650e2d39 370
3a6fd1f0 371 blk_start_plug(&plug);
71cdd40f 372
3a6fd1f0
CH
373 /* we always write out the whole page */
374 offset = offset & (loff_t)PAGE_CACHE_MASK;
375 isect = offset >> SECTOR_SHIFT;
71cdd40f 376
d45f60c6 377 for (i = pg_index; i < header->page_array.npages; i++) {
921b81a8 378 if (extent_length <= 0) {
650e2d39 379 /* We've used up the previous extent */
650e2d39
FI
380 bio = bl_submit_bio(WRITE, bio);
381 /* Get the next one */
8067253c 382 if (!ext_tree_lookup(bl, isect, &be, true)) {
cd841605 383 header->pnfs_error = -EINVAL;
650e2d39
FI
384 goto out;
385 }
fe6e1e8d 386
8067253c 387 extent_length = be.be_length - (isect - be.be_f_offset);
71cdd40f 388 }
fe6e1e8d 389
d45f60c6 390 bio = do_add_page_to_bio(bio, header->page_array.npages - i,
8067253c 391 WRITE, isect, pages[i], &be,
fe6e1e8d 392 bl_end_io_write, par,
3a6fd1f0 393 0, PAGE_CACHE_SIZE);
71cdd40f 394 if (IS_ERR(bio)) {
cd841605 395 header->pnfs_error = PTR_ERR(bio);
e6d05a75 396 bio = NULL;
71cdd40f 397 goto out;
650e2d39 398 }
3a6fd1f0
CH
399 offset += PAGE_CACHE_SIZE;
400 count -= PAGE_CACHE_SIZE;
650e2d39
FI
401 isect += PAGE_CACHE_SECTORS;
402 extent_length -= PAGE_CACHE_SECTORS;
403 }
71cdd40f 404
d45f60c6 405 header->res.count = header->args.count;
650e2d39 406out:
650e2d39 407 bl_submit_bio(WRITE, bio);
be98fd0a 408 blk_finish_plug(&plug);
650e2d39
FI
409 put_parallel(par);
410 return PNFS_ATTEMPTED;
155e7524
FI
411}
412
413static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
414{
415 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
8067253c 416 int err;
155e7524
FI
417
418 dprintk("%s enter\n", __func__);
8067253c
CH
419
420 err = ext_tree_remove(bl, true, 0, LLONG_MAX);
421 WARN_ON(err);
422
155e7524
FI
423 kfree(bl);
424}
425
426static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode,
427 gfp_t gfp_flags)
428{
429 struct pnfs_block_layout *bl;
430
431 dprintk("%s enter\n", __func__);
432 bl = kzalloc(sizeof(*bl), gfp_flags);
433 if (!bl)
434 return NULL;
8067253c
CH
435
436 bl->bl_ext_rw = RB_ROOT;
437 bl->bl_ext_ro = RB_ROOT;
155e7524 438 spin_lock_init(&bl->bl_ext_lock);
8067253c 439
155e7524
FI
440 return &bl->bl_layout;
441}
442
a60d2ebd 443static void bl_free_lseg(struct pnfs_layout_segment *lseg)
155e7524 444{
a60d2ebd
FI
445 dprintk("%s enter\n", __func__);
446 kfree(lseg);
155e7524
FI
447}
448
9cc47541
CH
449/* Tracks info needed to ensure extents in layout obey constraints of spec */
450struct layout_verification {
451 u32 mode; /* R or RW */
452 u64 start; /* Expected start of next non-COW extent */
453 u64 inval; /* Start of INVAL coverage */
454 u64 cowread; /* End of COW read coverage */
455};
456
457/* Verify the extent meets the layout requirements of the pnfs-block draft,
458 * section 2.3.1.
459 */
460static int verify_extent(struct pnfs_block_extent *be,
461 struct layout_verification *lv)
462{
463 if (lv->mode == IOMODE_READ) {
464 if (be->be_state == PNFS_BLOCK_READWRITE_DATA ||
465 be->be_state == PNFS_BLOCK_INVALID_DATA)
466 return -EIO;
467 if (be->be_f_offset != lv->start)
468 return -EIO;
469 lv->start += be->be_length;
470 return 0;
471 }
472 /* lv->mode == IOMODE_RW */
473 if (be->be_state == PNFS_BLOCK_READWRITE_DATA) {
474 if (be->be_f_offset != lv->start)
475 return -EIO;
476 if (lv->cowread > lv->start)
477 return -EIO;
478 lv->start += be->be_length;
479 lv->inval = lv->start;
480 return 0;
481 } else if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
482 if (be->be_f_offset != lv->start)
483 return -EIO;
484 lv->start += be->be_length;
485 return 0;
486 } else if (be->be_state == PNFS_BLOCK_READ_DATA) {
487 if (be->be_f_offset > lv->start)
488 return -EIO;
489 if (be->be_f_offset < lv->inval)
490 return -EIO;
491 if (be->be_f_offset < lv->cowread)
492 return -EIO;
493 /* It looks like you might want to min this with lv->start,
494 * but you really don't.
495 */
496 lv->inval = lv->inval + be->be_length;
497 lv->cowread = be->be_f_offset + be->be_length;
498 return 0;
499 } else
500 return -EIO;
501}
502
503static int decode_sector_number(__be32 **rp, sector_t *sp)
504{
505 uint64_t s;
506
507 *rp = xdr_decode_hyper(*rp, &s);
508 if (s & 0x1ff) {
509 printk(KERN_WARNING "NFS: %s: sector not aligned\n", __func__);
510 return -1;
511 }
512 *sp = s >> SECTOR_SHIFT;
513 return 0;
514}
515
9cc47541 516static int
ca0fe1df
CH
517bl_alloc_extent(struct xdr_stream *xdr, struct pnfs_layout_hdr *lo,
518 struct layout_verification *lv, struct list_head *extents,
519 gfp_t gfp_mask)
9cc47541 520{
ca0fe1df
CH
521 struct pnfs_block_extent *be;
522 struct nfs4_deviceid id;
523 int error;
9cc47541 524 __be32 *p;
ca0fe1df
CH
525
526 p = xdr_inline_decode(xdr, 28 + NFS4_DEVICEID4_SIZE);
527 if (!p)
528 return -EIO;
529
530 be = kzalloc(sizeof(*be), GFP_NOFS);
531 if (!be)
532 return -ENOMEM;
533
534 memcpy(&id, p, NFS4_DEVICEID4_SIZE);
535 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
536
537 error = -EIO;
538 be->be_device = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode), &id,
539 lo->plh_lc_cred, gfp_mask);
540 if (!be->be_device)
541 goto out_free_be;
542
543 /*
544 * The next three values are read in as bytes, but stored in the
545 * extent structure in 512-byte granularity.
546 */
547 if (decode_sector_number(&p, &be->be_f_offset) < 0)
548 goto out_put_deviceid;
549 if (decode_sector_number(&p, &be->be_length) < 0)
550 goto out_put_deviceid;
551 if (decode_sector_number(&p, &be->be_v_offset) < 0)
552 goto out_put_deviceid;
553 be->be_state = be32_to_cpup(p++);
554
555 error = verify_extent(be, lv);
556 if (error) {
557 dprintk("%s: extent verification failed\n", __func__);
558 goto out_put_deviceid;
559 }
560
561 list_add_tail(&be->be_list, extents);
562 return 0;
563
564out_put_deviceid:
565 nfs4_put_deviceid_node(be->be_device);
566out_free_be:
567 kfree(be);
568 return error;
569}
570
571static struct pnfs_layout_segment *
572bl_alloc_lseg(struct pnfs_layout_hdr *lo, struct nfs4_layoutget_res *lgr,
573 gfp_t gfp_mask)
574{
9cc47541
CH
575 struct layout_verification lv = {
576 .mode = lgr->range.iomode,
577 .start = lgr->range.offset >> SECTOR_SHIFT,
578 .inval = lgr->range.offset >> SECTOR_SHIFT,
579 .cowread = lgr->range.offset >> SECTOR_SHIFT,
580 };
ca0fe1df
CH
581 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
582 struct pnfs_layout_segment *lseg;
583 struct xdr_buf buf;
584 struct xdr_stream xdr;
585 struct page *scratch;
586 int status, i;
587 uint32_t count;
588 __be32 *p;
9cc47541
CH
589 LIST_HEAD(extents);
590
591 dprintk("---> %s\n", __func__);
592
ca0fe1df
CH
593 lseg = kzalloc(sizeof(*lseg), gfp_mask);
594 if (!lseg)
595 return ERR_PTR(-ENOMEM);
596
597 status = -ENOMEM;
598 scratch = alloc_page(gfp_mask);
9cc47541 599 if (!scratch)
ca0fe1df 600 goto out;
9cc47541 601
ca0fe1df
CH
602 xdr_init_decode_pages(&xdr, &buf,
603 lgr->layoutp->pages, lgr->layoutp->len);
604 xdr_set_scratch_buffer(&xdr, page_address(scratch), PAGE_SIZE);
9cc47541 605
ca0fe1df
CH
606 status = -EIO;
607 p = xdr_inline_decode(&xdr, 4);
9cc47541 608 if (unlikely(!p))
ca0fe1df 609 goto out_free_scratch;
9cc47541
CH
610
611 count = be32_to_cpup(p++);
ca0fe1df 612 dprintk("%s: number of extents %d\n", __func__, count);
9cc47541 613
ca0fe1df
CH
614 /*
615 * Decode individual extents, putting them in temporary staging area
616 * until whole layout is decoded to make error recovery easier.
9cc47541
CH
617 */
618 for (i = 0; i < count; i++) {
ca0fe1df
CH
619 status = bl_alloc_extent(&xdr, lo, &lv, &extents, gfp_mask);
620 if (status)
621 goto process_extents;
9cc47541 622 }
ca0fe1df 623
9cc47541
CH
624 if (lgr->range.offset + lgr->range.length !=
625 lv.start << SECTOR_SHIFT) {
626 dprintk("%s Final length mismatch\n", __func__);
ca0fe1df
CH
627 status = -EIO;
628 goto process_extents;
9cc47541 629 }
ca0fe1df 630
9cc47541
CH
631 if (lv.start < lv.cowread) {
632 dprintk("%s Final uncovered COW extent\n", __func__);
ca0fe1df 633 status = -EIO;
9cc47541 634 }
9cc47541 635
ca0fe1df 636process_extents:
9cc47541 637 while (!list_empty(&extents)) {
ca0fe1df
CH
638 struct pnfs_block_extent *be =
639 list_first_entry(&extents, struct pnfs_block_extent,
640 be_list);
9cc47541 641 list_del(&be->be_list);
9cc47541 642
ca0fe1df
CH
643 if (!status)
644 status = ext_tree_insert(bl, be);
a60d2ebd 645
ca0fe1df
CH
646 if (status) {
647 nfs4_put_deviceid_node(be->be_device);
648 kfree(be);
649 }
650 }
651
652out_free_scratch:
653 __free_page(scratch);
654out:
655 dprintk("%s returns %d\n", __func__, status);
a60d2ebd 656 if (status) {
a60d2ebd
FI
657 kfree(lseg);
658 return ERR_PTR(status);
659 }
660 return lseg;
155e7524
FI
661}
662
71d5b763
CH
663static void
664bl_return_range(struct pnfs_layout_hdr *lo,
665 struct pnfs_layout_range *range)
666{
667 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
668 sector_t offset = range->offset >> SECTOR_SHIFT, end;
669 int err;
670
671 if (range->offset % 8) {
672 dprintk("%s: offset %lld not block size aligned\n",
673 __func__, range->offset);
674 return;
675 }
676
677 if (range->length != NFS4_MAX_UINT64) {
678 if (range->length % 8) {
679 dprintk("%s: length %lld not block size aligned\n",
680 __func__, range->length);
681 return;
682 }
683
684 end = offset + (range->length >> SECTOR_SHIFT);
685 } else {
686 end = round_down(NFS4_MAX_UINT64, PAGE_SIZE);
687 }
688
689 err = ext_tree_remove(bl, range->iomode & IOMODE_RW, offset, end);
690}
691
34dc93c2
CH
692static int
693bl_prepare_layoutcommit(struct nfs4_layoutcommit_args *arg)
155e7524 694{
34dc93c2 695 return ext_tree_prepare_commit(arg);
155e7524
FI
696}
697
698static void
699bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
700{
34dc93c2 701 ext_tree_mark_committed(&lcdata->args, lcdata->res.status);
155e7524
FI
702}
703
704static int
705bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
706{
707 dprintk("%s enter\n", __func__);
2f9fd182
FI
708
709 if (server->pnfs_blksize == 0) {
710 dprintk("%s Server did not return blksize\n", __func__);
711 return -EINVAL;
712 }
e3aaf7f2
CH
713 if (server->pnfs_blksize > PAGE_SIZE) {
714 printk(KERN_ERR "%s: pNFS blksize %d not supported.\n",
715 __func__, server->pnfs_blksize);
716 return -EINVAL;
717 }
718
d4b18c3e 719 return 0;
155e7524
FI
720}
721
f742dc4a 722static bool
3a6fd1f0
CH
723is_aligned_req(struct nfs_pageio_descriptor *pgio,
724 struct nfs_page *req, unsigned int alignment)
f742dc4a 725{
3a6fd1f0
CH
726 /*
727 * Always accept buffered writes, higher layers take care of the
728 * right alignment.
729 */
730 if (pgio->pg_dreq == NULL)
731 return true;
732
733 if (!IS_ALIGNED(req->wb_offset, alignment))
734 return false;
735
736 if (IS_ALIGNED(req->wb_bytes, alignment))
737 return true;
738
739 if (req_offset(req) + req->wb_bytes == i_size_read(pgio->pg_inode)) {
740 /*
741 * If the write goes up to the inode size, just write
742 * the full page. Data past the inode size is
743 * guaranteed to be zeroed by the higher level client
744 * code, and this behaviour is mandated by RFC 5663
745 * section 2.3.2.
746 */
747 return true;
748 }
749
750 return false;
f742dc4a
PT
751}
752
753static void
754bl_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
755{
3a6fd1f0 756 if (!is_aligned_req(pgio, req, SECTOR_SIZE)) {
f742dc4a 757 nfs_pageio_reset_read_mds(pgio);
3a6fd1f0
CH
758 return;
759 }
760
761 pnfs_generic_pg_init_read(pgio, req);
f742dc4a
PT
762}
763
b4fdac1a
WAA
764/*
765 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
766 * of bytes (maximum @req->wb_bytes) that can be coalesced.
767 */
768static size_t
f742dc4a
PT
769bl_pg_test_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
770 struct nfs_page *req)
771{
3a6fd1f0 772 if (!is_aligned_req(pgio, req, SECTOR_SIZE))
b4fdac1a 773 return 0;
f742dc4a
PT
774 return pnfs_generic_pg_test(pgio, prev, req);
775}
776
6296556f
PT
777/*
778 * Return the number of contiguous bytes for a given inode
779 * starting at page frame idx.
780 */
781static u64 pnfs_num_cont_bytes(struct inode *inode, pgoff_t idx)
782{
783 struct address_space *mapping = inode->i_mapping;
784 pgoff_t end;
785
786 /* Optimize common case that writes from 0 to end of file */
787 end = DIV_ROUND_UP(i_size_read(inode), PAGE_CACHE_SIZE);
788 if (end != NFS_I(inode)->npages) {
789 rcu_read_lock();
e7b563bb 790 end = page_cache_next_hole(mapping, idx + 1, ULONG_MAX);
6296556f
PT
791 rcu_read_unlock();
792 }
793
794 if (!end)
795 return i_size_read(inode) - (idx << PAGE_CACHE_SHIFT);
796 else
797 return (end - idx) << PAGE_CACHE_SHIFT;
798}
799
6f018efa 800static void
96c9eae6
PT
801bl_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
802{
3a6fd1f0
CH
803 u64 wb_size;
804
805 if (!is_aligned_req(pgio, req, PAGE_SIZE)) {
96c9eae6 806 nfs_pageio_reset_write_mds(pgio);
3a6fd1f0 807 return;
6296556f 808 }
3a6fd1f0
CH
809
810 if (pgio->pg_dreq == NULL)
811 wb_size = pnfs_num_cont_bytes(pgio->pg_inode,
812 req->wb_index);
813 else
814 wb_size = nfs_dreq_bytes_left(pgio->pg_dreq);
815
816 pnfs_generic_pg_init_write(pgio, req, wb_size);
96c9eae6
PT
817}
818
b4fdac1a
WAA
819/*
820 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
821 * of bytes (maximum @req->wb_bytes) that can be coalesced.
822 */
823static size_t
96c9eae6
PT
824bl_pg_test_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
825 struct nfs_page *req)
826{
3a6fd1f0 827 if (!is_aligned_req(pgio, req, PAGE_SIZE))
b4fdac1a 828 return 0;
96c9eae6
PT
829 return pnfs_generic_pg_test(pgio, prev, req);
830}
831
e9643fe8 832static const struct nfs_pageio_ops bl_pg_read_ops = {
f742dc4a
PT
833 .pg_init = bl_pg_init_read,
834 .pg_test = bl_pg_test_read,
e9643fe8
BH
835 .pg_doio = pnfs_generic_pg_readpages,
836};
837
838static const struct nfs_pageio_ops bl_pg_write_ops = {
96c9eae6
PT
839 .pg_init = bl_pg_init_write,
840 .pg_test = bl_pg_test_write,
e9643fe8
BH
841 .pg_doio = pnfs_generic_pg_writepages,
842};
843
155e7524
FI
844static struct pnfs_layoutdriver_type blocklayout_type = {
845 .id = LAYOUT_BLOCK_VOLUME,
846 .name = "LAYOUT_BLOCK_VOLUME",
5a12cca6 847 .owner = THIS_MODULE,
848746bd
CH
848 .flags = PNFS_LAYOUTRET_ON_SETATTR |
849 PNFS_READ_WHOLE_PAGE,
155e7524
FI
850 .read_pagelist = bl_read_pagelist,
851 .write_pagelist = bl_write_pagelist,
852 .alloc_layout_hdr = bl_alloc_layout_hdr,
853 .free_layout_hdr = bl_free_layout_hdr,
854 .alloc_lseg = bl_alloc_lseg,
855 .free_lseg = bl_free_lseg,
71d5b763 856 .return_range = bl_return_range,
34dc93c2 857 .prepare_layoutcommit = bl_prepare_layoutcommit,
155e7524
FI
858 .cleanup_layoutcommit = bl_cleanup_layoutcommit,
859 .set_layoutdriver = bl_set_layoutdriver,
20d655d6
CH
860 .alloc_deviceid_node = bl_alloc_deviceid_node,
861 .free_deviceid_node = bl_free_deviceid_node,
e9643fe8
BH
862 .pg_read_ops = &bl_pg_read_ops,
863 .pg_write_ops = &bl_pg_write_ops,
155e7524
FI
864};
865
fe0a9b74 866static const struct rpc_pipe_ops bl_upcall_ops = {
c1225158 867 .upcall = rpc_pipe_generic_upcall,
fe0a9b74
JR
868 .downcall = bl_pipe_downcall,
869 .destroy_msg = bl_pipe_destroy_msg,
870};
871
332dfab6
SK
872static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb,
873 struct rpc_pipe *pipe)
874{
875 struct dentry *dir, *dentry;
876
877 dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME);
878 if (dir == NULL)
879 return ERR_PTR(-ENOENT);
880 dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe);
881 dput(dir);
882 return dentry;
883}
884
885static void nfs4blocklayout_unregister_sb(struct super_block *sb,
886 struct rpc_pipe *pipe)
887{
888 if (pipe->dentry)
889 rpc_unlink(pipe->dentry);
890}
891
627f3066
SK
892static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
893 void *ptr)
894{
895 struct super_block *sb = ptr;
896 struct net *net = sb->s_fs_info;
897 struct nfs_net *nn = net_generic(net, nfs_net_id);
898 struct dentry *dentry;
899 int ret = 0;
900
901 if (!try_module_get(THIS_MODULE))
902 return 0;
903
904 if (nn->bl_device_pipe == NULL) {
905 module_put(THIS_MODULE);
906 return 0;
907 }
908
909 switch (event) {
910 case RPC_PIPEFS_MOUNT:
911 dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe);
912 if (IS_ERR(dentry)) {
913 ret = PTR_ERR(dentry);
914 break;
915 }
916 nn->bl_device_pipe->dentry = dentry;
917 break;
918 case RPC_PIPEFS_UMOUNT:
919 if (nn->bl_device_pipe->dentry)
920 nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe);
921 break;
922 default:
923 ret = -ENOTSUPP;
924 break;
925 }
926 module_put(THIS_MODULE);
927 return ret;
928}
929
930static struct notifier_block nfs4blocklayout_block = {
931 .notifier_call = rpc_pipefs_event,
932};
933
332dfab6
SK
934static struct dentry *nfs4blocklayout_register_net(struct net *net,
935 struct rpc_pipe *pipe)
936{
937 struct super_block *pipefs_sb;
938 struct dentry *dentry;
939
940 pipefs_sb = rpc_get_sb_net(net);
941 if (!pipefs_sb)
2561d618 942 return NULL;
332dfab6
SK
943 dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe);
944 rpc_put_sb_net(net);
945 return dentry;
946}
947
948static void nfs4blocklayout_unregister_net(struct net *net,
949 struct rpc_pipe *pipe)
950{
951 struct super_block *pipefs_sb;
952
953 pipefs_sb = rpc_get_sb_net(net);
954 if (pipefs_sb) {
955 nfs4blocklayout_unregister_sb(pipefs_sb, pipe);
956 rpc_put_sb_net(net);
957 }
958}
959
9e2e74db
SK
960static int nfs4blocklayout_net_init(struct net *net)
961{
962 struct nfs_net *nn = net_generic(net, nfs_net_id);
963 struct dentry *dentry;
964
5ffaf855 965 init_waitqueue_head(&nn->bl_wq);
9e2e74db
SK
966 nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0);
967 if (IS_ERR(nn->bl_device_pipe))
968 return PTR_ERR(nn->bl_device_pipe);
969 dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe);
970 if (IS_ERR(dentry)) {
971 rpc_destroy_pipe_data(nn->bl_device_pipe);
972 return PTR_ERR(dentry);
973 }
974 nn->bl_device_pipe->dentry = dentry;
975 return 0;
976}
977
978static void nfs4blocklayout_net_exit(struct net *net)
979{
980 struct nfs_net *nn = net_generic(net, nfs_net_id);
981
982 nfs4blocklayout_unregister_net(net, nn->bl_device_pipe);
983 rpc_destroy_pipe_data(nn->bl_device_pipe);
984 nn->bl_device_pipe = NULL;
985}
986
987static struct pernet_operations nfs4blocklayout_net_ops = {
988 .init = nfs4blocklayout_net_init,
989 .exit = nfs4blocklayout_net_exit,
990};
991
155e7524
FI
992static int __init nfs4blocklayout_init(void)
993{
994 int ret;
995
996 dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
997
998 ret = pnfs_register_layoutdriver(&blocklayout_type);
fe0a9b74
JR
999 if (ret)
1000 goto out;
1001
627f3066 1002 ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block);
9e2e74db
SK
1003 if (ret)
1004 goto out_remove;
627f3066
SK
1005 ret = register_pernet_subsys(&nfs4blocklayout_net_ops);
1006 if (ret)
1007 goto out_notifier;
fe0a9b74
JR
1008out:
1009 return ret;
1010
627f3066
SK
1011out_notifier:
1012 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
fe0a9b74
JR
1013out_remove:
1014 pnfs_unregister_layoutdriver(&blocklayout_type);
155e7524
FI
1015 return ret;
1016}
1017
1018static void __exit nfs4blocklayout_exit(void)
1019{
1020 dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
1021 __func__);
1022
627f3066 1023 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
9e2e74db 1024 unregister_pernet_subsys(&nfs4blocklayout_net_ops);
155e7524
FI
1025 pnfs_unregister_layoutdriver(&blocklayout_type);
1026}
1027
1028MODULE_ALIAS("nfs-layouttype4-3");
1029
1030module_init(nfs4blocklayout_init);
1031module_exit(nfs4blocklayout_exit);