blob: 61501346324e0a91d6ed94a05294f91ca4f85a69 [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;
236 size_t count = rdata->args.count;
237 struct page **pages = rdata->args.pages;
238 int pg_index = rdata->args.pgbase >> PAGE_CACHE_SHIFT;
239
240 dprintk("%s enter nr_pages %u offset %lld count %Zd\n", __func__,
241 rdata->npages, f_offset, count);
242
243 par = alloc_parallel(rdata);
244 if (!par)
245 goto use_mds;
Fred Isaman9549ec02011-07-30 20:52:53 -0400246 par->pnfs_callback = bl_end_par_io_read;
247 /* At this point, we can no longer jump to use_mds */
248
249 isect = (sector_t) (f_offset >> SECTOR_SHIFT);
250 /* Code assumes extents are page-aligned */
251 for (i = pg_index; i < rdata->npages; i++) {
252 if (!extent_length) {
253 /* We've used up the previous extent */
254 bl_put_extent(be);
255 bl_put_extent(cow_read);
256 bio = bl_submit_bio(READ, bio);
257 /* Get the next one */
258 be = bl_find_get_extent(BLK_LSEG2EXT(rdata->lseg),
259 isect, &cow_read);
260 if (!be) {
261 rdata->pnfs_error = -EIO;
262 goto out;
263 }
264 extent_length = be->be_length -
265 (isect - be->be_f_offset);
266 if (cow_read) {
267 sector_t cow_length = cow_read->be_length -
268 (isect - cow_read->be_f_offset);
269 extent_length = min(extent_length, cow_length);
270 }
271 }
272 hole = is_hole(be, isect);
273 if (hole && !cow_read) {
274 bio = bl_submit_bio(READ, bio);
275 /* Fill hole w/ zeroes w/o accessing device */
276 dprintk("%s Zeroing page for hole\n", __func__);
277 zero_user_segment(pages[i], 0, PAGE_CACHE_SIZE);
278 print_page(pages[i]);
279 SetPageUptodate(pages[i]);
280 } else {
281 struct pnfs_block_extent *be_read;
282
283 be_read = (hole && cow_read) ? cow_read : be;
284 bio = bl_add_page_to_bio(bio, rdata->npages - i, READ,
285 isect, pages[i], be_read,
286 bl_end_io_read, par);
287 if (IS_ERR(bio)) {
288 rdata->pnfs_error = PTR_ERR(bio);
Peng Taoe6d05a72011-09-22 21:50:16 -0400289 bio = NULL;
Fred Isaman9549ec02011-07-30 20:52:53 -0400290 goto out;
291 }
292 }
293 isect += PAGE_CACHE_SECTORS;
294 extent_length -= PAGE_CACHE_SECTORS;
295 }
296 if ((isect << SECTOR_SHIFT) >= rdata->inode->i_size) {
297 rdata->res.eof = 1;
298 rdata->res.count = rdata->inode->i_size - f_offset;
299 } else {
300 rdata->res.count = (isect << SECTOR_SHIFT) - f_offset;
301 }
302out:
303 bl_put_extent(be);
304 bl_put_extent(cow_read);
305 bl_submit_bio(READ, bio);
306 put_parallel(par);
307 return PNFS_ATTEMPTED;
308
309 use_mds:
310 dprintk("Giving up and using normal NFS\n");
Fred Isaman155e7522011-07-30 20:52:39 -0400311 return PNFS_NOT_ATTEMPTED;
312}
313
Fred Isaman31e63062011-07-30 20:52:55 -0400314static void mark_extents_written(struct pnfs_block_layout *bl,
315 __u64 offset, __u32 count)
316{
317 sector_t isect, end;
318 struct pnfs_block_extent *be;
Peng Tao7c5465d2012-01-12 23:18:46 +0800319 struct pnfs_block_short_extent *se;
Fred Isaman31e63062011-07-30 20:52:55 -0400320
321 dprintk("%s(%llu, %u)\n", __func__, offset, count);
322 if (count == 0)
323 return;
324 isect = (offset & (long)(PAGE_CACHE_MASK)) >> SECTOR_SHIFT;
325 end = (offset + count + PAGE_CACHE_SIZE - 1) & (long)(PAGE_CACHE_MASK);
326 end >>= SECTOR_SHIFT;
327 while (isect < end) {
328 sector_t len;
329 be = bl_find_get_extent(bl, isect, NULL);
330 BUG_ON(!be); /* FIXME */
331 len = min(end, be->be_f_offset + be->be_length) - isect;
Peng Tao7c5465d2012-01-12 23:18:46 +0800332 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
333 se = bl_pop_one_short_extent(be->be_inval);
334 BUG_ON(!se);
335 bl_mark_for_commit(be, isect, len, se);
336 }
Fred Isaman31e63062011-07-30 20:52:55 -0400337 isect += len;
338 bl_put_extent(be);
339 }
340}
341
Peng Tao71cdd402011-07-30 20:52:56 -0400342static void bl_end_io_write_zero(struct bio *bio, int err)
343{
344 struct parallel_io *par = bio->bi_private;
345 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
346 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
347 struct nfs_write_data *wdata = (struct nfs_write_data *)par->data;
348
349 do {
350 struct page *page = bvec->bv_page;
351
352 if (--bvec >= bio->bi_io_vec)
353 prefetchw(&bvec->bv_page->flags);
354 /* This is the zeroing page we added */
355 end_page_writeback(page);
356 page_cache_release(page);
357 } while (bvec >= bio->bi_io_vec);
Peng Tao7c5465d2012-01-12 23:18:46 +0800358
359 if (unlikely(!uptodate)) {
Peng Tao71cdd402011-07-30 20:52:56 -0400360 if (!wdata->pnfs_error)
361 wdata->pnfs_error = -EIO;
Peng Tao1b0ae062011-09-22 21:50:12 -0400362 pnfs_set_lo_fail(wdata->lseg);
Peng Tao71cdd402011-07-30 20:52:56 -0400363 }
364 bio_put(bio);
365 put_parallel(par);
366}
367
Fred Isaman650e2d32011-07-30 20:52:54 -0400368static void bl_end_io_write(struct bio *bio, int err)
Fred Isaman155e7522011-07-30 20:52:39 -0400369{
Fred Isaman650e2d32011-07-30 20:52:54 -0400370 struct parallel_io *par = bio->bi_private;
371 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
372 struct nfs_write_data *wdata = (struct nfs_write_data *)par->data;
373
374 if (!uptodate) {
375 if (!wdata->pnfs_error)
376 wdata->pnfs_error = -EIO;
Peng Tao1b0ae062011-09-22 21:50:12 -0400377 pnfs_set_lo_fail(wdata->lseg);
Fred Isaman650e2d32011-07-30 20:52:54 -0400378 }
379 bio_put(bio);
380 put_parallel(par);
381}
382
383/* Function scheduled for call during bl_end_par_io_write,
384 * it marks sectors as written and extends the commitlist.
385 */
386static void bl_write_cleanup(struct work_struct *work)
387{
388 struct rpc_task *task;
389 struct nfs_write_data *wdata;
390 dprintk("%s enter\n", __func__);
391 task = container_of(work, struct rpc_task, u.tk_work);
392 wdata = container_of(task, struct nfs_write_data, task);
Peng Tao7c5465d2012-01-12 23:18:46 +0800393 if (likely(!wdata->pnfs_error)) {
Fred Isaman31e63062011-07-30 20:52:55 -0400394 /* Marks for LAYOUTCOMMIT */
Fred Isaman31e63062011-07-30 20:52:55 -0400395 mark_extents_written(BLK_LSEG2EXT(wdata->lseg),
396 wdata->args.offset, wdata->args.count);
397 }
Fred Isaman650e2d32011-07-30 20:52:54 -0400398 pnfs_ld_write_done(wdata);
399}
400
401/* Called when last of bios associated with a bl_write_pagelist call finishes */
Peng Tao7c5465d2012-01-12 23:18:46 +0800402static void bl_end_par_io_write(void *data, int num_se)
Fred Isaman650e2d32011-07-30 20:52:54 -0400403{
404 struct nfs_write_data *wdata = data;
405
Peng Tao7c5465d2012-01-12 23:18:46 +0800406 if (unlikely(wdata->pnfs_error)) {
407 bl_free_short_extents(&BLK_LSEG2EXT(wdata->lseg)->bl_inval,
408 num_se);
409 }
410
Peng Tao82b906d2012-01-12 23:18:43 +0800411 wdata->task.tk_status = wdata->pnfs_error;
Fred Isaman650e2d32011-07-30 20:52:54 -0400412 wdata->verf.committed = NFS_FILE_SYNC;
413 INIT_WORK(&wdata->task.u.tk_work, bl_write_cleanup);
414 schedule_work(&wdata->task.u.tk_work);
415}
416
Peng Tao71cdd402011-07-30 20:52:56 -0400417/* FIXME STUB - mark intersection of layout and page as bad, so is not
418 * used again.
419 */
420static void mark_bad_read(void)
421{
422 return;
423}
424
425/*
426 * map_block: map a requested I/0 block (isect) into an offset in the LVM
427 * block_device
428 */
429static void
430map_block(struct buffer_head *bh, sector_t isect, struct pnfs_block_extent *be)
431{
432 dprintk("%s enter be=%p\n", __func__, be);
433
434 set_buffer_mapped(bh);
435 bh->b_bdev = be->be_mdev;
436 bh->b_blocknr = (isect - be->be_f_offset + be->be_v_offset) >>
437 (be->be_mdev->bd_inode->i_blkbits - SECTOR_SHIFT);
438
439 dprintk("%s isect %llu, bh->b_blocknr %ld, using bsize %Zd\n",
440 __func__, (unsigned long long)isect, (long)bh->b_blocknr,
441 bh->b_size);
442 return;
443}
444
445/* Given an unmapped page, zero it or read in page for COW, page is locked
446 * by caller.
447 */
448static int
449init_page_for_write(struct page *page, struct pnfs_block_extent *cow_read)
450{
451 struct buffer_head *bh = NULL;
452 int ret = 0;
453 sector_t isect;
454
455 dprintk("%s enter, %p\n", __func__, page);
456 BUG_ON(PageUptodate(page));
457 if (!cow_read) {
458 zero_user_segment(page, 0, PAGE_SIZE);
459 SetPageUptodate(page);
460 goto cleanup;
461 }
462
463 bh = alloc_page_buffers(page, PAGE_CACHE_SIZE, 0);
464 if (!bh) {
465 ret = -ENOMEM;
466 goto cleanup;
467 }
468
469 isect = (sector_t) page->index << PAGE_CACHE_SECTOR_SHIFT;
470 map_block(bh, isect, cow_read);
471 if (!bh_uptodate_or_lock(bh))
472 ret = bh_submit_read(bh);
473 if (ret)
474 goto cleanup;
475 SetPageUptodate(page);
476
477cleanup:
478 bl_put_extent(cow_read);
479 if (bh)
480 free_buffer_head(bh);
481 if (ret) {
482 /* Need to mark layout with bad read...should now
483 * just use nfs4 for reads and writes.
484 */
485 mark_bad_read();
486 }
487 return ret;
488}
489
Peng Tao72c50882012-01-12 23:18:42 +0800490/* Find or create a zeroing page marked being writeback.
491 * Return ERR_PTR on error, NULL to indicate skip this page and page itself
492 * to indicate write out.
493 */
494static struct page *
495bl_find_get_zeroing_page(struct inode *inode, pgoff_t index,
496 struct pnfs_block_extent *cow_read)
497{
498 struct page *page;
499 int locked = 0;
500 page = find_get_page(inode->i_mapping, index);
501 if (page)
502 goto check_page;
503
504 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
505 if (unlikely(!page)) {
506 dprintk("%s oom\n", __func__);
507 return ERR_PTR(-ENOMEM);
508 }
509 locked = 1;
510
511check_page:
512 /* PageDirty: Other will write this out
513 * PageWriteback: Other is writing this out
514 * PageUptodate: It was read before
515 */
516 if (PageDirty(page) || PageWriteback(page)) {
517 print_page(page);
518 if (locked)
519 unlock_page(page);
520 page_cache_release(page);
521 return NULL;
522 }
523
524 if (!locked) {
525 lock_page(page);
526 locked = 1;
527 goto check_page;
528 }
529 if (!PageUptodate(page)) {
530 /* New page, readin or zero it */
531 init_page_for_write(page, cow_read);
532 }
533 set_page_writeback(page);
534 unlock_page(page);
535
536 return page;
537}
538
Fred Isaman650e2d32011-07-30 20:52:54 -0400539static enum pnfs_try_status
540bl_write_pagelist(struct nfs_write_data *wdata, int sync)
541{
Peng Tao71cdd402011-07-30 20:52:56 -0400542 int i, ret, npg_zero, pg_index, last = 0;
Fred Isaman650e2d32011-07-30 20:52:54 -0400543 struct bio *bio = NULL;
Peng Tao71cdd402011-07-30 20:52:56 -0400544 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
545 sector_t isect, last_isect = 0, extent_length = 0;
Fred Isaman650e2d32011-07-30 20:52:54 -0400546 struct parallel_io *par;
547 loff_t offset = wdata->args.offset;
548 size_t count = wdata->args.count;
549 struct page **pages = wdata->args.pages;
Peng Tao71cdd402011-07-30 20:52:56 -0400550 struct page *page;
551 pgoff_t index;
552 u64 temp;
553 int npg_per_block =
554 NFS_SERVER(wdata->inode)->pnfs_blksize >> PAGE_CACHE_SHIFT;
Fred Isaman650e2d32011-07-30 20:52:54 -0400555
556 dprintk("%s enter, %Zu@%lld\n", __func__, count, offset);
557 /* At this point, wdata->pages is a (sequential) list of nfs_pages.
Peng Tao71cdd402011-07-30 20:52:56 -0400558 * We want to write each, and if there is an error set pnfs_error
559 * to have it redone using nfs.
Fred Isaman650e2d32011-07-30 20:52:54 -0400560 */
561 par = alloc_parallel(wdata);
562 if (!par)
Peng Tao7c5465d2012-01-12 23:18:46 +0800563 goto out_mds;
Fred Isaman650e2d32011-07-30 20:52:54 -0400564 par->pnfs_callback = bl_end_par_io_write;
565 /* At this point, have to be more careful with error handling */
566
567 isect = (sector_t) ((offset & (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
Peng Tao71cdd402011-07-30 20:52:56 -0400568 be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg), isect, &cow_read);
569 if (!be || !is_writable(be, isect)) {
570 dprintk("%s no matching extents!\n", __func__);
Peng Tao7c5465d2012-01-12 23:18:46 +0800571 goto out_mds;
Peng Tao71cdd402011-07-30 20:52:56 -0400572 }
573
574 /* First page inside INVALID extent */
575 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
Peng Tao7c5465d2012-01-12 23:18:46 +0800576 if (likely(!bl_push_one_short_extent(be->be_inval)))
577 par->bse_count++;
578 else
579 goto out_mds;
Peng Tao71cdd402011-07-30 20:52:56 -0400580 temp = offset >> PAGE_CACHE_SHIFT;
581 npg_zero = do_div(temp, npg_per_block);
582 isect = (sector_t) (((offset - npg_zero * PAGE_CACHE_SIZE) &
583 (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
584 extent_length = be->be_length - (isect - be->be_f_offset);
585
586fill_invalid_ext:
587 dprintk("%s need to zero %d pages\n", __func__, npg_zero);
588 for (;npg_zero > 0; npg_zero--) {
Peng Tao75422742011-09-22 21:50:17 -0400589 if (bl_is_sector_init(be->be_inval, isect)) {
590 dprintk("isect %llu already init\n",
591 (unsigned long long)isect);
592 goto next_page;
593 }
Peng Tao71cdd402011-07-30 20:52:56 -0400594 /* page ref released in bl_end_io_write_zero */
595 index = isect >> PAGE_CACHE_SECTOR_SHIFT;
596 dprintk("%s zero %dth page: index %lu isect %llu\n",
597 __func__, npg_zero, index,
598 (unsigned long long)isect);
Peng Tao72c50882012-01-12 23:18:42 +0800599 page = bl_find_get_zeroing_page(wdata->inode, index,
600 cow_read);
601 if (unlikely(IS_ERR(page))) {
602 wdata->pnfs_error = PTR_ERR(page);
Peng Tao71cdd402011-07-30 20:52:56 -0400603 goto out;
Peng Tao72c50882012-01-12 23:18:42 +0800604 } else if (page == NULL)
Peng Tao71cdd402011-07-30 20:52:56 -0400605 goto next_page;
Peng Tao71cdd402011-07-30 20:52:56 -0400606
607 ret = bl_mark_sectors_init(be->be_inval, isect,
Peng Tao60c52e32012-01-12 23:18:40 +0800608 PAGE_CACHE_SECTORS);
Peng Tao71cdd402011-07-30 20:52:56 -0400609 if (unlikely(ret)) {
610 dprintk("%s bl_mark_sectors_init fail %d\n",
611 __func__, ret);
612 end_page_writeback(page);
613 page_cache_release(page);
614 wdata->pnfs_error = ret;
615 goto out;
616 }
Peng Tao7c5465d2012-01-12 23:18:46 +0800617 if (likely(!bl_push_one_short_extent(be->be_inval)))
618 par->bse_count++;
619 else {
620 end_page_writeback(page);
621 page_cache_release(page);
622 wdata->pnfs_error = -ENOMEM;
623 goto out;
624 }
625 /* FIXME: This should be done in bi_end_io */
626 mark_extents_written(BLK_LSEG2EXT(wdata->lseg),
627 page->index << PAGE_CACHE_SHIFT,
628 PAGE_CACHE_SIZE);
629
Peng Tao71cdd402011-07-30 20:52:56 -0400630 bio = bl_add_page_to_bio(bio, npg_zero, WRITE,
631 isect, page, be,
632 bl_end_io_write_zero, par);
633 if (IS_ERR(bio)) {
634 wdata->pnfs_error = PTR_ERR(bio);
Peng Taoe6d05a72011-09-22 21:50:16 -0400635 bio = NULL;
Peng Tao71cdd402011-07-30 20:52:56 -0400636 goto out;
637 }
Peng Tao71cdd402011-07-30 20:52:56 -0400638next_page:
639 isect += PAGE_CACHE_SECTORS;
640 extent_length -= PAGE_CACHE_SECTORS;
641 }
642 if (last)
643 goto write_done;
644 }
645 bio = bl_submit_bio(WRITE, bio);
646
647 /* Middle pages */
648 pg_index = wdata->args.pgbase >> PAGE_CACHE_SHIFT;
649 for (i = pg_index; i < wdata->npages; i++) {
Fred Isaman650e2d32011-07-30 20:52:54 -0400650 if (!extent_length) {
651 /* We've used up the previous extent */
652 bl_put_extent(be);
653 bio = bl_submit_bio(WRITE, bio);
654 /* Get the next one */
655 be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg),
656 isect, NULL);
657 if (!be || !is_writable(be, isect)) {
Peng Tao71cdd402011-07-30 20:52:56 -0400658 wdata->pnfs_error = -EINVAL;
Fred Isaman650e2d32011-07-30 20:52:54 -0400659 goto out;
660 }
Peng Tao7c5465d2012-01-12 23:18:46 +0800661 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
662 if (likely(!bl_push_one_short_extent(
663 be->be_inval)))
664 par->bse_count++;
665 else {
666 wdata->pnfs_error = -ENOMEM;
667 goto out;
668 }
669 }
Fred Isaman650e2d32011-07-30 20:52:54 -0400670 extent_length = be->be_length -
Peng Tao71cdd402011-07-30 20:52:56 -0400671 (isect - be->be_f_offset);
Fred Isaman650e2d32011-07-30 20:52:54 -0400672 }
Peng Tao71cdd402011-07-30 20:52:56 -0400673 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
674 ret = bl_mark_sectors_init(be->be_inval, isect,
Peng Tao60c52e32012-01-12 23:18:40 +0800675 PAGE_CACHE_SECTORS);
Peng Tao71cdd402011-07-30 20:52:56 -0400676 if (unlikely(ret)) {
677 dprintk("%s bl_mark_sectors_init fail %d\n",
678 __func__, ret);
679 wdata->pnfs_error = ret;
680 goto out;
Fred Isaman650e2d32011-07-30 20:52:54 -0400681 }
Peng Tao71cdd402011-07-30 20:52:56 -0400682 }
683 bio = bl_add_page_to_bio(bio, wdata->npages - i, WRITE,
684 isect, pages[i], be,
685 bl_end_io_write, par);
686 if (IS_ERR(bio)) {
687 wdata->pnfs_error = PTR_ERR(bio);
Peng Taoe6d05a72011-09-22 21:50:16 -0400688 bio = NULL;
Peng Tao71cdd402011-07-30 20:52:56 -0400689 goto out;
Fred Isaman650e2d32011-07-30 20:52:54 -0400690 }
691 isect += PAGE_CACHE_SECTORS;
Peng Tao71cdd402011-07-30 20:52:56 -0400692 last_isect = isect;
Fred Isaman650e2d32011-07-30 20:52:54 -0400693 extent_length -= PAGE_CACHE_SECTORS;
694 }
Peng Tao71cdd402011-07-30 20:52:56 -0400695
696 /* Last page inside INVALID extent */
697 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
698 bio = bl_submit_bio(WRITE, bio);
699 temp = last_isect >> PAGE_CACHE_SECTOR_SHIFT;
700 npg_zero = npg_per_block - do_div(temp, npg_per_block);
701 if (npg_zero < npg_per_block) {
702 last = 1;
703 goto fill_invalid_ext;
704 }
705 }
706
707write_done:
708 wdata->res.count = (last_isect << SECTOR_SHIFT) - (offset);
709 if (count < wdata->res.count) {
Fred Isaman650e2d32011-07-30 20:52:54 -0400710 wdata->res.count = count;
Peng Tao71cdd402011-07-30 20:52:56 -0400711 }
Fred Isaman650e2d32011-07-30 20:52:54 -0400712out:
713 bl_put_extent(be);
714 bl_submit_bio(WRITE, bio);
715 put_parallel(par);
716 return PNFS_ATTEMPTED;
Peng Tao7c5465d2012-01-12 23:18:46 +0800717out_mds:
718 bl_put_extent(be);
719 kfree(par);
720 return PNFS_NOT_ATTEMPTED;
Fred Isaman155e7522011-07-30 20:52:39 -0400721}
722
Fred Isaman9e692962011-07-30 20:52:41 -0400723/* FIXME - range ignored */
Fred Isaman155e7522011-07-30 20:52:39 -0400724static void
Fred Isaman9e692962011-07-30 20:52:41 -0400725release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range)
Fred Isaman155e7522011-07-30 20:52:39 -0400726{
Fred Isaman9e692962011-07-30 20:52:41 -0400727 int i;
728 struct pnfs_block_extent *be;
729
730 spin_lock(&bl->bl_ext_lock);
731 for (i = 0; i < EXTENT_LISTS; i++) {
732 while (!list_empty(&bl->bl_extents[i])) {
733 be = list_first_entry(&bl->bl_extents[i],
734 struct pnfs_block_extent,
735 be_node);
736 list_del(&be->be_node);
737 bl_put_extent(be);
738 }
739 }
740 spin_unlock(&bl->bl_ext_lock);
Fred Isaman155e7522011-07-30 20:52:39 -0400741}
742
Fred Isaman155e7522011-07-30 20:52:39 -0400743static void
744release_inval_marks(struct pnfs_inval_markings *marks)
745{
Fred Isamanc1c2a4c2011-07-30 20:52:49 -0400746 struct pnfs_inval_tracking *pos, *temp;
Peng Tao7c5465d2012-01-12 23:18:46 +0800747 struct pnfs_block_short_extent *se, *stemp;
Fred Isamanc1c2a4c2011-07-30 20:52:49 -0400748
749 list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) {
750 list_del(&pos->it_link);
751 kfree(pos);
752 }
Peng Tao7c5465d2012-01-12 23:18:46 +0800753
754 list_for_each_entry_safe(se, stemp, &marks->im_extents, bse_node) {
755 list_del(&se->bse_node);
756 kfree(se);
757 }
Fred Isaman155e7522011-07-30 20:52:39 -0400758 return;
759}
760
761static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
762{
763 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
764
765 dprintk("%s enter\n", __func__);
766 release_extents(bl, NULL);
767 release_inval_marks(&bl->bl_inval);
768 kfree(bl);
769}
770
771static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode,
772 gfp_t gfp_flags)
773{
774 struct pnfs_block_layout *bl;
775
776 dprintk("%s enter\n", __func__);
777 bl = kzalloc(sizeof(*bl), gfp_flags);
778 if (!bl)
779 return NULL;
780 spin_lock_init(&bl->bl_ext_lock);
781 INIT_LIST_HEAD(&bl->bl_extents[0]);
782 INIT_LIST_HEAD(&bl->bl_extents[1]);
783 INIT_LIST_HEAD(&bl->bl_commit);
784 INIT_LIST_HEAD(&bl->bl_committing);
785 bl->bl_count = 0;
786 bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT;
787 BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize);
788 return &bl->bl_layout;
789}
790
Fred Isamana60d2eb2011-07-30 20:52:44 -0400791static void bl_free_lseg(struct pnfs_layout_segment *lseg)
Fred Isaman155e7522011-07-30 20:52:39 -0400792{
Fred Isamana60d2eb2011-07-30 20:52:44 -0400793 dprintk("%s enter\n", __func__);
794 kfree(lseg);
Fred Isaman155e7522011-07-30 20:52:39 -0400795}
796
Fred Isamana60d2eb2011-07-30 20:52:44 -0400797/* We pretty much ignore lseg, and store all data layout wide, so we
798 * can correctly merge.
799 */
800static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo,
801 struct nfs4_layoutget_res *lgr,
802 gfp_t gfp_flags)
Fred Isaman155e7522011-07-30 20:52:39 -0400803{
Fred Isamana60d2eb2011-07-30 20:52:44 -0400804 struct pnfs_layout_segment *lseg;
805 int status;
806
807 dprintk("%s enter\n", __func__);
808 lseg = kzalloc(sizeof(*lseg), gfp_flags);
809 if (!lseg)
810 return ERR_PTR(-ENOMEM);
811 status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags);
812 if (status) {
813 /* We don't want to call the full-blown bl_free_lseg,
814 * since on error extents were not touched.
815 */
816 kfree(lseg);
817 return ERR_PTR(status);
818 }
819 return lseg;
Fred Isaman155e7522011-07-30 20:52:39 -0400820}
821
822static void
823bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr,
824 const struct nfs4_layoutcommit_args *arg)
825{
Fred Isaman90ace122011-07-30 20:52:51 -0400826 dprintk("%s enter\n", __func__);
827 encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg);
Fred Isaman155e7522011-07-30 20:52:39 -0400828}
829
830static void
831bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
832{
Fred Isamanb2be7812011-07-30 20:52:52 -0400833 struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout;
834
835 dprintk("%s enter\n", __func__);
836 clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status);
Fred Isaman155e7522011-07-30 20:52:39 -0400837}
838
Fred Isaman2f9fd182011-07-30 20:52:46 -0400839static void free_blk_mountid(struct block_mount_id *mid)
840{
841 if (mid) {
Peng Tao93a38442012-01-12 23:18:47 +0800842 struct pnfs_block_dev *dev, *tmp;
843
844 /* No need to take bm_lock as we are last user freeing bm_devlist */
845 list_for_each_entry_safe(dev, tmp, &mid->bm_devlist, bm_node) {
Fred Isaman2f9fd182011-07-30 20:52:46 -0400846 list_del(&dev->bm_node);
847 bl_free_block_dev(dev);
848 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400849 kfree(mid);
850 }
851}
852
853/* This is mostly copied from the filelayout's get_device_info function.
854 * It seems much of this should be at the generic pnfs level.
855 */
856static struct pnfs_block_dev *
857nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh,
858 struct nfs4_deviceid *d_id)
859{
860 struct pnfs_device *dev;
Jim Rees516f2e22011-09-22 21:50:08 -0400861 struct pnfs_block_dev *rv;
Fred Isaman2f9fd182011-07-30 20:52:46 -0400862 u32 max_resp_sz;
863 int max_pages;
864 struct page **pages = NULL;
865 int i, rc;
866
867 /*
868 * Use the session max response size as the basis for setting
869 * GETDEVICEINFO's maxcount
870 */
871 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
872 max_pages = max_resp_sz >> PAGE_SHIFT;
873 dprintk("%s max_resp_sz %u max_pages %d\n",
874 __func__, max_resp_sz, max_pages);
875
876 dev = kmalloc(sizeof(*dev), GFP_NOFS);
877 if (!dev) {
878 dprintk("%s kmalloc failed\n", __func__);
Jim Rees516f2e22011-09-22 21:50:08 -0400879 return ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400880 }
881
882 pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS);
883 if (pages == NULL) {
884 kfree(dev);
Jim Rees516f2e22011-09-22 21:50:08 -0400885 return ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400886 }
887 for (i = 0; i < max_pages; i++) {
888 pages[i] = alloc_page(GFP_NOFS);
Jim Rees516f2e22011-09-22 21:50:08 -0400889 if (!pages[i]) {
890 rv = ERR_PTR(-ENOMEM);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400891 goto out_free;
Jim Rees516f2e22011-09-22 21:50:08 -0400892 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400893 }
894
895 memcpy(&dev->dev_id, d_id, sizeof(*d_id));
896 dev->layout_type = LAYOUT_BLOCK_VOLUME;
897 dev->pages = pages;
898 dev->pgbase = 0;
899 dev->pglen = PAGE_SIZE * max_pages;
900 dev->mincount = 0;
901
902 dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data);
903 rc = nfs4_proc_getdeviceinfo(server, dev);
904 dprintk("%s getdevice info returns %d\n", __func__, rc);
Jim Rees516f2e22011-09-22 21:50:08 -0400905 if (rc) {
906 rv = ERR_PTR(rc);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400907 goto out_free;
Jim Rees516f2e22011-09-22 21:50:08 -0400908 }
Fred Isaman2f9fd182011-07-30 20:52:46 -0400909
910 rv = nfs4_blk_decode_device(server, dev);
911 out_free:
912 for (i = 0; i < max_pages; i++)
913 __free_page(pages[i]);
914 kfree(pages);
915 kfree(dev);
916 return rv;
917}
918
Fred Isaman155e7522011-07-30 20:52:39 -0400919static int
920bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
921{
Fred Isaman2f9fd182011-07-30 20:52:46 -0400922 struct block_mount_id *b_mt_id = NULL;
923 struct pnfs_devicelist *dlist = NULL;
924 struct pnfs_block_dev *bdev;
925 LIST_HEAD(block_disklist);
Jim Rees516f2e22011-09-22 21:50:08 -0400926 int status, i;
Fred Isaman2f9fd182011-07-30 20:52:46 -0400927
Fred Isaman155e7522011-07-30 20:52:39 -0400928 dprintk("%s enter\n", __func__);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400929
930 if (server->pnfs_blksize == 0) {
931 dprintk("%s Server did not return blksize\n", __func__);
932 return -EINVAL;
933 }
934 b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS);
935 if (!b_mt_id) {
936 status = -ENOMEM;
937 goto out_error;
938 }
939 /* Initialize nfs4 block layout mount id */
940 spin_lock_init(&b_mt_id->bm_lock);
941 INIT_LIST_HEAD(&b_mt_id->bm_devlist);
942
943 dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS);
944 if (!dlist) {
945 status = -ENOMEM;
946 goto out_error;
947 }
948 dlist->eof = 0;
949 while (!dlist->eof) {
950 status = nfs4_proc_getdevicelist(server, fh, dlist);
951 if (status)
952 goto out_error;
953 dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n",
954 __func__, dlist->num_devs, dlist->eof);
955 for (i = 0; i < dlist->num_devs; i++) {
956 bdev = nfs4_blk_get_deviceinfo(server, fh,
957 &dlist->dev_id[i]);
Jim Rees516f2e22011-09-22 21:50:08 -0400958 if (IS_ERR(bdev)) {
959 status = PTR_ERR(bdev);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400960 goto out_error;
961 }
962 spin_lock(&b_mt_id->bm_lock);
963 list_add(&bdev->bm_node, &b_mt_id->bm_devlist);
964 spin_unlock(&b_mt_id->bm_lock);
965 }
966 }
967 dprintk("%s SUCCESS\n", __func__);
968 server->pnfs_ld_data = b_mt_id;
969
970 out_return:
971 kfree(dlist);
972 return status;
973
974 out_error:
975 free_blk_mountid(b_mt_id);
976 goto out_return;
Fred Isaman155e7522011-07-30 20:52:39 -0400977}
978
979static int
980bl_clear_layoutdriver(struct nfs_server *server)
981{
Fred Isaman2f9fd182011-07-30 20:52:46 -0400982 struct block_mount_id *b_mt_id = server->pnfs_ld_data;
983
Fred Isaman155e7522011-07-30 20:52:39 -0400984 dprintk("%s enter\n", __func__);
Fred Isaman2f9fd182011-07-30 20:52:46 -0400985 free_blk_mountid(b_mt_id);
986 dprintk("%s RETURNS\n", __func__);
Fred Isaman155e7522011-07-30 20:52:39 -0400987 return 0;
988}
989
Benny Halevye9643fe2011-07-30 20:52:40 -0400990static const struct nfs_pageio_ops bl_pg_read_ops = {
991 .pg_init = pnfs_generic_pg_init_read,
992 .pg_test = pnfs_generic_pg_test,
993 .pg_doio = pnfs_generic_pg_readpages,
994};
995
996static const struct nfs_pageio_ops bl_pg_write_ops = {
997 .pg_init = pnfs_generic_pg_init_write,
998 .pg_test = pnfs_generic_pg_test,
999 .pg_doio = pnfs_generic_pg_writepages,
1000};
1001
Fred Isaman155e7522011-07-30 20:52:39 -04001002static struct pnfs_layoutdriver_type blocklayout_type = {
1003 .id = LAYOUT_BLOCK_VOLUME,
1004 .name = "LAYOUT_BLOCK_VOLUME",
1005 .read_pagelist = bl_read_pagelist,
1006 .write_pagelist = bl_write_pagelist,
1007 .alloc_layout_hdr = bl_alloc_layout_hdr,
1008 .free_layout_hdr = bl_free_layout_hdr,
1009 .alloc_lseg = bl_alloc_lseg,
1010 .free_lseg = bl_free_lseg,
1011 .encode_layoutcommit = bl_encode_layoutcommit,
1012 .cleanup_layoutcommit = bl_cleanup_layoutcommit,
1013 .set_layoutdriver = bl_set_layoutdriver,
1014 .clear_layoutdriver = bl_clear_layoutdriver,
Benny Halevye9643fe2011-07-30 20:52:40 -04001015 .pg_read_ops = &bl_pg_read_ops,
1016 .pg_write_ops = &bl_pg_write_ops,
Fred Isaman155e7522011-07-30 20:52:39 -04001017};
1018
Jim Reesfe0a9b72011-07-30 20:52:42 -04001019static const struct rpc_pipe_ops bl_upcall_ops = {
Peng Taoc1225152011-09-22 21:50:10 -04001020 .upcall = rpc_pipe_generic_upcall,
Jim Reesfe0a9b72011-07-30 20:52:42 -04001021 .downcall = bl_pipe_downcall,
1022 .destroy_msg = bl_pipe_destroy_msg,
1023};
1024
Stanislav Kinsbursky332dfab2012-01-10 17:04:16 +04001025static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb,
1026 struct rpc_pipe *pipe)
1027{
1028 struct dentry *dir, *dentry;
1029
1030 dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME);
1031 if (dir == NULL)
1032 return ERR_PTR(-ENOENT);
1033 dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe);
1034 dput(dir);
1035 return dentry;
1036}
1037
1038static void nfs4blocklayout_unregister_sb(struct super_block *sb,
1039 struct rpc_pipe *pipe)
1040{
1041 if (pipe->dentry)
1042 rpc_unlink(pipe->dentry);
1043}
1044
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001045static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
1046 void *ptr)
1047{
1048 struct super_block *sb = ptr;
1049 struct net *net = sb->s_fs_info;
1050 struct nfs_net *nn = net_generic(net, nfs_net_id);
1051 struct dentry *dentry;
1052 int ret = 0;
1053
1054 if (!try_module_get(THIS_MODULE))
1055 return 0;
1056
1057 if (nn->bl_device_pipe == NULL) {
1058 module_put(THIS_MODULE);
1059 return 0;
1060 }
1061
1062 switch (event) {
1063 case RPC_PIPEFS_MOUNT:
1064 dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe);
1065 if (IS_ERR(dentry)) {
1066 ret = PTR_ERR(dentry);
1067 break;
1068 }
1069 nn->bl_device_pipe->dentry = dentry;
1070 break;
1071 case RPC_PIPEFS_UMOUNT:
1072 if (nn->bl_device_pipe->dentry)
1073 nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe);
1074 break;
1075 default:
1076 ret = -ENOTSUPP;
1077 break;
1078 }
1079 module_put(THIS_MODULE);
1080 return ret;
1081}
1082
1083static struct notifier_block nfs4blocklayout_block = {
1084 .notifier_call = rpc_pipefs_event,
1085};
1086
Stanislav Kinsbursky332dfab2012-01-10 17:04:16 +04001087static struct dentry *nfs4blocklayout_register_net(struct net *net,
1088 struct rpc_pipe *pipe)
1089{
1090 struct super_block *pipefs_sb;
1091 struct dentry *dentry;
1092
1093 pipefs_sb = rpc_get_sb_net(net);
1094 if (!pipefs_sb)
Stanislav Kinsbursky2561d612012-01-10 17:04:40 +04001095 return NULL;
Stanislav Kinsbursky332dfab2012-01-10 17:04:16 +04001096 dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe);
1097 rpc_put_sb_net(net);
1098 return dentry;
1099}
1100
1101static void nfs4blocklayout_unregister_net(struct net *net,
1102 struct rpc_pipe *pipe)
1103{
1104 struct super_block *pipefs_sb;
1105
1106 pipefs_sb = rpc_get_sb_net(net);
1107 if (pipefs_sb) {
1108 nfs4blocklayout_unregister_sb(pipefs_sb, pipe);
1109 rpc_put_sb_net(net);
1110 }
1111}
1112
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04001113static int nfs4blocklayout_net_init(struct net *net)
1114{
1115 struct nfs_net *nn = net_generic(net, nfs_net_id);
1116 struct dentry *dentry;
1117
Stanislav Kinsbursky5ffaf852012-03-11 18:20:31 +04001118 init_waitqueue_head(&nn->bl_wq);
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04001119 nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0);
1120 if (IS_ERR(nn->bl_device_pipe))
1121 return PTR_ERR(nn->bl_device_pipe);
1122 dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe);
1123 if (IS_ERR(dentry)) {
1124 rpc_destroy_pipe_data(nn->bl_device_pipe);
1125 return PTR_ERR(dentry);
1126 }
1127 nn->bl_device_pipe->dentry = dentry;
1128 return 0;
1129}
1130
1131static void nfs4blocklayout_net_exit(struct net *net)
1132{
1133 struct nfs_net *nn = net_generic(net, nfs_net_id);
1134
1135 nfs4blocklayout_unregister_net(net, nn->bl_device_pipe);
1136 rpc_destroy_pipe_data(nn->bl_device_pipe);
1137 nn->bl_device_pipe = NULL;
1138}
1139
1140static struct pernet_operations nfs4blocklayout_net_ops = {
1141 .init = nfs4blocklayout_net_init,
1142 .exit = nfs4blocklayout_net_exit,
1143};
1144
Fred Isaman155e7522011-07-30 20:52:39 -04001145static int __init nfs4blocklayout_init(void)
1146{
1147 int ret;
1148
1149 dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
1150
1151 ret = pnfs_register_layoutdriver(&blocklayout_type);
Jim Reesfe0a9b72011-07-30 20:52:42 -04001152 if (ret)
1153 goto out;
1154
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001155 ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block);
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04001156 if (ret)
1157 goto out_remove;
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001158 ret = register_pernet_subsys(&nfs4blocklayout_net_ops);
1159 if (ret)
1160 goto out_notifier;
Jim Reesfe0a9b72011-07-30 20:52:42 -04001161out:
1162 return ret;
1163
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001164out_notifier:
1165 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
Jim Reesfe0a9b72011-07-30 20:52:42 -04001166out_remove:
1167 pnfs_unregister_layoutdriver(&blocklayout_type);
Fred Isaman155e7522011-07-30 20:52:39 -04001168 return ret;
1169}
1170
1171static void __exit nfs4blocklayout_exit(void)
1172{
1173 dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
1174 __func__);
1175
Stanislav Kinsbursky627f3062012-01-10 17:04:32 +04001176 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04001177 unregister_pernet_subsys(&nfs4blocklayout_net_ops);
Fred Isaman155e7522011-07-30 20:52:39 -04001178 pnfs_unregister_layoutdriver(&blocklayout_type);
Fred Isaman155e7522011-07-30 20:52:39 -04001179}
1180
1181MODULE_ALIAS("nfs-layouttype4-3");
1182
1183module_init(nfs4blocklayout_init);
1184module_exit(nfs4blocklayout_exit);