xref: /openbmc/linux/drivers/md/raid1-10.c (revision 6aa7de05)
1 /* Maximum size of each resync request */
2 #define RESYNC_BLOCK_SIZE (64*1024)
3 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
4 
5 /* for managing resync I/O pages */
6 struct resync_pages {
7 	void		*raid_bio;
8 	struct page	*pages[RESYNC_PAGES];
9 };
10 
11 static inline int resync_alloc_pages(struct resync_pages *rp,
12 				     gfp_t gfp_flags)
13 {
14 	int i;
15 
16 	for (i = 0; i < RESYNC_PAGES; i++) {
17 		rp->pages[i] = alloc_page(gfp_flags);
18 		if (!rp->pages[i])
19 			goto out_free;
20 	}
21 
22 	return 0;
23 
24 out_free:
25 	while (--i >= 0)
26 		put_page(rp->pages[i]);
27 	return -ENOMEM;
28 }
29 
30 static inline void resync_free_pages(struct resync_pages *rp)
31 {
32 	int i;
33 
34 	for (i = 0; i < RESYNC_PAGES; i++)
35 		put_page(rp->pages[i]);
36 }
37 
38 static inline void resync_get_all_pages(struct resync_pages *rp)
39 {
40 	int i;
41 
42 	for (i = 0; i < RESYNC_PAGES; i++)
43 		get_page(rp->pages[i]);
44 }
45 
46 static inline struct page *resync_fetch_page(struct resync_pages *rp,
47 					     unsigned idx)
48 {
49 	if (WARN_ON_ONCE(idx >= RESYNC_PAGES))
50 		return NULL;
51 	return rp->pages[idx];
52 }
53 
54 /*
55  * 'strct resync_pages' stores actual pages used for doing the resync
56  *  IO, and it is per-bio, so make .bi_private points to it.
57  */
58 static inline struct resync_pages *get_resync_pages(struct bio *bio)
59 {
60 	return bio->bi_private;
61 }
62 
63 /* generally called after bio_reset() for reseting bvec */
64 static void md_bio_reset_resync_pages(struct bio *bio, struct resync_pages *rp,
65 			       int size)
66 {
67 	int idx = 0;
68 
69 	/* initialize bvec table again */
70 	do {
71 		struct page *page = resync_fetch_page(rp, idx);
72 		int len = min_t(int, size, PAGE_SIZE);
73 
74 		/*
75 		 * won't fail because the vec table is big
76 		 * enough to hold all these pages
77 		 */
78 		bio_add_page(bio, page, len, 0);
79 		size -= len;
80 	} while (idx++ < RESYNC_PAGES && size > 0);
81 }
82