1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
6 * This file is part of the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
26 # define EXPORT_SYMTAB
28 #define DEBUG_SUBSYSTEM S_LOV
31 #include <libcfs/libcfs.h>
33 #include <liblustre.h>
36 #include <obd_class.h>
39 #include "lov_internal.h"
41 /* compute object size given "stripeno" and the ost size */
42 obd_size lov_stripe_size(struct lov_stripe_md *lsm, obd_size ost_size,
45 unsigned long ssize = lsm->lsm_stripe_size;
46 unsigned long swidth, stripe_size;
47 int sindex = stripeno;
49 int magic = lsm->lsm_magic;
55 LASSERT(lsm_op_find(magic) != NULL);
56 lsm_op_find(magic)->lsm_stripe_by_index(lsm, &stripeno, NULL, &swidth);
58 /* do_div(a, b) returns a % b, and a = a / b */
59 stripe_size = do_div(ost_size, ssize);
61 lov_size = ost_size * swidth + stripeno * ssize + stripe_size;
63 lov_size = (ost_size - 1) * swidth + (stripeno + 1) * ssize;
65 lov_size += lsm_op_find(magic)->lsm_stripe_offset_by_index(lsm, sindex);
69 /* we have an offset in file backed by an lov and want to find out where
70 * that offset lands in our given stripe of the file. for the easy
71 * case where the offset is within the stripe, we just have to scale the
72 * offset down to make it relative to the stripe instead of the lov.
74 * the harder case is what to do when the offset doesn't intersect the
75 * stripe. callers will want start offsets clamped ahead to the start
76 * of the nearest stripe in the file. end offsets similarly clamped to the
77 * nearest ending byte of a stripe in the file:
79 * all this function does is move offsets to the nearest region of the
80 * stripe, and it does its work "mod" the full length of all the stripes.
81 * consider a file with 3 stripes:
84 * ---------------------------------------------------------------------
85 * | 0 | 1 | 2 | 0 | 1 | 2 |
86 * ---------------------------------------------------------------------
88 * to find stripe 1's offsets for S and E, it divides by the full stripe
89 * width and does its math in the context of a single set of stripes:
92 * -----------------------------------
94 * -----------------------------------
96 * it'll notice that E is outside stripe 1 and clamp it to the end of the
97 * stripe, then multiply it back out by lov_off to give the real offsets in
101 * ---------------------------------------------------------------------
102 * | 1 | 1 | 1 | 1 | 1 | 1 |
103 * ---------------------------------------------------------------------
105 * it would have done similarly and pulled S forward to the start of a 1
106 * stripe if, say, S had landed in a 0 stripe.
108 * this rounding isn't always correct. consider an E lov offset that lands
109 * on a 0 stripe, the "mod stripe width" math will pull it forward to the
110 * start of a 1 stripe, when in fact it wanted to be rounded back to the end
111 * of a previous 1 stripe. this logic is handled by callers and this is why:
113 * this function returns < 0 when the offset was "before" the stripe and
114 * was moved forward to the start of the stripe in question; 0 when it
115 * falls in the stripe and no shifting was done; > 0 when the offset
116 * was outside the stripe and was pulled back to its final byte. */
117 int lov_stripe_offset(struct lov_stripe_md *lsm, obd_off lov_off,
118 int stripeno, obd_off *obd_off)
120 unsigned long ssize = lsm->lsm_stripe_size;
121 unsigned long swidth, stripe_off, this_stripe;
123 int magic = lsm->lsm_magic;
126 if (lov_off == OBD_OBJECT_EOF) {
127 *obd_off = OBD_OBJECT_EOF;
131 LASSERT(lsm_op_find(magic) != NULL);
132 /*It will check whether the lov_off and stripeno
133 *are in the same extent.
134 *1) lov_off extent < stripeno extent, ret = -1, obd_off = 0
135 *2) lov_off extent > stripeno extent, ret = 1,
136 * obd_off = lov_off extent offset*/
137 l_off = lsm_op_find(magic)->lsm_stripe_offset_by_index(lsm, stripeno);
138 s_off = lsm_op_find(magic)->lsm_stripe_offset_by_offset(lsm, lov_off);
143 } else if (s_off > l_off) {
148 /*If they are in the same extent, original logic*/
149 lsm_op_find(magic)->lsm_stripe_by_index(lsm, &stripeno, &lov_off,
152 /* do_div(a, b) returns a % b, and a = a / b */
153 stripe_off = do_div(lov_off, swidth);
155 this_stripe = stripeno * ssize;
156 if (stripe_off < this_stripe) {
160 stripe_off -= this_stripe;
162 if (stripe_off >= ssize) {
168 *obd_off = lov_off * ssize + stripe_off;
172 /* Given a whole-file size and a stripe number, give the file size which
173 * corresponds to the individual object of that stripe.
175 * This behaves basically in the same was as lov_stripe_offset, except that
176 * file sizes falling before the beginning of a stripe are clamped to the end
177 * of the previous stripe, not the beginning of the next:
180 * ---------------------------------------------------------------------
181 * | 0 | 1 | 2 | 0 | 1 | 2 |
182 * ---------------------------------------------------------------------
184 * if clamped to stripe 2 becomes:
187 * ---------------------------------------------------------------------
188 * | 0 | 1 | 2 | 0 | 1 | 2 |
189 * ---------------------------------------------------------------------
191 obd_off lov_size_to_stripe(struct lov_stripe_md *lsm, obd_off file_size,
194 unsigned long ssize = lsm->lsm_stripe_size;
195 unsigned long swidth, stripe_off, this_stripe;
196 int magic = lsm->lsm_magic;
198 if (file_size == OBD_OBJECT_EOF)
199 return OBD_OBJECT_EOF;
201 LASSERT(lsm_op_find(magic) != NULL);
202 lsm_op_find(magic)->lsm_stripe_by_index(lsm, &stripeno, &file_size,
205 /* do_div(a, b) returns a % b, and a = a / b */
206 stripe_off = do_div(file_size, swidth);
208 this_stripe = stripeno * ssize;
209 if (stripe_off < this_stripe) {
210 /* Move to end of previous stripe, or zero */
218 stripe_off -= this_stripe;
220 if (stripe_off >= ssize) {
221 /* Clamp to end of this stripe */
226 return (file_size * ssize + stripe_off);
229 /* given an extent in an lov and a stripe, calculate the extent of the stripe
230 * that is contained within the lov extent. this returns true if the given
231 * stripe does intersect with the lov extent. */
232 int lov_stripe_intersects(struct lov_stripe_md *lsm, int stripeno,
233 obd_off start, obd_off end,
234 obd_off *obd_start, obd_off *obd_end)
236 int start_side, end_side;
238 start_side = lov_stripe_offset(lsm, start, stripeno, obd_start);
239 end_side = lov_stripe_offset(lsm, end, stripeno, obd_end);
241 CDEBUG(D_INODE, "["LPU64"->"LPU64"] -> [(%d) "LPU64"->"LPU64" (%d)]\n",
242 start, end, start_side, *obd_start, *obd_end, end_side);
244 /* this stripe doesn't intersect the file extent when neither
245 * start or the end intersected the stripe and obd_start and
246 * obd_end got rounded up to the save value. */
247 if (start_side != 0 && end_side != 0 && *obd_start == *obd_end)
250 /* as mentioned in the lov_stripe_offset commentary, end
251 * might have been shifted in the wrong direction. This
252 * happens when an end offset is before the stripe when viewed
253 * through the "mod stripe size" math. we detect it being shifted
254 * in the wrong direction and touch it up.
255 * interestingly, this can't underflow since end must be > start
256 * if we passed through the previous check.
257 * (should we assert for that somewhere?) */
264 /* compute which stripe number "lov_off" will be written into */
265 int lov_stripe_number(struct lov_stripe_md *lsm, obd_off lov_off)
267 unsigned long ssize = lsm->lsm_stripe_size;
268 unsigned long swidth, stripe_off;
269 obd_off offset = lov_off;
270 int magic = lsm->lsm_magic;
272 LASSERT(lsm_op_find(magic) != NULL);
273 lsm_op_find(magic)->lsm_stripe_by_offset(lsm, NULL, &lov_off, &swidth);
275 stripe_off = do_div(lov_off, swidth);
277 return (stripe_off/ssize +
278 lsm_op_find(magic)->lsm_stripe_index_by_offset(lsm, offset));