Whamcloud - gitweb
Branch b1_4_mountconf
[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 <asm/div64.h>
32 #else
33 #include <liblustre.h>
34 #endif
35
36 #include <linux/obd_class.h>
37 #include <linux/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         int magic = lsm->lsm_magic;
123         int ret = 0;
124
125         if (lov_off == OBD_OBJECT_EOF) {
126                 *obd_off = OBD_OBJECT_EOF;
127                 return 0;
128         }
129
130         LASSERT(lsm_op_find(magic) != NULL);
131         lsm_op_find(magic)->lsm_stripe_by_index(lsm, &stripeno, &lov_off,
132                                                 &swidth);
133        
134         /* do_div(a, b) returns a % b, and a = a / b */
135         stripe_off = do_div(lov_off, swidth);
136
137         this_stripe = stripeno * ssize;
138         if (stripe_off < this_stripe) {
139                 stripe_off = 0;
140                 ret = -1;
141         } else {
142                 stripe_off -= this_stripe;
143
144                 if (stripe_off >= ssize) {
145                         stripe_off = ssize;
146                         ret = 1;
147                 }
148         }
149
150         *obd_off = lov_off * ssize + stripe_off;
151         return ret;
152 }
153
154 /* Given a whole-file size and a stripe number, give the file size which
155  * corresponds to the individual object of that stripe.
156  *
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:
160  *
161  *                                               S
162  * ---------------------------------------------------------------------
163  * |    0    |     1     |     2     |    0    |     1     |     2     |
164  * ---------------------------------------------------------------------
165  *
166  * if clamped to stripe 2 becomes:
167  *
168  *                                   S
169  * ---------------------------------------------------------------------
170  * |    0    |     1     |     2     |    0    |     1     |     2     |
171  * ---------------------------------------------------------------------
172  */
173 obd_off lov_size_to_stripe(struct lov_stripe_md *lsm, obd_off file_size,
174                            int stripeno)
175 {
176         unsigned long ssize  = lsm->lsm_stripe_size;
177         unsigned long swidth, stripe_off, this_stripe;
178         int magic = lsm->lsm_magic;
179
180         if (file_size == OBD_OBJECT_EOF)
181                 return OBD_OBJECT_EOF;
182
183         LASSERT(lsm_op_find(magic) != NULL);
184         lsm_op_find(magic)->lsm_stripe_by_index(lsm, &stripeno, &file_size,
185                                                 &swidth);
186
187         /* do_div(a, b) returns a % b, and a = a / b */
188         stripe_off = do_div(file_size, swidth);
189
190         this_stripe = stripeno * ssize;
191         if (stripe_off < this_stripe) {
192                 /* Move to end of previous stripe, or zero */
193                 if (file_size > 0) {
194                         file_size--;
195                         stripe_off = ssize;
196                 } else {
197                         stripe_off = 0;
198                 }
199         } else {
200                 stripe_off -= this_stripe;
201
202                 if (stripe_off >= ssize) {
203                         /* Clamp to end of this stripe */
204                         stripe_off = ssize;
205                 }
206         }
207
208         return (file_size * ssize + stripe_off);
209 }
210
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)
217 {
218         int start_side, end_side;
219
220         start_side = lov_stripe_offset(lsm, start, stripeno, obd_start);
221         end_side = lov_stripe_offset(lsm, end, stripeno, obd_end);
222
223         CDEBUG(D_INODE, "["LPU64"->"LPU64"] -> [(%d) "LPU64"->"LPU64" (%d)]\n",
224                start, end, start_side, *obd_start, *obd_end, end_side);
225
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)
230                 return 0;
231
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?) */
240         if (end_side != 0)
241                 (*obd_end)--;
242
243         return 1;
244 }
245
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)
248 {
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;
253
254         LASSERT(lsm_op_find(magic) != NULL);
255         lsm_op_find(magic)->lsm_stripe_by_offset(lsm, NULL, &lov_off, &swidth);
256
257         stripe_off = do_div(lov_off, swidth);
258
259         return (stripe_off/ssize +
260                 lsm_op_find(magic)->lsm_stripe_index_by_offset(lsm, offset));
261 }