Whamcloud - gitweb
bz-13289 and bz-13315
[fs/lustre-release.git] / lustre / lov / lov_offset.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_LOV
29
30 #ifdef __KERNEL__
31 #include <libcfs/libcfs.h>
32 #else
33 #include <liblustre.h>
34 #endif
35
36 #include <obd_class.h>
37 #include <obd_lov.h>
38
39 #include "lov_internal.h"
40
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,
43                          int stripeno)
44 {
45         unsigned long ssize  = lsm->lsm_stripe_size;
46         unsigned long swidth, stripe_size;
47         int sindex = stripeno;
48         obd_size lov_size;
49         int magic = lsm->lsm_magic;
50         ENTRY;
51
52         if (ost_size == 0)
53                 RETURN(0);
54
55         LASSERT(lsm_op_find(magic) != NULL);
56         lsm_op_find(magic)->lsm_stripe_by_index(lsm, &stripeno, NULL, &swidth);
57  
58         /* do_div(a, b) returns a % b, and a = a / b */
59         stripe_size = do_div(ost_size, ssize);
60         if (stripe_size)
61                 lov_size = ost_size * swidth + stripeno * ssize + stripe_size;
62         else
63                 lov_size = (ost_size - 1) * swidth + (stripeno + 1) * ssize;
64
65         lov_size += lsm_op_find(magic)->lsm_stripe_offset_by_index(lsm, sindex);
66         RETURN(lov_size);
67 }
68
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.
73  *
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:
78  *
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:
82  *
83  *             S                                              E
84  * ---------------------------------------------------------------------
85  * |    0    |     1     |     2     |    0    |     1     |     2     |
86  * ---------------------------------------------------------------------
87  *
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:
90  *
91  *             S         E
92  * -----------------------------------
93  * |    0    |     1     |     2     |
94  * -----------------------------------
95  *
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
98  * the stripe:
99  *
100  *   S                   E
101  * ---------------------------------------------------------------------
102  * |    1    |     1     |     1     |    1    |     1     |     1     |
103  * ---------------------------------------------------------------------
104  *
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.
107  *
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:
112  *
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)
119 {
120         unsigned long ssize  = lsm->lsm_stripe_size;
121         unsigned long swidth, stripe_off, this_stripe;
122         __u64 l_off, s_off;
123         int magic = lsm->lsm_magic;
124         int ret = 0;
125
126         if (lov_off == OBD_OBJECT_EOF) {
127                 *obd_off = OBD_OBJECT_EOF;
128                 return 0;
129         }
130
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);
139         if (s_off < l_off) {
140                 ret = -1;
141                 *obd_off = 0;
142                 return ret;
143         } else if (s_off > l_off) {
144                 ret = 1;
145                 *obd_off = s_off;
146                 return ret;
147         }
148         /*If they are in the same extent, original logic*/
149         lsm_op_find(magic)->lsm_stripe_by_index(lsm, &stripeno, &lov_off,
150                                                 &swidth);
151        
152         /* do_div(a, b) returns a % b, and a = a / b */
153         stripe_off = do_div(lov_off, swidth);
154
155         this_stripe = stripeno * ssize;
156         if (stripe_off < this_stripe) {
157                 stripe_off = 0;
158                 ret = -1;
159         } else {
160                 stripe_off -= this_stripe;
161
162                 if (stripe_off >= ssize) {
163                         stripe_off = ssize;
164                         ret = 1;
165                 }
166         }
167
168         *obd_off = lov_off * ssize + stripe_off;
169         return ret;
170 }
171
172 /* Given a whole-file size and a stripe number, give the file size which
173  * corresponds to the individual object of that stripe.
174  *
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:
178  *
179  *                                               S
180  * ---------------------------------------------------------------------
181  * |    0    |     1     |     2     |    0    |     1     |     2     |
182  * ---------------------------------------------------------------------
183  *
184  * if clamped to stripe 2 becomes:
185  *
186  *                                   S
187  * ---------------------------------------------------------------------
188  * |    0    |     1     |     2     |    0    |     1     |     2     |
189  * ---------------------------------------------------------------------
190  */
191 obd_off lov_size_to_stripe(struct lov_stripe_md *lsm, obd_off file_size,
192                            int stripeno)
193 {
194         unsigned long ssize  = lsm->lsm_stripe_size;
195         unsigned long swidth, stripe_off, this_stripe;
196         int magic = lsm->lsm_magic;
197
198         if (file_size == OBD_OBJECT_EOF)
199                 return OBD_OBJECT_EOF;
200
201         LASSERT(lsm_op_find(magic) != NULL);
202         lsm_op_find(magic)->lsm_stripe_by_index(lsm, &stripeno, &file_size,
203                                                 &swidth);
204
205         /* do_div(a, b) returns a % b, and a = a / b */
206         stripe_off = do_div(file_size, swidth);
207
208         this_stripe = stripeno * ssize;
209         if (stripe_off < this_stripe) {
210                 /* Move to end of previous stripe, or zero */
211                 if (file_size > 0) {
212                         file_size--;
213                         stripe_off = ssize;
214                 } else {
215                         stripe_off = 0;
216                 }
217         } else {
218                 stripe_off -= this_stripe;
219
220                 if (stripe_off >= ssize) {
221                         /* Clamp to end of this stripe */
222                         stripe_off = ssize;
223                 }
224         }
225
226         return (file_size * ssize + stripe_off);
227 }
228
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)
235 {
236         int start_side, end_side;
237
238         start_side = lov_stripe_offset(lsm, start, stripeno, obd_start);
239         end_side = lov_stripe_offset(lsm, end, stripeno, obd_end);
240
241         CDEBUG(D_INODE, "["LPU64"->"LPU64"] -> [(%d) "LPU64"->"LPU64" (%d)]\n",
242                start, end, start_side, *obd_start, *obd_end, end_side);
243
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)
248                 return 0;
249
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?) */
258         if (end_side != 0)
259                 (*obd_end)--;
260
261         return 1;
262 }
263
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)
266 {
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;
271
272         LASSERT(lsm_op_find(magic) != NULL);
273         lsm_op_find(magic)->lsm_stripe_by_offset(lsm, NULL, &lov_off, &swidth);
274
275         stripe_off = do_div(lov_off, swidth);
276
277         return (stripe_off/ssize +
278                 lsm_op_find(magic)->lsm_stripe_index_by_offset(lsm, offset));
279 }