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