blob: 5aa23750a14911c62be3df27deb202e0054d95d5 [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 */
Heiko Carstens88c9e422011-08-02 09:57:35 +020038#include <linux/prefetch.h>
Peng Tao62965562012-09-25 14:55:57 +080039#include <linux/pagevec.h>
Fred Isaman155e7522011-07-30 20:52:39 -040040
Jim Rees10bd295a2012-04-09 22:33:39 -040041#include "../pnfs.h"
Trond Myklebust76e697b2012-11-26 14:20:49 -050042#include "../nfs4session.h"
Jim Rees10bd295a2012-04-09 22:33:39 -040043#include "../internal.h"
Fred Isaman155e7522011-07-30 20:52:39 -040044#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
Fred Isaman9549ec02011-07-30 20:52:53 -040052/* Given the be associated with isect, determine if page data needs to be
53 * initialized.
54 */
55static int is_hole(struct pnfs_block_extent *be, sector_t isect)
56{
57 if (be->be_state == PNFS_BLOCK_NONE_DATA)
58 return 1;
59 else if (be->be_state != PNFS_BLOCK_INVALID_DATA)
60 return 0;
61 else
62 return !bl_is_sector_init(be->be_inval, isect);
63}
64
Fred Isaman650e2d32011-07-30 20:52:54 -040065/* Given the be associated with isect, determine if page data can be
66 * written to disk.
67 */
68static int is_writable(struct pnfs_block_extent *be, sector_t isect)
69{
Peng Tao71cdd402011-07-30 20:52:56 -040070 return (be->be_state == PNFS_BLOCK_READWRITE_DATA ||
71 be->be_state == PNFS_BLOCK_INVALID_DATA);
Fred Isaman650e2d32011-07-30 20:52:54 -040072}
73
Fred Isaman9549ec02011-07-30 20:52:53 -040074/* The data we are handed might be spread across several bios. We need
75 * to track when the last one is finished.
76 */
77struct parallel_io {
78 struct kref refcnt;
Peng Tao7c5465d2012-01-12 23:18:46 +080079 void (*pnfs_callback) (void *data, int num_se);
Fred Isaman9549ec02011-07-30 20:52:53 -040080 void *data;
Peng Tao7c5465d2012-01-12 23:18:46 +080081 int bse_count;
Fred Isaman9549ec02011-07-30 20:52:53 -040082};
83
84static inline struct parallel_io *alloc_parallel(void *data)
85{
86 struct parallel_io *rv;
87
88 rv = kmalloc(sizeof(*rv), GFP_NOFS);
89 if (rv) {
90 rv->data = data;
91 kref_init(&rv->refcnt);
Peng Tao7c5465d2012-01-12 23:18:46 +080092 rv->bse_count = 0;
Fred Isaman9549ec02011-07-30 20:52:53 -040093 }
94 return rv;
95}
96
97static inline void get_parallel(struct parallel_io *p)
98{
99 kref_get(&p->refcnt);
100}
101
102static void destroy_parallel(struct kref *kref)
103{
104 struct parallel_io *p = container_of(kref, struct parallel_io, refcnt);
105
106 dprintk("%s enter\n", __func__);
Peng Tao7c5465d2012-01-12 23:18:46 +0800107 p->pnfs_callback(p->data, p->bse_count);
Fred Isaman9549ec02011-07-30 20:52:53 -0400108 kfree(p);
109}
110
111static inline void put_parallel(struct parallel_io *p)
112{
113 kref_put(&p->refcnt, destroy_parallel);
114}
115
116static struct bio *
117bl_submit_bio(int rw, struct bio *bio)
118{
119 if (bio) {
120 get_parallel(bio->bi_private);
121 dprintk("%s submitting %s bio %u@%llu\n", __func__,
Kent Overstreet4f024f32013-10-11 15:44:27 -0700122 rw == READ ? "read" : "write", bio->bi_iter.bi_size,
123 (unsigned long long)bio->bi_iter.bi_sector);
Fred Isaman9549ec02011-07-30 20:52:53 -0400124 submit_bio(rw, bio);
125 }
126 return NULL;
127}
128
129static struct bio *bl_alloc_init_bio(int npg, sector_t isect,
130 struct pnfs_block_extent *be,
131 void (*end_io)(struct bio *, int err),
132 struct parallel_io *par)
133{
134 struct bio *bio;
135
Peng Tao74a6eeb2012-01-12 23:18:48 +0800136 npg = min(npg, BIO_MAX_PAGES);
Fred Isaman9549ec02011-07-30 20:52:53 -0400137 bio = bio_alloc(GFP_NOIO, npg);
Peng Tao74a6eeb2012-01-12 23:18:48 +0800138 if (!bio && (current->flags & PF_MEMALLOC)) {
139 while (!bio && (npg /= 2))
140 bio = bio_alloc(GFP_NOIO, npg);
141 }
Fred Isaman9549ec02011-07-30 20:52:53 -0400142
Peng Tao74a6eeb2012-01-12 23:18:48 +0800143 if (bio) {
Kent Overstreet4f024f32013-10-11 15:44:27 -0700144 bio->bi_iter.bi_sector = isect - be->be_f_offset +
145 be->be_v_offset;
Peng Tao74a6eeb2012-01-12 23:18:48 +0800146 bio->bi_bdev = be->be_mdev;
147 bio->bi_end_io = end_io;
148 bio->bi_private = par;
149 }
Fred Isaman9549ec02011-07-30 20:52:53 -0400150 return bio;
151}
152
Peng Taofe6e1e82012-08-24 00:27:51 +0800153static struct bio *do_add_page_to_bio(struct bio *bio, int npg, int rw,
Fred Isaman9549ec02011-07-30 20:52:53 -0400154 sector_t isect, struct page *page,
155 struct pnfs_block_extent *be,
156 void (*end_io)(struct bio *, int err),
Peng Taofe6e1e82012-08-24 00:27:51 +0800157 struct parallel_io *par,
158 unsigned int offset, int len)
Fred Isaman9549ec02011-07-30 20:52:53 -0400159{
Peng Taofe6e1e82012-08-24 00:27:51 +0800160 isect = isect + (offset >> SECTOR_SHIFT);
161 dprintk("%s: npg %d rw %d isect %llu offset %u len %d\n", __func__,
162 npg, rw, (unsigned long long)isect, offset, len);
Fred Isaman9549ec02011-07-30 20:52:53 -0400163retry:
164 if (!bio) {
165 bio = bl_alloc_init_bio(npg, isect, be, end_io, par);
166 if (!bio)
167 return ERR_PTR(-ENOMEM);
168 }
Peng Taofe6e1e82012-08-24 00:27:51 +0800169 if (bio_add_page(bio, page, len, offset) < len) {
Fred Isaman9549ec02011-07-30 20:52:53 -0400170 bio = bl_submit_bio(rw, bio);
171 goto retry;
172 }
173 return bio;
174}
175
Fred Isaman9549ec02011-07-30 20:52:53 -0400176static void bl_end_io_read(struct bio *bio, int err)
177{
178 struct parallel_io *par = bio->bi_private;
Fred Isaman9549ec02011-07-30 20:52:53 -0400179
Kent Overstreet2c30c712013-11-07 12:20:26 -0800180 if (err) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400181 struct nfs_pgio_header *header = par->data;
Fred Isamancd841602012-04-20 14:47:44 -0400182
183 if (!header->pnfs_error)
184 header->pnfs_error = -EIO;
185 pnfs_set_lo_fail(header->lseg);
Fred Isaman9549ec02011-07-30 20:52:53 -0400186 }
Christoph Hellwig8c792ea2014-09-10 08:23:33 -0700187
Fred Isaman9549ec02011-07-30 20:52:53 -0400188 bio_put(bio);
189 put_parallel(par);
190}
191
192static void bl_read_cleanup(struct work_struct *work)
193{
194 struct rpc_task *task;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400195 struct nfs_pgio_header *hdr;
Fred Isaman9549ec02011-07-30 20:52:53 -0400196 dprintk("%s enter\n", __func__);
197 task = container_of(work, struct rpc_task, u.tk_work);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400198 hdr = container_of(task, struct nfs_pgio_header, task);
199 pnfs_ld_read_done(hdr);
Fred Isaman9549ec02011-07-30 20:52:53 -0400200}
201
202static void
Peng Tao7c5465d2012-01-12 23:18:46 +0800203bl_end_par_io_read(void *data, int unused)
Fred Isaman9549ec02011-07-30 20:52:53 -0400204{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400205 struct nfs_pgio_header *hdr = data;
Fred Isaman9549ec02011-07-30 20:52:53 -0400206
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400207 hdr->task.tk_status = hdr->pnfs_error;
208 INIT_WORK(&hdr->task.u.tk_work, bl_read_cleanup);
209 schedule_work(&hdr->task.u.tk_work);
Fred Isaman9549ec02011-07-30 20:52:53 -0400210}
211
Fred Isaman155e7522011-07-30 20:52:39 -0400212static enum pnfs_try_status
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400213bl_read_pagelist(struct nfs_pgio_header *hdr)
Fred Isaman155e7522011-07-30 20:52:39 -0400214{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400215 struct nfs_pgio_header *header = hdr;
Fred Isaman9549ec02011-07-30 20:52:53 -0400216 int i, hole;
217 struct bio *bio = NULL;
218 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
219 sector_t isect, extent_length = 0;
220 struct parallel_io *par;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400221 loff_t f_offset = hdr->args.offset;
222 size_t bytes_left = hdr->args.count;
Peng Taof742dc42012-08-24 00:27:52 +0800223 unsigned int pg_offset, pg_len;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400224 struct page **pages = hdr->args.pages;
225 int pg_index = hdr->args.pgbase >> PAGE_CACHE_SHIFT;
Peng Taof742dc42012-08-24 00:27:52 +0800226 const bool is_dio = (header->dreq != NULL);
Christoph Hellwigbe98fd02014-08-21 11:09:28 -0500227 struct blk_plug plug;
Fred Isaman9549ec02011-07-30 20:52:53 -0400228
Trond Myklebust6f008662012-03-20 14:12:46 -0400229 dprintk("%s enter nr_pages %u offset %lld count %u\n", __func__,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400230 hdr->page_array.npages, f_offset,
231 (unsigned int)hdr->args.count);
Fred Isaman9549ec02011-07-30 20:52:53 -0400232
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400233 par = alloc_parallel(hdr);
Fred Isaman9549ec02011-07-30 20:52:53 -0400234 if (!par)
235 goto use_mds;
Fred Isaman9549ec02011-07-30 20:52:53 -0400236 par->pnfs_callback = bl_end_par_io_read;
237 /* At this point, we can no longer jump to use_mds */
238
Christoph Hellwigbe98fd02014-08-21 11:09:28 -0500239 blk_start_plug(&plug);
240
Fred Isaman9549ec02011-07-30 20:52:53 -0400241 isect = (sector_t) (f_offset >> SECTOR_SHIFT);
242 /* Code assumes extents are page-aligned */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400243 for (i = pg_index; i < hdr->page_array.npages; i++) {
Christoph Hellwig921b81a2014-08-21 11:09:29 -0500244 if (extent_length <= 0) {
Fred Isaman9549ec02011-07-30 20:52:53 -0400245 /* We've used up the previous extent */
246 bl_put_extent(be);
247 bl_put_extent(cow_read);
248 bio = bl_submit_bio(READ, bio);
249 /* Get the next one */
Fred Isamancd841602012-04-20 14:47:44 -0400250 be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg),
Fred Isaman9549ec02011-07-30 20:52:53 -0400251 isect, &cow_read);
252 if (!be) {
Fred Isamancd841602012-04-20 14:47:44 -0400253 header->pnfs_error = -EIO;
Fred Isaman9549ec02011-07-30 20:52:53 -0400254 goto out;
255 }
256 extent_length = be->be_length -
257 (isect - be->be_f_offset);
258 if (cow_read) {
259 sector_t cow_length = cow_read->be_length -
260 (isect - cow_read->be_f_offset);
261 extent_length = min(extent_length, cow_length);
262 }
263 }
Peng Taof742dc42012-08-24 00:27:52 +0800264
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700265 pg_offset = f_offset & ~PAGE_CACHE_MASK;
Peng Taof742dc42012-08-24 00:27:52 +0800266 if (is_dio) {
Peng Taof742dc42012-08-24 00:27:52 +0800267 if (pg_offset + bytes_left > PAGE_CACHE_SIZE)
268 pg_len = PAGE_CACHE_SIZE - pg_offset;
269 else
270 pg_len = bytes_left;
271
272 f_offset += pg_len;
273 bytes_left -= pg_len;
274 isect += (pg_offset >> SECTOR_SHIFT);
Christoph Hellwig921b81a2014-08-21 11:09:29 -0500275 extent_length -= (pg_offset >> SECTOR_SHIFT);
Peng Taof742dc42012-08-24 00:27:52 +0800276 } else {
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700277 BUG_ON(pg_offset != 0);
Peng Taof742dc42012-08-24 00:27:52 +0800278 pg_len = PAGE_CACHE_SIZE;
279 }
280
Fred Isaman9549ec02011-07-30 20:52:53 -0400281 hole = is_hole(be, isect);
282 if (hole && !cow_read) {
283 bio = bl_submit_bio(READ, bio);
284 /* Fill hole w/ zeroes w/o accessing device */
285 dprintk("%s Zeroing page for hole\n", __func__);
Peng Taof742dc42012-08-24 00:27:52 +0800286 zero_user_segment(pages[i], pg_offset, pg_len);
Fred Isaman9549ec02011-07-30 20:52:53 -0400287 } else {
288 struct pnfs_block_extent *be_read;
289
290 be_read = (hole && cow_read) ? cow_read : be;
Weston Andros Adamson823b0c92014-06-09 11:48:34 -0400291 bio = do_add_page_to_bio(bio,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400292 hdr->page_array.npages - i,
Fred Isaman30dd3742012-04-20 14:47:45 -0400293 READ,
Fred Isaman9549ec02011-07-30 20:52:53 -0400294 isect, pages[i], be_read,
Peng Taof742dc42012-08-24 00:27:52 +0800295 bl_end_io_read, par,
296 pg_offset, pg_len);
Fred Isaman9549ec02011-07-30 20:52:53 -0400297 if (IS_ERR(bio)) {
Fred Isamancd841602012-04-20 14:47:44 -0400298 header->pnfs_error = PTR_ERR(bio);
Peng Taoe6d05a72011-09-22 21:50:16 -0400299 bio = NULL;
Fred Isaman9549ec02011-07-30 20:52:53 -0400300 goto out;
301 }
302 }
Peng Taof742dc42012-08-24 00:27:52 +0800303 isect += (pg_len >> SECTOR_SHIFT);
Christoph Hellwig921b81a2014-08-21 11:09:29 -0500304 extent_length -= (pg_len >> SECTOR_SHIFT);
Fred Isaman9549ec02011-07-30 20:52:53 -0400305 }
Fred Isamancd841602012-04-20 14:47:44 -0400306 if ((isect << SECTOR_SHIFT) >= header->inode->i_size) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400307 hdr->res.eof = 1;
308 hdr->res.count = header->inode->i_size - hdr->args.offset;
Fred Isaman9549ec02011-07-30 20:52:53 -0400309 } else {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400310 hdr->res.count = (isect << SECTOR_SHIFT) - hdr->args.offset;
Fred Isaman9549ec02011-07-30 20:52:53 -0400311 }
312out:
313 bl_put_extent(be);
314 bl_put_extent(cow_read);
315 bl_submit_bio(READ, bio);
Christoph Hellwigbe98fd02014-08-21 11:09:28 -0500316 blk_finish_plug(&plug);
Fred Isaman9549ec02011-07-30 20:52:53 -0400317 put_parallel(par);
318 return PNFS_ATTEMPTED;
319
320 use_mds:
321 dprintk("Giving up and using normal NFS\n");
Fred Isaman155e7522011-07-30 20:52:39 -0400322 return PNFS_NOT_ATTEMPTED;
323}
324
Fred Isaman31e63062011-07-30 20:52:55 -0400325static void mark_extents_written(struct pnfs_block_layout *bl,
326 __u64 offset, __u32 count)
327{
328 sector_t isect, end;
329 struct pnfs_block_extent *be;
Peng Tao7c5465d2012-01-12 23:18:46 +0800330 struct pnfs_block_short_extent *se;
Fred Isaman31e63062011-07-30 20:52:55 -0400331
332 dprintk("%s(%llu, %u)\n", __func__, offset, count);
333 if (count == 0)
334 return;
335 isect = (offset & (long)(PAGE_CACHE_MASK)) >> SECTOR_SHIFT;
336 end = (offset + count + PAGE_CACHE_SIZE - 1) & (long)(PAGE_CACHE_MASK);
337 end >>= SECTOR_SHIFT;
338 while (isect < end) {
339 sector_t len;
340 be = bl_find_get_extent(bl, isect, NULL);
341 BUG_ON(!be); /* FIXME */
342 len = min(end, be->be_f_offset + be->be_length) - isect;
Peng Tao7c5465d2012-01-12 23:18:46 +0800343 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
344 se = bl_pop_one_short_extent(be->be_inval);
345 BUG_ON(!se);
346 bl_mark_for_commit(be, isect, len, se);
347 }
Fred Isaman31e63062011-07-30 20:52:55 -0400348 isect += len;
349 bl_put_extent(be);
350 }
351}
352
Fred Isaman650e2d32011-07-30 20:52:54 -0400353static void bl_end_io_write(struct bio *bio, int err)
Fred Isaman155e7522011-07-30 20:52:39 -0400354{
Fred Isaman650e2d32011-07-30 20:52:54 -0400355 struct parallel_io *par = bio->bi_private;
356 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400357 struct nfs_pgio_header *header = par->data;
Fred Isaman650e2d32011-07-30 20:52:54 -0400358
359 if (!uptodate) {
Fred Isamancd841602012-04-20 14:47:44 -0400360 if (!header->pnfs_error)
361 header->pnfs_error = -EIO;
362 pnfs_set_lo_fail(header->lseg);
Fred Isaman650e2d32011-07-30 20:52:54 -0400363 }
364 bio_put(bio);
365 put_parallel(par);
366}
367
368/* Function scheduled for call during bl_end_par_io_write,
369 * it marks sectors as written and extends the commitlist.
370 */
371static void bl_write_cleanup(struct work_struct *work)
372{
373 struct rpc_task *task;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400374 struct nfs_pgio_header *hdr;
Fred Isaman650e2d32011-07-30 20:52:54 -0400375 dprintk("%s enter\n", __func__);
376 task = container_of(work, struct rpc_task, u.tk_work);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400377 hdr = container_of(task, struct nfs_pgio_header, task);
378 if (likely(!hdr->pnfs_error)) {
Fred Isaman31e63062011-07-30 20:52:55 -0400379 /* Marks for LAYOUTCOMMIT */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400380 mark_extents_written(BLK_LSEG2EXT(hdr->lseg),
381 hdr->args.offset, hdr->args.count);
Fred Isaman31e63062011-07-30 20:52:55 -0400382 }
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400383 pnfs_ld_write_done(hdr);
Fred Isaman650e2d32011-07-30 20:52:54 -0400384}
385
386/* Called when last of bios associated with a bl_write_pagelist call finishes */
Peng Tao7c5465d2012-01-12 23:18:46 +0800387static void bl_end_par_io_write(void *data, int num_se)
Fred Isaman650e2d32011-07-30 20:52:54 -0400388{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400389 struct nfs_pgio_header *hdr = data;
Fred Isaman650e2d32011-07-30 20:52:54 -0400390
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400391 if (unlikely(hdr->pnfs_error)) {
392 bl_free_short_extents(&BLK_LSEG2EXT(hdr->lseg)->bl_inval,
Peng Tao7c5465d2012-01-12 23:18:46 +0800393 num_se);
394 }
395
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400396 hdr->task.tk_status = hdr->pnfs_error;
Weston Andros Adamsonc65e6252014-06-09 11:48:36 -0400397 hdr->verf.committed = NFS_FILE_SYNC;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400398 INIT_WORK(&hdr->task.u.tk_work, bl_write_cleanup);
399 schedule_work(&hdr->task.u.tk_work);
Fred Isaman650e2d32011-07-30 20:52:54 -0400400}
401
402static enum pnfs_try_status
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400403bl_write_pagelist(struct nfs_pgio_header *header, int sync)
Fred Isaman650e2d32011-07-30 20:52:54 -0400404{
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700405 int i, ret;
Fred Isaman650e2d32011-07-30 20:52:54 -0400406 struct bio *bio = NULL;
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700407 struct pnfs_block_extent *be = NULL;
408 sector_t isect, extent_length = 0;
Peng Tao96c9eae2012-08-24 00:27:53 +0800409 struct parallel_io *par = NULL;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400410 loff_t offset = header->args.offset;
411 size_t count = header->args.count;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400412 struct page **pages = header->args.pages;
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700413 int pg_index = pg_index = header->args.pgbase >> PAGE_CACHE_SHIFT;
Christoph Hellwigbe98fd02014-08-21 11:09:28 -0500414 struct blk_plug plug;
Fred Isaman650e2d32011-07-30 20:52:54 -0400415
416 dprintk("%s enter, %Zu@%lld\n", __func__, count, offset);
Peng Tao96c9eae2012-08-24 00:27:53 +0800417
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400418 /* At this point, header->page_aray is a (sequential) list of nfs_pages.
Peng Tao71cdd402011-07-30 20:52:56 -0400419 * We want to write each, and if there is an error set pnfs_error
420 * to have it redone using nfs.
Fred Isaman650e2d32011-07-30 20:52:54 -0400421 */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400422 par = alloc_parallel(header);
Fred Isaman650e2d32011-07-30 20:52:54 -0400423 if (!par)
Peng Tao7c5465d2012-01-12 23:18:46 +0800424 goto out_mds;
Fred Isaman650e2d32011-07-30 20:52:54 -0400425 par->pnfs_callback = bl_end_par_io_write;
426 /* At this point, have to be more careful with error handling */
427
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700428 blk_start_plug(&plug);
Peng Tao71cdd402011-07-30 20:52:56 -0400429
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700430 /* we always write out the whole page */
431 offset = offset & (loff_t)PAGE_CACHE_MASK;
432 isect = offset >> SECTOR_SHIFT;
Peng Tao71cdd402011-07-30 20:52:56 -0400433
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400434 for (i = pg_index; i < header->page_array.npages; i++) {
Christoph Hellwig921b81a2014-08-21 11:09:29 -0500435 if (extent_length <= 0) {
Fred Isaman650e2d32011-07-30 20:52:54 -0400436 /* We've used up the previous extent */
437 bl_put_extent(be);
438 bio = bl_submit_bio(WRITE, bio);
439 /* Get the next one */
Fred Isamancd841602012-04-20 14:47:44 -0400440 be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg),
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700441 isect, NULL);
Fred Isaman650e2d32011-07-30 20:52:54 -0400442 if (!be || !is_writable(be, isect)) {
Fred Isamancd841602012-04-20 14:47:44 -0400443 header->pnfs_error = -EINVAL;
Fred Isaman650e2d32011-07-30 20:52:54 -0400444 goto out;
445 }
Peng Tao7c5465d2012-01-12 23:18:46 +0800446 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
447 if (likely(!bl_push_one_short_extent(
448 be->be_inval)))
449 par->bse_count++;
450 else {
Fred Isamancd841602012-04-20 14:47:44 -0400451 header->pnfs_error = -ENOMEM;
Peng Tao7c5465d2012-01-12 23:18:46 +0800452 goto out;
453 }
454 }
Fred Isaman650e2d32011-07-30 20:52:54 -0400455 extent_length = be->be_length -
Peng Tao71cdd402011-07-30 20:52:56 -0400456 (isect - be->be_f_offset);
Fred Isaman650e2d32011-07-30 20:52:54 -0400457 }
Peng Taofe6e1e82012-08-24 00:27:51 +0800458
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700459 BUG_ON(offset & ~PAGE_CACHE_MASK);
Peng Taofe6e1e82012-08-24 00:27:51 +0800460
Peng Taofe6e1e82012-08-24 00:27:51 +0800461 if (be->be_state == PNFS_BLOCK_INVALID_DATA &&
462 !bl_is_sector_init(be->be_inval, isect)) {
Peng Tao71cdd402011-07-30 20:52:56 -0400463 ret = bl_mark_sectors_init(be->be_inval, isect,
Peng Tao60c52e32012-01-12 23:18:40 +0800464 PAGE_CACHE_SECTORS);
Peng Tao71cdd402011-07-30 20:52:56 -0400465 if (unlikely(ret)) {
466 dprintk("%s bl_mark_sectors_init fail %d\n",
467 __func__, ret);
Fred Isamancd841602012-04-20 14:47:44 -0400468 header->pnfs_error = ret;
Peng Tao71cdd402011-07-30 20:52:56 -0400469 goto out;
Fred Isaman650e2d32011-07-30 20:52:54 -0400470 }
Peng Tao71cdd402011-07-30 20:52:56 -0400471 }
Peng Taofe6e1e82012-08-24 00:27:51 +0800472
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400473 bio = do_add_page_to_bio(bio, header->page_array.npages - i,
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700474 WRITE, isect, pages[i], be,
Peng Taofe6e1e82012-08-24 00:27:51 +0800475 bl_end_io_write, par,
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700476 0, PAGE_CACHE_SIZE);
Peng Tao71cdd402011-07-30 20:52:56 -0400477 if (IS_ERR(bio)) {
Fred Isamancd841602012-04-20 14:47:44 -0400478 header->pnfs_error = PTR_ERR(bio);
Peng Taoe6d05a72011-09-22 21:50:16 -0400479 bio = NULL;
Peng Tao71cdd402011-07-30 20:52:56 -0400480 goto out;
Fred Isaman650e2d32011-07-30 20:52:54 -0400481 }
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700482 offset += PAGE_CACHE_SIZE;
483 count -= PAGE_CACHE_SIZE;
Fred Isaman650e2d32011-07-30 20:52:54 -0400484 isect += PAGE_CACHE_SECTORS;
485 extent_length -= PAGE_CACHE_SECTORS;
486 }
Peng Tao71cdd402011-07-30 20:52:56 -0400487
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400488 header->res.count = header->args.count;
Fred Isaman650e2d32011-07-30 20:52:54 -0400489out:
490 bl_put_extent(be);
491 bl_submit_bio(WRITE, bio);
Christoph Hellwigbe98fd02014-08-21 11:09:28 -0500492 blk_finish_plug(&plug);
Fred Isaman650e2d32011-07-30 20:52:54 -0400493 put_parallel(par);
494 return PNFS_ATTEMPTED;
Peng Tao7c5465d2012-01-12 23:18:46 +0800495out_mds:
Peng Tao7c5465d2012-01-12 23:18:46 +0800496 return PNFS_NOT_ATTEMPTED;
Fred Isaman155e7522011-07-30 20:52:39 -0400497}
498
Fred Isaman9e692962011-07-30 20:52:41 -0400499/* FIXME - range ignored */
Fred Isaman155e7522011-07-30 20:52:39 -0400500static void
Fred Isaman9e692962011-07-30 20:52:41 -0400501release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range)
Fred Isaman155e7522011-07-30 20:52:39 -0400502{
Fred Isaman9e692962011-07-30 20:52:41 -0400503 int i;
504 struct pnfs_block_extent *be;
505
506 spin_lock(&bl->bl_ext_lock);
507 for (i = 0; i < EXTENT_LISTS; i++) {
508 while (!list_empty(&bl->bl_extents[i])) {
509 be = list_first_entry(&bl->bl_extents[i],
510 struct pnfs_block_extent,
511 be_node);
512 list_del(&be->be_node);
513 bl_put_extent(be);
514 }
515 }
516 spin_unlock(&bl->bl_ext_lock);
Fred Isaman155e7522011-07-30 20:52:39 -0400517}
518
Fred Isaman155e7522011-07-30 20:52:39 -0400519static void
520release_inval_marks(struct pnfs_inval_markings *marks)
521{
Fred Isamanc1c2a4c2011-07-30 20:52:49 -0400522 struct pnfs_inval_tracking *pos, *temp;
Peng Tao7c5465d2012-01-12 23:18:46 +0800523 struct pnfs_block_short_extent *se, *stemp;
Fred Isamanc1c2a4c2011-07-30 20:52:49 -0400524
525 list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) {
526 list_del(&pos->it_link);
527 kfree(pos);
528 }
Peng Tao7c5465d2012-01-12 23:18:46 +0800529
530 list_for_each_entry_safe(se, stemp, &marks->im_extents, bse_node) {
531 list_del(&se->bse_node);
532 kfree(se);
533 }
Fred Isaman155e7522011-07-30 20:52:39 -0400534 return;
535}
536
537static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
538{
539 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
540
541 dprintk("%s enter\n", __func__);
542 release_extents(bl, NULL);
543 release_inval_marks(&bl->bl_inval);
544 kfree(bl);
545}
546
547static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode,
548 gfp_t gfp_flags)
549{
550 struct pnfs_block_layout *bl;
551
552 dprintk("%s enter\n", __func__);
553 bl = kzalloc(sizeof(*bl), gfp_flags);
554 if (!bl)
555 return NULL;
556 spin_lock_init(&bl->bl_ext_lock);
557 INIT_LIST_HEAD(&bl->bl_extents[0]);
558 INIT_LIST_HEAD(&bl->bl_extents[1]);
559 INIT_LIST_HEAD(&bl->bl_commit);
560 INIT_LIST_HEAD(&bl->bl_committing);
561 bl->bl_count = 0;
562 bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT;
563 BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize);
564 return &bl->bl_layout;
565}
566
Fred Isamana60d2eb2011-07-30 20:52:44 -0400567static void bl_free_lseg(struct pnfs_layout_segment *lseg)
Fred Isaman155e7522011-07-30 20:52:39 -0400568{
Fred Isamana60d2eb2011-07-30 20:52:44 -0400569 dprintk("%s enter\n", __func__);
570 kfree(lseg);
Fred Isaman155e7522011-07-30 20:52:39 -0400571}
572
Fred Isamana60d2eb2011-07-30 20:52:44 -0400573/* We pretty much ignore lseg, and store all data layout wide, so we
574 * can correctly merge.
575 */
576static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo,
577 struct nfs4_layoutget_res *lgr,
578 gfp_t gfp_flags)
Fred Isaman155e7522011-07-30 20:52:39 -0400579{
Fred Isamana60d2eb2011-07-30 20:52:44 -0400580 struct pnfs_layout_segment *lseg;
581 int status;
582
583 dprintk("%s enter\n", __func__);
584 lseg = kzalloc(sizeof(*lseg), gfp_flags);
585 if (!lseg)
586 return ERR_PTR(-ENOMEM);
587 status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags);
588 if (status) {
589 /* We don't want to call the full-blown bl_free_lseg,
590 * since on error extents were not touched.
591 */
592 kfree(lseg);
593 return ERR_PTR(status);
594 }
595 return lseg;
Fred Isaman155e7522011-07-30 20:52:39 -0400596}
597
598static void
599bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr,
600 const struct nfs4_layoutcommit_args *arg)
601{
Fred Isaman90ace122011-07-30 20:52:51 -0400602 dprintk("%s enter\n", __func__);
603 encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg);
Fred Isaman155e7522011-07-30 20:52:39 -0400604}
605
606static void
607bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
608{
Fred Isamanb2be7812011-07-30 20:52:52 -0400609 struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout;
610
611 dprintk("%s enter\n", __func__);
612 clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status);
Fred Isaman155e7522011-07-30 20:52:39 -0400613}
614
Fred Isaman2f9fd182011-07-30 20:52:46 -0400615static void free_blk_mountid(struct block_mount_id *mid)
616{
617 if (mid) {
Peng Tao93a38442012-01-12 23:18:47 +0800618 struct pnfs_block_dev *dev, *tmp;
619
620 /* No need to take bm_lock as we are last user freeing bm_devlist */
621 list_for_each_entry_safe(dev, tmp, &mid->bm_devlist, bm_node) {
Fred Isaman2f9fd182011-07-30 20:52:46 -0400622 list_del(&dev->bm_node);
623 bl_free_block_dev(dev);
624 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400625 kfree(mid);
626 }
627}
628
Trond Myklebust78e4e052012-09-18 21:02:29 -0400629/* This is mostly copied from the filelayout_get_device_info function.
Fred Isaman2f9fd182011-07-30 20:52:46 -0400630 * It seems much of this should be at the generic pnfs level.
631 */
632static struct pnfs_block_dev *
633nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh,
634 struct nfs4_deviceid *d_id)
635{
636 struct pnfs_device *dev;
Jim Rees516f2e22011-09-22 21:50:08 -0400637 struct pnfs_block_dev *rv;
Fred Isaman2f9fd182011-07-30 20:52:46 -0400638 u32 max_resp_sz;
639 int max_pages;
640 struct page **pages = NULL;
641 int i, rc;
642
643 /*
644 * Use the session max response size as the basis for setting
645 * GETDEVICEINFO's maxcount
646 */
647 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
Jim Rees10bd295a2012-04-09 22:33:39 -0400648 max_pages = nfs_page_array_len(0, max_resp_sz);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400649 dprintk("%s max_resp_sz %u max_pages %d\n",
650 __func__, max_resp_sz, max_pages);
651
652 dev = kmalloc(sizeof(*dev), GFP_NOFS);
653 if (!dev) {
654 dprintk("%s kmalloc failed\n", __func__);
Jim Rees516f2e22011-09-22 21:50:08 -0400655 return ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400656 }
657
Fabian Frederickf15b5042014-06-25 20:26:21 +0200658 pages = kcalloc(max_pages, sizeof(struct page *), GFP_NOFS);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400659 if (pages == NULL) {
660 kfree(dev);
Jim Rees516f2e22011-09-22 21:50:08 -0400661 return ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400662 }
663 for (i = 0; i < max_pages; i++) {
664 pages[i] = alloc_page(GFP_NOFS);
Jim Rees516f2e22011-09-22 21:50:08 -0400665 if (!pages[i]) {
666 rv = ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400667 goto out_free;
Jim Rees516f2e22011-09-22 21:50:08 -0400668 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400669 }
670
671 memcpy(&dev->dev_id, d_id, sizeof(*d_id));
672 dev->layout_type = LAYOUT_BLOCK_VOLUME;
673 dev->pages = pages;
674 dev->pgbase = 0;
675 dev->pglen = PAGE_SIZE * max_pages;
676 dev->mincount = 0;
Andy Adamson968fe252013-06-25 19:02:54 -0400677 dev->maxcount = max_resp_sz - nfs41_maxgetdevinfo_overhead;
Fred Isaman2f9fd182011-07-30 20:52:46 -0400678
679 dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data);
Trond Myklebustcd5875f2013-05-20 11:42:54 -0400680 rc = nfs4_proc_getdeviceinfo(server, dev, NULL);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400681 dprintk("%s getdevice info returns %d\n", __func__, rc);
Jim Rees516f2e22011-09-22 21:50:08 -0400682 if (rc) {
683 rv = ERR_PTR(rc);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400684 goto out_free;
Jim Rees516f2e22011-09-22 21:50:08 -0400685 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400686
687 rv = nfs4_blk_decode_device(server, dev);
688 out_free:
689 for (i = 0; i < max_pages; i++)
690 __free_page(pages[i]);
691 kfree(pages);
692 kfree(dev);
693 return rv;
694}
695
Fred Isaman155e7522011-07-30 20:52:39 -0400696static int
697bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
698{
Fred Isaman2f9fd182011-07-30 20:52:46 -0400699 struct block_mount_id *b_mt_id = NULL;
700 struct pnfs_devicelist *dlist = NULL;
701 struct pnfs_block_dev *bdev;
702 LIST_HEAD(block_disklist);
Jim Rees516f2e22011-09-22 21:50:08 -0400703 int status, i;
Fred Isaman2f9fd182011-07-30 20:52:46 -0400704
Fred Isaman155e7522011-07-30 20:52:39 -0400705 dprintk("%s enter\n", __func__);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400706
707 if (server->pnfs_blksize == 0) {
708 dprintk("%s Server did not return blksize\n", __func__);
709 return -EINVAL;
710 }
Christoph Hellwige3aaf7f2014-08-21 11:09:26 -0500711 if (server->pnfs_blksize > PAGE_SIZE) {
712 printk(KERN_ERR "%s: pNFS blksize %d not supported.\n",
713 __func__, server->pnfs_blksize);
714 return -EINVAL;
715 }
716
Fred Isaman2f9fd182011-07-30 20:52:46 -0400717 b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS);
718 if (!b_mt_id) {
719 status = -ENOMEM;
720 goto out_error;
721 }
722 /* Initialize nfs4 block layout mount id */
723 spin_lock_init(&b_mt_id->bm_lock);
724 INIT_LIST_HEAD(&b_mt_id->bm_devlist);
725
726 dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS);
727 if (!dlist) {
728 status = -ENOMEM;
729 goto out_error;
730 }
731 dlist->eof = 0;
732 while (!dlist->eof) {
733 status = nfs4_proc_getdevicelist(server, fh, dlist);
734 if (status)
735 goto out_error;
736 dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n",
737 __func__, dlist->num_devs, dlist->eof);
738 for (i = 0; i < dlist->num_devs; i++) {
739 bdev = nfs4_blk_get_deviceinfo(server, fh,
740 &dlist->dev_id[i]);
Jim Rees516f2e22011-09-22 21:50:08 -0400741 if (IS_ERR(bdev)) {
742 status = PTR_ERR(bdev);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400743 goto out_error;
744 }
745 spin_lock(&b_mt_id->bm_lock);
746 list_add(&bdev->bm_node, &b_mt_id->bm_devlist);
747 spin_unlock(&b_mt_id->bm_lock);
748 }
749 }
750 dprintk("%s SUCCESS\n", __func__);
751 server->pnfs_ld_data = b_mt_id;
752
753 out_return:
754 kfree(dlist);
755 return status;
756
757 out_error:
758 free_blk_mountid(b_mt_id);
759 goto out_return;
Fred Isaman155e7522011-07-30 20:52:39 -0400760}
761
762static int
763bl_clear_layoutdriver(struct nfs_server *server)
764{
Fred Isaman2f9fd182011-07-30 20:52:46 -0400765 struct block_mount_id *b_mt_id = server->pnfs_ld_data;
766
Fred Isaman155e7522011-07-30 20:52:39 -0400767 dprintk("%s enter\n", __func__);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400768 free_blk_mountid(b_mt_id);
769 dprintk("%s RETURNS\n", __func__);
Fred Isaman155e7522011-07-30 20:52:39 -0400770 return 0;
771}
772
Peng Taof742dc42012-08-24 00:27:52 +0800773static bool
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700774is_aligned_req(struct nfs_pageio_descriptor *pgio,
775 struct nfs_page *req, unsigned int alignment)
Peng Taof742dc42012-08-24 00:27:52 +0800776{
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700777 /*
778 * Always accept buffered writes, higher layers take care of the
779 * right alignment.
780 */
781 if (pgio->pg_dreq == NULL)
782 return true;
783
784 if (!IS_ALIGNED(req->wb_offset, alignment))
785 return false;
786
787 if (IS_ALIGNED(req->wb_bytes, alignment))
788 return true;
789
790 if (req_offset(req) + req->wb_bytes == i_size_read(pgio->pg_inode)) {
791 /*
792 * If the write goes up to the inode size, just write
793 * the full page. Data past the inode size is
794 * guaranteed to be zeroed by the higher level client
795 * code, and this behaviour is mandated by RFC 5663
796 * section 2.3.2.
797 */
798 return true;
799 }
800
801 return false;
Peng Taof742dc42012-08-24 00:27:52 +0800802}
803
804static void
805bl_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
806{
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700807 if (!is_aligned_req(pgio, req, SECTOR_SIZE)) {
Peng Taof742dc42012-08-24 00:27:52 +0800808 nfs_pageio_reset_read_mds(pgio);
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700809 return;
810 }
811
812 pnfs_generic_pg_init_read(pgio, req);
Peng Taof742dc42012-08-24 00:27:52 +0800813}
814
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400815/*
816 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
817 * of bytes (maximum @req->wb_bytes) that can be coalesced.
818 */
819static size_t
Peng Taof742dc42012-08-24 00:27:52 +0800820bl_pg_test_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
821 struct nfs_page *req)
822{
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700823 if (!is_aligned_req(pgio, req, SECTOR_SIZE))
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400824 return 0;
Peng Taof742dc42012-08-24 00:27:52 +0800825 return pnfs_generic_pg_test(pgio, prev, req);
826}
827
Peng Tao62965562012-09-25 14:55:57 +0800828/*
829 * Return the number of contiguous bytes for a given inode
830 * starting at page frame idx.
831 */
832static u64 pnfs_num_cont_bytes(struct inode *inode, pgoff_t idx)
833{
834 struct address_space *mapping = inode->i_mapping;
835 pgoff_t end;
836
837 /* Optimize common case that writes from 0 to end of file */
838 end = DIV_ROUND_UP(i_size_read(inode), PAGE_CACHE_SIZE);
839 if (end != NFS_I(inode)->npages) {
840 rcu_read_lock();
Johannes Weinere7b563b2014-04-03 14:47:44 -0700841 end = page_cache_next_hole(mapping, idx + 1, ULONG_MAX);
Peng Tao62965562012-09-25 14:55:57 +0800842 rcu_read_unlock();
843 }
844
845 if (!end)
846 return i_size_read(inode) - (idx << PAGE_CACHE_SHIFT);
847 else
848 return (end - idx) << PAGE_CACHE_SHIFT;
849}
850
Trond Myklebust6f018ef2012-10-02 08:29:14 -0700851static void
Peng Tao96c9eae2012-08-24 00:27:53 +0800852bl_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
853{
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700854 u64 wb_size;
Peng Tao62965562012-09-25 14:55:57 +0800855
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700856 if (!is_aligned_req(pgio, req, PAGE_SIZE)) {
857 nfs_pageio_reset_write_mds(pgio);
858 return;
Peng Tao62965562012-09-25 14:55:57 +0800859 }
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700860
861 if (pgio->pg_dreq == NULL)
862 wb_size = pnfs_num_cont_bytes(pgio->pg_inode,
863 req->wb_index);
864 else
865 wb_size = nfs_dreq_bytes_left(pgio->pg_dreq);
866
867 pnfs_generic_pg_init_write(pgio, req, wb_size);
Peng Tao96c9eae2012-08-24 00:27:53 +0800868}
869
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400870/*
871 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
872 * of bytes (maximum @req->wb_bytes) that can be coalesced.
873 */
874static size_t
Peng Tao96c9eae2012-08-24 00:27:53 +0800875bl_pg_test_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
876 struct nfs_page *req)
877{
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700878 if (!is_aligned_req(pgio, req, PAGE_SIZE))
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400879 return 0;
Peng Tao96c9eae2012-08-24 00:27:53 +0800880 return pnfs_generic_pg_test(pgio, prev, req);
881}
882
Benny Halevye9643fe2011-07-30 20:52:40 -0400883static const struct nfs_pageio_ops bl_pg_read_ops = {
Peng Taof742dc42012-08-24 00:27:52 +0800884 .pg_init = bl_pg_init_read,
885 .pg_test = bl_pg_test_read,
Benny Halevye9643fe2011-07-30 20:52:40 -0400886 .pg_doio = pnfs_generic_pg_readpages,
887};
888
889static const struct nfs_pageio_ops bl_pg_write_ops = {
Peng Tao96c9eae2012-08-24 00:27:53 +0800890 .pg_init = bl_pg_init_write,
891 .pg_test = bl_pg_test_write,
Benny Halevye9643fe2011-07-30 20:52:40 -0400892 .pg_doio = pnfs_generic_pg_writepages,
893};
894
Fred Isaman155e7522011-07-30 20:52:39 -0400895static struct pnfs_layoutdriver_type blocklayout_type = {
896 .id = LAYOUT_BLOCK_VOLUME,
897 .name = "LAYOUT_BLOCK_VOLUME",
fanchaoting5a12cca2013-02-04 21:15:02 +0800898 .owner = THIS_MODULE,
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700899 .flags = PNFS_READ_WHOLE_PAGE,
Fred Isaman155e7522011-07-30 20:52:39 -0400900 .read_pagelist = bl_read_pagelist,
901 .write_pagelist = bl_write_pagelist,
902 .alloc_layout_hdr = bl_alloc_layout_hdr,
903 .free_layout_hdr = bl_free_layout_hdr,
904 .alloc_lseg = bl_alloc_lseg,
905 .free_lseg = bl_free_lseg,
906 .encode_layoutcommit = bl_encode_layoutcommit,
907 .cleanup_layoutcommit = bl_cleanup_layoutcommit,
908 .set_layoutdriver = bl_set_layoutdriver,
909 .clear_layoutdriver = bl_clear_layoutdriver,
Benny Halevye9643fe2011-07-30 20:52:40 -0400910 .pg_read_ops = &bl_pg_read_ops,
911 .pg_write_ops = &bl_pg_write_ops,
Fred Isaman155e7522011-07-30 20:52:39 -0400912};
913
Jim Reesfe0a9b72011-07-30 20:52:42 -0400914static const struct rpc_pipe_ops bl_upcall_ops = {
Peng Taoc1225152011-09-22 21:50:10 -0400915 .upcall = rpc_pipe_generic_upcall,
Jim Reesfe0a9b72011-07-30 20:52:42 -0400916 .downcall = bl_pipe_downcall,
917 .destroy_msg = bl_pipe_destroy_msg,
918};
919
Stanislav Kinsbursky332dfab2012-01-10 17:04:16 +0400920static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb,
921 struct rpc_pipe *pipe)
922{
923 struct dentry *dir, *dentry;
924
925 dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME);
926 if (dir == NULL)
927 return ERR_PTR(-ENOENT);
928 dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe);
929 dput(dir);
930 return dentry;
931}
932
933static void nfs4blocklayout_unregister_sb(struct super_block *sb,
934 struct rpc_pipe *pipe)
935{
936 if (pipe->dentry)
937 rpc_unlink(pipe->dentry);
938}
939
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +0400940static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
941 void *ptr)
942{
943 struct super_block *sb = ptr;
944 struct net *net = sb->s_fs_info;
945 struct nfs_net *nn = net_generic(net, nfs_net_id);
946 struct dentry *dentry;
947 int ret = 0;
948
949 if (!try_module_get(THIS_MODULE))
950 return 0;
951
952 if (nn->bl_device_pipe == NULL) {
953 module_put(THIS_MODULE);
954 return 0;
955 }
956
957 switch (event) {
958 case RPC_PIPEFS_MOUNT:
959 dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe);
960 if (IS_ERR(dentry)) {
961 ret = PTR_ERR(dentry);
962 break;
963 }
964 nn->bl_device_pipe->dentry = dentry;
965 break;
966 case RPC_PIPEFS_UMOUNT:
967 if (nn->bl_device_pipe->dentry)
968 nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe);
969 break;
970 default:
971 ret = -ENOTSUPP;
972 break;
973 }
974 module_put(THIS_MODULE);
975 return ret;
976}
977
978static struct notifier_block nfs4blocklayout_block = {
979 .notifier_call = rpc_pipefs_event,
980};
981
Stanislav Kinsbursky332dfab2012-01-10 17:04:16 +0400982static struct dentry *nfs4blocklayout_register_net(struct net *net,
983 struct rpc_pipe *pipe)
984{
985 struct super_block *pipefs_sb;
986 struct dentry *dentry;
987
988 pipefs_sb = rpc_get_sb_net(net);
989 if (!pipefs_sb)
Stanislav Kinsbursky2561d612012-01-10 17:04:40 +0400990 return NULL;
Stanislav Kinsbursky332dfab2012-01-10 17:04:16 +0400991 dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe);
992 rpc_put_sb_net(net);
993 return dentry;
994}
995
996static void nfs4blocklayout_unregister_net(struct net *net,
997 struct rpc_pipe *pipe)
998{
999 struct super_block *pipefs_sb;
1000
1001 pipefs_sb = rpc_get_sb_net(net);
1002 if (pipefs_sb) {
1003 nfs4blocklayout_unregister_sb(pipefs_sb, pipe);
1004 rpc_put_sb_net(net);
1005 }
1006}
1007
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04001008static int nfs4blocklayout_net_init(struct net *net)
1009{
1010 struct nfs_net *nn = net_generic(net, nfs_net_id);
1011 struct dentry *dentry;
1012
Stanislav Kinsbursky5ffaf852012-03-11 18:20:31 +04001013 init_waitqueue_head(&nn->bl_wq);
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04001014 nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0);
1015 if (IS_ERR(nn->bl_device_pipe))
1016 return PTR_ERR(nn->bl_device_pipe);
1017 dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe);
1018 if (IS_ERR(dentry)) {
1019 rpc_destroy_pipe_data(nn->bl_device_pipe);
1020 return PTR_ERR(dentry);
1021 }
1022 nn->bl_device_pipe->dentry = dentry;
1023 return 0;
1024}
1025
1026static void nfs4blocklayout_net_exit(struct net *net)
1027{
1028 struct nfs_net *nn = net_generic(net, nfs_net_id);
1029
1030 nfs4blocklayout_unregister_net(net, nn->bl_device_pipe);
1031 rpc_destroy_pipe_data(nn->bl_device_pipe);
1032 nn->bl_device_pipe = NULL;
1033}
1034
1035static struct pernet_operations nfs4blocklayout_net_ops = {
1036 .init = nfs4blocklayout_net_init,
1037 .exit = nfs4blocklayout_net_exit,
1038};
1039
Fred Isaman155e7522011-07-30 20:52:39 -04001040static int __init nfs4blocklayout_init(void)
1041{
1042 int ret;
1043
1044 dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
1045
1046 ret = pnfs_register_layoutdriver(&blocklayout_type);
Jim Reesfe0a9b72011-07-30 20:52:42 -04001047 if (ret)
1048 goto out;
1049
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001050 ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block);
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04001051 if (ret)
1052 goto out_remove;
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001053 ret = register_pernet_subsys(&nfs4blocklayout_net_ops);
1054 if (ret)
1055 goto out_notifier;
Jim Reesfe0a9b72011-07-30 20:52:42 -04001056out:
1057 return ret;
1058
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001059out_notifier:
1060 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
Jim Reesfe0a9b72011-07-30 20:52:42 -04001061out_remove:
1062 pnfs_unregister_layoutdriver(&blocklayout_type);
Fred Isaman155e7522011-07-30 20:52:39 -04001063 return ret;
1064}
1065
1066static void __exit nfs4blocklayout_exit(void)
1067{
1068 dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
1069 __func__);
1070
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001071 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04001072 unregister_pernet_subsys(&nfs4blocklayout_net_ops);
Fred Isaman155e7522011-07-30 20:52:39 -04001073 pnfs_unregister_layoutdriver(&blocklayout_type);
Fred Isaman155e7522011-07-30 20:52:39 -04001074}
1075
1076MODULE_ALIAS("nfs-layouttype4-3");
1077
1078module_init(nfs4blocklayout_init);
1079module_exit(nfs4blocklayout_exit);