da768923bf7cc02d6b5e9ae327dee08b8e1152ce
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / fs / nfs / blocklayout / blocklayout.c
1 /*
2 * linux/fs/nfs/blocklayout/blocklayout.c
3 *
4 * Module for the NFSv4.1 pNFS block layout driver.
5 *
6 * Copyright (c) 2006 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Andy Adamson <andros@citi.umich.edu>
10 * Fred Isaman <iisaman@umich.edu>
11 *
12 * permission is granted to use, copy, create derivative works and
13 * redistribute this software and such derivative works for any purpose,
14 * so long as the name of the university of michigan is not used in
15 * any advertising or publicity pertaining to the use or distribution
16 * of this software without specific, written prior authorization. if
17 * the above copyright notice or any other identification of the
18 * university of michigan is included in any copy of any portion of
19 * this software, then the disclaimer below must also be included.
20 *
21 * this software is provided as is, without representation from the
22 * university of michigan as to its fitness for any purpose, and without
23 * warranty by the university of michigan of any kind, either express
24 * or implied, including without limitation the implied warranties of
25 * merchantability and fitness for a particular purpose. the regents
26 * of the university of michigan shall not be liable for any damages,
27 * including special, indirect, incidental, or consequential damages,
28 * with respect to any claim arising out or in connection with the use
29 * of the software, even if it has been or is hereafter advised of the
30 * possibility of such damages.
31 */
32
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/mount.h>
36 #include <linux/namei.h>
37 #include <linux/bio.h> /* struct bio */
38 #include <linux/buffer_head.h> /* various write calls */
39 #include <linux/prefetch.h>
40 #include <linux/pagevec.h>
41
42 #include "../pnfs.h"
43 #include "../nfs4session.h"
44 #include "../internal.h"
45 #include "blocklayout.h"
46
47 #define NFSDBG_FACILITY NFSDBG_PNFS_LD
48
49 MODULE_LICENSE("GPL");
50 MODULE_AUTHOR("Andy Adamson <andros@citi.umich.edu>");
51 MODULE_DESCRIPTION("The NFSv4.1 pNFS Block layout driver");
52
53 static void print_page(struct page *page)
54 {
55 dprintk("PRINTPAGE page %p\n", page);
56 dprintk(" PagePrivate %d\n", PagePrivate(page));
57 dprintk(" PageUptodate %d\n", PageUptodate(page));
58 dprintk(" PageError %d\n", PageError(page));
59 dprintk(" PageDirty %d\n", PageDirty(page));
60 dprintk(" PageReferenced %d\n", PageReferenced(page));
61 dprintk(" PageLocked %d\n", PageLocked(page));
62 dprintk(" PageWriteback %d\n", PageWriteback(page));
63 dprintk(" PageMappedToDisk %d\n", PageMappedToDisk(page));
64 dprintk("\n");
65 }
66
67 /* Given the be associated with isect, determine if page data needs to be
68 * initialized.
69 */
70 static int is_hole(struct pnfs_block_extent *be, sector_t isect)
71 {
72 if (be->be_state == PNFS_BLOCK_NONE_DATA)
73 return 1;
74 else if (be->be_state != PNFS_BLOCK_INVALID_DATA)
75 return 0;
76 else
77 return !bl_is_sector_init(be->be_inval, isect);
78 }
79
80 /* Given the be associated with isect, determine if page data can be
81 * written to disk.
82 */
83 static int is_writable(struct pnfs_block_extent *be, sector_t isect)
84 {
85 return (be->be_state == PNFS_BLOCK_READWRITE_DATA ||
86 be->be_state == PNFS_BLOCK_INVALID_DATA);
87 }
88
89 /* The data we are handed might be spread across several bios. We need
90 * to track when the last one is finished.
91 */
92 struct parallel_io {
93 struct kref refcnt;
94 void (*pnfs_callback) (void *data, int num_se);
95 void *data;
96 int bse_count;
97 };
98
99 static inline struct parallel_io *alloc_parallel(void *data)
100 {
101 struct parallel_io *rv;
102
103 rv = kmalloc(sizeof(*rv), GFP_NOFS);
104 if (rv) {
105 rv->data = data;
106 kref_init(&rv->refcnt);
107 rv->bse_count = 0;
108 }
109 return rv;
110 }
111
112 static inline void get_parallel(struct parallel_io *p)
113 {
114 kref_get(&p->refcnt);
115 }
116
117 static void destroy_parallel(struct kref *kref)
118 {
119 struct parallel_io *p = container_of(kref, struct parallel_io, refcnt);
120
121 dprintk("%s enter\n", __func__);
122 p->pnfs_callback(p->data, p->bse_count);
123 kfree(p);
124 }
125
126 static inline void put_parallel(struct parallel_io *p)
127 {
128 kref_put(&p->refcnt, destroy_parallel);
129 }
130
131 static struct bio *
132 bl_submit_bio(int rw, struct bio *bio)
133 {
134 if (bio) {
135 get_parallel(bio->bi_private);
136 dprintk("%s submitting %s bio %u@%llu\n", __func__,
137 rw == READ ? "read" : "write",
138 bio->bi_size, (unsigned long long)bio->bi_sector);
139 submit_bio(rw, bio);
140 }
141 return NULL;
142 }
143
144 static struct bio *bl_alloc_init_bio(int npg, sector_t isect,
145 struct pnfs_block_extent *be,
146 void (*end_io)(struct bio *, int err),
147 struct parallel_io *par)
148 {
149 struct bio *bio;
150
151 npg = min(npg, BIO_MAX_PAGES);
152 bio = bio_alloc(GFP_NOIO, npg);
153 if (!bio && (current->flags & PF_MEMALLOC)) {
154 while (!bio && (npg /= 2))
155 bio = bio_alloc(GFP_NOIO, npg);
156 }
157
158 if (bio) {
159 bio->bi_sector = isect - be->be_f_offset + be->be_v_offset;
160 bio->bi_bdev = be->be_mdev;
161 bio->bi_end_io = end_io;
162 bio->bi_private = par;
163 }
164 return bio;
165 }
166
167 static struct bio *do_add_page_to_bio(struct bio *bio, int npg, int rw,
168 sector_t isect, struct page *page,
169 struct pnfs_block_extent *be,
170 void (*end_io)(struct bio *, int err),
171 struct parallel_io *par,
172 unsigned int offset, int len)
173 {
174 isect = isect + (offset >> SECTOR_SHIFT);
175 dprintk("%s: npg %d rw %d isect %llu offset %u len %d\n", __func__,
176 npg, rw, (unsigned long long)isect, offset, len);
177 retry:
178 if (!bio) {
179 bio = bl_alloc_init_bio(npg, isect, be, end_io, par);
180 if (!bio)
181 return ERR_PTR(-ENOMEM);
182 }
183 if (bio_add_page(bio, page, len, offset) < len) {
184 bio = bl_submit_bio(rw, bio);
185 goto retry;
186 }
187 return bio;
188 }
189
190 static struct bio *bl_add_page_to_bio(struct bio *bio, int npg, int rw,
191 sector_t isect, struct page *page,
192 struct pnfs_block_extent *be,
193 void (*end_io)(struct bio *, int err),
194 struct parallel_io *par)
195 {
196 return do_add_page_to_bio(bio, npg, rw, isect, page, be,
197 end_io, par, 0, PAGE_CACHE_SIZE);
198 }
199
200 /* This is basically copied from mpage_end_io_read */
201 static void bl_end_io_read(struct bio *bio, int err)
202 {
203 struct parallel_io *par = bio->bi_private;
204 struct bio_vec *bvec;
205 int i;
206
207 if (!err)
208 bio_for_each_segment_all(bvec, bio, i)
209 SetPageUptodate(bvec->bv_page);
210
211 if (err) {
212 struct nfs_read_data *rdata = par->data;
213 struct nfs_pgio_header *header = rdata->header;
214
215 if (!header->pnfs_error)
216 header->pnfs_error = -EIO;
217 pnfs_set_lo_fail(header->lseg);
218 }
219 bio_put(bio);
220 put_parallel(par);
221 }
222
223 static void bl_read_cleanup(struct work_struct *work)
224 {
225 struct rpc_task *task;
226 struct nfs_read_data *rdata;
227 dprintk("%s enter\n", __func__);
228 task = container_of(work, struct rpc_task, u.tk_work);
229 rdata = container_of(task, struct nfs_read_data, task);
230 pnfs_ld_read_done(rdata);
231 }
232
233 static void
234 bl_end_par_io_read(void *data, int unused)
235 {
236 struct nfs_read_data *rdata = data;
237
238 rdata->task.tk_status = rdata->header->pnfs_error;
239 INIT_WORK(&rdata->task.u.tk_work, bl_read_cleanup);
240 schedule_work(&rdata->task.u.tk_work);
241 }
242
243 static enum pnfs_try_status
244 bl_read_pagelist(struct nfs_read_data *rdata)
245 {
246 struct nfs_pgio_header *header = rdata->header;
247 int i, hole;
248 struct bio *bio = NULL;
249 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
250 sector_t isect, extent_length = 0;
251 struct parallel_io *par;
252 loff_t f_offset = rdata->args.offset;
253 size_t bytes_left = rdata->args.count;
254 unsigned int pg_offset, pg_len;
255 struct page **pages = rdata->args.pages;
256 int pg_index = rdata->args.pgbase >> PAGE_CACHE_SHIFT;
257 const bool is_dio = (header->dreq != NULL);
258
259 dprintk("%s enter nr_pages %u offset %lld count %u\n", __func__,
260 rdata->pages.npages, f_offset, (unsigned int)rdata->args.count);
261
262 par = alloc_parallel(rdata);
263 if (!par)
264 goto use_mds;
265 par->pnfs_callback = bl_end_par_io_read;
266 /* At this point, we can no longer jump to use_mds */
267
268 isect = (sector_t) (f_offset >> SECTOR_SHIFT);
269 /* Code assumes extents are page-aligned */
270 for (i = pg_index; i < rdata->pages.npages; i++) {
271 if (!extent_length) {
272 /* We've used up the previous extent */
273 bl_put_extent(be);
274 bl_put_extent(cow_read);
275 bio = bl_submit_bio(READ, bio);
276 /* Get the next one */
277 be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg),
278 isect, &cow_read);
279 if (!be) {
280 header->pnfs_error = -EIO;
281 goto out;
282 }
283 extent_length = be->be_length -
284 (isect - be->be_f_offset);
285 if (cow_read) {
286 sector_t cow_length = cow_read->be_length -
287 (isect - cow_read->be_f_offset);
288 extent_length = min(extent_length, cow_length);
289 }
290 }
291
292 if (is_dio) {
293 pg_offset = f_offset & ~PAGE_CACHE_MASK;
294 if (pg_offset + bytes_left > PAGE_CACHE_SIZE)
295 pg_len = PAGE_CACHE_SIZE - pg_offset;
296 else
297 pg_len = bytes_left;
298
299 f_offset += pg_len;
300 bytes_left -= pg_len;
301 isect += (pg_offset >> SECTOR_SHIFT);
302 } else {
303 pg_offset = 0;
304 pg_len = PAGE_CACHE_SIZE;
305 }
306
307 hole = is_hole(be, isect);
308 if (hole && !cow_read) {
309 bio = bl_submit_bio(READ, bio);
310 /* Fill hole w/ zeroes w/o accessing device */
311 dprintk("%s Zeroing page for hole\n", __func__);
312 zero_user_segment(pages[i], pg_offset, pg_len);
313 print_page(pages[i]);
314 SetPageUptodate(pages[i]);
315 } else {
316 struct pnfs_block_extent *be_read;
317
318 be_read = (hole && cow_read) ? cow_read : be;
319 bio = do_add_page_to_bio(bio, rdata->pages.npages - i,
320 READ,
321 isect, pages[i], be_read,
322 bl_end_io_read, par,
323 pg_offset, pg_len);
324 if (IS_ERR(bio)) {
325 header->pnfs_error = PTR_ERR(bio);
326 bio = NULL;
327 goto out;
328 }
329 }
330 isect += (pg_len >> SECTOR_SHIFT);
331 extent_length -= PAGE_CACHE_SECTORS;
332 }
333 if ((isect << SECTOR_SHIFT) >= header->inode->i_size) {
334 rdata->res.eof = 1;
335 rdata->res.count = header->inode->i_size - rdata->args.offset;
336 } else {
337 rdata->res.count = (isect << SECTOR_SHIFT) - rdata->args.offset;
338 }
339 out:
340 bl_put_extent(be);
341 bl_put_extent(cow_read);
342 bl_submit_bio(READ, bio);
343 put_parallel(par);
344 return PNFS_ATTEMPTED;
345
346 use_mds:
347 dprintk("Giving up and using normal NFS\n");
348 return PNFS_NOT_ATTEMPTED;
349 }
350
351 static void mark_extents_written(struct pnfs_block_layout *bl,
352 __u64 offset, __u32 count)
353 {
354 sector_t isect, end;
355 struct pnfs_block_extent *be;
356 struct pnfs_block_short_extent *se;
357
358 dprintk("%s(%llu, %u)\n", __func__, offset, count);
359 if (count == 0)
360 return;
361 isect = (offset & (long)(PAGE_CACHE_MASK)) >> SECTOR_SHIFT;
362 end = (offset + count + PAGE_CACHE_SIZE - 1) & (long)(PAGE_CACHE_MASK);
363 end >>= SECTOR_SHIFT;
364 while (isect < end) {
365 sector_t len;
366 be = bl_find_get_extent(bl, isect, NULL);
367 BUG_ON(!be); /* FIXME */
368 len = min(end, be->be_f_offset + be->be_length) - isect;
369 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
370 se = bl_pop_one_short_extent(be->be_inval);
371 BUG_ON(!se);
372 bl_mark_for_commit(be, isect, len, se);
373 }
374 isect += len;
375 bl_put_extent(be);
376 }
377 }
378
379 static void bl_end_io_write_zero(struct bio *bio, int err)
380 {
381 struct parallel_io *par = bio->bi_private;
382 struct bio_vec *bvec;
383 int i;
384
385 bio_for_each_segment_all(bvec, bio, i) {
386 /* This is the zeroing page we added */
387 end_page_writeback(bvec->bv_page);
388 page_cache_release(bvec->bv_page);
389 }
390
391 if (unlikely(err)) {
392 struct nfs_write_data *data = par->data;
393 struct nfs_pgio_header *header = data->header;
394
395 if (!header->pnfs_error)
396 header->pnfs_error = -EIO;
397 pnfs_set_lo_fail(header->lseg);
398 }
399 bio_put(bio);
400 put_parallel(par);
401 }
402
403 static void bl_end_io_write(struct bio *bio, int err)
404 {
405 struct parallel_io *par = bio->bi_private;
406 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
407 struct nfs_write_data *data = par->data;
408 struct nfs_pgio_header *header = data->header;
409
410 if (!uptodate) {
411 if (!header->pnfs_error)
412 header->pnfs_error = -EIO;
413 pnfs_set_lo_fail(header->lseg);
414 }
415 bio_put(bio);
416 put_parallel(par);
417 }
418
419 /* Function scheduled for call during bl_end_par_io_write,
420 * it marks sectors as written and extends the commitlist.
421 */
422 static void bl_write_cleanup(struct work_struct *work)
423 {
424 struct rpc_task *task;
425 struct nfs_write_data *wdata;
426 dprintk("%s enter\n", __func__);
427 task = container_of(work, struct rpc_task, u.tk_work);
428 wdata = container_of(task, struct nfs_write_data, task);
429 if (likely(!wdata->header->pnfs_error)) {
430 /* Marks for LAYOUTCOMMIT */
431 mark_extents_written(BLK_LSEG2EXT(wdata->header->lseg),
432 wdata->args.offset, wdata->args.count);
433 }
434 pnfs_ld_write_done(wdata);
435 }
436
437 /* Called when last of bios associated with a bl_write_pagelist call finishes */
438 static void bl_end_par_io_write(void *data, int num_se)
439 {
440 struct nfs_write_data *wdata = data;
441
442 if (unlikely(wdata->header->pnfs_error)) {
443 bl_free_short_extents(&BLK_LSEG2EXT(wdata->header->lseg)->bl_inval,
444 num_se);
445 }
446
447 wdata->task.tk_status = wdata->header->pnfs_error;
448 wdata->verf.committed = NFS_FILE_SYNC;
449 INIT_WORK(&wdata->task.u.tk_work, bl_write_cleanup);
450 schedule_work(&wdata->task.u.tk_work);
451 }
452
453 /* FIXME STUB - mark intersection of layout and page as bad, so is not
454 * used again.
455 */
456 static void mark_bad_read(void)
457 {
458 return;
459 }
460
461 /*
462 * map_block: map a requested I/0 block (isect) into an offset in the LVM
463 * block_device
464 */
465 static void
466 map_block(struct buffer_head *bh, sector_t isect, struct pnfs_block_extent *be)
467 {
468 dprintk("%s enter be=%p\n", __func__, be);
469
470 set_buffer_mapped(bh);
471 bh->b_bdev = be->be_mdev;
472 bh->b_blocknr = (isect - be->be_f_offset + be->be_v_offset) >>
473 (be->be_mdev->bd_inode->i_blkbits - SECTOR_SHIFT);
474
475 dprintk("%s isect %llu, bh->b_blocknr %ld, using bsize %Zd\n",
476 __func__, (unsigned long long)isect, (long)bh->b_blocknr,
477 bh->b_size);
478 return;
479 }
480
481 static void
482 bl_read_single_end_io(struct bio *bio, int error)
483 {
484 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
485 struct page *page = bvec->bv_page;
486
487 /* Only one page in bvec */
488 unlock_page(page);
489 }
490
491 static int
492 bl_do_readpage_sync(struct page *page, struct pnfs_block_extent *be,
493 unsigned int offset, unsigned int len)
494 {
495 struct bio *bio;
496 struct page *shadow_page;
497 sector_t isect;
498 char *kaddr, *kshadow_addr;
499 int ret = 0;
500
501 dprintk("%s: offset %u len %u\n", __func__, offset, len);
502
503 shadow_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
504 if (shadow_page == NULL)
505 return -ENOMEM;
506
507 bio = bio_alloc(GFP_NOIO, 1);
508 if (bio == NULL)
509 return -ENOMEM;
510
511 isect = (page->index << PAGE_CACHE_SECTOR_SHIFT) +
512 (offset / SECTOR_SIZE);
513
514 bio->bi_sector = isect - be->be_f_offset + be->be_v_offset;
515 bio->bi_bdev = be->be_mdev;
516 bio->bi_end_io = bl_read_single_end_io;
517
518 lock_page(shadow_page);
519 if (bio_add_page(bio, shadow_page,
520 SECTOR_SIZE, round_down(offset, SECTOR_SIZE)) == 0) {
521 unlock_page(shadow_page);
522 bio_put(bio);
523 return -EIO;
524 }
525
526 submit_bio(READ, bio);
527 wait_on_page_locked(shadow_page);
528 if (unlikely(!test_bit(BIO_UPTODATE, &bio->bi_flags))) {
529 ret = -EIO;
530 } else {
531 kaddr = kmap_atomic(page);
532 kshadow_addr = kmap_atomic(shadow_page);
533 memcpy(kaddr + offset, kshadow_addr + offset, len);
534 kunmap_atomic(kshadow_addr);
535 kunmap_atomic(kaddr);
536 }
537 __free_page(shadow_page);
538 bio_put(bio);
539
540 return ret;
541 }
542
543 static int
544 bl_read_partial_page_sync(struct page *page, struct pnfs_block_extent *be,
545 unsigned int dirty_offset, unsigned int dirty_len,
546 bool full_page)
547 {
548 int ret = 0;
549 unsigned int start, end;
550
551 if (full_page) {
552 start = 0;
553 end = PAGE_CACHE_SIZE;
554 } else {
555 start = round_down(dirty_offset, SECTOR_SIZE);
556 end = round_up(dirty_offset + dirty_len, SECTOR_SIZE);
557 }
558
559 dprintk("%s: offset %u len %d\n", __func__, dirty_offset, dirty_len);
560 if (!be) {
561 zero_user_segments(page, start, dirty_offset,
562 dirty_offset + dirty_len, end);
563 if (start == 0 && end == PAGE_CACHE_SIZE &&
564 trylock_page(page)) {
565 SetPageUptodate(page);
566 unlock_page(page);
567 }
568 return ret;
569 }
570
571 if (start != dirty_offset)
572 ret = bl_do_readpage_sync(page, be, start, dirty_offset - start);
573
574 if (!ret && (dirty_offset + dirty_len < end))
575 ret = bl_do_readpage_sync(page, be, dirty_offset + dirty_len,
576 end - dirty_offset - dirty_len);
577
578 return ret;
579 }
580
581 /* Given an unmapped page, zero it or read in page for COW, page is locked
582 * by caller.
583 */
584 static int
585 init_page_for_write(struct page *page, struct pnfs_block_extent *cow_read)
586 {
587 struct buffer_head *bh = NULL;
588 int ret = 0;
589 sector_t isect;
590
591 dprintk("%s enter, %p\n", __func__, page);
592 BUG_ON(PageUptodate(page));
593 if (!cow_read) {
594 zero_user_segment(page, 0, PAGE_SIZE);
595 SetPageUptodate(page);
596 goto cleanup;
597 }
598
599 bh = alloc_page_buffers(page, PAGE_CACHE_SIZE, 0);
600 if (!bh) {
601 ret = -ENOMEM;
602 goto cleanup;
603 }
604
605 isect = (sector_t) page->index << PAGE_CACHE_SECTOR_SHIFT;
606 map_block(bh, isect, cow_read);
607 if (!bh_uptodate_or_lock(bh))
608 ret = bh_submit_read(bh);
609 if (ret)
610 goto cleanup;
611 SetPageUptodate(page);
612
613 cleanup:
614 if (bh)
615 free_buffer_head(bh);
616 if (ret) {
617 /* Need to mark layout with bad read...should now
618 * just use nfs4 for reads and writes.
619 */
620 mark_bad_read();
621 }
622 return ret;
623 }
624
625 /* Find or create a zeroing page marked being writeback.
626 * Return ERR_PTR on error, NULL to indicate skip this page and page itself
627 * to indicate write out.
628 */
629 static struct page *
630 bl_find_get_zeroing_page(struct inode *inode, pgoff_t index,
631 struct pnfs_block_extent *cow_read)
632 {
633 struct page *page;
634 int locked = 0;
635 page = find_get_page(inode->i_mapping, index);
636 if (page)
637 goto check_page;
638
639 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
640 if (unlikely(!page)) {
641 dprintk("%s oom\n", __func__);
642 return ERR_PTR(-ENOMEM);
643 }
644 locked = 1;
645
646 check_page:
647 /* PageDirty: Other will write this out
648 * PageWriteback: Other is writing this out
649 * PageUptodate: It was read before
650 */
651 if (PageDirty(page) || PageWriteback(page)) {
652 print_page(page);
653 if (locked)
654 unlock_page(page);
655 page_cache_release(page);
656 return NULL;
657 }
658
659 if (!locked) {
660 lock_page(page);
661 locked = 1;
662 goto check_page;
663 }
664 if (!PageUptodate(page)) {
665 /* New page, readin or zero it */
666 init_page_for_write(page, cow_read);
667 }
668 set_page_writeback(page);
669 unlock_page(page);
670
671 return page;
672 }
673
674 static enum pnfs_try_status
675 bl_write_pagelist(struct nfs_write_data *wdata, int sync)
676 {
677 struct nfs_pgio_header *header = wdata->header;
678 int i, ret, npg_zero, pg_index, last = 0;
679 struct bio *bio = NULL;
680 struct pnfs_block_extent *be = NULL, *cow_read = NULL;
681 sector_t isect, last_isect = 0, extent_length = 0;
682 struct parallel_io *par = NULL;
683 loff_t offset = wdata->args.offset;
684 size_t count = wdata->args.count;
685 unsigned int pg_offset, pg_len, saved_len;
686 struct page **pages = wdata->args.pages;
687 struct page *page;
688 pgoff_t index;
689 u64 temp;
690 int npg_per_block =
691 NFS_SERVER(header->inode)->pnfs_blksize >> PAGE_CACHE_SHIFT;
692
693 dprintk("%s enter, %Zu@%lld\n", __func__, count, offset);
694
695 if (header->dreq != NULL &&
696 (!IS_ALIGNED(offset, NFS_SERVER(header->inode)->pnfs_blksize) ||
697 !IS_ALIGNED(count, NFS_SERVER(header->inode)->pnfs_blksize))) {
698 dprintk("pnfsblock nonblock aligned DIO writes. Resend MDS\n");
699 goto out_mds;
700 }
701 /* At this point, wdata->pages is a (sequential) list of nfs_pages.
702 * We want to write each, and if there is an error set pnfs_error
703 * to have it redone using nfs.
704 */
705 par = alloc_parallel(wdata);
706 if (!par)
707 goto out_mds;
708 par->pnfs_callback = bl_end_par_io_write;
709 /* At this point, have to be more careful with error handling */
710
711 isect = (sector_t) ((offset & (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
712 be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg), isect, &cow_read);
713 if (!be || !is_writable(be, isect)) {
714 dprintk("%s no matching extents!\n", __func__);
715 goto out_mds;
716 }
717
718 /* First page inside INVALID extent */
719 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
720 if (likely(!bl_push_one_short_extent(be->be_inval)))
721 par->bse_count++;
722 else
723 goto out_mds;
724 temp = offset >> PAGE_CACHE_SHIFT;
725 npg_zero = do_div(temp, npg_per_block);
726 isect = (sector_t) (((offset - npg_zero * PAGE_CACHE_SIZE) &
727 (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
728 extent_length = be->be_length - (isect - be->be_f_offset);
729
730 fill_invalid_ext:
731 dprintk("%s need to zero %d pages\n", __func__, npg_zero);
732 for (;npg_zero > 0; npg_zero--) {
733 if (bl_is_sector_init(be->be_inval, isect)) {
734 dprintk("isect %llu already init\n",
735 (unsigned long long)isect);
736 goto next_page;
737 }
738 /* page ref released in bl_end_io_write_zero */
739 index = isect >> PAGE_CACHE_SECTOR_SHIFT;
740 dprintk("%s zero %dth page: index %lu isect %llu\n",
741 __func__, npg_zero, index,
742 (unsigned long long)isect);
743 page = bl_find_get_zeroing_page(header->inode, index,
744 cow_read);
745 if (unlikely(IS_ERR(page))) {
746 header->pnfs_error = PTR_ERR(page);
747 goto out;
748 } else if (page == NULL)
749 goto next_page;
750
751 ret = bl_mark_sectors_init(be->be_inval, isect,
752 PAGE_CACHE_SECTORS);
753 if (unlikely(ret)) {
754 dprintk("%s bl_mark_sectors_init fail %d\n",
755 __func__, ret);
756 end_page_writeback(page);
757 page_cache_release(page);
758 header->pnfs_error = ret;
759 goto out;
760 }
761 if (likely(!bl_push_one_short_extent(be->be_inval)))
762 par->bse_count++;
763 else {
764 end_page_writeback(page);
765 page_cache_release(page);
766 header->pnfs_error = -ENOMEM;
767 goto out;
768 }
769 /* FIXME: This should be done in bi_end_io */
770 mark_extents_written(BLK_LSEG2EXT(header->lseg),
771 page->index << PAGE_CACHE_SHIFT,
772 PAGE_CACHE_SIZE);
773
774 bio = bl_add_page_to_bio(bio, npg_zero, WRITE,
775 isect, page, be,
776 bl_end_io_write_zero, par);
777 if (IS_ERR(bio)) {
778 header->pnfs_error = PTR_ERR(bio);
779 bio = NULL;
780 goto out;
781 }
782 next_page:
783 isect += PAGE_CACHE_SECTORS;
784 extent_length -= PAGE_CACHE_SECTORS;
785 }
786 if (last)
787 goto write_done;
788 }
789 bio = bl_submit_bio(WRITE, bio);
790
791 /* Middle pages */
792 pg_index = wdata->args.pgbase >> PAGE_CACHE_SHIFT;
793 for (i = pg_index; i < wdata->pages.npages; i++) {
794 if (!extent_length) {
795 /* We've used up the previous extent */
796 bl_put_extent(be);
797 bl_put_extent(cow_read);
798 bio = bl_submit_bio(WRITE, bio);
799 /* Get the next one */
800 be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg),
801 isect, &cow_read);
802 if (!be || !is_writable(be, isect)) {
803 header->pnfs_error = -EINVAL;
804 goto out;
805 }
806 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
807 if (likely(!bl_push_one_short_extent(
808 be->be_inval)))
809 par->bse_count++;
810 else {
811 header->pnfs_error = -ENOMEM;
812 goto out;
813 }
814 }
815 extent_length = be->be_length -
816 (isect - be->be_f_offset);
817 }
818
819 dprintk("%s offset %lld count %Zu\n", __func__, offset, count);
820 pg_offset = offset & ~PAGE_CACHE_MASK;
821 if (pg_offset + count > PAGE_CACHE_SIZE)
822 pg_len = PAGE_CACHE_SIZE - pg_offset;
823 else
824 pg_len = count;
825
826 saved_len = pg_len;
827 if (be->be_state == PNFS_BLOCK_INVALID_DATA &&
828 !bl_is_sector_init(be->be_inval, isect)) {
829 ret = bl_read_partial_page_sync(pages[i], cow_read,
830 pg_offset, pg_len, true);
831 if (ret) {
832 dprintk("%s bl_read_partial_page_sync fail %d\n",
833 __func__, ret);
834 header->pnfs_error = ret;
835 goto out;
836 }
837
838 ret = bl_mark_sectors_init(be->be_inval, isect,
839 PAGE_CACHE_SECTORS);
840 if (unlikely(ret)) {
841 dprintk("%s bl_mark_sectors_init fail %d\n",
842 __func__, ret);
843 header->pnfs_error = ret;
844 goto out;
845 }
846
847 /* Expand to full page write */
848 pg_offset = 0;
849 pg_len = PAGE_CACHE_SIZE;
850 } else if ((pg_offset & (SECTOR_SIZE - 1)) ||
851 (pg_len & (SECTOR_SIZE - 1))){
852 /* ahh, nasty case. We have to do sync full sector
853 * read-modify-write cycles.
854 */
855 unsigned int saved_offset = pg_offset;
856 ret = bl_read_partial_page_sync(pages[i], be, pg_offset,
857 pg_len, false);
858 pg_offset = round_down(pg_offset, SECTOR_SIZE);
859 pg_len = round_up(saved_offset + pg_len, SECTOR_SIZE)
860 - pg_offset;
861 }
862
863
864 bio = do_add_page_to_bio(bio, wdata->pages.npages - i, WRITE,
865 isect, pages[i], be,
866 bl_end_io_write, par,
867 pg_offset, pg_len);
868 if (IS_ERR(bio)) {
869 header->pnfs_error = PTR_ERR(bio);
870 bio = NULL;
871 goto out;
872 }
873 offset += saved_len;
874 count -= saved_len;
875 isect += PAGE_CACHE_SECTORS;
876 last_isect = isect;
877 extent_length -= PAGE_CACHE_SECTORS;
878 }
879
880 /* Last page inside INVALID extent */
881 if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
882 bio = bl_submit_bio(WRITE, bio);
883 temp = last_isect >> PAGE_CACHE_SECTOR_SHIFT;
884 npg_zero = npg_per_block - do_div(temp, npg_per_block);
885 if (npg_zero < npg_per_block) {
886 last = 1;
887 goto fill_invalid_ext;
888 }
889 }
890
891 write_done:
892 wdata->res.count = wdata->args.count;
893 out:
894 bl_put_extent(be);
895 bl_put_extent(cow_read);
896 bl_submit_bio(WRITE, bio);
897 put_parallel(par);
898 return PNFS_ATTEMPTED;
899 out_mds:
900 bl_put_extent(be);
901 bl_put_extent(cow_read);
902 kfree(par);
903 return PNFS_NOT_ATTEMPTED;
904 }
905
906 /* FIXME - range ignored */
907 static void
908 release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range)
909 {
910 int i;
911 struct pnfs_block_extent *be;
912
913 spin_lock(&bl->bl_ext_lock);
914 for (i = 0; i < EXTENT_LISTS; i++) {
915 while (!list_empty(&bl->bl_extents[i])) {
916 be = list_first_entry(&bl->bl_extents[i],
917 struct pnfs_block_extent,
918 be_node);
919 list_del(&be->be_node);
920 bl_put_extent(be);
921 }
922 }
923 spin_unlock(&bl->bl_ext_lock);
924 }
925
926 static void
927 release_inval_marks(struct pnfs_inval_markings *marks)
928 {
929 struct pnfs_inval_tracking *pos, *temp;
930 struct pnfs_block_short_extent *se, *stemp;
931
932 list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) {
933 list_del(&pos->it_link);
934 kfree(pos);
935 }
936
937 list_for_each_entry_safe(se, stemp, &marks->im_extents, bse_node) {
938 list_del(&se->bse_node);
939 kfree(se);
940 }
941 return;
942 }
943
944 static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
945 {
946 struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
947
948 dprintk("%s enter\n", __func__);
949 release_extents(bl, NULL);
950 release_inval_marks(&bl->bl_inval);
951 kfree(bl);
952 }
953
954 static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode,
955 gfp_t gfp_flags)
956 {
957 struct pnfs_block_layout *bl;
958
959 dprintk("%s enter\n", __func__);
960 bl = kzalloc(sizeof(*bl), gfp_flags);
961 if (!bl)
962 return NULL;
963 spin_lock_init(&bl->bl_ext_lock);
964 INIT_LIST_HEAD(&bl->bl_extents[0]);
965 INIT_LIST_HEAD(&bl->bl_extents[1]);
966 INIT_LIST_HEAD(&bl->bl_commit);
967 INIT_LIST_HEAD(&bl->bl_committing);
968 bl->bl_count = 0;
969 bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT;
970 BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize);
971 return &bl->bl_layout;
972 }
973
974 static void bl_free_lseg(struct pnfs_layout_segment *lseg)
975 {
976 dprintk("%s enter\n", __func__);
977 kfree(lseg);
978 }
979
980 /* We pretty much ignore lseg, and store all data layout wide, so we
981 * can correctly merge.
982 */
983 static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo,
984 struct nfs4_layoutget_res *lgr,
985 gfp_t gfp_flags)
986 {
987 struct pnfs_layout_segment *lseg;
988 int status;
989
990 dprintk("%s enter\n", __func__);
991 lseg = kzalloc(sizeof(*lseg), gfp_flags);
992 if (!lseg)
993 return ERR_PTR(-ENOMEM);
994 status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags);
995 if (status) {
996 /* We don't want to call the full-blown bl_free_lseg,
997 * since on error extents were not touched.
998 */
999 kfree(lseg);
1000 return ERR_PTR(status);
1001 }
1002 return lseg;
1003 }
1004
1005 static void
1006 bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr,
1007 const struct nfs4_layoutcommit_args *arg)
1008 {
1009 dprintk("%s enter\n", __func__);
1010 encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg);
1011 }
1012
1013 static void
1014 bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
1015 {
1016 struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout;
1017
1018 dprintk("%s enter\n", __func__);
1019 clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status);
1020 }
1021
1022 static void free_blk_mountid(struct block_mount_id *mid)
1023 {
1024 if (mid) {
1025 struct pnfs_block_dev *dev, *tmp;
1026
1027 /* No need to take bm_lock as we are last user freeing bm_devlist */
1028 list_for_each_entry_safe(dev, tmp, &mid->bm_devlist, bm_node) {
1029 list_del(&dev->bm_node);
1030 bl_free_block_dev(dev);
1031 }
1032 kfree(mid);
1033 }
1034 }
1035
1036 /* This is mostly copied from the filelayout_get_device_info function.
1037 * It seems much of this should be at the generic pnfs level.
1038 */
1039 static struct pnfs_block_dev *
1040 nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh,
1041 struct nfs4_deviceid *d_id)
1042 {
1043 struct pnfs_device *dev;
1044 struct pnfs_block_dev *rv;
1045 u32 max_resp_sz;
1046 int max_pages;
1047 struct page **pages = NULL;
1048 int i, rc;
1049
1050 /*
1051 * Use the session max response size as the basis for setting
1052 * GETDEVICEINFO's maxcount
1053 */
1054 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
1055 max_pages = nfs_page_array_len(0, max_resp_sz);
1056 dprintk("%s max_resp_sz %u max_pages %d\n",
1057 __func__, max_resp_sz, max_pages);
1058
1059 dev = kmalloc(sizeof(*dev), GFP_NOFS);
1060 if (!dev) {
1061 dprintk("%s kmalloc failed\n", __func__);
1062 return ERR_PTR(-ENOMEM);
1063 }
1064
1065 pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS);
1066 if (pages == NULL) {
1067 kfree(dev);
1068 return ERR_PTR(-ENOMEM);
1069 }
1070 for (i = 0; i < max_pages; i++) {
1071 pages[i] = alloc_page(GFP_NOFS);
1072 if (!pages[i]) {
1073 rv = ERR_PTR(-ENOMEM);
1074 goto out_free;
1075 }
1076 }
1077
1078 memcpy(&dev->dev_id, d_id, sizeof(*d_id));
1079 dev->layout_type = LAYOUT_BLOCK_VOLUME;
1080 dev->pages = pages;
1081 dev->pgbase = 0;
1082 dev->pglen = PAGE_SIZE * max_pages;
1083 dev->mincount = 0;
1084 dev->maxcount = max_resp_sz - nfs41_maxgetdevinfo_overhead;
1085
1086 dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data);
1087 rc = nfs4_proc_getdeviceinfo(server, dev, NULL);
1088 dprintk("%s getdevice info returns %d\n", __func__, rc);
1089 if (rc) {
1090 rv = ERR_PTR(rc);
1091 goto out_free;
1092 }
1093
1094 rv = nfs4_blk_decode_device(server, dev);
1095 out_free:
1096 for (i = 0; i < max_pages; i++)
1097 __free_page(pages[i]);
1098 kfree(pages);
1099 kfree(dev);
1100 return rv;
1101 }
1102
1103 static int
1104 bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
1105 {
1106 struct block_mount_id *b_mt_id = NULL;
1107 struct pnfs_devicelist *dlist = NULL;
1108 struct pnfs_block_dev *bdev;
1109 LIST_HEAD(block_disklist);
1110 int status, i;
1111
1112 dprintk("%s enter\n", __func__);
1113
1114 if (server->pnfs_blksize == 0) {
1115 dprintk("%s Server did not return blksize\n", __func__);
1116 return -EINVAL;
1117 }
1118 b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS);
1119 if (!b_mt_id) {
1120 status = -ENOMEM;
1121 goto out_error;
1122 }
1123 /* Initialize nfs4 block layout mount id */
1124 spin_lock_init(&b_mt_id->bm_lock);
1125 INIT_LIST_HEAD(&b_mt_id->bm_devlist);
1126
1127 dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS);
1128 if (!dlist) {
1129 status = -ENOMEM;
1130 goto out_error;
1131 }
1132 dlist->eof = 0;
1133 while (!dlist->eof) {
1134 status = nfs4_proc_getdevicelist(server, fh, dlist);
1135 if (status)
1136 goto out_error;
1137 dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n",
1138 __func__, dlist->num_devs, dlist->eof);
1139 for (i = 0; i < dlist->num_devs; i++) {
1140 bdev = nfs4_blk_get_deviceinfo(server, fh,
1141 &dlist->dev_id[i]);
1142 if (IS_ERR(bdev)) {
1143 status = PTR_ERR(bdev);
1144 goto out_error;
1145 }
1146 spin_lock(&b_mt_id->bm_lock);
1147 list_add(&bdev->bm_node, &b_mt_id->bm_devlist);
1148 spin_unlock(&b_mt_id->bm_lock);
1149 }
1150 }
1151 dprintk("%s SUCCESS\n", __func__);
1152 server->pnfs_ld_data = b_mt_id;
1153
1154 out_return:
1155 kfree(dlist);
1156 return status;
1157
1158 out_error:
1159 free_blk_mountid(b_mt_id);
1160 goto out_return;
1161 }
1162
1163 static int
1164 bl_clear_layoutdriver(struct nfs_server *server)
1165 {
1166 struct block_mount_id *b_mt_id = server->pnfs_ld_data;
1167
1168 dprintk("%s enter\n", __func__);
1169 free_blk_mountid(b_mt_id);
1170 dprintk("%s RETURNS\n", __func__);
1171 return 0;
1172 }
1173
1174 static bool
1175 is_aligned_req(struct nfs_page *req, unsigned int alignment)
1176 {
1177 return IS_ALIGNED(req->wb_offset, alignment) &&
1178 IS_ALIGNED(req->wb_bytes, alignment);
1179 }
1180
1181 static void
1182 bl_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1183 {
1184 if (pgio->pg_dreq != NULL &&
1185 !is_aligned_req(req, SECTOR_SIZE))
1186 nfs_pageio_reset_read_mds(pgio);
1187 else
1188 pnfs_generic_pg_init_read(pgio, req);
1189 }
1190
1191 static bool
1192 bl_pg_test_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
1193 struct nfs_page *req)
1194 {
1195 if (pgio->pg_dreq != NULL &&
1196 !is_aligned_req(req, SECTOR_SIZE))
1197 return false;
1198
1199 return pnfs_generic_pg_test(pgio, prev, req);
1200 }
1201
1202 /*
1203 * Return the number of contiguous bytes for a given inode
1204 * starting at page frame idx.
1205 */
1206 static u64 pnfs_num_cont_bytes(struct inode *inode, pgoff_t idx)
1207 {
1208 struct address_space *mapping = inode->i_mapping;
1209 pgoff_t end;
1210
1211 /* Optimize common case that writes from 0 to end of file */
1212 end = DIV_ROUND_UP(i_size_read(inode), PAGE_CACHE_SIZE);
1213 if (end != NFS_I(inode)->npages) {
1214 rcu_read_lock();
1215 end = radix_tree_next_hole(&mapping->page_tree, idx + 1, ULONG_MAX);
1216 rcu_read_unlock();
1217 }
1218
1219 if (!end)
1220 return i_size_read(inode) - (idx << PAGE_CACHE_SHIFT);
1221 else
1222 return (end - idx) << PAGE_CACHE_SHIFT;
1223 }
1224
1225 static void
1226 bl_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1227 {
1228 if (pgio->pg_dreq != NULL &&
1229 !is_aligned_req(req, PAGE_CACHE_SIZE)) {
1230 nfs_pageio_reset_write_mds(pgio);
1231 } else {
1232 u64 wb_size;
1233 if (pgio->pg_dreq == NULL)
1234 wb_size = pnfs_num_cont_bytes(pgio->pg_inode,
1235 req->wb_index);
1236 else
1237 wb_size = nfs_dreq_bytes_left(pgio->pg_dreq);
1238
1239 pnfs_generic_pg_init_write(pgio, req, wb_size);
1240 }
1241 }
1242
1243 static bool
1244 bl_pg_test_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
1245 struct nfs_page *req)
1246 {
1247 if (pgio->pg_dreq != NULL &&
1248 !is_aligned_req(req, PAGE_CACHE_SIZE))
1249 return false;
1250
1251 return pnfs_generic_pg_test(pgio, prev, req);
1252 }
1253
1254 static const struct nfs_pageio_ops bl_pg_read_ops = {
1255 .pg_init = bl_pg_init_read,
1256 .pg_test = bl_pg_test_read,
1257 .pg_doio = pnfs_generic_pg_readpages,
1258 };
1259
1260 static const struct nfs_pageio_ops bl_pg_write_ops = {
1261 .pg_init = bl_pg_init_write,
1262 .pg_test = bl_pg_test_write,
1263 .pg_doio = pnfs_generic_pg_writepages,
1264 };
1265
1266 static struct pnfs_layoutdriver_type blocklayout_type = {
1267 .id = LAYOUT_BLOCK_VOLUME,
1268 .name = "LAYOUT_BLOCK_VOLUME",
1269 .owner = THIS_MODULE,
1270 .read_pagelist = bl_read_pagelist,
1271 .write_pagelist = bl_write_pagelist,
1272 .alloc_layout_hdr = bl_alloc_layout_hdr,
1273 .free_layout_hdr = bl_free_layout_hdr,
1274 .alloc_lseg = bl_alloc_lseg,
1275 .free_lseg = bl_free_lseg,
1276 .encode_layoutcommit = bl_encode_layoutcommit,
1277 .cleanup_layoutcommit = bl_cleanup_layoutcommit,
1278 .set_layoutdriver = bl_set_layoutdriver,
1279 .clear_layoutdriver = bl_clear_layoutdriver,
1280 .pg_read_ops = &bl_pg_read_ops,
1281 .pg_write_ops = &bl_pg_write_ops,
1282 };
1283
1284 static const struct rpc_pipe_ops bl_upcall_ops = {
1285 .upcall = rpc_pipe_generic_upcall,
1286 .downcall = bl_pipe_downcall,
1287 .destroy_msg = bl_pipe_destroy_msg,
1288 };
1289
1290 static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb,
1291 struct rpc_pipe *pipe)
1292 {
1293 struct dentry *dir, *dentry;
1294
1295 dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME);
1296 if (dir == NULL)
1297 return ERR_PTR(-ENOENT);
1298 dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe);
1299 dput(dir);
1300 return dentry;
1301 }
1302
1303 static void nfs4blocklayout_unregister_sb(struct super_block *sb,
1304 struct rpc_pipe *pipe)
1305 {
1306 if (pipe->dentry)
1307 rpc_unlink(pipe->dentry);
1308 }
1309
1310 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
1311 void *ptr)
1312 {
1313 struct super_block *sb = ptr;
1314 struct net *net = sb->s_fs_info;
1315 struct nfs_net *nn = net_generic(net, nfs_net_id);
1316 struct dentry *dentry;
1317 int ret = 0;
1318
1319 if (!try_module_get(THIS_MODULE))
1320 return 0;
1321
1322 if (nn->bl_device_pipe == NULL) {
1323 module_put(THIS_MODULE);
1324 return 0;
1325 }
1326
1327 switch (event) {
1328 case RPC_PIPEFS_MOUNT:
1329 dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe);
1330 if (IS_ERR(dentry)) {
1331 ret = PTR_ERR(dentry);
1332 break;
1333 }
1334 nn->bl_device_pipe->dentry = dentry;
1335 break;
1336 case RPC_PIPEFS_UMOUNT:
1337 if (nn->bl_device_pipe->dentry)
1338 nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe);
1339 break;
1340 default:
1341 ret = -ENOTSUPP;
1342 break;
1343 }
1344 module_put(THIS_MODULE);
1345 return ret;
1346 }
1347
1348 static struct notifier_block nfs4blocklayout_block = {
1349 .notifier_call = rpc_pipefs_event,
1350 };
1351
1352 static struct dentry *nfs4blocklayout_register_net(struct net *net,
1353 struct rpc_pipe *pipe)
1354 {
1355 struct super_block *pipefs_sb;
1356 struct dentry *dentry;
1357
1358 pipefs_sb = rpc_get_sb_net(net);
1359 if (!pipefs_sb)
1360 return NULL;
1361 dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe);
1362 rpc_put_sb_net(net);
1363 return dentry;
1364 }
1365
1366 static void nfs4blocklayout_unregister_net(struct net *net,
1367 struct rpc_pipe *pipe)
1368 {
1369 struct super_block *pipefs_sb;
1370
1371 pipefs_sb = rpc_get_sb_net(net);
1372 if (pipefs_sb) {
1373 nfs4blocklayout_unregister_sb(pipefs_sb, pipe);
1374 rpc_put_sb_net(net);
1375 }
1376 }
1377
1378 static int nfs4blocklayout_net_init(struct net *net)
1379 {
1380 struct nfs_net *nn = net_generic(net, nfs_net_id);
1381 struct dentry *dentry;
1382
1383 init_waitqueue_head(&nn->bl_wq);
1384 nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0);
1385 if (IS_ERR(nn->bl_device_pipe))
1386 return PTR_ERR(nn->bl_device_pipe);
1387 dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe);
1388 if (IS_ERR(dentry)) {
1389 rpc_destroy_pipe_data(nn->bl_device_pipe);
1390 return PTR_ERR(dentry);
1391 }
1392 nn->bl_device_pipe->dentry = dentry;
1393 return 0;
1394 }
1395
1396 static void nfs4blocklayout_net_exit(struct net *net)
1397 {
1398 struct nfs_net *nn = net_generic(net, nfs_net_id);
1399
1400 nfs4blocklayout_unregister_net(net, nn->bl_device_pipe);
1401 rpc_destroy_pipe_data(nn->bl_device_pipe);
1402 nn->bl_device_pipe = NULL;
1403 }
1404
1405 static struct pernet_operations nfs4blocklayout_net_ops = {
1406 .init = nfs4blocklayout_net_init,
1407 .exit = nfs4blocklayout_net_exit,
1408 };
1409
1410 static int __init nfs4blocklayout_init(void)
1411 {
1412 int ret;
1413
1414 dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
1415
1416 ret = pnfs_register_layoutdriver(&blocklayout_type);
1417 if (ret)
1418 goto out;
1419
1420 ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block);
1421 if (ret)
1422 goto out_remove;
1423 ret = register_pernet_subsys(&nfs4blocklayout_net_ops);
1424 if (ret)
1425 goto out_notifier;
1426 out:
1427 return ret;
1428
1429 out_notifier:
1430 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
1431 out_remove:
1432 pnfs_unregister_layoutdriver(&blocklayout_type);
1433 return ret;
1434 }
1435
1436 static void __exit nfs4blocklayout_exit(void)
1437 {
1438 dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
1439 __func__);
1440
1441 rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
1442 unregister_pernet_subsys(&nfs4blocklayout_net_ops);
1443 pnfs_unregister_layoutdriver(&blocklayout_type);
1444 }
1445
1446 MODULE_ALIAS("nfs-layouttype4-3");
1447
1448 module_init(nfs4blocklayout_init);
1449 module_exit(nfs4blocklayout_exit);