blob: 234273621854365f97d0c8081aa0d2a1aa68f8c5 [file] [log] [blame]
Fred Isaman155e7522011-07-30 20:52:39 -04001/*
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 */
Fred Isaman9549ec02011-07-30 20:52:53 -040032
Fred Isaman155e7522011-07-30 20:52:39 -040033#include <linux/module.h>
34#include <linux/init.h>
Jim Reesfe0a9b72011-07-30 20:52:42 -040035#include <linux/mount.h>
36#include <linux/namei.h>
Fred Isaman9549ec02011-07-30 20:52:53 -040037#include <linux/bio.h> /* struct bio */
Peng Tao71cdd402011-07-30 20:52:56 -040038#include <linux/buffer_head.h> /* various write calls */
Heiko Carstens88c9e422011-08-02 09:57:35 +020039#include <linux/prefetch.h>
Fred Isaman155e7522011-07-30 20:52:39 -040040
41#include "blocklayout.h"
42
43#define NFSDBG_FACILITY NFSDBG_PNFS_LD
44
45MODULE_LICENSE("GPL");
46MODULE_AUTHOR("Andy Adamson <andros@citi.umich.edu>");
47MODULE_DESCRIPTION("The NFSv4.1 pNFS Block layout driver");
48
Jim Reesfe0a9b72011-07-30 20:52:42 -040049struct dentry *bl_device_pipe;
50wait_queue_head_t bl_wq;
51
Fred Isaman9549ec02011-07-30 20:52:53 -040052static void print_page(struct page *page)
53{
54 dprintk("PRINTPAGE page %p\n", page);
55 dprintk(" PagePrivate %d\n", PagePrivate(page));
56 dprintk(" PageUptodate %d\n", PageUptodate(page));
57 dprintk(" PageError %d\n", PageError(page));
58 dprintk(" PageDirty %d\n", PageDirty(page));
59 dprintk(" PageReferenced %d\n", PageReferenced(page));
60 dprintk(" PageLocked %d\n", PageLocked(page));
61 dprintk(" PageWriteback %d\n", PageWriteback(page));
62 dprintk(" PageMappedToDisk %d\n", PageMappedToDisk(page));
63 dprintk("\n");
64}
65
66/* Given the be associated with isect, determine if page data needs to be
67 * initialized.
68 */
69static int is_hole(struct pnfs_block_extent *be, sector_t isect)
70{
71 if (be->be_state == PNFS_BLOCK_NONE_DATA)
72 return 1;
73 else if (be->be_state != PNFS_BLOCK_INVALID_DATA)
74 return 0;
75 else
76 return !bl_is_sector_init(be->be_inval, isect);
77}
78
Fred Isaman650e2d32011-07-30 20:52:54 -040079/* Given the be associated with isect, determine if page data can be
80 * written to disk.
81 */
82static int is_writable(struct pnfs_block_extent *be, sector_t isect)
83{
Peng Tao71cdd402011-07-30 20:52:56 -040084 return (be->be_state == PNFS_BLOCK_READWRITE_DATA ||
85 be->be_state == PNFS_BLOCK_INVALID_DATA);
Fred Isaman650e2d32011-07-30 20:52:54 -040086}
87
Fred Isaman9549ec02011-07-30 20:52:53 -040088/* The data we are handed might be spread across several bios. We need
89 * to track when the last one is finished.
90 */
91struct parallel_io {
92 struct kref refcnt;
93 struct rpc_call_ops call_ops;
94 void (*pnfs_callback) (void *data);
95 void *data;
96};
97
98static inline struct parallel_io *alloc_parallel(void *data)
99{
100 struct parallel_io *rv;
101
102 rv = kmalloc(sizeof(*rv), GFP_NOFS);
103 if (rv) {
104 rv->data = data;
105 kref_init(&rv->refcnt);
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__);
120 p->pnfs_callback(p->data);
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
Peng Tao74a6eeb2012-01-12 23:18:48 +0800149 npg = min(npg, BIO_MAX_PAGES);
Fred Isaman9549ec02011-07-30 20:52:53 -0400150 bio = bio_alloc(GFP_NOIO, npg);
Peng Tao74a6eeb2012-01-12 23:18:48 +0800151 if (!bio && (current->flags & PF_MEMALLOC)) {
152 while (!bio && (npg /= 2))
153 bio = bio_alloc(GFP_NOIO, npg);
154 }
Fred Isaman9549ec02011-07-30 20:52:53 -0400155
Peng Tao74a6eeb2012-01-12 23:18:48 +0800156 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 }
Fred Isaman9549ec02011-07-30 20:52:53 -0400162 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
Fred Isaman9549ec02011-07-30 20:52:53 -0400184/* 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;
Peng Tao1b0ae062011-09-22 21:50:12 -0400203 pnfs_set_lo_fail(rdata->lseg);
Fred Isaman9549ec02011-07-30 20:52:53 -0400204 }
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
220bl_end_par_io_read(void *data)
221{
222 struct nfs_read_data *rdata = data;
223
Peng Tao82b906d2012-01-12 23:18:43 +0800224 rdata->task.tk_status = rdata->pnfs_error;
Fred Isaman9549ec02011-07-30 20:52:53 -0400225 INIT_WORK(&rdata->task.u.tk_work, bl_read_cleanup);
226 schedule_work(&rdata->task.u.tk_work);
227}
228
229/* We don't want normal .rpc_call_done callback used, so we replace it
230 * with this stub.
231 */
232static void bl_rpc_do_nothing(struct rpc_task *task, void *calldata)
233{
234 return;
235}
236
Fred Isaman155e7522011-07-30 20:52:39 -0400237static enum pnfs_try_status
238bl_read_pagelist(struct nfs_read_data *rdata)
239{
Fred Isaman9549ec02011-07-30 20:52:53 -0400240 int i, hole;
241 struct bio *bio = NULL;
242 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
243 sector_t isect, extent_length = 0;
244 struct parallel_io *par;
245 loff_t f_offset = rdata->args.offset;
246 size_t count = rdata->args.count;
247 struct page **pages = rdata->args.pages;
248 int pg_index = rdata->args.pgbase >> PAGE_CACHE_SHIFT;
249
250 dprintk("%s enter nr_pages %u offset %lld count %Zd\n", __func__,
251 rdata->npages, f_offset, count);
252
253 par = alloc_parallel(rdata);
254 if (!par)
255 goto use_mds;
256 par->call_ops = *rdata->mds_ops;
257 par->call_ops.rpc_call_done = bl_rpc_do_nothing;
258 par->pnfs_callback = bl_end_par_io_read;
259 /* At this point, we can no longer jump to use_mds */
260
261 isect = (sector_t) (f_offset >> SECTOR_SHIFT);
262 /* Code assumes extents are page-aligned */
263 for (i = pg_index; i < rdata->npages; i++) {
264 if (!extent_length) {
265 /* We've used up the previous extent */
266 bl_put_extent(be);
267 bl_put_extent(cow_read);
268 bio = bl_submit_bio(READ, bio);
269 /* Get the next one */
270 be = bl_find_get_extent(BLK_LSEG2EXT(rdata->lseg),
271 isect, &cow_read);
272 if (!be) {
273 rdata->pnfs_error = -EIO;
274 goto out;
275 }
276 extent_length = be->be_length -
277 (isect - be->be_f_offset);
278 if (cow_read) {
279 sector_t cow_length = cow_read->be_length -
280 (isect - cow_read->be_f_offset);
281 extent_length = min(extent_length, cow_length);
282 }
283 }
284 hole = is_hole(be, isect);
285 if (hole && !cow_read) {
286 bio = bl_submit_bio(READ, bio);
287 /* Fill hole w/ zeroes w/o accessing device */
288 dprintk("%s Zeroing page for hole\n", __func__);
289 zero_user_segment(pages[i], 0, PAGE_CACHE_SIZE);
290 print_page(pages[i]);
291 SetPageUptodate(pages[i]);
292 } else {
293 struct pnfs_block_extent *be_read;
294
295 be_read = (hole && cow_read) ? cow_read : be;
296 bio = bl_add_page_to_bio(bio, rdata->npages - i, READ,
297 isect, pages[i], be_read,
298 bl_end_io_read, par);
299 if (IS_ERR(bio)) {
300 rdata->pnfs_error = PTR_ERR(bio);
Peng Taoe6d05a72011-09-22 21:50:16 -0400301 bio = NULL;
Fred Isaman9549ec02011-07-30 20:52:53 -0400302 goto out;
303 }
304 }
305 isect += PAGE_CACHE_SECTORS;
306 extent_length -= PAGE_CACHE_SECTORS;
307 }
308 if ((isect << SECTOR_SHIFT) >= rdata->inode->i_size) {
309 rdata->res.eof = 1;
310 rdata->res.count = rdata->inode->i_size - f_offset;
311 } else {
312 rdata->res.count = (isect << SECTOR_SHIFT) - f_offset;
313 }
314out:
315 bl_put_extent(be);
316 bl_put_extent(cow_read);
317 bl_submit_bio(READ, bio);
318 put_parallel(par);
319 return PNFS_ATTEMPTED;
320
321 use_mds:
322 dprintk("Giving up and using normal NFS\n");
Fred Isaman155e7522011-07-30 20:52:39 -0400323 return PNFS_NOT_ATTEMPTED;
324}
325
Fred Isaman31e63062011-07-30 20:52:55 -0400326static void mark_extents_written(struct pnfs_block_layout *bl,
327 __u64 offset, __u32 count)
328{
329 sector_t isect, end;
330 struct pnfs_block_extent *be;
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;
343 if (be->be_state == PNFS_BLOCK_INVALID_DATA)
344 bl_mark_for_commit(be, isect, len); /* What if fails? */
345 isect += len;
346 bl_put_extent(be);
347 }
348}
349
Peng Tao71cdd402011-07-30 20:52:56 -0400350static void bl_end_io_write_zero(struct bio *bio, int err)
351{
352 struct parallel_io *par = bio->bi_private;
353 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
354 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
355 struct nfs_write_data *wdata = (struct nfs_write_data *)par->data;
356
357 do {
358 struct page *page = bvec->bv_page;
359
360 if (--bvec >= bio->bi_io_vec)
361 prefetchw(&bvec->bv_page->flags);
362 /* This is the zeroing page we added */
363 end_page_writeback(page);
364 page_cache_release(page);
365 } while (bvec >= bio->bi_io_vec);
366 if (!uptodate) {
367 if (!wdata->pnfs_error)
368 wdata->pnfs_error = -EIO;
Peng Tao1b0ae062011-09-22 21:50:12 -0400369 pnfs_set_lo_fail(wdata->lseg);
Peng Tao71cdd402011-07-30 20:52:56 -0400370 }
371 bio_put(bio);
372 put_parallel(par);
373}
374
Fred Isaman650e2d32011-07-30 20:52:54 -0400375/* This is basically copied from mpage_end_io_read */
376static void bl_end_io_write(struct bio *bio, int err)
Fred Isaman155e7522011-07-30 20:52:39 -0400377{
Fred Isaman650e2d32011-07-30 20:52:54 -0400378 struct parallel_io *par = bio->bi_private;
379 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
380 struct nfs_write_data *wdata = (struct nfs_write_data *)par->data;
381
382 if (!uptodate) {
383 if (!wdata->pnfs_error)
384 wdata->pnfs_error = -EIO;
Peng Tao1b0ae062011-09-22 21:50:12 -0400385 pnfs_set_lo_fail(wdata->lseg);
Fred Isaman650e2d32011-07-30 20:52:54 -0400386 }
387 bio_put(bio);
388 put_parallel(par);
389}
390
391/* Function scheduled for call during bl_end_par_io_write,
392 * it marks sectors as written and extends the commitlist.
393 */
394static void bl_write_cleanup(struct work_struct *work)
395{
396 struct rpc_task *task;
397 struct nfs_write_data *wdata;
398 dprintk("%s enter\n", __func__);
399 task = container_of(work, struct rpc_task, u.tk_work);
400 wdata = container_of(task, struct nfs_write_data, task);
Peng Tao71cdd402011-07-30 20:52:56 -0400401 if (!wdata->pnfs_error) {
Fred Isaman31e63062011-07-30 20:52:55 -0400402 /* Marks for LAYOUTCOMMIT */
Fred Isaman31e63062011-07-30 20:52:55 -0400403 mark_extents_written(BLK_LSEG2EXT(wdata->lseg),
404 wdata->args.offset, wdata->args.count);
405 }
Fred Isaman650e2d32011-07-30 20:52:54 -0400406 pnfs_ld_write_done(wdata);
407}
408
409/* Called when last of bios associated with a bl_write_pagelist call finishes */
Peng Tao71cdd402011-07-30 20:52:56 -0400410static void bl_end_par_io_write(void *data)
Fred Isaman650e2d32011-07-30 20:52:54 -0400411{
412 struct nfs_write_data *wdata = data;
413
Peng Tao82b906d2012-01-12 23:18:43 +0800414 wdata->task.tk_status = wdata->pnfs_error;
Fred Isaman650e2d32011-07-30 20:52:54 -0400415 wdata->verf.committed = NFS_FILE_SYNC;
416 INIT_WORK(&wdata->task.u.tk_work, bl_write_cleanup);
417 schedule_work(&wdata->task.u.tk_work);
418}
419
Peng Tao71cdd402011-07-30 20:52:56 -0400420/* FIXME STUB - mark intersection of layout and page as bad, so is not
421 * used again.
422 */
423static void mark_bad_read(void)
424{
425 return;
426}
427
428/*
429 * map_block: map a requested I/0 block (isect) into an offset in the LVM
430 * block_device
431 */
432static void
433map_block(struct buffer_head *bh, sector_t isect, struct pnfs_block_extent *be)
434{
435 dprintk("%s enter be=%p\n", __func__, be);
436
437 set_buffer_mapped(bh);
438 bh->b_bdev = be->be_mdev;
439 bh->b_blocknr = (isect - be->be_f_offset + be->be_v_offset) >>
440 (be->be_mdev->bd_inode->i_blkbits - SECTOR_SHIFT);
441
442 dprintk("%s isect %llu, bh->b_blocknr %ld, using bsize %Zd\n",
443 __func__, (unsigned long long)isect, (long)bh->b_blocknr,
444 bh->b_size);
445 return;
446}
447
448/* Given an unmapped page, zero it or read in page for COW, page is locked
449 * by caller.
450 */
451static int
452init_page_for_write(struct page *page, struct pnfs_block_extent *cow_read)
453{
454 struct buffer_head *bh = NULL;
455 int ret = 0;
456 sector_t isect;
457
458 dprintk("%s enter, %p\n", __func__, page);
459 BUG_ON(PageUptodate(page));
460 if (!cow_read) {
461 zero_user_segment(page, 0, PAGE_SIZE);
462 SetPageUptodate(page);
463 goto cleanup;
464 }
465
466 bh = alloc_page_buffers(page, PAGE_CACHE_SIZE, 0);
467 if (!bh) {
468 ret = -ENOMEM;
469 goto cleanup;
470 }
471
472 isect = (sector_t) page->index << PAGE_CACHE_SECTOR_SHIFT;
473 map_block(bh, isect, cow_read);
474 if (!bh_uptodate_or_lock(bh))
475 ret = bh_submit_read(bh);
476 if (ret)
477 goto cleanup;
478 SetPageUptodate(page);
479
480cleanup:
481 bl_put_extent(cow_read);
482 if (bh)
483 free_buffer_head(bh);
484 if (ret) {
485 /* Need to mark layout with bad read...should now
486 * just use nfs4 for reads and writes.
487 */
488 mark_bad_read();
489 }
490 return ret;
491}
492
Peng Tao72c50882012-01-12 23:18:42 +0800493/* Find or create a zeroing page marked being writeback.
494 * Return ERR_PTR on error, NULL to indicate skip this page and page itself
495 * to indicate write out.
496 */
497static struct page *
498bl_find_get_zeroing_page(struct inode *inode, pgoff_t index,
499 struct pnfs_block_extent *cow_read)
500{
501 struct page *page;
502 int locked = 0;
503 page = find_get_page(inode->i_mapping, index);
504 if (page)
505 goto check_page;
506
507 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
508 if (unlikely(!page)) {
509 dprintk("%s oom\n", __func__);
510 return ERR_PTR(-ENOMEM);
511 }
512 locked = 1;
513
514check_page:
515 /* PageDirty: Other will write this out
516 * PageWriteback: Other is writing this out
517 * PageUptodate: It was read before
518 */
519 if (PageDirty(page) || PageWriteback(page)) {
520 print_page(page);
521 if (locked)
522 unlock_page(page);
523 page_cache_release(page);
524 return NULL;
525 }
526
527 if (!locked) {
528 lock_page(page);
529 locked = 1;
530 goto check_page;
531 }
532 if (!PageUptodate(page)) {
533 /* New page, readin or zero it */
534 init_page_for_write(page, cow_read);
535 }
536 set_page_writeback(page);
537 unlock_page(page);
538
539 return page;
540}
541
Fred Isaman650e2d32011-07-30 20:52:54 -0400542static enum pnfs_try_status
543bl_write_pagelist(struct nfs_write_data *wdata, int sync)
544{
Peng Tao71cdd402011-07-30 20:52:56 -0400545 int i, ret, npg_zero, pg_index, last = 0;
Fred Isaman650e2d32011-07-30 20:52:54 -0400546 struct bio *bio = NULL;
Peng Tao71cdd402011-07-30 20:52:56 -0400547 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
548 sector_t isect, last_isect = 0, extent_length = 0;
Fred Isaman650e2d32011-07-30 20:52:54 -0400549 struct parallel_io *par;
550 loff_t offset = wdata->args.offset;
551 size_t count = wdata->args.count;
552 struct page **pages = wdata->args.pages;
Peng Tao71cdd402011-07-30 20:52:56 -0400553 struct page *page;
554 pgoff_t index;
555 u64 temp;
556 int npg_per_block =
557 NFS_SERVER(wdata->inode)->pnfs_blksize >> PAGE_CACHE_SHIFT;
Fred Isaman650e2d32011-07-30 20:52:54 -0400558
559 dprintk("%s enter, %Zu@%lld\n", __func__, count, offset);
560 /* At this point, wdata->pages is a (sequential) list of nfs_pages.
Peng Tao71cdd402011-07-30 20:52:56 -0400561 * We want to write each, and if there is an error set pnfs_error
562 * to have it redone using nfs.
Fred Isaman650e2d32011-07-30 20:52:54 -0400563 */
564 par = alloc_parallel(wdata);
565 if (!par)
566 return PNFS_NOT_ATTEMPTED;
567 par->call_ops = *wdata->mds_ops;
568 par->call_ops.rpc_call_done = bl_rpc_do_nothing;
569 par->pnfs_callback = bl_end_par_io_write;
570 /* At this point, have to be more careful with error handling */
571
572 isect = (sector_t) ((offset & (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
Peng Tao71cdd402011-07-30 20:52:56 -0400573 be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg), isect, &cow_read);
574 if (!be || !is_writable(be, isect)) {
575 dprintk("%s no matching extents!\n", __func__);
576 wdata->pnfs_error = -EINVAL;
577 goto out;
578 }
579
580 /* First page inside INVALID extent */
581 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
582 temp = offset >> PAGE_CACHE_SHIFT;
583 npg_zero = do_div(temp, npg_per_block);
584 isect = (sector_t) (((offset - npg_zero * PAGE_CACHE_SIZE) &
585 (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
586 extent_length = be->be_length - (isect - be->be_f_offset);
587
588fill_invalid_ext:
589 dprintk("%s need to zero %d pages\n", __func__, npg_zero);
590 for (;npg_zero > 0; npg_zero--) {
Peng Tao75422742011-09-22 21:50:17 -0400591 if (bl_is_sector_init(be->be_inval, isect)) {
592 dprintk("isect %llu already init\n",
593 (unsigned long long)isect);
594 goto next_page;
595 }
Peng Tao71cdd402011-07-30 20:52:56 -0400596 /* page ref released in bl_end_io_write_zero */
597 index = isect >> PAGE_CACHE_SECTOR_SHIFT;
598 dprintk("%s zero %dth page: index %lu isect %llu\n",
599 __func__, npg_zero, index,
600 (unsigned long long)isect);
Peng Tao72c50882012-01-12 23:18:42 +0800601 page = bl_find_get_zeroing_page(wdata->inode, index,
602 cow_read);
603 if (unlikely(IS_ERR(page))) {
604 wdata->pnfs_error = PTR_ERR(page);
Peng Tao71cdd402011-07-30 20:52:56 -0400605 goto out;
Peng Tao72c50882012-01-12 23:18:42 +0800606 } else if (page == NULL)
Peng Tao71cdd402011-07-30 20:52:56 -0400607 goto next_page;
Peng Tao71cdd402011-07-30 20:52:56 -0400608
609 ret = bl_mark_sectors_init(be->be_inval, isect,
Peng Tao60c52e32012-01-12 23:18:40 +0800610 PAGE_CACHE_SECTORS);
Peng Tao71cdd402011-07-30 20:52:56 -0400611 if (unlikely(ret)) {
612 dprintk("%s bl_mark_sectors_init fail %d\n",
613 __func__, ret);
614 end_page_writeback(page);
615 page_cache_release(page);
616 wdata->pnfs_error = ret;
617 goto out;
618 }
619 bio = bl_add_page_to_bio(bio, npg_zero, WRITE,
620 isect, page, be,
621 bl_end_io_write_zero, par);
622 if (IS_ERR(bio)) {
623 wdata->pnfs_error = PTR_ERR(bio);
Peng Taoe6d05a72011-09-22 21:50:16 -0400624 bio = NULL;
Peng Tao71cdd402011-07-30 20:52:56 -0400625 goto out;
626 }
627 /* FIXME: This should be done in bi_end_io */
628 mark_extents_written(BLK_LSEG2EXT(wdata->lseg),
629 page->index << PAGE_CACHE_SHIFT,
630 PAGE_CACHE_SIZE);
631next_page:
632 isect += PAGE_CACHE_SECTORS;
633 extent_length -= PAGE_CACHE_SECTORS;
634 }
635 if (last)
636 goto write_done;
637 }
638 bio = bl_submit_bio(WRITE, bio);
639
640 /* Middle pages */
641 pg_index = wdata->args.pgbase >> PAGE_CACHE_SHIFT;
642 for (i = pg_index; i < wdata->npages; i++) {
Fred Isaman650e2d32011-07-30 20:52:54 -0400643 if (!extent_length) {
644 /* We've used up the previous extent */
645 bl_put_extent(be);
646 bio = bl_submit_bio(WRITE, bio);
647 /* Get the next one */
648 be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg),
649 isect, NULL);
650 if (!be || !is_writable(be, isect)) {
Peng Tao71cdd402011-07-30 20:52:56 -0400651 wdata->pnfs_error = -EINVAL;
Fred Isaman650e2d32011-07-30 20:52:54 -0400652 goto out;
653 }
654 extent_length = be->be_length -
Peng Tao71cdd402011-07-30 20:52:56 -0400655 (isect - be->be_f_offset);
Fred Isaman650e2d32011-07-30 20:52:54 -0400656 }
Peng Tao71cdd402011-07-30 20:52:56 -0400657 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
658 ret = bl_mark_sectors_init(be->be_inval, isect,
Peng Tao60c52e32012-01-12 23:18:40 +0800659 PAGE_CACHE_SECTORS);
Peng Tao71cdd402011-07-30 20:52:56 -0400660 if (unlikely(ret)) {
661 dprintk("%s bl_mark_sectors_init fail %d\n",
662 __func__, ret);
663 wdata->pnfs_error = ret;
664 goto out;
Fred Isaman650e2d32011-07-30 20:52:54 -0400665 }
Peng Tao71cdd402011-07-30 20:52:56 -0400666 }
667 bio = bl_add_page_to_bio(bio, wdata->npages - i, WRITE,
668 isect, pages[i], be,
669 bl_end_io_write, par);
670 if (IS_ERR(bio)) {
671 wdata->pnfs_error = PTR_ERR(bio);
Peng Taoe6d05a72011-09-22 21:50:16 -0400672 bio = NULL;
Peng Tao71cdd402011-07-30 20:52:56 -0400673 goto out;
Fred Isaman650e2d32011-07-30 20:52:54 -0400674 }
675 isect += PAGE_CACHE_SECTORS;
Peng Tao71cdd402011-07-30 20:52:56 -0400676 last_isect = isect;
Fred Isaman650e2d32011-07-30 20:52:54 -0400677 extent_length -= PAGE_CACHE_SECTORS;
678 }
Peng Tao71cdd402011-07-30 20:52:56 -0400679
680 /* Last page inside INVALID extent */
681 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
682 bio = bl_submit_bio(WRITE, bio);
683 temp = last_isect >> PAGE_CACHE_SECTOR_SHIFT;
684 npg_zero = npg_per_block - do_div(temp, npg_per_block);
685 if (npg_zero < npg_per_block) {
686 last = 1;
687 goto fill_invalid_ext;
688 }
689 }
690
691write_done:
692 wdata->res.count = (last_isect << SECTOR_SHIFT) - (offset);
693 if (count < wdata->res.count) {
Fred Isaman650e2d32011-07-30 20:52:54 -0400694 wdata->res.count = count;
Peng Tao71cdd402011-07-30 20:52:56 -0400695 }
Fred Isaman650e2d32011-07-30 20:52:54 -0400696out:
697 bl_put_extent(be);
698 bl_submit_bio(WRITE, bio);
699 put_parallel(par);
700 return PNFS_ATTEMPTED;
Fred Isaman155e7522011-07-30 20:52:39 -0400701}
702
Fred Isaman9e692962011-07-30 20:52:41 -0400703/* FIXME - range ignored */
Fred Isaman155e7522011-07-30 20:52:39 -0400704static void
Fred Isaman9e692962011-07-30 20:52:41 -0400705release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range)
Fred Isaman155e7522011-07-30 20:52:39 -0400706{
Fred Isaman9e692962011-07-30 20:52:41 -0400707 int i;
708 struct pnfs_block_extent *be;
709
710 spin_lock(&bl->bl_ext_lock);
711 for (i = 0; i < EXTENT_LISTS; i++) {
712 while (!list_empty(&bl->bl_extents[i])) {
713 be = list_first_entry(&bl->bl_extents[i],
714 struct pnfs_block_extent,
715 be_node);
716 list_del(&be->be_node);
717 bl_put_extent(be);
718 }
719 }
720 spin_unlock(&bl->bl_ext_lock);
Fred Isaman155e7522011-07-30 20:52:39 -0400721}
722
Fred Isaman155e7522011-07-30 20:52:39 -0400723static void
724release_inval_marks(struct pnfs_inval_markings *marks)
725{
Fred Isamanc1c2a4c2011-07-30 20:52:49 -0400726 struct pnfs_inval_tracking *pos, *temp;
727
728 list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) {
729 list_del(&pos->it_link);
730 kfree(pos);
731 }
Fred Isaman155e7522011-07-30 20:52:39 -0400732 return;
733}
734
735static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
736{
737 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
738
739 dprintk("%s enter\n", __func__);
740 release_extents(bl, NULL);
741 release_inval_marks(&bl->bl_inval);
742 kfree(bl);
743}
744
745static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode,
746 gfp_t gfp_flags)
747{
748 struct pnfs_block_layout *bl;
749
750 dprintk("%s enter\n", __func__);
751 bl = kzalloc(sizeof(*bl), gfp_flags);
752 if (!bl)
753 return NULL;
754 spin_lock_init(&bl->bl_ext_lock);
755 INIT_LIST_HEAD(&bl->bl_extents[0]);
756 INIT_LIST_HEAD(&bl->bl_extents[1]);
757 INIT_LIST_HEAD(&bl->bl_commit);
758 INIT_LIST_HEAD(&bl->bl_committing);
759 bl->bl_count = 0;
760 bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT;
761 BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize);
762 return &bl->bl_layout;
763}
764
Fred Isamana60d2eb2011-07-30 20:52:44 -0400765static void bl_free_lseg(struct pnfs_layout_segment *lseg)
Fred Isaman155e7522011-07-30 20:52:39 -0400766{
Fred Isamana60d2eb2011-07-30 20:52:44 -0400767 dprintk("%s enter\n", __func__);
768 kfree(lseg);
Fred Isaman155e7522011-07-30 20:52:39 -0400769}
770
Fred Isamana60d2eb2011-07-30 20:52:44 -0400771/* We pretty much ignore lseg, and store all data layout wide, so we
772 * can correctly merge.
773 */
774static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo,
775 struct nfs4_layoutget_res *lgr,
776 gfp_t gfp_flags)
Fred Isaman155e7522011-07-30 20:52:39 -0400777{
Fred Isamana60d2eb2011-07-30 20:52:44 -0400778 struct pnfs_layout_segment *lseg;
779 int status;
780
781 dprintk("%s enter\n", __func__);
782 lseg = kzalloc(sizeof(*lseg), gfp_flags);
783 if (!lseg)
784 return ERR_PTR(-ENOMEM);
785 status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags);
786 if (status) {
787 /* We don't want to call the full-blown bl_free_lseg,
788 * since on error extents were not touched.
789 */
790 kfree(lseg);
791 return ERR_PTR(status);
792 }
793 return lseg;
Fred Isaman155e7522011-07-30 20:52:39 -0400794}
795
796static void
797bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr,
798 const struct nfs4_layoutcommit_args *arg)
799{
Fred Isaman90ace122011-07-30 20:52:51 -0400800 dprintk("%s enter\n", __func__);
801 encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg);
Fred Isaman155e7522011-07-30 20:52:39 -0400802}
803
804static void
805bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
806{
Fred Isamanb2be7812011-07-30 20:52:52 -0400807 struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout;
808
809 dprintk("%s enter\n", __func__);
810 clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status);
Fred Isaman155e7522011-07-30 20:52:39 -0400811}
812
Fred Isaman2f9fd182011-07-30 20:52:46 -0400813static void free_blk_mountid(struct block_mount_id *mid)
814{
815 if (mid) {
Peng Tao93a38442012-01-12 23:18:47 +0800816 struct pnfs_block_dev *dev, *tmp;
817
818 /* No need to take bm_lock as we are last user freeing bm_devlist */
819 list_for_each_entry_safe(dev, tmp, &mid->bm_devlist, bm_node) {
Fred Isaman2f9fd182011-07-30 20:52:46 -0400820 list_del(&dev->bm_node);
821 bl_free_block_dev(dev);
822 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400823 kfree(mid);
824 }
825}
826
827/* This is mostly copied from the filelayout's get_device_info function.
828 * It seems much of this should be at the generic pnfs level.
829 */
830static struct pnfs_block_dev *
831nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh,
832 struct nfs4_deviceid *d_id)
833{
834 struct pnfs_device *dev;
Jim Rees516f2e22011-09-22 21:50:08 -0400835 struct pnfs_block_dev *rv;
Fred Isaman2f9fd182011-07-30 20:52:46 -0400836 u32 max_resp_sz;
837 int max_pages;
838 struct page **pages = NULL;
839 int i, rc;
840
841 /*
842 * Use the session max response size as the basis for setting
843 * GETDEVICEINFO's maxcount
844 */
845 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
846 max_pages = max_resp_sz >> PAGE_SHIFT;
847 dprintk("%s max_resp_sz %u max_pages %d\n",
848 __func__, max_resp_sz, max_pages);
849
850 dev = kmalloc(sizeof(*dev), GFP_NOFS);
851 if (!dev) {
852 dprintk("%s kmalloc failed\n", __func__);
Jim Rees516f2e22011-09-22 21:50:08 -0400853 return ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400854 }
855
856 pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS);
857 if (pages == NULL) {
858 kfree(dev);
Jim Rees516f2e22011-09-22 21:50:08 -0400859 return ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400860 }
861 for (i = 0; i < max_pages; i++) {
862 pages[i] = alloc_page(GFP_NOFS);
Jim Rees516f2e22011-09-22 21:50:08 -0400863 if (!pages[i]) {
864 rv = ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400865 goto out_free;
Jim Rees516f2e22011-09-22 21:50:08 -0400866 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400867 }
868
869 memcpy(&dev->dev_id, d_id, sizeof(*d_id));
870 dev->layout_type = LAYOUT_BLOCK_VOLUME;
871 dev->pages = pages;
872 dev->pgbase = 0;
873 dev->pglen = PAGE_SIZE * max_pages;
874 dev->mincount = 0;
875
876 dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data);
877 rc = nfs4_proc_getdeviceinfo(server, dev);
878 dprintk("%s getdevice info returns %d\n", __func__, rc);
Jim Rees516f2e22011-09-22 21:50:08 -0400879 if (rc) {
880 rv = ERR_PTR(rc);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400881 goto out_free;
Jim Rees516f2e22011-09-22 21:50:08 -0400882 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400883
884 rv = nfs4_blk_decode_device(server, dev);
885 out_free:
886 for (i = 0; i < max_pages; i++)
887 __free_page(pages[i]);
888 kfree(pages);
889 kfree(dev);
890 return rv;
891}
892
Fred Isaman155e7522011-07-30 20:52:39 -0400893static int
894bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
895{
Fred Isaman2f9fd182011-07-30 20:52:46 -0400896 struct block_mount_id *b_mt_id = NULL;
897 struct pnfs_devicelist *dlist = NULL;
898 struct pnfs_block_dev *bdev;
899 LIST_HEAD(block_disklist);
Jim Rees516f2e22011-09-22 21:50:08 -0400900 int status, i;
Fred Isaman2f9fd182011-07-30 20:52:46 -0400901
Fred Isaman155e7522011-07-30 20:52:39 -0400902 dprintk("%s enter\n", __func__);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400903
904 if (server->pnfs_blksize == 0) {
905 dprintk("%s Server did not return blksize\n", __func__);
906 return -EINVAL;
907 }
908 b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS);
909 if (!b_mt_id) {
910 status = -ENOMEM;
911 goto out_error;
912 }
913 /* Initialize nfs4 block layout mount id */
914 spin_lock_init(&b_mt_id->bm_lock);
915 INIT_LIST_HEAD(&b_mt_id->bm_devlist);
916
917 dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS);
918 if (!dlist) {
919 status = -ENOMEM;
920 goto out_error;
921 }
922 dlist->eof = 0;
923 while (!dlist->eof) {
924 status = nfs4_proc_getdevicelist(server, fh, dlist);
925 if (status)
926 goto out_error;
927 dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n",
928 __func__, dlist->num_devs, dlist->eof);
929 for (i = 0; i < dlist->num_devs; i++) {
930 bdev = nfs4_blk_get_deviceinfo(server, fh,
931 &dlist->dev_id[i]);
Jim Rees516f2e22011-09-22 21:50:08 -0400932 if (IS_ERR(bdev)) {
933 status = PTR_ERR(bdev);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400934 goto out_error;
935 }
936 spin_lock(&b_mt_id->bm_lock);
937 list_add(&bdev->bm_node, &b_mt_id->bm_devlist);
938 spin_unlock(&b_mt_id->bm_lock);
939 }
940 }
941 dprintk("%s SUCCESS\n", __func__);
942 server->pnfs_ld_data = b_mt_id;
943
944 out_return:
945 kfree(dlist);
946 return status;
947
948 out_error:
949 free_blk_mountid(b_mt_id);
950 goto out_return;
Fred Isaman155e7522011-07-30 20:52:39 -0400951}
952
953static int
954bl_clear_layoutdriver(struct nfs_server *server)
955{
Fred Isaman2f9fd182011-07-30 20:52:46 -0400956 struct block_mount_id *b_mt_id = server->pnfs_ld_data;
957
Fred Isaman155e7522011-07-30 20:52:39 -0400958 dprintk("%s enter\n", __func__);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400959 free_blk_mountid(b_mt_id);
960 dprintk("%s RETURNS\n", __func__);
Fred Isaman155e7522011-07-30 20:52:39 -0400961 return 0;
962}
963
Benny Halevye9643fe2011-07-30 20:52:40 -0400964static const struct nfs_pageio_ops bl_pg_read_ops = {
965 .pg_init = pnfs_generic_pg_init_read,
966 .pg_test = pnfs_generic_pg_test,
967 .pg_doio = pnfs_generic_pg_readpages,
968};
969
970static const struct nfs_pageio_ops bl_pg_write_ops = {
971 .pg_init = pnfs_generic_pg_init_write,
972 .pg_test = pnfs_generic_pg_test,
973 .pg_doio = pnfs_generic_pg_writepages,
974};
975
Fred Isaman155e7522011-07-30 20:52:39 -0400976static struct pnfs_layoutdriver_type blocklayout_type = {
977 .id = LAYOUT_BLOCK_VOLUME,
978 .name = "LAYOUT_BLOCK_VOLUME",
979 .read_pagelist = bl_read_pagelist,
980 .write_pagelist = bl_write_pagelist,
981 .alloc_layout_hdr = bl_alloc_layout_hdr,
982 .free_layout_hdr = bl_free_layout_hdr,
983 .alloc_lseg = bl_alloc_lseg,
984 .free_lseg = bl_free_lseg,
985 .encode_layoutcommit = bl_encode_layoutcommit,
986 .cleanup_layoutcommit = bl_cleanup_layoutcommit,
987 .set_layoutdriver = bl_set_layoutdriver,
988 .clear_layoutdriver = bl_clear_layoutdriver,
Benny Halevye9643fe2011-07-30 20:52:40 -0400989 .pg_read_ops = &bl_pg_read_ops,
990 .pg_write_ops = &bl_pg_write_ops,
Fred Isaman155e7522011-07-30 20:52:39 -0400991};
992
Jim Reesfe0a9b72011-07-30 20:52:42 -0400993static const struct rpc_pipe_ops bl_upcall_ops = {
Peng Taoc1225152011-09-22 21:50:10 -0400994 .upcall = rpc_pipe_generic_upcall,
Jim Reesfe0a9b72011-07-30 20:52:42 -0400995 .downcall = bl_pipe_downcall,
996 .destroy_msg = bl_pipe_destroy_msg,
997};
998
Fred Isaman155e7522011-07-30 20:52:39 -0400999static int __init nfs4blocklayout_init(void)
1000{
Jim Reesfe0a9b72011-07-30 20:52:42 -04001001 struct vfsmount *mnt;
1002 struct path path;
Fred Isaman155e7522011-07-30 20:52:39 -04001003 int ret;
1004
1005 dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
1006
1007 ret = pnfs_register_layoutdriver(&blocklayout_type);
Jim Reesfe0a9b72011-07-30 20:52:42 -04001008 if (ret)
1009 goto out;
1010
1011 init_waitqueue_head(&bl_wq);
1012
1013 mnt = rpc_get_mount();
1014 if (IS_ERR(mnt)) {
1015 ret = PTR_ERR(mnt);
1016 goto out_remove;
1017 }
1018
1019 ret = vfs_path_lookup(mnt->mnt_root,
1020 mnt,
1021 NFS_PIPE_DIRNAME, 0, &path);
1022 if (ret)
Peng Tao760383f2011-09-22 21:50:11 -04001023 goto out_putrpc;
Jim Reesfe0a9b72011-07-30 20:52:42 -04001024
1025 bl_device_pipe = rpc_mkpipe(path.dentry, "blocklayout", NULL,
1026 &bl_upcall_ops, 0);
Peng Tao760383f2011-09-22 21:50:11 -04001027 path_put(&path);
Jim Reesfe0a9b72011-07-30 20:52:42 -04001028 if (IS_ERR(bl_device_pipe)) {
1029 ret = PTR_ERR(bl_device_pipe);
Peng Tao760383f2011-09-22 21:50:11 -04001030 goto out_putrpc;
Jim Reesfe0a9b72011-07-30 20:52:42 -04001031 }
1032out:
1033 return ret;
1034
Peng Tao760383f2011-09-22 21:50:11 -04001035out_putrpc:
1036 rpc_put_mount();
Jim Reesfe0a9b72011-07-30 20:52:42 -04001037out_remove:
1038 pnfs_unregister_layoutdriver(&blocklayout_type);
Fred Isaman155e7522011-07-30 20:52:39 -04001039 return ret;
1040}
1041
1042static void __exit nfs4blocklayout_exit(void)
1043{
1044 dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
1045 __func__);
1046
1047 pnfs_unregister_layoutdriver(&blocklayout_type);
Jim Reesfe0a9b72011-07-30 20:52:42 -04001048 rpc_unlink(bl_device_pipe);
Peng Tao760383f2011-09-22 21:50:11 -04001049 rpc_put_mount();
Fred Isaman155e7522011-07-30 20:52:39 -04001050}
1051
1052MODULE_ALIAS("nfs-layouttype4-3");
1053
1054module_init(nfs4blocklayout_init);
1055module_exit(nfs4blocklayout_exit);