NFS: use req_offset where appropriate
[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
165static struct bio *bl_add_page_to_bio(struct bio *bio, int npg, int rw,
166 sector_t isect, struct page *page,
167 struct pnfs_block_extent *be,
168 void (*end_io)(struct bio *, int err),
169 struct parallel_io *par)
170{
171retry:
172 if (!bio) {
173 bio = bl_alloc_init_bio(npg, isect, be, end_io, par);
174 if (!bio)
175 return ERR_PTR(-ENOMEM);
176 }
177 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
178 bio = bl_submit_bio(rw, bio);
179 goto retry;
180 }
181 return bio;
182}
183
9549ec01
FI
184/* This is basically copied from mpage_end_io_read */
185static void bl_end_io_read(struct bio *bio, int err)
186{
187 struct parallel_io *par = bio->bi_private;
188 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
189 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
190 struct nfs_read_data *rdata = (struct nfs_read_data *)par->data;
191
192 do {
193 struct page *page = bvec->bv_page;
194
195 if (--bvec >= bio->bi_io_vec)
196 prefetchw(&bvec->bv_page->flags);
197 if (uptodate)
198 SetPageUptodate(page);
199 } while (bvec >= bio->bi_io_vec);
200 if (!uptodate) {
201 if (!rdata->pnfs_error)
202 rdata->pnfs_error = -EIO;
1b0ae068 203 pnfs_set_lo_fail(rdata->lseg);
9549ec01
FI
204 }
205 bio_put(bio);
206 put_parallel(par);
207}
208
209static void bl_read_cleanup(struct work_struct *work)
210{
211 struct rpc_task *task;
212 struct nfs_read_data *rdata;
213 dprintk("%s enter\n", __func__);
214 task = container_of(work, struct rpc_task, u.tk_work);
215 rdata = container_of(task, struct nfs_read_data, task);
216 pnfs_ld_read_done(rdata);
217}
218
219static void
7c5465d6 220bl_end_par_io_read(void *data, int unused)
9549ec01
FI
221{
222 struct nfs_read_data *rdata = data;
223
82b906d6 224 rdata->task.tk_status = rdata->pnfs_error;
9549ec01
FI
225 INIT_WORK(&rdata->task.u.tk_work, bl_read_cleanup);
226 schedule_work(&rdata->task.u.tk_work);
227}
228
155e7524
FI
229static enum pnfs_try_status
230bl_read_pagelist(struct nfs_read_data *rdata)
231{
9549ec01
FI
232 int i, hole;
233 struct bio *bio = NULL;
234 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
235 sector_t isect, extent_length = 0;
236 struct parallel_io *par;
237 loff_t f_offset = rdata->args.offset;
9549ec01
FI
238 struct page **pages = rdata->args.pages;
239 int pg_index = rdata->args.pgbase >> PAGE_CACHE_SHIFT;
240
6f00866d
TM
241 dprintk("%s enter nr_pages %u offset %lld count %u\n", __func__,
242 rdata->npages, f_offset, (unsigned int)rdata->args.count);
9549ec01
FI
243
244 par = alloc_parallel(rdata);
245 if (!par)
246 goto use_mds;
9549ec01
FI
247 par->pnfs_callback = bl_end_par_io_read;
248 /* At this point, we can no longer jump to use_mds */
249
250 isect = (sector_t) (f_offset >> SECTOR_SHIFT);
251 /* Code assumes extents are page-aligned */
252 for (i = pg_index; i < rdata->npages; i++) {
253 if (!extent_length) {
254 /* We've used up the previous extent */
255 bl_put_extent(be);
256 bl_put_extent(cow_read);
257 bio = bl_submit_bio(READ, bio);
258 /* Get the next one */
259 be = bl_find_get_extent(BLK_LSEG2EXT(rdata->lseg),
260 isect, &cow_read);
261 if (!be) {
262 rdata->pnfs_error = -EIO;
263 goto out;
264 }
265 extent_length = be->be_length -
266 (isect - be->be_f_offset);
267 if (cow_read) {
268 sector_t cow_length = cow_read->be_length -
269 (isect - cow_read->be_f_offset);
270 extent_length = min(extent_length, cow_length);
271 }
272 }
273 hole = is_hole(be, isect);
274 if (hole && !cow_read) {
275 bio = bl_submit_bio(READ, bio);
276 /* Fill hole w/ zeroes w/o accessing device */
277 dprintk("%s Zeroing page for hole\n", __func__);
278 zero_user_segment(pages[i], 0, PAGE_CACHE_SIZE);
279 print_page(pages[i]);
280 SetPageUptodate(pages[i]);
281 } else {
282 struct pnfs_block_extent *be_read;
283
284 be_read = (hole && cow_read) ? cow_read : be;
285 bio = bl_add_page_to_bio(bio, rdata->npages - i, READ,
286 isect, pages[i], be_read,
287 bl_end_io_read, par);
288 if (IS_ERR(bio)) {
289 rdata->pnfs_error = PTR_ERR(bio);
e6d05a75 290 bio = NULL;
9549ec01
FI
291 goto out;
292 }
293 }
294 isect += PAGE_CACHE_SECTORS;
295 extent_length -= PAGE_CACHE_SECTORS;
296 }
297 if ((isect << SECTOR_SHIFT) >= rdata->inode->i_size) {
298 rdata->res.eof = 1;
299 rdata->res.count = rdata->inode->i_size - f_offset;
300 } else {
301 rdata->res.count = (isect << SECTOR_SHIFT) - f_offset;
302 }
303out:
304 bl_put_extent(be);
305 bl_put_extent(cow_read);
306 bl_submit_bio(READ, bio);
307 put_parallel(par);
308 return PNFS_ATTEMPTED;
309
310 use_mds:
311 dprintk("Giving up and using normal NFS\n");
155e7524
FI
312 return PNFS_NOT_ATTEMPTED;
313}
314
31e6306a
FI
315static void mark_extents_written(struct pnfs_block_layout *bl,
316 __u64 offset, __u32 count)
317{
318 sector_t isect, end;
319 struct pnfs_block_extent *be;
7c5465d6 320 struct pnfs_block_short_extent *se;
31e6306a
FI
321
322 dprintk("%s(%llu, %u)\n", __func__, offset, count);
323 if (count == 0)
324 return;
325 isect = (offset & (long)(PAGE_CACHE_MASK)) >> SECTOR_SHIFT;
326 end = (offset + count + PAGE_CACHE_SIZE - 1) & (long)(PAGE_CACHE_MASK);
327 end >>= SECTOR_SHIFT;
328 while (isect < end) {
329 sector_t len;
330 be = bl_find_get_extent(bl, isect, NULL);
331 BUG_ON(!be); /* FIXME */
332 len = min(end, be->be_f_offset + be->be_length) - isect;
7c5465d6
PT
333 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
334 se = bl_pop_one_short_extent(be->be_inval);
335 BUG_ON(!se);
336 bl_mark_for_commit(be, isect, len, se);
337 }
31e6306a
FI
338 isect += len;
339 bl_put_extent(be);
340 }
341}
342
71cdd40f
PT
343static void bl_end_io_write_zero(struct bio *bio, int err)
344{
345 struct parallel_io *par = bio->bi_private;
346 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
347 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
348 struct nfs_write_data *wdata = (struct nfs_write_data *)par->data;
349
350 do {
351 struct page *page = bvec->bv_page;
352
353 if (--bvec >= bio->bi_io_vec)
354 prefetchw(&bvec->bv_page->flags);
355 /* This is the zeroing page we added */
356 end_page_writeback(page);
357 page_cache_release(page);
358 } while (bvec >= bio->bi_io_vec);
7c5465d6
PT
359
360 if (unlikely(!uptodate)) {
71cdd40f
PT
361 if (!wdata->pnfs_error)
362 wdata->pnfs_error = -EIO;
1b0ae068 363 pnfs_set_lo_fail(wdata->lseg);
71cdd40f
PT
364 }
365 bio_put(bio);
366 put_parallel(par);
367}
368
650e2d39
FI
369static void bl_end_io_write(struct bio *bio, int err)
370{
371 struct parallel_io *par = bio->bi_private;
372 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
373 struct nfs_write_data *wdata = (struct nfs_write_data *)par->data;
374
375 if (!uptodate) {
376 if (!wdata->pnfs_error)
377 wdata->pnfs_error = -EIO;
1b0ae068 378 pnfs_set_lo_fail(wdata->lseg);
650e2d39
FI
379 }
380 bio_put(bio);
381 put_parallel(par);
382}
383
384/* Function scheduled for call during bl_end_par_io_write,
385 * it marks sectors as written and extends the commitlist.
386 */
387static void bl_write_cleanup(struct work_struct *work)
388{
389 struct rpc_task *task;
390 struct nfs_write_data *wdata;
391 dprintk("%s enter\n", __func__);
392 task = container_of(work, struct rpc_task, u.tk_work);
393 wdata = container_of(task, struct nfs_write_data, task);
7c5465d6 394 if (likely(!wdata->pnfs_error)) {
31e6306a 395 /* Marks for LAYOUTCOMMIT */
31e6306a
FI
396 mark_extents_written(BLK_LSEG2EXT(wdata->lseg),
397 wdata->args.offset, wdata->args.count);
398 }
650e2d39
FI
399 pnfs_ld_write_done(wdata);
400}
401
402/* Called when last of bios associated with a bl_write_pagelist call finishes */
7c5465d6 403static void bl_end_par_io_write(void *data, int num_se)
650e2d39
FI
404{
405 struct nfs_write_data *wdata = data;
406
7c5465d6
PT
407 if (unlikely(wdata->pnfs_error)) {
408 bl_free_short_extents(&BLK_LSEG2EXT(wdata->lseg)->bl_inval,
409 num_se);
410 }
411
82b906d6 412 wdata->task.tk_status = wdata->pnfs_error;
650e2d39
FI
413 wdata->verf.committed = NFS_FILE_SYNC;
414 INIT_WORK(&wdata->task.u.tk_work, bl_write_cleanup);
415 schedule_work(&wdata->task.u.tk_work);
416}
417
71cdd40f
PT
418/* FIXME STUB - mark intersection of layout and page as bad, so is not
419 * used again.
420 */
421static void mark_bad_read(void)
422{
423 return;
424}
425
426/*
427 * map_block: map a requested I/0 block (isect) into an offset in the LVM
428 * block_device
429 */
430static void
431map_block(struct buffer_head *bh, sector_t isect, struct pnfs_block_extent *be)
432{
433 dprintk("%s enter be=%p\n", __func__, be);
434
435 set_buffer_mapped(bh);
436 bh->b_bdev = be->be_mdev;
437 bh->b_blocknr = (isect - be->be_f_offset + be->be_v_offset) >>
438 (be->be_mdev->bd_inode->i_blkbits - SECTOR_SHIFT);
439
440 dprintk("%s isect %llu, bh->b_blocknr %ld, using bsize %Zd\n",
441 __func__, (unsigned long long)isect, (long)bh->b_blocknr,
442 bh->b_size);
443 return;
444}
445
446/* Given an unmapped page, zero it or read in page for COW, page is locked
447 * by caller.
448 */
449static int
450init_page_for_write(struct page *page, struct pnfs_block_extent *cow_read)
451{
452 struct buffer_head *bh = NULL;
453 int ret = 0;
454 sector_t isect;
455
456 dprintk("%s enter, %p\n", __func__, page);
457 BUG_ON(PageUptodate(page));
458 if (!cow_read) {
459 zero_user_segment(page, 0, PAGE_SIZE);
460 SetPageUptodate(page);
461 goto cleanup;
462 }
463
464 bh = alloc_page_buffers(page, PAGE_CACHE_SIZE, 0);
465 if (!bh) {
466 ret = -ENOMEM;
467 goto cleanup;
468 }
469
470 isect = (sector_t) page->index << PAGE_CACHE_SECTOR_SHIFT;
471 map_block(bh, isect, cow_read);
472 if (!bh_uptodate_or_lock(bh))
473 ret = bh_submit_read(bh);
474 if (ret)
475 goto cleanup;
476 SetPageUptodate(page);
477
478cleanup:
479 bl_put_extent(cow_read);
480 if (bh)
481 free_buffer_head(bh);
482 if (ret) {
483 /* Need to mark layout with bad read...should now
484 * just use nfs4 for reads and writes.
485 */
486 mark_bad_read();
487 }
488 return ret;
489}
490
72c50887
PT
491/* Find or create a zeroing page marked being writeback.
492 * Return ERR_PTR on error, NULL to indicate skip this page and page itself
493 * to indicate write out.
494 */
495static struct page *
496bl_find_get_zeroing_page(struct inode *inode, pgoff_t index,
497 struct pnfs_block_extent *cow_read)
498{
499 struct page *page;
500 int locked = 0;
501 page = find_get_page(inode->i_mapping, index);
502 if (page)
503 goto check_page;
504
505 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
506 if (unlikely(!page)) {
507 dprintk("%s oom\n", __func__);
508 return ERR_PTR(-ENOMEM);
509 }
510 locked = 1;
511
512check_page:
513 /* PageDirty: Other will write this out
514 * PageWriteback: Other is writing this out
515 * PageUptodate: It was read before
516 */
517 if (PageDirty(page) || PageWriteback(page)) {
518 print_page(page);
519 if (locked)
520 unlock_page(page);
521 page_cache_release(page);
522 return NULL;
523 }
524
525 if (!locked) {
526 lock_page(page);
527 locked = 1;
528 goto check_page;
529 }
530 if (!PageUptodate(page)) {
531 /* New page, readin or zero it */
532 init_page_for_write(page, cow_read);
533 }
534 set_page_writeback(page);
535 unlock_page(page);
536
537 return page;
538}
539
155e7524 540static enum pnfs_try_status
650e2d39 541bl_write_pagelist(struct nfs_write_data *wdata, int sync)
155e7524 542{
71cdd40f 543 int i, ret, npg_zero, pg_index, last = 0;
650e2d39 544 struct bio *bio = NULL;
71cdd40f
PT
545 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
546 sector_t isect, last_isect = 0, extent_length = 0;
650e2d39
FI
547 struct parallel_io *par;
548 loff_t offset = wdata->args.offset;
549 size_t count = wdata->args.count;
550 struct page **pages = wdata->args.pages;
71cdd40f
PT
551 struct page *page;
552 pgoff_t index;
553 u64 temp;
554 int npg_per_block =
555 NFS_SERVER(wdata->inode)->pnfs_blksize >> PAGE_CACHE_SHIFT;
650e2d39
FI
556
557 dprintk("%s enter, %Zu@%lld\n", __func__, count, offset);
558 /* At this point, wdata->pages is a (sequential) list of nfs_pages.
71cdd40f
PT
559 * We want to write each, and if there is an error set pnfs_error
560 * to have it redone using nfs.
650e2d39
FI
561 */
562 par = alloc_parallel(wdata);
563 if (!par)
7c5465d6 564 goto out_mds;
650e2d39
FI
565 par->pnfs_callback = bl_end_par_io_write;
566 /* At this point, have to be more careful with error handling */
567
568 isect = (sector_t) ((offset & (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
71cdd40f
PT
569 be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg), isect, &cow_read);
570 if (!be || !is_writable(be, isect)) {
571 dprintk("%s no matching extents!\n", __func__);
7c5465d6 572 goto out_mds;
71cdd40f
PT
573 }
574
575 /* First page inside INVALID extent */
576 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
7c5465d6
PT
577 if (likely(!bl_push_one_short_extent(be->be_inval)))
578 par->bse_count++;
579 else
580 goto out_mds;
71cdd40f
PT
581 temp = offset >> PAGE_CACHE_SHIFT;
582 npg_zero = do_div(temp, npg_per_block);
583 isect = (sector_t) (((offset - npg_zero * PAGE_CACHE_SIZE) &
584 (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
585 extent_length = be->be_length - (isect - be->be_f_offset);
586
587fill_invalid_ext:
588 dprintk("%s need to zero %d pages\n", __func__, npg_zero);
589 for (;npg_zero > 0; npg_zero--) {
75422745
PT
590 if (bl_is_sector_init(be->be_inval, isect)) {
591 dprintk("isect %llu already init\n",
592 (unsigned long long)isect);
593 goto next_page;
594 }
71cdd40f
PT
595 /* page ref released in bl_end_io_write_zero */
596 index = isect >> PAGE_CACHE_SECTOR_SHIFT;
597 dprintk("%s zero %dth page: index %lu isect %llu\n",
598 __func__, npg_zero, index,
599 (unsigned long long)isect);
72c50887
PT
600 page = bl_find_get_zeroing_page(wdata->inode, index,
601 cow_read);
602 if (unlikely(IS_ERR(page))) {
603 wdata->pnfs_error = PTR_ERR(page);
71cdd40f 604 goto out;
72c50887 605 } else if (page == NULL)
71cdd40f 606 goto next_page;
71cdd40f
PT
607
608 ret = bl_mark_sectors_init(be->be_inval, isect,
60c52e3a 609 PAGE_CACHE_SECTORS);
71cdd40f
PT
610 if (unlikely(ret)) {
611 dprintk("%s bl_mark_sectors_init fail %d\n",
612 __func__, ret);
613 end_page_writeback(page);
614 page_cache_release(page);
615 wdata->pnfs_error = ret;
616 goto out;
617 }
7c5465d6
PT
618 if (likely(!bl_push_one_short_extent(be->be_inval)))
619 par->bse_count++;
620 else {
621 end_page_writeback(page);
622 page_cache_release(page);
623 wdata->pnfs_error = -ENOMEM;
624 goto out;
625 }
626 /* FIXME: This should be done in bi_end_io */
627 mark_extents_written(BLK_LSEG2EXT(wdata->lseg),
628 page->index << PAGE_CACHE_SHIFT,
629 PAGE_CACHE_SIZE);
630
71cdd40f
PT
631 bio = bl_add_page_to_bio(bio, npg_zero, WRITE,
632 isect, page, be,
633 bl_end_io_write_zero, par);
634 if (IS_ERR(bio)) {
635 wdata->pnfs_error = PTR_ERR(bio);
e6d05a75 636 bio = NULL;
71cdd40f
PT
637 goto out;
638 }
71cdd40f
PT
639next_page:
640 isect += PAGE_CACHE_SECTORS;
641 extent_length -= PAGE_CACHE_SECTORS;
642 }
643 if (last)
644 goto write_done;
645 }
646 bio = bl_submit_bio(WRITE, bio);
647
648 /* Middle pages */
649 pg_index = wdata->args.pgbase >> PAGE_CACHE_SHIFT;
650 for (i = pg_index; i < wdata->npages; i++) {
650e2d39
FI
651 if (!extent_length) {
652 /* We've used up the previous extent */
653 bl_put_extent(be);
654 bio = bl_submit_bio(WRITE, bio);
655 /* Get the next one */
656 be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg),
657 isect, NULL);
658 if (!be || !is_writable(be, isect)) {
71cdd40f 659 wdata->pnfs_error = -EINVAL;
650e2d39
FI
660 goto out;
661 }
7c5465d6
PT
662 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
663 if (likely(!bl_push_one_short_extent(
664 be->be_inval)))
665 par->bse_count++;
666 else {
667 wdata->pnfs_error = -ENOMEM;
668 goto out;
669 }
670 }
650e2d39 671 extent_length = be->be_length -
71cdd40f 672 (isect - be->be_f_offset);
650e2d39 673 }
71cdd40f
PT
674 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
675 ret = bl_mark_sectors_init(be->be_inval, isect,
60c52e3a 676 PAGE_CACHE_SECTORS);
71cdd40f
PT
677 if (unlikely(ret)) {
678 dprintk("%s bl_mark_sectors_init fail %d\n",
679 __func__, ret);
680 wdata->pnfs_error = ret;
681 goto out;
650e2d39 682 }
71cdd40f
PT
683 }
684 bio = bl_add_page_to_bio(bio, wdata->npages - i, WRITE,
685 isect, pages[i], be,
686 bl_end_io_write, par);
687 if (IS_ERR(bio)) {
688 wdata->pnfs_error = PTR_ERR(bio);
e6d05a75 689 bio = NULL;
71cdd40f 690 goto out;
650e2d39
FI
691 }
692 isect += PAGE_CACHE_SECTORS;
71cdd40f 693 last_isect = isect;
650e2d39
FI
694 extent_length -= PAGE_CACHE_SECTORS;
695 }
71cdd40f
PT
696
697 /* Last page inside INVALID extent */
698 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
699 bio = bl_submit_bio(WRITE, bio);
700 temp = last_isect >> PAGE_CACHE_SECTOR_SHIFT;
701 npg_zero = npg_per_block - do_div(temp, npg_per_block);
702 if (npg_zero < npg_per_block) {
703 last = 1;
704 goto fill_invalid_ext;
705 }
706 }
707
708write_done:
709 wdata->res.count = (last_isect << SECTOR_SHIFT) - (offset);
710 if (count < wdata->res.count) {
650e2d39 711 wdata->res.count = count;
71cdd40f 712 }
650e2d39
FI
713out:
714 bl_put_extent(be);
715 bl_submit_bio(WRITE, bio);
716 put_parallel(par);
717 return PNFS_ATTEMPTED;
7c5465d6
PT
718out_mds:
719 bl_put_extent(be);
720 kfree(par);
721 return PNFS_NOT_ATTEMPTED;
155e7524
FI
722}
723
9e692969 724/* FIXME - range ignored */
155e7524 725static void
9e692969 726release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range)
155e7524 727{
9e692969
FI
728 int i;
729 struct pnfs_block_extent *be;
730
731 spin_lock(&bl->bl_ext_lock);
732 for (i = 0; i < EXTENT_LISTS; i++) {
733 while (!list_empty(&bl->bl_extents[i])) {
734 be = list_first_entry(&bl->bl_extents[i],
735 struct pnfs_block_extent,
736 be_node);
737 list_del(&be->be_node);
738 bl_put_extent(be);
739 }
740 }
741 spin_unlock(&bl->bl_ext_lock);
155e7524
FI
742}
743
155e7524
FI
744static void
745release_inval_marks(struct pnfs_inval_markings *marks)
746{
c1c2a4cd 747 struct pnfs_inval_tracking *pos, *temp;
7c5465d6 748 struct pnfs_block_short_extent *se, *stemp;
c1c2a4cd
FI
749
750 list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) {
751 list_del(&pos->it_link);
752 kfree(pos);
753 }
7c5465d6
PT
754
755 list_for_each_entry_safe(se, stemp, &marks->im_extents, bse_node) {
756 list_del(&se->bse_node);
757 kfree(se);
758 }
155e7524
FI
759 return;
760}
761
762static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
763{
764 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
765
766 dprintk("%s enter\n", __func__);
767 release_extents(bl, NULL);
768 release_inval_marks(&bl->bl_inval);
769 kfree(bl);
770}
771
772static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode,
773 gfp_t gfp_flags)
774{
775 struct pnfs_block_layout *bl;
776
777 dprintk("%s enter\n", __func__);
778 bl = kzalloc(sizeof(*bl), gfp_flags);
779 if (!bl)
780 return NULL;
781 spin_lock_init(&bl->bl_ext_lock);
782 INIT_LIST_HEAD(&bl->bl_extents[0]);
783 INIT_LIST_HEAD(&bl->bl_extents[1]);
784 INIT_LIST_HEAD(&bl->bl_commit);
785 INIT_LIST_HEAD(&bl->bl_committing);
786 bl->bl_count = 0;
787 bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT;
788 BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize);
789 return &bl->bl_layout;
790}
791
a60d2ebd 792static void bl_free_lseg(struct pnfs_layout_segment *lseg)
155e7524 793{
a60d2ebd
FI
794 dprintk("%s enter\n", __func__);
795 kfree(lseg);
155e7524
FI
796}
797
a60d2ebd
FI
798/* We pretty much ignore lseg, and store all data layout wide, so we
799 * can correctly merge.
800 */
801static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo,
802 struct nfs4_layoutget_res *lgr,
803 gfp_t gfp_flags)
155e7524 804{
a60d2ebd
FI
805 struct pnfs_layout_segment *lseg;
806 int status;
807
808 dprintk("%s enter\n", __func__);
809 lseg = kzalloc(sizeof(*lseg), gfp_flags);
810 if (!lseg)
811 return ERR_PTR(-ENOMEM);
812 status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags);
813 if (status) {
814 /* We don't want to call the full-blown bl_free_lseg,
815 * since on error extents were not touched.
816 */
817 kfree(lseg);
818 return ERR_PTR(status);
819 }
820 return lseg;
155e7524
FI
821}
822
823static void
824bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr,
825 const struct nfs4_layoutcommit_args *arg)
826{
90ace12a
FI
827 dprintk("%s enter\n", __func__);
828 encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg);
155e7524
FI
829}
830
831static void
832bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
833{
b2be7811
FI
834 struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout;
835
836 dprintk("%s enter\n", __func__);
837 clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status);
155e7524
FI
838}
839
2f9fd182
FI
840static void free_blk_mountid(struct block_mount_id *mid)
841{
842 if (mid) {
93a3844e
PT
843 struct pnfs_block_dev *dev, *tmp;
844
845 /* No need to take bm_lock as we are last user freeing bm_devlist */
846 list_for_each_entry_safe(dev, tmp, &mid->bm_devlist, bm_node) {
2f9fd182
FI
847 list_del(&dev->bm_node);
848 bl_free_block_dev(dev);
849 }
2f9fd182
FI
850 kfree(mid);
851 }
852}
853
854/* This is mostly copied from the filelayout's get_device_info function.
855 * It seems much of this should be at the generic pnfs level.
856 */
857static struct pnfs_block_dev *
858nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh,
859 struct nfs4_deviceid *d_id)
860{
861 struct pnfs_device *dev;
516f2e24 862 struct pnfs_block_dev *rv;
2f9fd182
FI
863 u32 max_resp_sz;
864 int max_pages;
865 struct page **pages = NULL;
866 int i, rc;
867
868 /*
869 * Use the session max response size as the basis for setting
870 * GETDEVICEINFO's maxcount
871 */
872 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
10bd295a 873 max_pages = nfs_page_array_len(0, max_resp_sz);
2f9fd182
FI
874 dprintk("%s max_resp_sz %u max_pages %d\n",
875 __func__, max_resp_sz, max_pages);
876
877 dev = kmalloc(sizeof(*dev), GFP_NOFS);
878 if (!dev) {
879 dprintk("%s kmalloc failed\n", __func__);
516f2e24 880 return ERR_PTR(-ENOMEM);
2f9fd182
FI
881 }
882
883 pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS);
884 if (pages == NULL) {
885 kfree(dev);
516f2e24 886 return ERR_PTR(-ENOMEM);
2f9fd182
FI
887 }
888 for (i = 0; i < max_pages; i++) {
889 pages[i] = alloc_page(GFP_NOFS);
516f2e24
JR
890 if (!pages[i]) {
891 rv = ERR_PTR(-ENOMEM);
2f9fd182 892 goto out_free;
516f2e24 893 }
2f9fd182
FI
894 }
895
896 memcpy(&dev->dev_id, d_id, sizeof(*d_id));
897 dev->layout_type = LAYOUT_BLOCK_VOLUME;
898 dev->pages = pages;
899 dev->pgbase = 0;
900 dev->pglen = PAGE_SIZE * max_pages;
901 dev->mincount = 0;
902
903 dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data);
904 rc = nfs4_proc_getdeviceinfo(server, dev);
905 dprintk("%s getdevice info returns %d\n", __func__, rc);
516f2e24
JR
906 if (rc) {
907 rv = ERR_PTR(rc);
2f9fd182 908 goto out_free;
516f2e24 909 }
2f9fd182
FI
910
911 rv = nfs4_blk_decode_device(server, dev);
912 out_free:
913 for (i = 0; i < max_pages; i++)
914 __free_page(pages[i]);
915 kfree(pages);
916 kfree(dev);
917 return rv;
918}
919
155e7524
FI
920static int
921bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
922{
2f9fd182
FI
923 struct block_mount_id *b_mt_id = NULL;
924 struct pnfs_devicelist *dlist = NULL;
925 struct pnfs_block_dev *bdev;
926 LIST_HEAD(block_disklist);
516f2e24 927 int status, i;
2f9fd182 928
155e7524 929 dprintk("%s enter\n", __func__);
2f9fd182
FI
930
931 if (server->pnfs_blksize == 0) {
932 dprintk("%s Server did not return blksize\n", __func__);
933 return -EINVAL;
934 }
935 b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS);
936 if (!b_mt_id) {
937 status = -ENOMEM;
938 goto out_error;
939 }
940 /* Initialize nfs4 block layout mount id */
941 spin_lock_init(&b_mt_id->bm_lock);
942 INIT_LIST_HEAD(&b_mt_id->bm_devlist);
943
944 dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS);
945 if (!dlist) {
946 status = -ENOMEM;
947 goto out_error;
948 }
949 dlist->eof = 0;
950 while (!dlist->eof) {
951 status = nfs4_proc_getdevicelist(server, fh, dlist);
952 if (status)
953 goto out_error;
954 dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n",
955 __func__, dlist->num_devs, dlist->eof);
956 for (i = 0; i < dlist->num_devs; i++) {
957 bdev = nfs4_blk_get_deviceinfo(server, fh,
958 &dlist->dev_id[i]);
516f2e24
JR
959 if (IS_ERR(bdev)) {
960 status = PTR_ERR(bdev);
2f9fd182
FI
961 goto out_error;
962 }
963 spin_lock(&b_mt_id->bm_lock);
964 list_add(&bdev->bm_node, &b_mt_id->bm_devlist);
965 spin_unlock(&b_mt_id->bm_lock);
966 }
967 }
968 dprintk("%s SUCCESS\n", __func__);
969 server->pnfs_ld_data = b_mt_id;
970
971 out_return:
972 kfree(dlist);
973 return status;
974
975 out_error:
976 free_blk_mountid(b_mt_id);
977 goto out_return;
155e7524
FI
978}
979
980static int
981bl_clear_layoutdriver(struct nfs_server *server)
982{
2f9fd182
FI
983 struct block_mount_id *b_mt_id = server->pnfs_ld_data;
984
155e7524 985 dprintk("%s enter\n", __func__);
2f9fd182
FI
986 free_blk_mountid(b_mt_id);
987 dprintk("%s RETURNS\n", __func__);
155e7524
FI
988 return 0;
989}
990
e9643fe8
BH
991static const struct nfs_pageio_ops bl_pg_read_ops = {
992 .pg_init = pnfs_generic_pg_init_read,
993 .pg_test = pnfs_generic_pg_test,
994 .pg_doio = pnfs_generic_pg_readpages,
995};
996
997static const struct nfs_pageio_ops bl_pg_write_ops = {
998 .pg_init = pnfs_generic_pg_init_write,
999 .pg_test = pnfs_generic_pg_test,
1000 .pg_doio = pnfs_generic_pg_writepages,
1001};
1002
155e7524
FI
1003static struct pnfs_layoutdriver_type blocklayout_type = {
1004 .id = LAYOUT_BLOCK_VOLUME,
1005 .name = "LAYOUT_BLOCK_VOLUME",
1006 .read_pagelist = bl_read_pagelist,
1007 .write_pagelist = bl_write_pagelist,
1008 .alloc_layout_hdr = bl_alloc_layout_hdr,
1009 .free_layout_hdr = bl_free_layout_hdr,
1010 .alloc_lseg = bl_alloc_lseg,
1011 .free_lseg = bl_free_lseg,
1012 .encode_layoutcommit = bl_encode_layoutcommit,
1013 .cleanup_layoutcommit = bl_cleanup_layoutcommit,
1014 .set_layoutdriver = bl_set_layoutdriver,
1015 .clear_layoutdriver = bl_clear_layoutdriver,
e9643fe8
BH
1016 .pg_read_ops = &bl_pg_read_ops,
1017 .pg_write_ops = &bl_pg_write_ops,
155e7524
FI
1018};
1019
fe0a9b74 1020static const struct rpc_pipe_ops bl_upcall_ops = {
c1225158 1021 .upcall = rpc_pipe_generic_upcall,
fe0a9b74
JR
1022 .downcall = bl_pipe_downcall,
1023 .destroy_msg = bl_pipe_destroy_msg,
1024};
1025
332dfab6
SK
1026static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb,
1027 struct rpc_pipe *pipe)
1028{
1029 struct dentry *dir, *dentry;
1030
1031 dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME);
1032 if (dir == NULL)
1033 return ERR_PTR(-ENOENT);
1034 dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe);
1035 dput(dir);
1036 return dentry;
1037}
1038
1039static void nfs4blocklayout_unregister_sb(struct super_block *sb,
1040 struct rpc_pipe *pipe)
1041{
1042 if (pipe->dentry)
1043 rpc_unlink(pipe->dentry);
1044}
1045
627f3066
SK
1046static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
1047 void *ptr)
1048{
1049 struct super_block *sb = ptr;
1050 struct net *net = sb->s_fs_info;
1051 struct nfs_net *nn = net_generic(net, nfs_net_id);
1052 struct dentry *dentry;
1053 int ret = 0;
1054
1055 if (!try_module_get(THIS_MODULE))
1056 return 0;
1057
1058 if (nn->bl_device_pipe == NULL) {
1059 module_put(THIS_MODULE);
1060 return 0;
1061 }
1062
1063 switch (event) {
1064 case RPC_PIPEFS_MOUNT:
1065 dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe);
1066 if (IS_ERR(dentry)) {
1067 ret = PTR_ERR(dentry);
1068 break;
1069 }
1070 nn->bl_device_pipe->dentry = dentry;
1071 break;
1072 case RPC_PIPEFS_UMOUNT:
1073 if (nn->bl_device_pipe->dentry)
1074 nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe);
1075 break;
1076 default:
1077 ret = -ENOTSUPP;
1078 break;
1079 }
1080 module_put(THIS_MODULE);
1081 return ret;
1082}
1083
1084static struct notifier_block nfs4blocklayout_block = {
1085 .notifier_call = rpc_pipefs_event,
1086};
1087
332dfab6
SK
1088static struct dentry *nfs4blocklayout_register_net(struct net *net,
1089 struct rpc_pipe *pipe)
1090{
1091 struct super_block *pipefs_sb;
1092 struct dentry *dentry;
1093
1094 pipefs_sb = rpc_get_sb_net(net);
1095 if (!pipefs_sb)
2561d618 1096 return NULL;
332dfab6
SK
1097 dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe);
1098 rpc_put_sb_net(net);
1099 return dentry;
1100}
1101
1102static void nfs4blocklayout_unregister_net(struct net *net,
1103 struct rpc_pipe *pipe)
1104{
1105 struct super_block *pipefs_sb;
1106
1107 pipefs_sb = rpc_get_sb_net(net);
1108 if (pipefs_sb) {
1109 nfs4blocklayout_unregister_sb(pipefs_sb, pipe);
1110 rpc_put_sb_net(net);
1111 }
1112}
1113
9e2e74db
SK
1114static int nfs4blocklayout_net_init(struct net *net)
1115{
1116 struct nfs_net *nn = net_generic(net, nfs_net_id);
1117 struct dentry *dentry;
1118
5ffaf855 1119 init_waitqueue_head(&nn->bl_wq);
9e2e74db
SK
1120 nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0);
1121 if (IS_ERR(nn->bl_device_pipe))
1122 return PTR_ERR(nn->bl_device_pipe);
1123 dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe);
1124 if (IS_ERR(dentry)) {
1125 rpc_destroy_pipe_data(nn->bl_device_pipe);
1126 return PTR_ERR(dentry);
1127 }
1128 nn->bl_device_pipe->dentry = dentry;
1129 return 0;
1130}
1131
1132static void nfs4blocklayout_net_exit(struct net *net)
1133{
1134 struct nfs_net *nn = net_generic(net, nfs_net_id);
1135
1136 nfs4blocklayout_unregister_net(net, nn->bl_device_pipe);
1137 rpc_destroy_pipe_data(nn->bl_device_pipe);
1138 nn->bl_device_pipe = NULL;
1139}
1140
1141static struct pernet_operations nfs4blocklayout_net_ops = {
1142 .init = nfs4blocklayout_net_init,
1143 .exit = nfs4blocklayout_net_exit,
1144};
1145
155e7524
FI
1146static int __init nfs4blocklayout_init(void)
1147{
1148 int ret;
1149
1150 dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
1151
1152 ret = pnfs_register_layoutdriver(&blocklayout_type);
fe0a9b74
JR
1153 if (ret)
1154 goto out;
1155
627f3066 1156 ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block);
9e2e74db
SK
1157 if (ret)
1158 goto out_remove;
627f3066
SK
1159 ret = register_pernet_subsys(&nfs4blocklayout_net_ops);
1160 if (ret)
1161 goto out_notifier;
fe0a9b74
JR
1162out:
1163 return ret;
1164
627f3066
SK
1165out_notifier:
1166 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
fe0a9b74
JR
1167out_remove:
1168 pnfs_unregister_layoutdriver(&blocklayout_type);
155e7524
FI
1169 return ret;
1170}
1171
1172static void __exit nfs4blocklayout_exit(void)
1173{
1174 dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
1175 __func__);
1176
627f3066 1177 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
9e2e74db 1178 unregister_pernet_subsys(&nfs4blocklayout_net_ops);
155e7524
FI
1179 pnfs_unregister_layoutdriver(&blocklayout_type);
1180}
1181
1182MODULE_ALIAS("nfs-layouttype4-3");
1183
1184module_init(nfs4blocklayout_init);
1185module_exit(nfs4blocklayout_exit);