blob: 9c94297bb70e9502c40825249eecb24093e142d2 [file] [log] [blame]
Fred Isaman155e7522011-07-30 20:52:39 -04001/*
2 * linux/fs/nfs/blocklayout/blocklayout.c
3 *
4 * Module for the NFSv4.1 pNFS block layout driver.
5 *
6 * Copyright (c) 2006 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Andy Adamson <andros@citi.umich.edu>
10 * Fred Isaman <iisaman@umich.edu>
11 *
12 * permission is granted to use, copy, create derivative works and
13 * redistribute this software and such derivative works for any purpose,
14 * so long as the name of the university of michigan is not used in
15 * any advertising or publicity pertaining to the use or distribution
16 * of this software without specific, written prior authorization. if
17 * the above copyright notice or any other identification of the
18 * university of michigan is included in any copy of any portion of
19 * this software, then the disclaimer below must also be included.
20 *
21 * this software is provided as is, without representation from the
22 * university of michigan as to its fitness for any purpose, and without
23 * warranty by the university of michigan of any kind, either express
24 * or implied, including without limitation the implied warranties of
25 * merchantability and fitness for a particular purpose. the regents
26 * of the university of michigan shall not be liable for any damages,
27 * including special, indirect, incidental, or consequential damages,
28 * with respect to any claim arising out or in connection with the use
29 * of the software, even if it has been or is hereafter advised of the
30 * possibility of such damages.
31 */
Fred Isaman9549ec02011-07-30 20:52:53 -040032
Fred Isaman155e7522011-07-30 20:52:39 -040033#include <linux/module.h>
34#include <linux/init.h>
Jim Reesfe0a9b72011-07-30 20:52:42 -040035#include <linux/mount.h>
36#include <linux/namei.h>
Fred Isaman9549ec02011-07-30 20:52:53 -040037#include <linux/bio.h> /* struct bio */
Peng Tao71cdd402011-07-30 20:52:56 -040038#include <linux/buffer_head.h> /* various write calls */
Heiko Carstens88c9e422011-08-02 09:57:35 +020039#include <linux/prefetch.h>
Fred Isaman155e7522011-07-30 20:52:39 -040040
41#include "blocklayout.h"
42
43#define NFSDBG_FACILITY NFSDBG_PNFS_LD
44
45MODULE_LICENSE("GPL");
46MODULE_AUTHOR("Andy Adamson <andros@citi.umich.edu>");
47MODULE_DESCRIPTION("The NFSv4.1 pNFS Block layout driver");
48
Fred Isaman9549ec02011-07-30 20:52:53 -040049static void print_page(struct page *page)
50{
51 dprintk("PRINTPAGE page %p\n", page);
52 dprintk(" PagePrivate %d\n", PagePrivate(page));
53 dprintk(" PageUptodate %d\n", PageUptodate(page));
54 dprintk(" PageError %d\n", PageError(page));
55 dprintk(" PageDirty %d\n", PageDirty(page));
56 dprintk(" PageReferenced %d\n", PageReferenced(page));
57 dprintk(" PageLocked %d\n", PageLocked(page));
58 dprintk(" PageWriteback %d\n", PageWriteback(page));
59 dprintk(" PageMappedToDisk %d\n", PageMappedToDisk(page));
60 dprintk("\n");
61}
62
63/* Given the be associated with isect, determine if page data needs to be
64 * initialized.
65 */
66static int is_hole(struct pnfs_block_extent *be, sector_t isect)
67{
68 if (be->be_state == PNFS_BLOCK_NONE_DATA)
69 return 1;
70 else if (be->be_state != PNFS_BLOCK_INVALID_DATA)
71 return 0;
72 else
73 return !bl_is_sector_init(be->be_inval, isect);
74}
75
Fred Isaman650e2d32011-07-30 20:52:54 -040076/* Given the be associated with isect, determine if page data can be
77 * written to disk.
78 */
79static int is_writable(struct pnfs_block_extent *be, sector_t isect)
80{
Peng Tao71cdd402011-07-30 20:52:56 -040081 return (be->be_state == PNFS_BLOCK_READWRITE_DATA ||
82 be->be_state == PNFS_BLOCK_INVALID_DATA);
Fred Isaman650e2d32011-07-30 20:52:54 -040083}
84
Fred Isaman9549ec02011-07-30 20:52:53 -040085/* The data we are handed might be spread across several bios. We need
86 * to track when the last one is finished.
87 */
88struct parallel_io {
89 struct kref refcnt;
Peng Tao7c5465d2012-01-12 23:18:46 +080090 void (*pnfs_callback) (void *data, int num_se);
Fred Isaman9549ec02011-07-30 20:52:53 -040091 void *data;
Peng Tao7c5465d2012-01-12 23:18:46 +080092 int bse_count;
Fred Isaman9549ec02011-07-30 20:52:53 -040093};
94
95static inline struct parallel_io *alloc_parallel(void *data)
96{
97 struct parallel_io *rv;
98
99 rv = kmalloc(sizeof(*rv), GFP_NOFS);
100 if (rv) {
101 rv->data = data;
102 kref_init(&rv->refcnt);
Peng Tao7c5465d2012-01-12 23:18:46 +0800103 rv->bse_count = 0;
Fred Isaman9549ec02011-07-30 20:52:53 -0400104 }
105 return rv;
106}
107
108static inline void get_parallel(struct parallel_io *p)
109{
110 kref_get(&p->refcnt);
111}
112
113static void destroy_parallel(struct kref *kref)
114{
115 struct parallel_io *p = container_of(kref, struct parallel_io, refcnt);
116
117 dprintk("%s enter\n", __func__);
Peng Tao7c5465d2012-01-12 23:18:46 +0800118 p->pnfs_callback(p->data, p->bse_count);
Fred Isaman9549ec02011-07-30 20:52:53 -0400119 kfree(p);
120}
121
122static inline void put_parallel(struct parallel_io *p)
123{
124 kref_put(&p->refcnt, destroy_parallel);
125}
126
127static struct bio *
128bl_submit_bio(int rw, struct bio *bio)
129{
130 if (bio) {
131 get_parallel(bio->bi_private);
132 dprintk("%s submitting %s bio %u@%llu\n", __func__,
133 rw == READ ? "read" : "write",
134 bio->bi_size, (unsigned long long)bio->bi_sector);
135 submit_bio(rw, bio);
136 }
137 return NULL;
138}
139
140static struct bio *bl_alloc_init_bio(int npg, sector_t isect,
141 struct pnfs_block_extent *be,
142 void (*end_io)(struct bio *, int err),
143 struct parallel_io *par)
144{
145 struct bio *bio;
146
Peng Tao74a6eeb2012-01-12 23:18:48 +0800147 npg = min(npg, BIO_MAX_PAGES);
Fred Isaman9549ec02011-07-30 20:52:53 -0400148 bio = bio_alloc(GFP_NOIO, npg);
Peng Tao74a6eeb2012-01-12 23:18:48 +0800149 if (!bio && (current->flags & PF_MEMALLOC)) {
150 while (!bio && (npg /= 2))
151 bio = bio_alloc(GFP_NOIO, npg);
152 }
Fred Isaman9549ec02011-07-30 20:52:53 -0400153
Peng Tao74a6eeb2012-01-12 23:18:48 +0800154 if (bio) {
155 bio->bi_sector = isect - be->be_f_offset + be->be_v_offset;
156 bio->bi_bdev = be->be_mdev;
157 bio->bi_end_io = end_io;
158 bio->bi_private = par;
159 }
Fred Isaman9549ec02011-07-30 20:52:53 -0400160 return bio;
161}
162
163static struct bio *bl_add_page_to_bio(struct bio *bio, int npg, int rw,
164 sector_t isect, struct page *page,
165 struct pnfs_block_extent *be,
166 void (*end_io)(struct bio *, int err),
167 struct parallel_io *par)
168{
169retry:
170 if (!bio) {
171 bio = bl_alloc_init_bio(npg, isect, be, end_io, par);
172 if (!bio)
173 return ERR_PTR(-ENOMEM);
174 }
175 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
176 bio = bl_submit_bio(rw, bio);
177 goto retry;
178 }
179 return bio;
180}
181
Fred Isaman9549ec02011-07-30 20:52:53 -0400182/* This is basically copied from mpage_end_io_read */
183static void bl_end_io_read(struct bio *bio, int err)
184{
185 struct parallel_io *par = bio->bi_private;
186 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
187 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
188 struct nfs_read_data *rdata = (struct nfs_read_data *)par->data;
189
190 do {
191 struct page *page = bvec->bv_page;
192
193 if (--bvec >= bio->bi_io_vec)
194 prefetchw(&bvec->bv_page->flags);
195 if (uptodate)
196 SetPageUptodate(page);
197 } while (bvec >= bio->bi_io_vec);
198 if (!uptodate) {
199 if (!rdata->pnfs_error)
200 rdata->pnfs_error = -EIO;
Peng Tao1b0ae062011-09-22 21:50:12 -0400201 pnfs_set_lo_fail(rdata->lseg);
Fred Isaman9549ec02011-07-30 20:52:53 -0400202 }
203 bio_put(bio);
204 put_parallel(par);
205}
206
207static void bl_read_cleanup(struct work_struct *work)
208{
209 struct rpc_task *task;
210 struct nfs_read_data *rdata;
211 dprintk("%s enter\n", __func__);
212 task = container_of(work, struct rpc_task, u.tk_work);
213 rdata = container_of(task, struct nfs_read_data, task);
214 pnfs_ld_read_done(rdata);
215}
216
217static void
Peng Tao7c5465d2012-01-12 23:18:46 +0800218bl_end_par_io_read(void *data, int unused)
Fred Isaman9549ec02011-07-30 20:52:53 -0400219{
220 struct nfs_read_data *rdata = data;
221
Peng Tao82b906d2012-01-12 23:18:43 +0800222 rdata->task.tk_status = rdata->pnfs_error;
Fred Isaman9549ec02011-07-30 20:52:53 -0400223 INIT_WORK(&rdata->task.u.tk_work, bl_read_cleanup);
224 schedule_work(&rdata->task.u.tk_work);
225}
226
Fred Isaman155e7522011-07-30 20:52:39 -0400227static enum pnfs_try_status
228bl_read_pagelist(struct nfs_read_data *rdata)
229{
Fred Isaman9549ec02011-07-30 20:52:53 -0400230 int i, hole;
231 struct bio *bio = NULL;
232 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
233 sector_t isect, extent_length = 0;
234 struct parallel_io *par;
235 loff_t f_offset = rdata->args.offset;
Fred Isaman9549ec02011-07-30 20:52:53 -0400236 struct page **pages = rdata->args.pages;
237 int pg_index = rdata->args.pgbase >> PAGE_CACHE_SHIFT;
238
Trond Myklebust6f008662012-03-20 14:12:46 -0400239 dprintk("%s enter nr_pages %u offset %lld count %u\n", __func__,
240 rdata->npages, f_offset, (unsigned int)rdata->args.count);
Fred Isaman9549ec02011-07-30 20:52:53 -0400241
242 par = alloc_parallel(rdata);
243 if (!par)
244 goto use_mds;
Fred Isaman9549ec02011-07-30 20:52:53 -0400245 par->pnfs_callback = bl_end_par_io_read;
246 /* At this point, we can no longer jump to use_mds */
247
248 isect = (sector_t) (f_offset >> SECTOR_SHIFT);
249 /* Code assumes extents are page-aligned */
250 for (i = pg_index; i < rdata->npages; i++) {
251 if (!extent_length) {
252 /* We've used up the previous extent */
253 bl_put_extent(be);
254 bl_put_extent(cow_read);
255 bio = bl_submit_bio(READ, bio);
256 /* Get the next one */
257 be = bl_find_get_extent(BLK_LSEG2EXT(rdata->lseg),
258 isect, &cow_read);
259 if (!be) {
260 rdata->pnfs_error = -EIO;
261 goto out;
262 }
263 extent_length = be->be_length -
264 (isect - be->be_f_offset);
265 if (cow_read) {
266 sector_t cow_length = cow_read->be_length -
267 (isect - cow_read->be_f_offset);
268 extent_length = min(extent_length, cow_length);
269 }
270 }
271 hole = is_hole(be, isect);
272 if (hole && !cow_read) {
273 bio = bl_submit_bio(READ, bio);
274 /* Fill hole w/ zeroes w/o accessing device */
275 dprintk("%s Zeroing page for hole\n", __func__);
276 zero_user_segment(pages[i], 0, PAGE_CACHE_SIZE);
277 print_page(pages[i]);
278 SetPageUptodate(pages[i]);
279 } else {
280 struct pnfs_block_extent *be_read;
281
282 be_read = (hole && cow_read) ? cow_read : be;
283 bio = bl_add_page_to_bio(bio, rdata->npages - i, READ,
284 isect, pages[i], be_read,
285 bl_end_io_read, par);
286 if (IS_ERR(bio)) {
287 rdata->pnfs_error = PTR_ERR(bio);
Peng Taoe6d05a72011-09-22 21:50:16 -0400288 bio = NULL;
Fred Isaman9549ec02011-07-30 20:52:53 -0400289 goto out;
290 }
291 }
292 isect += PAGE_CACHE_SECTORS;
293 extent_length -= PAGE_CACHE_SECTORS;
294 }
295 if ((isect << SECTOR_SHIFT) >= rdata->inode->i_size) {
296 rdata->res.eof = 1;
297 rdata->res.count = rdata->inode->i_size - f_offset;
298 } else {
299 rdata->res.count = (isect << SECTOR_SHIFT) - f_offset;
300 }
301out:
302 bl_put_extent(be);
303 bl_put_extent(cow_read);
304 bl_submit_bio(READ, bio);
305 put_parallel(par);
306 return PNFS_ATTEMPTED;
307
308 use_mds:
309 dprintk("Giving up and using normal NFS\n");
Fred Isaman155e7522011-07-30 20:52:39 -0400310 return PNFS_NOT_ATTEMPTED;
311}
312
Fred Isaman31e63062011-07-30 20:52:55 -0400313static void mark_extents_written(struct pnfs_block_layout *bl,
314 __u64 offset, __u32 count)
315{
316 sector_t isect, end;
317 struct pnfs_block_extent *be;
Peng Tao7c5465d2012-01-12 23:18:46 +0800318 struct pnfs_block_short_extent *se;
Fred Isaman31e63062011-07-30 20:52:55 -0400319
320 dprintk("%s(%llu, %u)\n", __func__, offset, count);
321 if (count == 0)
322 return;
323 isect = (offset & (long)(PAGE_CACHE_MASK)) >> SECTOR_SHIFT;
324 end = (offset + count + PAGE_CACHE_SIZE - 1) & (long)(PAGE_CACHE_MASK);
325 end >>= SECTOR_SHIFT;
326 while (isect < end) {
327 sector_t len;
328 be = bl_find_get_extent(bl, isect, NULL);
329 BUG_ON(!be); /* FIXME */
330 len = min(end, be->be_f_offset + be->be_length) - isect;
Peng Tao7c5465d2012-01-12 23:18:46 +0800331 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
332 se = bl_pop_one_short_extent(be->be_inval);
333 BUG_ON(!se);
334 bl_mark_for_commit(be, isect, len, se);
335 }
Fred Isaman31e63062011-07-30 20:52:55 -0400336 isect += len;
337 bl_put_extent(be);
338 }
339}
340
Peng Tao71cdd402011-07-30 20:52:56 -0400341static void bl_end_io_write_zero(struct bio *bio, int err)
342{
343 struct parallel_io *par = bio->bi_private;
344 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
345 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
346 struct nfs_write_data *wdata = (struct nfs_write_data *)par->data;
347
348 do {
349 struct page *page = bvec->bv_page;
350
351 if (--bvec >= bio->bi_io_vec)
352 prefetchw(&bvec->bv_page->flags);
353 /* This is the zeroing page we added */
354 end_page_writeback(page);
355 page_cache_release(page);
356 } while (bvec >= bio->bi_io_vec);
Peng Tao7c5465d2012-01-12 23:18:46 +0800357
358 if (unlikely(!uptodate)) {
Peng Tao71cdd402011-07-30 20:52:56 -0400359 if (!wdata->pnfs_error)
360 wdata->pnfs_error = -EIO;
Peng Tao1b0ae062011-09-22 21:50:12 -0400361 pnfs_set_lo_fail(wdata->lseg);
Peng Tao71cdd402011-07-30 20:52:56 -0400362 }
363 bio_put(bio);
364 put_parallel(par);
365}
366
Fred Isaman650e2d32011-07-30 20:52:54 -0400367static void bl_end_io_write(struct bio *bio, int err)
Fred Isaman155e7522011-07-30 20:52:39 -0400368{
Fred Isaman650e2d32011-07-30 20:52:54 -0400369 struct parallel_io *par = bio->bi_private;
370 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
371 struct nfs_write_data *wdata = (struct nfs_write_data *)par->data;
372
373 if (!uptodate) {
374 if (!wdata->pnfs_error)
375 wdata->pnfs_error = -EIO;
Peng Tao1b0ae062011-09-22 21:50:12 -0400376 pnfs_set_lo_fail(wdata->lseg);
Fred Isaman650e2d32011-07-30 20:52:54 -0400377 }
378 bio_put(bio);
379 put_parallel(par);
380}
381
382/* Function scheduled for call during bl_end_par_io_write,
383 * it marks sectors as written and extends the commitlist.
384 */
385static void bl_write_cleanup(struct work_struct *work)
386{
387 struct rpc_task *task;
388 struct nfs_write_data *wdata;
389 dprintk("%s enter\n", __func__);
390 task = container_of(work, struct rpc_task, u.tk_work);
391 wdata = container_of(task, struct nfs_write_data, task);
Peng Tao7c5465d2012-01-12 23:18:46 +0800392 if (likely(!wdata->pnfs_error)) {
Fred Isaman31e63062011-07-30 20:52:55 -0400393 /* Marks for LAYOUTCOMMIT */
Fred Isaman31e63062011-07-30 20:52:55 -0400394 mark_extents_written(BLK_LSEG2EXT(wdata->lseg),
395 wdata->args.offset, wdata->args.count);
396 }
Fred Isaman650e2d32011-07-30 20:52:54 -0400397 pnfs_ld_write_done(wdata);
398}
399
400/* Called when last of bios associated with a bl_write_pagelist call finishes */
Peng Tao7c5465d2012-01-12 23:18:46 +0800401static void bl_end_par_io_write(void *data, int num_se)
Fred Isaman650e2d32011-07-30 20:52:54 -0400402{
403 struct nfs_write_data *wdata = data;
404
Peng Tao7c5465d2012-01-12 23:18:46 +0800405 if (unlikely(wdata->pnfs_error)) {
406 bl_free_short_extents(&BLK_LSEG2EXT(wdata->lseg)->bl_inval,
407 num_se);
408 }
409
Peng Tao82b906d2012-01-12 23:18:43 +0800410 wdata->task.tk_status = wdata->pnfs_error;
Fred Isaman650e2d32011-07-30 20:52:54 -0400411 wdata->verf.committed = NFS_FILE_SYNC;
412 INIT_WORK(&wdata->task.u.tk_work, bl_write_cleanup);
413 schedule_work(&wdata->task.u.tk_work);
414}
415
Peng Tao71cdd402011-07-30 20:52:56 -0400416/* FIXME STUB - mark intersection of layout and page as bad, so is not
417 * used again.
418 */
419static void mark_bad_read(void)
420{
421 return;
422}
423
424/*
425 * map_block: map a requested I/0 block (isect) into an offset in the LVM
426 * block_device
427 */
428static void
429map_block(struct buffer_head *bh, sector_t isect, struct pnfs_block_extent *be)
430{
431 dprintk("%s enter be=%p\n", __func__, be);
432
433 set_buffer_mapped(bh);
434 bh->b_bdev = be->be_mdev;
435 bh->b_blocknr = (isect - be->be_f_offset + be->be_v_offset) >>
436 (be->be_mdev->bd_inode->i_blkbits - SECTOR_SHIFT);
437
438 dprintk("%s isect %llu, bh->b_blocknr %ld, using bsize %Zd\n",
439 __func__, (unsigned long long)isect, (long)bh->b_blocknr,
440 bh->b_size);
441 return;
442}
443
444/* Given an unmapped page, zero it or read in page for COW, page is locked
445 * by caller.
446 */
447static int
448init_page_for_write(struct page *page, struct pnfs_block_extent *cow_read)
449{
450 struct buffer_head *bh = NULL;
451 int ret = 0;
452 sector_t isect;
453
454 dprintk("%s enter, %p\n", __func__, page);
455 BUG_ON(PageUptodate(page));
456 if (!cow_read) {
457 zero_user_segment(page, 0, PAGE_SIZE);
458 SetPageUptodate(page);
459 goto cleanup;
460 }
461
462 bh = alloc_page_buffers(page, PAGE_CACHE_SIZE, 0);
463 if (!bh) {
464 ret = -ENOMEM;
465 goto cleanup;
466 }
467
468 isect = (sector_t) page->index << PAGE_CACHE_SECTOR_SHIFT;
469 map_block(bh, isect, cow_read);
470 if (!bh_uptodate_or_lock(bh))
471 ret = bh_submit_read(bh);
472 if (ret)
473 goto cleanup;
474 SetPageUptodate(page);
475
476cleanup:
477 bl_put_extent(cow_read);
478 if (bh)
479 free_buffer_head(bh);
480 if (ret) {
481 /* Need to mark layout with bad read...should now
482 * just use nfs4 for reads and writes.
483 */
484 mark_bad_read();
485 }
486 return ret;
487}
488
Peng Tao72c50882012-01-12 23:18:42 +0800489/* Find or create a zeroing page marked being writeback.
490 * Return ERR_PTR on error, NULL to indicate skip this page and page itself
491 * to indicate write out.
492 */
493static struct page *
494bl_find_get_zeroing_page(struct inode *inode, pgoff_t index,
495 struct pnfs_block_extent *cow_read)
496{
497 struct page *page;
498 int locked = 0;
499 page = find_get_page(inode->i_mapping, index);
500 if (page)
501 goto check_page;
502
503 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
504 if (unlikely(!page)) {
505 dprintk("%s oom\n", __func__);
506 return ERR_PTR(-ENOMEM);
507 }
508 locked = 1;
509
510check_page:
511 /* PageDirty: Other will write this out
512 * PageWriteback: Other is writing this out
513 * PageUptodate: It was read before
514 */
515 if (PageDirty(page) || PageWriteback(page)) {
516 print_page(page);
517 if (locked)
518 unlock_page(page);
519 page_cache_release(page);
520 return NULL;
521 }
522
523 if (!locked) {
524 lock_page(page);
525 locked = 1;
526 goto check_page;
527 }
528 if (!PageUptodate(page)) {
529 /* New page, readin or zero it */
530 init_page_for_write(page, cow_read);
531 }
532 set_page_writeback(page);
533 unlock_page(page);
534
535 return page;
536}
537
Fred Isaman650e2d32011-07-30 20:52:54 -0400538static enum pnfs_try_status
539bl_write_pagelist(struct nfs_write_data *wdata, int sync)
540{
Peng Tao71cdd402011-07-30 20:52:56 -0400541 int i, ret, npg_zero, pg_index, last = 0;
Fred Isaman650e2d32011-07-30 20:52:54 -0400542 struct bio *bio = NULL;
Peng Tao71cdd402011-07-30 20:52:56 -0400543 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
544 sector_t isect, last_isect = 0, extent_length = 0;
Fred Isaman650e2d32011-07-30 20:52:54 -0400545 struct parallel_io *par;
546 loff_t offset = wdata->args.offset;
547 size_t count = wdata->args.count;
548 struct page **pages = wdata->args.pages;
Peng Tao71cdd402011-07-30 20:52:56 -0400549 struct page *page;
550 pgoff_t index;
551 u64 temp;
552 int npg_per_block =
553 NFS_SERVER(wdata->inode)->pnfs_blksize >> PAGE_CACHE_SHIFT;
Fred Isaman650e2d32011-07-30 20:52:54 -0400554
555 dprintk("%s enter, %Zu@%lld\n", __func__, count, offset);
556 /* At this point, wdata->pages is a (sequential) list of nfs_pages.
Peng Tao71cdd402011-07-30 20:52:56 -0400557 * We want to write each, and if there is an error set pnfs_error
558 * to have it redone using nfs.
Fred Isaman650e2d32011-07-30 20:52:54 -0400559 */
560 par = alloc_parallel(wdata);
561 if (!par)
Peng Tao7c5465d2012-01-12 23:18:46 +0800562 goto out_mds;
Fred Isaman650e2d32011-07-30 20:52:54 -0400563 par->pnfs_callback = bl_end_par_io_write;
564 /* At this point, have to be more careful with error handling */
565
566 isect = (sector_t) ((offset & (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
Peng Tao71cdd402011-07-30 20:52:56 -0400567 be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg), isect, &cow_read);
568 if (!be || !is_writable(be, isect)) {
569 dprintk("%s no matching extents!\n", __func__);
Peng Tao7c5465d2012-01-12 23:18:46 +0800570 goto out_mds;
Peng Tao71cdd402011-07-30 20:52:56 -0400571 }
572
573 /* First page inside INVALID extent */
574 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
Peng Tao7c5465d2012-01-12 23:18:46 +0800575 if (likely(!bl_push_one_short_extent(be->be_inval)))
576 par->bse_count++;
577 else
578 goto out_mds;
Peng Tao71cdd402011-07-30 20:52:56 -0400579 temp = offset >> PAGE_CACHE_SHIFT;
580 npg_zero = do_div(temp, npg_per_block);
581 isect = (sector_t) (((offset - npg_zero * PAGE_CACHE_SIZE) &
582 (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
583 extent_length = be->be_length - (isect - be->be_f_offset);
584
585fill_invalid_ext:
586 dprintk("%s need to zero %d pages\n", __func__, npg_zero);
587 for (;npg_zero > 0; npg_zero--) {
Peng Tao75422742011-09-22 21:50:17 -0400588 if (bl_is_sector_init(be->be_inval, isect)) {
589 dprintk("isect %llu already init\n",
590 (unsigned long long)isect);
591 goto next_page;
592 }
Peng Tao71cdd402011-07-30 20:52:56 -0400593 /* page ref released in bl_end_io_write_zero */
594 index = isect >> PAGE_CACHE_SECTOR_SHIFT;
595 dprintk("%s zero %dth page: index %lu isect %llu\n",
596 __func__, npg_zero, index,
597 (unsigned long long)isect);
Peng Tao72c50882012-01-12 23:18:42 +0800598 page = bl_find_get_zeroing_page(wdata->inode, index,
599 cow_read);
600 if (unlikely(IS_ERR(page))) {
601 wdata->pnfs_error = PTR_ERR(page);
Peng Tao71cdd402011-07-30 20:52:56 -0400602 goto out;
Peng Tao72c50882012-01-12 23:18:42 +0800603 } else if (page == NULL)
Peng Tao71cdd402011-07-30 20:52:56 -0400604 goto next_page;
Peng Tao71cdd402011-07-30 20:52:56 -0400605
606 ret = bl_mark_sectors_init(be->be_inval, isect,
Peng Tao60c52e32012-01-12 23:18:40 +0800607 PAGE_CACHE_SECTORS);
Peng Tao71cdd402011-07-30 20:52:56 -0400608 if (unlikely(ret)) {
609 dprintk("%s bl_mark_sectors_init fail %d\n",
610 __func__, ret);
611 end_page_writeback(page);
612 page_cache_release(page);
613 wdata->pnfs_error = ret;
614 goto out;
615 }
Peng Tao7c5465d2012-01-12 23:18:46 +0800616 if (likely(!bl_push_one_short_extent(be->be_inval)))
617 par->bse_count++;
618 else {
619 end_page_writeback(page);
620 page_cache_release(page);
621 wdata->pnfs_error = -ENOMEM;
622 goto out;
623 }
624 /* FIXME: This should be done in bi_end_io */
625 mark_extents_written(BLK_LSEG2EXT(wdata->lseg),
626 page->index << PAGE_CACHE_SHIFT,
627 PAGE_CACHE_SIZE);
628
Peng Tao71cdd402011-07-30 20:52:56 -0400629 bio = bl_add_page_to_bio(bio, npg_zero, WRITE,
630 isect, page, be,
631 bl_end_io_write_zero, par);
632 if (IS_ERR(bio)) {
633 wdata->pnfs_error = PTR_ERR(bio);
Peng Taoe6d05a72011-09-22 21:50:16 -0400634 bio = NULL;
Peng Tao71cdd402011-07-30 20:52:56 -0400635 goto out;
636 }
Peng Tao71cdd402011-07-30 20:52:56 -0400637next_page:
638 isect += PAGE_CACHE_SECTORS;
639 extent_length -= PAGE_CACHE_SECTORS;
640 }
641 if (last)
642 goto write_done;
643 }
644 bio = bl_submit_bio(WRITE, bio);
645
646 /* Middle pages */
647 pg_index = wdata->args.pgbase >> PAGE_CACHE_SHIFT;
648 for (i = pg_index; i < wdata->npages; i++) {
Fred Isaman650e2d32011-07-30 20:52:54 -0400649 if (!extent_length) {
650 /* We've used up the previous extent */
651 bl_put_extent(be);
652 bio = bl_submit_bio(WRITE, bio);
653 /* Get the next one */
654 be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg),
655 isect, NULL);
656 if (!be || !is_writable(be, isect)) {
Peng Tao71cdd402011-07-30 20:52:56 -0400657 wdata->pnfs_error = -EINVAL;
Fred Isaman650e2d32011-07-30 20:52:54 -0400658 goto out;
659 }
Peng Tao7c5465d2012-01-12 23:18:46 +0800660 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
661 if (likely(!bl_push_one_short_extent(
662 be->be_inval)))
663 par->bse_count++;
664 else {
665 wdata->pnfs_error = -ENOMEM;
666 goto out;
667 }
668 }
Fred Isaman650e2d32011-07-30 20:52:54 -0400669 extent_length = be->be_length -
Peng Tao71cdd402011-07-30 20:52:56 -0400670 (isect - be->be_f_offset);
Fred Isaman650e2d32011-07-30 20:52:54 -0400671 }
Peng Tao71cdd402011-07-30 20:52:56 -0400672 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
673 ret = bl_mark_sectors_init(be->be_inval, isect,
Peng Tao60c52e32012-01-12 23:18:40 +0800674 PAGE_CACHE_SECTORS);
Peng Tao71cdd402011-07-30 20:52:56 -0400675 if (unlikely(ret)) {
676 dprintk("%s bl_mark_sectors_init fail %d\n",
677 __func__, ret);
678 wdata->pnfs_error = ret;
679 goto out;
Fred Isaman650e2d32011-07-30 20:52:54 -0400680 }
Peng Tao71cdd402011-07-30 20:52:56 -0400681 }
682 bio = bl_add_page_to_bio(bio, wdata->npages - i, WRITE,
683 isect, pages[i], be,
684 bl_end_io_write, par);
685 if (IS_ERR(bio)) {
686 wdata->pnfs_error = PTR_ERR(bio);
Peng Taoe6d05a72011-09-22 21:50:16 -0400687 bio = NULL;
Peng Tao71cdd402011-07-30 20:52:56 -0400688 goto out;
Fred Isaman650e2d32011-07-30 20:52:54 -0400689 }
690 isect += PAGE_CACHE_SECTORS;
Peng Tao71cdd402011-07-30 20:52:56 -0400691 last_isect = isect;
Fred Isaman650e2d32011-07-30 20:52:54 -0400692 extent_length -= PAGE_CACHE_SECTORS;
693 }
Peng Tao71cdd402011-07-30 20:52:56 -0400694
695 /* Last page inside INVALID extent */
696 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
697 bio = bl_submit_bio(WRITE, bio);
698 temp = last_isect >> PAGE_CACHE_SECTOR_SHIFT;
699 npg_zero = npg_per_block - do_div(temp, npg_per_block);
700 if (npg_zero < npg_per_block) {
701 last = 1;
702 goto fill_invalid_ext;
703 }
704 }
705
706write_done:
707 wdata->res.count = (last_isect << SECTOR_SHIFT) - (offset);
708 if (count < wdata->res.count) {
Fred Isaman650e2d32011-07-30 20:52:54 -0400709 wdata->res.count = count;
Peng Tao71cdd402011-07-30 20:52:56 -0400710 }
Fred Isaman650e2d32011-07-30 20:52:54 -0400711out:
712 bl_put_extent(be);
713 bl_submit_bio(WRITE, bio);
714 put_parallel(par);
715 return PNFS_ATTEMPTED;
Peng Tao7c5465d2012-01-12 23:18:46 +0800716out_mds:
717 bl_put_extent(be);
718 kfree(par);
719 return PNFS_NOT_ATTEMPTED;
Fred Isaman155e7522011-07-30 20:52:39 -0400720}
721
Fred Isaman9e692962011-07-30 20:52:41 -0400722/* FIXME - range ignored */
Fred Isaman155e7522011-07-30 20:52:39 -0400723static void
Fred Isaman9e692962011-07-30 20:52:41 -0400724release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range)
Fred Isaman155e7522011-07-30 20:52:39 -0400725{
Fred Isaman9e692962011-07-30 20:52:41 -0400726 int i;
727 struct pnfs_block_extent *be;
728
729 spin_lock(&bl->bl_ext_lock);
730 for (i = 0; i < EXTENT_LISTS; i++) {
731 while (!list_empty(&bl->bl_extents[i])) {
732 be = list_first_entry(&bl->bl_extents[i],
733 struct pnfs_block_extent,
734 be_node);
735 list_del(&be->be_node);
736 bl_put_extent(be);
737 }
738 }
739 spin_unlock(&bl->bl_ext_lock);
Fred Isaman155e7522011-07-30 20:52:39 -0400740}
741
Fred Isaman155e7522011-07-30 20:52:39 -0400742static void
743release_inval_marks(struct pnfs_inval_markings *marks)
744{
Fred Isamanc1c2a4c2011-07-30 20:52:49 -0400745 struct pnfs_inval_tracking *pos, *temp;
Peng Tao7c5465d2012-01-12 23:18:46 +0800746 struct pnfs_block_short_extent *se, *stemp;
Fred Isamanc1c2a4c2011-07-30 20:52:49 -0400747
748 list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) {
749 list_del(&pos->it_link);
750 kfree(pos);
751 }
Peng Tao7c5465d2012-01-12 23:18:46 +0800752
753 list_for_each_entry_safe(se, stemp, &marks->im_extents, bse_node) {
754 list_del(&se->bse_node);
755 kfree(se);
756 }
Fred Isaman155e7522011-07-30 20:52:39 -0400757 return;
758}
759
760static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
761{
762 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
763
764 dprintk("%s enter\n", __func__);
765 release_extents(bl, NULL);
766 release_inval_marks(&bl->bl_inval);
767 kfree(bl);
768}
769
770static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode,
771 gfp_t gfp_flags)
772{
773 struct pnfs_block_layout *bl;
774
775 dprintk("%s enter\n", __func__);
776 bl = kzalloc(sizeof(*bl), gfp_flags);
777 if (!bl)
778 return NULL;
779 spin_lock_init(&bl->bl_ext_lock);
780 INIT_LIST_HEAD(&bl->bl_extents[0]);
781 INIT_LIST_HEAD(&bl->bl_extents[1]);
782 INIT_LIST_HEAD(&bl->bl_commit);
783 INIT_LIST_HEAD(&bl->bl_committing);
784 bl->bl_count = 0;
785 bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT;
786 BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize);
787 return &bl->bl_layout;
788}
789
Fred Isamana60d2eb2011-07-30 20:52:44 -0400790static void bl_free_lseg(struct pnfs_layout_segment *lseg)
Fred Isaman155e7522011-07-30 20:52:39 -0400791{
Fred Isamana60d2eb2011-07-30 20:52:44 -0400792 dprintk("%s enter\n", __func__);
793 kfree(lseg);
Fred Isaman155e7522011-07-30 20:52:39 -0400794}
795
Fred Isamana60d2eb2011-07-30 20:52:44 -0400796/* We pretty much ignore lseg, and store all data layout wide, so we
797 * can correctly merge.
798 */
799static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo,
800 struct nfs4_layoutget_res *lgr,
801 gfp_t gfp_flags)
Fred Isaman155e7522011-07-30 20:52:39 -0400802{
Fred Isamana60d2eb2011-07-30 20:52:44 -0400803 struct pnfs_layout_segment *lseg;
804 int status;
805
806 dprintk("%s enter\n", __func__);
807 lseg = kzalloc(sizeof(*lseg), gfp_flags);
808 if (!lseg)
809 return ERR_PTR(-ENOMEM);
810 status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags);
811 if (status) {
812 /* We don't want to call the full-blown bl_free_lseg,
813 * since on error extents were not touched.
814 */
815 kfree(lseg);
816 return ERR_PTR(status);
817 }
818 return lseg;
Fred Isaman155e7522011-07-30 20:52:39 -0400819}
820
821static void
822bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr,
823 const struct nfs4_layoutcommit_args *arg)
824{
Fred Isaman90ace122011-07-30 20:52:51 -0400825 dprintk("%s enter\n", __func__);
826 encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg);
Fred Isaman155e7522011-07-30 20:52:39 -0400827}
828
829static void
830bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
831{
Fred Isamanb2be7812011-07-30 20:52:52 -0400832 struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout;
833
834 dprintk("%s enter\n", __func__);
835 clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status);
Fred Isaman155e7522011-07-30 20:52:39 -0400836}
837
Fred Isaman2f9fd182011-07-30 20:52:46 -0400838static void free_blk_mountid(struct block_mount_id *mid)
839{
840 if (mid) {
Peng Tao93a38442012-01-12 23:18:47 +0800841 struct pnfs_block_dev *dev, *tmp;
842
843 /* No need to take bm_lock as we are last user freeing bm_devlist */
844 list_for_each_entry_safe(dev, tmp, &mid->bm_devlist, bm_node) {
Fred Isaman2f9fd182011-07-30 20:52:46 -0400845 list_del(&dev->bm_node);
846 bl_free_block_dev(dev);
847 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400848 kfree(mid);
849 }
850}
851
852/* This is mostly copied from the filelayout's get_device_info function.
853 * It seems much of this should be at the generic pnfs level.
854 */
855static struct pnfs_block_dev *
856nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh,
857 struct nfs4_deviceid *d_id)
858{
859 struct pnfs_device *dev;
Jim Rees516f2e22011-09-22 21:50:08 -0400860 struct pnfs_block_dev *rv;
Fred Isaman2f9fd182011-07-30 20:52:46 -0400861 u32 max_resp_sz;
862 int max_pages;
863 struct page **pages = NULL;
864 int i, rc;
865
866 /*
867 * Use the session max response size as the basis for setting
868 * GETDEVICEINFO's maxcount
869 */
870 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
871 max_pages = max_resp_sz >> PAGE_SHIFT;
872 dprintk("%s max_resp_sz %u max_pages %d\n",
873 __func__, max_resp_sz, max_pages);
874
875 dev = kmalloc(sizeof(*dev), GFP_NOFS);
876 if (!dev) {
877 dprintk("%s kmalloc failed\n", __func__);
Jim Rees516f2e22011-09-22 21:50:08 -0400878 return ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400879 }
880
881 pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS);
882 if (pages == NULL) {
883 kfree(dev);
Jim Rees516f2e22011-09-22 21:50:08 -0400884 return ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400885 }
886 for (i = 0; i < max_pages; i++) {
887 pages[i] = alloc_page(GFP_NOFS);
Jim Rees516f2e22011-09-22 21:50:08 -0400888 if (!pages[i]) {
889 rv = ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400890 goto out_free;
Jim Rees516f2e22011-09-22 21:50:08 -0400891 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400892 }
893
894 memcpy(&dev->dev_id, d_id, sizeof(*d_id));
895 dev->layout_type = LAYOUT_BLOCK_VOLUME;
896 dev->pages = pages;
897 dev->pgbase = 0;
898 dev->pglen = PAGE_SIZE * max_pages;
899 dev->mincount = 0;
900
901 dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data);
902 rc = nfs4_proc_getdeviceinfo(server, dev);
903 dprintk("%s getdevice info returns %d\n", __func__, rc);
Jim Rees516f2e22011-09-22 21:50:08 -0400904 if (rc) {
905 rv = ERR_PTR(rc);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400906 goto out_free;
Jim Rees516f2e22011-09-22 21:50:08 -0400907 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400908
909 rv = nfs4_blk_decode_device(server, dev);
910 out_free:
911 for (i = 0; i < max_pages; i++)
912 __free_page(pages[i]);
913 kfree(pages);
914 kfree(dev);
915 return rv;
916}
917
Fred Isaman155e7522011-07-30 20:52:39 -0400918static int
919bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
920{
Fred Isaman2f9fd182011-07-30 20:52:46 -0400921 struct block_mount_id *b_mt_id = NULL;
922 struct pnfs_devicelist *dlist = NULL;
923 struct pnfs_block_dev *bdev;
924 LIST_HEAD(block_disklist);
Jim Rees516f2e22011-09-22 21:50:08 -0400925 int status, i;
Fred Isaman2f9fd182011-07-30 20:52:46 -0400926
Fred Isaman155e7522011-07-30 20:52:39 -0400927 dprintk("%s enter\n", __func__);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400928
929 if (server->pnfs_blksize == 0) {
930 dprintk("%s Server did not return blksize\n", __func__);
931 return -EINVAL;
932 }
933 b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS);
934 if (!b_mt_id) {
935 status = -ENOMEM;
936 goto out_error;
937 }
938 /* Initialize nfs4 block layout mount id */
939 spin_lock_init(&b_mt_id->bm_lock);
940 INIT_LIST_HEAD(&b_mt_id->bm_devlist);
941
942 dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS);
943 if (!dlist) {
944 status = -ENOMEM;
945 goto out_error;
946 }
947 dlist->eof = 0;
948 while (!dlist->eof) {
949 status = nfs4_proc_getdevicelist(server, fh, dlist);
950 if (status)
951 goto out_error;
952 dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n",
953 __func__, dlist->num_devs, dlist->eof);
954 for (i = 0; i < dlist->num_devs; i++) {
955 bdev = nfs4_blk_get_deviceinfo(server, fh,
956 &dlist->dev_id[i]);
Jim Rees516f2e22011-09-22 21:50:08 -0400957 if (IS_ERR(bdev)) {
958 status = PTR_ERR(bdev);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400959 goto out_error;
960 }
961 spin_lock(&b_mt_id->bm_lock);
962 list_add(&bdev->bm_node, &b_mt_id->bm_devlist);
963 spin_unlock(&b_mt_id->bm_lock);
964 }
965 }
966 dprintk("%s SUCCESS\n", __func__);
967 server->pnfs_ld_data = b_mt_id;
968
969 out_return:
970 kfree(dlist);
971 return status;
972
973 out_error:
974 free_blk_mountid(b_mt_id);
975 goto out_return;
Fred Isaman155e7522011-07-30 20:52:39 -0400976}
977
978static int
979bl_clear_layoutdriver(struct nfs_server *server)
980{
Fred Isaman2f9fd182011-07-30 20:52:46 -0400981 struct block_mount_id *b_mt_id = server->pnfs_ld_data;
982
Fred Isaman155e7522011-07-30 20:52:39 -0400983 dprintk("%s enter\n", __func__);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400984 free_blk_mountid(b_mt_id);
985 dprintk("%s RETURNS\n", __func__);
Fred Isaman155e7522011-07-30 20:52:39 -0400986 return 0;
987}
988
Benny Halevye9643fe2011-07-30 20:52:40 -0400989static const struct nfs_pageio_ops bl_pg_read_ops = {
990 .pg_init = pnfs_generic_pg_init_read,
991 .pg_test = pnfs_generic_pg_test,
992 .pg_doio = pnfs_generic_pg_readpages,
993};
994
995static const struct nfs_pageio_ops bl_pg_write_ops = {
996 .pg_init = pnfs_generic_pg_init_write,
997 .pg_test = pnfs_generic_pg_test,
998 .pg_doio = pnfs_generic_pg_writepages,
999};
1000
Fred Isaman155e7522011-07-30 20:52:39 -04001001static struct pnfs_layoutdriver_type blocklayout_type = {
1002 .id = LAYOUT_BLOCK_VOLUME,
1003 .name = "LAYOUT_BLOCK_VOLUME",
1004 .read_pagelist = bl_read_pagelist,
1005 .write_pagelist = bl_write_pagelist,
1006 .alloc_layout_hdr = bl_alloc_layout_hdr,
1007 .free_layout_hdr = bl_free_layout_hdr,
1008 .alloc_lseg = bl_alloc_lseg,
1009 .free_lseg = bl_free_lseg,
1010 .encode_layoutcommit = bl_encode_layoutcommit,
1011 .cleanup_layoutcommit = bl_cleanup_layoutcommit,
1012 .set_layoutdriver = bl_set_layoutdriver,
1013 .clear_layoutdriver = bl_clear_layoutdriver,
Benny Halevye9643fe2011-07-30 20:52:40 -04001014 .pg_read_ops = &bl_pg_read_ops,
1015 .pg_write_ops = &bl_pg_write_ops,
Fred Isaman155e7522011-07-30 20:52:39 -04001016};
1017
Jim Reesfe0a9b72011-07-30 20:52:42 -04001018static const struct rpc_pipe_ops bl_upcall_ops = {
Peng Taoc1225152011-09-22 21:50:10 -04001019 .upcall = rpc_pipe_generic_upcall,
Jim Reesfe0a9b72011-07-30 20:52:42 -04001020 .downcall = bl_pipe_downcall,
1021 .destroy_msg = bl_pipe_destroy_msg,
1022};
1023
Stanislav Kinsbursky332dfab2012-01-10 17:04:16 +04001024static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb,
1025 struct rpc_pipe *pipe)
1026{
1027 struct dentry *dir, *dentry;
1028
1029 dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME);
1030 if (dir == NULL)
1031 return ERR_PTR(-ENOENT);
1032 dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe);
1033 dput(dir);
1034 return dentry;
1035}
1036
1037static void nfs4blocklayout_unregister_sb(struct super_block *sb,
1038 struct rpc_pipe *pipe)
1039{
1040 if (pipe->dentry)
1041 rpc_unlink(pipe->dentry);
1042}
1043
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001044static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
1045 void *ptr)
1046{
1047 struct super_block *sb = ptr;
1048 struct net *net = sb->s_fs_info;
1049 struct nfs_net *nn = net_generic(net, nfs_net_id);
1050 struct dentry *dentry;
1051 int ret = 0;
1052
1053 if (!try_module_get(THIS_MODULE))
1054 return 0;
1055
1056 if (nn->bl_device_pipe == NULL) {
1057 module_put(THIS_MODULE);
1058 return 0;
1059 }
1060
1061 switch (event) {
1062 case RPC_PIPEFS_MOUNT:
1063 dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe);
1064 if (IS_ERR(dentry)) {
1065 ret = PTR_ERR(dentry);
1066 break;
1067 }
1068 nn->bl_device_pipe->dentry = dentry;
1069 break;
1070 case RPC_PIPEFS_UMOUNT:
1071 if (nn->bl_device_pipe->dentry)
1072 nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe);
1073 break;
1074 default:
1075 ret = -ENOTSUPP;
1076 break;
1077 }
1078 module_put(THIS_MODULE);
1079 return ret;
1080}
1081
1082static struct notifier_block nfs4blocklayout_block = {
1083 .notifier_call = rpc_pipefs_event,
1084};
1085
Stanislav Kinsbursky332dfab2012-01-10 17:04:16 +04001086static struct dentry *nfs4blocklayout_register_net(struct net *net,
1087 struct rpc_pipe *pipe)
1088{
1089 struct super_block *pipefs_sb;
1090 struct dentry *dentry;
1091
1092 pipefs_sb = rpc_get_sb_net(net);
1093 if (!pipefs_sb)
Stanislav Kinsbursky2561d612012-01-10 17:04:40 +04001094 return NULL;
Stanislav Kinsbursky332dfab2012-01-10 17:04:16 +04001095 dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe);
1096 rpc_put_sb_net(net);
1097 return dentry;
1098}
1099
1100static void nfs4blocklayout_unregister_net(struct net *net,
1101 struct rpc_pipe *pipe)
1102{
1103 struct super_block *pipefs_sb;
1104
1105 pipefs_sb = rpc_get_sb_net(net);
1106 if (pipefs_sb) {
1107 nfs4blocklayout_unregister_sb(pipefs_sb, pipe);
1108 rpc_put_sb_net(net);
1109 }
1110}
1111
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04001112static int nfs4blocklayout_net_init(struct net *net)
1113{
1114 struct nfs_net *nn = net_generic(net, nfs_net_id);
1115 struct dentry *dentry;
1116
Stanislav Kinsbursky5ffaf852012-03-11 18:20:31 +04001117 init_waitqueue_head(&nn->bl_wq);
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04001118 nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0);
1119 if (IS_ERR(nn->bl_device_pipe))
1120 return PTR_ERR(nn->bl_device_pipe);
1121 dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe);
1122 if (IS_ERR(dentry)) {
1123 rpc_destroy_pipe_data(nn->bl_device_pipe);
1124 return PTR_ERR(dentry);
1125 }
1126 nn->bl_device_pipe->dentry = dentry;
1127 return 0;
1128}
1129
1130static void nfs4blocklayout_net_exit(struct net *net)
1131{
1132 struct nfs_net *nn = net_generic(net, nfs_net_id);
1133
1134 nfs4blocklayout_unregister_net(net, nn->bl_device_pipe);
1135 rpc_destroy_pipe_data(nn->bl_device_pipe);
1136 nn->bl_device_pipe = NULL;
1137}
1138
1139static struct pernet_operations nfs4blocklayout_net_ops = {
1140 .init = nfs4blocklayout_net_init,
1141 .exit = nfs4blocklayout_net_exit,
1142};
1143
Fred Isaman155e7522011-07-30 20:52:39 -04001144static int __init nfs4blocklayout_init(void)
1145{
1146 int ret;
1147
1148 dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
1149
1150 ret = pnfs_register_layoutdriver(&blocklayout_type);
Jim Reesfe0a9b72011-07-30 20:52:42 -04001151 if (ret)
1152 goto out;
1153
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001154 ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block);
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04001155 if (ret)
1156 goto out_remove;
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001157 ret = register_pernet_subsys(&nfs4blocklayout_net_ops);
1158 if (ret)
1159 goto out_notifier;
Jim Reesfe0a9b72011-07-30 20:52:42 -04001160out:
1161 return ret;
1162
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001163out_notifier:
1164 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
Jim Reesfe0a9b72011-07-30 20:52:42 -04001165out_remove:
1166 pnfs_unregister_layoutdriver(&blocklayout_type);
Fred Isaman155e7522011-07-30 20:52:39 -04001167 return ret;
1168}
1169
1170static void __exit nfs4blocklayout_exit(void)
1171{
1172 dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
1173 __func__);
1174
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001175 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04001176 unregister_pernet_subsys(&nfs4blocklayout_net_ops);
Fred Isaman155e7522011-07-30 20:52:39 -04001177 pnfs_unregister_layoutdriver(&blocklayout_type);
Fred Isaman155e7522011-07-30 20:52:39 -04001178}
1179
1180MODULE_ALIAS("nfs-layouttype4-3");
1181
1182module_init(nfs4blocklayout_init);
1183module_exit(nfs4blocklayout_exit);