Merge tag 'iommu-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux
[linux/fpc-iii.git] / net / ceph / striper.c
blob3b3fa75d11894f27f542ae4bb84696d41e6e6b46
1 /* SPDX-License-Identifier: GPL-2.0 */
3 #include <linux/ceph/ceph_debug.h>
5 #include <linux/math64.h>
6 #include <linux/slab.h>
8 #include <linux/ceph/striper.h>
9 #include <linux/ceph/types.h>
12 * Map a file extent to a stripe unit within an object.
13 * Fill in objno, offset into object, and object extent length (i.e. the
14 * number of bytes mapped, less than or equal to @l->stripe_unit).
16 * Example for stripe_count = 3, stripes_per_object = 4:
18 * blockno | 0 3 6 9 | 1 4 7 10 | 2 5 8 11 | 12 15 18 21 | 13 16 19
19 * stripeno | 0 1 2 3 | 0 1 2 3 | 0 1 2 3 | 4 5 6 7 | 4 5 6
20 * stripepos | 0 | 1 | 2 | 0 | 1
21 * objno | 0 | 1 | 2 | 3 | 4
22 * objsetno | 0 | 1
24 void ceph_calc_file_object_mapping(struct ceph_file_layout *l,
25 u64 off, u64 len,
26 u64 *objno, u64 *objoff, u32 *xlen)
28 u32 stripes_per_object = l->object_size / l->stripe_unit;
29 u64 blockno; /* which su in the file (i.e. globally) */
30 u32 blockoff; /* offset into su */
31 u64 stripeno; /* which stripe */
32 u32 stripepos; /* which su in the stripe,
33 which object in the object set */
34 u64 objsetno; /* which object set */
35 u32 objsetpos; /* which stripe in the object set */
37 blockno = div_u64_rem(off, l->stripe_unit, &blockoff);
38 stripeno = div_u64_rem(blockno, l->stripe_count, &stripepos);
39 objsetno = div_u64_rem(stripeno, stripes_per_object, &objsetpos);
41 *objno = objsetno * l->stripe_count + stripepos;
42 *objoff = objsetpos * l->stripe_unit + blockoff;
43 *xlen = min_t(u64, len, l->stripe_unit - blockoff);
45 EXPORT_SYMBOL(ceph_calc_file_object_mapping);
48 * Return the last extent with given objno (@object_extents is sorted
49 * by objno). If not found, return NULL and set @add_pos so that the
50 * new extent can be added with list_add(add_pos, new_ex).
52 static struct ceph_object_extent *
53 lookup_last(struct list_head *object_extents, u64 objno,
54 struct list_head **add_pos)
56 struct list_head *pos;
58 list_for_each_prev(pos, object_extents) {
59 struct ceph_object_extent *ex =
60 list_entry(pos, typeof(*ex), oe_item);
62 if (ex->oe_objno == objno)
63 return ex;
65 if (ex->oe_objno < objno)
66 break;
69 *add_pos = pos;
70 return NULL;
73 static struct ceph_object_extent *
74 lookup_containing(struct list_head *object_extents, u64 objno,
75 u64 objoff, u32 xlen)
77 struct ceph_object_extent *ex;
79 list_for_each_entry(ex, object_extents, oe_item) {
80 if (ex->oe_objno == objno &&
81 ex->oe_off <= objoff &&
82 ex->oe_off + ex->oe_len >= objoff + xlen) /* paranoia */
83 return ex;
85 if (ex->oe_objno > objno)
86 break;
89 return NULL;
93 * Map a file extent to a sorted list of object extents.
95 * We want only one (or as few as possible) object extents per object.
96 * Adjacent object extents will be merged together, each returned object
97 * extent may reverse map to multiple different file extents.
99 * Call @alloc_fn for each new object extent and @action_fn for each
100 * mapped stripe unit, whether it was merged into an already allocated
101 * object extent or started a new object extent.
103 * Newly allocated object extents are added to @object_extents.
104 * To keep @object_extents sorted, successive calls to this function
105 * must map successive file extents (i.e. the list of file extents that
106 * are mapped using the same @object_extents must be sorted).
108 * The caller is responsible for @object_extents.
110 int ceph_file_to_extents(struct ceph_file_layout *l, u64 off, u64 len,
111 struct list_head *object_extents,
112 struct ceph_object_extent *alloc_fn(void *arg),
113 void *alloc_arg,
114 ceph_object_extent_fn_t action_fn,
115 void *action_arg)
117 struct ceph_object_extent *last_ex, *ex;
119 while (len) {
120 struct list_head *add_pos = NULL;
121 u64 objno, objoff;
122 u32 xlen;
124 ceph_calc_file_object_mapping(l, off, len, &objno, &objoff,
125 &xlen);
127 last_ex = lookup_last(object_extents, objno, &add_pos);
128 if (!last_ex || last_ex->oe_off + last_ex->oe_len != objoff) {
129 ex = alloc_fn(alloc_arg);
130 if (!ex)
131 return -ENOMEM;
133 ex->oe_objno = objno;
134 ex->oe_off = objoff;
135 ex->oe_len = xlen;
136 if (action_fn)
137 action_fn(ex, xlen, action_arg);
139 if (!last_ex)
140 list_add(&ex->oe_item, add_pos);
141 else
142 list_add(&ex->oe_item, &last_ex->oe_item);
143 } else {
144 last_ex->oe_len += xlen;
145 if (action_fn)
146 action_fn(last_ex, xlen, action_arg);
149 off += xlen;
150 len -= xlen;
153 for (last_ex = list_first_entry(object_extents, typeof(*ex), oe_item),
154 ex = list_next_entry(last_ex, oe_item);
155 &ex->oe_item != object_extents;
156 last_ex = ex, ex = list_next_entry(ex, oe_item)) {
157 if (last_ex->oe_objno > ex->oe_objno ||
158 (last_ex->oe_objno == ex->oe_objno &&
159 last_ex->oe_off + last_ex->oe_len >= ex->oe_off)) {
160 WARN(1, "%s: object_extents list not sorted!\n",
161 __func__);
162 return -EINVAL;
166 return 0;
168 EXPORT_SYMBOL(ceph_file_to_extents);
171 * A stripped down, non-allocating version of ceph_file_to_extents(),
172 * for when @object_extents is already populated.
174 int ceph_iterate_extents(struct ceph_file_layout *l, u64 off, u64 len,
175 struct list_head *object_extents,
176 ceph_object_extent_fn_t action_fn,
177 void *action_arg)
179 while (len) {
180 struct ceph_object_extent *ex;
181 u64 objno, objoff;
182 u32 xlen;
184 ceph_calc_file_object_mapping(l, off, len, &objno, &objoff,
185 &xlen);
187 ex = lookup_containing(object_extents, objno, objoff, xlen);
188 if (!ex) {
189 WARN(1, "%s: objno %llu %llu~%u not found!\n",
190 __func__, objno, objoff, xlen);
191 return -EINVAL;
194 action_fn(ex, xlen, action_arg);
196 off += xlen;
197 len -= xlen;
200 return 0;
202 EXPORT_SYMBOL(ceph_iterate_extents);
205 * Reverse map an object extent to a sorted list of file extents.
207 * On success, the caller is responsible for:
209 * kfree(file_extents)
211 int ceph_extent_to_file(struct ceph_file_layout *l,
212 u64 objno, u64 objoff, u64 objlen,
213 struct ceph_file_extent **file_extents,
214 u32 *num_file_extents)
216 u32 stripes_per_object = l->object_size / l->stripe_unit;
217 u64 blockno; /* which su */
218 u32 blockoff; /* offset into su */
219 u64 stripeno; /* which stripe */
220 u32 stripepos; /* which su in the stripe,
221 which object in the object set */
222 u64 objsetno; /* which object set */
223 u32 i = 0;
225 if (!objlen) {
226 *file_extents = NULL;
227 *num_file_extents = 0;
228 return 0;
231 *num_file_extents = DIV_ROUND_UP_ULL(objoff + objlen, l->stripe_unit) -
232 DIV_ROUND_DOWN_ULL(objoff, l->stripe_unit);
233 *file_extents = kmalloc_array(*num_file_extents, sizeof(**file_extents),
234 GFP_NOIO);
235 if (!*file_extents)
236 return -ENOMEM;
238 div_u64_rem(objoff, l->stripe_unit, &blockoff);
239 while (objlen) {
240 u64 off, len;
242 objsetno = div_u64_rem(objno, l->stripe_count, &stripepos);
243 stripeno = div_u64(objoff, l->stripe_unit) +
244 objsetno * stripes_per_object;
245 blockno = stripeno * l->stripe_count + stripepos;
246 off = blockno * l->stripe_unit + blockoff;
247 len = min_t(u64, objlen, l->stripe_unit - blockoff);
249 (*file_extents)[i].fe_off = off;
250 (*file_extents)[i].fe_len = len;
252 blockoff = 0;
253 objoff += len;
254 objlen -= len;
255 i++;
258 BUG_ON(i != *num_file_extents);
259 return 0;
261 EXPORT_SYMBOL(ceph_extent_to_file);
263 u64 ceph_get_num_objects(struct ceph_file_layout *l, u64 size)
265 u64 period = (u64)l->stripe_count * l->object_size;
266 u64 num_periods = DIV64_U64_ROUND_UP(size, period);
267 u64 remainder_bytes;
268 u64 remainder_objs = 0;
270 div64_u64_rem(size, period, &remainder_bytes);
271 if (remainder_bytes > 0 &&
272 remainder_bytes < (u64)l->stripe_count * l->stripe_unit)
273 remainder_objs = l->stripe_count -
274 DIV_ROUND_UP_ULL(remainder_bytes, l->stripe_unit);
276 return num_periods * l->stripe_count - remainder_objs;
278 EXPORT_SYMBOL(ceph_get_num_objects);