blob: 8502e620f64422b7c7b8d23ac86969fe521f4bc3 [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
Christoph Hellwig80672532014-09-10 08:23:34 -070052static bool is_hole(struct pnfs_block_extent *be)
Fred Isaman9549ec02011-07-30 20:52:53 -040053{
Christoph Hellwig80672532014-09-10 08:23:34 -070054 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 }
Fred Isaman650e2d32011-07-30 20:52:54 -040062}
63
Fred Isaman9549ec02011-07-30 20:52:53 -040064/* 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;
Christoph Hellwig80672532014-09-10 08:23:34 -070069 void (*pnfs_callback) (void *data);
Fred Isaman9549ec02011-07-30 20:52:53 -040070 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__);
Christoph Hellwig80672532014-09-10 08:23:34 -070095 p->pnfs_callback(p->data);
Fred Isaman9549ec02011-07-30 20:52:53 -040096 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__,
Kent Overstreet4f024f32013-10-11 15:44:27 -0700110 rw == READ ? "read" : "write", bio->bi_iter.bi_size,
111 (unsigned long long)bio->bi_iter.bi_sector);
Fred Isaman9549ec02011-07-30 20:52:53 -0400112 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
Peng Tao74a6eeb2012-01-12 23:18:48 +0800124 npg = min(npg, BIO_MAX_PAGES);
Fred Isaman9549ec02011-07-30 20:52:53 -0400125 bio = bio_alloc(GFP_NOIO, npg);
Peng Tao74a6eeb2012-01-12 23:18:48 +0800126 if (!bio && (current->flags & PF_MEMALLOC)) {
127 while (!bio && (npg /= 2))
128 bio = bio_alloc(GFP_NOIO, npg);
129 }
Fred Isaman9549ec02011-07-30 20:52:53 -0400130
Peng Tao74a6eeb2012-01-12 23:18:48 +0800131 if (bio) {
Kent Overstreet4f024f32013-10-11 15:44:27 -0700132 bio->bi_iter.bi_sector = isect - be->be_f_offset +
133 be->be_v_offset;
Peng Tao74a6eeb2012-01-12 23:18:48 +0800134 bio->bi_bdev = be->be_mdev;
135 bio->bi_end_io = end_io;
136 bio->bi_private = par;
137 }
Fred Isaman9549ec02011-07-30 20:52:53 -0400138 return bio;
139}
140
Peng Taofe6e1e82012-08-24 00:27:51 +0800141static struct bio *do_add_page_to_bio(struct bio *bio, int npg, int rw,
Fred Isaman9549ec02011-07-30 20:52:53 -0400142 sector_t isect, struct page *page,
143 struct pnfs_block_extent *be,
144 void (*end_io)(struct bio *, int err),
Peng Taofe6e1e82012-08-24 00:27:51 +0800145 struct parallel_io *par,
146 unsigned int offset, int len)
Fred Isaman9549ec02011-07-30 20:52:53 -0400147{
Peng Taofe6e1e82012-08-24 00:27:51 +0800148 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);
Fred Isaman9549ec02011-07-30 20:52:53 -0400151retry:
152 if (!bio) {
153 bio = bl_alloc_init_bio(npg, isect, be, end_io, par);
154 if (!bio)
155 return ERR_PTR(-ENOMEM);
156 }
Peng Taofe6e1e82012-08-24 00:27:51 +0800157 if (bio_add_page(bio, page, len, offset) < len) {
Fred Isaman9549ec02011-07-30 20:52:53 -0400158 bio = bl_submit_bio(rw, bio);
159 goto retry;
160 }
161 return bio;
162}
163
Fred Isaman9549ec02011-07-30 20:52:53 -0400164static void bl_end_io_read(struct bio *bio, int err)
165{
166 struct parallel_io *par = bio->bi_private;
Fred Isaman9549ec02011-07-30 20:52:53 -0400167
Kent Overstreet2c30c712013-11-07 12:20:26 -0800168 if (err) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400169 struct nfs_pgio_header *header = par->data;
Fred Isamancd841602012-04-20 14:47:44 -0400170
171 if (!header->pnfs_error)
172 header->pnfs_error = -EIO;
173 pnfs_set_lo_fail(header->lseg);
Fred Isaman9549ec02011-07-30 20:52:53 -0400174 }
Christoph Hellwig8c792ea2014-09-10 08:23:33 -0700175
Fred Isaman9549ec02011-07-30 20:52:53 -0400176 bio_put(bio);
177 put_parallel(par);
178}
179
180static void bl_read_cleanup(struct work_struct *work)
181{
182 struct rpc_task *task;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400183 struct nfs_pgio_header *hdr;
Fred Isaman9549ec02011-07-30 20:52:53 -0400184 dprintk("%s enter\n", __func__);
185 task = container_of(work, struct rpc_task, u.tk_work);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400186 hdr = container_of(task, struct nfs_pgio_header, task);
187 pnfs_ld_read_done(hdr);
Fred Isaman9549ec02011-07-30 20:52:53 -0400188}
189
190static void
Christoph Hellwig80672532014-09-10 08:23:34 -0700191bl_end_par_io_read(void *data)
Fred Isaman9549ec02011-07-30 20:52:53 -0400192{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400193 struct nfs_pgio_header *hdr = data;
Fred Isaman9549ec02011-07-30 20:52:53 -0400194
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400195 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);
Fred Isaman9549ec02011-07-30 20:52:53 -0400198}
199
Fred Isaman155e7522011-07-30 20:52:39 -0400200static enum pnfs_try_status
Christoph Hellwig80672532014-09-10 08:23:34 -0700201bl_read_pagelist(struct nfs_pgio_header *header)
Fred Isaman155e7522011-07-30 20:52:39 -0400202{
Christoph Hellwig80672532014-09-10 08:23:34 -0700203 struct pnfs_block_layout *bl = BLK_LSEG2EXT(header->lseg);
Fred Isaman9549ec02011-07-30 20:52:53 -0400204 struct bio *bio = NULL;
Christoph Hellwig80672532014-09-10 08:23:34 -0700205 struct pnfs_block_extent be;
Fred Isaman9549ec02011-07-30 20:52:53 -0400206 sector_t isect, extent_length = 0;
207 struct parallel_io *par;
Christoph Hellwig80672532014-09-10 08:23:34 -0700208 loff_t f_offset = header->args.offset;
209 size_t bytes_left = header->args.count;
Peng Taof742dc42012-08-24 00:27:52 +0800210 unsigned int pg_offset, pg_len;
Christoph Hellwig80672532014-09-10 08:23:34 -0700211 struct page **pages = header->args.pages;
212 int pg_index = header->args.pgbase >> PAGE_CACHE_SHIFT;
Peng Taof742dc42012-08-24 00:27:52 +0800213 const bool is_dio = (header->dreq != NULL);
Christoph Hellwigbe98fd02014-08-21 11:09:28 -0500214 struct blk_plug plug;
Christoph Hellwig80672532014-09-10 08:23:34 -0700215 int i;
Fred Isaman9549ec02011-07-30 20:52:53 -0400216
Trond Myklebust6f008662012-03-20 14:12:46 -0400217 dprintk("%s enter nr_pages %u offset %lld count %u\n", __func__,
Christoph Hellwig80672532014-09-10 08:23:34 -0700218 header->page_array.npages, f_offset,
219 (unsigned int)header->args.count);
Fred Isaman9549ec02011-07-30 20:52:53 -0400220
Christoph Hellwig80672532014-09-10 08:23:34 -0700221 par = alloc_parallel(header);
Fred Isaman9549ec02011-07-30 20:52:53 -0400222 if (!par)
Christoph Hellwig80672532014-09-10 08:23:34 -0700223 return PNFS_NOT_ATTEMPTED;
Fred Isaman9549ec02011-07-30 20:52:53 -0400224 par->pnfs_callback = bl_end_par_io_read;
Fred Isaman9549ec02011-07-30 20:52:53 -0400225
Christoph Hellwigbe98fd02014-08-21 11:09:28 -0500226 blk_start_plug(&plug);
227
Fred Isaman9549ec02011-07-30 20:52:53 -0400228 isect = (sector_t) (f_offset >> SECTOR_SHIFT);
229 /* Code assumes extents are page-aligned */
Christoph Hellwig80672532014-09-10 08:23:34 -0700230 for (i = pg_index; i < header->page_array.npages; i++) {
Christoph Hellwig921b81a2014-08-21 11:09:29 -0500231 if (extent_length <= 0) {
Fred Isaman9549ec02011-07-30 20:52:53 -0400232 /* We've used up the previous extent */
Fred Isaman9549ec02011-07-30 20:52:53 -0400233 bio = bl_submit_bio(READ, bio);
Christoph Hellwig80672532014-09-10 08:23:34 -0700234
Fred Isaman9549ec02011-07-30 20:52:53 -0400235 /* Get the next one */
Christoph Hellwig80672532014-09-10 08:23:34 -0700236 if (!ext_tree_lookup(bl, isect, &be, false)) {
Fred Isamancd841602012-04-20 14:47:44 -0400237 header->pnfs_error = -EIO;
Fred Isaman9549ec02011-07-30 20:52:53 -0400238 goto out;
239 }
Christoph Hellwig80672532014-09-10 08:23:34 -0700240 extent_length = be.be_length - (isect - be.be_f_offset);
Fred Isaman9549ec02011-07-30 20:52:53 -0400241 }
Peng Taof742dc42012-08-24 00:27:52 +0800242
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700243 pg_offset = f_offset & ~PAGE_CACHE_MASK;
Peng Taof742dc42012-08-24 00:27:52 +0800244 if (is_dio) {
Peng Taof742dc42012-08-24 00:27:52 +0800245 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);
Christoph Hellwig921b81a2014-08-21 11:09:29 -0500253 extent_length -= (pg_offset >> SECTOR_SHIFT);
Peng Taof742dc42012-08-24 00:27:52 +0800254 } else {
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700255 BUG_ON(pg_offset != 0);
Peng Taof742dc42012-08-24 00:27:52 +0800256 pg_len = PAGE_CACHE_SIZE;
257 }
258
Christoph Hellwig80672532014-09-10 08:23:34 -0700259 if (is_hole(&be)) {
Fred Isaman9549ec02011-07-30 20:52:53 -0400260 bio = bl_submit_bio(READ, bio);
261 /* Fill hole w/ zeroes w/o accessing device */
262 dprintk("%s Zeroing page for hole\n", __func__);
Peng Taof742dc42012-08-24 00:27:52 +0800263 zero_user_segment(pages[i], pg_offset, pg_len);
Fred Isaman9549ec02011-07-30 20:52:53 -0400264 } else {
Weston Andros Adamson823b0c92014-06-09 11:48:34 -0400265 bio = do_add_page_to_bio(bio,
Christoph Hellwig80672532014-09-10 08:23:34 -0700266 header->page_array.npages - i,
Fred Isaman30dd3742012-04-20 14:47:45 -0400267 READ,
Christoph Hellwig80672532014-09-10 08:23:34 -0700268 isect, pages[i], &be,
Peng Taof742dc42012-08-24 00:27:52 +0800269 bl_end_io_read, par,
270 pg_offset, pg_len);
Fred Isaman9549ec02011-07-30 20:52:53 -0400271 if (IS_ERR(bio)) {
Fred Isamancd841602012-04-20 14:47:44 -0400272 header->pnfs_error = PTR_ERR(bio);
Peng Taoe6d05a72011-09-22 21:50:16 -0400273 bio = NULL;
Fred Isaman9549ec02011-07-30 20:52:53 -0400274 goto out;
275 }
276 }
Peng Taof742dc42012-08-24 00:27:52 +0800277 isect += (pg_len >> SECTOR_SHIFT);
Christoph Hellwig921b81a2014-08-21 11:09:29 -0500278 extent_length -= (pg_len >> SECTOR_SHIFT);
Fred Isaman9549ec02011-07-30 20:52:53 -0400279 }
Fred Isamancd841602012-04-20 14:47:44 -0400280 if ((isect << SECTOR_SHIFT) >= header->inode->i_size) {
Christoph Hellwig80672532014-09-10 08:23:34 -0700281 header->res.eof = 1;
282 header->res.count = header->inode->i_size - header->args.offset;
Fred Isaman9549ec02011-07-30 20:52:53 -0400283 } else {
Christoph Hellwig80672532014-09-10 08:23:34 -0700284 header->res.count = (isect << SECTOR_SHIFT) - header->args.offset;
Fred Isaman9549ec02011-07-30 20:52:53 -0400285 }
286out:
Fred Isaman9549ec02011-07-30 20:52:53 -0400287 bl_submit_bio(READ, bio);
Christoph Hellwigbe98fd02014-08-21 11:09:28 -0500288 blk_finish_plug(&plug);
Fred Isaman9549ec02011-07-30 20:52:53 -0400289 put_parallel(par);
290 return PNFS_ATTEMPTED;
Fred Isaman31e63062011-07-30 20:52:55 -0400291}
292
Fred Isaman650e2d32011-07-30 20:52:54 -0400293static void bl_end_io_write(struct bio *bio, int err)
Fred Isaman155e7522011-07-30 20:52:39 -0400294{
Fred Isaman650e2d32011-07-30 20:52:54 -0400295 struct parallel_io *par = bio->bi_private;
296 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400297 struct nfs_pgio_header *header = par->data;
Fred Isaman650e2d32011-07-30 20:52:54 -0400298
299 if (!uptodate) {
Fred Isamancd841602012-04-20 14:47:44 -0400300 if (!header->pnfs_error)
301 header->pnfs_error = -EIO;
302 pnfs_set_lo_fail(header->lseg);
Fred Isaman650e2d32011-07-30 20:52:54 -0400303 }
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{
Christoph Hellwig80672532014-09-10 08:23:34 -0700313 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
Fred Isaman650e2d32011-07-30 20:52:54 -0400317 dprintk("%s enter\n", __func__);
Christoph Hellwig80672532014-09-10 08:23:34 -0700318
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400319 if (likely(!hdr->pnfs_error)) {
Christoph Hellwig80672532014-09-10 08:23:34 -0700320 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);
Fred Isaman31e63062011-07-30 20:52:55 -0400327 }
Christoph Hellwig80672532014-09-10 08:23:34 -0700328
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400329 pnfs_ld_write_done(hdr);
Fred Isaman650e2d32011-07-30 20:52:54 -0400330}
331
332/* Called when last of bios associated with a bl_write_pagelist call finishes */
Christoph Hellwig80672532014-09-10 08:23:34 -0700333static void bl_end_par_io_write(void *data)
Fred Isaman650e2d32011-07-30 20:52:54 -0400334{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400335 struct nfs_pgio_header *hdr = data;
Fred Isaman650e2d32011-07-30 20:52:54 -0400336
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400337 hdr->task.tk_status = hdr->pnfs_error;
Weston Andros Adamsonc65e6252014-06-09 11:48:36 -0400338 hdr->verf.committed = NFS_FILE_SYNC;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400339 INIT_WORK(&hdr->task.u.tk_work, bl_write_cleanup);
340 schedule_work(&hdr->task.u.tk_work);
Fred Isaman650e2d32011-07-30 20:52:54 -0400341}
342
343static enum pnfs_try_status
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400344bl_write_pagelist(struct nfs_pgio_header *header, int sync)
Fred Isaman650e2d32011-07-30 20:52:54 -0400345{
Christoph Hellwig80672532014-09-10 08:23:34 -0700346 struct pnfs_block_layout *bl = BLK_LSEG2EXT(header->lseg);
Fred Isaman650e2d32011-07-30 20:52:54 -0400347 struct bio *bio = NULL;
Christoph Hellwig80672532014-09-10 08:23:34 -0700348 struct pnfs_block_extent be;
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700349 sector_t isect, extent_length = 0;
Peng Tao96c9eae2012-08-24 00:27:53 +0800350 struct parallel_io *par = NULL;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400351 loff_t offset = header->args.offset;
352 size_t count = header->args.count;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400353 struct page **pages = header->args.pages;
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700354 int pg_index = pg_index = header->args.pgbase >> PAGE_CACHE_SHIFT;
Christoph Hellwigbe98fd02014-08-21 11:09:28 -0500355 struct blk_plug plug;
Christoph Hellwig80672532014-09-10 08:23:34 -0700356 int i;
Fred Isaman650e2d32011-07-30 20:52:54 -0400357
358 dprintk("%s enter, %Zu@%lld\n", __func__, count, offset);
Peng Tao96c9eae2012-08-24 00:27:53 +0800359
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400360 /* At this point, header->page_aray is a (sequential) list of nfs_pages.
Peng Tao71cdd402011-07-30 20:52:56 -0400361 * We want to write each, and if there is an error set pnfs_error
362 * to have it redone using nfs.
Fred Isaman650e2d32011-07-30 20:52:54 -0400363 */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400364 par = alloc_parallel(header);
Fred Isaman650e2d32011-07-30 20:52:54 -0400365 if (!par)
Christoph Hellwig80672532014-09-10 08:23:34 -0700366 return PNFS_NOT_ATTEMPTED;
Fred Isaman650e2d32011-07-30 20:52:54 -0400367 par->pnfs_callback = bl_end_par_io_write;
Fred Isaman650e2d32011-07-30 20:52:54 -0400368
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700369 blk_start_plug(&plug);
Peng Tao71cdd402011-07-30 20:52:56 -0400370
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700371 /* we always write out the whole page */
372 offset = offset & (loff_t)PAGE_CACHE_MASK;
373 isect = offset >> SECTOR_SHIFT;
Peng Tao71cdd402011-07-30 20:52:56 -0400374
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400375 for (i = pg_index; i < header->page_array.npages; i++) {
Christoph Hellwig921b81a2014-08-21 11:09:29 -0500376 if (extent_length <= 0) {
Fred Isaman650e2d32011-07-30 20:52:54 -0400377 /* We've used up the previous extent */
Fred Isaman650e2d32011-07-30 20:52:54 -0400378 bio = bl_submit_bio(WRITE, bio);
379 /* Get the next one */
Christoph Hellwig80672532014-09-10 08:23:34 -0700380 if (!ext_tree_lookup(bl, isect, &be, true)) {
Fred Isamancd841602012-04-20 14:47:44 -0400381 header->pnfs_error = -EINVAL;
Fred Isaman650e2d32011-07-30 20:52:54 -0400382 goto out;
383 }
Peng Taofe6e1e82012-08-24 00:27:51 +0800384
Christoph Hellwig80672532014-09-10 08:23:34 -0700385 extent_length = be.be_length - (isect - be.be_f_offset);
Peng Tao71cdd402011-07-30 20:52:56 -0400386 }
Peng Taofe6e1e82012-08-24 00:27:51 +0800387
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400388 bio = do_add_page_to_bio(bio, header->page_array.npages - i,
Christoph Hellwig80672532014-09-10 08:23:34 -0700389 WRITE, isect, pages[i], &be,
Peng Taofe6e1e82012-08-24 00:27:51 +0800390 bl_end_io_write, par,
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700391 0, PAGE_CACHE_SIZE);
Peng Tao71cdd402011-07-30 20:52:56 -0400392 if (IS_ERR(bio)) {
Fred Isamancd841602012-04-20 14:47:44 -0400393 header->pnfs_error = PTR_ERR(bio);
Peng Taoe6d05a72011-09-22 21:50:16 -0400394 bio = NULL;
Peng Tao71cdd402011-07-30 20:52:56 -0400395 goto out;
Fred Isaman650e2d32011-07-30 20:52:54 -0400396 }
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700397 offset += PAGE_CACHE_SIZE;
398 count -= PAGE_CACHE_SIZE;
Fred Isaman650e2d32011-07-30 20:52:54 -0400399 isect += PAGE_CACHE_SECTORS;
400 extent_length -= PAGE_CACHE_SECTORS;
401 }
Peng Tao71cdd402011-07-30 20:52:56 -0400402
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400403 header->res.count = header->args.count;
Fred Isaman650e2d32011-07-30 20:52:54 -0400404out:
Fred Isaman650e2d32011-07-30 20:52:54 -0400405 bl_submit_bio(WRITE, bio);
Christoph Hellwigbe98fd02014-08-21 11:09:28 -0500406 blk_finish_plug(&plug);
Fred Isaman650e2d32011-07-30 20:52:54 -0400407 put_parallel(par);
408 return PNFS_ATTEMPTED;
Fred Isaman155e7522011-07-30 20:52:39 -0400409}
410
411static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
412{
413 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
Christoph Hellwig80672532014-09-10 08:23:34 -0700414 int err;
Fred Isaman155e7522011-07-30 20:52:39 -0400415
416 dprintk("%s enter\n", __func__);
Christoph Hellwig80672532014-09-10 08:23:34 -0700417
418 err = ext_tree_remove(bl, true, 0, LLONG_MAX);
419 WARN_ON(err);
420
Fred Isaman155e7522011-07-30 20:52:39 -0400421 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;
Christoph Hellwig80672532014-09-10 08:23:34 -0700433
434 bl->bl_ext_rw = RB_ROOT;
435 bl->bl_ext_ro = RB_ROOT;
Fred Isaman155e7522011-07-30 20:52:39 -0400436 spin_lock_init(&bl->bl_ext_lock);
Christoph Hellwig80672532014-09-10 08:23:34 -0700437
Fred Isaman155e7522011-07-30 20:52:39 -0400438 return &bl->bl_layout;
439}
440
Fred Isamana60d2eb2011-07-30 20:52:44 -0400441static void bl_free_lseg(struct pnfs_layout_segment *lseg)
Fred Isaman155e7522011-07-30 20:52:39 -0400442{
Fred Isamana60d2eb2011-07-30 20:52:44 -0400443 dprintk("%s enter\n", __func__);
444 kfree(lseg);
Fred Isaman155e7522011-07-30 20:52:39 -0400445}
446
Fred Isamana60d2eb2011-07-30 20:52:44 -0400447/* 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)
Fred Isaman155e7522011-07-30 20:52:39 -0400453{
Fred Isamana60d2eb2011-07-30 20:52:44 -0400454 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;
Fred Isaman155e7522011-07-30 20:52:39 -0400470}
471
472static void
473bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr,
474 const struct nfs4_layoutcommit_args *arg)
475{
Fred Isaman90ace122011-07-30 20:52:51 -0400476 dprintk("%s enter\n", __func__);
Christoph Hellwig80672532014-09-10 08:23:34 -0700477 ext_tree_encode_commit(BLK_LO2EXT(lo), xdr);
Fred Isaman155e7522011-07-30 20:52:39 -0400478}
479
480static void
481bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
482{
Fred Isamanb2be7812011-07-30 20:52:52 -0400483 struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout;
484
485 dprintk("%s enter\n", __func__);
Christoph Hellwig80672532014-09-10 08:23:34 -0700486 ext_tree_mark_committed(BLK_LO2EXT(lo), lcdata->res.status);
Fred Isaman155e7522011-07-30 20:52:39 -0400487}
488
Fred Isaman2f9fd182011-07-30 20:52:46 -0400489static void free_blk_mountid(struct block_mount_id *mid)
490{
491 if (mid) {
Peng Tao93a38442012-01-12 23:18:47 +0800492 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) {
Fred Isaman2f9fd182011-07-30 20:52:46 -0400496 list_del(&dev->bm_node);
497 bl_free_block_dev(dev);
498 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400499 kfree(mid);
500 }
501}
502
Trond Myklebust78e4e052012-09-18 21:02:29 -0400503/* This is mostly copied from the filelayout_get_device_info function.
Fred Isaman2f9fd182011-07-30 20:52:46 -0400504 * 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;
Jim Rees516f2e22011-09-22 21:50:08 -0400511 struct pnfs_block_dev *rv;
Fred Isaman2f9fd182011-07-30 20:52:46 -0400512 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;
Jim Rees10bd295a2012-04-09 22:33:39 -0400522 max_pages = nfs_page_array_len(0, max_resp_sz);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400523 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__);
Jim Rees516f2e22011-09-22 21:50:08 -0400529 return ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400530 }
531
Fabian Frederickf15b5042014-06-25 20:26:21 +0200532 pages = kcalloc(max_pages, sizeof(struct page *), GFP_NOFS);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400533 if (pages == NULL) {
534 kfree(dev);
Jim Rees516f2e22011-09-22 21:50:08 -0400535 return ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400536 }
537 for (i = 0; i < max_pages; i++) {
538 pages[i] = alloc_page(GFP_NOFS);
Jim Rees516f2e22011-09-22 21:50:08 -0400539 if (!pages[i]) {
540 rv = ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400541 goto out_free;
Jim Rees516f2e22011-09-22 21:50:08 -0400542 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400543 }
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;
Andy Adamson968fe252013-06-25 19:02:54 -0400551 dev->maxcount = max_resp_sz - nfs41_maxgetdevinfo_overhead;
Fred Isaman2f9fd182011-07-30 20:52:46 -0400552
553 dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data);
Trond Myklebustcd5875f2013-05-20 11:42:54 -0400554 rc = nfs4_proc_getdeviceinfo(server, dev, NULL);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400555 dprintk("%s getdevice info returns %d\n", __func__, rc);
Jim Rees516f2e22011-09-22 21:50:08 -0400556 if (rc) {
557 rv = ERR_PTR(rc);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400558 goto out_free;
Jim Rees516f2e22011-09-22 21:50:08 -0400559 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400560
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
Fred Isaman155e7522011-07-30 20:52:39 -0400570static int
571bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
572{
Fred Isaman2f9fd182011-07-30 20:52:46 -0400573 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);
Jim Rees516f2e22011-09-22 21:50:08 -0400577 int status, i;
Fred Isaman2f9fd182011-07-30 20:52:46 -0400578
Fred Isaman155e7522011-07-30 20:52:39 -0400579 dprintk("%s enter\n", __func__);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400580
581 if (server->pnfs_blksize == 0) {
582 dprintk("%s Server did not return blksize\n", __func__);
583 return -EINVAL;
584 }
Christoph Hellwige3aaf7f2014-08-21 11:09:26 -0500585 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
Fred Isaman2f9fd182011-07-30 20:52:46 -0400591 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]);
Jim Rees516f2e22011-09-22 21:50:08 -0400615 if (IS_ERR(bdev)) {
616 status = PTR_ERR(bdev);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400617 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;
Fred Isaman155e7522011-07-30 20:52:39 -0400634}
635
636static int
637bl_clear_layoutdriver(struct nfs_server *server)
638{
Fred Isaman2f9fd182011-07-30 20:52:46 -0400639 struct block_mount_id *b_mt_id = server->pnfs_ld_data;
640
Fred Isaman155e7522011-07-30 20:52:39 -0400641 dprintk("%s enter\n", __func__);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400642 free_blk_mountid(b_mt_id);
643 dprintk("%s RETURNS\n", __func__);
Fred Isaman155e7522011-07-30 20:52:39 -0400644 return 0;
645}
646
Peng Taof742dc42012-08-24 00:27:52 +0800647static bool
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700648is_aligned_req(struct nfs_pageio_descriptor *pgio,
649 struct nfs_page *req, unsigned int alignment)
Peng Taof742dc42012-08-24 00:27:52 +0800650{
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700651 /*
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;
Peng Taof742dc42012-08-24 00:27:52 +0800676}
677
678static void
679bl_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
680{
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700681 if (!is_aligned_req(pgio, req, SECTOR_SIZE)) {
Peng Taof742dc42012-08-24 00:27:52 +0800682 nfs_pageio_reset_read_mds(pgio);
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700683 return;
684 }
685
686 pnfs_generic_pg_init_read(pgio, req);
Peng Taof742dc42012-08-24 00:27:52 +0800687}
688
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400689/*
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
Peng Taof742dc42012-08-24 00:27:52 +0800694bl_pg_test_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
695 struct nfs_page *req)
696{
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700697 if (!is_aligned_req(pgio, req, SECTOR_SIZE))
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400698 return 0;
Peng Taof742dc42012-08-24 00:27:52 +0800699 return pnfs_generic_pg_test(pgio, prev, req);
700}
701
Peng Tao62965562012-09-25 14:55:57 +0800702/*
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();
Johannes Weinere7b563b2014-04-03 14:47:44 -0700715 end = page_cache_next_hole(mapping, idx + 1, ULONG_MAX);
Peng Tao62965562012-09-25 14:55:57 +0800716 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
Trond Myklebust6f018ef2012-10-02 08:29:14 -0700725static void
Peng Tao96c9eae2012-08-24 00:27:53 +0800726bl_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
727{
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700728 u64 wb_size;
Peng Tao62965562012-09-25 14:55:57 +0800729
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700730 if (!is_aligned_req(pgio, req, PAGE_SIZE)) {
731 nfs_pageio_reset_write_mds(pgio);
732 return;
Peng Tao62965562012-09-25 14:55:57 +0800733 }
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700734
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);
Peng Tao96c9eae2012-08-24 00:27:53 +0800742}
743
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400744/*
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
Peng Tao96c9eae2012-08-24 00:27:53 +0800749bl_pg_test_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
750 struct nfs_page *req)
751{
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700752 if (!is_aligned_req(pgio, req, PAGE_SIZE))
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400753 return 0;
Peng Tao96c9eae2012-08-24 00:27:53 +0800754 return pnfs_generic_pg_test(pgio, prev, req);
755}
756
Benny Halevye9643fe2011-07-30 20:52:40 -0400757static const struct nfs_pageio_ops bl_pg_read_ops = {
Peng Taof742dc42012-08-24 00:27:52 +0800758 .pg_init = bl_pg_init_read,
759 .pg_test = bl_pg_test_read,
Benny Halevye9643fe2011-07-30 20:52:40 -0400760 .pg_doio = pnfs_generic_pg_readpages,
761};
762
763static const struct nfs_pageio_ops bl_pg_write_ops = {
Peng Tao96c9eae2012-08-24 00:27:53 +0800764 .pg_init = bl_pg_init_write,
765 .pg_test = bl_pg_test_write,
Benny Halevye9643fe2011-07-30 20:52:40 -0400766 .pg_doio = pnfs_generic_pg_writepages,
767};
768
Fred Isaman155e7522011-07-30 20:52:39 -0400769static struct pnfs_layoutdriver_type blocklayout_type = {
770 .id = LAYOUT_BLOCK_VOLUME,
771 .name = "LAYOUT_BLOCK_VOLUME",
fanchaoting5a12cca2013-02-04 21:15:02 +0800772 .owner = THIS_MODULE,
Christoph Hellwig3a6fd1f2014-09-10 08:23:32 -0700773 .flags = PNFS_READ_WHOLE_PAGE,
Fred Isaman155e7522011-07-30 20:52:39 -0400774 .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,
Benny Halevye9643fe2011-07-30 20:52:40 -0400784 .pg_read_ops = &bl_pg_read_ops,
785 .pg_write_ops = &bl_pg_write_ops,
Fred Isaman155e7522011-07-30 20:52:39 -0400786};
787
Jim Reesfe0a9b72011-07-30 20:52:42 -0400788static const struct rpc_pipe_ops bl_upcall_ops = {
Peng Taoc1225152011-09-22 21:50:10 -0400789 .upcall = rpc_pipe_generic_upcall,
Jim Reesfe0a9b72011-07-30 20:52:42 -0400790 .downcall = bl_pipe_downcall,
791 .destroy_msg = bl_pipe_destroy_msg,
792};
793
Stanislav Kinsbursky332dfab2012-01-10 17:04:16 +0400794static 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
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +0400814static 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
Stanislav Kinsbursky332dfab2012-01-10 17:04:16 +0400856static 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)
Stanislav Kinsbursky2561d612012-01-10 17:04:40 +0400864 return NULL;
Stanislav Kinsbursky332dfab2012-01-10 17:04:16 +0400865 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
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +0400882static int nfs4blocklayout_net_init(struct net *net)
883{
884 struct nfs_net *nn = net_generic(net, nfs_net_id);
885 struct dentry *dentry;
886
Stanislav Kinsbursky5ffaf852012-03-11 18:20:31 +0400887 init_waitqueue_head(&nn->bl_wq);
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +0400888 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
Fred Isaman155e7522011-07-30 20:52:39 -0400914static 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);
Jim Reesfe0a9b72011-07-30 20:52:42 -0400921 if (ret)
922 goto out;
923
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +0400924 ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block);
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +0400925 if (ret)
926 goto out_remove;
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +0400927 ret = register_pernet_subsys(&nfs4blocklayout_net_ops);
928 if (ret)
929 goto out_notifier;
Jim Reesfe0a9b72011-07-30 20:52:42 -0400930out:
931 return ret;
932
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +0400933out_notifier:
934 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
Jim Reesfe0a9b72011-07-30 20:52:42 -0400935out_remove:
936 pnfs_unregister_layoutdriver(&blocklayout_type);
Fred Isaman155e7522011-07-30 20:52:39 -0400937 return ret;
938}
939
940static void __exit nfs4blocklayout_exit(void)
941{
942 dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
943 __func__);
944
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +0400945 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +0400946 unregister_pernet_subsys(&nfs4blocklayout_net_ops);
Fred Isaman155e7522011-07-30 20:52:39 -0400947 pnfs_unregister_layoutdriver(&blocklayout_type);
Fred Isaman155e7522011-07-30 20:52:39 -0400948}
949
950MODULE_ALIAS("nfs-layouttype4-3");
951
952module_init(nfs4blocklayout_init);
953module_exit(nfs4blocklayout_exit);