]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/nfs/blocklayout/blocklayout.c
pnfs/blocklayout: rewrite extent tracking
[mirror_ubuntu-zesty-kernel.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
472static void
473bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr,
474 const struct nfs4_layoutcommit_args *arg)
475{
90ace12a 476 dprintk("%s enter\n", __func__);
8067253c 477 ext_tree_encode_commit(BLK_LO2EXT(lo), xdr);
155e7524
FI
478}
479
480static void
481bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
482{
b2be7811
FI
483 struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout;
484
485 dprintk("%s enter\n", __func__);
8067253c 486 ext_tree_mark_committed(BLK_LO2EXT(lo), lcdata->res.status);
155e7524
FI
487}
488
2f9fd182
FI
489static void free_blk_mountid(struct block_mount_id *mid)
490{
491 if (mid) {
93a3844e
PT
492 struct pnfs_block_dev *dev, *tmp;
493
494 /* No need to take bm_lock as we are last user freeing bm_devlist */
495 list_for_each_entry_safe(dev, tmp, &mid->bm_devlist, bm_node) {
2f9fd182
FI
496 list_del(&dev->bm_node);
497 bl_free_block_dev(dev);
498 }
2f9fd182
FI
499 kfree(mid);
500 }
501}
502
78e4e05c 503/* This is mostly copied from the filelayout_get_device_info function.
2f9fd182
FI
504 * It seems much of this should be at the generic pnfs level.
505 */
506static struct pnfs_block_dev *
507nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh,
508 struct nfs4_deviceid *d_id)
509{
510 struct pnfs_device *dev;
516f2e24 511 struct pnfs_block_dev *rv;
2f9fd182
FI
512 u32 max_resp_sz;
513 int max_pages;
514 struct page **pages = NULL;
515 int i, rc;
516
517 /*
518 * Use the session max response size as the basis for setting
519 * GETDEVICEINFO's maxcount
520 */
521 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
10bd295a 522 max_pages = nfs_page_array_len(0, max_resp_sz);
2f9fd182
FI
523 dprintk("%s max_resp_sz %u max_pages %d\n",
524 __func__, max_resp_sz, max_pages);
525
526 dev = kmalloc(sizeof(*dev), GFP_NOFS);
527 if (!dev) {
528 dprintk("%s kmalloc failed\n", __func__);
516f2e24 529 return ERR_PTR(-ENOMEM);
2f9fd182
FI
530 }
531
f15b5041 532 pages = kcalloc(max_pages, sizeof(struct page *), GFP_NOFS);
2f9fd182
FI
533 if (pages == NULL) {
534 kfree(dev);
516f2e24 535 return ERR_PTR(-ENOMEM);
2f9fd182
FI
536 }
537 for (i = 0; i < max_pages; i++) {
538 pages[i] = alloc_page(GFP_NOFS);
516f2e24
JR
539 if (!pages[i]) {
540 rv = ERR_PTR(-ENOMEM);
2f9fd182 541 goto out_free;
516f2e24 542 }
2f9fd182
FI
543 }
544
545 memcpy(&dev->dev_id, d_id, sizeof(*d_id));
546 dev->layout_type = LAYOUT_BLOCK_VOLUME;
547 dev->pages = pages;
548 dev->pgbase = 0;
549 dev->pglen = PAGE_SIZE * max_pages;
550 dev->mincount = 0;
968fe252 551 dev->maxcount = max_resp_sz - nfs41_maxgetdevinfo_overhead;
2f9fd182
FI
552
553 dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data);
cd5875fe 554 rc = nfs4_proc_getdeviceinfo(server, dev, NULL);
2f9fd182 555 dprintk("%s getdevice info returns %d\n", __func__, rc);
516f2e24
JR
556 if (rc) {
557 rv = ERR_PTR(rc);
2f9fd182 558 goto out_free;
516f2e24 559 }
2f9fd182
FI
560
561 rv = nfs4_blk_decode_device(server, dev);
562 out_free:
563 for (i = 0; i < max_pages; i++)
564 __free_page(pages[i]);
565 kfree(pages);
566 kfree(dev);
567 return rv;
568}
569
155e7524
FI
570static int
571bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
572{
2f9fd182
FI
573 struct block_mount_id *b_mt_id = NULL;
574 struct pnfs_devicelist *dlist = NULL;
575 struct pnfs_block_dev *bdev;
576 LIST_HEAD(block_disklist);
516f2e24 577 int status, i;
2f9fd182 578
155e7524 579 dprintk("%s enter\n", __func__);
2f9fd182
FI
580
581 if (server->pnfs_blksize == 0) {
582 dprintk("%s Server did not return blksize\n", __func__);
583 return -EINVAL;
584 }
e3aaf7f2
CH
585 if (server->pnfs_blksize > PAGE_SIZE) {
586 printk(KERN_ERR "%s: pNFS blksize %d not supported.\n",
587 __func__, server->pnfs_blksize);
588 return -EINVAL;
589 }
590
2f9fd182
FI
591 b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS);
592 if (!b_mt_id) {
593 status = -ENOMEM;
594 goto out_error;
595 }
596 /* Initialize nfs4 block layout mount id */
597 spin_lock_init(&b_mt_id->bm_lock);
598 INIT_LIST_HEAD(&b_mt_id->bm_devlist);
599
600 dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS);
601 if (!dlist) {
602 status = -ENOMEM;
603 goto out_error;
604 }
605 dlist->eof = 0;
606 while (!dlist->eof) {
607 status = nfs4_proc_getdevicelist(server, fh, dlist);
608 if (status)
609 goto out_error;
610 dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n",
611 __func__, dlist->num_devs, dlist->eof);
612 for (i = 0; i < dlist->num_devs; i++) {
613 bdev = nfs4_blk_get_deviceinfo(server, fh,
614 &dlist->dev_id[i]);
516f2e24
JR
615 if (IS_ERR(bdev)) {
616 status = PTR_ERR(bdev);
2f9fd182
FI
617 goto out_error;
618 }
619 spin_lock(&b_mt_id->bm_lock);
620 list_add(&bdev->bm_node, &b_mt_id->bm_devlist);
621 spin_unlock(&b_mt_id->bm_lock);
622 }
623 }
624 dprintk("%s SUCCESS\n", __func__);
625 server->pnfs_ld_data = b_mt_id;
626
627 out_return:
628 kfree(dlist);
629 return status;
630
631 out_error:
632 free_blk_mountid(b_mt_id);
633 goto out_return;
155e7524
FI
634}
635
636static int
637bl_clear_layoutdriver(struct nfs_server *server)
638{
2f9fd182
FI
639 struct block_mount_id *b_mt_id = server->pnfs_ld_data;
640
155e7524 641 dprintk("%s enter\n", __func__);
2f9fd182
FI
642 free_blk_mountid(b_mt_id);
643 dprintk("%s RETURNS\n", __func__);
155e7524
FI
644 return 0;
645}
646
f742dc4a 647static bool
3a6fd1f0
CH
648is_aligned_req(struct nfs_pageio_descriptor *pgio,
649 struct nfs_page *req, unsigned int alignment)
f742dc4a 650{
3a6fd1f0
CH
651 /*
652 * Always accept buffered writes, higher layers take care of the
653 * right alignment.
654 */
655 if (pgio->pg_dreq == NULL)
656 return true;
657
658 if (!IS_ALIGNED(req->wb_offset, alignment))
659 return false;
660
661 if (IS_ALIGNED(req->wb_bytes, alignment))
662 return true;
663
664 if (req_offset(req) + req->wb_bytes == i_size_read(pgio->pg_inode)) {
665 /*
666 * If the write goes up to the inode size, just write
667 * the full page. Data past the inode size is
668 * guaranteed to be zeroed by the higher level client
669 * code, and this behaviour is mandated by RFC 5663
670 * section 2.3.2.
671 */
672 return true;
673 }
674
675 return false;
f742dc4a
PT
676}
677
678static void
679bl_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
680{
3a6fd1f0 681 if (!is_aligned_req(pgio, req, SECTOR_SIZE)) {
f742dc4a 682 nfs_pageio_reset_read_mds(pgio);
3a6fd1f0
CH
683 return;
684 }
685
686 pnfs_generic_pg_init_read(pgio, req);
f742dc4a
PT
687}
688
b4fdac1a
WAA
689/*
690 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
691 * of bytes (maximum @req->wb_bytes) that can be coalesced.
692 */
693static size_t
f742dc4a
PT
694bl_pg_test_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
695 struct nfs_page *req)
696{
3a6fd1f0 697 if (!is_aligned_req(pgio, req, SECTOR_SIZE))
b4fdac1a 698 return 0;
f742dc4a
PT
699 return pnfs_generic_pg_test(pgio, prev, req);
700}
701
6296556f
PT
702/*
703 * Return the number of contiguous bytes for a given inode
704 * starting at page frame idx.
705 */
706static u64 pnfs_num_cont_bytes(struct inode *inode, pgoff_t idx)
707{
708 struct address_space *mapping = inode->i_mapping;
709 pgoff_t end;
710
711 /* Optimize common case that writes from 0 to end of file */
712 end = DIV_ROUND_UP(i_size_read(inode), PAGE_CACHE_SIZE);
713 if (end != NFS_I(inode)->npages) {
714 rcu_read_lock();
e7b563bb 715 end = page_cache_next_hole(mapping, idx + 1, ULONG_MAX);
6296556f
PT
716 rcu_read_unlock();
717 }
718
719 if (!end)
720 return i_size_read(inode) - (idx << PAGE_CACHE_SHIFT);
721 else
722 return (end - idx) << PAGE_CACHE_SHIFT;
723}
724
6f018efa 725static void
96c9eae6
PT
726bl_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
727{
3a6fd1f0
CH
728 u64 wb_size;
729
730 if (!is_aligned_req(pgio, req, PAGE_SIZE)) {
96c9eae6 731 nfs_pageio_reset_write_mds(pgio);
3a6fd1f0 732 return;
6296556f 733 }
3a6fd1f0
CH
734
735 if (pgio->pg_dreq == NULL)
736 wb_size = pnfs_num_cont_bytes(pgio->pg_inode,
737 req->wb_index);
738 else
739 wb_size = nfs_dreq_bytes_left(pgio->pg_dreq);
740
741 pnfs_generic_pg_init_write(pgio, req, wb_size);
96c9eae6
PT
742}
743
b4fdac1a
WAA
744/*
745 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
746 * of bytes (maximum @req->wb_bytes) that can be coalesced.
747 */
748static size_t
96c9eae6
PT
749bl_pg_test_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
750 struct nfs_page *req)
751{
3a6fd1f0 752 if (!is_aligned_req(pgio, req, PAGE_SIZE))
b4fdac1a 753 return 0;
96c9eae6
PT
754 return pnfs_generic_pg_test(pgio, prev, req);
755}
756
e9643fe8 757static const struct nfs_pageio_ops bl_pg_read_ops = {
f742dc4a
PT
758 .pg_init = bl_pg_init_read,
759 .pg_test = bl_pg_test_read,
e9643fe8
BH
760 .pg_doio = pnfs_generic_pg_readpages,
761};
762
763static const struct nfs_pageio_ops bl_pg_write_ops = {
96c9eae6
PT
764 .pg_init = bl_pg_init_write,
765 .pg_test = bl_pg_test_write,
e9643fe8
BH
766 .pg_doio = pnfs_generic_pg_writepages,
767};
768
155e7524
FI
769static struct pnfs_layoutdriver_type blocklayout_type = {
770 .id = LAYOUT_BLOCK_VOLUME,
771 .name = "LAYOUT_BLOCK_VOLUME",
5a12cca6 772 .owner = THIS_MODULE,
3a6fd1f0 773 .flags = PNFS_READ_WHOLE_PAGE,
155e7524
FI
774 .read_pagelist = bl_read_pagelist,
775 .write_pagelist = bl_write_pagelist,
776 .alloc_layout_hdr = bl_alloc_layout_hdr,
777 .free_layout_hdr = bl_free_layout_hdr,
778 .alloc_lseg = bl_alloc_lseg,
779 .free_lseg = bl_free_lseg,
780 .encode_layoutcommit = bl_encode_layoutcommit,
781 .cleanup_layoutcommit = bl_cleanup_layoutcommit,
782 .set_layoutdriver = bl_set_layoutdriver,
783 .clear_layoutdriver = bl_clear_layoutdriver,
e9643fe8
BH
784 .pg_read_ops = &bl_pg_read_ops,
785 .pg_write_ops = &bl_pg_write_ops,
155e7524
FI
786};
787
fe0a9b74 788static const struct rpc_pipe_ops bl_upcall_ops = {
c1225158 789 .upcall = rpc_pipe_generic_upcall,
fe0a9b74
JR
790 .downcall = bl_pipe_downcall,
791 .destroy_msg = bl_pipe_destroy_msg,
792};
793
332dfab6
SK
794static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb,
795 struct rpc_pipe *pipe)
796{
797 struct dentry *dir, *dentry;
798
799 dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME);
800 if (dir == NULL)
801 return ERR_PTR(-ENOENT);
802 dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe);
803 dput(dir);
804 return dentry;
805}
806
807static void nfs4blocklayout_unregister_sb(struct super_block *sb,
808 struct rpc_pipe *pipe)
809{
810 if (pipe->dentry)
811 rpc_unlink(pipe->dentry);
812}
813
627f3066
SK
814static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
815 void *ptr)
816{
817 struct super_block *sb = ptr;
818 struct net *net = sb->s_fs_info;
819 struct nfs_net *nn = net_generic(net, nfs_net_id);
820 struct dentry *dentry;
821 int ret = 0;
822
823 if (!try_module_get(THIS_MODULE))
824 return 0;
825
826 if (nn->bl_device_pipe == NULL) {
827 module_put(THIS_MODULE);
828 return 0;
829 }
830
831 switch (event) {
832 case RPC_PIPEFS_MOUNT:
833 dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe);
834 if (IS_ERR(dentry)) {
835 ret = PTR_ERR(dentry);
836 break;
837 }
838 nn->bl_device_pipe->dentry = dentry;
839 break;
840 case RPC_PIPEFS_UMOUNT:
841 if (nn->bl_device_pipe->dentry)
842 nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe);
843 break;
844 default:
845 ret = -ENOTSUPP;
846 break;
847 }
848 module_put(THIS_MODULE);
849 return ret;
850}
851
852static struct notifier_block nfs4blocklayout_block = {
853 .notifier_call = rpc_pipefs_event,
854};
855
332dfab6
SK
856static struct dentry *nfs4blocklayout_register_net(struct net *net,
857 struct rpc_pipe *pipe)
858{
859 struct super_block *pipefs_sb;
860 struct dentry *dentry;
861
862 pipefs_sb = rpc_get_sb_net(net);
863 if (!pipefs_sb)
2561d618 864 return NULL;
332dfab6
SK
865 dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe);
866 rpc_put_sb_net(net);
867 return dentry;
868}
869
870static void nfs4blocklayout_unregister_net(struct net *net,
871 struct rpc_pipe *pipe)
872{
873 struct super_block *pipefs_sb;
874
875 pipefs_sb = rpc_get_sb_net(net);
876 if (pipefs_sb) {
877 nfs4blocklayout_unregister_sb(pipefs_sb, pipe);
878 rpc_put_sb_net(net);
879 }
880}
881
9e2e74db
SK
882static int nfs4blocklayout_net_init(struct net *net)
883{
884 struct nfs_net *nn = net_generic(net, nfs_net_id);
885 struct dentry *dentry;
886
5ffaf855 887 init_waitqueue_head(&nn->bl_wq);
9e2e74db
SK
888 nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0);
889 if (IS_ERR(nn->bl_device_pipe))
890 return PTR_ERR(nn->bl_device_pipe);
891 dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe);
892 if (IS_ERR(dentry)) {
893 rpc_destroy_pipe_data(nn->bl_device_pipe);
894 return PTR_ERR(dentry);
895 }
896 nn->bl_device_pipe->dentry = dentry;
897 return 0;
898}
899
900static void nfs4blocklayout_net_exit(struct net *net)
901{
902 struct nfs_net *nn = net_generic(net, nfs_net_id);
903
904 nfs4blocklayout_unregister_net(net, nn->bl_device_pipe);
905 rpc_destroy_pipe_data(nn->bl_device_pipe);
906 nn->bl_device_pipe = NULL;
907}
908
909static struct pernet_operations nfs4blocklayout_net_ops = {
910 .init = nfs4blocklayout_net_init,
911 .exit = nfs4blocklayout_net_exit,
912};
913
155e7524
FI
914static int __init nfs4blocklayout_init(void)
915{
916 int ret;
917
918 dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
919
920 ret = pnfs_register_layoutdriver(&blocklayout_type);
fe0a9b74
JR
921 if (ret)
922 goto out;
923
627f3066 924 ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block);
9e2e74db
SK
925 if (ret)
926 goto out_remove;
627f3066
SK
927 ret = register_pernet_subsys(&nfs4blocklayout_net_ops);
928 if (ret)
929 goto out_notifier;
fe0a9b74
JR
930out:
931 return ret;
932
627f3066
SK
933out_notifier:
934 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
fe0a9b74
JR
935out_remove:
936 pnfs_unregister_layoutdriver(&blocklayout_type);
155e7524
FI
937 return ret;
938}
939
940static void __exit nfs4blocklayout_exit(void)
941{
942 dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
943 __func__);
944
627f3066 945 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
9e2e74db 946 unregister_pernet_subsys(&nfs4blocklayout_net_ops);
155e7524
FI
947 pnfs_unregister_layoutdriver(&blocklayout_type);
948}
949
950MODULE_ALIAS("nfs-layouttype4-3");
951
952module_init(nfs4blocklayout_init);
953module_exit(nfs4blocklayout_exit);