]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/scatterlist.h
Move c_src dirs back to toplevel
[bcachefs-tools-debian] / include / linux / scatterlist.h
1 #ifndef _LINUX_SCATTERLIST_H
2 #define _LINUX_SCATTERLIST_H
3
4 #include <linux/bug.h>
5 #include <linux/slab.h>
6
7 struct scatterlist {
8         unsigned long   page_link;
9         unsigned int    offset;
10         unsigned int    length;
11 };
12
13 #define sg_is_chain(sg)         ((sg)->page_link & 0x01)
14 #define sg_is_last(sg)          ((sg)->page_link & 0x02)
15 #define sg_chain_ptr(sg)        \
16         ((struct scatterlist *) ((sg)->page_link & ~0x03))
17
18 static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
19 {
20         unsigned long page_link = sg->page_link & 0x3;
21
22         /*
23          * In order for the low bit stealing approach to work, pages
24          * must be aligned at a 32-bit boundary as a minimum.
25          */
26         BUG_ON((unsigned long) page & 0x03);
27         sg->page_link = page_link | (unsigned long) page;
28 }
29
30 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
31                                unsigned int len, unsigned int offset)
32 {
33         sg_assign_page(sg, page);
34         sg->offset = offset;
35         sg->length = len;
36 }
37
38 static inline struct page *sg_page(struct scatterlist *sg)
39 {
40         return (struct page *)((sg)->page_link & ~0x3);
41 }
42
43 static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
44                               unsigned int buflen)
45 {
46         sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
47 }
48
49 static inline struct scatterlist *sg_next(struct scatterlist *sg)
50 {
51         if (sg_is_last(sg))
52                 return NULL;
53
54         sg++;
55         if (unlikely(sg_is_chain(sg)))
56                 sg = sg_chain_ptr(sg);
57
58         return sg;
59 }
60
61 #define for_each_sg(sglist, sg, nr, __i)        \
62         for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
63
64 static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
65                             struct scatterlist *sgl)
66 {
67         /*
68          * offset and length are unused for chain entry.  Clear them.
69          */
70         prv[prv_nents - 1].offset = 0;
71         prv[prv_nents - 1].length = 0;
72
73         /*
74          * Set lowest bit to indicate a link pointer, and make sure to clear
75          * the termination bit if it happens to be set.
76          */
77         prv[prv_nents - 1].page_link = ((unsigned long) sgl | 0x01) & ~0x02;
78 }
79
80 static inline void sg_mark_end(struct scatterlist *sg)
81 {
82         sg->page_link |= 0x02;
83         sg->page_link &= ~0x01;
84 }
85
86 static inline void sg_unmark_end(struct scatterlist *sg)
87 {
88         sg->page_link &= ~0x02;
89 }
90
91 static inline void *sg_virt(struct scatterlist *sg)
92 {
93         return page_address(sg_page(sg)) + sg->offset;
94 }
95
96 static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
97 {
98         memset(sgl, 0, sizeof(*sgl) * nents);
99         sg_mark_end(&sgl[nents - 1]);
100 }
101
102 static inline void sg_init_one(struct scatterlist *sg, const void *buf,
103                                unsigned int buflen)
104 {
105         sg_init_table(sg, 1);
106         sg_set_buf(sg, buf, buflen);
107 }
108
109 #endif /* _LINUX_SCATTERLIST_H */