blob: baf0bf2acbd442e6115bc11d01c4c5313a9f29d2 [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
Fred Isaman650e2d32011-07-30 20:52:54 -0400493static enum pnfs_try_status
494bl_write_pagelist(struct nfs_write_data *wdata, int sync)
495{
Peng Tao71cdd402011-07-30 20:52:56 -0400496 int i, ret, npg_zero, pg_index, last = 0;
Fred Isaman650e2d32011-07-30 20:52:54 -0400497 struct bio *bio = NULL;
Peng Tao71cdd402011-07-30 20:52:56 -0400498 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
499 sector_t isect, last_isect = 0, extent_length = 0;
Fred Isaman650e2d32011-07-30 20:52:54 -0400500 struct parallel_io *par;
501 loff_t offset = wdata->args.offset;
502 size_t count = wdata->args.count;
503 struct page **pages = wdata->args.pages;
Peng Tao71cdd402011-07-30 20:52:56 -0400504 struct page *page;
505 pgoff_t index;
506 u64 temp;
507 int npg_per_block =
508 NFS_SERVER(wdata->inode)->pnfs_blksize >> PAGE_CACHE_SHIFT;
Fred Isaman650e2d32011-07-30 20:52:54 -0400509
510 dprintk("%s enter, %Zu@%lld\n", __func__, count, offset);
511 /* At this point, wdata->pages is a (sequential) list of nfs_pages.
Peng Tao71cdd402011-07-30 20:52:56 -0400512 * We want to write each, and if there is an error set pnfs_error
513 * to have it redone using nfs.
Fred Isaman650e2d32011-07-30 20:52:54 -0400514 */
515 par = alloc_parallel(wdata);
516 if (!par)
517 return PNFS_NOT_ATTEMPTED;
518 par->call_ops = *wdata->mds_ops;
519 par->call_ops.rpc_call_done = bl_rpc_do_nothing;
520 par->pnfs_callback = bl_end_par_io_write;
521 /* At this point, have to be more careful with error handling */
522
523 isect = (sector_t) ((offset & (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
Peng Tao71cdd402011-07-30 20:52:56 -0400524 be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg), isect, &cow_read);
525 if (!be || !is_writable(be, isect)) {
526 dprintk("%s no matching extents!\n", __func__);
527 wdata->pnfs_error = -EINVAL;
528 goto out;
529 }
530
531 /* First page inside INVALID extent */
532 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
533 temp = offset >> PAGE_CACHE_SHIFT;
534 npg_zero = do_div(temp, npg_per_block);
535 isect = (sector_t) (((offset - npg_zero * PAGE_CACHE_SIZE) &
536 (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
537 extent_length = be->be_length - (isect - be->be_f_offset);
538
539fill_invalid_ext:
540 dprintk("%s need to zero %d pages\n", __func__, npg_zero);
541 for (;npg_zero > 0; npg_zero--) {
Peng Tao75422742011-09-22 21:50:17 -0400542 if (bl_is_sector_init(be->be_inval, isect)) {
543 dprintk("isect %llu already init\n",
544 (unsigned long long)isect);
545 goto next_page;
546 }
Peng Tao71cdd402011-07-30 20:52:56 -0400547 /* page ref released in bl_end_io_write_zero */
548 index = isect >> PAGE_CACHE_SECTOR_SHIFT;
549 dprintk("%s zero %dth page: index %lu isect %llu\n",
550 __func__, npg_zero, index,
551 (unsigned long long)isect);
552 page =
553 find_or_create_page(wdata->inode->i_mapping, index,
554 GFP_NOFS);
555 if (!page) {
556 dprintk("%s oom\n", __func__);
557 wdata->pnfs_error = -ENOMEM;
558 goto out;
559 }
560
561 /* PageDirty: Other will write this out
562 * PageWriteback: Other is writing this out
563 * PageUptodate: It was read before
564 * sector_initialized: already written out
565 */
Peng Tao75422742011-09-22 21:50:17 -0400566 if (PageDirty(page) || PageWriteback(page)) {
Peng Tao71cdd402011-07-30 20:52:56 -0400567 print_page(page);
568 unlock_page(page);
569 page_cache_release(page);
570 goto next_page;
571 }
572 if (!PageUptodate(page)) {
573 /* New page, readin or zero it */
574 init_page_for_write(page, cow_read);
575 }
576 set_page_writeback(page);
577 unlock_page(page);
578
579 ret = bl_mark_sectors_init(be->be_inval, isect,
580 PAGE_CACHE_SECTORS,
581 NULL);
582 if (unlikely(ret)) {
583 dprintk("%s bl_mark_sectors_init fail %d\n",
584 __func__, ret);
585 end_page_writeback(page);
586 page_cache_release(page);
587 wdata->pnfs_error = ret;
588 goto out;
589 }
590 bio = bl_add_page_to_bio(bio, npg_zero, WRITE,
591 isect, page, be,
592 bl_end_io_write_zero, par);
593 if (IS_ERR(bio)) {
594 wdata->pnfs_error = PTR_ERR(bio);
Peng Taoe6d05a72011-09-22 21:50:16 -0400595 bio = NULL;
Peng Tao71cdd402011-07-30 20:52:56 -0400596 goto out;
597 }
598 /* FIXME: This should be done in bi_end_io */
599 mark_extents_written(BLK_LSEG2EXT(wdata->lseg),
600 page->index << PAGE_CACHE_SHIFT,
601 PAGE_CACHE_SIZE);
602next_page:
603 isect += PAGE_CACHE_SECTORS;
604 extent_length -= PAGE_CACHE_SECTORS;
605 }
606 if (last)
607 goto write_done;
608 }
609 bio = bl_submit_bio(WRITE, bio);
610
611 /* Middle pages */
612 pg_index = wdata->args.pgbase >> PAGE_CACHE_SHIFT;
613 for (i = pg_index; i < wdata->npages; i++) {
Fred Isaman650e2d32011-07-30 20:52:54 -0400614 if (!extent_length) {
615 /* We've used up the previous extent */
616 bl_put_extent(be);
617 bio = bl_submit_bio(WRITE, bio);
618 /* Get the next one */
619 be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg),
620 isect, NULL);
621 if (!be || !is_writable(be, isect)) {
Peng Tao71cdd402011-07-30 20:52:56 -0400622 wdata->pnfs_error = -EINVAL;
Fred Isaman650e2d32011-07-30 20:52:54 -0400623 goto out;
624 }
625 extent_length = be->be_length -
Peng Tao71cdd402011-07-30 20:52:56 -0400626 (isect - be->be_f_offset);
Fred Isaman650e2d32011-07-30 20:52:54 -0400627 }
Peng Tao71cdd402011-07-30 20:52:56 -0400628 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
629 ret = bl_mark_sectors_init(be->be_inval, isect,
630 PAGE_CACHE_SECTORS,
631 NULL);
632 if (unlikely(ret)) {
633 dprintk("%s bl_mark_sectors_init fail %d\n",
634 __func__, ret);
635 wdata->pnfs_error = ret;
636 goto out;
Fred Isaman650e2d32011-07-30 20:52:54 -0400637 }
Peng Tao71cdd402011-07-30 20:52:56 -0400638 }
639 bio = bl_add_page_to_bio(bio, wdata->npages - i, WRITE,
640 isect, pages[i], be,
641 bl_end_io_write, par);
642 if (IS_ERR(bio)) {
643 wdata->pnfs_error = PTR_ERR(bio);
Peng Taoe6d05a72011-09-22 21:50:16 -0400644 bio = NULL;
Peng Tao71cdd402011-07-30 20:52:56 -0400645 goto out;
Fred Isaman650e2d32011-07-30 20:52:54 -0400646 }
647 isect += PAGE_CACHE_SECTORS;
Peng Tao71cdd402011-07-30 20:52:56 -0400648 last_isect = isect;
Fred Isaman650e2d32011-07-30 20:52:54 -0400649 extent_length -= PAGE_CACHE_SECTORS;
650 }
Peng Tao71cdd402011-07-30 20:52:56 -0400651
652 /* Last page inside INVALID extent */
653 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
654 bio = bl_submit_bio(WRITE, bio);
655 temp = last_isect >> PAGE_CACHE_SECTOR_SHIFT;
656 npg_zero = npg_per_block - do_div(temp, npg_per_block);
657 if (npg_zero < npg_per_block) {
658 last = 1;
659 goto fill_invalid_ext;
660 }
661 }
662
663write_done:
664 wdata->res.count = (last_isect << SECTOR_SHIFT) - (offset);
665 if (count < wdata->res.count) {
Fred Isaman650e2d32011-07-30 20:52:54 -0400666 wdata->res.count = count;
Peng Tao71cdd402011-07-30 20:52:56 -0400667 }
Fred Isaman650e2d32011-07-30 20:52:54 -0400668out:
669 bl_put_extent(be);
670 bl_submit_bio(WRITE, bio);
671 put_parallel(par);
672 return PNFS_ATTEMPTED;
Fred Isaman155e7522011-07-30 20:52:39 -0400673}
674
Fred Isaman9e692962011-07-30 20:52:41 -0400675/* FIXME - range ignored */
Fred Isaman155e7522011-07-30 20:52:39 -0400676static void
Fred Isaman9e692962011-07-30 20:52:41 -0400677release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range)
Fred Isaman155e7522011-07-30 20:52:39 -0400678{
Fred Isaman9e692962011-07-30 20:52:41 -0400679 int i;
680 struct pnfs_block_extent *be;
681
682 spin_lock(&bl->bl_ext_lock);
683 for (i = 0; i < EXTENT_LISTS; i++) {
684 while (!list_empty(&bl->bl_extents[i])) {
685 be = list_first_entry(&bl->bl_extents[i],
686 struct pnfs_block_extent,
687 be_node);
688 list_del(&be->be_node);
689 bl_put_extent(be);
690 }
691 }
692 spin_unlock(&bl->bl_ext_lock);
Fred Isaman155e7522011-07-30 20:52:39 -0400693}
694
Fred Isaman155e7522011-07-30 20:52:39 -0400695static void
696release_inval_marks(struct pnfs_inval_markings *marks)
697{
Fred Isamanc1c2a4c2011-07-30 20:52:49 -0400698 struct pnfs_inval_tracking *pos, *temp;
699
700 list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) {
701 list_del(&pos->it_link);
702 kfree(pos);
703 }
Fred Isaman155e7522011-07-30 20:52:39 -0400704 return;
705}
706
707static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
708{
709 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
710
711 dprintk("%s enter\n", __func__);
712 release_extents(bl, NULL);
713 release_inval_marks(&bl->bl_inval);
714 kfree(bl);
715}
716
717static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode,
718 gfp_t gfp_flags)
719{
720 struct pnfs_block_layout *bl;
721
722 dprintk("%s enter\n", __func__);
723 bl = kzalloc(sizeof(*bl), gfp_flags);
724 if (!bl)
725 return NULL;
726 spin_lock_init(&bl->bl_ext_lock);
727 INIT_LIST_HEAD(&bl->bl_extents[0]);
728 INIT_LIST_HEAD(&bl->bl_extents[1]);
729 INIT_LIST_HEAD(&bl->bl_commit);
730 INIT_LIST_HEAD(&bl->bl_committing);
731 bl->bl_count = 0;
732 bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT;
733 BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize);
734 return &bl->bl_layout;
735}
736
Fred Isamana60d2eb2011-07-30 20:52:44 -0400737static void bl_free_lseg(struct pnfs_layout_segment *lseg)
Fred Isaman155e7522011-07-30 20:52:39 -0400738{
Fred Isamana60d2eb2011-07-30 20:52:44 -0400739 dprintk("%s enter\n", __func__);
740 kfree(lseg);
Fred Isaman155e7522011-07-30 20:52:39 -0400741}
742
Fred Isamana60d2eb2011-07-30 20:52:44 -0400743/* We pretty much ignore lseg, and store all data layout wide, so we
744 * can correctly merge.
745 */
746static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo,
747 struct nfs4_layoutget_res *lgr,
748 gfp_t gfp_flags)
Fred Isaman155e7522011-07-30 20:52:39 -0400749{
Fred Isamana60d2eb2011-07-30 20:52:44 -0400750 struct pnfs_layout_segment *lseg;
751 int status;
752
753 dprintk("%s enter\n", __func__);
754 lseg = kzalloc(sizeof(*lseg), gfp_flags);
755 if (!lseg)
756 return ERR_PTR(-ENOMEM);
757 status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags);
758 if (status) {
759 /* We don't want to call the full-blown bl_free_lseg,
760 * since on error extents were not touched.
761 */
762 kfree(lseg);
763 return ERR_PTR(status);
764 }
765 return lseg;
Fred Isaman155e7522011-07-30 20:52:39 -0400766}
767
768static void
769bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr,
770 const struct nfs4_layoutcommit_args *arg)
771{
Fred Isaman90ace122011-07-30 20:52:51 -0400772 dprintk("%s enter\n", __func__);
773 encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg);
Fred Isaman155e7522011-07-30 20:52:39 -0400774}
775
776static void
777bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
778{
Fred Isamanb2be7812011-07-30 20:52:52 -0400779 struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout;
780
781 dprintk("%s enter\n", __func__);
782 clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status);
Fred Isaman155e7522011-07-30 20:52:39 -0400783}
784
Fred Isaman2f9fd182011-07-30 20:52:46 -0400785static void free_blk_mountid(struct block_mount_id *mid)
786{
787 if (mid) {
Peng Tao93a38442012-01-12 23:18:47 +0800788 struct pnfs_block_dev *dev, *tmp;
789
790 /* No need to take bm_lock as we are last user freeing bm_devlist */
791 list_for_each_entry_safe(dev, tmp, &mid->bm_devlist, bm_node) {
Fred Isaman2f9fd182011-07-30 20:52:46 -0400792 list_del(&dev->bm_node);
793 bl_free_block_dev(dev);
794 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400795 kfree(mid);
796 }
797}
798
799/* This is mostly copied from the filelayout's get_device_info function.
800 * It seems much of this should be at the generic pnfs level.
801 */
802static struct pnfs_block_dev *
803nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh,
804 struct nfs4_deviceid *d_id)
805{
806 struct pnfs_device *dev;
Jim Rees516f2e22011-09-22 21:50:08 -0400807 struct pnfs_block_dev *rv;
Fred Isaman2f9fd182011-07-30 20:52:46 -0400808 u32 max_resp_sz;
809 int max_pages;
810 struct page **pages = NULL;
811 int i, rc;
812
813 /*
814 * Use the session max response size as the basis for setting
815 * GETDEVICEINFO's maxcount
816 */
817 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
818 max_pages = max_resp_sz >> PAGE_SHIFT;
819 dprintk("%s max_resp_sz %u max_pages %d\n",
820 __func__, max_resp_sz, max_pages);
821
822 dev = kmalloc(sizeof(*dev), GFP_NOFS);
823 if (!dev) {
824 dprintk("%s kmalloc failed\n", __func__);
Jim Rees516f2e22011-09-22 21:50:08 -0400825 return ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400826 }
827
828 pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS);
829 if (pages == NULL) {
830 kfree(dev);
Jim Rees516f2e22011-09-22 21:50:08 -0400831 return ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400832 }
833 for (i = 0; i < max_pages; i++) {
834 pages[i] = alloc_page(GFP_NOFS);
Jim Rees516f2e22011-09-22 21:50:08 -0400835 if (!pages[i]) {
836 rv = ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400837 goto out_free;
Jim Rees516f2e22011-09-22 21:50:08 -0400838 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400839 }
840
841 memcpy(&dev->dev_id, d_id, sizeof(*d_id));
842 dev->layout_type = LAYOUT_BLOCK_VOLUME;
843 dev->pages = pages;
844 dev->pgbase = 0;
845 dev->pglen = PAGE_SIZE * max_pages;
846 dev->mincount = 0;
847
848 dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data);
849 rc = nfs4_proc_getdeviceinfo(server, dev);
850 dprintk("%s getdevice info returns %d\n", __func__, rc);
Jim Rees516f2e22011-09-22 21:50:08 -0400851 if (rc) {
852 rv = ERR_PTR(rc);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400853 goto out_free;
Jim Rees516f2e22011-09-22 21:50:08 -0400854 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400855
856 rv = nfs4_blk_decode_device(server, dev);
857 out_free:
858 for (i = 0; i < max_pages; i++)
859 __free_page(pages[i]);
860 kfree(pages);
861 kfree(dev);
862 return rv;
863}
864
Fred Isaman155e7522011-07-30 20:52:39 -0400865static int
866bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
867{
Fred Isaman2f9fd182011-07-30 20:52:46 -0400868 struct block_mount_id *b_mt_id = NULL;
869 struct pnfs_devicelist *dlist = NULL;
870 struct pnfs_block_dev *bdev;
871 LIST_HEAD(block_disklist);
Jim Rees516f2e22011-09-22 21:50:08 -0400872 int status, i;
Fred Isaman2f9fd182011-07-30 20:52:46 -0400873
Fred Isaman155e7522011-07-30 20:52:39 -0400874 dprintk("%s enter\n", __func__);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400875
876 if (server->pnfs_blksize == 0) {
877 dprintk("%s Server did not return blksize\n", __func__);
878 return -EINVAL;
879 }
880 b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS);
881 if (!b_mt_id) {
882 status = -ENOMEM;
883 goto out_error;
884 }
885 /* Initialize nfs4 block layout mount id */
886 spin_lock_init(&b_mt_id->bm_lock);
887 INIT_LIST_HEAD(&b_mt_id->bm_devlist);
888
889 dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS);
890 if (!dlist) {
891 status = -ENOMEM;
892 goto out_error;
893 }
894 dlist->eof = 0;
895 while (!dlist->eof) {
896 status = nfs4_proc_getdevicelist(server, fh, dlist);
897 if (status)
898 goto out_error;
899 dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n",
900 __func__, dlist->num_devs, dlist->eof);
901 for (i = 0; i < dlist->num_devs; i++) {
902 bdev = nfs4_blk_get_deviceinfo(server, fh,
903 &dlist->dev_id[i]);
Jim Rees516f2e22011-09-22 21:50:08 -0400904 if (IS_ERR(bdev)) {
905 status = PTR_ERR(bdev);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400906 goto out_error;
907 }
908 spin_lock(&b_mt_id->bm_lock);
909 list_add(&bdev->bm_node, &b_mt_id->bm_devlist);
910 spin_unlock(&b_mt_id->bm_lock);
911 }
912 }
913 dprintk("%s SUCCESS\n", __func__);
914 server->pnfs_ld_data = b_mt_id;
915
916 out_return:
917 kfree(dlist);
918 return status;
919
920 out_error:
921 free_blk_mountid(b_mt_id);
922 goto out_return;
Fred Isaman155e7522011-07-30 20:52:39 -0400923}
924
925static int
926bl_clear_layoutdriver(struct nfs_server *server)
927{
Fred Isaman2f9fd182011-07-30 20:52:46 -0400928 struct block_mount_id *b_mt_id = server->pnfs_ld_data;
929
Fred Isaman155e7522011-07-30 20:52:39 -0400930 dprintk("%s enter\n", __func__);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400931 free_blk_mountid(b_mt_id);
932 dprintk("%s RETURNS\n", __func__);
Fred Isaman155e7522011-07-30 20:52:39 -0400933 return 0;
934}
935
Benny Halevye9643fe2011-07-30 20:52:40 -0400936static const struct nfs_pageio_ops bl_pg_read_ops = {
937 .pg_init = pnfs_generic_pg_init_read,
938 .pg_test = pnfs_generic_pg_test,
939 .pg_doio = pnfs_generic_pg_readpages,
940};
941
942static const struct nfs_pageio_ops bl_pg_write_ops = {
943 .pg_init = pnfs_generic_pg_init_write,
944 .pg_test = pnfs_generic_pg_test,
945 .pg_doio = pnfs_generic_pg_writepages,
946};
947
Fred Isaman155e7522011-07-30 20:52:39 -0400948static struct pnfs_layoutdriver_type blocklayout_type = {
949 .id = LAYOUT_BLOCK_VOLUME,
950 .name = "LAYOUT_BLOCK_VOLUME",
951 .read_pagelist = bl_read_pagelist,
952 .write_pagelist = bl_write_pagelist,
953 .alloc_layout_hdr = bl_alloc_layout_hdr,
954 .free_layout_hdr = bl_free_layout_hdr,
955 .alloc_lseg = bl_alloc_lseg,
956 .free_lseg = bl_free_lseg,
957 .encode_layoutcommit = bl_encode_layoutcommit,
958 .cleanup_layoutcommit = bl_cleanup_layoutcommit,
959 .set_layoutdriver = bl_set_layoutdriver,
960 .clear_layoutdriver = bl_clear_layoutdriver,
Benny Halevye9643fe2011-07-30 20:52:40 -0400961 .pg_read_ops = &bl_pg_read_ops,
962 .pg_write_ops = &bl_pg_write_ops,
Fred Isaman155e7522011-07-30 20:52:39 -0400963};
964
Jim Reesfe0a9b72011-07-30 20:52:42 -0400965static const struct rpc_pipe_ops bl_upcall_ops = {
Peng Taoc1225152011-09-22 21:50:10 -0400966 .upcall = rpc_pipe_generic_upcall,
Jim Reesfe0a9b72011-07-30 20:52:42 -0400967 .downcall = bl_pipe_downcall,
968 .destroy_msg = bl_pipe_destroy_msg,
969};
970
Fred Isaman155e7522011-07-30 20:52:39 -0400971static int __init nfs4blocklayout_init(void)
972{
Jim Reesfe0a9b72011-07-30 20:52:42 -0400973 struct vfsmount *mnt;
974 struct path path;
Fred Isaman155e7522011-07-30 20:52:39 -0400975 int ret;
976
977 dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
978
979 ret = pnfs_register_layoutdriver(&blocklayout_type);
Jim Reesfe0a9b72011-07-30 20:52:42 -0400980 if (ret)
981 goto out;
982
983 init_waitqueue_head(&bl_wq);
984
985 mnt = rpc_get_mount();
986 if (IS_ERR(mnt)) {
987 ret = PTR_ERR(mnt);
988 goto out_remove;
989 }
990
991 ret = vfs_path_lookup(mnt->mnt_root,
992 mnt,
993 NFS_PIPE_DIRNAME, 0, &path);
994 if (ret)
Peng Tao760383f2011-09-22 21:50:11 -0400995 goto out_putrpc;
Jim Reesfe0a9b72011-07-30 20:52:42 -0400996
997 bl_device_pipe = rpc_mkpipe(path.dentry, "blocklayout", NULL,
998 &bl_upcall_ops, 0);
Peng Tao760383f2011-09-22 21:50:11 -0400999 path_put(&path);
Jim Reesfe0a9b72011-07-30 20:52:42 -04001000 if (IS_ERR(bl_device_pipe)) {
1001 ret = PTR_ERR(bl_device_pipe);
Peng Tao760383f2011-09-22 21:50:11 -04001002 goto out_putrpc;
Jim Reesfe0a9b72011-07-30 20:52:42 -04001003 }
1004out:
1005 return ret;
1006
Peng Tao760383f2011-09-22 21:50:11 -04001007out_putrpc:
1008 rpc_put_mount();
Jim Reesfe0a9b72011-07-30 20:52:42 -04001009out_remove:
1010 pnfs_unregister_layoutdriver(&blocklayout_type);
Fred Isaman155e7522011-07-30 20:52:39 -04001011 return ret;
1012}
1013
1014static void __exit nfs4blocklayout_exit(void)
1015{
1016 dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
1017 __func__);
1018
1019 pnfs_unregister_layoutdriver(&blocklayout_type);
Jim Reesfe0a9b72011-07-30 20:52:42 -04001020 rpc_unlink(bl_device_pipe);
Peng Tao760383f2011-09-22 21:50:11 -04001021 rpc_put_mount();
Fred Isaman155e7522011-07-30 20:52:39 -04001022}
1023
1024MODULE_ALIAS("nfs-layouttype4-3");
1025
1026module_init(nfs4blocklayout_init);
1027module_exit(nfs4blocklayout_exit);