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