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