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 <asm/div64.h>
33 #include <liblustre.h>
36 #include <linux/obd_class.h>
37 #include <linux/obd_lov.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;
122 int magic = lsm->lsm_magic;
125 if (lov_off == OBD_OBJECT_EOF) {
126 *obd_off = OBD_OBJECT_EOF;
130 LASSERT(lsm_op_find(magic) != NULL);
131 lsm_op_find(magic)->lsm_stripe_by_index(lsm, &stripeno, &lov_off,
134 /* do_div(a, b) returns a % b, and a = a / b */
135 stripe_off = do_div(lov_off, swidth);
137 this_stripe = stripeno * ssize;
138 if (stripe_off < this_stripe) {
142 stripe_off -= this_stripe;
144 if (stripe_off >= ssize) {
150 *obd_off = lov_off * ssize + stripe_off;
154 /* Given a whole-file size and a stripe number, give the file size which
155 * corresponds to the individual object of that stripe.
157 * This behaves basically in the same was as lov_stripe_offset, except that
158 * file sizes falling before the beginning of a stripe are clamped to the end
159 * of the previous stripe, not the beginning of the next:
162 * ---------------------------------------------------------------------
163 * | 0 | 1 | 2 | 0 | 1 | 2 |
164 * ---------------------------------------------------------------------
166 * if clamped to stripe 2 becomes:
169 * ---------------------------------------------------------------------
170 * | 0 | 1 | 2 | 0 | 1 | 2 |
171 * ---------------------------------------------------------------------
173 obd_off lov_size_to_stripe(struct lov_stripe_md *lsm, obd_off file_size,
176 unsigned long ssize = lsm->lsm_stripe_size;
177 unsigned long swidth, stripe_off, this_stripe;
178 int magic = lsm->lsm_magic;
180 if (file_size == OBD_OBJECT_EOF)
181 return OBD_OBJECT_EOF;
183 LASSERT(lsm_op_find(magic) != NULL);
184 lsm_op_find(magic)->lsm_stripe_by_index(lsm, &stripeno, &file_size,
187 /* do_div(a, b) returns a % b, and a = a / b */
188 stripe_off = do_div(file_size, swidth);
190 this_stripe = stripeno * ssize;
191 if (stripe_off < this_stripe) {
192 /* Move to end of previous stripe, or zero */
200 stripe_off -= this_stripe;
202 if (stripe_off >= ssize) {
203 /* Clamp to end of this stripe */
208 return (file_size * ssize + stripe_off);
211 /* given an extent in an lov and a stripe, calculate the extent of the stripe
212 * that is contained within the lov extent. this returns true if the given
213 * stripe does intersect with the lov extent. */
214 int lov_stripe_intersects(struct lov_stripe_md *lsm, int stripeno,
215 obd_off start, obd_off end,
216 obd_off *obd_start, obd_off *obd_end)
218 int start_side, end_side;
220 start_side = lov_stripe_offset(lsm, start, stripeno, obd_start);
221 end_side = lov_stripe_offset(lsm, end, stripeno, obd_end);
223 CDEBUG(D_INODE, "["LPU64"->"LPU64"] -> [(%d) "LPU64"->"LPU64" (%d)]\n",
224 start, end, start_side, *obd_start, *obd_end, end_side);
226 /* this stripe doesn't intersect the file extent when neither
227 * start or the end intersected the stripe and obd_start and
228 * obd_end got rounded up to the save value. */
229 if (start_side != 0 && end_side != 0 && *obd_start == *obd_end)
232 /* as mentioned in the lov_stripe_offset commentary, end
233 * might have been shifted in the wrong direction. This
234 * happens when an end offset is before the stripe when viewed
235 * through the "mod stripe size" math. we detect it being shifted
236 * in the wrong direction and touch it up.
237 * interestingly, this can't underflow since end must be > start
238 * if we passed through the previous check.
239 * (should we assert for that somewhere?) */
246 /* compute which stripe number "lov_off" will be written into */
247 int lov_stripe_number(struct lov_stripe_md *lsm, obd_off lov_off)
249 unsigned long ssize = lsm->lsm_stripe_size;
250 unsigned long swidth, stripe_off;
251 obd_off offset = lov_off;
252 int magic = lsm->lsm_magic;
254 LASSERT(lsm_op_find(magic) != NULL);
255 lsm_op_find(magic)->lsm_stripe_by_offset(lsm, NULL, &lov_off, &swidth);
257 stripe_off = do_div(lov_off, swidth);
259 return (stripe_off/ssize +
260 lsm_op_find(magic)->lsm_stripe_index_by_offset(lsm, offset));