xref: /linux/net/ceph/pagevec.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
3 
4 #include <linux/module.h>
5 #include <linux/sched.h>
6 #include <linux/slab.h>
7 #include <linux/file.h>
8 #include <linux/namei.h>
9 #include <linux/writeback.h>
10 
11 #include <linux/ceph/libceph.h>
12 
13 void ceph_put_page_vector(struct page **pages, int num_pages, bool dirty)
14 {
15 	int i;
16 
17 	for (i = 0; i < num_pages; i++) {
18 		if (dirty)
19 			set_page_dirty_lock(pages[i]);
20 		put_page(pages[i]);
21 	}
22 	kvfree(pages);
23 }
24 EXPORT_SYMBOL(ceph_put_page_vector);
25 
26 void ceph_release_page_vector(struct page **pages, int num_pages)
27 {
28 	int i;
29 
30 	for (i = 0; i < num_pages; i++)
31 		__free_pages(pages[i], 0);
32 	kfree(pages);
33 }
34 EXPORT_SYMBOL(ceph_release_page_vector);
35 
36 /*
37  * allocate a vector new pages
38  */
39 struct page **ceph_alloc_page_vector(int num_pages, gfp_t flags)
40 {
41 	struct page **pages;
42 	int i;
43 
44 	pages = kmalloc_array(num_pages, sizeof(*pages), flags);
45 	if (!pages)
46 		return ERR_PTR(-ENOMEM);
47 	for (i = 0; i < num_pages; i++) {
48 		pages[i] = __page_cache_alloc(flags);
49 		if (pages[i] == NULL) {
50 			ceph_release_page_vector(pages, i);
51 			return ERR_PTR(-ENOMEM);
52 		}
53 	}
54 	return pages;
55 }
56 EXPORT_SYMBOL(ceph_alloc_page_vector);
57 
58 void ceph_copy_from_page_vector(struct page **pages,
59 				    void *data,
60 				    loff_t off, size_t len)
61 {
62 	int i = 0;
63 	size_t po = off & ~PAGE_MASK;
64 	size_t left = len;
65 
66 	while (left > 0) {
67 		size_t l = min_t(size_t, PAGE_SIZE-po, left);
68 
69 		memcpy(data, page_address(pages[i]) + po, l);
70 		data += l;
71 		left -= l;
72 		po += l;
73 		if (po == PAGE_SIZE) {
74 			po = 0;
75 			i++;
76 		}
77 	}
78 }
79 EXPORT_SYMBOL(ceph_copy_from_page_vector);
80 
81 /*
82  * Zero an extent within a page vector.  Offset is relative to the
83  * start of the first page.
84  */
85 void ceph_zero_page_vector_range(int off, int len, struct page **pages)
86 {
87 	int i = off >> PAGE_SHIFT;
88 
89 	off &= ~PAGE_MASK;
90 
91 	dout("zero_page_vector_page %u~%u\n", off, len);
92 
93 	/* leading partial page? */
94 	if (off) {
95 		int end = min((int)PAGE_SIZE, off + len);
96 		dout("zeroing %d %p head from %d\n", i, pages[i],
97 		     (int)off);
98 		zero_user_segment(pages[i], off, end);
99 		len -= (end - off);
100 		i++;
101 	}
102 	while (len >= PAGE_SIZE) {
103 		dout("zeroing %d %p len=%d\n", i, pages[i], len);
104 		zero_user_segment(pages[i], 0, PAGE_SIZE);
105 		len -= PAGE_SIZE;
106 		i++;
107 	}
108 	/* trailing partial page? */
109 	if (len) {
110 		dout("zeroing %d %p tail to %d\n", i, pages[i], (int)len);
111 		zero_user_segment(pages[i], 0, len);
112 	}
113 }
114 EXPORT_SYMBOL(ceph_zero_page_vector_range);
115