blob: dd392ed5f2e28ef48a85b37d0dc2ade6ab903d80 [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
Jim Rees10bd2952012-04-09 22:33:39 -040041#include "../pnfs.h"
42#include "../internal.h"
Fred Isaman155e7522011-07-30 20:52:39 -040043#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
Fred Isaman9549ec02011-07-30 20:52:53 -040051static 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
Fred Isaman650e2d32011-07-30 20:52:54 -040078/* 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{
Peng Tao71cdd402011-07-30 20:52:56 -040083 return (be->be_state == PNFS_BLOCK_READWRITE_DATA ||
84 be->be_state == PNFS_BLOCK_INVALID_DATA);
Fred Isaman650e2d32011-07-30 20:52:54 -040085}
86
Fred Isaman9549ec02011-07-30 20:52:53 -040087/* 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;
Peng Tao7c5465d2012-01-12 23:18:46 +080092 void (*pnfs_callback) (void *data, int num_se);
Fred Isaman9549ec02011-07-30 20:52:53 -040093 void *data;
Peng Tao7c5465d2012-01-12 23:18:46 +080094 int bse_count;
Fred Isaman9549ec02011-07-30 20:52:53 -040095};
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);
Peng Tao7c5465d2012-01-12 23:18:46 +0800105 rv->bse_count = 0;
Fred Isaman9549ec02011-07-30 20:52:53 -0400106 }
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__);
Peng Tao7c5465d2012-01-12 23:18:46 +0800120 p->pnfs_callback(p->data, p->bse_count);
Fred Isaman9549ec02011-07-30 20:52:53 -0400121 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;
Fred Isaman9549ec02011-07-30 20:52:53 -0400190
191 do {
192 struct page *page = bvec->bv_page;
193
194 if (--bvec >= bio->bi_io_vec)
195 prefetchw(&bvec->bv_page->flags);
196 if (uptodate)
197 SetPageUptodate(page);
198 } while (bvec >= bio->bi_io_vec);
199 if (!uptodate) {
Fred Isamancd841602012-04-20 14:47:44 -0400200 struct nfs_read_data *rdata = par->data;
201 struct nfs_pgio_header *header = rdata->header;
202
203 if (!header->pnfs_error)
204 header->pnfs_error = -EIO;
205 pnfs_set_lo_fail(header->lseg);
Fred Isaman9549ec02011-07-30 20:52:53 -0400206 }
207 bio_put(bio);
208 put_parallel(par);
209}
210
211static void bl_read_cleanup(struct work_struct *work)
212{
213 struct rpc_task *task;
214 struct nfs_read_data *rdata;
215 dprintk("%s enter\n", __func__);
216 task = container_of(work, struct rpc_task, u.tk_work);
217 rdata = container_of(task, struct nfs_read_data, task);
218 pnfs_ld_read_done(rdata);
219}
220
221static void
Peng Tao7c5465d2012-01-12 23:18:46 +0800222bl_end_par_io_read(void *data, int unused)
Fred Isaman9549ec02011-07-30 20:52:53 -0400223{
224 struct nfs_read_data *rdata = data;
225
Fred Isamancd841602012-04-20 14:47:44 -0400226 rdata->task.tk_status = rdata->header->pnfs_error;
Fred Isaman9549ec02011-07-30 20:52:53 -0400227 INIT_WORK(&rdata->task.u.tk_work, bl_read_cleanup);
228 schedule_work(&rdata->task.u.tk_work);
229}
230
Peng Tao159e0562012-05-29 13:57:58 +0800231static bool
232bl_check_alignment(u64 offset, u32 len, unsigned long blkmask)
233{
234 if ((offset & blkmask) || (len & blkmask))
235 return false;
236 return true;
237}
238
Fred Isaman155e7522011-07-30 20:52:39 -0400239static enum pnfs_try_status
240bl_read_pagelist(struct nfs_read_data *rdata)
241{
Fred Isamancd841602012-04-20 14:47:44 -0400242 struct nfs_pgio_header *header = rdata->header;
Fred Isaman9549ec02011-07-30 20:52:53 -0400243 int i, hole;
244 struct bio *bio = NULL;
245 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
246 sector_t isect, extent_length = 0;
247 struct parallel_io *par;
248 loff_t f_offset = rdata->args.offset;
Fred Isaman9549ec02011-07-30 20:52:53 -0400249 struct page **pages = rdata->args.pages;
250 int pg_index = rdata->args.pgbase >> PAGE_CACHE_SHIFT;
251
Trond Myklebust6f008662012-03-20 14:12:46 -0400252 dprintk("%s enter nr_pages %u offset %lld count %u\n", __func__,
Fred Isaman30dd3742012-04-20 14:47:45 -0400253 rdata->pages.npages, f_offset, (unsigned int)rdata->args.count);
Fred Isaman9549ec02011-07-30 20:52:53 -0400254
Peng Tao159e0562012-05-29 13:57:58 +0800255 if (!bl_check_alignment(f_offset, rdata->args.count, PAGE_CACHE_MASK))
256 goto use_mds;
257
Fred Isaman9549ec02011-07-30 20:52:53 -0400258 par = alloc_parallel(rdata);
259 if (!par)
260 goto use_mds;
Fred Isaman9549ec02011-07-30 20:52:53 -0400261 par->pnfs_callback = bl_end_par_io_read;
262 /* At this point, we can no longer jump to use_mds */
263
264 isect = (sector_t) (f_offset >> SECTOR_SHIFT);
265 /* Code assumes extents are page-aligned */
Fred Isaman30dd3742012-04-20 14:47:45 -0400266 for (i = pg_index; i < rdata->pages.npages; i++) {
Fred Isaman9549ec02011-07-30 20:52:53 -0400267 if (!extent_length) {
268 /* We've used up the previous extent */
269 bl_put_extent(be);
270 bl_put_extent(cow_read);
271 bio = bl_submit_bio(READ, bio);
272 /* Get the next one */
Fred Isamancd841602012-04-20 14:47:44 -0400273 be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg),
Fred Isaman9549ec02011-07-30 20:52:53 -0400274 isect, &cow_read);
275 if (!be) {
Fred Isamancd841602012-04-20 14:47:44 -0400276 header->pnfs_error = -EIO;
Fred Isaman9549ec02011-07-30 20:52:53 -0400277 goto out;
278 }
279 extent_length = be->be_length -
280 (isect - be->be_f_offset);
281 if (cow_read) {
282 sector_t cow_length = cow_read->be_length -
283 (isect - cow_read->be_f_offset);
284 extent_length = min(extent_length, cow_length);
285 }
286 }
287 hole = is_hole(be, isect);
288 if (hole && !cow_read) {
289 bio = bl_submit_bio(READ, bio);
290 /* Fill hole w/ zeroes w/o accessing device */
291 dprintk("%s Zeroing page for hole\n", __func__);
292 zero_user_segment(pages[i], 0, PAGE_CACHE_SIZE);
293 print_page(pages[i]);
294 SetPageUptodate(pages[i]);
295 } else {
296 struct pnfs_block_extent *be_read;
297
298 be_read = (hole && cow_read) ? cow_read : be;
Fred Isaman30dd3742012-04-20 14:47:45 -0400299 bio = bl_add_page_to_bio(bio, rdata->pages.npages - i,
300 READ,
Fred Isaman9549ec02011-07-30 20:52:53 -0400301 isect, pages[i], be_read,
302 bl_end_io_read, par);
303 if (IS_ERR(bio)) {
Fred Isamancd841602012-04-20 14:47:44 -0400304 header->pnfs_error = PTR_ERR(bio);
Peng Taoe6d05a72011-09-22 21:50:16 -0400305 bio = NULL;
Fred Isaman9549ec02011-07-30 20:52:53 -0400306 goto out;
307 }
308 }
309 isect += PAGE_CACHE_SECTORS;
310 extent_length -= PAGE_CACHE_SECTORS;
311 }
Fred Isamancd841602012-04-20 14:47:44 -0400312 if ((isect << SECTOR_SHIFT) >= header->inode->i_size) {
Fred Isaman9549ec02011-07-30 20:52:53 -0400313 rdata->res.eof = 1;
Fred Isamancd841602012-04-20 14:47:44 -0400314 rdata->res.count = header->inode->i_size - f_offset;
Fred Isaman9549ec02011-07-30 20:52:53 -0400315 } else {
316 rdata->res.count = (isect << SECTOR_SHIFT) - f_offset;
317 }
318out:
319 bl_put_extent(be);
320 bl_put_extent(cow_read);
321 bl_submit_bio(READ, bio);
322 put_parallel(par);
323 return PNFS_ATTEMPTED;
324
325 use_mds:
326 dprintk("Giving up and using normal NFS\n");
Fred Isaman155e7522011-07-30 20:52:39 -0400327 return PNFS_NOT_ATTEMPTED;
328}
329
Fred Isaman31e63062011-07-30 20:52:55 -0400330static void mark_extents_written(struct pnfs_block_layout *bl,
331 __u64 offset, __u32 count)
332{
333 sector_t isect, end;
334 struct pnfs_block_extent *be;
Peng Tao7c5465d2012-01-12 23:18:46 +0800335 struct pnfs_block_short_extent *se;
Fred Isaman31e63062011-07-30 20:52:55 -0400336
337 dprintk("%s(%llu, %u)\n", __func__, offset, count);
338 if (count == 0)
339 return;
340 isect = (offset & (long)(PAGE_CACHE_MASK)) >> SECTOR_SHIFT;
341 end = (offset + count + PAGE_CACHE_SIZE - 1) & (long)(PAGE_CACHE_MASK);
342 end >>= SECTOR_SHIFT;
343 while (isect < end) {
344 sector_t len;
345 be = bl_find_get_extent(bl, isect, NULL);
346 BUG_ON(!be); /* FIXME */
347 len = min(end, be->be_f_offset + be->be_length) - isect;
Peng Tao7c5465d2012-01-12 23:18:46 +0800348 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
349 se = bl_pop_one_short_extent(be->be_inval);
350 BUG_ON(!se);
351 bl_mark_for_commit(be, isect, len, se);
352 }
Fred Isaman31e63062011-07-30 20:52:55 -0400353 isect += len;
354 bl_put_extent(be);
355 }
356}
357
Peng Tao71cdd402011-07-30 20:52:56 -0400358static void bl_end_io_write_zero(struct bio *bio, int err)
359{
360 struct parallel_io *par = bio->bi_private;
361 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
362 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Peng Tao71cdd402011-07-30 20:52:56 -0400363
364 do {
365 struct page *page = bvec->bv_page;
366
367 if (--bvec >= bio->bi_io_vec)
368 prefetchw(&bvec->bv_page->flags);
369 /* This is the zeroing page we added */
370 end_page_writeback(page);
371 page_cache_release(page);
372 } while (bvec >= bio->bi_io_vec);
Peng Tao7c5465d2012-01-12 23:18:46 +0800373
374 if (unlikely(!uptodate)) {
Fred Isamancd841602012-04-20 14:47:44 -0400375 struct nfs_write_data *data = par->data;
376 struct nfs_pgio_header *header = data->header;
377
378 if (!header->pnfs_error)
379 header->pnfs_error = -EIO;
380 pnfs_set_lo_fail(header->lseg);
Peng Tao71cdd402011-07-30 20:52:56 -0400381 }
382 bio_put(bio);
383 put_parallel(par);
384}
385
Fred Isaman650e2d32011-07-30 20:52:54 -0400386static void bl_end_io_write(struct bio *bio, int err)
Fred Isaman155e7522011-07-30 20:52:39 -0400387{
Fred Isaman650e2d32011-07-30 20:52:54 -0400388 struct parallel_io *par = bio->bi_private;
389 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Fred Isamancd841602012-04-20 14:47:44 -0400390 struct nfs_write_data *data = par->data;
391 struct nfs_pgio_header *header = data->header;
Fred Isaman650e2d32011-07-30 20:52:54 -0400392
393 if (!uptodate) {
Fred Isamancd841602012-04-20 14:47:44 -0400394 if (!header->pnfs_error)
395 header->pnfs_error = -EIO;
396 pnfs_set_lo_fail(header->lseg);
Fred Isaman650e2d32011-07-30 20:52:54 -0400397 }
398 bio_put(bio);
399 put_parallel(par);
400}
401
402/* Function scheduled for call during bl_end_par_io_write,
403 * it marks sectors as written and extends the commitlist.
404 */
405static void bl_write_cleanup(struct work_struct *work)
406{
407 struct rpc_task *task;
408 struct nfs_write_data *wdata;
409 dprintk("%s enter\n", __func__);
410 task = container_of(work, struct rpc_task, u.tk_work);
411 wdata = container_of(task, struct nfs_write_data, task);
Fred Isamancd841602012-04-20 14:47:44 -0400412 if (likely(!wdata->header->pnfs_error)) {
Fred Isaman31e63062011-07-30 20:52:55 -0400413 /* Marks for LAYOUTCOMMIT */
Fred Isamancd841602012-04-20 14:47:44 -0400414 mark_extents_written(BLK_LSEG2EXT(wdata->header->lseg),
Fred Isaman31e63062011-07-30 20:52:55 -0400415 wdata->args.offset, wdata->args.count);
416 }
Fred Isaman650e2d32011-07-30 20:52:54 -0400417 pnfs_ld_write_done(wdata);
418}
419
420/* Called when last of bios associated with a bl_write_pagelist call finishes */
Peng Tao7c5465d2012-01-12 23:18:46 +0800421static void bl_end_par_io_write(void *data, int num_se)
Fred Isaman650e2d32011-07-30 20:52:54 -0400422{
423 struct nfs_write_data *wdata = data;
424
Fred Isamancd841602012-04-20 14:47:44 -0400425 if (unlikely(wdata->header->pnfs_error)) {
426 bl_free_short_extents(&BLK_LSEG2EXT(wdata->header->lseg)->bl_inval,
Peng Tao7c5465d2012-01-12 23:18:46 +0800427 num_se);
428 }
429
Fred Isamancd841602012-04-20 14:47:44 -0400430 wdata->task.tk_status = wdata->header->pnfs_error;
Fred Isaman650e2d32011-07-30 20:52:54 -0400431 wdata->verf.committed = NFS_FILE_SYNC;
432 INIT_WORK(&wdata->task.u.tk_work, bl_write_cleanup);
433 schedule_work(&wdata->task.u.tk_work);
434}
435
Peng Tao71cdd402011-07-30 20:52:56 -0400436/* FIXME STUB - mark intersection of layout and page as bad, so is not
437 * used again.
438 */
439static void mark_bad_read(void)
440{
441 return;
442}
443
444/*
445 * map_block: map a requested I/0 block (isect) into an offset in the LVM
446 * block_device
447 */
448static void
449map_block(struct buffer_head *bh, sector_t isect, struct pnfs_block_extent *be)
450{
451 dprintk("%s enter be=%p\n", __func__, be);
452
453 set_buffer_mapped(bh);
454 bh->b_bdev = be->be_mdev;
455 bh->b_blocknr = (isect - be->be_f_offset + be->be_v_offset) >>
456 (be->be_mdev->bd_inode->i_blkbits - SECTOR_SHIFT);
457
458 dprintk("%s isect %llu, bh->b_blocknr %ld, using bsize %Zd\n",
459 __func__, (unsigned long long)isect, (long)bh->b_blocknr,
460 bh->b_size);
461 return;
462}
463
464/* Given an unmapped page, zero it or read in page for COW, page is locked
465 * by caller.
466 */
467static int
468init_page_for_write(struct page *page, struct pnfs_block_extent *cow_read)
469{
470 struct buffer_head *bh = NULL;
471 int ret = 0;
472 sector_t isect;
473
474 dprintk("%s enter, %p\n", __func__, page);
475 BUG_ON(PageUptodate(page));
476 if (!cow_read) {
477 zero_user_segment(page, 0, PAGE_SIZE);
478 SetPageUptodate(page);
479 goto cleanup;
480 }
481
482 bh = alloc_page_buffers(page, PAGE_CACHE_SIZE, 0);
483 if (!bh) {
484 ret = -ENOMEM;
485 goto cleanup;
486 }
487
488 isect = (sector_t) page->index << PAGE_CACHE_SECTOR_SHIFT;
489 map_block(bh, isect, cow_read);
490 if (!bh_uptodate_or_lock(bh))
491 ret = bh_submit_read(bh);
492 if (ret)
493 goto cleanup;
494 SetPageUptodate(page);
495
496cleanup:
497 bl_put_extent(cow_read);
498 if (bh)
499 free_buffer_head(bh);
500 if (ret) {
501 /* Need to mark layout with bad read...should now
502 * just use nfs4 for reads and writes.
503 */
504 mark_bad_read();
505 }
506 return ret;
507}
508
Peng Tao72c50882012-01-12 23:18:42 +0800509/* Find or create a zeroing page marked being writeback.
510 * Return ERR_PTR on error, NULL to indicate skip this page and page itself
511 * to indicate write out.
512 */
513static struct page *
514bl_find_get_zeroing_page(struct inode *inode, pgoff_t index,
515 struct pnfs_block_extent *cow_read)
516{
517 struct page *page;
518 int locked = 0;
519 page = find_get_page(inode->i_mapping, index);
520 if (page)
521 goto check_page;
522
523 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
524 if (unlikely(!page)) {
525 dprintk("%s oom\n", __func__);
526 return ERR_PTR(-ENOMEM);
527 }
528 locked = 1;
529
530check_page:
531 /* PageDirty: Other will write this out
532 * PageWriteback: Other is writing this out
533 * PageUptodate: It was read before
534 */
535 if (PageDirty(page) || PageWriteback(page)) {
536 print_page(page);
537 if (locked)
538 unlock_page(page);
539 page_cache_release(page);
540 return NULL;
541 }
542
543 if (!locked) {
544 lock_page(page);
545 locked = 1;
546 goto check_page;
547 }
548 if (!PageUptodate(page)) {
549 /* New page, readin or zero it */
550 init_page_for_write(page, cow_read);
551 }
552 set_page_writeback(page);
553 unlock_page(page);
554
555 return page;
556}
557
Fred Isaman650e2d32011-07-30 20:52:54 -0400558static enum pnfs_try_status
559bl_write_pagelist(struct nfs_write_data *wdata, int sync)
560{
Fred Isamancd841602012-04-20 14:47:44 -0400561 struct nfs_pgio_header *header = wdata->header;
Peng Tao71cdd402011-07-30 20:52:56 -0400562 int i, ret, npg_zero, pg_index, last = 0;
Fred Isaman650e2d32011-07-30 20:52:54 -0400563 struct bio *bio = NULL;
Peng Tao71cdd402011-07-30 20:52:56 -0400564 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
565 sector_t isect, last_isect = 0, extent_length = 0;
Peng Tao159e0562012-05-29 13:57:58 +0800566 struct parallel_io *par = NULL;
Fred Isaman650e2d32011-07-30 20:52:54 -0400567 loff_t offset = wdata->args.offset;
568 size_t count = wdata->args.count;
569 struct page **pages = wdata->args.pages;
Peng Tao71cdd402011-07-30 20:52:56 -0400570 struct page *page;
571 pgoff_t index;
572 u64 temp;
573 int npg_per_block =
Fred Isamancd841602012-04-20 14:47:44 -0400574 NFS_SERVER(header->inode)->pnfs_blksize >> PAGE_CACHE_SHIFT;
Fred Isaman650e2d32011-07-30 20:52:54 -0400575
576 dprintk("%s enter, %Zu@%lld\n", __func__, count, offset);
Peng Tao159e0562012-05-29 13:57:58 +0800577 /* Check for alignment first */
578 if (!bl_check_alignment(offset, count, PAGE_CACHE_MASK))
579 goto out_mds;
580
Fred Isaman650e2d32011-07-30 20:52:54 -0400581 /* At this point, wdata->pages is a (sequential) list of nfs_pages.
Peng Tao71cdd402011-07-30 20:52:56 -0400582 * We want to write each, and if there is an error set pnfs_error
583 * to have it redone using nfs.
Fred Isaman650e2d32011-07-30 20:52:54 -0400584 */
585 par = alloc_parallel(wdata);
586 if (!par)
Peng Tao7c5465d2012-01-12 23:18:46 +0800587 goto out_mds;
Fred Isaman650e2d32011-07-30 20:52:54 -0400588 par->pnfs_callback = bl_end_par_io_write;
589 /* At this point, have to be more careful with error handling */
590
591 isect = (sector_t) ((offset & (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
Fred Isamancd841602012-04-20 14:47:44 -0400592 be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg), isect, &cow_read);
Peng Tao71cdd402011-07-30 20:52:56 -0400593 if (!be || !is_writable(be, isect)) {
594 dprintk("%s no matching extents!\n", __func__);
Peng Tao7c5465d2012-01-12 23:18:46 +0800595 goto out_mds;
Peng Tao71cdd402011-07-30 20:52:56 -0400596 }
597
598 /* First page inside INVALID extent */
599 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
Peng Tao7c5465d2012-01-12 23:18:46 +0800600 if (likely(!bl_push_one_short_extent(be->be_inval)))
601 par->bse_count++;
602 else
603 goto out_mds;
Peng Tao71cdd402011-07-30 20:52:56 -0400604 temp = offset >> PAGE_CACHE_SHIFT;
605 npg_zero = do_div(temp, npg_per_block);
606 isect = (sector_t) (((offset - npg_zero * PAGE_CACHE_SIZE) &
607 (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
608 extent_length = be->be_length - (isect - be->be_f_offset);
609
610fill_invalid_ext:
611 dprintk("%s need to zero %d pages\n", __func__, npg_zero);
612 for (;npg_zero > 0; npg_zero--) {
Peng Tao75422742011-09-22 21:50:17 -0400613 if (bl_is_sector_init(be->be_inval, isect)) {
614 dprintk("isect %llu already init\n",
615 (unsigned long long)isect);
616 goto next_page;
617 }
Peng Tao71cdd402011-07-30 20:52:56 -0400618 /* page ref released in bl_end_io_write_zero */
619 index = isect >> PAGE_CACHE_SECTOR_SHIFT;
620 dprintk("%s zero %dth page: index %lu isect %llu\n",
621 __func__, npg_zero, index,
622 (unsigned long long)isect);
Fred Isamancd841602012-04-20 14:47:44 -0400623 page = bl_find_get_zeroing_page(header->inode, index,
Peng Tao72c50882012-01-12 23:18:42 +0800624 cow_read);
625 if (unlikely(IS_ERR(page))) {
Fred Isamancd841602012-04-20 14:47:44 -0400626 header->pnfs_error = PTR_ERR(page);
Peng Tao71cdd402011-07-30 20:52:56 -0400627 goto out;
Peng Tao72c50882012-01-12 23:18:42 +0800628 } else if (page == NULL)
Peng Tao71cdd402011-07-30 20:52:56 -0400629 goto next_page;
Peng Tao71cdd402011-07-30 20:52:56 -0400630
631 ret = bl_mark_sectors_init(be->be_inval, isect,
Peng Tao60c52e32012-01-12 23:18:40 +0800632 PAGE_CACHE_SECTORS);
Peng Tao71cdd402011-07-30 20:52:56 -0400633 if (unlikely(ret)) {
634 dprintk("%s bl_mark_sectors_init fail %d\n",
635 __func__, ret);
636 end_page_writeback(page);
637 page_cache_release(page);
Fred Isamancd841602012-04-20 14:47:44 -0400638 header->pnfs_error = ret;
Peng Tao71cdd402011-07-30 20:52:56 -0400639 goto out;
640 }
Peng Tao7c5465d2012-01-12 23:18:46 +0800641 if (likely(!bl_push_one_short_extent(be->be_inval)))
642 par->bse_count++;
643 else {
644 end_page_writeback(page);
645 page_cache_release(page);
Fred Isamancd841602012-04-20 14:47:44 -0400646 header->pnfs_error = -ENOMEM;
Peng Tao7c5465d2012-01-12 23:18:46 +0800647 goto out;
648 }
649 /* FIXME: This should be done in bi_end_io */
Fred Isamancd841602012-04-20 14:47:44 -0400650 mark_extents_written(BLK_LSEG2EXT(header->lseg),
Peng Tao7c5465d2012-01-12 23:18:46 +0800651 page->index << PAGE_CACHE_SHIFT,
652 PAGE_CACHE_SIZE);
653
Peng Tao71cdd402011-07-30 20:52:56 -0400654 bio = bl_add_page_to_bio(bio, npg_zero, WRITE,
655 isect, page, be,
656 bl_end_io_write_zero, par);
657 if (IS_ERR(bio)) {
Fred Isamancd841602012-04-20 14:47:44 -0400658 header->pnfs_error = PTR_ERR(bio);
Peng Taoe6d05a72011-09-22 21:50:16 -0400659 bio = NULL;
Peng Tao71cdd402011-07-30 20:52:56 -0400660 goto out;
661 }
Peng Tao71cdd402011-07-30 20:52:56 -0400662next_page:
663 isect += PAGE_CACHE_SECTORS;
664 extent_length -= PAGE_CACHE_SECTORS;
665 }
666 if (last)
667 goto write_done;
668 }
669 bio = bl_submit_bio(WRITE, bio);
670
671 /* Middle pages */
672 pg_index = wdata->args.pgbase >> PAGE_CACHE_SHIFT;
Fred Isaman30dd3742012-04-20 14:47:45 -0400673 for (i = pg_index; i < wdata->pages.npages; i++) {
Fred Isaman650e2d32011-07-30 20:52:54 -0400674 if (!extent_length) {
675 /* We've used up the previous extent */
676 bl_put_extent(be);
677 bio = bl_submit_bio(WRITE, bio);
678 /* Get the next one */
Fred Isamancd841602012-04-20 14:47:44 -0400679 be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg),
Fred Isaman650e2d32011-07-30 20:52:54 -0400680 isect, NULL);
681 if (!be || !is_writable(be, isect)) {
Fred Isamancd841602012-04-20 14:47:44 -0400682 header->pnfs_error = -EINVAL;
Fred Isaman650e2d32011-07-30 20:52:54 -0400683 goto out;
684 }
Peng Tao7c5465d2012-01-12 23:18:46 +0800685 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
686 if (likely(!bl_push_one_short_extent(
687 be->be_inval)))
688 par->bse_count++;
689 else {
Fred Isamancd841602012-04-20 14:47:44 -0400690 header->pnfs_error = -ENOMEM;
Peng Tao7c5465d2012-01-12 23:18:46 +0800691 goto out;
692 }
693 }
Fred Isaman650e2d32011-07-30 20:52:54 -0400694 extent_length = be->be_length -
Peng Tao71cdd402011-07-30 20:52:56 -0400695 (isect - be->be_f_offset);
Fred Isaman650e2d32011-07-30 20:52:54 -0400696 }
Peng Tao71cdd402011-07-30 20:52:56 -0400697 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
698 ret = bl_mark_sectors_init(be->be_inval, isect,
Peng Tao60c52e32012-01-12 23:18:40 +0800699 PAGE_CACHE_SECTORS);
Peng Tao71cdd402011-07-30 20:52:56 -0400700 if (unlikely(ret)) {
701 dprintk("%s bl_mark_sectors_init fail %d\n",
702 __func__, ret);
Fred Isamancd841602012-04-20 14:47:44 -0400703 header->pnfs_error = ret;
Peng Tao71cdd402011-07-30 20:52:56 -0400704 goto out;
Fred Isaman650e2d32011-07-30 20:52:54 -0400705 }
Peng Tao71cdd402011-07-30 20:52:56 -0400706 }
Fred Isaman30dd3742012-04-20 14:47:45 -0400707 bio = bl_add_page_to_bio(bio, wdata->pages.npages - i, WRITE,
Peng Tao71cdd402011-07-30 20:52:56 -0400708 isect, pages[i], be,
709 bl_end_io_write, par);
710 if (IS_ERR(bio)) {
Fred Isamancd841602012-04-20 14:47:44 -0400711 header->pnfs_error = PTR_ERR(bio);
Peng Taoe6d05a72011-09-22 21:50:16 -0400712 bio = NULL;
Peng Tao71cdd402011-07-30 20:52:56 -0400713 goto out;
Fred Isaman650e2d32011-07-30 20:52:54 -0400714 }
715 isect += PAGE_CACHE_SECTORS;
Peng Tao71cdd402011-07-30 20:52:56 -0400716 last_isect = isect;
Fred Isaman650e2d32011-07-30 20:52:54 -0400717 extent_length -= PAGE_CACHE_SECTORS;
718 }
Peng Tao71cdd402011-07-30 20:52:56 -0400719
720 /* Last page inside INVALID extent */
721 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
722 bio = bl_submit_bio(WRITE, bio);
723 temp = last_isect >> PAGE_CACHE_SECTOR_SHIFT;
724 npg_zero = npg_per_block - do_div(temp, npg_per_block);
725 if (npg_zero < npg_per_block) {
726 last = 1;
727 goto fill_invalid_ext;
728 }
729 }
730
731write_done:
732 wdata->res.count = (last_isect << SECTOR_SHIFT) - (offset);
733 if (count < wdata->res.count) {
Fred Isaman650e2d32011-07-30 20:52:54 -0400734 wdata->res.count = count;
Peng Tao71cdd402011-07-30 20:52:56 -0400735 }
Fred Isaman650e2d32011-07-30 20:52:54 -0400736out:
737 bl_put_extent(be);
738 bl_submit_bio(WRITE, bio);
739 put_parallel(par);
740 return PNFS_ATTEMPTED;
Peng Tao7c5465d2012-01-12 23:18:46 +0800741out_mds:
742 bl_put_extent(be);
743 kfree(par);
744 return PNFS_NOT_ATTEMPTED;
Fred Isaman155e7522011-07-30 20:52:39 -0400745}
746
Fred Isaman9e692962011-07-30 20:52:41 -0400747/* FIXME - range ignored */
Fred Isaman155e7522011-07-30 20:52:39 -0400748static void
Fred Isaman9e692962011-07-30 20:52:41 -0400749release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range)
Fred Isaman155e7522011-07-30 20:52:39 -0400750{
Fred Isaman9e692962011-07-30 20:52:41 -0400751 int i;
752 struct pnfs_block_extent *be;
753
754 spin_lock(&bl->bl_ext_lock);
755 for (i = 0; i < EXTENT_LISTS; i++) {
756 while (!list_empty(&bl->bl_extents[i])) {
757 be = list_first_entry(&bl->bl_extents[i],
758 struct pnfs_block_extent,
759 be_node);
760 list_del(&be->be_node);
761 bl_put_extent(be);
762 }
763 }
764 spin_unlock(&bl->bl_ext_lock);
Fred Isaman155e7522011-07-30 20:52:39 -0400765}
766
Fred Isaman155e7522011-07-30 20:52:39 -0400767static void
768release_inval_marks(struct pnfs_inval_markings *marks)
769{
Fred Isamanc1c2a4c2011-07-30 20:52:49 -0400770 struct pnfs_inval_tracking *pos, *temp;
Peng Tao7c5465d2012-01-12 23:18:46 +0800771 struct pnfs_block_short_extent *se, *stemp;
Fred Isamanc1c2a4c2011-07-30 20:52:49 -0400772
773 list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) {
774 list_del(&pos->it_link);
775 kfree(pos);
776 }
Peng Tao7c5465d2012-01-12 23:18:46 +0800777
778 list_for_each_entry_safe(se, stemp, &marks->im_extents, bse_node) {
779 list_del(&se->bse_node);
780 kfree(se);
781 }
Fred Isaman155e7522011-07-30 20:52:39 -0400782 return;
783}
784
785static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
786{
787 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
788
789 dprintk("%s enter\n", __func__);
790 release_extents(bl, NULL);
791 release_inval_marks(&bl->bl_inval);
792 kfree(bl);
793}
794
795static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode,
796 gfp_t gfp_flags)
797{
798 struct pnfs_block_layout *bl;
799
800 dprintk("%s enter\n", __func__);
801 bl = kzalloc(sizeof(*bl), gfp_flags);
802 if (!bl)
803 return NULL;
804 spin_lock_init(&bl->bl_ext_lock);
805 INIT_LIST_HEAD(&bl->bl_extents[0]);
806 INIT_LIST_HEAD(&bl->bl_extents[1]);
807 INIT_LIST_HEAD(&bl->bl_commit);
808 INIT_LIST_HEAD(&bl->bl_committing);
809 bl->bl_count = 0;
810 bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT;
811 BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize);
812 return &bl->bl_layout;
813}
814
Fred Isamana60d2eb2011-07-30 20:52:44 -0400815static void bl_free_lseg(struct pnfs_layout_segment *lseg)
Fred Isaman155e7522011-07-30 20:52:39 -0400816{
Fred Isamana60d2eb2011-07-30 20:52:44 -0400817 dprintk("%s enter\n", __func__);
818 kfree(lseg);
Fred Isaman155e7522011-07-30 20:52:39 -0400819}
820
Fred Isamana60d2eb2011-07-30 20:52:44 -0400821/* We pretty much ignore lseg, and store all data layout wide, so we
822 * can correctly merge.
823 */
824static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo,
825 struct nfs4_layoutget_res *lgr,
826 gfp_t gfp_flags)
Fred Isaman155e7522011-07-30 20:52:39 -0400827{
Fred Isamana60d2eb2011-07-30 20:52:44 -0400828 struct pnfs_layout_segment *lseg;
829 int status;
830
831 dprintk("%s enter\n", __func__);
832 lseg = kzalloc(sizeof(*lseg), gfp_flags);
833 if (!lseg)
834 return ERR_PTR(-ENOMEM);
835 status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags);
836 if (status) {
837 /* We don't want to call the full-blown bl_free_lseg,
838 * since on error extents were not touched.
839 */
840 kfree(lseg);
841 return ERR_PTR(status);
842 }
843 return lseg;
Fred Isaman155e7522011-07-30 20:52:39 -0400844}
845
846static void
847bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr,
848 const struct nfs4_layoutcommit_args *arg)
849{
Fred Isaman90ace122011-07-30 20:52:51 -0400850 dprintk("%s enter\n", __func__);
851 encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg);
Fred Isaman155e7522011-07-30 20:52:39 -0400852}
853
854static void
855bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
856{
Fred Isamanb2be7812011-07-30 20:52:52 -0400857 struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout;
858
859 dprintk("%s enter\n", __func__);
860 clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status);
Fred Isaman155e7522011-07-30 20:52:39 -0400861}
862
Fred Isaman2f9fd182011-07-30 20:52:46 -0400863static void free_blk_mountid(struct block_mount_id *mid)
864{
865 if (mid) {
Peng Tao93a38442012-01-12 23:18:47 +0800866 struct pnfs_block_dev *dev, *tmp;
867
868 /* No need to take bm_lock as we are last user freeing bm_devlist */
869 list_for_each_entry_safe(dev, tmp, &mid->bm_devlist, bm_node) {
Fred Isaman2f9fd182011-07-30 20:52:46 -0400870 list_del(&dev->bm_node);
871 bl_free_block_dev(dev);
872 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400873 kfree(mid);
874 }
875}
876
877/* This is mostly copied from the filelayout's get_device_info function.
878 * It seems much of this should be at the generic pnfs level.
879 */
880static struct pnfs_block_dev *
881nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh,
882 struct nfs4_deviceid *d_id)
883{
884 struct pnfs_device *dev;
Jim Rees516f2e22011-09-22 21:50:08 -0400885 struct pnfs_block_dev *rv;
Fred Isaman2f9fd182011-07-30 20:52:46 -0400886 u32 max_resp_sz;
887 int max_pages;
888 struct page **pages = NULL;
889 int i, rc;
890
891 /*
892 * Use the session max response size as the basis for setting
893 * GETDEVICEINFO's maxcount
894 */
895 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
Jim Rees10bd2952012-04-09 22:33:39 -0400896 max_pages = nfs_page_array_len(0, max_resp_sz);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400897 dprintk("%s max_resp_sz %u max_pages %d\n",
898 __func__, max_resp_sz, max_pages);
899
900 dev = kmalloc(sizeof(*dev), GFP_NOFS);
901 if (!dev) {
902 dprintk("%s kmalloc failed\n", __func__);
Jim Rees516f2e22011-09-22 21:50:08 -0400903 return ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400904 }
905
906 pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS);
907 if (pages == NULL) {
908 kfree(dev);
Jim Rees516f2e22011-09-22 21:50:08 -0400909 return ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400910 }
911 for (i = 0; i < max_pages; i++) {
912 pages[i] = alloc_page(GFP_NOFS);
Jim Rees516f2e22011-09-22 21:50:08 -0400913 if (!pages[i]) {
914 rv = ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400915 goto out_free;
Jim Rees516f2e22011-09-22 21:50:08 -0400916 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400917 }
918
919 memcpy(&dev->dev_id, d_id, sizeof(*d_id));
920 dev->layout_type = LAYOUT_BLOCK_VOLUME;
921 dev->pages = pages;
922 dev->pgbase = 0;
923 dev->pglen = PAGE_SIZE * max_pages;
924 dev->mincount = 0;
925
926 dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data);
927 rc = nfs4_proc_getdeviceinfo(server, dev);
928 dprintk("%s getdevice info returns %d\n", __func__, rc);
Jim Rees516f2e22011-09-22 21:50:08 -0400929 if (rc) {
930 rv = ERR_PTR(rc);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400931 goto out_free;
Jim Rees516f2e22011-09-22 21:50:08 -0400932 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400933
934 rv = nfs4_blk_decode_device(server, dev);
935 out_free:
936 for (i = 0; i < max_pages; i++)
937 __free_page(pages[i]);
938 kfree(pages);
939 kfree(dev);
940 return rv;
941}
942
Fred Isaman155e7522011-07-30 20:52:39 -0400943static int
944bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
945{
Fred Isaman2f9fd182011-07-30 20:52:46 -0400946 struct block_mount_id *b_mt_id = NULL;
947 struct pnfs_devicelist *dlist = NULL;
948 struct pnfs_block_dev *bdev;
949 LIST_HEAD(block_disklist);
Jim Rees516f2e22011-09-22 21:50:08 -0400950 int status, i;
Fred Isaman2f9fd182011-07-30 20:52:46 -0400951
Fred Isaman155e7522011-07-30 20:52:39 -0400952 dprintk("%s enter\n", __func__);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400953
954 if (server->pnfs_blksize == 0) {
955 dprintk("%s Server did not return blksize\n", __func__);
956 return -EINVAL;
957 }
958 b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS);
959 if (!b_mt_id) {
960 status = -ENOMEM;
961 goto out_error;
962 }
963 /* Initialize nfs4 block layout mount id */
964 spin_lock_init(&b_mt_id->bm_lock);
965 INIT_LIST_HEAD(&b_mt_id->bm_devlist);
966
967 dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS);
968 if (!dlist) {
969 status = -ENOMEM;
970 goto out_error;
971 }
972 dlist->eof = 0;
973 while (!dlist->eof) {
974 status = nfs4_proc_getdevicelist(server, fh, dlist);
975 if (status)
976 goto out_error;
977 dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n",
978 __func__, dlist->num_devs, dlist->eof);
979 for (i = 0; i < dlist->num_devs; i++) {
980 bdev = nfs4_blk_get_deviceinfo(server, fh,
981 &dlist->dev_id[i]);
Jim Rees516f2e22011-09-22 21:50:08 -0400982 if (IS_ERR(bdev)) {
983 status = PTR_ERR(bdev);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400984 goto out_error;
985 }
986 spin_lock(&b_mt_id->bm_lock);
987 list_add(&bdev->bm_node, &b_mt_id->bm_devlist);
988 spin_unlock(&b_mt_id->bm_lock);
989 }
990 }
991 dprintk("%s SUCCESS\n", __func__);
992 server->pnfs_ld_data = b_mt_id;
993
994 out_return:
995 kfree(dlist);
996 return status;
997
998 out_error:
999 free_blk_mountid(b_mt_id);
1000 goto out_return;
Fred Isaman155e7522011-07-30 20:52:39 -04001001}
1002
1003static int
1004bl_clear_layoutdriver(struct nfs_server *server)
1005{
Fred Isaman2f9fd182011-07-30 20:52:46 -04001006 struct block_mount_id *b_mt_id = server->pnfs_ld_data;
1007
Fred Isaman155e7522011-07-30 20:52:39 -04001008 dprintk("%s enter\n", __func__);
Fred Isaman2f9fd182011-07-30 20:52:46 -04001009 free_blk_mountid(b_mt_id);
1010 dprintk("%s RETURNS\n", __func__);
Fred Isaman155e7522011-07-30 20:52:39 -04001011 return 0;
1012}
1013
Peng Tao159e0562012-05-29 13:57:58 +08001014static void
1015bl_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1016{
1017 if (!bl_check_alignment(req->wb_offset, req->wb_bytes, PAGE_CACHE_MASK))
1018 nfs_pageio_reset_read_mds(pgio);
1019 else
1020 pnfs_generic_pg_init_read(pgio, req);
1021}
1022
1023static void
1024bl_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1025{
1026 if (!bl_check_alignment(req->wb_offset, req->wb_bytes, PAGE_CACHE_MASK))
1027 nfs_pageio_reset_write_mds(pgio);
1028 else
1029 pnfs_generic_pg_init_write(pgio, req);
1030}
1031
Benny Halevye9643fe2011-07-30 20:52:40 -04001032static const struct nfs_pageio_ops bl_pg_read_ops = {
Peng Tao159e0562012-05-29 13:57:58 +08001033 .pg_init = bl_pg_init_read,
Benny Halevye9643fe2011-07-30 20:52:40 -04001034 .pg_test = pnfs_generic_pg_test,
1035 .pg_doio = pnfs_generic_pg_readpages,
1036};
1037
1038static const struct nfs_pageio_ops bl_pg_write_ops = {
Peng Tao159e0562012-05-29 13:57:58 +08001039 .pg_init = bl_pg_init_write,
Benny Halevye9643fe2011-07-30 20:52:40 -04001040 .pg_test = pnfs_generic_pg_test,
1041 .pg_doio = pnfs_generic_pg_writepages,
1042};
1043
Fred Isaman155e7522011-07-30 20:52:39 -04001044static struct pnfs_layoutdriver_type blocklayout_type = {
1045 .id = LAYOUT_BLOCK_VOLUME,
1046 .name = "LAYOUT_BLOCK_VOLUME",
1047 .read_pagelist = bl_read_pagelist,
1048 .write_pagelist = bl_write_pagelist,
1049 .alloc_layout_hdr = bl_alloc_layout_hdr,
1050 .free_layout_hdr = bl_free_layout_hdr,
1051 .alloc_lseg = bl_alloc_lseg,
1052 .free_lseg = bl_free_lseg,
1053 .encode_layoutcommit = bl_encode_layoutcommit,
1054 .cleanup_layoutcommit = bl_cleanup_layoutcommit,
1055 .set_layoutdriver = bl_set_layoutdriver,
1056 .clear_layoutdriver = bl_clear_layoutdriver,
Benny Halevye9643fe2011-07-30 20:52:40 -04001057 .pg_read_ops = &bl_pg_read_ops,
1058 .pg_write_ops = &bl_pg_write_ops,
Fred Isaman155e7522011-07-30 20:52:39 -04001059};
1060
Jim Reesfe0a9b72011-07-30 20:52:42 -04001061static const struct rpc_pipe_ops bl_upcall_ops = {
Peng Taoc1225152011-09-22 21:50:10 -04001062 .upcall = rpc_pipe_generic_upcall,
Jim Reesfe0a9b72011-07-30 20:52:42 -04001063 .downcall = bl_pipe_downcall,
1064 .destroy_msg = bl_pipe_destroy_msg,
1065};
1066
Stanislav Kinsbursky332dfab2012-01-10 17:04:16 +04001067static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb,
1068 struct rpc_pipe *pipe)
1069{
1070 struct dentry *dir, *dentry;
1071
1072 dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME);
1073 if (dir == NULL)
1074 return ERR_PTR(-ENOENT);
1075 dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe);
1076 dput(dir);
1077 return dentry;
1078}
1079
1080static void nfs4blocklayout_unregister_sb(struct super_block *sb,
1081 struct rpc_pipe *pipe)
1082{
1083 if (pipe->dentry)
1084 rpc_unlink(pipe->dentry);
1085}
1086
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001087static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
1088 void *ptr)
1089{
1090 struct super_block *sb = ptr;
1091 struct net *net = sb->s_fs_info;
1092 struct nfs_net *nn = net_generic(net, nfs_net_id);
1093 struct dentry *dentry;
1094 int ret = 0;
1095
1096 if (!try_module_get(THIS_MODULE))
1097 return 0;
1098
1099 if (nn->bl_device_pipe == NULL) {
1100 module_put(THIS_MODULE);
1101 return 0;
1102 }
1103
1104 switch (event) {
1105 case RPC_PIPEFS_MOUNT:
1106 dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe);
1107 if (IS_ERR(dentry)) {
1108 ret = PTR_ERR(dentry);
1109 break;
1110 }
1111 nn->bl_device_pipe->dentry = dentry;
1112 break;
1113 case RPC_PIPEFS_UMOUNT:
1114 if (nn->bl_device_pipe->dentry)
1115 nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe);
1116 break;
1117 default:
1118 ret = -ENOTSUPP;
1119 break;
1120 }
1121 module_put(THIS_MODULE);
1122 return ret;
1123}
1124
1125static struct notifier_block nfs4blocklayout_block = {
1126 .notifier_call = rpc_pipefs_event,
1127};
1128
Stanislav Kinsbursky332dfab2012-01-10 17:04:16 +04001129static struct dentry *nfs4blocklayout_register_net(struct net *net,
1130 struct rpc_pipe *pipe)
1131{
1132 struct super_block *pipefs_sb;
1133 struct dentry *dentry;
1134
1135 pipefs_sb = rpc_get_sb_net(net);
1136 if (!pipefs_sb)
Stanislav Kinsbursky2561d612012-01-10 17:04:40 +04001137 return NULL;
Stanislav Kinsbursky332dfab2012-01-10 17:04:16 +04001138 dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe);
1139 rpc_put_sb_net(net);
1140 return dentry;
1141}
1142
1143static void nfs4blocklayout_unregister_net(struct net *net,
1144 struct rpc_pipe *pipe)
1145{
1146 struct super_block *pipefs_sb;
1147
1148 pipefs_sb = rpc_get_sb_net(net);
1149 if (pipefs_sb) {
1150 nfs4blocklayout_unregister_sb(pipefs_sb, pipe);
1151 rpc_put_sb_net(net);
1152 }
1153}
1154
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04001155static int nfs4blocklayout_net_init(struct net *net)
1156{
1157 struct nfs_net *nn = net_generic(net, nfs_net_id);
1158 struct dentry *dentry;
1159
Stanislav Kinsbursky5ffaf852012-03-11 18:20:31 +04001160 init_waitqueue_head(&nn->bl_wq);
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04001161 nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0);
1162 if (IS_ERR(nn->bl_device_pipe))
1163 return PTR_ERR(nn->bl_device_pipe);
1164 dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe);
1165 if (IS_ERR(dentry)) {
1166 rpc_destroy_pipe_data(nn->bl_device_pipe);
1167 return PTR_ERR(dentry);
1168 }
1169 nn->bl_device_pipe->dentry = dentry;
1170 return 0;
1171}
1172
1173static void nfs4blocklayout_net_exit(struct net *net)
1174{
1175 struct nfs_net *nn = net_generic(net, nfs_net_id);
1176
1177 nfs4blocklayout_unregister_net(net, nn->bl_device_pipe);
1178 rpc_destroy_pipe_data(nn->bl_device_pipe);
1179 nn->bl_device_pipe = NULL;
1180}
1181
1182static struct pernet_operations nfs4blocklayout_net_ops = {
1183 .init = nfs4blocklayout_net_init,
1184 .exit = nfs4blocklayout_net_exit,
1185};
1186
Fred Isaman155e7522011-07-30 20:52:39 -04001187static int __init nfs4blocklayout_init(void)
1188{
1189 int ret;
1190
1191 dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
1192
1193 ret = pnfs_register_layoutdriver(&blocklayout_type);
Jim Reesfe0a9b72011-07-30 20:52:42 -04001194 if (ret)
1195 goto out;
1196
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001197 ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block);
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04001198 if (ret)
1199 goto out_remove;
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001200 ret = register_pernet_subsys(&nfs4blocklayout_net_ops);
1201 if (ret)
1202 goto out_notifier;
Jim Reesfe0a9b72011-07-30 20:52:42 -04001203out:
1204 return ret;
1205
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001206out_notifier:
1207 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
Jim Reesfe0a9b72011-07-30 20:52:42 -04001208out_remove:
1209 pnfs_unregister_layoutdriver(&blocklayout_type);
Fred Isaman155e7522011-07-30 20:52:39 -04001210 return ret;
1211}
1212
1213static void __exit nfs4blocklayout_exit(void)
1214{
1215 dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
1216 __func__);
1217
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001218 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04001219 unregister_pernet_subsys(&nfs4blocklayout_net_ops);
Fred Isaman155e7522011-07-30 20:52:39 -04001220 pnfs_unregister_layoutdriver(&blocklayout_type);
Fred Isaman155e7522011-07-30 20:52:39 -04001221}
1222
1223MODULE_ALIAS("nfs-layouttype4-3");
1224
1225module_init(nfs4blocklayout_init);
1226module_exit(nfs4blocklayout_exit);