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